1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35 public class HeaderTest extends TestCase {
36
37
38
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
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
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
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
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
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
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
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
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
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
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
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
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
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
307
308
309
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
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
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 }