KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > batch > TestBatchMakeP12


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.batch;
15
16 import java.io.File JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.Random JavaDoc;
19
20 import javax.naming.Context JavaDoc;
21 import javax.naming.NamingException JavaDoc;
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 /** Tests the batch making of soft cards.
36  *
37  * @version $Id: TestBatchMakeP12.java,v 1.3.10.1 2007/06/05 13:37:23 anatom Exp $
38  */

39
40 public class TestBatchMakeP12 extends TestCase {
41     private static Logger log = Logger.getLogger(TestBatchMakeP12.class);
42     private static Context JavaDoc ctx;
43     private static IUserAdminSessionHome home;
44     private static int caid;
45     private static Admin admin;
46
47     /**
48      * Creates a new TestBatchMakeP12 object.
49      *
50      * @param name name
51      */

52     public TestBatchMakeP12(String JavaDoc name) {
53         super(name);
54     }
55
56     protected void setUp() throws Exception JavaDoc {
57         log.debug(">setUp()");
58         admin = new Admin(Admin.TYPE_BATCHCOMMANDLINE_USER);
59         ctx = getInitialContext();
60         Object JavaDoc 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 JavaDoc {
73     }
74
75     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
76         log.debug(">getInitialContext");
77         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
78         log.debug("<getInitialContext");
79         return ctx;
80     }
81
82     private String JavaDoc genRandomUserName() throws Exception JavaDoc {
83         // Gen random user
84
Random JavaDoc rand = new Random JavaDoc(new Date JavaDoc().getTime() + 4711);
85         String JavaDoc username = "";
86
87         for (int i = 0; i < 6; i++) {
88             int randint = rand.nextInt(9);
89             username += (new Integer JavaDoc(randint)).toString();
90         }
91
92         log.debug("Generated random username: username =" + username);
93
94         return username;
95     } // genRandomUserName
96

97     /**
98      * test creation of new user
99      *
100      * @throws Exception error
101      */

102     public void test01CreateNewUsers() throws Exception JavaDoc {
103         log.debug(">test01CreateNewUser()");
104         IUserAdminSessionRemote data1 = null;
105         String JavaDoc username = genRandomUserName();
106
107         data1 = home.create();
108
109         Object JavaDoc 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 JavaDoc("");
116         } catch (Exception JavaDoc e) {
117             assertNotNull("Failed to create user " + username, o);
118         }
119
120         log.debug("created " + username + ", pwd=foo123");
121
122         String JavaDoc 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 JavaDoc("");
130         } catch (Exception JavaDoc e) {
131             assertNotNull("Failed to create user " + username1, o);
132         }
133         log.debug("created " + username1 + ", pwd=foo123");
134         log.debug("<test01CreateNewUsers()");
135     }
136
137     /**
138      * Tests creation of P12 file
139      *
140      * @throws Exception error
141      */

142     public void test02MakeP12() throws Exception JavaDoc {
143         log.debug(">test02MakeP12()");
144
145         BatchMakeP12 makep12 = new BatchMakeP12();
146         File JavaDoc tmpfile = File.createTempFile("ejbca", "p12");
147
148         //System.out.println("tempdir="+tmpfile.getParent());
149
makep12.setMainStoreDir(tmpfile.getParent());
150         makep12.createAllNew();
151         log.debug("<test02MakeP12()");
152     }
153 }
154
Popular Tags