1 19 20 21 package org.apache.james.smtpserver; 22 23 import java.net.InetAddress ; 24 import java.net.UnknownHostException ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import javax.mail.internet.ParseException ; 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 remoteIp = "127.0.0.2"; 54 55 private boolean relaying = false; 56 57 public static final String RBL_BLOCKLISTED_MAIL_ATTRIBUTE_NAME = "org.apache.james.smtpserver.rbl.blocklisted"; 58 59 public static final String RBL_DETAIL_MAIL_ATTRIBUTE_NAME = "org.apache.james.smtpserver.rbl.detail"; 60 61 protected void setUp() throws Exception { 62 super.setUp(); 63 setupMockedDnsServer(); 64 setRelayingAllowed(false); 65 } 66 67 72 private void setRemoteIp(String remoteIp) { 73 this.remoteIp = remoteIp; 74 } 75 76 81 private void setRelayingAllowed(boolean relaying) { 82 this.relaying = relaying; 83 } 84 85 89 private void setupMockedDnsServer() { 90 mockedDnsServer = new AbstractDNSServer() { 91 92 public Collection findMXRecords(String hostname) { 93 throw new UnsupportedOperationException ("Unimplemented in mock"); 94 } 95 96 public Collection findTXTRecords(String hostname) { 97 List res = new ArrayList (); 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 getByName(String host) 109 throws UnknownHostException { 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 (host); 116 } 117 throw new UnsupportedOperationException ("getByName("+host+") not implemented in DNSRBLHandlerTest mock"); 118 } 119 }; 120 } 121 122 125 private void setupMockedSMTPSession(final MailAddress rcpt) { 126 mockedSMTPSession = new AbstractSMTPSession() { 127 HashMap state = new HashMap (); 128 HashMap connectionState = new HashMap (); 129 boolean stopHandler = false; 130 131 public String getRemoteIPAddress() { 132 return remoteIp; 133 } 134 135 public Map 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 getConnectionState() { 161 return connectionState; 162 } 163 164 public void resetConnectionState() { 165 connectionState.clear(); 166 } 167 168 }; 169 } 170 171 public void testBlackListedTextPresent() throws ParseException { 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 [] { "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 public void testGetNoDetail() throws ParseException { 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 [] { "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 public void testRelayAllowed() throws ParseException { 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 [] { "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 public void testNotBlackListed() throws ParseException { 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 [] { "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 public void testBlackListedNoTxt() throws ParseException { 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 [] { "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 public void testWhiteListed() throws ParseException { 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 [] { "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 { 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 [] { "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 |