1 13 14 package se.anatom.ejbca.ra; 15 16 import java.rmi.ServerException ; 17 import java.util.Date ; 18 import java.util.Random ; 19 20 import javax.ejb.DuplicateKeyException ; 21 import javax.naming.Context ; 22 import javax.naming.NamingException ; 23 import javax.transaction.TransactionRolledbackException ; 24 25 import junit.framework.TestCase; 26 27 import org.apache.log4j.Logger; 28 import org.ejbca.core.ejb.ra.IUserAdminSessionHome; 29 import org.ejbca.core.ejb.ra.IUserAdminSessionRemote; 30 import org.ejbca.core.model.SecConst; 31 import org.ejbca.core.model.log.Admin; 32 import org.ejbca.core.model.ra.NotFoundException; 33 34 35 39 public class TestUserAdminSession extends TestCase { 40 41 private static Logger log = Logger.getLogger(TestUserAdminSession.class); 42 private static Context ctx; 43 private static IUserAdminSessionRemote usersession; 44 private static String username; 45 private static String pwd; 46 private static int caid; 47 private static Admin admin = null; 48 49 54 public TestUserAdminSession(String name) { 55 super(name); 56 } 57 58 protected void setUp() throws Exception { 59 60 log.debug(">setUp()"); 61 ctx = getInitialContext(); 62 caid = "CN=TEST".hashCode(); 63 Object obj = ctx.lookup("UserAdminSession"); 64 IUserAdminSessionHome userhome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, IUserAdminSessionHome.class); 65 usersession = userhome.create(); 66 admin = new Admin(Admin.TYPE_INTERNALUSER); 67 68 log.debug("<setUp()"); 69 } 70 71 protected void tearDown() throws Exception { 72 } 73 74 private Context getInitialContext() throws NamingException { 75 log.debug(">getInitialContext"); 76 77 Context ctx = new javax.naming.InitialContext (); 78 log.debug("<getInitialContext"); 79 80 return ctx; 81 } 82 83 private String genRandomUserName() throws Exception { 84 Random rand = new Random (new Date ().getTime() + 4711); 86 String username = ""; 87 for (int i = 0; i < 6; i++) { 88 int randint = rand.nextInt(9); 89 username += (new Integer (randint)).toString(); 90 } 91 log.debug("Generated random username: username =" + username); 92 93 return username; 94 } 96 private String genRandomPwd() throws Exception { 97 Random rand = new Random (new Date ().getTime() + 4812); 99 String password = ""; 100 101 for (int i = 0; i < 8; i++) { 102 int randint = rand.nextInt(9); 103 password += (new Integer (randint)).toString(); 104 } 105 106 log.debug("Generated random pwd: password=" + password); 107 108 return password; 109 } 111 112 117 public void test01AddUser() throws Exception { 118 log.debug(">test01AddUser()"); 119 120 username = genRandomUserName(); 122 pwd = genRandomPwd(); 123 String email = username + "@anatom.se"; 124 usersession.addUser(admin, username, pwd, "C=SE, O=AnaTom, CN=" + username, "rfc822name=" + email, email, false, SecConst.EMPTY_ENDENTITYPROFILE, SecConst.CERTPROFILE_FIXED_ENDUSER, SecConst.USER_ENDUSER, SecConst.TOKEN_SOFT_P12, 0, caid); 125 log.debug("created user: " + username + ", " + pwd + ", C=SE, O=AnaTom, CN=" + username); 126 boolean userexists = false; 128 try { 129 usersession.addUser(admin, username, pwd, "C=SE, O=AnaTom, CN=" + username, "rfc822name=" + email, email, false, SecConst.EMPTY_ENDENTITYPROFILE, SecConst.CERTPROFILE_FIXED_ENDUSER, SecConst.USER_ENDUSER, SecConst.TOKEN_SOFT_P12, 0, caid); 130 } catch (DuplicateKeyException e) { 131 userexists = true; 133 } catch (TransactionRolledbackException e) { 134 if (e.getCause() instanceof DuplicateKeyException ) { 136 userexists = true; 137 } 138 } catch (ServerException e) { 139 userexists = true; 141 } 142 assertTrue("User already exist does not throw DuplicateKeyException", userexists); 143 144 log.debug("<test01AddUser()"); 145 } 146 147 152 public void test01DeleteUser() throws Exception { 153 log.debug(">test01DeleteUser()"); 154 155 usersession.deleteUser(admin, username); 156 log.debug("deleted user: " + username); 157 boolean removed = false; 159 try { 160 usersession.deleteUser(admin, username); 161 } catch (NotFoundException e) { 162 removed = true; 163 } 164 assertTrue("User does not exist does not throw NotFoundException", removed); 165 166 log.debug("<test01DeleteUser()"); 167 } 168 } 169 | Popular Tags |