KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > smtpserver > SPFHandlerTest


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
21 package org.apache.james.smtpserver;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.avalon.framework.container.ContainerUtil;
29 import org.apache.james.jspf.core.DNSService;
30 import org.apache.james.smtpserver.core.filter.fastfail.SPFHandler;
31 import org.apache.james.test.mock.avalon.MockLogger;
32 import org.apache.james.test.mock.mailet.MockMail;
33 import org.apache.james.util.junkscore.JunkScore;
34 import org.apache.james.util.junkscore.JunkScoreImpl;
35 import org.apache.mailet.Mail;
36 import org.apache.mailet.MailAddress;
37
38 import junit.framework.TestCase;
39
40 public class SPFHandlerTest extends TestCase {
41
42     private DNSService mockedDnsService;
43
44     private SMTPSession mockedSMTPSession;;
45
46     private boolean relaying = false;
47
48     private String JavaDoc command = "MAIL";
49
50     protected void setUp() throws Exception JavaDoc {
51         super.setUp();
52         setupMockedDnsService();
53         setRelayingAllowed(false);
54     }
55
56     /**
57      * Set relayingAllowed
58      *
59      * @param relaying
60      * true or false
61      */

62     private void setRelayingAllowed(boolean relaying) {
63         this.relaying = relaying;
64     }
65
66     /**
67      * Setup the mocked dnsserver
68      *
69      */

70     private void setupMockedDnsService() {
71         mockedDnsService = new DNSService() {
72
73             public List JavaDoc getLocalDomainNames() {
74                 throw new UnsupportedOperationException JavaDoc(
75                         "Unimplemented mock service");
76             }
77
78             public void setTimeOut(int arg0) {
79                 // do nothing
80
}
81
82             public int getRecordLimit() {
83                 return 0;
84             }
85
86             public void setRecordLimit(int arg0) {
87                 throw new UnsupportedOperationException JavaDoc(
88                 "Unimplemented mock service");
89             }
90
91             public List JavaDoc getRecords(String JavaDoc host, int type) throws TimeoutException {
92                 switch (type) {
93                     case DNSService.TXT:
94                     case DNSService.SPF:
95                         List JavaDoc l = new ArrayList JavaDoc();
96                         if (host.equals("spf1.james.apache.org")) {
97                             // pass
98
l.add("v=spf1 +all");
99                             return l;
100                         } else if (host.equals("spf2.james.apache.org")) {
101                             // fail
102
l.add("v=spf1 -all");
103                             return l;
104                         } else if (host.equals("spf3.james.apache.org")) {
105                             // softfail
106
l.add("v=spf1 ~all");
107                             return l;
108                         } else if (host.equals("spf4.james.apache.org")) {
109                             // permerror
110
l.add("v=spf1 badcontent!");
111                             return l;
112                         } else if (host.equals("spf5.james.apache.org")) {
113                             // temperror
114
throw new TimeoutException();
115                         } else {
116                             return null;
117                         }
118                     default:
119                         throw new UnsupportedOperationException JavaDoc(
120                         "Unimplemented mock service");
121                 }
122             }
123
124         };
125     }
126
127     private void setCommand(String JavaDoc command) {
128         this.command = command;
129     }
130
131     /**
132      * Setup mocked smtpsession
133      */

134     private void setupMockedSMTPSession(final String JavaDoc ip, final String JavaDoc helo,
135             final MailAddress sender, final MailAddress recipient) {
136         mockedSMTPSession = new AbstractSMTPSession() {
137             HashMap JavaDoc state = new HashMap JavaDoc();
138
139             HashMap JavaDoc connectionState = new HashMap JavaDoc();
140
141             Mail mail = new MockMail();
142
143             boolean stopHandler = false;
144
145             public void writeResponse(String JavaDoc respString) {
146                 // Do nothing
147
}
148
149             public String JavaDoc getCommandName() {
150                 return command;
151             }
152
153             public Mail getMail() {
154                 return mail;
155             }
156
157             public String JavaDoc getRemoteIPAddress() {
158                 return ip;
159             }
160
161             public Map JavaDoc getState() {
162                 state.put(SMTPSession.CURRENT_HELO_NAME, helo);
163                 state.put(SMTPSession.SENDER, sender);
164                 state.put(SMTPSession.CURRENT_RECIPIENT, recipient);
165                 return state;
166             }
167
168             public boolean isRelayingAllowed() {
169                 return relaying;
170             }
171
172             public boolean isAuthRequired() {
173                 return false;
174             }
175
176             public int getRcptCount() {
177                 return 0;
178             }
179
180             public void setStopHandlerProcessing(boolean b) {
181                 stopHandler = b;
182             }
183
184             public boolean getStopHandlerProcessing() {
185                 return stopHandler;
186             }
187
188             public Map JavaDoc getConnectionState() {
189                 return connectionState;
190             }
191
192             public void resetConnectionState() {
193                 connectionState.clear();
194             }
195
196         };
197     }
198
199     private void runHandlers(SPFHandler spf, SMTPSession mockedSMTPSession) {
200
201         setCommand("MAIL");
202         spf.onCommand(mockedSMTPSession);
203
204         setCommand("RCPT");
205         spf.onCommand(mockedSMTPSession);
206
207         spf.onMessage(mockedSMTPSession);
208     }
209
210     public void testSPFpass() throws Exception JavaDoc {
211         setupMockedSMTPSession("192.168.100.1", "spf1.james.apache.org",
212                 new MailAddress("test@spf1.james.apache.org"), new MailAddress(
213                         "test@localhost"));
214         SPFHandler spf = new SPFHandler();
215
216
217         ContainerUtil.enableLogging(spf, new MockLogger());
218         
219         spf.setDNSService(mockedDnsService);
220         
221         spf.initialize();
222
223         runHandlers(spf, mockedSMTPSession);
224
225         assertNull("Not reject", mockedSMTPSession.getState().get(
226                 SPFHandler.SPF_BLOCKLISTED));
227         assertNull("Not blocked so no details", mockedSMTPSession.getState()
228                 .get(SPFHandler.SPF_DETAIL));
229         assertNull("No tempError", mockedSMTPSession.getState().get(
230                 SPFHandler.SPF_TEMPBLOCKLISTED));
231         assertNotNull("Header should present", mockedSMTPSession.getState()
232                 .get(SPFHandler.SPF_HEADER));
233         assertEquals("header", mockedSMTPSession.getState().get(
234                 SPFHandler.SPF_HEADER), mockedSMTPSession.getMail()
235                 .getAttribute(SPFHandler.SPF_HEADER_MAIL_ATTRIBUTE_NAME));
236         assertFalse(mockedSMTPSession.getStopHandlerProcessing());
237     }
238
239     public void testSPFfail() throws Exception JavaDoc {
240         setupMockedSMTPSession("192.168.100.1", "spf2.james.apache.org",
241                 new MailAddress("test@spf2.james.apache.org"), new MailAddress(
242                         "test@localhost"));
243         SPFHandler spf = new SPFHandler();
244
245         ContainerUtil.enableLogging(spf, new MockLogger());
246         
247         spf.setDNSService(mockedDnsService);
248         
249         spf.initialize();
250
251         runHandlers(spf, mockedSMTPSession);
252
253         assertNotNull("reject", mockedSMTPSession.getState().get(
254                 SPFHandler.SPF_BLOCKLISTED));
255         assertNotNull("blocked", mockedSMTPSession.getState().get(
256                 SPFHandler.SPF_DETAIL));
257         assertNull("No tempError", mockedSMTPSession.getState().get(
258                 SPFHandler.SPF_TEMPBLOCKLISTED));
259         assertNotNull("Header should present", mockedSMTPSession.getState()
260                 .get(SPFHandler.SPF_HEADER));
261         assertTrue(mockedSMTPSession.getStopHandlerProcessing());
262     }
263
264     public void testSPFsoftFail() throws Exception JavaDoc {
265         setupMockedSMTPSession("192.168.100.1", "spf3.james.apache.org",
266                 new MailAddress("test@spf3.james.apache.org"), new MailAddress(
267                         "test@localhost"));
268         SPFHandler spf = new SPFHandler();
269
270         ContainerUtil.enableLogging(spf, new MockLogger());
271         
272         spf.setDNSService(mockedDnsService);
273         
274         spf.initialize();
275
276         runHandlers(spf, mockedSMTPSession);
277
278         assertNull("not reject", mockedSMTPSession.getState().get(
279                 SPFHandler.SPF_BLOCKLISTED));
280         assertNull("no details ", mockedSMTPSession.getState().get(
281                 SPFHandler.SPF_DETAIL));
282         assertNull("No tempError", mockedSMTPSession.getState().get(
283                 SPFHandler.SPF_TEMPBLOCKLISTED));
284         assertNotNull("Header should present", mockedSMTPSession.getState()
285                 .get(SPFHandler.SPF_HEADER));
286         assertEquals("header", mockedSMTPSession.getState().get(
287                 SPFHandler.SPF_HEADER), mockedSMTPSession.getMail()
288                 .getAttribute(SPFHandler.SPF_HEADER_MAIL_ATTRIBUTE_NAME));
289         assertFalse(mockedSMTPSession.getStopHandlerProcessing());
290     }
291
292     public void testSPFsoftFailRejectEnabled() throws Exception JavaDoc {
293         setupMockedSMTPSession("192.168.100.1", "spf3.james.apache.org",
294                 new MailAddress("test@spf3.james.apache.org"), new MailAddress(
295                         "test@localhost"));
296         SPFHandler spf = new SPFHandler();
297
298         ContainerUtil.enableLogging(spf, new MockLogger());
299        
300         spf.setDNSService(mockedDnsService);
301        
302         spf.initialize();
303         
304         spf.setBlockSoftFail(true);
305
306         setCommand("MAIL");
307         spf.onCommand(mockedSMTPSession);
308
309         setCommand("RCPT");
310         spf.onCommand(mockedSMTPSession);
311
312         assertNotNull("reject", mockedSMTPSession.getState().get(
313                 SPFHandler.SPF_BLOCKLISTED));
314         assertNotNull("details ", mockedSMTPSession.getState().get(
315                 SPFHandler.SPF_DETAIL));
316         assertNull("No tempError", mockedSMTPSession.getState().get(
317                 SPFHandler.SPF_TEMPBLOCKLISTED));
318         assertNotNull("Header should present", mockedSMTPSession.getState()
319                 .get(SPFHandler.SPF_HEADER));
320         assertTrue(mockedSMTPSession.getStopHandlerProcessing());
321     }
322
323     public void testSPFpermError() throws Exception JavaDoc {
324         setupMockedSMTPSession("192.168.100.1", "spf4.james.apache.org",
325                 new MailAddress("test@spf4.james.apache.org"), new MailAddress(
326                         "test@localhost"));
327         SPFHandler spf = new SPFHandler();
328
329         ContainerUtil.enableLogging(spf, new MockLogger());
330         
331         spf.setDNSService(mockedDnsService);
332         
333         spf.initialize();
334         
335         spf.setBlockSoftFail(true);
336
337         runHandlers(spf, mockedSMTPSession);
338
339         assertNotNull("reject", mockedSMTPSession.getState().get(
340                 SPFHandler.SPF_BLOCKLISTED));
341         assertNotNull("details ", mockedSMTPSession.getState().get(
342                 SPFHandler.SPF_DETAIL));
343         assertNull("No tempError", mockedSMTPSession.getState().get(
344                 SPFHandler.SPF_TEMPBLOCKLISTED));
345         assertNotNull("Header should present", mockedSMTPSession.getState()
346                 .get(SPFHandler.SPF_HEADER));
347         assertTrue(mockedSMTPSession.getStopHandlerProcessing());
348     }
349
350     public void testSPFtempError() throws Exception JavaDoc {
351         setupMockedSMTPSession("192.168.100.1", "spf5.james.apache.org",
352                 new MailAddress("test@spf5.james.apache.org"), new MailAddress(
353                         "test@localhost"));
354         SPFHandler spf = new SPFHandler();
355
356         ContainerUtil.enableLogging(spf, new MockLogger());
357         
358         spf.setDNSService(mockedDnsService);
359
360         spf.initialize();
361         
362         runHandlers(spf, mockedSMTPSession);
363
364         assertNull("no reject", mockedSMTPSession.getState().get(
365                 SPFHandler.SPF_BLOCKLISTED));
366         assertNull("no details ", mockedSMTPSession.getState().get(
367                 SPFHandler.SPF_DETAIL));
368         assertNotNull("tempError", mockedSMTPSession.getState().get(
369                 SPFHandler.SPF_TEMPBLOCKLISTED));
370         assertNotNull("Header should present", mockedSMTPSession.getState()
371                 .get(SPFHandler.SPF_HEADER));
372         assertTrue(mockedSMTPSession.getStopHandlerProcessing());
373     }
374
375     public void testSPFNoRecord() throws Exception JavaDoc {
376         setupMockedSMTPSession("192.168.100.1", "spf6.james.apache.org",
377                 new MailAddress("test@spf6.james.apache.org"), new MailAddress(
378                         "test@localhost"));
379         SPFHandler spf = new SPFHandler();
380
381         ContainerUtil.enableLogging(spf, new MockLogger());
382
383         spf.setDNSService(mockedDnsService);
384
385         spf.initialize();
386         
387         runHandlers(spf, mockedSMTPSession);
388
389         assertNull("no reject", mockedSMTPSession.getState().get(
390                 SPFHandler.SPF_BLOCKLISTED));
391         assertNull("no details ", mockedSMTPSession.getState().get(
392                 SPFHandler.SPF_DETAIL));
393         assertNull("no tempError", mockedSMTPSession.getState().get(
394                 SPFHandler.SPF_TEMPBLOCKLISTED));
395         assertNotNull("Header should present", mockedSMTPSession.getState()
396                 .get(SPFHandler.SPF_HEADER));
397         assertEquals("header", mockedSMTPSession.getState().get(
398                 SPFHandler.SPF_HEADER), mockedSMTPSession.getMail()
399                 .getAttribute(SPFHandler.SPF_HEADER_MAIL_ATTRIBUTE_NAME));
400         assertFalse(mockedSMTPSession.getStopHandlerProcessing());
401     }
402
403     public void testSPFpermErrorNotRejectPostmaster() throws Exception JavaDoc {
404         setupMockedSMTPSession("192.168.100.1", "spf4.james.apache.org",
405                 new MailAddress("test@spf4.james.apache.org"), new MailAddress(
406                         "postmaster@localhost"));
407         SPFHandler spf = new SPFHandler();
408
409         ContainerUtil.enableLogging(spf, new MockLogger());
410
411         spf.setDNSService(mockedDnsService);
412         
413         spf.initialize();
414         
415         spf.setBlockSoftFail(true);
416
417         runHandlers(spf, mockedSMTPSession);
418
419         assertNotNull("not removed this state", mockedSMTPSession.getState().get(
420                 SPFHandler.SPF_BLOCKLISTED));
421         assertNotNull("not removed this state", mockedSMTPSession.getState().get(
422                 SPFHandler.SPF_DETAIL));
423         assertNotNull("not removed this state", mockedSMTPSession.getState()
424                 .get(SPFHandler.SPF_HEADER));
425         assertFalse("not rejected", mockedSMTPSession.getStopHandlerProcessing());
426     }
427
428     public void testSPFpermErrorNotRejectAbuse() throws Exception JavaDoc {
429         setupMockedSMTPSession("192.168.100.1", "spf4.james.apache.org",
430                 new MailAddress("test@spf4.james.apache.org"), new MailAddress("abuse@localhost"));
431         SPFHandler spf = new SPFHandler();
432
433         ContainerUtil.enableLogging(spf, new MockLogger());
434         
435         spf.initialize();
436
437         spf.setDNSService(mockedDnsService);
438         spf.setBlockSoftFail(true);
439
440         runHandlers(spf, mockedSMTPSession);
441
442         assertFalse("not rejected",mockedSMTPSession.getStopHandlerProcessing());
443     }
444     
445     public void testSPFpermErrorRejectDisabled() throws Exception JavaDoc {
446         setupMockedSMTPSession("192.168.100.1", "spf4.james.apache.org",
447                 new MailAddress("test@spf4.james.apache.org"), new MailAddress(
448                         "test@localhost"));
449         SPFHandler spf = new SPFHandler();
450
451         ContainerUtil.enableLogging(spf, new MockLogger());
452         
453         spf.setDNSService(mockedDnsService);
454         
455         spf.initialize();
456         
457         spf.setBlockPermError(false);
458
459         runHandlers(spf, mockedSMTPSession);
460
461         assertNull("not reject", mockedSMTPSession.getState().get(
462                 SPFHandler.SPF_BLOCKLISTED));
463         assertNull("details ", mockedSMTPSession.getState().get(
464                 SPFHandler.SPF_DETAIL));
465         assertNull("No tempError", mockedSMTPSession.getState().get(
466                 SPFHandler.SPF_TEMPBLOCKLISTED));
467         assertNotNull("Header should present", mockedSMTPSession.getState()
468                 .get(SPFHandler.SPF_HEADER));
469         assertFalse(mockedSMTPSession.getStopHandlerProcessing());
470     }
471     
472     public void testSPFfailAddJunkScore() throws Exception JavaDoc {
473         setupMockedSMTPSession("192.168.100.1", "spf2.james.apache.org",
474                 new MailAddress("test@spf2.james.apache.org"), new MailAddress(
475                         "test@localhost"));
476         mockedSMTPSession.getState().put(JunkScore.JUNK_SCORE, new JunkScoreImpl());
477         
478         SPFHandler spf = new SPFHandler();
479
480         ContainerUtil.enableLogging(spf, new MockLogger());
481         spf.setAction("junkScore");
482         spf.setScore(20);
483         spf.setDNSService(mockedDnsService);
484         
485         spf.initialize();
486
487         runHandlers(spf, mockedSMTPSession);
488
489         assertNotNull("reject", mockedSMTPSession.getState().get(
490                 SPFHandler.SPF_BLOCKLISTED));
491         assertNotNull("blocked", mockedSMTPSession.getState().get(
492                 SPFHandler.SPF_DETAIL));
493         assertNull("No tempError", mockedSMTPSession.getState().get(
494                 SPFHandler.SPF_TEMPBLOCKLISTED));
495         assertNotNull("Header should present", mockedSMTPSession.getState()
496                 .get(SPFHandler.SPF_HEADER));
497         assertFalse("Not stopped", mockedSMTPSession.getStopHandlerProcessing());
498         assertEquals("Score added",((JunkScore) mockedSMTPSession.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("SPFCheck"), 20.0, 0d);
499     }
500 }
501
Popular Tags