1 19 20 21 package org.apache.james.smtpserver; 22 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import javax.mail.MessagingException ; 27 import javax.mail.internet.MimeMessage ; 28 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.container.ContainerUtil; 31 import org.apache.james.smtpserver.core.filter.fastfail.JunkScoreHandler; 32 import org.apache.james.test.mock.avalon.MockLogger; 33 import org.apache.james.test.mock.javaxmail.MockMimeMessage; 34 import org.apache.james.test.mock.mailet.MockMail; 35 import org.apache.james.util.junkscore.JunkScore; 36 import org.apache.mailet.Mail; 37 38 import junit.framework.TestCase; 39 40 public class JunkScoreHandlerTest extends TestCase { 41 private String response = null; 42 private boolean stopped = false; 43 private boolean messageAborted = false; 44 private final static String KEY1 = "KEY1"; 45 private final static String KEY2 = "KEY2"; 46 private final static double SCORE1 = 10.0; 47 private final static double SCORE2 = 7.1; 48 49 public void setUp() { 50 response = null; 51 stopped = false; 52 messageAborted = false; 53 } 54 55 private SMTPSession setupMockedSMTPSession() { 56 SMTPSession session = new AbstractSMTPSession() { 57 HashMap state = new HashMap (); 58 HashMap cState = new HashMap (); 59 Mail m = null; 60 61 public Map getState() { 62 state.put(SMTPSession.SENDER, "sender@localhost"); 63 return state; 64 } 65 66 public Map getConnectionState() { 67 return cState; 68 } 69 70 public void writeResponse(String resp) { 71 response = resp; 72 } 73 74 public void setStopHandlerProcessing(boolean b) { 75 stopped = b; 76 } 77 78 public void abortMessage() { 79 messageAborted = true; 80 } 81 82 public Mail getMail(){ 83 if (m == null) m = getMockMail(); 84 return m; 85 } 86 87 public String getRemoteHost() { 88 return "anyHost"; 89 } 90 91 public String getRemoteIPAddress() { 92 return "000.000.000.001"; 93 } 94 }; 95 return session; 96 } 97 98 private Mail getMockMail(){ 99 Mail m = new MockMail(); 100 try { 101 m.setMessage(new MockMimeMessage()); 102 } catch (MessagingException e) { 103 e.printStackTrace(); 104 } 105 return m; 106 } 107 108 public void testIllegalActionThrowException() { 109 boolean exception = false; 110 JunkScoreHandler handler = new JunkScoreHandler(); 111 ContainerUtil.enableLogging(handler,new MockLogger()); 112 113 try { 114 handler.setAction("invalid"); 115 } catch (ConfigurationException e) { 116 exception = true; 117 } 118 119 assertTrue("Exception thrown",exception); 120 } 121 122 public void testRejectAction() throws ConfigurationException { 123 124 SMTPSession session = setupMockedSMTPSession(); 125 JunkScoreHandler handler = new JunkScoreHandler(); 126 ContainerUtil.enableLogging(handler,new MockLogger()); 127 128 handler.setAction("reject"); 129 handler.setMaxScore(15.0); 130 handler.onConnect(session); 131 handler.onMessage(session); 132 133 assertNull("Not rejected",response); 134 ((JunkScore) session.getState().get(JunkScore.JUNK_SCORE)).setStoredScore(KEY1, SCORE1); 135 ((JunkScore) session.getConnectionState().get(JunkScore.JUNK_SCORE_SESSION)).setStoredScore(KEY2, SCORE2); 136 handler.onMessage(session); 137 138 assertNotNull("Rejected",response); 139 assertTrue("Rejected",stopped); 140 assertTrue("Rejected",messageAborted); 141 } 142 143 public void testHeaderAction() throws ConfigurationException, MessagingException { 144 SMTPSession session = setupMockedSMTPSession(); 145 JunkScoreHandler handler = new JunkScoreHandler(); 146 ContainerUtil.enableLogging(handler,new MockLogger()); 147 148 handler.setAction("header"); 149 150 handler.onConnect(session); 151 ((JunkScore) session.getState().get(JunkScore.JUNK_SCORE)).setStoredScore(KEY1, SCORE1); 152 ((JunkScore) session.getConnectionState().get(JunkScore.JUNK_SCORE_SESSION)).setStoredScore(KEY2, SCORE2); 153 handler.onMessage(session); 154 155 MimeMessage message = session.getMail().getMessage(); 156 assertNotNull("Header added",message.getHeader("X-JUNKSCORE")[0]); 157 assertNotNull("Header added",message.getHeader("X-JUNKSCORE-COMPOSED")[0]); 158 } 159 } 160 | Popular Tags |