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 javax.mail.MessagingException;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.jsieve.commands.ThrowTestException;
27  import org.apache.jsieve.exception.SieveException;
28  import org.apache.jsieve.parser.generated.ParseException;
29  import org.apache.jsieve.utils.JUnitUtils;
30  import org.apache.jsieve.utils.SieveMailAdapter;
31  
32  /**
33   * Class AddressTest
34   */
35  public class AddressTest extends TestCase {
36  
37      /**
38       * Test for Test 'address'
39       */
40      public void testIfAddressAllIsTrue() {
41          boolean isTestPassed = false;
42          String script = "if address :all :is \"From\" \"user@domain\" {throwTestException;}";
43          try {
44              SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
45              mail.getMessage().addHeader("From", "user@domain");
46              JUnitUtils.interpret(mail, script);
47          } catch (MessagingException e) {
48          } catch (ThrowTestException.TestException e) {
49              isTestPassed = true;
50          } catch (ParseException e) {
51          } catch (SieveException e) {
52          }
53          assertTrue(isTestPassed);
54      }
55  
56      /**
57       * Test for Test 'address'
58       */
59      public void testCaseInsensitiveHeaderName() {
60          boolean isTestPassed = false;
61          String script = "if address :all :is \"from\" \"user@domain\" {throwTestException;}";
62          try {
63              SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
64              mail.getMessage().addHeader("From", "user@domain");
65              JUnitUtils.interpret(mail, script);
66          } catch (MessagingException e) {
67          } catch (ThrowTestException.TestException e) {
68              isTestPassed = true;
69          } catch (ParseException e) {
70          } catch (SieveException e) {
71          }
72          assertTrue(isTestPassed);
73      }
74  
75      /**
76       * Test for Test 'address'
77       */
78      public void testTreatmentOfEmbededSpacesInHeaderName() {
79          boolean isTestPassed = false;
80          String script = "if address :all :is \"From\" \"user@domain\" {throwTestException;}";
81          try {
82              SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
83              mail.getMessage().addHeader("From ", "user@domain");
84              JUnitUtils.interpret(mail, script);
85          } catch (MessagingException e) {
86          } catch (ThrowTestException.TestException e) {
87              isTestPassed = true;
88          } catch (ParseException e) {
89          } catch (SieveException e) {
90          }
91          assertTrue(isTestPassed);
92      }
93  
94      /**
95       * Test for Test 'address'
96       */
97      public void testOctetComparatorTrue() {
98          boolean isTestPassed = false;
99          String script = "if address :comparator \"i;octet\" :all :is \"From\" \"uSeR@dOmAiN\" {throwTestException;}";
100         try {
101             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
102             mail.getMessage().addHeader("From ", "uSeR@dOmAiN");
103             JUnitUtils.interpret(mail, script);
104         } catch (MessagingException e) {
105         } catch (ThrowTestException.TestException e) {
106             isTestPassed = true;
107         } catch (ParseException e) {
108         } catch (SieveException e) {
109         }
110         assertTrue(isTestPassed);
111     }
112 
113     /**
114      * Test for Test 'address'
115      */
116     public void testOctetComparatorFalse() {
117         boolean isTestPassed = false;
118         String script = "if address :comparator \"i;octet\" :all :is \"From\" \"uSeR@dOmAiN\" {throwTestException;}";
119         try {
120             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
121             mail.getMessage().addHeader("From ", "user@domain");
122             JUnitUtils.interpret(mail, script);
123             isTestPassed = true;
124         } catch (MessagingException e) {
125         } catch (ThrowTestException.TestException e) {
126         } catch (ParseException e) {
127         } catch (SieveException e) {
128         }
129         assertTrue(isTestPassed);
130     }
131 
132     /**
133      * Test for Test 'address'
134      */
135     public void testAsciiComparatorTrue() {
136         boolean isTestPassed = false;
137         String script = "if address :comparator \"i;ascii-casemap\" :all :is \"From\" \"uSeR@dOmAiN\" {throwTestException;}";
138         try {
139             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
140             mail.getMessage().addHeader("From ", "user@domain");
141             JUnitUtils.interpret(mail, script);
142         } catch (MessagingException e) {
143         } catch (ThrowTestException.TestException e) {
144             isTestPassed = true;
145         } catch (ParseException e) {
146         } catch (SieveException e) {
147         }
148         assertTrue(isTestPassed);
149     }
150 
151     /**
152      * Test for Test 'address'
153      */
154     public void testAsciiComparatorFalse() {
155         boolean isTestPassed = false;
156         String script = "if address :comparator \"i;ascii-casemap\" :all :is \"From\" \"uSeR@dOmAiN\" {throwTestException;}";
157         try {
158             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
159             mail.getMessage().addHeader("From ", "user@domain1");
160             JUnitUtils.interpret(mail, script);
161             isTestPassed = true;
162         } catch (MessagingException e) {
163         } catch (ThrowTestException.TestException e) {
164         } catch (ParseException e) {
165         } catch (SieveException e) {
166         }
167         assertTrue(isTestPassed);
168     }
169 
170     /**
171      * Test for Test 'address'
172      */
173     public void testIfAddressAllIsMultiTrue1() {
174         boolean isTestPassed = false;
175         String script = "if address :all :is [\"From\", \"To\"] \"user@domain\" {throwTestException;}";
176         try {
177             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
178             mail.getMessage().addHeader("From", "user@domain");
179             mail.getMessage().addHeader("To", "user@domain");
180             JUnitUtils.interpret(mail, script);
181         } catch (MessagingException e) {
182         } catch (ThrowTestException.TestException e) {
183             isTestPassed = true;
184         } catch (ParseException e) {
185         } catch (SieveException e) {
186         }
187         assertTrue(isTestPassed);
188     }
189 
190     /**
191      * Test for Test 'address'
192      */
193     public void testIfAddressAllIsMultiTrue2() {
194         boolean isTestPassed = false;
195         String script = "if address :all :is [\"From\", \"To\"] [\"user@domain\", \"tweety@pie\"] {throwTestException;}";
196         try {
197             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
198             mail.getMessage().addHeader("From", "user@domain");
199             mail.getMessage().addHeader("To", "user@domain");
200             JUnitUtils.interpret(mail, script);
201         } catch (MessagingException e) {
202         } catch (ThrowTestException.TestException e) {
203             isTestPassed = true;
204         } catch (ParseException e) {
205         } catch (SieveException e) {
206         }
207         assertTrue(isTestPassed);
208     }
209 
210     /**
211      * Test for Test 'address'
212      */
213     public void testIfAddressAllIsMultiTrue3() {
214         boolean isTestPassed = false;
215         String script = "if address :all :is [\"From\", \"To\"] [\"user@domain\", \"tweety@pie\"] {throwTestException;}";
216         try {
217             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
218             mail.getMessage().addHeader("From", "user@domain");
219             mail.getMessage().addHeader("To", "tweety@pie");
220             JUnitUtils.interpret(mail, script);
221         } catch (MessagingException e) {
222         } catch (ThrowTestException.TestException e) {
223             isTestPassed = true;
224         } catch (ParseException e) {
225         } catch (SieveException e) {
226         }
227         assertTrue(isTestPassed);
228     }
229 
230     /**
231      * Test for Test 'address'
232      */
233     public void testIfAddressAllIsMultiTrue4() {
234         boolean isTestPassed = false;
235         String script = "if address :all :is [\"From\", \"To\"] [\"user@domain\", \"tweety@pie\"] {throwTestException;}";
236         try {
237             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
238             mail.getMessage().addHeader("From", "tweety@pie");
239             mail.getMessage().addHeader("To", "tweety@pie");
240             JUnitUtils.interpret(mail, script);
241         } catch (MessagingException e) {
242         } catch (ThrowTestException.TestException e) {
243             isTestPassed = true;
244         } catch (ParseException e) {
245         } catch (SieveException e) {
246         }
247         assertTrue(isTestPassed);
248     }
249 
250     /**
251      * Test for Test 'address'
252      */
253     public void testIfAddressAllMatchesTrue() {
254         boolean isTestPassed = false;
255         String script = "if address :all :matches \"From\" \"*@domain\" {throwTestException;}";
256         try {
257             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
258             mail.getMessage().addHeader("From", "user@domain");
259             JUnitUtils.interpret(mail, script);
260         } catch (MessagingException e) {
261         } catch (ThrowTestException.TestException e) {
262             isTestPassed = true;
263         } catch (ParseException e) {
264         } catch (SieveException e) {
265         }
266         assertTrue(isTestPassed);
267     }
268 
269     /**
270      * Test for Test 'address'
271      */
272     public void testIfAddressAllContainsTrue() {
273         boolean isTestPassed = false;
274         String script = "if address :all :contains \"From\" \"r@dom\" {throwTestException;}";
275         try {
276             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
277             mail.getMessage().addHeader("From", "user@domain");
278             JUnitUtils.interpret(mail, script);
279         } catch (MessagingException e) {
280         } catch (ThrowTestException.TestException e) {
281             isTestPassed = true;
282         } catch (ParseException e) {
283         } catch (SieveException e) {
284         }
285         assertTrue(isTestPassed);
286     }
287 
288     /**
289      * Test for Test 'address'
290      */
291     public void testIfAddressLocalpartIsTrue() {
292         boolean isTestPassed = false;
293         String script = "if address :localpart :is \"From\" \"user\" {throwTestException;}";
294         try {
295             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
296             mail.getMessage().addHeader("From", "user@domain");
297             JUnitUtils.interpret(mail, script);
298         } catch (MessagingException e) {
299         } catch (ThrowTestException.TestException e) {
300             isTestPassed = true;
301         } catch (ParseException e) {
302         } catch (SieveException e) {
303         }
304         assertTrue(isTestPassed);
305     }
306 
307     /**
308      * Test for Test 'address'
309      */
310     public void testIfAddressLocalpartMatchesTrue() {
311         boolean isTestPassed = false;
312         String script = "if address :localpart :matches \"From\" \"*er\" {throwTestException;}";
313         try {
314             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
315             mail.getMessage().addHeader("From", "user@domain");
316             JUnitUtils.interpret(mail, script);
317         } catch (MessagingException e) {
318         } catch (ThrowTestException.TestException e) {
319             isTestPassed = true;
320         } catch (ParseException e) {
321         } catch (SieveException e) {
322         }
323         assertTrue(isTestPassed);
324     }
325 
326     /**
327      * Test for Test 'address'
328      */
329     public void testIfAddressLocalpartContainsTrue() {
330         boolean isTestPassed = false;
331         String script = "if address :localpart :contains \"From\" \"r\" {throwTestException;}";
332         try {
333             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
334             mail.getMessage().addHeader("From", "user@domain");
335             JUnitUtils.interpret(mail, script);
336         } catch (MessagingException e) {
337         } catch (ThrowTestException.TestException e) {
338             isTestPassed = true;
339         } catch (ParseException e) {
340         } catch (SieveException e) {
341         }
342         assertTrue(isTestPassed);
343     }
344 
345     /**
346      * Test for Test 'address'
347      */
348     public void testIfAddressDomainIsTrue() {
349         boolean isTestPassed = false;
350         String script = "if address :domain :is \"From\" \"domain\" {throwTestException;}";
351 
352         try {
353             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
354             mail.getMessage().addHeader("From", "user@domain");
355             JUnitUtils.interpret(mail, script);
356         } catch (MessagingException e) {
357         } catch (ThrowTestException.TestException e) {
358             isTestPassed = true;
359         } catch (ParseException e) {
360         } catch (SieveException e) {
361         }
362         assertTrue(isTestPassed);
363     }
364 
365     /**
366      * Test for Test 'address'
367      */
368     public void testIfAddressDomainMatchesTrue() {
369         boolean isTestPassed = false;
370         String script = "if address :domain :matches \"From\" \"*main\" {throwTestException;}";
371         try {
372             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
373             mail.getMessage().addHeader("From", "user@domain");
374             JUnitUtils.interpret(mail, script);
375         } catch (MessagingException e) {
376         } catch (ThrowTestException.TestException e) {
377             isTestPassed = true;
378         } catch (ParseException e) {
379         } catch (SieveException e) {
380         }
381         assertTrue(isTestPassed);
382     }
383 
384     /**
385      * Test for Test 'address'
386      */
387     public void testIfAddressDomainContainsTrue() {
388         boolean isTestPassed = false;
389         String script = "if address :domain :contains \"From\" \"dom\" {throwTestException;}";
390         try {
391             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
392             mail.getMessage().addHeader("From", "user@domain");
393             JUnitUtils.interpret(mail, script);
394         } catch (MessagingException e) {
395         } catch (ThrowTestException.TestException e) {
396             isTestPassed = true;
397         } catch (ParseException e) {
398         } catch (SieveException e) {
399         }
400         assertTrue(isTestPassed);
401     }
402 
403     /**
404      * Test for Test 'address'
405      */
406     public void testIfAddressAllIsFalse() {
407         boolean isTestPassed = false;
408         String script = "if address :all :is \"From\" \"user@domain\" {throwTestException;}";
409         try {
410             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
411             mail.getMessage().addHeader("From", "tweety@pie");
412             JUnitUtils.interpret(mail, script);
413             isTestPassed = true;
414         } catch (MessagingException e) {
415         } catch (ThrowTestException.TestException e) {
416         } catch (ParseException e) {
417         } catch (SieveException e) {
418         }
419         assertTrue(isTestPassed);
420     }
421 
422     /**
423      * Test for Test 'address'
424      */
425     public void testIfAddressAllMatchesFalse() {
426         boolean isTestPassed = false;
427         String script = "if address :all :matches \"From\" \"(.*)@domain\" {throwTestException;}";
428         try {
429             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
430             mail.getMessage().addHeader("From", "tweety@pie");
431             JUnitUtils.interpret(mail, script);
432             isTestPassed = true;
433         } catch (MessagingException e) {
434         } catch (ThrowTestException.TestException e) {
435         } catch (ParseException e) {
436         } catch (SieveException e) {
437         }
438         assertTrue(isTestPassed);
439     }
440 
441     /**
442      * Test for Test 'address'
443      */
444     public void testIfAddressAllContainsFalse() {
445         boolean isTestPassed = false;
446         String script = "if address :all :contains \"From\" \"r@dom\" {throwTestException;}";
447         try {
448             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
449             mail.getMessage().addHeader("From", "tweety@pie");
450             JUnitUtils.interpret(mail, script);
451             isTestPassed = true;
452         } catch (MessagingException e) {
453         } catch (ThrowTestException.TestException e) {
454         } catch (ParseException e) {
455         } catch (SieveException e) {
456         }
457         assertTrue(isTestPassed);
458     }
459 
460     /**
461      * Test for Test 'address'
462      */
463     public void testIfAddressLocalpartIsFalse() {
464         boolean isTestPassed = false;
465         String script = "if address :localpart :is \"From\" \"user\" {throwTestException;}";
466         try {
467             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
468             mail.getMessage().addHeader("From", "tweety@pie");
469             JUnitUtils.interpret(mail, script);
470             isTestPassed = true;
471         } catch (MessagingException e) {
472         } catch (ThrowTestException.TestException e) {
473         } catch (ParseException e) {
474         } catch (SieveException e) {
475         }
476         assertTrue(isTestPassed);
477     }
478 
479     /**
480      * Test for Test 'address'
481      */
482     public void testIfAddressLocalpartMatchesFalse() {
483         boolean isTestPassed = false;
484         String script = "if address :localpart :matches \"From\" \"(.*)er\" {throwTestException;}";
485         try {
486             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
487             mail.getMessage().addHeader("From", "tweety@pie");
488             JUnitUtils.interpret(mail, script);
489             isTestPassed = true;
490         } catch (MessagingException e) {
491         } catch (ThrowTestException.TestException e) {
492         } catch (ParseException e) {
493         } catch (SieveException e) {
494         }
495         assertTrue(isTestPassed);
496     }
497 
498     /**
499      * Test for Test 'address'
500      */
501     public void testIfAddressLocalpartContainsFalse() {
502         boolean isTestPassed = false;
503         String script = "if address :localpart :contains \"From\" \"r\" {throwTestException;}";
504         try {
505             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
506             mail.getMessage().addHeader("From", "tweety@pie");
507             JUnitUtils.interpret(mail, script);
508             isTestPassed = true;
509         } catch (MessagingException e) {
510         } catch (ThrowTestException.TestException e) {
511         } catch (ParseException e) {
512         } catch (SieveException e) {
513         }
514         assertTrue(isTestPassed);
515     }
516 
517     /**
518      * Test for Test 'address'
519      */
520     public void testIfAddressDomainIsFalse() {
521         boolean isTestPassed = false;
522         String script = "if address :domain :is \"From\" \"domain\" {throwTestException;}";
523 
524         try {
525             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
526             mail.getMessage().addHeader("From", "tweety@pie");
527             JUnitUtils.interpret(mail, script);
528             isTestPassed = true;
529         } catch (MessagingException e) {
530         } catch (ThrowTestException.TestException e) {
531         } catch (ParseException e) {
532         } catch (SieveException e) {
533         }
534         assertTrue(isTestPassed);
535     }
536 
537     /**
538      * Test for Test 'address'
539      */
540     public void testIfAddressDomainMatchesFalse() {
541         boolean isTestPassed = false;
542         String script = "if address :domain :matches \"From\" \"(.*)main\" {throwTestException;}";
543         try {
544             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
545             mail.getMessage().addHeader("From", "tweety@pie");
546             JUnitUtils.interpret(mail, script);
547             isTestPassed = true;
548         } catch (MessagingException e) {
549         } catch (ThrowTestException.TestException e) {
550         } catch (ParseException e) {
551         } catch (SieveException e) {
552         }
553         assertTrue(isTestPassed);
554     }
555 
556     /**
557      * Test for Test 'address'
558      */
559     public void testIfAddressDomainContainsFalse() {
560         boolean isTestPassed = false;
561         String script = "if address :domain :contains \"From\" \"dom\" {throwTestException;}";
562         try {
563             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
564             mail.getMessage().addHeader("From", "tweety@pie");
565             JUnitUtils.interpret(mail, script);
566             isTestPassed = true;
567         } catch (MessagingException e) {
568         } catch (ThrowTestException.TestException e) {
569         } catch (ParseException e) {
570         } catch (SieveException e) {
571         }
572         assertTrue(isTestPassed);
573     }
574 
575     /**
576      * Test for Test 'address'
577      */
578     public void testIfAddressAllIsMultiFalse1() {
579         boolean isTestPassed = false;
580         String script = "if address :all :is [\"From\", \"To\"] \"user@domain\" {throwTestException;}";
581         try {
582             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
583             mail.getMessage().addHeader("From", "bugs@bunny");
584             mail.getMessage().addHeader("To", "bugs@bunny");
585             JUnitUtils.interpret(mail, script);
586             isTestPassed = true;
587         } catch (MessagingException e) {
588         } catch (ThrowTestException.TestException e) {
589         } catch (ParseException e) {
590         } catch (SieveException e) {
591         }
592         assertTrue(isTestPassed);
593     }
594 
595     /**
596      * Test for Test 'address'
597      */
598     public void testIfAddressAllIsMultiFalse2() {
599         boolean isTestPassed = false;
600         String script = "if address :all :is [\"From\", \"To\"] [\"user@domain\", \"tweety@pie\"] {throwTestException;}";
601         try {
602             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
603             mail.getMessage().addHeader("From", "bugs@bunny");
604             mail.getMessage().addHeader("To", "bugs@bunny");
605             JUnitUtils.interpret(mail, script);
606             isTestPassed = true;
607         } catch (MessagingException e) {
608         } catch (ThrowTestException.TestException e) {
609         } catch (ParseException e) {
610         } catch (SieveException e) {
611         }
612         assertTrue(isTestPassed);
613     }
614 
615 }