1   package com.atlassian.user.impl.osuser.security.password;
2   
3   import junit.framework.TestCase;
4   import com.opensymphony.user.ImmutableException;
5   import com.opensymphony.user.DuplicateEntityException;
6   import com.opensymphony.user.provider.ejb.util.Base64;
7   import com.opensymphony.user.provider.ejb.util.PasswordDigester;
8   
9   import java.util.Map;
10  
11  public class TestOSUPasswordEncryptor extends TestCase
12  {
13      public static final String ADMIN = "x61Ey612Kl2gpFL56FT9weDnpSo4AV8j8+qx2AuTHdRyY036xxzTTrw10Wq3+4qQyB+XURPWx1ONxp3Y3pB37A==";
14      public static final String SPHERE = "uQieO/1CGMUIXXftw3ynrsaYLShI+GTcPS4LdUGWbIusFvHPfUzD7CZvms6yMMvA8I7FViHVEqr6Mj4pCLKAFQ==";
15  
16      OSUPasswordEncryptor encryptor;
17  
18      public void setUp() throws Exception
19      {
20          encryptor = new OSUPasswordEncryptor();
21          super.setUp();
22      }
23  
24      public void tearDown() throws Exception
25      {
26          super.tearDown();
27      }
28  
29      public void testAdminPasswordEquality()
30      {
31          assertEquals("OSUPasswordEncryptor is not matching typical osuser SHA1 algorithm", ADMIN, encryptor.encrypt("admin"));
32      }
33  
34      public void testSpherePasswordEquality()
35      {
36          assertEquals("OSUPasswordEncryptor is not matching typical osuser SHA1 algorithm", SPHERE, encryptor.encrypt("sphere"));
37      }
38  
39      public void testRandomPasswordEquality() throws ImmutableException, DuplicateEntityException
40      {
41          //generate 10 letter password
42          StringBuffer unencryptedPassword = new StringBuffer();
43  
44          for (int i = 0; i < 10; i++)
45          {
46              int ch = (int) (Math.random() * 127);
47              int x = (int) (Math.random() * 1);
48  
49              if (x == 1)
50                  ch = ch * -1;
51  
52              unencryptedPassword.append((char)ch);
53          }
54  
55          byte[] encrypted = PasswordDigester.digest(unencryptedPassword.toString().getBytes());
56          byte[] encoded = Base64.encode(encrypted);
57  
58          assertEquals(new String(encoded), encryptor.encrypt(unencryptedPassword.toString()));
59      }
60  }