KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > ca > sign > TestSignLotsOfCerts


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.ca.sign;
15
16 import java.rmi.RemoteException JavaDoc;
17 import java.security.KeyPair JavaDoc;
18 import java.security.KeyPairGenerator JavaDoc;
19 import java.security.cert.X509Certificate JavaDoc;
20 import java.security.interfaces.RSAPrivateKey JavaDoc;
21
22 import javax.ejb.DuplicateKeyException JavaDoc;
23 import javax.naming.Context JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.apache.log4j.Logger;
29 import org.ejbca.core.ejb.ca.sign.ISignSessionHome;
30 import org.ejbca.core.ejb.ca.sign.ISignSessionRemote;
31 import org.ejbca.core.ejb.ra.IUserAdminSessionHome;
32 import org.ejbca.core.ejb.ra.IUserAdminSessionRemote;
33 import org.ejbca.core.model.SecConst;
34 import org.ejbca.core.model.log.Admin;
35 import org.ejbca.core.model.ra.UserDataConstants;
36 import org.ejbca.util.CertTools;
37
38
39 /** This is a manual test that requires some manual set up and configuration.
40  * -caid should a CA that has "finish user" set to off
41  * -the users performancefoono1-10 should not exist in the database
42  *
43  *
44  * @version $Id: TestSignLotsOfCerts.java,v 1.7 2006/08/12 09:49:53 herrvendil Exp $
45  */

46 public class TestSignLotsOfCerts extends TestCase {
47     private static Logger log = Logger.getLogger(TestSignLotsOfCerts.class);
48     public static Context JavaDoc ctx;
49     private static IUserAdminSessionRemote usersession;
50     public static KeyPair JavaDoc keys;
51     private static int caid = 0;
52     public Admin admin;
53
54     /**
55      * Creates a new TestSignSession object.
56      *
57      * @param name name
58      */

59     public TestSignLotsOfCerts(String JavaDoc name) {
60         super(name);
61     }
62
63     protected void setUp() throws Exception JavaDoc {
64         log.debug(">setUp()");
65
66         // Install BouncyCastle provider
67
CertTools.installBCProvider();
68
69         admin = new Admin(Admin.TYPE_BATCHCOMMANDLINE_USER);
70
71         caid = -1688117755; // EDIT THIS
72

73         ctx = getInitialContext();
74         Object JavaDoc obj = ctx.lookup("UserAdminSession");
75         IUserAdminSessionHome userhome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, IUserAdminSessionHome.class);
76         usersession = userhome.create();
77         
78         keys = genKeys();
79
80         log.debug("<setUp()");
81     }
82
83     protected void tearDown() throws Exception JavaDoc {
84     }
85
86     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
87         log.debug(">getInitialContext");
88         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
89         log.debug("<getInitialContext");
90         return ctx;
91     }
92
93     /**
94      * Generates a RSA key pair.
95      *
96      * @return KeyPair the generated key pair
97      *
98      * @throws Exception if en error occurs...
99      */

100     private static KeyPair JavaDoc genKeys() throws Exception JavaDoc {
101         KeyPairGenerator JavaDoc keygen = KeyPairGenerator.getInstance("RSA", "BC");
102         keygen.initialize(1024);
103         log.debug("Generating keys, please wait...");
104         KeyPair JavaDoc rsaKeys = keygen.generateKeyPair();
105         log.debug("Generated " + rsaKeys.getPrivate().getAlgorithm() + " keys with length" +
106                 ((RSAPrivateKey JavaDoc) rsaKeys.getPrivate()).getModulus().bitLength());
107
108         return rsaKeys;
109     } // genKeys
110

111     private void newUser(String JavaDoc post) throws Exception JavaDoc {
112         // Make user that we know...
113
boolean userExists = false;
114         try {
115             usersession.addUser(admin,"performancefoo"+post,"foo123","C=SE,O=AnaTom,OU=Performance Test,CN=performancefoo",null,"performancefoo@foo.se",false,SecConst.EMPTY_ENDENTITYPROFILE,SecConst.CERTPROFILE_FIXED_ENDUSER,SecConst.USER_ENDUSER,SecConst.TOKEN_SOFT_PEM,0,caid);
116             log.debug("created user: performancefoo"+post+", foo123, C=SE, O=AnaTom, OU=Performance Test,CN=performancefoo");
117         } catch (RemoteException JavaDoc re) {
118             if (re.detail instanceof DuplicateKeyException JavaDoc) {
119                 userExists = true;
120             }
121         } catch (DuplicateKeyException JavaDoc dke) {
122             userExists = true;
123         }
124         if (userExists) {
125             log.info("User performancefoo already exists, resetting status.");
126             usersession.setUserStatus(admin,"performancefoo"+post,UserDataConstants.STATUS_NEW);
127             log.debug("Reset status to NEW");
128         }
129
130     }
131     /**
132      * creates new user
133      *
134      * @throws Exception if en error occurs...
135      */

