1 19 package org.apache.james.smtpserver; 20 21 import org.apache.avalon.framework.container.ContainerUtil; 22 import org.apache.james.smtpserver.core.filter.fastfail.TarpitHandler; 23 import org.apache.james.test.mock.avalon.MockLogger; 24 25 import junit.framework.TestCase; 26 27 public class TarpitHandlerTest extends TestCase { 28 29 private SMTPSession session; 30 31 private SMTPSession setupMockedSession(final int rcptCount) { 32 session = new AbstractSMTPSession() { 33 34 public int getRcptCount() { 35 return rcptCount; 36 } 37 38 }; 39 40 return session; 41 } 42 43 public void testTarpit() { 44 long tarpitTime = 1000; 45 long tarpitTolerance = 100; 46 long startTime; 47 TarpitHandler handler = new TarpitHandler(); 48 49 ContainerUtil.enableLogging(handler, new MockLogger()); 50 51 handler.setTarpitRcptCount(2); 52 handler.setTarpitSleepTime(tarpitTime); 53 54 startTime = System.currentTimeMillis(); 56 handler.onCommand(setupMockedSession(0)); 57 assertTrue("No tarpit", 58 (System.currentTimeMillis() - startTime) < tarpitTime - tarpitTolerance); 59 60 startTime = System.currentTimeMillis(); 62 handler.onCommand(setupMockedSession(3)); 63 assertTrue("tarpit", 64 (System.currentTimeMillis() - startTime) >= tarpitTime - tarpitTolerance); 65 } 66 } 67 | Popular Tags |