001: /*************************************************************************
002: * *
003: * EJBCA: The OpenSource Certificate Authority *
004: * *
005: * This software is free software; you can redistribute it and/or *
006: * modify it under the terms of the GNU Lesser General Public *
007: * License as published by the Free Software Foundation; either *
008: * version 2.1 of the License, or any later version. *
009: * *
010: * See terms of license at gnu.org. *
011: * *
012: *************************************************************************/package se.anatom.ejbca.ra;
013:
014: import java.util.Calendar;
015: import java.util.Date;
016: import java.util.Random;
017:
018: import javax.naming.Context;
019: import javax.naming.NamingException;
020:
021: import junit.framework.TestCase;
022:
023: import org.apache.log4j.Logger;
024: import org.ejbca.core.ejb.ra.IUserAdminSessionHome;
025: import org.ejbca.core.ejb.ra.IUserAdminSessionRemote;
026: import org.ejbca.core.model.SecConst;
027: import org.ejbca.core.model.log.Admin;
028: import org.ejbca.util.CertTools;
029:
030: /**
031: * Tests the UserData entity bean and some parts of UserAdminSession.
032: *
033: * @version $Id: TestAddLotsofUsers.java,v 1.5 2006/12/22 09:29:59 herrvendil Exp $
034: */
035: public class TestAddLotsofUsers extends TestCase {
036: private static Logger log = Logger
037: .getLogger(TestAddLotsofUsers.class);
038: /**
039: * UserAdminSession handle, not static since different object should go to different session
040: * beans concurrently
041: */
042: private IUserAdminSessionRemote cacheAdmin;
043:
044: /** Handle to AdminSessionHome */
045: private static IUserAdminSessionHome cacheHome;
046:
047: //private static UserDataHome home;
048: private static String baseUsername;
049: private static String pwd;
050: private static int userNo = 0;
051: private static int caid;
052:
053: /**
054: * Creates a new TestAddLotsofUsers object.
055: *
056: * @param name name
057: */
058: public TestAddLotsofUsers(String name) {
059: super (name);
060: }
061:
062: protected void setUp() throws Exception {
063:
064: log.debug(">setUp()");
065: //Object obj = ctx.lookup("UserData");
066: //home = (UserDataHome) javax.rmi.PortableRemoteObject.narrow(obj, UserDataHome.class);
067: if (cacheAdmin == null) {
068: if (cacheHome == null) {
069: Context jndiContext = getInitialContext();
070: Object obj1 = jndiContext.lookup("UserAdminSession");
071: cacheHome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject
072: .narrow(obj1, IUserAdminSessionHome.class);
073: caid = "CN=TEST".hashCode();
074:
075: }
076:
077: cacheAdmin = cacheHome.create();
078: }
079:
080: Calendar cal = Calendar.getInstance();
081: baseUsername = "lotsausers" + cal.get(Calendar.SECOND) + "-";
082:
083: log.debug("<setUp()");
084: }
085:
086: protected void tearDown() throws Exception {
087: }
088:
089: private Context getInitialContext() throws NamingException {
090: log.debug(">getInitialContext");
091:
092: Context ctx = new javax.naming.InitialContext();
093: log.debug("<getInitialContext");
094:
095: return ctx;
096: }
097:
098: private String genUserName() throws Exception {
099: // Gen new user
100: userNo++;
101:
102: return baseUsername + userNo;
103: } // genRandomUserName
104:
105: private String genRandomPwd() throws Exception {
106: // Gen random pwd
107: Random rand = new Random(new Date().getTime() + 4812);
108: String password = "";
109:
110: for (int i = 0; i < 8; i++) {
111: int randint = rand.nextInt(9);
112: password += (new Integer(randint)).toString();
113: }
114:
115: //log.debug("Generated random pwd: password=" + password);
116: return password;
117: } // genRandomPwd
118:
119: /**
120: * tests creating 2000 users
121: *
122: * @throws Exception error
123: */
124: public void test01Create2000Users() throws Exception {
125: log.debug(">test01Create2000Users()");
126:
127: //UserDataRemote data1=null;
128: Admin administrator = new Admin(Admin.TYPE_INTERNALUSER);
129:
130: for (int i = 0; i < 2000; i++) {
131: String username = genUserName();
132: pwd = genRandomPwd();
133:
134: /*
135: data1 = home.create(username, pwd, "C=SE, O=AnaTom, CN="+username);
136: assertNotNull("Error creating", data1);
137: */
138: int type = SecConst.USER_ENDUSER;
139: int token = SecConst.TOKEN_SOFT_P12;
140: int profileid = SecConst.EMPTY_ENDENTITYPROFILE;
141: int certificatetypeid = SecConst.CERTPROFILE_FIXED_ENDUSER;
142: int hardtokenissuerid = SecConst.NO_HARDTOKENISSUER;
143: String dn = "C=SE, O=AnaTom, CN=" + username;
144: String subjectaltname = "rfc822Name=" + username
145: + "@foo.se";
146: String email = username + "@foo.se";
147: if (cacheAdmin.findUser(administrator, username) != null) {
148: System.out
149: .println("Error : User already exists in the database.");
150: }
151: cacheAdmin.addUser(administrator, username, pwd, CertTools
152: .stringToBCDNString(dn), subjectaltname, email,
153: false, profileid, certificatetypeid, type, token,
154: hardtokenissuerid, caid);
155: cacheAdmin.setClearTextPassword(administrator, username,
156: pwd);
157: if (i % 100 == 0) {
158: log.debug("Created " + i + " users...");
159: }
160: }
161: log.debug("Created 2000 users!");
162: log.debug("<test01Create2000Users()");
163: }
164: }
|