01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15:
16: package org.acegisecurity.captcha;
17:
18: import junit.framework.*;
19:
20: import org.acegisecurity.captcha.AlwaysTestAfterTimeInMillisCaptchaChannelProcessor;
21:
22: /**
23: * WARNING! This test class make some assumptions concerning the compute speed! For example the two following
24: * instructions should be computed in the same millis or the test is not valid.<pre><code>context.setHuman();
25: * assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
26: * </code></pre>This should be the case for most environements unless
27: * <ul>
28: * <li>you run it on a good old TRS-80</li>
29: * <li>you start M$office during this test ;)</li>
30: * </ul>
31: */
32: public class AlwaysTestAfterTimeInMillisCaptchaChannelProcessorTests
33: extends TestCase {
34: //~ Instance fields ================================================================================================
35:
36: AlwaysTestAfterTimeInMillisCaptchaChannelProcessor alwaysTestAfterTimeInMillisCaptchaChannelProcessor;
37:
38: //~ Methods ========================================================================================================
39:
40: protected void setUp() throws Exception {
41: super .setUp();
42: alwaysTestAfterTimeInMillisCaptchaChannelProcessor = new AlwaysTestAfterTimeInMillisCaptchaChannelProcessor();
43: }
44:
45: public void testEqualsThresold() {
46: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
47: assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor
48: .isContextValidConcerningHumanity(context));
49:
50: //the two following instructions should be computed or the test is not valid (never fails). This should be the case
51: // for most environements unless if you run it on a good old TRS-80 (thanks mom).
52: context.setHuman();
53: assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor
54: .isContextValidConcerningHumanity(context));
55: }
56:
57: public void testIsContextValidConcerningHumanity() throws Exception {
58: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
59: alwaysTestAfterTimeInMillisCaptchaChannelProcessor
60: .setThresold(100);
61: context.setHuman();
62:
63: while ((System.currentTimeMillis() - context
64: .getLastPassedCaptchaDateInMillis()) < alwaysTestAfterTimeInMillisCaptchaChannelProcessor
65: .getThresold()) {
66: assertTrue(alwaysTestAfterTimeInMillisCaptchaChannelProcessor
67: .isContextValidConcerningHumanity(context));
68: context.incrementHumanRestrictedRessoucesRequestsCount();
69:
70: long now = System.currentTimeMillis();
71:
72: while ((System.currentTimeMillis() - now) < 1) {
73: }
74:
75: ;
76: }
77:
78: assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor
79: .isContextValidConcerningHumanity(context));
80: }
81:
82: public void testNewContext() {
83: CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
84:
85: //alwaysTestAfterTimeInMillisCaptchaChannelProcessor.setThresold(10);
86: assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor
87: .isContextValidConcerningHumanity(context));
88: }
89: }
|