1 19 20 package org.apache.james.smtpserver; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import javax.mail.internet.ParseException ; 28 29 import org.apache.avalon.framework.container.ContainerUtil; 30 import org.apache.james.services.AbstractDNSServer; 31 import org.apache.james.services.DNSServer; 32 import org.apache.james.smtpserver.core.filter.fastfail.ValidSenderDomainHandler; 33 import org.apache.james.test.mock.avalon.MockLogger; 34 import org.apache.james.util.junkscore.JunkScore; 35 import org.apache.james.util.junkscore.JunkScoreImpl; 36 import org.apache.mailet.MailAddress; 37 38 import junit.framework.TestCase; 39 40 public class ValidSenderDomainHandlerTest extends TestCase { 41 42 private String response = null; 43 44 public void setUp() { 45 response = null; 46 } 47 48 private DNSServer setupDNSServer() { 49 DNSServer dns = new AbstractDNSServer(){ 50 51 public Collection findMXRecords(String hostname) { 52 Collection mx = new ArrayList (); 53 if (hostname.equals("test.james.apache.org")) { 54 mx.add("mail.james.apache.org"); 55 } 56 return mx; 57 } 58 59 }; 60 return dns; 61 } 62 63 private SMTPSession setupMockedSession(final MailAddress sender) { 64 SMTPSession session = new AbstractSMTPSession() { 65 HashMap state = new HashMap (); 66 boolean processing = false; 67 68 public Map getState() { 69 70 state.put(SMTPSession.SENDER, sender); 71 72 return state; 73 } 74 75 public boolean isRelayingAllowed() { 76 return false; 77 } 78 79 public void writeResponse(String resp) { 80 response = resp; 81 } 82 83 public void setStopHandlerProcessing(boolean processing) { 84 this.processing = processing; 85 } 86 87 public boolean getStopHandlerProcessing() { 88 return processing; 89 } 90 91 }; 92 return session; 93 } 94 95 private String getResponse() { 96 return response; 97 } 98 99 public void testNullSenderNotReject() { 101 ValidSenderDomainHandler handler = new ValidSenderDomainHandler(); 102 ContainerUtil.enableLogging(handler, new MockLogger()); 103 104 handler.setDnsServer(setupDNSServer()); 105 handler.onCommand(setupMockedSession(null)); 106 107 assertNull("Not blocked cause its a nullsender",getResponse()); 108 } 109 110 public void testInvalidSenderDomainAddJunkScore() throws ParseException { 111 ValidSenderDomainHandler handler = new ValidSenderDomainHandler(); 112 SMTPSession session = setupMockedSession(new MailAddress("invalid@invalid")); 113 ContainerUtil.enableLogging(handler, new MockLogger()); 114 session.getState().put(JunkScore.JUNK_SCORE, new JunkScoreImpl()); 115 handler.setAction("JunkScore"); 116 handler.setScore(20); 117 handler.setDnsServer(setupDNSServer()); 118 handler.onCommand(session); 119 120 assertNull("Not blocked cause we use JunkScore",getResponse()); 121 assertEquals("JunkScore is stored",((JunkScore) session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("ValidSenderDomainCheck"),20.0,0d); 122 } 123 124 public void testInvalidSenderDomainReject() throws ParseException { 125 ValidSenderDomainHandler handler = new ValidSenderDomainHandler(); 126 SMTPSession session = setupMockedSession(new MailAddress("invalid@invalid")); 127 ContainerUtil.enableLogging(handler, new MockLogger()); 128 handler.setDnsServer(setupDNSServer()); 129 handler.onCommand(session); 130 131 assertNotNull("Blocked cause we use reject action",getResponse()); 132 } 133 } 134 | Popular Tags |