KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 package org.apache.james.smtpserver;
23
24 import java.net.InetAddress JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.mail.internet.ParseException JavaDoc;
30
31 import junit.framework.TestCase;
32
33 import org.apache.avalon.framework.container.ContainerUtil;
34 import org.apache.james.services.AbstractDNSServer;
35 import org.apache.james.services.DNSServer;
36 import org.apache.james.smtpserver.core.filter.fastfail.ResolvableEhloHeloHandler;
37 import org.apache.james.test.mock.avalon.MockLogger;
38 import org.apache.james.util.junkscore.JunkScore;
39 import org.apache.james.util.junkscore.JunkScoreImpl;
40 import org.apache.mailet.MailAddress;
41
42 public class ResolvableEhloHeloHandlerTest extends TestCase {
43
44     public final static String JavaDoc INVALID_HOST = "foo.bar";
45     
46     public final static String JavaDoc VALID_HOST = "james.apache.org";
47     
48     public final static String JavaDoc HELO = "HELO";
49     
50     public final static String JavaDoc RCPT = "RCPT";
51     
52     private String JavaDoc response = null;
53     
54     private String JavaDoc command = null;
55     
56     public void setUp() {
57         response = null;
58         command = null;
59     }
60     
61     private String JavaDoc getResponse() {
62         return response;
63     }
64     
65     private void setCommand(String JavaDoc command) {
66         this.command = command;
67     }
68     
69     private SMTPSession setupMockSession(final String JavaDoc argument,
70              final boolean relaying, final boolean authRequired, final String JavaDoc user, final MailAddress recipient) {
71         
72         SMTPSession session = new AbstractSMTPSession() {
73
74             boolean stop = false;
75             HashMap JavaDoc connectionMap = new HashMap JavaDoc();
76             HashMap JavaDoc map = new HashMap JavaDoc();
77             
78             public String JavaDoc getCommandArgument() {
79                 return argument;
80             }
81             
82             public String JavaDoc getCommandName() {
83                 return command;
84             }
85             
86             public boolean isAuthRequired() {
87                 return authRequired;
88             }
89             
90             public String JavaDoc getUser() {
91                 return user;
92             }
93             
94             public Map JavaDoc getConnectionState() {
95                 return connectionMap;
96             }
97             
98             public void writeResponse(String JavaDoc resp) {
99                 response = resp;
100             }
101             
102             public boolean isRelayingAllowed() {
103                 return relaying;
104             }
105             
106             public void setStopHandlerProcessing(boolean stop) {
107                 this.stop = stop;
108             }
109             
110             public boolean getStopHandlerProcessing() {
111                 return stop;
112             }
113             
114             public Map JavaDoc getState() {
115                 map.put(SMTPSession.CURRENT_RECIPIENT, recipient);
116                 return map;
117             }
118             
119             
120         };
121
122         return session;
123     }
124     
125     private DNSServer setupMockDNSServer() {
126         DNSServer dns = new AbstractDNSServer(){
127             public InetAddress JavaDoc getByName(String JavaDoc host) throws UnknownHostException JavaDoc {
128                 if (host.equals(INVALID_HOST))
129                     throw new UnknownHostException JavaDoc();
130                 return InetAddress.getLocalHost();
131             }
132         };
133         
134         return dns;
135     }
136     
137     public void testRejectInvalidHelo() throws ParseException JavaDoc {
138         SMTPSession session = setupMockSession(INVALID_HOST,false,false,null,new MailAddress("test@localhost"));
139         ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
140         
141         ContainerUtil.enableLogging(handler,new MockLogger());
142         
143         handler.setDnsServer(setupMockDNSServer());
144         
145         // helo
146
setCommand(HELO);
147         handler.onCommand(session);
148         assertNotNull("Invalid HELO",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
149         
150         
151         // rcpt
152
setCommand(RCPT);
153         handler.onCommand(session);
154         assertNotNull("Reject", getResponse());
155         
156         assertTrue("Stop handler processing",session.getStopHandlerProcessing());
157     }
158     
159     
160     public void testNotRejectValidHelo() throws ParseException JavaDoc {
161         SMTPSession session = setupMockSession(VALID_HOST,false,false,null,new MailAddress("test@localhost"));
162         ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
163         
164         ContainerUtil.enableLogging(handler,new MockLogger());
165         
166         handler.setDnsServer(setupMockDNSServer());
167         
168         // helo
169
setCommand(HELO);
170         handler.onCommand(session);
171         assertNull("Valid HELO",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
172         
173         
174         // rcpt
175
setCommand(RCPT);
176         handler.onCommand(session);
177         assertNull("Not reject", getResponse());
178         
179         assertFalse("Not stop handler processing",session.getStopHandlerProcessing());
180     }
181     
182     public void testNotRejectInvalidHeloAuthUser() throws ParseException JavaDoc {
183         SMTPSession session = setupMockSession(INVALID_HOST,false,true,"valid@user",new MailAddress("test@localhost"));
184         ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
185         
186         ContainerUtil.enableLogging(handler,new MockLogger());
187         
188         handler.setDnsServer(setupMockDNSServer());
189         
190         // helo
191
setCommand(HELO);
192         handler.onCommand(session);
193         assertNotNull("Value stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
194         
195         
196         // rcpt
197
setCommand(RCPT);
198         handler.onCommand(session);
199         assertNull("Not reject", getResponse());
200         
201         assertFalse("Not stop handler processing",session.getStopHandlerProcessing());
202     }
203     
204     public void testRejectInvalidHeloAuthUser() throws ParseException JavaDoc {
205         SMTPSession session = setupMockSession(INVALID_HOST,false,true,"valid@user",new MailAddress("test@localhost"));
206         ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
207         
208         ContainerUtil.enableLogging(handler,new MockLogger());
209         
210         handler.setDnsServer(setupMockDNSServer());
211         handler.setCheckAuthUsers(true);
212         
213         // helo
214
setCommand(HELO);
215         handler.onCommand(session);
216         assertNotNull("Value stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
217         
218         
219         // rcpt
220
setCommand(RCPT);
221         handler.onCommand(session);
222         assertNotNull("reject", getResponse());
223         
224         assertTrue("stop handler processing",session.getStopHandlerProcessing());
225     }
226     
227     public void testNotRejectRelay() throws ParseException JavaDoc {
228         SMTPSession session = setupMockSession(INVALID_HOST,true,false,null,new MailAddress("test@localhost"));
229         ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
230         
231         ContainerUtil.enableLogging(handler,new MockLogger());
232         
233         handler.setDnsServer(setupMockDNSServer());
234         
235         // helo
236
setCommand(HELO);
237         handler.onCommand(session);
238         assertNull("Value not stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
239         
240         
241         // rcpt
242
setCommand(RCPT);
243         handler.onCommand(session);
244         assertNull("Not reject", getResponse());
245         
246         assertFalse("Not stop handler processing",session.getStopHandlerProcessing());
247     }
248     
249     public void testRejectRelay() throws ParseException JavaDoc {
250         SMTPSession session = setupMockSession(INVALID_HOST,true,false,null,new MailAddress("test@localhost"));
251         ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
252         
253         ContainerUtil.enableLogging(handler,new MockLogger());
254         
255         handler.setDnsServer(setupMockDNSServer());
256         handler.setCheckAuthNetworks(true);
257         
258         // helo
259
setCommand(HELO);
260         handler.onCommand(session);
261         assertNotNull("Value stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
262         
263         
264         // rcpt
265
setCommand(RCPT);
266         handler.onCommand(session);
267         assertNotNull("Reject", getResponse());
268         
269         assertTrue("Stop handler processing",session.getStopHandlerProcessing());
270     }
271     
272     public void testNotRejectInvalidHeloPostmaster() throws ParseException JavaDoc {
273         SMTPSession session = setupMockSession(INVALID_HOST,false,false,null,new MailAddress("postmaster@localhost"));
274         ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
275         
276         ContainerUtil.enableLogging(handler,new MockLogger());
277         
278         handler.setDnsServer(setupMockDNSServer());
279         
280         // helo
281
setCommand(HELO);
282         handler.onCommand(session);
283         assertNotNull("stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
284         
285         
286         // rcpt
287
setCommand(RCPT);
288         handler.onCommand(session);
289         assertNull("Not Reject", getResponse());
290         
291         assertFalse("Not stop handler processing",session.getStopHandlerProcessing());
292     }
293     
294     public void testNotRejectInvalidHeloAbuse() throws ParseException JavaDoc {
295         SMTPSession session = setupMockSession(INVALID_HOST,false,false,null,new MailAddress("abuse@localhost"));
296         ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
297         
298         ContainerUtil.enableLogging(handler,new MockLogger());
299         
300         handler.setDnsServer(setupMockDNSServer());
301         
302         // helo
303
setCommand(HELO);
304         handler.onCommand(session);
305         assertNotNull("stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
306         
307         
308         // rcpt
309
setCommand(RCPT);
310         handler.onCommand(session);
311         assertNull("Not Reject", getResponse());
312         
313         assertFalse("Not stop handler processing",session.getStopHandlerProcessing());
314     }
315     
316     public void testAddJunkScoreInvalidHelo() throws ParseException JavaDoc {
317         SMTPSession session = setupMockSession(INVALID_HOST,false,false,null,new MailAddress("test@localhost"));
318         session.getConnectionState().put(JunkScore.JUNK_SCORE_SESSION, new JunkScoreImpl());
319         ResolvableEhloHeloHandler handler = new ResolvableEhloHeloHandler();
320         
321         ContainerUtil.enableLogging(handler,new MockLogger());
322         
323         handler.setDnsServer(setupMockDNSServer());
324         handler.setAction("junkScore");
325         handler.setScore(20);
326         
327         // helo
328
setCommand(HELO);
329         handler.onCommand(session);
330         assertNotNull("Invalid HELO",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO));
331         
332         
333         // rcpt
334
setCommand(RCPT);
335         handler.onCommand(session);
336         assertNull("Not Reject", getResponse());
337         
338         assertFalse("Don'T stop handler processing",session.getStopHandlerProcessing());
339         assertEquals("JunkScore added", ((JunkScore) session.getConnectionState().get(JunkScore.JUNK_SCORE_SESSION)).getStoredScore("ResolvableEhloHeloCheck"), 20.0, 0d);
340     }
341 }
342
Popular Tags