1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * 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  /***
24   * WARNING! This test class make some assumptions concerning the compute speed!
25   * For example the two following instructions should be computed in the same
26   * millis or the test is not valid.
27   * <pre><code>
28   * context.setHuman();
29   * assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor.isContextValidConcerningHumanity(context));
30   * </code></pre>
31   * This should be the case for most environements unless
32   * 
33   * <ul>
34   * <li>
35   * you run it on a good old TRS-80
36   * </li>
37   * <li>
38   * you start M$office during this test ;)
39   * </li>
40   * </ul>
41   */
42  public class AlwaysTestAfterTimeInMillisCaptchaChannelProcessorTests
43      extends TestCase {
44      //~ Instance fields ========================================================
45  
46      AlwaysTestAfterTimeInMillisCaptchaChannelProcessor alwaysTestAfterTimeInMillisCaptchaChannelProcessor;
47  
48      //~ Methods ================================================================
49  
50      public void testEqualsThresold() {
51          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
52          assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor
53              .isContextValidConcerningHumanity(context));
54  
55          //the two following instructions should be computed or the test is not valid (never fails). This should be the case
56          // for most environements unless if you run it on a good old TRS-80 (thanks mom).
57          context.setHuman();
58          assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor
59              .isContextValidConcerningHumanity(context));
60      }
61  
62      public void testIsContextValidConcerningHumanity()
63          throws Exception {
64          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
65          alwaysTestAfterTimeInMillisCaptchaChannelProcessor.setThresold(100);
66          context.setHuman();
67  
68          while ((System.currentTimeMillis()
69              - context.getLastPassedCaptchaDateInMillis()) < alwaysTestAfterTimeInMillisCaptchaChannelProcessor
70              .getThresold()) {
71              assertTrue(alwaysTestAfterTimeInMillisCaptchaChannelProcessor
72                  .isContextValidConcerningHumanity(context));
73              context.incrementHumanRestrictedRessoucesRequestsCount();
74  
75              long now = System.currentTimeMillis();
76  
77              while ((System.currentTimeMillis() - now) < 1) {}
78  
79              ;
80          }
81  
82          assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor
83              .isContextValidConcerningHumanity(context));
84      }
85  
86      public void testNewContext() {
87          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
88  
89          //alwaysTestAfterTimeInMillisCaptchaChannelProcessor.setThresold(10);
90          assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor
91              .isContextValidConcerningHumanity(context));
92      }
93  
94      protected void setUp() throws Exception {
95          super.setUp();
96          alwaysTestAfterTimeInMillisCaptchaChannelProcessor = new AlwaysTestAfterTimeInMillisCaptchaChannelProcessor();
97      }
98  }