KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > ra > TestUserData


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package se.anatom.ejbca.ra;
15
16 import java.util.Collection JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Random JavaDoc;
20
21 import javax.naming.Context JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.apache.log4j.Logger;
27 import org.ejbca.core.ejb.ra.IUserAdminSessionHome;
28 import org.ejbca.core.ejb.ra.IUserAdminSessionRemote;
29 import org.ejbca.core.model.SecConst;
30 import org.ejbca.core.model.log.Admin;
31 import org.ejbca.core.model.ra.UserDataConstants;
32 import org.ejbca.core.model.ra.UserDataVO;
33
34
35
36
37 /** Tests the UserData entity bean and some parts of UserAdminSession.
38  *
39  * @version $Id: TestUserData.java,v 1.8 2006/08/12 09:49:53 herrvendil Exp $
40  */

41 public class TestUserData extends TestCase {
42
43     private static Logger log = Logger.getLogger(TestUserData.class);
44     private static Context JavaDoc ctx;
45     private static IUserAdminSessionRemote usersession;
46     private static String JavaDoc username;
47     private static String JavaDoc username1;
48     private static String JavaDoc pwd;
49     private static String JavaDoc pwd1;
50     private static int caid;
51     private static Admin admin = null;
52
53     /**
54      * Creates a new TestUserData object.
55      *
56      * @param name DOCUMENT ME!
57      */

58     public TestUserData(String JavaDoc name) {
59         super(name);
60     }
61
62     protected void setUp() throws Exception JavaDoc {
63         log.debug(">setUp()");
64         ctx = getInitialContext();
65
66         caid = "CN=TEST".hashCode();
67
68         Object JavaDoc obj = ctx.lookup("UserAdminSession");
69         IUserAdminSessionHome userhome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, IUserAdminSessionHome.class);
70         usersession = userhome.create();
71         admin = new Admin(Admin.TYPE_INTERNALUSER);
72
73         log.debug("<setUp()");
74     }
75
76     protected void tearDown() throws Exception JavaDoc {
77     }
78
79     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
80         log.debug(">getInitialContext");
81
82         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
83         log.debug("<getInitialContext");
84
85         return ctx;
86     }
87
88     private String JavaDoc genRandomUserName() throws Exception JavaDoc {
89         // Gen random user
90
Random JavaDoc rand = new Random JavaDoc(new Date JavaDoc().getTime() + 4711);
91         String JavaDoc username = "";
92         for (int i = 0; i < 6; i++) {
93             int randint = rand.nextInt(9);
94             username += (new Integer JavaDoc(randint)).toString();
95         }
96         log.debug("Generated random username: username =" + username);
97
98         return username;
99     } // genRandomUserName
100

101     private String JavaDoc genRandomPwd() throws Exception JavaDoc {
102         // Gen random pwd
103
Random JavaDoc rand = new Random JavaDoc(new Date JavaDoc().getTime() + 4812);
104         String JavaDoc password = "";
105
106         for (int i = 0; i < 8; i++) {
107             int randint = rand.nextInt(9);
108             password += (new Integer JavaDoc(randint)).toString();
109         }
110
111         log.debug("Generated random pwd: password=" + password);
112
113         return password;
114     } // genRandomPwd
115

116
117     /**
118      * DOCUMENT ME!
119      *
120      * @throws Exception DOCUMENT ME!
121      */

122     public void test01CreateNewUser() throws Exception JavaDoc {
123         log.debug(">test01CreateNewUser()");
124         username = genRandomUserName();
125         pwd = genRandomPwd();
126         usersession.addUser(admin,username,pwd,"C=SE,O=AnaTom,CN="+username,null,null,false,SecConst.EMPTY_ENDENTITYPROFILE,SecConst.CERTPROFILE_FIXED_ENDUSER,SecConst.USER_INVALID,SecConst.TOKEN_SOFT_PEM,0,caid);
127         log.debug("created it!");
128         log.debug("<test01CreateNewUser()");
129     }
130
131     /**
132      * DOCUMENT ME!
133      *
134      * @throws Exception DOCUMENT ME!
135      */

