1 |
8420
|
tao
|
/** '$RCSfile$'
|
2 |
|
|
* Copyright: 2010 Regents of the University of California and the
|
3 |
|
|
* National Center for Ecological Analysis and Synthesis
|
4 |
|
|
*
|
5 |
|
|
* This program is free software; you can redistribute it and/or modify
|
6 |
|
|
* it under the terms of the GNU General Public License as published by
|
7 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
8 |
|
|
* (at your option) any later version.
|
9 |
|
|
*
|
10 |
|
|
* This program is distributed in the hope that it will be useful,
|
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
|
|
* GNU General Public License for more details.
|
14 |
|
|
*
|
15 |
|
|
* You should have received a copy of the GNU General Public License
|
16 |
|
|
* along with this program; if not, write to the Free Software
|
17 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18 |
|
|
*/
|
19 |
|
|
package edu.ucsb.nceas.metacat.authentication;
|
20 |
|
|
|
21 |
|
|
import junit.framework.Test;
|
22 |
|
|
import junit.framework.TestSuite;
|
23 |
|
|
import edu.ucsb.nceas.MCTestCase;
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
public class AuthFileTest extends MCTestCase {
|
27 |
|
|
private static final String PASSWORDFILEPATH = "build/password";
|
28 |
|
|
private static final String GROUPNAME = "nceas-dev";
|
29 |
8432
|
tao
|
private static final String GROUPNAME2 = "dataone-dev";
|
30 |
|
|
private static final String GROUPNAME3 = "dev";
|
31 |
|
|
private static final String USERNAME = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
|
32 |
8435
|
tao
|
private static final String USERNAME2="uid=smith,o=unaffiliated,dc=ecoinformatics,dc=org";
|
33 |
8422
|
tao
|
private static final String PASSWORD = "ecoinformatics";
|
34 |
8420
|
tao
|
/**
|
35 |
|
|
* consstructor for the test
|
36 |
|
|
*/
|
37 |
|
|
public AuthFileTest(String name)
|
38 |
|
|
{
|
39 |
|
|
super(name);
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
/**
|
43 |
|
|
* Establish a testing framework by initializing appropriate objects
|
44 |
|
|
*/
|
45 |
|
|
public void setUp() throws Exception
|
46 |
|
|
{
|
47 |
|
|
super.setUp();
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
/**
|
51 |
|
|
* Release any objects after tests are complete
|
52 |
|
|
*/
|
53 |
|
|
public void tearDown()
|
54 |
|
|
{
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
/**
|
58 |
|
|
* Create a suite of tests to be run together
|
59 |
|
|
*/
|
60 |
|
|
public static Test suite()
|
61 |
|
|
{
|
62 |
|
|
TestSuite suite = new TestSuite();
|
63 |
|
|
suite.addTest(new AuthFileTest("testAddGroup"));
|
64 |
8422
|
tao
|
suite.addTest(new AuthFileTest("testAddUser"));
|
65 |
8423
|
tao
|
suite.addTest(new AuthFileTest("testAuthenticate"));
|
66 |
8428
|
tao
|
suite.addTest(new AuthFileTest("testGetUsers"));
|
67 |
8430
|
tao
|
suite.addTest(new AuthFileTest("testGetGroups"));
|
68 |
|
|
suite.addTest(new AuthFileTest("testChangePassword"));
|
69 |
8432
|
tao
|
suite.addTest(new AuthFileTest("testAddRemoveUserToFromGroup"));
|
70 |
|
|
suite.addTest(new AuthFileTest("testGetPrincipals"));
|
71 |
8420
|
tao
|
return suite;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
/**
|
75 |
|
|
* Test the addGroup method
|
76 |
|
|
* @throws Exception
|
77 |
|
|
*/
|
78 |
|
|
public void testAddGroup() throws Exception{
|
79 |
|
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
80 |
8435
|
tao
|
authFile.addGroup(GROUPNAME, "Developers at NCEAS");
|
81 |
8420
|
tao
|
try {
|
82 |
8435
|
tao
|
authFile.addGroup(GROUPNAME, "Developers at NCEAS");
|
83 |
8420
|
tao
|
assertTrue("We can't reach here since we can't add the group twice", false);
|
84 |
|
|
} catch (AuthenticationException e) {
|
85 |
|
|
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
}
|
89 |
8422
|
tao
|
|
90 |
|
|
/**
|
91 |
|
|
* Test the addGroup method
|
92 |
|
|
* @throws Exception
|
93 |
|
|
*/
|
94 |
|
|
public void testAddUser() throws Exception{
|
95 |
|
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
96 |
|
|
String[]groups = {GROUPNAME};
|
97 |
8466
|
tao
|
authFile.addUser(USERNAME, groups, PASSWORD, null, null, null, null);
|
98 |
|
|
authFile.addUser(USERNAME2, null, PASSWORD, null, null, null,null);
|
99 |
8422
|
tao
|
try {
|
100 |
8466
|
tao
|
authFile.addUser(USERNAME, groups, PASSWORD, null, null, null, null);
|
101 |
8422
|
tao
|
assertTrue("We can't reach here since we can't add the user twice", false);
|
102 |
|
|
} catch (AuthenticationException e) {
|
103 |
|
|
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
}
|
107 |
8423
|
tao
|
|
108 |
|
|
/**
|
109 |
|
|
* Test the authentication method
|
110 |
|
|
* @throws Exception
|
111 |
|
|
*/
|
112 |
|
|
public void testAuthenticate() throws Exception {
|
113 |
|
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
114 |
|
|
boolean success = authFile.authenticate(USERNAME, PASSWORD);
|
115 |
|
|
if(!success) {
|
116 |
|
|
assertTrue("The authentication should succeed.", false);
|
117 |
|
|
}
|
118 |
|
|
success = authFile.authenticate(USERNAME, "hello");
|
119 |
|
|
if(success) {
|
120 |
|
|
assertTrue("The authentication should NOT succeed.", false);
|
121 |
|
|
}
|
122 |
|
|
success = authFile.authenticate("hello", PASSWORD);
|
123 |
|
|
if(success) {
|
124 |
|
|
assertTrue("The authentication should NOT succeed.", false);
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
8428
|
tao
|
|
128 |
|
|
/**
|
129 |
|
|
* Test the getUsers method
|
130 |
|
|
* @throws Exception
|
131 |
|
|
*/
|
132 |
|
|
public void testGetUsers() throws Exception {
|
133 |
|
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
134 |
|
|
String[][] users = authFile.getUsers(null, null);
|
135 |
|
|
assertTrue("The file should have one user "+USERNAME, users[0][0].equals(USERNAME));
|
136 |
|
|
String[]userInGroup = authFile.getUsers(null, null, GROUPNAME);
|
137 |
|
|
assertTrue("There should be at least one user in the group "+GROUPNAME, userInGroup[0].equals(USERNAME));
|
138 |
|
|
userInGroup = authFile.getUsers(null, null, "group1");
|
139 |
|
|
assertTrue("There shouldn't have any users in the group1 ", userInGroup==null);
|
140 |
|
|
}
|
141 |
8430
|
tao
|
|
142 |
|
|
/**
|
143 |
|
|
* Test the getGroups method
|
144 |
|
|
* @throws Exception
|
145 |
|
|
*/
|
146 |
|
|
public void testGetGroups() throws Exception {
|
147 |
|
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
148 |
|
|
String[][] groups = authFile.getGroups(null, null);
|
149 |
|
|
assertTrue("The file should have one group associated with "+USERNAME, groups[0][0].equals(GROUPNAME));
|
150 |
|
|
String[][]groupForUser = authFile.getGroups(null, null, USERNAME);
|
151 |
|
|
assertTrue("There should be at least one group for user "+USERNAME, groupForUser[0][0].equals(GROUPNAME));
|
152 |
|
|
groupForUser = authFile.getGroups(null, null, "user1");
|
153 |
|
|
assertTrue("There shouldn't have any groups assoicated with user1 ", groupForUser==null);
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
/**
|
157 |
|
|
* Test the change password methods
|
158 |
|
|
* @throws Exception
|
159 |
|
|
*/
|
160 |
|
|
public void testChangePassword() throws Exception {
|
161 |
|
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
162 |
8432
|
tao
|
authFile.authenticate(USERNAME, password);
|
163 |
8430
|
tao
|
String newPassword = "hello";
|
164 |
8466
|
tao
|
authFile.modifyPassWithHash(USERNAME,newPassword);
|
165 |
8432
|
tao
|
authFile.authenticate(USERNAME, newPassword);
|
166 |
8430
|
tao
|
try {
|
167 |
8466
|
tao
|
authFile.modifyPassWithPlain("user1", "new");
|
168 |
8430
|
tao
|
assertTrue("Can't reach here since we tried to change the password for an unexisting user ", false);
|
169 |
|
|
} catch (AuthenticationException e) {
|
170 |
8432
|
tao
|
System.out.println("Failed to change the password for a user: "+e.getMessage());
|
171 |
8430
|
tao
|
}
|
172 |
|
|
}
|
173 |
8432
|
tao
|
|
174 |
|
|
/**
|
175 |
|
|
* Test the addUserToGroup and removeUserFromGroup methods
|
176 |
|
|
* @throws Exception
|
177 |
|
|
*/
|
178 |
|
|
public void testAddRemoveUserToFromGroup() throws Exception{
|
179 |
|
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
180 |
|
|
try {
|
181 |
|
|
authFile.addUserToGroup("user1", GROUPNAME);
|
182 |
|
|
assertTrue("Can't reach here since we tried to add an unexisting user to a group", false);
|
183 |
|
|
} catch(AuthenticationException e) {
|
184 |
|
|
System.out.println("Failed to add a user to a group "+e.getMessage());
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
try {
|
188 |
|
|
authFile.addUserToGroup(USERNAME, "group2");
|
189 |
|
|
assertTrue("Can't reach here since we tried to add a user to an unexisting group", false);
|
190 |
|
|
} catch(AuthenticationException e) {
|
191 |
|
|
System.out.println("Failed to add a user to a group "+e.getMessage());
|
192 |
|
|
}
|
193 |
|
|
try {
|
194 |
|
|
authFile.addUserToGroup(USERNAME, GROUPNAME);
|
195 |
|
|
assertTrue("Can't reach here since the user is already in the group", false);
|
196 |
|
|
} catch(AuthenticationException e) {
|
197 |
|
|
System.out.println("Failed to add a user to a group "+e.getMessage());
|
198 |
|
|
}
|
199 |
8435
|
tao
|
authFile.addGroup(GROUPNAME2, null);
|
200 |
8432
|
tao
|
authFile.addUserToGroup(USERNAME, GROUPNAME2);
|
201 |
|
|
String[][]groups = authFile.getGroups(null, null, USERNAME);
|
202 |
|
|
assertTrue("The user "+USERNAME+" should be in the group "+GROUPNAME2, groups[0][0].equals(GROUPNAME2)||groups[1][0].equals(GROUPNAME2));
|
203 |
|
|
|
204 |
|
|
|
205 |
|
|
try {
|
206 |
|
|
authFile.removeUserFromGroup("user1", GROUPNAME);
|
207 |
|
|
assertTrue("Can't reach here since we tried to remove an unexisting user from a group", false);
|
208 |
|
|
} catch(AuthenticationException e) {
|
209 |
|
|
System.out.println("Failed to remove a user from a group "+e.getMessage());
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
try {
|
213 |
|
|
authFile.removeUserFromGroup(USERNAME, "group2");
|
214 |
|
|
assertTrue("Can't reach here since we tried to remove a user from an unexisting group", false);
|
215 |
|
|
} catch(AuthenticationException e) {
|
216 |
|
|
System.out.println("Failed to remove a user from a group "+e.getMessage());
|
217 |
|
|
}
|
218 |
8435
|
tao
|
authFile.addGroup(GROUPNAME3, "Developers");
|
219 |
8432
|
tao
|
try {
|
220 |
|
|
authFile.removeUserFromGroup(USERNAME, GROUPNAME3);
|
221 |
|
|
assertTrue("Can't reach here since the user is not in the group", false);
|
222 |
|
|
} catch(AuthenticationException e) {
|
223 |
|
|
System.out.println("Failed to remove a user from a group "+e.getMessage());
|
224 |
|
|
}
|
225 |
|
|
authFile.removeUserFromGroup(USERNAME, GROUPNAME2);
|
226 |
|
|
groups = authFile.getGroups(null, null, USERNAME);
|
227 |
|
|
assertTrue("The size of groups of the user "+USERNAME+" shouldn be one rather than "+groups.length, groups.length ==1);
|
228 |
|
|
assertTrue("The user "+USERNAME+" shouldn't be in the group "+GROUPNAME2, !groups[0][0].equals(GROUPNAME2));
|
229 |
|
|
assertTrue("The user "+USERNAME+" should still be in the group "+GROUPNAME, groups[0][0].equals(GROUPNAME));
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
/**
|
233 |
|
|
* Test the getPrincipal
|
234 |
|
|
* @throws Exception
|
235 |
|
|
*/
|
236 |
|
|
public void testGetPrincipals() throws Exception {
|
237 |
|
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
238 |
|
|
System.out.println(""+authFile.getPrincipals(null, null));
|
239 |
|
|
}
|
240 |
8420
|
tao
|
}
|