1 19 20 21 package org.apache.james.smtpserver; 22 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import javax.mail.MessagingException ; 28 import javax.mail.internet.MimeMessage ; 29 30 import junit.framework.TestCase; 31 32 import org.apache.avalon.framework.container.ContainerUtil; 33 import org.apache.james.smtpserver.core.filter.fastfail.SpamAssassinHandler; 34 import org.apache.james.test.mock.avalon.MockLogger; 35 import org.apache.james.test.mock.javaxmail.MockMimeMessage; 36 import org.apache.james.test.mock.mailet.MockMail; 37 import org.apache.james.test.mock.util.MockSpamd; 38 import org.apache.james.test.util.Util; 39 import org.apache.james.util.SpamAssassinInvoker; 40 import org.apache.mailet.Mail; 41 42 public class SpamAssassinHandlerTest extends TestCase { 43 private SMTPSession mockedSMTPSession; 44 45 private String response = null; 46 47 public final static String SPAMD_HOST = "localhost"; 48 49 public void setUp() { 50 response = null; 52 } 53 54 private SMTPSession setupMockedSMTPSession(final Mail mail) { 55 mockedSMTPSession = new AbstractSMTPSession() { 56 57 private HashMap state = new HashMap (); 58 59 private String ipAddress = "192.168.0.1"; 60 61 private String host = "localhost"; 62 63 private boolean relayingAllowed; 64 65 public void abortMessage() { 66 } 67 68 public Mail getMail() { 69 return mail; 70 } 71 72 public String getRemoteHost() { 73 return host; 74 } 75 76 public String getRemoteIPAddress() { 77 return ipAddress; 78 } 79 80 public Map getState() { 81 state.put(SMTPSession.SENDER, "sender@james.apache.org"); 82 return state; 83 } 84 85 public boolean isRelayingAllowed() { 86 return relayingAllowed; 87 } 88 89 public void setRelayingAllowed(boolean relayingAllowed) { 90 this.relayingAllowed = relayingAllowed; 91 } 92 93 public void writeResponse(String respString) { 94 response = respString; 95 } 96 }; 97 98 return mockedSMTPSession; 99 100 } 101 102 private String getResponse() { 103 return response; 104 } 105 106 private Mail setupMockedMail(MimeMessage message) { 107 MockMail mail = new MockMail(); 108 mail.setMessage(message); 109 return mail; 110 } 111 112 public MimeMessage setupMockedMimeMessage(String text) 113 throws MessagingException { 114 MimeMessage message = new MimeMessage (new MockMimeMessage()); 115 message.setText(text); 116 message.saveChanges(); 117 118 return message; 119 } 120 121 public void testNonSpam() throws IOException , MessagingException { 122 123 int port = Util.getNonPrivilegedPort(); 124 MockSpamd spamd = new MockSpamd(port); 125 new Thread (spamd).start(); 126 127 SMTPSession session = setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage("test"))); 128 129 SpamAssassinHandler handler = new SpamAssassinHandler(); 130 131 ContainerUtil.enableLogging(handler, new MockLogger()); 132 handler.setSpamdHost(SPAMD_HOST); 133 handler.setSpamdPort(port); 134 handler.setSpamdRejectionHits(200.0); 135 handler.onMessage(session); 136 137 assertNull("Email was not rejected", getResponse()); 138 assertEquals("email was not spam", session.getMail().getAttribute( 139 SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME), "NO"); 140 assertNotNull("spam hits", session.getMail().getAttribute( 141 SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME)); 142 143 } 144 145 public void testSpam() throws IOException , MessagingException { 146 147 int port = Util.getNonPrivilegedPort(); 148 new Thread (new MockSpamd(port)).start(); 149 150 SMTPSession session = setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage(MockSpamd.GTUBE))); 151 152 SpamAssassinHandler handler = new SpamAssassinHandler(); 153 154 ContainerUtil.enableLogging(handler, new MockLogger()); 155 handler.setSpamdHost(SPAMD_HOST); 156 handler.setSpamdPort(port); 157 handler.setSpamdRejectionHits(2000.0); 158 handler.onMessage(session); 159 160 assertNull("Email was not rejected", getResponse()); 161 assertEquals("email was spam", session.getMail().getAttribute( 162 SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME), "YES"); 163 assertNotNull("spam hits", session.getMail().getAttribute( 164 SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME)); 165 } 166 167 public void testSpamReject() throws IOException , MessagingException { 168 169 int port = Util.getNonPrivilegedPort(); 170 new Thread (new MockSpamd(port)).start(); 171 172 SMTPSession session = setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage(MockSpamd.GTUBE))); 173 174 SpamAssassinHandler handler = new SpamAssassinHandler(); 175 176 ContainerUtil.enableLogging(handler, new MockLogger()); 177 handler.setSpamdHost(SPAMD_HOST); 178 handler.setSpamdPort(port); 179 handler.setSpamdRejectionHits(200.0); 180 handler.onMessage(session); 181 182 assertNotNull("Email was rejected", getResponse()); 183 assertEquals("email was spam", session.getMail().getAttribute( 184 SpamAssassinInvoker.FLAG_MAIL_ATTRIBUTE_NAME), "YES"); 185 assertNotNull("spam hits", session.getMail().getAttribute( 186 SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME)); 187 } 188 189 } 190 | Popular Tags |