136     public void test01CreateNewUser() throws Exception JavaDoc {
137         log.debug(">test01CreateNewUser()");
138         newUser("no1");
139         newUser("no2");
140         newUser("no3");
141         newUser("no4");
142         newUser("no5");
143         newUser("no6");
144         newUser("no7");
145         newUser("no8");
146         newUser("no9");
147         newUser("no10");
148         log.debug("<test01CreateNewUser()");
149     }
150
151     /**
152      * creates cert
153      *
154      * @throws Exception if en error occurs...
155      */

156     public void test03SignLotsOfCerts() throws Exception JavaDoc {
157         log.debug(">test03SignLotsOfCerts()");
158
159         long before = System.currentTimeMillis();
160         Thread JavaDoc no1 = new Thread JavaDoc(new SignTester(),"no1");
161         Thread JavaDoc no2 = new Thread JavaDoc(new SignTester(),"no2");
162         Thread JavaDoc no3 = new Thread JavaDoc(new SignTester(),"no3");
163         Thread JavaDoc no4 = new Thread JavaDoc(new SignTester(),"no4");
164         Thread JavaDoc no5 = new Thread JavaDoc(new SignTester(),"no5");
165         Thread JavaDoc no6 = new Thread JavaDoc(new SignTester(),"no6");
166         Thread JavaDoc no7 = new Thread JavaDoc(new SignTester(),"no7");
167         Thread JavaDoc no8 = new Thread JavaDoc(new SignTester(),"no8");
168         Thread JavaDoc no9 = new Thread JavaDoc(new SignTester(),"no9");
169         Thread JavaDoc no10 = new Thread JavaDoc(new SignTester(),"no10");
170         no1.start();
171         System.out.println("Started no1");
172         no2.start();
173         System.out.println("Started no2");
174         no3.start();
175         System.out.println("Started no3");
176         no4.start();
177         System.out.println("Started no4");
178         no5.start();
179         System.out.println("Started no5");
180         no6.start();
181         System.out.println("Started no6");
182         no7.start();
183         System.out.println("Started no7");
184         no8.start();
185         System.out.println("Started no8");
186         no9.start();
187         System.out.println("Started no9");
188         no10.start();
189         System.out.println("Started no10");
190         no1.join();
191         no2.join();
192         no3.join();
193         no4.join();
194         no5.join();
195         no6.join();
196         no7.join();
197         no8.join();
198         no9.join();
199         no10.join();
200         long after = System.currentTimeMillis();
201         long diff = after - before;
202         System.out.println("All threads finished. Total time: "+diff);
203         //FileOutputStream fos = new FileOutputStream("testcert.crt");
204
//fos.write(cert.getEncoded());
205
//fos.close();
206
log.debug("<test03SignLotsOfCerts()");
207     }
208     
209     private class SignTester implements Runnable JavaDoc {
210         public void run() {
211             try {
212                 Object JavaDoc obj = ctx.lookup("RSASignSession");
213                 ISignSessionHome rsahome = (ISignSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, ISignSessionHome.class);
214                 ISignSessionRemote rsaremote = rsahome.create();
215                 String JavaDoc user = "performancefoo"+Thread.currentThread().getName();
216                 long before = System.currentTimeMillis();
217                 for (int i = 0; i<1000;i++) {
218                     // user that we know exists...
219
X509Certificate JavaDoc cert = (X509Certificate JavaDoc) rsaremote.createCertificate(admin, user, "foo123", keys.getPublic());
220                     assertNotNull("Misslyckades skapa cert", cert);
221                     if ((i % 100) == 0) {
222                         long mellantid = System.currentTimeMillis() - before;
223                         System.out.println(Thread.currentThread().getName()+" har skapat "+i+", tid="+mellantid);
224                         
225                     }
226                 }
227                 long after = System.currentTimeMillis();
228                 long diff = after - before;
229                 System.out.println("Tidsåtgång ("+Thread.currentThread().getName()+"): "+diff);
230             } catch (Exception JavaDoc e) {
231                 // TODO Auto-generated catch block
232
e.printStackTrace();
233             }
234         }
235     }
236 }
237
Popular Tags