1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.security.spi;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.apache.jetspeed.security.PasswordCredential;
25 import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
26 import org.apache.jetspeed.security.SecurityException;
27
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30
31 /***
32 * <p>
33 * Unit testing for {@link PasswordCredentialProvider}.
34 * </p>
35 *
36 * @author <a href="mailto:ate@apache.org">Ate Douma</a>
37 */
38 public class TestPasswordCredentialProvider extends AbstractSecurityTestcase
39 {
40 /***
41 * @see junit.framework.TestCase#setUp()
42 */
43 protected void setUp() throws Exception
44 {
45 super.setUp();
46
47 destroyUser();
48 }
49
50 /***
51 * @see junit.framework.TestCase#tearDown()
52 */
53 public void tearDown() throws Exception
54 {
55 super.tearDown();
56 }
57
58 /***
59 * <p>
60 * Constructs the suite.
61 * </p>
62 *
63 * @return The {@Test}.
64 */
65 public static Test suite()
66 {
67 return new TestSuite(TestPasswordCredentialProvider.class);
68 }
69
70 /***
71 * <p>
72 * Test <code>getPrivateCredentials</code>..
73 * </p>
74 */
75 public void testGetPrivateCredentials() throws Exception
76 {
77 initUser();
78 Set privateCredentials = ums.getUser("testcred").getSubject().getPrivateCredentials();
79 assertNotNull(privateCredentials);
80 assertEquals(1, privateCredentials.size());
81 PasswordCredential[] pwdCreds = (PasswordCredential[]) privateCredentials.toArray(new PasswordCredential[0]);
82 assertEquals("testcred", pwdCreds[0].getUserName());
83 assertNotSame("password01", new String(pwdCreds[0].getPassword()));
84 destroyUser();
85 }
86
87 /***
88 * <p>
89 * Test <code>setPassword</code>..
90 * </p>
91 */
92 public void testSetPassword() throws Exception
93 {
94 initUser();
95 Set privateCredentials = ums.getUser("testcred").getSubject().getPrivateCredentials();
96 assertNotNull(privateCredentials);
97 assertEquals(1, privateCredentials.size());
98 PasswordCredential[] pwdCreds = (PasswordCredential[]) privateCredentials.toArray(new PasswordCredential[0]);
99 assertEquals("testcred", pwdCreds[0].getUserName());
100 String encodedPassword = new String(pwdCreds[0].getPassword());
101 assertNotSame("password01", encodedPassword );
102
103
104 try
105 {
106 ums.setPassword("testcred","password01","1234567");
107 fail("Should not be able to set an invalid password");
108 }
109 catch (SecurityException e){}
110
111 try
112 {
113 ums.setPassword("testcred","password01","newpassword");
114 fail("Should not be able to set an invalid password");
115 }
116 catch (SecurityException e){}
117
118 ums.setPassword("testcred","password01","passwd01");
119
120
121 privateCredentials = ums.getUser("testcred").getSubject().getPrivateCredentials();
122 assertNotNull(privateCredentials);
123 assertEquals(1, privateCredentials.size());
124 pwdCreds = (PasswordCredential[]) privateCredentials.toArray(new PasswordCredential[0]);
125 assertEquals("testcred", pwdCreds[0].getUserName());
126 String newEncodedPassword = new String(pwdCreds[0].getPassword());
127 assertNotSame(encodedPassword, newEncodedPassword);
128 assertNotSame("passwd01", newEncodedPassword);
129
130
131 assertTrue(ums.authenticate("testcred","passwd01"));
132 destroyUser();
133 }
134
135 /***
136 * <p>
137 * Initialize user test object.
138 * </p>
139 */
140 protected void initUser() throws Exception
141 {
142 ums.addUser("testcred", "password01");
143 }
144
145 /***
146 * <p>
147 * Destroy user test object.
148 * </p>
149 */
150 protected void destroyUser() throws Exception
151 {
152 ums.removeUser("testcred");
153 }
154
155 protected String[] getConfigurations()
156 {
157 String[] confs = super.getConfigurations();
158 List confList = new ArrayList(Arrays.asList(confs));
159 confList.add("JETSPEED-INF/spring/TestPasswordCredentialProvider.xml");
160 return (String[]) confList.toArray(new String[1]);
161 }
162 }