136     public void test02LookupAndChangeUser() throws Exception JavaDoc {
137         log.debug(">test02LookupAndChangeUser()");
138
139         log.debug("username=" + username);
140         UserDataVO data2 = usersession.findUser(admin,username);
141         log.debug("found by key! =" + data2);
142         log.debug("username=" + data2.getUsername());
143         assertTrue("wrong username", data2.getUsername().equals(username));
144         log.debug("subject=" + data2.getDN());
145         assertTrue("wrong DN", data2.getDN().indexOf(username) != -1);
146         log.debug("email=" + data2.getEmail());
147         assertNull("wrong email", data2.getEmail());
148         log.debug("status=" + data2.getStatus());
149         assertTrue("wrong status", data2.getStatus() == UserDataConstants.STATUS_NEW);
150         log.debug("type=" + data2.getType());
151         assertTrue("wrong type", data2.getType() == SecConst.USER_INVALID);
152         assertTrue("wrong pwd (foo123 works)", usersession.verifyPassword(admin,username,"foo123") == false);
153         assertTrue("wrong pwd " + pwd, usersession.verifyPassword(admin,username,pwd));
154
155         // Change DN
156
usersession.changeUser(admin,username,"foo123","C=SE,O=AnaTom,OU=Engineering, CN="+username,null,username+"@anatom.se",false,SecConst.EMPTY_ENDENTITYPROFILE,SecConst.CERTPROFILE_FIXED_ENDUSER,SecConst.USER_ENDUSER,SecConst.TOKEN_SOFT_PEM,0,UserDataConstants.STATUS_GENERATED,caid);
157         log.debug("Changed it");
158         log.debug("<test02LookupAndChangeUser()");
159     }
160
161     /**
162      * DOCUMENT ME!
163      *
164      * @throws Exception DOCUMENT ME!
165      */

166     public void test03LookupChangedUser() throws Exception JavaDoc {
167         log.debug(">test03LookupChangedUser()");
168
169         UserDataVO data = usersession.findUser(admin,username);
170         log.debug("found by key! =" + data);
171         log.debug("username=" + data.getUsername());
172         assertTrue("wrong username", data.getUsername().equals(username));
173         log.debug("subject=" + data.getDN());
174         assertTrue("wrong DN (cn)", data.getDN().indexOf(username) != -1);
175         assertTrue("wrong DN (ou)", data.getDN().indexOf("Engineering") != -1);
176         log.debug("email=" + data.getEmail());
177         assertNotNull("Email should not be null now.", data.getEmail());
178         assertTrue("wrong email", data.getEmail().equals(username + "@anatom.se"));
179         log.debug("status=" + data.getStatus());
180         assertTrue("wrong status", data.getStatus() == UserDataConstants.STATUS_GENERATED);
181         log.debug("type=" + data.getType());
182         assertTrue("wrong type", data.getType() == SecConst.USER_ENDUSER);
183         assertTrue("wrong pwd foo123", usersession.verifyPassword(admin,username,"foo123"));
184         assertTrue("wrong pwd (" + pwd + " works)" + pwd, usersession.verifyPassword(admin,username,pwd) == false);
185
186         // Use clear text pwd instead, new email, reverse DN again
187
usersession.changeUser(admin,username,"foo234","C=SE,O=AnaTom,CN="+username,null,username+"@anatom.nu",true,SecConst.EMPTY_ENDENTITYPROFILE,SecConst.CERTPROFILE_FIXED_ENDUSER,SecConst.USER_ENDUSER,SecConst.TOKEN_SOFT_PEM,0,UserDataConstants.STATUS_GENERATED,caid);
188         log.debug("<test03LookupChangedUser()");
189     }
190
191     /**
192      * DOCUMENT ME!
193      *
194      * @throws Exception DOCUMENT ME!
195      */

