1 19 20 21 package org.apache.james.transport.matchers; 22 23 import org.apache.james.Constants; 24 import org.apache.james.services.AbstractDNSServer; 25 import org.apache.james.services.DNSServer; 26 import org.apache.james.test.mock.avalon.MockServiceManager; 27 import org.apache.james.test.mock.mailet.MockMail; 28 import org.apache.james.test.mock.mailet.MockMailContext; 29 import org.apache.james.test.mock.mailet.MockMatcherConfig; 30 31 import org.apache.mailet.MailAddress; 32 import org.apache.mailet.Matcher; 33 34 import javax.mail.MessagingException ; 35 import javax.mail.internet.ParseException ; 36 37 import java.io.UnsupportedEncodingException ; 38 import java.net.InetAddress ; 39 import java.net.UnknownHostException ; 40 import java.util.Arrays ; 41 import java.util.Collection ; 42 43 import junit.framework.TestCase; 44 45 public class InSpammerBlacklistTest extends TestCase { 46 47 private MockMail mockedMail; 48 49 private Matcher matcher; 50 51 private final static String BLACKLIST = "my.black.list."; 52 private final static StringBuffer LISTED_HOST = new StringBuffer ("111.222.111.222"); 53 54 public InSpammerBlacklistTest(String arg0) throws UnsupportedEncodingException { 55 super(arg0); 56 } 57 58 private MockServiceManager setUpServiceManager() { 59 MockServiceManager sMan = new MockServiceManager(); 60 sMan.put(DNSServer.ROLE, setUpDNSServer()); 61 return sMan; 62 } 63 64 private DNSServer setUpDNSServer() { 65 DNSServer dns = new AbstractDNSServer() { 66 public InetAddress getByName(String name) throws UnknownHostException { 67 if (name.equals(LISTED_HOST.reverse() + "." + BLACKLIST)) { 68 return null; 69 } else { 70 throw new UnknownHostException ("Not listed"); 71 } 72 } 73 }; 74 return dns; 75 } 76 private void setupMockedMail(String remoteAddr) throws ParseException { 77 mockedMail = new MockMail(); 78 mockedMail.setRemoteAddr(remoteAddr); 79 mockedMail.setRecipients(Arrays.asList(new MailAddress[] {new MailAddress("test@email")})); 80 81 } 82 83 private void setupMatcher(String blacklist) throws MessagingException { 84 matcher = new InSpammerBlacklist(); 85 MockMailContext context = new MockMailContext(); 86 context.setAttribute(Constants.AVALON_COMPONENT_MANAGER, setUpServiceManager()); 87 MockMatcherConfig mci = new MockMatcherConfig("InSpammerBlacklist=" + blacklist,context); 88 matcher.init(mci); 89 } 90 91 92 public void testInBlackList() throws MessagingException { 93 setupMockedMail(LISTED_HOST.toString()); 94 setupMatcher(BLACKLIST); 95 96 Collection matchedRecipients = matcher.match(mockedMail); 97 98 assertNotNull(matchedRecipients); 99 assertEquals(matchedRecipients.size(), mockedMail.getRecipients().size()); 100 } 101 102 public void testNotInBlackList() throws MessagingException { 103 setupMockedMail("212.12.14.1"); 104 setupMatcher(BLACKLIST); 105 106 Collection matchedRecipients = matcher.match(mockedMail); 107 108 assertNull(matchedRecipients); 109 } 110 } 111 | Popular Tags |