Project

General

Profile

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