1
2
3
4
5
6
7
8
9
10
11
12
13
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
45
46 AlwaysTestAfterTimeInMillisCaptchaChannelProcessor alwaysTestAfterTimeInMillisCaptchaChannelProcessor;
47
48
49
50 public void testEqualsThresold() {
51 CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
52 assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor
53 .isContextValidConcerningHumanity(context));
54
55
56
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
90 assertFalse(alwaysTestAfterTimeInMillisCaptchaChannelProcessor
91 .isContextValidConcerningHumanity(context));
92 }
93
94 protected void setUp() throws Exception {
95 super.setUp();
96 alwaysTestAfterTimeInMillisCaptchaChannelProcessor = new AlwaysTestAfterTimeInMillisCaptchaChannelProcessor();
97 }
98 }