KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.mail.BodyPart JavaDoc;
33 import javax.mail.MessagingException JavaDoc;
34 import javax.mail.Multipart JavaDoc;
35 import javax.mail.internet.MimeBodyPart JavaDoc;
36 import javax.mail.internet.MimeMessage JavaDoc;
37 import javax.mail.internet.MimeMultipart JavaDoc;
38
39 import junit.framework.TestCase;
40
41 import org.apache.avalon.framework.container.ContainerUtil;
42 import org.apache.james.services.AbstractDNSServer;
43 import org.apache.james.services.DNSServer;
44 import org.apache.james.smtpserver.core.filter.fastfail.URIRBLHandler;
45 import org.apache.james.test.mock.avalon.MockLogger;
46 import org.apache.james.test.mock.javaxmail.MockMimeMessage;
47 import org.apache.james.test.mock.mailet.MockMail;
48 import org.apache.james.util.junkscore.JunkScore;
49 import org.apache.james.util.junkscore.JunkScoreImpl;
50 import org.apache.mailet.Mail;
51
52 public class URIRBLHandlerTest extends TestCase {
53     private static final String JavaDoc BAD_DOMAIN1 = "bad.domain.de";
54     private static final String JavaDoc BAD_DOMAIN2 = "bad2.domain.de";
55     private static final String JavaDoc GOOD_DOMAIN = "good.apache.org";
56     private static final String JavaDoc URISERVER = "multi.surbl.org.";
57     private SMTPSession mockedSMTPSession;
58
59     private String JavaDoc response = null;
60     
61     public void setUp() {
62         // reset reponse
63
response = null;
64     }
65
66     private SMTPSession setupMockedSMTPSession(final Mail mail) {
67         mockedSMTPSession = new AbstractSMTPSession() {
68
69             private HashMap JavaDoc state = new HashMap JavaDoc();
70
71             private String JavaDoc ipAddress = "192.168.0.1";
72
73             private String JavaDoc host = "localhost";
74
75             private boolean relayingAllowed;
76
77             private boolean processing;
78             
79             public void abortMessage() {
80             }
81
82             public Mail getMail() {
83                 return mail;
84             }
85
86             public String JavaDoc getRemoteHost() {
87                 return host;
88             }
89
90             public String JavaDoc getRemoteIPAddress() {
91                 return ipAddress;
92             }
93
94             public Map JavaDoc getState() {
95                 state.put(SMTPSession.SENDER, "sender@james.apache.org");
96                 return state;
97             }
98
99             public boolean isRelayingAllowed() {
100                 return relayingAllowed;
101             }
102
103             public void setRelayingAllowed(boolean relayingAllowed) {
104                 this.relayingAllowed = relayingAllowed;
105             }
106
107             public void writeResponse(String JavaDoc respString) {
108                 response = respString;
109             }
110             
111             public void setStopHandlerProcessing(boolean processing) {
112                 this.processing = processing;
113             }
114             
115             public boolean getStopHandlerProcessing() {
116                 return processing;
117             }
118         };
119
120         return mockedSMTPSession;
121
122     }
123
124     private String JavaDoc getResponse() {
125         return response;
126     }
127
128     private Mail setupMockedMail(MimeMessage JavaDoc message) {
129         MockMail mail = new MockMail();
130         mail.setMessage(message);
131         return mail;
132     }
133
134     public MimeMessage JavaDoc setupMockedMimeMessage(String JavaDoc text)
135             throws MessagingException JavaDoc {
136         MimeMessage JavaDoc message = new MimeMessage JavaDoc(new MockMimeMessage());
137         message.setText(text);
138         message.saveChanges();
139
140         return message;
141     }
142     
143     public MimeMessage JavaDoc setupMockedMimeMessageMP(String JavaDoc text) throws MessagingException JavaDoc {
144         MimeMessage JavaDoc message = new MimeMessage JavaDoc(new MockMimeMessage());
145         
146 // Create the message part
147
BodyPart JavaDoc messageBodyPart = new MimeBodyPart JavaDoc();
148
149 // Fill the message
150
messageBodyPart.setText(text);
151
152         Multipart JavaDoc multipart = new MimeMultipart JavaDoc();
153         multipart.addBodyPart(messageBodyPart);
154         message.setContent(multipart);
155         message.saveChanges();
156
157         return message;
158     }
159     
160
161     /**
162      * Setup the mocked dnsserver
163      *
164      */

165     private DNSServer setupMockedDnsServer() {
166         DNSServer mockedDnsServer = new AbstractDNSServer() {
167
168             public Collection JavaDoc findTXTRecords(String JavaDoc hostname) {
169                 List JavaDoc res = new ArrayList JavaDoc();
170                 if (hostname == null) {
171                     return res;
172                 }
173                 ;
174                 if ((BAD_DOMAIN1.substring(4)).equals(hostname)) {
175                     res.add("Blocked - see http://www.surbl.org");
176                 }
177                 return res;
178             }
179
180             public InetAddress JavaDoc getByName(String JavaDoc host)
181                     throws UnknownHostException JavaDoc {
182                 if ((BAD_DOMAIN1.substring(4) + "." + URISERVER).equals(host)) {
183                     return InetAddress.getByName("127.0.0.1");
184                 } else if ((BAD_DOMAIN2.substring(4) + "." + URISERVER).equals(host)) {
185                     return InetAddress.getByName("127.0.0.1");
186                 } else if ((GOOD_DOMAIN.substring(5) + "." + URISERVER).equals(host)) {
187                     throw new UnknownHostException JavaDoc();
188                 }
189                 throw new UnsupportedOperationException JavaDoc("getByName("+host+") not implemented by this mock");
190             }
191         };
192         
193         return mockedDnsServer;
194     }
195     
196     public void testNotBlocked() throws IOException JavaDoc, MessagingException JavaDoc {
197         
198         ArrayList JavaDoc servers = new ArrayList JavaDoc();
199         servers.add(URISERVER);
200         
201         SMTPSession session = setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage("http://" + GOOD_DOMAIN + "/")));
202
203         URIRBLHandler handler = new URIRBLHandler();
204
205         ContainerUtil.enableLogging(handler, new MockLogger());
206         handler.setDnsServer(setupMockedDnsServer());
207         handler.setUriRblServer(servers);
208         handler.onMessage(session);
209
210         assertFalse("Not Stop handler processing", session.getStopHandlerProcessing());
211         assertNull("Email was not rejected", getResponse());
212     }
213     
214     public void testBlocked() throws IOException JavaDoc, MessagingException JavaDoc {
215         
216         ArrayList JavaDoc servers = new ArrayList JavaDoc();
217         servers.add(URISERVER);
218         
219         SMTPSession session = setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage("http://" + BAD_DOMAIN1 + "/")));
220
221         URIRBLHandler handler = new URIRBLHandler();
222
223         ContainerUtil.enableLogging(handler, new MockLogger());
224         handler.setDnsServer(setupMockedDnsServer());
225         handler.setUriRblServer(servers);
226         handler.onMessage(session);
227
228         assertTrue("Stop handler processing", session.getStopHandlerProcessing());
229         assertNotNull("Email was rejected", getResponse());
230     }
231     
232     public void testBlockedMultiPart() throws IOException JavaDoc, MessagingException JavaDoc {
233         
234         ArrayList JavaDoc servers = new ArrayList JavaDoc();
235         servers.add(URISERVER);
236         
237         SMTPSession session = setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessageMP("http://" + BAD_DOMAIN1 + "/" + " " +"http://" + GOOD_DOMAIN + "/")));
238
239         URIRBLHandler handler = new URIRBLHandler();
240
241         ContainerUtil.enableLogging(handler, new MockLogger());
242         handler.setDnsServer(setupMockedDnsServer());
243         handler.setUriRblServer(servers);
244         handler.onMessage(session);
245
246         assertTrue("Stop handler processing", session.getStopHandlerProcessing());
247         assertNotNull("Email was rejected", getResponse());
248     }
249     
250     public void testAddJunkScore() throws IOException JavaDoc, MessagingException JavaDoc {
251         
252         ArrayList JavaDoc servers = new ArrayList JavaDoc();
253         servers.add(URISERVER);
254         
255         SMTPSession session = setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage("http://" + BAD_DOMAIN1 + "/")));
256         session.getState().put(JunkScore.JUNK_SCORE, new JunkScoreImpl());
257         
258         URIRBLHandler handler = new URIRBLHandler();
259
260         ContainerUtil.enableLogging(handler, new MockLogger());
261         handler.setDnsServer(setupMockedDnsServer());
262         handler.setUriRblServer(servers);
263         handler.setAction("junkScore");
264         handler.setScore(20);
265         handler.onMessage(session);
266
267         assertFalse("Not stop handler processing", session.getStopHandlerProcessing());
268         assertNull("Email was not rejected", getResponse());
269         assertEquals("JunkScore added", ((JunkScore) session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("UriRBLCheck"), 20.0, 0d);
270     }
271 }
272
Popular Tags