1 13 14 package se.anatom.ejbca.ra; 15 16 import java.util.Collection ; 17 import java.util.Date ; 18 import java.util.Iterator ; 19 import java.util.Random ; 20 21 import javax.naming.Context ; 22 import javax.naming.NamingException ; 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 41 public class TestUserData extends TestCase { 42 43 private static Logger log = Logger.getLogger(TestUserData.class); 44 private static Context ctx; 45 private static IUserAdminSessionRemote usersession; 46 private static String username; 47 private static String username1; 48 private static String pwd; 49 private static String pwd1; 50 private static int caid; 51 private static Admin admin = null; 52 53 58 public TestUserData(String name) { 59 super(name); 60 } 61 62 protected void setUp() throws Exception { 63 log.debug(">setUp()"); 64 ctx = getInitialContext(); 65 66 caid = "CN=TEST".hashCode(); 67 68 Object 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 { 77 } 78 79 private Context getInitialContext() throws NamingException { 80 log.debug(">getInitialContext"); 81 82 Context ctx = new javax.naming.InitialContext (); 83 log.debug("<getInitialContext"); 84 85 return ctx; 86 } 87 88 private String genRandomUserName() throws Exception { 89 Random rand = new Random (new Date ().getTime() + 4711); 91 String username = ""; 92 for (int i = 0; i < 6; i++) { 93 int randint = rand.nextInt(9); 94 username += (new Integer (randint)).toString(); 95 } 96 log.debug("Generated random username: username =" + username); 97 98 return username; 99 } 101 private String genRandomPwd() throws Exception { 102 Random rand = new Random (new Date ().getTime() + 4812); 104 String password = ""; 105 106 for (int i = 0; i < 8; i++) { 107 int randint = rand.nextInt(9); 108 password += (new Integer (randint)).toString(); 109 } 110 111 log.debug("Generated random pwd: password=" + password); 112 113 return password; 114 } 116 117 122 public void test01CreateNewUser() throws Exception { 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 136 public void test02LookupAndChangeUser() throws Exception { 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 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 166 public void test03LookupChangedUser() throws Exception { 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 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 196 public void test03LookupChangedUser2() throws Exception { 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 226 public void test04CreateNewUser() throws Exception { 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 240 public void test05ListNewUser() throws Exception { 241 log.debug(">test05ListNewUser()"); 242 243 Object obj1 = ctx.lookup("UserAdminSession"); 244 IUserAdminSessionHome adminhome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, IUserAdminSessionHome.class); 245 IUserAdminSessionRemote admin = adminhome.create(); 246 Collection coll = admin.findAllUsersByStatus(new Admin(Admin.TYPE_INTERNALUSER), UserDataConstants.STATUS_NEW); 247 Iterator 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 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 265 public void test06RemoveUser() throws Exception { 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 |