1 19 20 21 22 package org.apache.james.smtpserver; 23 24 import java.net.InetAddress ; 25 import java.net.UnknownHostException ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import javax.mail.internet.ParseException ; 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 INVALID_HOST = "foo.bar"; 45 46 public final static String VALID_HOST = "james.apache.org"; 47 48 public final static String HELO = "HELO"; 49 50 public final static String RCPT = "RCPT"; 51 52 private String response = null; 53 54 private String command = null; 55 56 public void setUp() { 57 response = null; 58 command = null; 59 } 60 61 private String getResponse() { 62 return response; 63 } 64 65 private void setCommand(String command) { 66 this.command = command; 67 } 68 69 private SMTPSession setupMockSession(final String argument, 70 final boolean relaying, final boolean authRequired, final String user, final MailAddress recipient) { 71 72 SMTPSession session = new AbstractSMTPSession() { 73 74 boolean stop = false; 75 HashMap connectionMap = new HashMap (); 76 HashMap map = new HashMap (); 77 78 public String getCommandArgument() { 79 return argument; 80 } 81 82 public String getCommandName() { 83 return command; 84 } 85 86 public boolean isAuthRequired() { 87 return authRequired; 88 } 89 90 public String getUser() { 91 return user; 92 } 93 94 public Map getConnectionState() { 95 return connectionMap; 96 } 97 98 public void writeResponse(String 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 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 getByName(String host) throws UnknownHostException { 128 if (host.equals(INVALID_HOST)) 129 throw new UnknownHostException (); 130 return InetAddress.getLocalHost(); 131 } 132 }; 133 134 return dns; 135 } 136 137 public void testRejectInvalidHelo() throws ParseException { 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 setCommand(HELO); 147 handler.onCommand(session); 148 assertNotNull("Invalid HELO",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO)); 149 150 151 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 { 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 setCommand(HELO); 170 handler.onCommand(session); 171 assertNull("Valid HELO",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO)); 172 173 174 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 { 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 setCommand(HELO); 192 handler.onCommand(session); 193 assertNotNull("Value stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO)); 194 195 196 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 { 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 setCommand(HELO); 215 handler.onCommand(session); 216 assertNotNull("Value stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO)); 217 218 219 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 { 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 setCommand(HELO); 237 handler.onCommand(session); 238 assertNull("Value not stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO)); 239 240 241 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 { 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 setCommand(HELO); 260 handler.onCommand(session); 261 assertNotNull("Value stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO)); 262 263 264 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 { 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 setCommand(HELO); 282 handler.onCommand(session); 283 assertNotNull("stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO)); 284 285 286 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 { 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 setCommand(HELO); 304 handler.onCommand(session); 305 assertNotNull("stored",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO)); 306 307 308 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 { 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 setCommand(HELO); 329 handler.onCommand(session); 330 assertNotNull("Invalid HELO",session.getState().get(ResolvableEhloHeloHandler.BAD_EHLO_HELO)); 331 332 333 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 |