1 19 20 package org.apache.james.smtpserver; 21 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import javax.mail.internet.ParseException ; 30 31 import org.apache.avalon.framework.container.ContainerUtil; 32 import org.apache.james.services.AbstractDNSServer; 33 import org.apache.james.services.DNSServer; 34 import org.apache.james.smtpserver.core.filter.fastfail.ValidRcptMX; 35 import org.apache.james.test.mock.avalon.MockLogger; 36 import org.apache.james.util.junkscore.JunkScore; 37 import org.apache.james.util.junkscore.JunkScoreImpl; 38 import org.apache.mailet.MailAddress; 39 40 import junit.framework.TestCase; 41 42 public class ValidRcptMXTest extends TestCase { 43 private String response = null; 44 45 private final static String INVALID_HOST = "invalid.host.de"; 46 47 private final static String INVALID_MX = "mx." + INVALID_HOST; 48 49 private final static String LOOPBACK = "127.0.0.1"; 50 51 protected void setUp() throws Exception { 52 response = null; 53 54 super.setUp(); 55 } 56 57 private SMTPSession setupMockedSMTPSession(final MailAddress rcpt) { 58 SMTPSession session = new AbstractSMTPSession() { 59 HashMap state = new HashMap (); 60 boolean stopProcessing = false; 61 62 public Map getState() { 63 state.put(SMTPSession.CURRENT_RECIPIENT, rcpt); 64 return state; 65 } 66 67 public void writeResponse(String resp) { 68 response = resp; 69 } 70 71 public String getRemoteIPAddress() { 72 return "127.0.0.1"; 73 } 74 75 public void setStopHandlerProcessing(boolean stopProcessing) { 76 this.stopProcessing = stopProcessing; 77 } 78 79 }; 80 return session; 81 } 82 83 private DNSServer setupMockedDNSServer() { 84 DNSServer dns = new AbstractDNSServer() { 85 86 public Collection findMXRecords(String hostname) { 87 Collection mx = new ArrayList (); 88 89 if (hostname.equals(INVALID_HOST)) { 90 mx.add(INVALID_MX); 91 } 92 return mx; 93 } 94 95 public InetAddress getByName(String host) throws UnknownHostException { 96 if (host.equals(INVALID_MX) || host.equals(LOOPBACK)) { 97 return InetAddress.getByName(LOOPBACK); 98 } else if (host.equals("255.255.255.255")) { 99 return InetAddress.getByName("255.255.255.255"); 100 } 101 throw new UnknownHostException ("Unknown host"); 102 } 103 104 }; 105 106 return dns; 107 } 108 109 public void testRejectLoopbackMX() throws ParseException { 110 Collection bNetworks = new ArrayList (); 111 bNetworks.add("127.0.0.1"); 112 113 DNSServer dns = setupMockedDNSServer(); 114 ValidRcptMX handler = new ValidRcptMX(); 115 116 ContainerUtil.enableLogging(handler, new MockLogger()); 117 118 handler.setDNSServer(dns); 119 handler.setBannedNetworks(bNetworks, dns); 120 handler.onCommand(setupMockedSMTPSession(new MailAddress("test@" + INVALID_HOST))); 121 122 assertNotNull("Reject", response); 123 } 124 125 126 public void testAddJunkScoreLoopbackMX() throws ParseException { 127 Collection bNetworks = new ArrayList (); 128 bNetworks.add("127.0.0.1"); 129 130 SMTPSession session = setupMockedSMTPSession(new MailAddress("test@" + INVALID_HOST)); 131 session.getState().put(JunkScore.JUNK_SCORE, new JunkScoreImpl()); 132 133 DNSServer dns = setupMockedDNSServer(); 134 ValidRcptMX handler = new ValidRcptMX(); 135 handler.setScore(20); 136 handler.setAction("junkScore"); 137 138 ContainerUtil.enableLogging(handler, new MockLogger()); 139 140 handler.setDNSServer(dns); 141 handler.setBannedNetworks(bNetworks, dns); 142 handler.onCommand(session); 143 144 assertNull("Not Reject", response); 145 assertEquals("JunkScore added",((JunkScore) session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("ValidRcptMXCheck"),20.0, 0d); 146 } 147 } 148 | Popular Tags |