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 PASSWORD = "ecoinformatics";
|
34
|
/**
|
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
|
suite.addTest(new AuthFileTest("testAddUser"));
|
65
|
suite.addTest(new AuthFileTest("testAuthenticate"));
|
66
|
suite.addTest(new AuthFileTest("testGetUsers"));
|
67
|
suite.addTest(new AuthFileTest("testGetGroups"));
|
68
|
suite.addTest(new AuthFileTest("testChangePassword"));
|
69
|
suite.addTest(new AuthFileTest("testAddRemoveUserToFromGroup"));
|
70
|
suite.addTest(new AuthFileTest("testGetPrincipals"));
|
71
|
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
|
authFile.addGroup(GROUPNAME, "Developers at NCEAS");
|
81
|
try {
|
82
|
authFile.addGroup(GROUPNAME, "Developers at NCEAS");
|
83
|
assertTrue("We can't reach here since we can't add the group twice", false);
|
84
|
} catch (AuthenticationException e) {
|
85
|
|
86
|
}
|
87
|
|
88
|
}
|
89
|
|
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
|
authFile.addUser(USERNAME, groups, PASSWORD);
|
98
|
authFile.addUser(USERNAME2, null, PASSWORD);
|
99
|
try {
|
100
|
authFile.addUser(USERNAME, groups, PASSWORD);
|
101
|
assertTrue("We can't reach here since we can't add the user twice", false);
|
102
|
} catch (AuthenticationException e) {
|
103
|
|
104
|
}
|
105
|
|
106
|
}
|
107
|
|
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
|
|
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
|
|
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
|
String password = authFile.resetPassword(USERNAME);
|
163
|
authFile.authenticate(USERNAME, password);
|
164
|
String newPassword = "hello";
|
165
|
authFile.modifyPassword(USERNAME, password, newPassword);
|
166
|
authFile.authenticate(USERNAME, newPassword);
|
167
|
try {
|
168
|
authFile.resetPassword("user1");
|
169
|
assertTrue("Can't reach here since we tried to reset the password for an unexisting user ", false);
|
170
|
} catch (AuthenticationException e) {
|
171
|
System.out.println("Failed to reset the password for a user: "+e.getMessage());
|
172
|
}
|
173
|
try {
|
174
|
authFile.modifyPassword("user1", "old", "new");
|
175
|
assertTrue("Can't reach here since we tried to change the password for an unexisting user ", false);
|
176
|
} catch (AuthenticationException e) {
|
177
|
System.out.println("Failed to change the password for a user: "+e.getMessage());
|
178
|
}
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* Test the addUserToGroup and removeUserFromGroup methods
|
183
|
* @throws Exception
|
184
|
*/
|
185
|
public void testAddRemoveUserToFromGroup() throws Exception{
|
186
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
187
|
try {
|
188
|
authFile.addUserToGroup("user1", GROUPNAME);
|
189
|
assertTrue("Can't reach here since we tried to add an unexisting user to a group", false);
|
190
|
} catch(AuthenticationException e) {
|
191
|
System.out.println("Failed to add a user to a group "+e.getMessage());
|
192
|
}
|
193
|
|
194
|
try {
|
195
|
authFile.addUserToGroup(USERNAME, "group2");
|
196
|
assertTrue("Can't reach here since we tried to add a user to an unexisting group", false);
|
197
|
} catch(AuthenticationException e) {
|
198
|
System.out.println("Failed to add a user to a group "+e.getMessage());
|
199
|
}
|
200
|
try {
|
201
|
authFile.addUserToGroup(USERNAME, GROUPNAME);
|
202
|
assertTrue("Can't reach here since the user is already in the group", false);
|
203
|
} catch(AuthenticationException e) {
|
204
|
System.out.println("Failed to add a user to a group "+e.getMessage());
|
205
|
}
|
206
|
authFile.addGroup(GROUPNAME2, null);
|
207
|
authFile.addUserToGroup(USERNAME, GROUPNAME2);
|
208
|
String[][]groups = authFile.getGroups(null, null, USERNAME);
|
209
|
assertTrue("The user "+USERNAME+" should be in the group "+GROUPNAME2, groups[0][0].equals(GROUPNAME2)||groups[1][0].equals(GROUPNAME2));
|
210
|
|
211
|
|
212
|
try {
|
213
|
authFile.removeUserFromGroup("user1", GROUPNAME);
|
214
|
assertTrue("Can't reach here since we tried to remove an unexisting user from a group", false);
|
215
|
} catch(AuthenticationException e) {
|
216
|
System.out.println("Failed to remove a user from a group "+e.getMessage());
|
217
|
}
|
218
|
|
219
|
try {
|
220
|
authFile.removeUserFromGroup(USERNAME, "group2");
|
221
|
assertTrue("Can't reach here since we tried to remove a user from an unexisting group", false);
|
222
|
} catch(AuthenticationException e) {
|
223
|
System.out.println("Failed to remove a user from a group "+e.getMessage());
|
224
|
}
|
225
|
authFile.addGroup(GROUPNAME3, "Developers");
|
226
|
try {
|
227
|
authFile.removeUserFromGroup(USERNAME, GROUPNAME3);
|
228
|
assertTrue("Can't reach here since the user is not in the group", false);
|
229
|
} catch(AuthenticationException e) {
|
230
|
System.out.println("Failed to remove a user from a group "+e.getMessage());
|
231
|
}
|
232
|
authFile.removeUserFromGroup(USERNAME, GROUPNAME2);
|
233
|
groups = authFile.getGroups(null, null, USERNAME);
|
234
|
assertTrue("The size of groups of the user "+USERNAME+" shouldn be one rather than "+groups.length, groups.length ==1);
|
235
|
assertTrue("The user "+USERNAME+" shouldn't be in the group "+GROUPNAME2, !groups[0][0].equals(GROUPNAME2));
|
236
|
assertTrue("The user "+USERNAME+" should still be in the group "+GROUPNAME, groups[0][0].equals(GROUPNAME));
|
237
|
}
|
238
|
|
239
|
/**
|
240
|
* Test the getPrincipal
|
241
|
* @throws Exception
|
242
|
*/
|
243
|
public void testGetPrincipals() throws Exception {
|
244
|
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
|
245
|
System.out.println(""+authFile.getPrincipals(null, null));
|
246
|
}
|
247
|
}
|