1 13 14 package se.anatom.ejbca.batch; 15 16 import java.io.File ; 17 import java.util.Date ; 18 import java.util.Random ; 19 20 import javax.naming.Context ; 21 import javax.naming.NamingException ; 22 23 import junit.framework.TestCase; 24 25 import org.apache.log4j.Logger; 26 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionHome; 27 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionRemote; 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.ca.caadmin.CAInfo; 32 import org.ejbca.core.model.log.Admin; 33 import org.ejbca.ui.cli.batch.BatchMakeP12; 34 35 39 40 public class TestBatchMakeP12 extends TestCase { 41 private static Logger log = Logger.getLogger(TestBatchMakeP12.class); 42 private static Context ctx; 43 private static IUserAdminSessionHome home; 44 private static int caid; 45 private static Admin admin; 46 47 52 public TestBatchMakeP12(String name) { 53 super(name); 54 } 55 56 protected void setUp() throws Exception { 57 log.debug(">setUp()"); 58 admin = new Admin(Admin.TYPE_BATCHCOMMANDLINE_USER); 59 ctx = getInitialContext(); 60 Object obj = ctx.lookup("UserAdminSession"); 61 home = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, IUserAdminSessionHome.class); 62 63 obj = ctx.lookup("CAAdminSession"); 64 ICAAdminSessionHome cahome = (ICAAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, ICAAdminSessionHome.class); 65 ICAAdminSessionRemote casession = cahome.create(); 66 CAInfo info = casession.getCAInfo(admin, "TEST"); 67 caid = info.getCAId(); 68 69 log.debug("<setUp()"); 70 } 71 72 protected void tearDown() throws Exception { 73 } 74 75 private Context getInitialContext() throws NamingException { 76 log.debug(">getInitialContext"); 77 Context ctx = new javax.naming.InitialContext (); 78 log.debug("<getInitialContext"); 79 return ctx; 80 } 81 82 private String genRandomUserName() throws Exception { 83 Random rand = new Random (new Date ().getTime() + 4711); 85 String username = ""; 86 87 for (int i = 0; i < 6; i++) { 88 int randint = rand.nextInt(9); 89 username += (new Integer (randint)).toString(); 90 } 91 92 log.debug("Generated random username: username =" + username); 93 94 return username; 95 } 97 102 public void test01CreateNewUsers() throws Exception { 103 log.debug(">test01CreateNewUser()"); 104 IUserAdminSessionRemote data1 = null; 105 String username = genRandomUserName(); 106 107 data1 = home.create(); 108 109 Object o = null; 110 try { 111 data1.addUser(admin, username, "foo123", "C=SE, O=AnaTom, CN=" + username, "", username + "@anatom.se", false, 112 SecConst.EMPTY_ENDENTITYPROFILE, SecConst.CERTPROFILE_FIXED_ENDUSER, 113 SecConst.USER_ENDUSER, SecConst.TOKEN_SOFT_P12, 0, caid); 114 data1.setClearTextPassword(admin, username, "foo123"); 115 o = new String (""); 116 } catch (Exception e) { 117 assertNotNull("Failed to create user " + username, o); 118 } 119 120 log.debug("created " + username + ", pwd=foo123"); 121 122 String username1 = genRandomUserName(); 123 o = null; 124 try { 125 data1.addUser(admin, username1, "foo123", "C=SE, O=AnaTom, CN=" + username1, "", username1 + "@anatom.se", false, 126 SecConst.EMPTY_ENDENTITYPROFILE, SecConst.CERTPROFILE_FIXED_ENDUSER, 127 SecConst.USER_ENDUSER, SecConst.TOKEN_SOFT_P12, 0, caid); 128 data1.setClearTextPassword(admin, username1, "foo123"); 129 o = new String (""); 130 } catch (Exception e) { 131 assertNotNull("Failed to create user " + username1, o); 132 } 133 log.debug("created " + username1 + ", pwd=foo123"); 134 log.debug("<test01CreateNewUsers()"); 135 } 136 137 142 public void test02MakeP12() throws Exception { 143 log.debug(">test02MakeP12()"); 144 145 BatchMakeP12 makep12 = new BatchMakeP12(); 146 File tmpfile = File.createTempFile("ejbca", "p12"); 147 148 makep12.setMainStoreDir(tmpfile.getParent()); 150 makep12.createAllNew(); 151 log.debug("<test02MakeP12()"); 152 } 153 } 154 | Popular Tags |