001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.captcha;
017:
018: import junit.framework.TestCase;
019:
020: /**
021: * DOCUMENT ME!
022: *
023: * @author $author$
024: * @version $Revision: 1496 $
025: */
026: public class AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessorTests
027: extends TestCase {
028: //~ Instance fields ================================================================================================
029:
030: AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor;
031:
032: //~ Methods ========================================================================================================
033:
034: protected void setUp() throws Exception {
035: super .setUp();
036: alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor = new AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor();
037: }
038:
039: public void testEqualsThresold() {
040: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
041: alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
042: .setThresold(100);
043:
044: context.setHuman();
045:
046: long now = System.currentTimeMillis();
047:
048: while ((System.currentTimeMillis() - now) <= 100) {
049: assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
050: .isContextValidConcerningHumanity(context));
051: }
052:
053: context.incrementHumanRestrictedRessoucesRequestsCount();
054: assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
055: .isContextValidConcerningHumanity(context));
056:
057: context.setHuman();
058: context.incrementHumanRestrictedRessoucesRequestsCount();
059: assertFalse(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
060: .isContextValidConcerningHumanity(context));
061:
062: alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
063: .setThresold(0);
064: context.setHuman();
065: context.incrementHumanRestrictedRessoucesRequestsCount();
066: assertFalse(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
067: .isContextValidConcerningHumanity(context));
068: alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
069: .setThresold(0);
070: }
071:
072: public void testIsContextValidConcerningHumanity() throws Exception {
073: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
074: alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
075: .setThresold(10);
076: context.setHuman();
077:
078: while ((System.currentTimeMillis() - context
079: .getLastPassedCaptchaDateInMillis()) < (10 * alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
080: .getThresold())) {
081: assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
082: .isContextValidConcerningHumanity(context));
083: }
084: }
085:
086: public void testNewContext() {
087: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
088: assertFalse(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
089: .isContextValidConcerningHumanity(context));
090:
091: context.setHuman();
092: assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
093: .isContextValidConcerningHumanity(context));
094: }
095:
096: public void testShouldPassAbove() {
097: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
098:
099: context.setHuman();
100:
101: int i = 0;
102:
103: while ((System.currentTimeMillis() - context
104: .getLastPassedCaptchaDateInMillis()) < (100 * alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
105: .getThresold())) {
106: System.out.println((System.currentTimeMillis() - context
107: .getLastPassedCaptchaDateInMillis()));
108:
109: context.incrementHumanRestrictedRessoucesRequestsCount();
110: i++;
111:
112: while ((System.currentTimeMillis() - context
113: .getLastPassedCaptchaDateInMillis()) < (alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
114: .getThresold() * i)) {
115: }
116:
117: System.out.println((System.currentTimeMillis() - context
118: .getLastPassedCaptchaDateInMillis()));
119:
120: assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
121: .isContextValidConcerningHumanity(context));
122: }
123: }
124: }
|