KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.InetAddress JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.mail.internet.ParseException JavaDoc;
32
33 import org.apache.avalon.framework.configuration.Configuration;
34 import org.apache.avalon.framework.configuration.ConfigurationException;
35 import org.apache.avalon.framework.configuration.DefaultConfiguration;
36 import org.apache.avalon.framework.container.ContainerUtil;
37 import org.apache.james.services.AbstractDNSServer;
38 import org.apache.james.services.DNSServer;
39 import org.apache.james.smtpserver.core.filter.fastfail.DNSRBLHandler;
40 import org.apache.james.test.mock.avalon.MockLogger;
41 import org.apache.james.util.junkscore.JunkScore;
42 import org.apache.james.util.junkscore.JunkScoreImpl;
43 import org.apache.mailet.MailAddress;
44
45 import junit.framework.TestCase;
46
47 public class DNSRBLHandlerTest extends TestCase {
48
49     private DNSServer mockedDnsServer;
50
51     private SMTPSession mockedSMTPSession;
52
53     private String JavaDoc remoteIp = "127.0.0.2";
54
55     private boolean relaying = false;
56     
57     public static final String JavaDoc RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME = "org.apache.james.smtpserver.rbl.blocklisted";
58     
59     public static final String JavaDoc RBL_DETAIL_MAIL_ATTRIBUTE_NAME = "org.apache.james.smtpserver.rbl.detail";
60
61     protected void setUp() throws Exception JavaDoc {
62         super.setUp();
63         setupMockedDnsServer();
64         setRelayingAllowed(false);
65     }
66
67     /**
68      * Set the remoteIp
69      *
70      * @param remoteIp The remoteIP to set
71      */

72     private void setRemoteIp(String JavaDoc remoteIp) {
73         this.remoteIp = remoteIp;
74     }
75
76     /**
77      * Set relayingAllowed
78      *
79      * @param relaying true or false
80      */

81     private void setRelayingAllowed(boolean relaying) {
82         this.relaying = relaying;
83     }
84
85     /**
86      * Setup the mocked dnsserver
87      *
88      */

89     private void setupMockedDnsServer() {
90         mockedDnsServer = new AbstractDNSServer() {
91
92             public Collection JavaDoc findMXRecords(String JavaDoc hostname) {
93                 throw new UnsupportedOperationException JavaDoc("Unimplemented in mock");
94             }
95
96             public Collection JavaDoc findTXTRecords(String JavaDoc hostname) {
97                 List JavaDoc res = new ArrayList JavaDoc();
98                 if (hostname == null) {
99                     return res;
100                 }
101                 ;
102                 if ("2.0.0.127.bl.spamcop.net.".equals(hostname)) {
103                     res.add("Blocked - see http://www.spamcop.net/bl.shtml?127.0.0.2");
104                 }
105                 return res;
106             }
107
108             public InetAddress JavaDoc getByName(String JavaDoc host)
109                     throws UnknownHostException JavaDoc {
110                 if ("2.0.0.127.bl.spamcop.net.".equals(host)) {
111                     return InetAddress.getByName("127.0.0.1");
112                 } else if ("3.0.0.127.bl.spamcop.net.".equals(host)) {
113                     return InetAddress.getByName("127.0.0.1");
114                 } else if ("1.0.168.192.bl.spamcop.net.".equals(host)) {
115                     throw new UnknownHostException JavaDoc(host);
116                 }
117                 throw new UnsupportedOperationException JavaDoc("getByName("+host+") not implemented in DNSRBLHandlerTest mock");
118             }
119         };
120     }
121
122     /**
123      * Setup mocked smtpsession
124      */

125     private void setupMockedSMTPSession(final MailAddress rcpt) {
126         mockedSMTPSession = new AbstractSMTPSession() {
127             HashMap JavaDoc state = new HashMap JavaDoc();
128             HashMap JavaDoc connectionState = new HashMap JavaDoc();
129             boolean stopHandler = false;
130             
131             public String JavaDoc getRemoteIPAddress() {
132                 return remoteIp;
133             }
134
135             public Map JavaDoc getState() {
136             state.put(SMTPSession.CURRENT_RECIPIENT, rcpt);
137                 return state;
138             }
139
140             public boolean isRelayingAllowed() {
141                 return relaying;
142             }
143
144             public boolean isAuthRequired() {
145                 return false;
146             }
147
148             public int getRcptCount() {
149                 return 0;
150             }
151
152             public void setStopHandlerProcessing(boolean b) {
153                 stopHandler = b;
154             }
155
156             public boolean getStopHandlerProcessing() {
157                 return stopHandler;
158             }
159
160             public Map JavaDoc getConnectionState() {
161                 return connectionState;
162             }
163
164             public void resetConnectionState() {
165                 connectionState.clear();
166             }
167
168         };
169     }
170
171     // ip is blacklisted and has txt details
172
public void testBlackListedTextPresent() throws ParseException JavaDoc {
173         DNSRBLHandler rbl = new DNSRBLHandler();
174
175         ContainerUtil.enableLogging(rbl, new MockLogger());
176
177         setupMockedSMTPSession(new MailAddress("any@domain"));
178         rbl.setDNSServer(mockedDnsServer);
179
180         rbl.setBlacklist(new String JavaDoc[] { "bl.spamcop.net." });
181         rbl.setGetDetail(true);
182         rbl.onConnect(mockedSMTPSession);
183         assertEquals("Details","Blocked - see http://www.spamcop.net/bl.shtml?127.0.0.2",
184                mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
185         assertNotNull("Blocked",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
186     }
187
188     // ip is blacklisted and has txt details but we don'T want to retrieve the txt record
189
public void testGetNoDetail() throws ParseException JavaDoc {
190         DNSRBLHandler rbl = new DNSRBLHandler();
191
192         ContainerUtil.enableLogging(rbl, new MockLogger());
193
194         setupMockedSMTPSession(new MailAddress("any@domain"));
195         rbl.setDNSServer(mockedDnsServer);
196
197         rbl.setBlacklist(new String JavaDoc[] { "bl.spamcop.net." });
198         rbl.setGetDetail(false);
199         rbl.onConnect(mockedSMTPSession);
200         assertNull("No details",mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
201         assertNotNull("Blocked",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
202     }
203
204     // ip is allowed to relay
205
public void testRelayAllowed() throws ParseException JavaDoc {
206         DNSRBLHandler rbl = new DNSRBLHandler();
207
208         ContainerUtil.enableLogging(rbl, new MockLogger());
209
210         setRelayingAllowed(true);
211         setupMockedSMTPSession(new MailAddress("any@domain"));
212
213         rbl.setDNSServer(mockedDnsServer);
214
215         rbl.setBlacklist(new String JavaDoc[] { "bl.spamcop.net." });
216         rbl.setGetDetail(true);
217         rbl.onConnect(mockedSMTPSession);
218         assertNull("No details",mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
219         assertNull("Not blocked",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
220     }
221
222     // ip not on blacklist
223
public void testNotBlackListed() throws ParseException JavaDoc {
224         DNSRBLHandler rbl = new DNSRBLHandler();
225
226         ContainerUtil.enableLogging(rbl, new MockLogger());
227         setRemoteIp("192.168.0.1");
228         setupMockedSMTPSession(new MailAddress("any@domain"));
229
230         rbl.setDNSServer(mockedDnsServer);
231
232         rbl.setBlacklist(new String JavaDoc[] { "bl.spamcop.net." });
233         rbl.setGetDetail(true);
234         rbl.onConnect(mockedSMTPSession);
235         assertNull("No details",mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
236         assertNull("Not blocked",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
237     }
238
239     // ip on blacklist without txt details
240
public void testBlackListedNoTxt() throws ParseException JavaDoc {
241         DNSRBLHandler rbl = new DNSRBLHandler();
242
243         ContainerUtil.enableLogging(rbl, new MockLogger());
244         setRemoteIp("127.0.0.3");
245         setupMockedSMTPSession(new MailAddress("any@domain"));
246
247         rbl.setDNSServer(mockedDnsServer);
248
249         rbl.setBlacklist(new String JavaDoc[] { "bl.spamcop.net." });
250         rbl.setGetDetail(true);
251         rbl.onConnect(mockedSMTPSession);
252         assertNull(mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
253         assertNotNull("Blocked",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
254     }
255
256     // ip on whitelist
257
public void testWhiteListed() throws ParseException JavaDoc {
258         DNSRBLHandler rbl = new DNSRBLHandler();
259
260         ContainerUtil.enableLogging(rbl, new MockLogger());
261         setRemoteIp("127.0.0.2");
262         setupMockedSMTPSession(new MailAddress("any@domain"));
263
264         rbl.setDNSServer(mockedDnsServer);
265
266         rbl.setWhitelist(new String JavaDoc[] { "bl.spamcop.net." });
267         rbl.setGetDetail(true);
268         rbl.onConnect(mockedSMTPSession);
269         assertNull(mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
270         assertNull("Not blocked",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
271     }
272     
273     public void testInvalidConfig() {
274         boolean exception = false;
275         DNSRBLHandler rbl = new DNSRBLHandler();
276         try {
277             rbl.configure((Configuration) new DefaultConfiguration("rblserver"));
278         } catch (ConfigurationException e) {
279             exception = true;
280         }
281         
282         assertTrue("Invalid config",exception);
283     }
284
285     public void testAddJunkScore() throws ParseException JavaDoc {
286         DNSRBLHandler rbl = new DNSRBLHandler();
287
288         ContainerUtil.enableLogging(rbl, new MockLogger());
289
290         setupMockedSMTPSession(new MailAddress("any@domain"));
291         mockedSMTPSession.getConnectionState().put(JunkScore.JUNK_SCORE_SESSION, new JunkScoreImpl());
292         rbl.setDNSServer(mockedDnsServer);
293
294         rbl.setBlacklist(new String JavaDoc[] { "bl.spamcop.net." });
295         rbl.setGetDetail(false);
296         rbl.setScore(20);
297         rbl.setAction("junkScore");
298         rbl.onConnect(mockedSMTPSession);
299         assertNull("No details",mockedSMTPSession.getConnectionState().get(RBL_DETAIL_MAIL_ATTRIBUTE_NAME));
300         assertNotNull("Listed on RBL",mockedSMTPSession.getConnectionState().get(RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME));
301         
302         rbl.onCommand(mockedSMTPSession);
303         assertEquals("Score stored",((JunkScore) mockedSMTPSession.getConnectionState().get(JunkScore.JUNK_SCORE_SESSION)).getStoredScore("DNSRBLCheck"), 20.0, 0d);
304     }
305
306 }
307
Popular Tags