    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.TestCase;
    19
    20
    21  /***
    22   * DOCUMENT ME!
    23   *
    24   * @author $author$
    25   * @version $Revision: 1.2 $
    26   */
    27  public class AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessorTests
    28      extends TestCase {
    29      //~ Instance fields ========================================================
    30
    31      AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor;
    32
    33      //~ Methods ================================================================
    34
    35      public void testEqualsThresold() {
    36          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
    37          alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    38          .setThresold(100);
    39
    40          context.setHuman();
    41
    42          long now = System.currentTimeMillis();
    43
    44          while ((System.currentTimeMillis() - now) <= 100) {
    45              assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    46                  .isContextValidConcerningHumanity(context));
    47          }
    48
    49          context.incrementHumanRestrictedRessoucesRequestsCount();
    50          assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    51              .isContextValidConcerningHumanity(context));
    52
    53          context.setHuman();
    54          context.incrementHumanRestrictedRessoucesRequestsCount();
    55          assertFalse(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    56              .isContextValidConcerningHumanity(context));
    57
    58          alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    59          .setThresold(0);
    60          context.setHuman();
    61          context.incrementHumanRestrictedRessoucesRequestsCount();
    62          assertFalse(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    63              .isContextValidConcerningHumanity(context));
    64          alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    65          .setThresold(0);
    66      }
    67
    68      public void testIsContextValidConcerningHumanity()
    69          throws Exception {
    70          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
    71          alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    72          .setThresold(10);
    73          context.setHuman();
    74
    75          while ((System.currentTimeMillis()
    76              - context.getLastPassedCaptchaDateInMillis()) < (10 * alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    77              .getThresold())) {
    78              assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    79                  .isContextValidConcerningHumanity(context));
    80          }
    81      }
    82
    83      public void testNewContext() {
    84          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
    85          assertFalse(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    86              .isContextValidConcerningHumanity(context));
    87
    88          context.setHuman();
    89          assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    90              .isContextValidConcerningHumanity(context));
    91      }
    92
    93      public void testShouldPassAbove() {
    94          CaptchaSecurityContext context = new CaptchaSecurityContextImpl();
    95
    96          context.setHuman();
    97
    98          int i = 0;
    99
    100         while ((System.currentTimeMillis()
    101             - context.getLastPassedCaptchaDateInMillis()) < (100 * alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    102             .getThresold())) {
    103             System.out.println((System.currentTimeMillis()
    104                 - context.getLastPassedCaptchaDateInMillis()));
    105
    106             context.incrementHumanRestrictedRessoucesRequestsCount();
    107             i++;
    108
    109             while ((System.currentTimeMillis()
    110                 - context.getLastPassedCaptchaDateInMillis()) < (alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    111                 .getThresold() * i)) {}
    112
    113             System.out.println((System.currentTimeMillis()
    114                 - context.getLastPassedCaptchaDateInMillis()));
    115
    116             assertTrue(alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor
    117                 .isContextValidConcerningHumanity(context));
    118         }
    119     }
    120
    121     protected void setUp() throws Exception {
    122         super.setUp();
    123         alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor = new AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor();
    124     }
    125 }