1
|
/** '$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
|
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
|
private static final String USERNAME2="uid=smith,o=unaffiliated,dc=ecoinformatics,dc=org";
|
33
|
private static final String PLAINPASSWORD = "ecoinformatics";
|
34
|
private static final String PLAINPASSWORD2 = "n%cea4s";
|
35
|
private static final String HASHEDPASSWORD2 = "$2a$10$iMZXvVYs8nEUAWDFfcCF8ePEvzcnak32tx7TQAecsZcPGRouqSdse";
|
36
|
private static final String PLAINPASSWORD3 = "q8w*er";
|
37
|
private static final String HASHEDPASSWORD3 = "$2a$10$zO4Cw1p38xWeUh4DneMGCecg67yo2SN25m0wzWCJ9zu7FfRwLTvue";
|
38
|
private static final String EMAILADDRESS = "john@nceas.ucsb.edu";
|
39
|
private static final String SURNAME = "John";
|
40
|
private static final String GIVENNAME = "Joe";
|
41
|
/**
|
42
|
* consstructor for the test
|
43
|
*/
|
44
|
public AuthFileTest(String name)
|
45
|
{
|
46
|
super(name);
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Establish a testing framework by initializing appropriate objects
|
51
|
*/
|
52
|
public void setUp() throws Exception
|
53
|
{
|
54
|
super.setUp();
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Release any objects after tests are complete
|
59
|
*/
|
60
|
public void tearDown()
|
61
|
{
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* Create a suite of tests to be run together
|
66
|
*/
|
67
|
public static Test suite()
|
68
|
{
|
69
|
TestSuite suite = new TestSuite();
|
70
|
suite.addTest(new AuthFileTest("testAddGroup"));
|
71
|
suite.addTest(new AuthFileTest("testAddUser"));
|
72
|
suite.addTest(new AuthFileTest("testAuthenticate"));
|
73
|
suite.addTest(new AuthFileTest("testChangePassword"));
|
74
|
suite.addTest(new AuthFileTest("testGetUsers"));
|
75
|
suite.addTest(new AuthFileTest("testGetGroups"));
|
76
|
suite.addTest(new AuthFileTest("testAddRemoveUserToFromGroup"));
|
77
|
suite.addTest(new AuthFileTest("testGetPrincipals"));
|
78
|
return suite;
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* Test the addGroup method
|
83
|
* @throws Exception
|
84
|
*/
|
85
|
public void testAddGroup() throws Exception{
|
86
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
87
|
authFile.addGroup(GROUPNAME, "Developers at NCEAS");
|
88
|
try {
|
89
|
authFile.addGroup(GROUPNAME, "Developers at NCEAS");
|
90
|
assertTrue("We can't reach here since we can't add the group twice", false);
|
91
|
} catch (AuthenticationException e) {
|
92
|
|
93
|
}
|
94
|
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Test the addGroup method
|
99
|
* @throws Exception
|
100
|
*/
|
101
|
public void testAddUser() throws Exception{
|
102
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
103
|
String[]groups = {GROUPNAME};
|
104
|
authFile.addUser(USERNAME, groups, PLAINPASSWORD, null, EMAILADDRESS, SURNAME, GIVENNAME);
|
105
|
//user a hash value of the PASSWORD
|
106
|
authFile.addUser(USERNAME2, null, null, HASHEDPASSWORD2, null, null,null);
|
107
|
try {
|
108
|
authFile.addUser(USERNAME, groups, PLAINPASSWORD, null, null, null, null);
|
109
|
assertTrue("We can't reach here since we can't add the user twice", false);
|
110
|
} catch (AuthenticationException e) {
|
111
|
|
112
|
}
|
113
|
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* Test the authentication method
|
118
|
* @throws Exception
|
119
|
*/
|
120
|
public void testAuthenticate() throws Exception {
|
121
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
122
|
boolean success = authFile.authenticate(USERNAME, PLAINPASSWORD);
|
123
|
if(!success) {
|
124
|
assertTrue("The authentication should succeed.", false);
|
125
|
}
|
126
|
success = authFile.authenticate(USERNAME, "hello");
|
127
|
if(success) {
|
128
|
assertTrue("The authentication should NOT succeed.", false);
|
129
|
}
|
130
|
success = authFile.authenticate("hello", PLAINPASSWORD);
|
131
|
if(success) {
|
132
|
assertTrue("The authentication should NOT succeed.", false);
|
133
|
}
|
134
|
success = authFile.authenticate(USERNAME2, PLAINPASSWORD2);
|
135
|
if(!success) {
|
136
|
assertTrue("The authentication for "+USERNAME2 +" should succeed.", false);
|
137
|
}
|
138
|
success = authFile.authenticate(USERNAME2, HASHEDPASSWORD2);
|
139
|
if(success) {
|
140
|
assertTrue("The authentication should NOT succeed.", false);
|
141
|
}
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* Test the getUsers method
|
146
|
* @throws Exception
|
147
|
*/
|
148
|
public void testGetUsers() throws Exception {
|
149
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
150
|
String[][] users = authFile.getUsers(null, null);
|
151
|
assertTrue("The file should have one user "+USERNAME, users[0][0].equals(USERNAME));
|
152
|
assertTrue("The common name for the user "+USERNAME+" should be "+GIVENNAME+" "+SURNAME, users[0][1].equals(GIVENNAME+" "+SURNAME));
|
153
|
assertTrue("The org name for the user "+USERNAME+" should be null ", users[0][2]== null);
|
154
|
assertTrue("The org unit name for the user "+USERNAME+" should be null ", users[0][3]== null);
|
155
|
assertTrue("The email address for the user "+USERNAME+" should be "+EMAILADDRESS, users[0][4].equals(EMAILADDRESS));
|
156
|
assertTrue("The file should have one user "+USERNAME2, users[1][0].equals(USERNAME2));
|
157
|
assertTrue("The common name for the user "+USERNAME2+" should be null", users[1][1]==null);
|
158
|
assertTrue("The org name for the user "+USERNAME2+" should be null ", users[1][2]== null);
|
159
|
assertTrue("The org unit name for the user "+USERNAME2+" should be null ", users[1][3]== null);
|
160
|
assertTrue("The email address for the user "+USERNAME2+" should be null.", users[1][4]==null);
|
161
|
String[]userInGroup = authFile.getUsers(null, null, GROUPNAME);
|
162
|
assertTrue("There should be at least one user in the group "+GROUPNAME, userInGroup[0].equals(USERNAME));
|
163
|
userInGroup = authFile.getUsers(null, null, "group1");
|
164
|
assertTrue("There shouldn't have any users in the group1 ", userInGroup==null);
|
165
|
}
|
166
|
|
167
|
/**
|
168
|
* Test the getGroups method
|
169
|
* @throws Exception
|
170
|
*/
|
171
|
public void testGetGroups() throws Exception {
|
172
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
173
|
String[][] groups = authFile.getGroups(null, null);
|
174
|
assertTrue("The file should have one group associated with "+USERNAME, groups[0][0].equals(GROUPNAME));
|
175
|
String[][]groupForUser = authFile.getGroups(null, null, USERNAME);
|
176
|
assertTrue("There should be at least one group for user "+USERNAME, groupForUser[0][0].equals(GROUPNAME));
|
177
|
groupForUser = authFile.getGroups(null, null, "user1");
|
178
|
assertTrue("There shouldn't have any groups assoicated with user1 ", groupForUser==null);
|
179
|
groupForUser = authFile.getGroups(null, null, USERNAME2);
|
180
|
assertTrue("There shouldn't have any groups assoicated with user "+USERNAME2, groupForUser==null);
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* Test the change password methods
|
185
|
* @throws Exception
|
186
|
*/
|
187
|
public void testChangePassword() throws Exception {
|
188
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
189
|
authFile.authenticate(USERNAME, PLAINPASSWORD);
|
190
|
String newPassword = "hello";
|
191
|
authFile.modifyPassWithPlain(USERNAME,newPassword);
|
192
|
boolean success = authFile.authenticate(USERNAME, newPassword);
|
193
|
assertTrue("The authentication should be successful with the new password", success);
|
194
|
try {
|
195
|
authFile.modifyPassWithPlain("user1", "new");
|
196
|
assertTrue("Can't reach here since we tried to change the password for an unexisting user ", false);
|
197
|
} catch (AuthenticationException e) {
|
198
|
//System.out.println("Failed to change the password for a user: "+e.getMessage());
|
199
|
}
|
200
|
|
201
|
success = authFile.authenticate(USERNAME, "qws");
|
202
|
assertTrue("The authentication should fail with a wrong password", !success);
|
203
|
|
204
|
//test change the password with hashed version
|
205
|
authFile.modifyPassWithHash(USERNAME, HASHEDPASSWORD3);
|
206
|
success = authFile.authenticate(USERNAME, PLAINPASSWORD3);
|
207
|
assertTrue("The authentication should be successful with the new password (after modifying the password with a hashed value", success);
|
208
|
success = authFile.authenticate(USERNAME, HASHEDPASSWORD3);
|
209
|
assertTrue("The authentication should faile when the user directly use the hash password.", !success);
|
210
|
success = authFile.authenticate(USERNAME, newPassword);
|
211
|
assertTrue("The authentication should be successful with a wrong password", !success);
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* Test the addUserToGroup and removeUserFromGroup methods
|
216
|
* @throws Exception
|
217
|
*/
|
218
|
public void testAddRemoveUserToFromGroup() throws Exception{
|
219
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
220
|
try {
|
221
|
authFile.addUserToGroup("user1", GROUPNAME);
|
222
|
assertTrue("Can't reach here since we tried to add an unexisting user to a group", false);
|
223
|
} catch(AuthenticationException e) {
|
224
|
System.out.println("Failed to add a user to a group "+e.getMessage());
|
225
|
}
|
226
|
|
227
|
try {
|
228
|
authFile.addUserToGroup(USERNAME, "group2");
|
229
|
assertTrue("Can't reach here since we tried to add a user to an unexisting group", false);
|
230
|
} catch(AuthenticationException e) {
|
231
|
System.out.println("Failed to add a user to a group "+e.getMessage());
|
232
|
}
|
233
|
try {
|
234
|
authFile.addUserToGroup(USERNAME, GROUPNAME);
|
235
|
assertTrue("Can't reach here since the user is already in the group", false);
|
236
|
} catch(AuthenticationException e) {
|
237
|
System.out.println("Failed to add a user to a group "+e.getMessage());
|
238
|
}
|
239
|
authFile.addGroup(GROUPNAME2, null);
|
240
|
authFile.addUserToGroup(USERNAME, GROUPNAME2);
|
241
|
String[][]groups = authFile.getGroups(null, null, USERNAME);
|
242
|
assertTrue("The user "+USERNAME+" should be in the group "+GROUPNAME2, groups[0][0].equals(GROUPNAME2)||groups[1][0].equals(GROUPNAME2));
|
243
|
|
244
|
|
245
|
try {
|
246
|
authFile.removeUserFromGroup("user1", GROUPNAME);
|
247
|
assertTrue("Can't reach here since we tried to remove an unexisting user from a group", false);
|
248
|
} catch(AuthenticationException e) {
|
249
|
System.out.println("Failed to remove a user from a group "+e.getMessage());
|
250
|
}
|
251
|
|
252
|
try {
|
253
|
authFile.removeUserFromGroup(USERNAME, "group2");
|
254
|
assertTrue("Can't reach here since we tried to remove a user from an unexisting group", false);
|
255
|
} catch(AuthenticationException e) {
|
256
|
System.out.println("Failed to remove a user from a group "+e.getMessage());
|
257
|
}
|
258
|
authFile.addGroup(GROUPNAME3, "Developers");
|
259
|
try {
|
260
|
authFile.removeUserFromGroup(USERNAME, GROUPNAME3);
|
261
|
assertTrue("Can't reach here since the user is not in the group", false);
|
262
|
} catch(AuthenticationException e) {
|
263
|
System.out.println("Failed to remove a user from a group "+e.getMessage());
|
264
|
}
|
265
|
authFile.removeUserFromGroup(USERNAME, GROUPNAME2);
|
266
|
groups = authFile.getGroups(null, null, USERNAME);
|
267
|
assertTrue("The size of groups of the user "+USERNAME+" shouldn be one rather than "+groups.length, groups.length ==1);
|
268
|
assertTrue("The user "+USERNAME+" shouldn't be in the group "+GROUPNAME2, !groups[0][0].equals(GROUPNAME2));
|
269
|
assertTrue("The user "+USERNAME+" should still be in the group "+GROUPNAME, groups[0][0].equals(GROUPNAME));
|
270
|
}
|
271
|
|
272
|
/**
|
273
|
* Test the getPrincipal
|
274
|
* @throws Exception
|
275
|
*/
|
276
|
public void testGetPrincipals() throws Exception {
|
277
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
278
|
System.out.println(""+authFile.getPrincipals(null, null));
|
279
|
}
|
280
|
}
|