1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.jsieve;
21  
22  import junit.framework.TestCase;
23  
24  import org.apache.jsieve.commands.ThrowTestException;
25  import org.apache.jsieve.exception.SieveException;
26  import org.apache.jsieve.parser.generated.ParseException;
27  import org.apache.jsieve.utils.JUnitUtils;
28  import org.apache.jsieve.utils.SieveEnvelopeMailAdapter;
29  
30  /**
31   * Class EnvelopeTest
32   */
33  public class EnvelopeTest extends TestCase {
34  
35      /**
36       * Test for Test 'envelope'
37       */
38      public void testIfEnvelopeAllIsTrue() {
39          boolean isTestPassed = false;
40          String script = "if envelope :all :is \"From\" \"user@domain\" {throwTestException;}";
41          try {
42              SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
43              mail.setEnvelopeFrom("user@domain");
44              JUnitUtils.interpret(mail, script);
45          } catch (ThrowTestException.TestException e) {
46              isTestPassed = true;
47          } catch (ParseException e) {
48          } catch (SieveException e) {
49          }
50          assertTrue(isTestPassed);
51      }
52  
53      /**
54       * Test for Test 'envelope'
55       */
56      public void testCaseInsensitiveEnvelopeName() {
57          boolean isTestPassed = false;
58          String script = "if envelope :all :is \"from\" \"user@domain\" {throwTestException;}";
59          try {
60              SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
61              mail.setEnvelopeFrom("user@domain");
62              JUnitUtils.interpret(mail, script);
63          } catch (ThrowTestException.TestException e) {
64              isTestPassed = true;
65          } catch (ParseException e) {
66          } catch (SieveException e) {
67          }
68          assertTrue(isTestPassed);
69      }
70  
71      /**
72       * Test for Test 'envelope'
73       */
74      public void testOctetComparatorTrue() {
75          boolean isTestPassed = false;
76          String script = "if envelope :comparator \"i;octet\" :all :is \"From\" \"uSeR@dOmAiN\" {throwTestException;}";
77          try {
78              SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
79              mail.setEnvelopeFrom("uSeR@dOmAiN");
80              JUnitUtils.interpret(mail, script);
81          } catch (ThrowTestException.TestException e) {
82              isTestPassed = true;
83          } catch (ParseException e) {
84          } catch (SieveException e) {
85          }
86          assertTrue(isTestPassed);
87      }
88  
89      /**
90       * Test for Test 'envelope'
91       */
92      public void testOctetComparatorFalse() {
93          boolean isTestPassed = false;
94          String script = "if envelope :comparator \"i;octet\" :all :is \"From\" \"uSeR@dOmAiN\" {throwTestException;}";
95          try {
96              SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
97              mail.setEnvelopeFrom("user@domain");
98              JUnitUtils.interpret(mail, script);
99              isTestPassed = true;
100         } catch (ThrowTestException.TestException e) {
101         } catch (ParseException e) {
102         } catch (SieveException e) {
103         }
104         assertTrue(isTestPassed);
105     }
106 
107     /**
108      * Test for Test 'envelope'
109      */
110     public void testAsciiComparatorTrue() {
111         boolean isTestPassed = false;
112         String script = "if envelope :comparator \"i;ascii-casemap\" :all :is \"From\" \"uSeR@dOmAiN\" {throwTestException;}";
113         try {
114             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
115             mail.setEnvelopeFrom("user@domain");
116             JUnitUtils.interpret(mail, script);
117         } catch (ThrowTestException.TestException e) {
118             isTestPassed = true;
119         } catch (ParseException e) {
120         } catch (SieveException e) {
121         }
122         assertTrue(isTestPassed);
123     }
124 
125     /**
126      * Test for Test 'envelope'
127      */
128     public void testAsciiComparatorFalse() {
129         boolean isTestPassed = false;
130         String script = "if envelope :comparator \"i;ascii-casemap\" :all :is \"From\" \"uSeR@dOmAiN\" {throwTestException;}";
131         try {
132             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
133             mail.setEnvelopeFrom("user@domain1");
134             JUnitUtils.interpret(mail, script);
135             isTestPassed = true;
136         } catch (ThrowTestException.TestException e) {
137         } catch (ParseException e) {
138         } catch (SieveException e) {
139         }
140         assertTrue(isTestPassed);
141     }
142 
143     /**
144      * Test for Test 'envelope'
145      */
146     public void testIfEnvelopeAllIsMultiTrue1() {
147         boolean isTestPassed = false;
148         String script = "if envelope :all :is [\"From\", \"To\"] \"user@domain\" {throwTestException;}";
149         try {
150             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
151             mail.setEnvelopeFrom("user@domain");
152             mail.setEnvelopeTo("user@domain");
153             JUnitUtils.interpret(mail, script);
154         } catch (ThrowTestException.TestException e) {
155             isTestPassed = true;
156         } catch (ParseException e) {
157         } catch (SieveException e) {
158         }
159         assertTrue(isTestPassed);
160     }
161 
162     /**
163      * Test for Test 'envelope'
164      */
165     public void testIfEnvelopeAllIsMultiTrue2() {
166         boolean isTestPassed = false;
167         String script = "if envelope :all :is [\"From\", \"To\"] [\"user@domain\", \"tweety@pie\"] {throwTestException;}";
168         try {
169             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
170             mail.setEnvelopeFrom("user@domain");
171             mail.setEnvelopeTo("user@domain");
172             JUnitUtils.interpret(mail, script);
173         } catch (ThrowTestException.TestException e) {
174             isTestPassed = true;
175         } catch (ParseException e) {
176         } catch (SieveException e) {
177         }
178         assertTrue(isTestPassed);
179     }
180 
181     /**
182      * Test for Test 'envelope'
183      */
184     public void testIfEnvelopeAllIsMultiTrue3() {
185         boolean isTestPassed = false;
186         String script = "if envelope :all :is [\"From\", \"To\"] [\"user@domain\", \"tweety@pie\"] {throwTestException;}";
187         try {
188             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
189             mail.setEnvelopeFrom("user@domain");
190             mail.setEnvelopeTo("tweety@pie");
191             JUnitUtils.interpret(mail, script);
192         } catch (ThrowTestException.TestException e) {
193             isTestPassed = true;
194         } catch (ParseException e) {
195         } catch (SieveException e) {
196         }
197         assertTrue(isTestPassed);
198     }
199 
200     /**
201      * Test for Test 'envelope'
202      */
203     public void testIfEnvelopeAllIsMultiTrue4() {
204         boolean isTestPassed = false;
205         String script = "if envelope :all :is [\"From\", \"To\"] [\"user@domain\", \"tweety@pie\"] {throwTestException;}";
206         try {
207             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
208             mail.setEnvelopeFrom("tweety@pie");
209             mail.setEnvelopeTo("tweety@pie");
210             JUnitUtils.interpret(mail, script);
211         } catch (ThrowTestException.TestException e) {
212             isTestPassed = true;
213         } catch (ParseException e) {
214         } catch (SieveException e) {
215         }
216         assertTrue(isTestPassed);
217     }
218 
219     /**
220      * Test for Test 'envelope'
221      */
222     public void testIfEnvelopeAllMatchesTrue() {
223         boolean isTestPassed = false;
224         String script = "if envelope :all :matches \"From\" \"*@domain\" {throwTestException;}";
225         try {
226             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
227             mail.setEnvelopeFrom("user@domain");
228             JUnitUtils.interpret(mail, script);
229         } catch (ThrowTestException.TestException e) {
230             isTestPassed = true;
231         } catch (ParseException e) {
232         } catch (SieveException e) {
233         }
234         assertTrue(isTestPassed);
235     }
236 
237     /**
238      * Test for Test 'envelope'
239      */
240     public void testIfEnvelopeAllContainsTrue() {
241         boolean isTestPassed = false;
242         String script = "if envelope :all :contains \"From\" \"r@dom\" {throwTestException;}";
243         try {
244             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
245             mail.setEnvelopeFrom("user@domain");
246             JUnitUtils.interpret(mail, script);
247         } catch (ThrowTestException.TestException e) {
248             isTestPassed = true;
249         } catch (ParseException e) {
250         } catch (SieveException e) {
251         }
252         assertTrue(isTestPassed);
253     }
254 
255     /**
256      * Test for Test 'envelope'
257      */
258     public void testIfEnvelopeLocalpartIsTrue() {
259         boolean isTestPassed = false;
260         String script = "if envelope :localpart :is \"From\" \"user\" {throwTestException;}";
261         try {
262             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
263             mail.setEnvelopeFrom("user@domain");
264             JUnitUtils.interpret(mail, script);
265         } catch (ThrowTestException.TestException e) {
266             isTestPassed = true;
267         } catch (ParseException e) {
268         } catch (SieveException e) {
269         }
270         assertTrue(isTestPassed);
271     }
272 
273     /**
274      * Test for Test 'envelope'
275      */
276     public void testIfEnvelopeLocalpartMatchesTrue() {
277         boolean isTestPassed = false;
278         String script = "if envelope :localpart :matches \"From\" \"*er\" {throwTestException;}";
279         try {
280             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
281             mail.setEnvelopeFrom("user@domain");
282             JUnitUtils.interpret(mail, script);
283         } catch (ThrowTestException.TestException e) {
284             isTestPassed = true;
285         } catch (ParseException e) {
286         } catch (SieveException e) {
287         }
288         assertTrue(isTestPassed);
289     }
290 
291     /**
292      * Test for Test 'envelope'
293      */
294     public void testIfEnvelopeLocalpartContainsTrue() {
295         boolean isTestPassed = false;
296         String script = "if envelope :localpart :contains \"From\" \"r\" {throwTestException;}";
297         try {
298             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
299             mail.setEnvelopeFrom("user@domain");
300             JUnitUtils.interpret(mail, script);
301         } catch (ThrowTestException.TestException e) {
302             isTestPassed = true;
303         } catch (ParseException e) {
304         } catch (SieveException e) {
305         }
306         assertTrue(isTestPassed);
307     }
308 
309     /**
310      * Test for Test 'envelope'
311      */
312     public void testIfEnvelopeDomainIsTrue() {
313         boolean isTestPassed = false;
314         String script = "if envelope :domain :is \"From\" \"domain\" {throwTestException;}";
315 
316         try {
317             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
318             mail.setEnvelopeFrom("user@domain");
319             JUnitUtils.interpret(mail, script);
320         } catch (ThrowTestException.TestException e) {
321             isTestPassed = true;
322         } catch (ParseException e) {
323         } catch (SieveException e) {
324         }
325         assertTrue(isTestPassed);
326     }
327 
328     /**
329      * Test for Test 'envelope'
330      */
331     public void testIfEnvelopeDomainMatchesTrue() {
332         boolean isTestPassed = false;
333         String script = "if envelope :domain :matches \"From\" \"*main\" {throwTestException;}";
334         try {
335             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
336             mail.setEnvelopeFrom("user@domain");
337             JUnitUtils.interpret(mail, script);
338         } catch (ThrowTestException.TestException e) {
339             isTestPassed = true;
340         } catch (ParseException e) {
341         } catch (SieveException e) {
342         }
343         assertTrue(isTestPassed);
344     }
345 
346     /**
347      * Test for Test 'envelope'
348      */
349     public void testIfEnvelopeDomainContainsTrue() {
350         boolean isTestPassed = false;
351         String script = "if envelope :domain :contains \"From\" \"dom\" {throwTestException;}";
352         try {
353             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
354             mail.setEnvelopeFrom("user@domain");
355             JUnitUtils.interpret(mail, script);
356         } catch (ThrowTestException.TestException e) {
357             isTestPassed = true;
358         } catch (ParseException e) {
359         } catch (SieveException e) {
360         }
361         assertTrue(isTestPassed);
362     }
363 
364     /**
365      * Test for Test 'envelope'
366      */
367     public void testIfEnvelopeAllIsFalse() {
368         boolean isTestPassed = false;
369         String script = "if envelope :all :is \"From\" \"user@domain\" {throwTestException;}";
370         try {
371             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
372             mail.setEnvelopeFrom("tweety@pie");
373             JUnitUtils.interpret(mail, script);
374             isTestPassed = true;
375         } catch (ThrowTestException.TestException e) {
376         } catch (ParseException e) {
377         } catch (SieveException e) {
378         }
379         assertTrue(isTestPassed);
380     }
381 
382     /**
383      * Test for Test 'envelope'
384      */
385     public void testIfEnvelopeAllMatchesFalse() {
386         boolean isTestPassed = false;
387         String script = "if envelope :all :matches \"From\" \"(.*)@domain\" {throwTestException;}";
388         try {
389             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
390             mail.setEnvelopeFrom("bugs@bunny");
391             JUnitUtils.interpret(mail, script);
392             isTestPassed = true;
393         } catch (ThrowTestException.TestException e) {
394         } catch (ParseException e) {
395         } catch (SieveException e) {
396         }
397         assertTrue(isTestPassed);
398     }
399 
400     /**
401      * Test for Test 'envelope'
402      */
403     public void testIfEnvelopeAllContainsFalse() {
404         boolean isTestPassed = false;
405         String script = "if envelope :all :contains \"From\" \"r@dom\" {throwTestException;}";
406         try {
407             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
408             mail.setEnvelopeFrom("tweety@pie");
409             JUnitUtils.interpret(mail, script);
410             isTestPassed = true;
411         } catch (ThrowTestException.TestException e) {
412         } catch (ParseException e) {
413         } catch (SieveException e) {
414         }
415         assertTrue(isTestPassed);
416     }
417 
418     /**
419      * Test for Test 'envelope'
420      */
421     public void testIfEnvelopeLocalpartIsFalse() {
422         boolean isTestPassed = false;
423         String script = "if envelope :localpart :is \"From\" \"user\" {throwTestException;}";
424         try {
425             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
426             mail.setEnvelopeFrom("tweety@pie");
427             JUnitUtils.interpret(mail, script);
428             isTestPassed = true;
429         } catch (ThrowTestException.TestException e) {
430         } catch (ParseException e) {
431         } catch (SieveException e) {
432         }
433         assertTrue(isTestPassed);
434     }
435 
436     /**
437      * Test for Test 'envelope'
438      */
439     public void testIfEnvelopeLocalpartMatchesFalse() {
440         boolean isTestPassed = false;
441         String script = "if envelope :localpart :matches \"From\" \"(.*)er\" {throwTestException;}";
442         try {
443             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
444             mail.setEnvelopeFrom("tweety@pie");
445             JUnitUtils.interpret(mail, script);
446             isTestPassed = true;
447         } catch (ThrowTestException.TestException e) {
448         } catch (ParseException e) {
449         } catch (SieveException e) {
450         }
451         assertTrue(isTestPassed);
452     }
453 
454     /**
455      * Test for Test 'envelope'
456      */
457     public void testIfEnvelopeLocalpartContainsFalse() {
458         boolean isTestPassed = false;
459         String script = "if envelope :localpart :contains \"From\" \"r\" {throwTestException;}";
460         try {
461             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
462             mail.setEnvelopeFrom("tweety@pie");
463             JUnitUtils.interpret(mail, script);
464             isTestPassed = true;
465         } catch (ThrowTestException.TestException e) {
466         } catch (ParseException e) {
467         } catch (SieveException e) {
468         }
469         assertTrue(isTestPassed);
470     }
471 
472     /**
473      * Test for Test 'envelope'
474      */
475     public void testIfEnvelopeDomainIsFalse() {
476         boolean isTestPassed = false;
477         String script = "if envelope :domain :is \"From\" \"domain\" {throwTestException;}";
478 
479         try {
480             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
481             mail.setEnvelopeFrom("tweety@pie");
482             JUnitUtils.interpret(mail, script);
483             isTestPassed = true;
484         } catch (ThrowTestException.TestException e) {
485         } catch (ParseException e) {
486         } catch (SieveException e) {
487         }
488         assertTrue(isTestPassed);
489     }
490 
491     /**
492      * Test for Test 'envelope'
493      */
494     public void testIfEnvelopeDomainMatchesFalse() {
495         boolean isTestPassed = false;
496         String script = "if envelope :domain :matches \"From\" \"(.*)main\" {throwTestException;}";
497         try {
498             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
499             mail.setEnvelopeFrom("tweety@pie");
500             JUnitUtils.interpret(mail, script);
501             isTestPassed = true;
502         } catch (ThrowTestException.TestException e) {
503         } catch (ParseException e) {
504         } catch (SieveException e) {
505         }
506         assertTrue(isTestPassed);
507     }
508 
509     /**
510      * Test for Test 'envelope'
511      */
512     public void testIfEnvelopeDomainContainsFalse() {
513         boolean isTestPassed = false;
514         String script = "if envelope :domain :contains \"From\" \"dom\" {throwTestException;}";
515         try {
516             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
517             mail.setEnvelopeFrom("tweety@pie");
518             JUnitUtils.interpret(mail, script);
519             isTestPassed = true;
520         } catch (ThrowTestException.TestException e) {
521         } catch (ParseException e) {
522         } catch (SieveException e) {
523         }
524         assertTrue(isTestPassed);
525     }
526 
527     /**
528      * Test for Test 'envelope'
529      */
530     public void testIfEnvelopeAllIsMultiFalse1() {
531         boolean isTestPassed = false;
532         String script = "if envelope :all :is [\"From\", \"To\"] \"user@domain\" {throwTestException;}";
533         try {
534             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
535             mail.setEnvelopeFrom("bugs@bunny");
536             mail.setEnvelopeTo("bugs@bunny");
537             JUnitUtils.interpret(mail, script);
538             isTestPassed = true;
539         } catch (ThrowTestException.TestException e) {
540         } catch (ParseException e) {
541         } catch (SieveException e) {
542         }
543         assertTrue(isTestPassed);
544     }
545 
546     /**
547      * Test for Test 'envelope'
548      */
549     public void testIfEnvelopeAllIsMultiFalse2() {
550         boolean isTestPassed = false;
551         String script = "if envelope :all :is [\"From\", \"To\"] [\"user@domain\", \"tweety@pie\"] {throwTestException;}";
552         try {
553             SieveEnvelopeMailAdapter mail = JUnitUtils.createEnvelopeMail();
554             mail.setEnvelopeFrom("bugs@bunny");
555             mail.setEnvelopeTo("bugs@bunny");
556             JUnitUtils.interpret(mail, script);
557             isTestPassed = true;
558         } catch (ThrowTestException.TestException e) {
559         } catch (ParseException e) {
560         } catch (SieveException e) {
561         }
562         assertTrue(isTestPassed);
563     }
564 
565 }