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 HeaderTest
34   */
35  public class HeaderTest extends TestCase {
36  
37      /**
38       * Test for Test 'header'
39       */
40      public void testHeaderIsTrue() {
41          boolean isTestPassed = false;
42          String script = "if header :is \"X-Caffeine\" \"C8H10N4O2\" {throwTestException;}";
43          try {
44              SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
45              mail.getMessage().addHeader("X-Caffeine", "C8H10N4O2");
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 'header'
58       */
59      public void testHeaderCaseInsensitivity() {
60          boolean isTestPassed = false;
61          String script = "if header :is \"X-Caffeine\" \"C8H10N4O2\" {throwTestException;}";
62          try {
63              SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
64              mail.getMessage().addHeader("x-caffeine", "C8H10N4O2");
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 'header'
77       */
78      public void testHeaderIsTrueMulti1() {
79          boolean isTestPassed = false;
80          String script = "if header :is [\"X-Decaf\", \"X-Caffeine\"] \"C8H10N4O2\" {throwTestException;}";
81          try {
82              SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
83              mail.getMessage().addHeader("X-Caffeine", "C8H10N4O2");
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 'header'
96       */
97      public void testHeaderIsFalseMulti1() {
98          boolean isTestPassed = false;
99          String script = "if header :is [\"X-Decaf\", \"X-Caffeine\"] \"C8H10N4O2\" {throwTestException;}";
100         try {
101             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
102             mail.getMessage().addHeader("X-Caffeine", "stuff");
103             mail.getMessage().addHeader("X-Decaf", "more stuff");
104             JUnitUtils.interpret(mail, script);
105             isTestPassed = true;
106         } catch (MessagingException e) {
107         } catch (ThrowTestException.TestException e) {
108         } catch (ParseException e) {
109         } catch (SieveException e) {
110         }
111         assertTrue(isTestPassed);
112     }
113 
114     /**
115      * Test for Test 'header'
116      */
117     public void testHeaderIsTrueMulti2() {
118         boolean isTestPassed = false;
119         String script = "if header :is \"X-Caffeine\" [\"absent\", \"C8H10N4O2\"] {throwTestException;}";
120         try {
121             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
122             mail.getMessage().addHeader("X-Caffeine", "C8H10N4O2");
123             JUnitUtils.interpret(mail, script);
124         } catch (MessagingException e) {
125         } catch (ThrowTestException.TestException e) {
126             isTestPassed = true;
127         } catch (ParseException e) {
128         } catch (SieveException e) {
129         }
130         assertTrue(isTestPassed);
131     }
132 
133     /**
134      * Test for Test 'header'
135      */
136     public void testHeaderIsTrueMulti3() {
137         boolean isTestPassed = false;
138         String script = "if header :is [\"X-Decaf\", \"X-Caffeine\"] [\"absent\", \"C8H10N4O2\"] {throwTestException;}";
139         try {
140             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
141             mail.getMessage().addHeader("X-Caffeine", "stuff");
142             mail.getMessage().addHeader("X-Decaf", "C8H10N4O2");
143             JUnitUtils.interpret(mail, script);
144         } catch (MessagingException e) {
145         } catch (ThrowTestException.TestException e) {
146             isTestPassed = true;
147         } catch (ParseException e) {
148         } catch (SieveException e) {
149         }
150         assertTrue(isTestPassed);
151     }
152 
153     /**
154      * Test for Test 'header'
155      */
156     public void testHeaderIsFalseValue() {
157         boolean isTestPassed = false;
158         String script = "if header :is \"X-Caffeine\" \"C8H10N4O2\" {throwTestException;}";
159         try {
160             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
161             mail.getMessage().addHeader("X-Caffeine", "C8H10N4O");
162             JUnitUtils.interpret(mail, script);
163             isTestPassed = true;
164         } catch (MessagingException e) {
165         } catch (ThrowTestException.TestException e) {
166         } catch (ParseException e) {
167         } catch (SieveException e) {
168         }
169         assertTrue(isTestPassed);
170     }
171 
172     /**
173      * Test for Test 'header'
174      */
175     public void testHeaderIsFalseHeader() {
176         boolean isTestPassed = false;
177         String script = "if header :is \"X-Caffeine\" \"C8H10N4O2\" {throwTestException;}";
178         try {
179             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
180             mail.getMessage().addHeader("X-Caffein", "C8H10N4O2");
181             JUnitUtils.interpret(mail, script);
182             isTestPassed = true;
183         } catch (MessagingException e) {
184         } catch (ThrowTestException.TestException e) {
185         } catch (ParseException e) {
186         } catch (SieveException e) {
187         }
188         assertTrue(isTestPassed);
189     }
190 
191     /**
192      * Test for Test 'header'
193      */
194     public void testHeaderContainsTrue() {
195         boolean isTestPassed = false;
196         String script = "if header :contains \"X-Caffeine\" \"C8H10\" {throwTestException;}";
197         try {
198             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
199             mail.getMessage().addHeader("X-Caffeine", "C8H10N4O2");
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 'header'
212      */
213     public void testHeaderContainsFalse() {
214         boolean isTestPassed = false;
215         String script = "if header :is \"X-Caffeine\" \"C8H10N4O2\" {throwTestException;}";
216         try {
217             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
218             mail.getMessage().addHeader("X-Caffeine", "izzy");
219             JUnitUtils.interpret(mail, script);
220             isTestPassed = true;
221         } catch (MessagingException e) {
222         } catch (ThrowTestException.TestException e) {
223         } catch (ParseException e) {
224         } catch (SieveException e) {
225         }
226         assertTrue(isTestPassed);
227     }
228 
229     /**
230      * Test for Test 'header'
231      */
232     public void testHeaderContainsNullTrue() {
233         boolean isTestPassed = false;
234         String script = "if header :contains \"X-Caffeine\" \"\" {throwTestException;}";
235         try {
236             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
237             mail.getMessage().addHeader("X-Caffeine", null);
238             JUnitUtils.interpret(mail, script);
239         } catch (MessagingException e) {
240         } catch (ThrowTestException.TestException e) {
241             isTestPassed = true;
242         } catch (ParseException e) {
243         } catch (SieveException e) {
244         }
245         assertTrue(isTestPassed);
246     }
247 
248     /**
249      * Test for Test 'header'
250      */
251     public void testHeaderIsNullFalse() {
252         boolean isTestPassed = false;
253         String script = "if header :is \"X-Caffeine\" \"\" {throwTestException;}";
254         try {
255             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
256             mail.getMessage().addHeader("X-Caffeine", null);
257             JUnitUtils.interpret(mail, script);
258             isTestPassed = true;
259         } catch (MessagingException e) {
260         } catch (ThrowTestException.TestException e) {
261         } catch (ParseException e) {
262         } catch (SieveException e) {
263         }
264         assertTrue(isTestPassed);
265     }
266 
267     /**
268      * Test for Test 'header'
269      */
270     public void testHeaderMatchesTrue() {
271         boolean isTestPassed = false;
272         String script = "if header :matches \"X-Caffeine\" \"*10N?O2\" {throwTestException;}";
273         try {
274             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
275             mail.getMessage().addHeader("X-Caffeine", "C8H10N4O2");
276             JUnitUtils.interpret(mail, script);
277         } catch (MessagingException e) {
278         } catch (ThrowTestException.TestException e) {
279             isTestPassed = true;
280         } catch (ParseException e) {
281         } catch (SieveException e) {
282         }
283         assertTrue(isTestPassed);
284     }
285 
286     /**
287      * Test for Test 'header'
288      */
289     public void testHeaderMatchesFalse() {
290         boolean isTestPassed = false;
291         String script = "if header :matches \"X-Caffeine\" \"*10N?O2\" {throwTestException;}";
292         try {
293             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
294             mail.getMessage().addHeader("X-Caffeine", "C8H10N4O3");
295             JUnitUtils.interpret(mail, script);
296             isTestPassed = true;
297         } catch (MessagingException e) {
298         } catch (ThrowTestException.TestException e) {
299         } catch (ParseException e) {
300         } catch (SieveException e) {
301         }
302         assertTrue(isTestPassed);
303     }
304 
305     /**
306      * Test for square brackets in matching headers When the "[" is in the first
307      * char of the pattern it does not matches.
308      * 
309      * See http://issues.apache.org/jira/browse/JSIEVE-19
310      */
311     public void testSquareBracketsInMatch() {
312         boolean isTestPassed = false;
313         String script = "if header :matches \"X-Caffeine\" \"[test*\" {throwTestException;}";
314         try {
315             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
316             mail.getMessage().addHeader("X-Caffeine", "[test] my subject");
317             JUnitUtils.interpret(mail, script);
318         } catch (MessagingException e) {
319         } catch (ThrowTestException.TestException e) {
320             isTestPassed = true;
321         } catch (ParseException e) {
322         } catch (SieveException e) {
323         }
324         assertTrue(isTestPassed);
325     }
326 
327     /**
328      * Test for special char escaping: \\? is a ? and \\* is an *
329      */
330     public void testSpecialCharsEscapingInMatch() {
331         boolean isTestPassed = false;
332         String script = "if header :matches \"X-Caffeine\" \"my,\\\\,?,\\?,\\\\?,*,\\*,\\\\*,pattern\" {throwTestException;}";
333         try {
334             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
335             mail.getMessage().addHeader("X-Caffeine",
336                     "my,\\,x,x,?,foo,bar,*,pattern");
337             JUnitUtils.interpret(mail, script);
338         } catch (MessagingException e) {
339         } catch (ThrowTestException.TestException e) {
340             isTestPassed = true;
341         } catch (ParseException e) {
342         } catch (SieveException e) {
343         }
344         assertTrue(isTestPassed);
345     }
346 
347     /**
348      * Test for special char escaping: \\? is a ? and \\* is an *
349      */
350     public void testSpecialCharsEscapingInMatchFalse() {
351         boolean isTestPassed = false;
352         String script = "if header :matches \"X-Caffeine\" \"my,?,\\?,\\\\?,*,\\*,\\\\*,pattern\" {throwTestException;}";
353         try {
354             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
355             mail.getMessage().addHeader("X-Caffeine",
356                     "my,x,x,q,foo,bar,*,pattern");
357             JUnitUtils.interpret(mail, script);
358             isTestPassed = true;
359         } catch (MessagingException e) {
360         } catch (ThrowTestException.TestException e) {
361         } catch (ParseException e) {
362         } catch (SieveException e) {
363         }
364         assertTrue(isTestPassed);
365     }
366 
367 }