1 19 20 21 package org.apache.james.smtpserver; 22 23 import java.io.IOException ; 24 import java.net.InetAddress ; 25 import java.net.UnknownHostException ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import javax.mail.BodyPart ; 33 import javax.mail.MessagingException ; 34 import javax.mail.Multipart ; 35 import javax.mail.internet.MimeBodyPart ; 36 import javax.mail.internet.MimeMessage ; 37 import javax.mail.internet.MimeMultipart ; 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 BAD_DOMAIN1 = "bad.domain.de"; 54 private static final String BAD_DOMAIN2 = "bad2.domain.de"; 55 private static final String GOOD_DOMAIN = "good.apache.org"; 56 private static final String URISERVER = "multi.surbl.org."; 57 private SMTPSession mockedSMTPSession; 58 59 private String response = null; 60 61 public void setUp() { 62 response = null; 64 } 65 66 private SMTPSession setupMockedSMTPSession(final Mail mail) { 67 mockedSMTPSession = new AbstractSMTPSession() { 68 69 private HashMap state = new HashMap (); 70 71 private String ipAddress = "192.168.0.1"; 72 73 private String 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 getRemoteHost() { 87 return host; 88 } 89 90 public String getRemoteIPAddress() { 91 return ipAddress; 92 } 93 94 public Map 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 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 getResponse() { 125 return response; 126 } 127 128 private Mail setupMockedMail(MimeMessage message) { 129 MockMail mail = new MockMail(); 130 mail.setMessage(message); 131 return mail; 132 } 133 134 public MimeMessage setupMockedMimeMessage(String text) 135 throws MessagingException { 136 MimeMessage message = new MimeMessage (new MockMimeMessage()); 137 message.setText(text); 138 message.saveChanges(); 139 140 return message; 141 } 142 143 public MimeMessage setupMockedMimeMessageMP(String text) throws MessagingException { 144 MimeMessage message = new MimeMessage (new MockMimeMessage()); 145 146 BodyPart messageBodyPart = new MimeBodyPart (); 148 149 messageBodyPart.setText(text); 151 152 Multipart multipart = new MimeMultipart (); 153 multipart.addBodyPart(messageBodyPart); 154 message.setContent(multipart); 155 message.saveChanges(); 156 157 return message; 158 } 159 160 161 165 private DNSServer setupMockedDnsServer() { 166 DNSServer mockedDnsServer = new AbstractDNSServer() { 167 168 public Collection findTXTRecords(String hostname) { 169 List res = new ArrayList (); 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 getByName(String host) 181 throws UnknownHostException { 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 (); 188 } 189 throw new UnsupportedOperationException ("getByName("+host+") not implemented by this mock"); 190 } 191 }; 192 193 return mockedDnsServer; 194 } 195 196 public void testNotBlocked() throws IOException , MessagingException { 197 198 ArrayList servers = new ArrayList (); 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 , MessagingException { 215 216 ArrayList servers = new ArrayList (); 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 , MessagingException { 233 234 ArrayList servers = new ArrayList (); 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 , MessagingException { 251 252 ArrayList servers = new ArrayList (); 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 |