1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.changepw.service;
21  
22  
23  import javax.security.auth.kerberos.KerberosPrincipal;
24  
25  import junit.framework.TestCase;
26  
27  
28  /**
29   * Tests {@link CheckPasswordPolicy}.
30   * 
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @version $Rev: 540371 $, $Date: 2007-05-22 02:00:43 +0200 (Di, 22 Mai 2007) $
33   */
34  public class CheckPasswordPolicyTest extends TestCase
35  {
36      private int passwordLength = 6;
37      private int categoryCount = 3;
38      private int tokenSize = 3;
39  
40      private CheckPasswordPolicy policy = new CheckPasswordPolicy();
41  
42  
43      /**
44       * Tests that a good password is valid according to all policy checks.
45       */
46      public void testGoodPassword()
47      {
48          String username = "Enrique Rodriguez";
49          String password = "d1r3ct0rY";
50          assertTrue( policy.isValidPasswordLength( password, passwordLength ) );
51          assertTrue( policy.isValidCategoryCount( password, categoryCount ) );
52          assertTrue( policy.isValidUsernameSubstring( username, password, tokenSize ) );
53          assertTrue( policy.isValid( username, password, passwordLength, categoryCount, tokenSize ) );
54      }
55  
56  
57      /**
58       * Tests that a bad password fails all validity checks.
59       */
60      public void testBadPassword()
61      {
62          String username = "Erin Randall";
63          String password = "erin1";
64          assertFalse( policy.isValidPasswordLength( password, passwordLength ) );
65          assertFalse( policy.isValidCategoryCount( password, categoryCount ) );
66          assertFalse( policy.isValidUsernameSubstring( username, password, tokenSize ) );
67          assertFalse( policy.isValid( username, password, passwordLength, categoryCount, tokenSize ) );
68      }
69  
70  
71      /**
72       * Tests variations of a password where the password includes tokens of the username.
73       */
74      public void testPrincipalAsUsername()
75      {
76          String username = new KerberosPrincipal( "erodriguez@EXAMPLE.COM" ).getName();
77          String password1 = "d1r3ct0rY";
78          String password2 = "ERodriguez@d1r3ct0rY";
79          String password3 = "Example@d1r3ct0rY";
80  
81          assertTrue( policy.isValidUsernameSubstring( username, password1, tokenSize ) );
82  
83          assertFalse( policy.isValidUsernameSubstring( username, password2, tokenSize ) );
84          assertFalse( policy.isValidUsernameSubstring( username, password3, tokenSize ) );
85      }
86  }