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  
21  package org.apache.directory.server.core.authn;
22  
23  
24  import org.apache.directory.server.core.authn.SimpleAuthenticator;
25  import org.apache.directory.shared.ldap.util.StringTools;
26  
27  import junit.framework.TestCase;
28  
29  
30  /**
31   * Test case for helper methods within SimpleAuthenticator.
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class SimpleAuthenticatorOneWayEncryptedTest extends TestCase
36  {
37      private SimpleAuthenticator auth = null;
38  
39  
40      protected void setUp() throws Exception
41      {
42          super.setUp();
43          this.auth = new SimpleAuthenticator();
44      }
45  
46  
47      public void testGetAlgorithmForHashedPassword()
48      {
49          String digestetValue = "{SHA}LhkDrSoM6qr0fW6hzlfOJQW61tc=";
50          assertEquals( "SHA", auth.getAlgorithmForHashedPassword( StringTools.getBytesUtf8( digestetValue ) ) );
51          assertEquals( "SHA", auth.getAlgorithmForHashedPassword( digestetValue.getBytes() ) );
52  
53          String noAlgorithm = "Secret1!";
54          assertEquals( null, auth.getAlgorithmForHashedPassword( StringTools.getBytesUtf8( noAlgorithm ) ) );
55          assertEquals( null, auth.getAlgorithmForHashedPassword( noAlgorithm.getBytes() ) );
56  
57          String unknownAlgorithm = "{XYZ}LhkDrSoM6qr0fW6hzlfOJQW61tc=";
58          assertEquals( null, auth.getAlgorithmForHashedPassword( StringTools.getBytesUtf8( unknownAlgorithm ) ) );
59          assertEquals( null, auth.getAlgorithmForHashedPassword( unknownAlgorithm.getBytes() ) );
60      }
61  
62  
63      public void testCreateDigestedPassword() throws IllegalArgumentException
64      {
65          String pwd = "Secret1!";
66          String expected = "{SHA}znbJr3+tymFoQD4+Njh4ITtI7Cc=";
67          String digested = auth.createDigestedPassword( "SHA", StringTools.getBytesUtf8( pwd ) );
68  
69          assertEquals( expected, digested );
70      }
71  }