1 19 20 21 package org.apache.james.smtpserver; 22 23 import org.apache.avalon.framework.container.ContainerUtil; 24 import org.apache.james.smtpserver.core.POP3BeforeSMTPHandler; 25 import org.apache.james.test.mock.avalon.MockLogger; 26 import org.apache.james.util.POP3BeforeSMTPHelper; 27 28 import junit.framework.TestCase; 29 30 public class POP3BeforeSMTPHandlerTest extends TestCase { 31 32 private SMTPSession mockedSession; 33 34 private void setupMockedSMTPSession() { 35 mockedSession = new AbstractSMTPSession() { 36 private boolean relayingAllowed = false; 37 38 public String getRemoteIPAddress() { 39 return "192.168.200.1"; 40 } 41 42 public boolean isRelayingAllowed() { 43 return relayingAllowed; 44 } 45 46 public void setRelayingAllowed(boolean relayingAllowed) { 47 this.relayingAllowed = relayingAllowed; 48 } 49 50 }; 51 } 52 53 public void testAuthWorks() { 54 55 POP3BeforeSMTPHandler handler = new POP3BeforeSMTPHandler(); 56 57 ContainerUtil.enableLogging(handler, new MockLogger()); 58 setupMockedSMTPSession(); 59 POP3BeforeSMTPHelper.addIPAddress("192.168.200.1"); 60 61 assertFalse(mockedSession.isRelayingAllowed()); 62 handler.onConnect(mockedSession); 63 assertTrue(mockedSession.isRelayingAllowed()); 64 } 65 66 public void testIPGetRemoved() { 67 long sleepTime = 100; 68 POP3BeforeSMTPHandler handler = new POP3BeforeSMTPHandler(); 69 70 ContainerUtil.enableLogging(handler, new MockLogger()); 71 setupMockedSMTPSession(); 72 POP3BeforeSMTPHelper.addIPAddress("192.168.200.1"); 73 assertFalse(mockedSession.isRelayingAllowed()); 74 75 try { 76 Thread.sleep(sleepTime); 77 POP3BeforeSMTPHelper.removeExpiredIP(10); 78 handler.onConnect(mockedSession); 79 assertFalse(mockedSession.isRelayingAllowed()); 80 81 } catch (InterruptedException e) { 82 } 84 } 85 86 public void testThrowExceptionOnIllegalExpireTime() { 87 boolean exception = false; 88 POP3BeforeSMTPHandler handler = new POP3BeforeSMTPHandler(); 89 90 ContainerUtil.enableLogging(handler, new MockLogger()); 91 setupMockedSMTPSession(); 92 93 try { 94 handler.setExpireTime("1 unit"); 95 } catch (NumberFormatException e) { 96 exception = true; 97 } 98 assertTrue(exception); 99 } 100 101 public void testValidExpireTime() { 102 boolean exception = false; 103 POP3BeforeSMTPHandler handler = new POP3BeforeSMTPHandler(); 104 105 ContainerUtil.enableLogging(handler, new MockLogger()); 106 setupMockedSMTPSession(); 107 108 try { 109 handler.setExpireTime("1 hour"); 110 } catch (NumberFormatException e) { 111 exception = true; 112 } 113 assertFalse(exception); 114 } 115 116 } 117 | Popular Tags |