KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > ca > caadmin > TestRenewCA


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.caadmin;
15
16 import java.security.cert.X509Certificate JavaDoc;
17 import java.util.Arrays JavaDoc;
18
19 import javax.naming.Context JavaDoc;
20 import javax.naming.NamingException JavaDoc;
21
22 import junit.framework.TestCase;
23
24 import org.apache.log4j.Logger;
25 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionHome;
26 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionRemote;
27 import org.ejbca.core.model.ca.caadmin.X509CAInfo;
28 import org.ejbca.core.model.log.Admin;
29
30 /**
31  * Tests and removes the ca data entity bean.
32  *
33  * @version $Id: TestRenewCA.java,v 1.1.2.1 2007/02/27 15:23:36 anatom Exp $
34  */

35 public class TestRenewCA extends TestCase {
36     private static Logger log = Logger.getLogger(TestCAs.class);
37
38     private ICAAdminSessionRemote cacheAdmin;
39     private static ICAAdminSessionHome cacheHome;
40
41     private static final Admin admin = new Admin(Admin.TYPE_INTERNALUSER);
42
43     /**
44      * Creates a new TestCAs object.
45      *
46      * @param name name
47      */

48     public TestRenewCA(String JavaDoc name) {
49         super(name);
50     }
51
52     protected void setUp() throws Exception JavaDoc {
53
54         log.debug(">setUp()");
55
56         if (cacheAdmin == null) {
57             if (cacheHome == null) {
58                 Context JavaDoc jndiContext = getInitialContext();
59                 Object JavaDoc obj1 = jndiContext.lookup("CAAdminSession");
60                 cacheHome = (ICAAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, ICAAdminSessionHome.class);
61             }
62
63             cacheAdmin = cacheHome.create();
64         }
65
66         log.debug("<setUp()");
67     }
68
69     protected void tearDown() throws Exception JavaDoc {
70     }
71
72     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
73         log.debug(">getInitialContext");
74
75         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
76         log.debug("<getInitialContext");
77
78         return ctx;
79     }
80
81     /**
82      * edits ca and checks that it's stored correctly.
83      *
84      * @throws Exception error
85      */

86     public void test01renewCA() throws Exception JavaDoc {
87         log.debug(">test01renewCA()");
88
89         X509CAInfo info = (X509CAInfo) cacheAdmin.getCAInfo(admin, "TEST");
90         X509Certificate JavaDoc orgcert = (X509Certificate JavaDoc) info.getCertificateChain().iterator().next();
91         
92         cacheAdmin.renewCA(admin,info.getCAId(),null,false);
93         X509CAInfo newinfo = (X509CAInfo) cacheAdmin.getCAInfo(admin, "TEST");
94         X509Certificate JavaDoc newcertsamekeys = (X509Certificate JavaDoc) newinfo.getCertificateChain().iterator().next();
95         assertTrue(!orgcert.getSerialNumber().equals(newcertsamekeys.getSerialNumber()));
96         byte[] orgkey = orgcert.getPublicKey().getEncoded();
97         byte[] samekey = newcertsamekeys.getPublicKey().getEncoded();
98         assertTrue(Arrays.equals(orgkey,samekey));
99         // The new certificate must have a validity greater than the old cert
100
assertTrue(newcertsamekeys.getNotAfter().after(orgcert.getNotAfter()));
101
102         cacheAdmin.renewCA(admin,info.getCAId(),null,true);
103         X509CAInfo newinfo2 = (X509CAInfo) cacheAdmin.getCAInfo(admin, "TEST");
104         X509Certificate JavaDoc newcertnewkeys = (X509Certificate JavaDoc) newinfo2.getCertificateChain().iterator().next();
105         assertTrue(!orgcert.getSerialNumber().equals(newcertnewkeys.getSerialNumber()));
106         byte[] newkey = newcertnewkeys.getPublicKey().getEncoded();
107         assertFalse(Arrays.equals(orgkey,newkey));
108         
109         log.debug("<test01renewCA()");
110     }
111
112 }
113
Popular Tags