196     public void test03LookupChangedUser2() throws Exception JavaDoc {
197         log.debug(">test03LookupChangedUser2()");
198
199         UserDataVO data = usersession.findUser(admin,username);
200         log.debug("found by key! =" + data);
201         log.debug("username=" + data.getUsername());
202         assertTrue("wrong username", data.getUsername().equals(username));
203         log.debug("subject=" + data.getDN());
204         assertTrue("wrong DN", data.getDN().indexOf(username) != -1);
205         assertTrue("wrong DN", data.getDN().indexOf("Engineering") == -1);
206         log.debug("email=" + data.getEmail());
207         assertNotNull("Email should not be null now.", data.getEmail());
208         assertTrue("wrong email", data.getEmail().equals(username + "@anatom.nu"));
209         log.debug("status=" + data.getStatus());
210         assertTrue("wrong status", data.getStatus() == UserDataConstants.STATUS_GENERATED);
211         log.debug("type=" + data.getType());
212         assertTrue("wrong type", data.getType() == SecConst.USER_ENDUSER);
213         assertTrue("wrong pwd foo234", usersession.verifyPassword(admin,username,"foo234"));
214         assertEquals("wrong clear pwd foo234", data.getPassword(), "foo234");
215         assertTrue("wrong pwd (" + pwd + " works)", usersession.verifyPassword(admin,username,pwd) == false);
216         
217         usersession.setPassword(admin,username,"foo234");
218         log.debug("<test03LookupChangedUser2()");
219     }
220
221     /**
222      * DOCUMENT ME!
223      *
224      * @throws Exception DOCUMENT ME!
225      */

226     public void test04CreateNewUser() throws Exception JavaDoc {
227         log.debug(">test04CreateNewUser()");
228         username1 = genRandomUserName();
229         pwd1 = genRandomPwd();
230         usersession.addUser(admin,username1,pwd1,"C=SE,O=AnaTom,CN="+username1,null,null,false,SecConst.EMPTY_ENDENTITYPROFILE,SecConst.CERTPROFILE_FIXED_ENDUSER,SecConst.USER_INVALID,SecConst.TOKEN_SOFT_PEM,0,caid);
231         log.debug("created it again!");
232         log.debug("<test04CreateNewUser()");
233     }
234
235     /**
236      * DOCUMENT ME!
237      *
238      * @throws Exception DOCUMENT ME!
239      */

240     public void test05ListNewUser() throws Exception JavaDoc {
241         log.debug(">test05ListNewUser()");
242
243         Object JavaDoc obj1 = ctx.lookup("UserAdminSession");
244         IUserAdminSessionHome adminhome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, IUserAdminSessionHome.class);
245         IUserAdminSessionRemote admin = adminhome.create();
246         Collection JavaDoc coll = admin.findAllUsersByStatus(new Admin(Admin.TYPE_INTERNALUSER), UserDataConstants.STATUS_NEW);
247         Iterator JavaDoc iter = coll.iterator();
248         while (iter.hasNext()) {
249
250             UserDataVO data = (UserDataVO) iter.next();
251             log.debug("New user: " + data.getUsername() + ", " + data.getDN() + ", " + data.getEmail() + ", " + data.getStatus() + ", " + data.getType());
252             admin.setUserStatus(new Admin(Admin.TYPE_INTERNALUSER), data.getUsername(), UserDataConstants.STATUS_GENERATED);
253         }
254
255         Collection JavaDoc coll1 = admin.findAllUsersByStatus(new Admin(Admin.TYPE_INTERNALUSER), UserDataConstants.STATUS_NEW);
256         assertTrue("found NEW users though there should be none!", coll1.isEmpty());
257         log.debug("<test05ListNewUser()");
258     }
259
260     /**
261      * DOCUMENT ME!
262      *
263      * @throws Exception DOCUMENT ME!
264      */

265     public void test06RemoveUser() throws Exception JavaDoc {
266         log.debug(">test06RemoveUser()");
267
268         usersession.deleteUser(admin,username);
269         usersession.deleteUser(admin,username1);
270         log.debug("Removed it!");
271         log.debug("<test06RemoveUser()");
272     }
273 }
274
Popular Tags