KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > smtpserver > TarpitHandlerTest


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

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         // no tarpit used
55
startTime = System.currentTimeMillis();
56         handler.onCommand(setupMockedSession(0));
57         assertTrue("No tarpit",
58                 (System.currentTimeMillis() - startTime) < tarpitTime - tarpitTolerance);
59
60         // tarpit used
61
startTime = System.currentTimeMillis();
62         handler.onCommand(setupMockedSession(3));
63         assertTrue("tarpit",
64                 (System.currentTimeMillis() - startTime) >= tarpitTime - tarpitTolerance);
65     }
66 }
67
Popular Tags