1   /* Copyright 2004 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.providers.encoding;
17  
18  import junit.framework.TestCase;
19  
20  
21  /***
22   * <p>
23   * TestCase for ShaPasswordEncoder.
24   * </p>
25   *
26   * @author colin sampaleanu
27   * @author Ben Alex
28   * @version $Id: ShaPasswordEncoderTests.java,v 1.2 2005/11/17 00:56:08 benalex Exp $
29   */
30  public class ShaPasswordEncoderTests extends TestCase {
31      //~ Methods ================================================================
32  
33      public void testBasicFunctionality() {
34          ShaPasswordEncoder pe = new ShaPasswordEncoder();
35          String raw = "abc123";
36          String badRaw = "abc321";
37          String salt = "THIS_IS_A_SALT";
38          String encoded = pe.encodePassword(raw, salt);
39          assertTrue(pe.isPasswordValid(encoded, raw, salt));
40          assertFalse(pe.isPasswordValid(encoded, badRaw, salt));
41          assertTrue(encoded.length() == 40);
42  
43          // now try Base64
44          pe.setEncodeHashAsBase64(true);
45          encoded = pe.encodePassword(raw, salt);
46          assertTrue(pe.isPasswordValid(encoded, raw, salt));
47          assertFalse(pe.isPasswordValid(encoded, badRaw, salt));
48          assertTrue(encoded.length() != 40);
49      }
50  }