KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > ca > caadmin > TestCmsCAService


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 org.ejbca.core.model.ca.caadmin;
15
16 import java.security.cert.CertStore JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import javax.naming.Context JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import javax.security.auth.x500.X500Principal JavaDoc;
24
25 import junit.framework.TestCase;
26
27 import org.apache.log4j.Logger;
28 import org.bouncycastle.cms.CMSProcessable;
29 import org.bouncycastle.cms.CMSSignedData;
30 import org.bouncycastle.cms.CMSSignedGenerator;
31 import org.bouncycastle.cms.SignerId;
32 import org.bouncycastle.cms.SignerInformation;
33 import org.bouncycastle.cms.SignerInformationStore;
34 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionHome;
35 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionRemote;
36 import org.ejbca.core.ejb.ca.sign.ISignSessionHome;
37 import org.ejbca.core.ejb.ca.sign.ISignSessionRemote;
38 import org.ejbca.core.model.ca.caadmin.extendedcaservices.CmsCAServiceInfo;
39 import org.ejbca.core.model.ca.caadmin.extendedcaservices.CmsCAServiceRequest;
40 import org.ejbca.core.model.ca.caadmin.extendedcaservices.CmsCAServiceResponse;
41 import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceInfo;
42 import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceNotActiveException;
43 import org.ejbca.core.model.log.Admin;
44 import org.ejbca.util.CertTools;
45
46
47 /**
48  * Tests the CertTools class.
49  *
50  * @version $Id: TestCmsCAService.java,v 1.1 2006/12/28 13:51:15 anatom Exp $
51  */

52 public class TestCmsCAService extends TestCase {
53     private static Logger log = Logger.getLogger(TestCmsCAService.class);
54
55     private byte[] doc = "foo123".getBytes();
56
57     private static ISignSessionRemote remote;
58     private static ICAAdminSessionRemote casession;
59     private static int rsacaid = 0;
60     private Admin admin;
61
62     /**
63      * Creates a new TestCertTools object.
64      *
65      * @param name DOCUMENT ME!
66      */

67     public TestCmsCAService(String JavaDoc name) throws Exception JavaDoc {
68         super(name);
69         // Install BouncyCastle provider
70
CertTools.installBCProvider();
71         Context JavaDoc ctx = getInitialContext();
72         Object JavaDoc obj = ctx.lookup(ISignSessionHome.JNDI_NAME);
73         ISignSessionHome home = (ISignSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, ISignSessionHome.class);
74         remote = home.create();
75
76         admin = new Admin(Admin.TYPE_BATCHCOMMANDLINE_USER);
77
78         obj = ctx.lookup(ICAAdminSessionHome.JNDI_NAME);
79         ICAAdminSessionHome cahome = (ICAAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, ICAAdminSessionHome.class);
80         casession = cahome.create();
81         CAInfo inforsa = casession.getCAInfo(admin, "TEST");
82         rsacaid = inforsa.getCAId();
83         if (rsacaid == 0){
84             assertTrue("No active RSA CA! Must have at least one active CA to run tests!", false);
85         }
86
87     }
88
89     protected void setUp() throws Exception JavaDoc {
90         log.debug(">setUp()");
91         CertTools.installBCProvider();
92         log.debug("<setUp()");
93     }
94
95     protected void tearDown() throws Exception JavaDoc {
96     }
97
98     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
99         log.debug(">getInitialContext");
100
101         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
102         log.debug("<getInitialContext");
103
104         return ctx;
105     }
106
107     /**
108      */

109     public void test01CmsCAServiceNotActive() throws Exception JavaDoc {
110         CmsCAServiceRequest request = new CmsCAServiceRequest(doc, true);
111         
112         // First try a request when the service is not active
113
boolean active = true;
114         try {
115             remote.extendedService(admin, rsacaid, request);
116         } catch (ExtendedCAServiceNotActiveException e) {
117             active = false;
118         }
119         // By default the CA service is not active
120
assertTrue(!active);
121         
122     }
123     
124     /**
125      */

126     public void test02ActivateCmsCAService() throws Exception JavaDoc {
127         // Activate the CA service in the CA
128
CAInfo cainfo = casession.getCAInfo(admin, "TEST");
129         ArrayList JavaDoc newlist = new ArrayList JavaDoc();
130         newlist.add(new CmsCAServiceInfo(ExtendedCAServiceInfo.STATUS_ACTIVE, false));
131         cainfo.setExtendedCAServiceInfos(newlist);
132         casession.editCA(admin, cainfo);
133     }
134     
135     /**
136      */

137     public void test03CmsCAServiceActive() throws Exception JavaDoc {
138         CmsCAServiceRequest request = new CmsCAServiceRequest(doc, true);
139         CmsCAServiceResponse resp = null;
140         // Try the request again
141
boolean active = true;
142         try {
143             resp = (CmsCAServiceResponse)remote.extendedService(admin, rsacaid, request);
144         } catch (ExtendedCAServiceNotActiveException e) {
145             active = false;
146         }
147         // By default the CA service is not active
148
assertTrue(active);
149         
150         assertNotNull(resp);
151         byte[] respdoc = resp.getCmsDocument();
152         assertNotNull(resp);
153         CMSSignedData csd = new CMSSignedData(respdoc);
154         SignerInformationStore infoStore = csd.getSignerInfos();
155         Collection JavaDoc signers = infoStore.getSigners();
156         Iterator JavaDoc iter = signers.iterator();
157 // FileOutputStream fos = new FileOutputStream("/home/tomas/p7.der");
158
// fos.write(doc);
159
// fos.close();
160
if (iter.hasNext()) {
161             SignerInformation si = (SignerInformation)iter.next();
162             assertNotNull(si);
163             //log.info("Digest alg is: "+si.getDigestAlgOID());
164
assertEquals(CMSSignedGenerator.DIGEST_SHA1, si.getDigestAlgOID());
165             SignerId sid = si.getSID();
166             //log.info(sid.toString());
167
X500Principal JavaDoc issuer = sid.getIssuer();
168             assertNotNull(issuer);
169             assertEquals("CN=TEST", issuer.getName());
170         }
171         CertStore JavaDoc store = csd.getCertificatesAndCRLs("Collection", "BC");
172         Collection JavaDoc certs = store.getCertificates(null);
173         assertEquals(2, certs.size());
174         
175         CMSProcessable cp = csd.getSignedContent();
176         Object JavaDoc o = cp.getContent();
177         byte[] ob = (byte[])o;
178         assertEquals(new String JavaDoc(doc), new String JavaDoc(ob));
179     }
180
181     /**
182      */

183     public void test04DeActivateCmsCAService() throws Exception JavaDoc {
184         // Activate the CA service in the CA
185
CAInfo cainfo = casession.getCAInfo(admin, "TEST");
186         ArrayList JavaDoc newlist = new ArrayList JavaDoc();
187         newlist.add(new CmsCAServiceInfo(ExtendedCAServiceInfo.STATUS_INACTIVE, false));
188         cainfo.setExtendedCAServiceInfos(newlist);
189         casession.editCA(admin, cainfo);
190     }
191
192 }
193
Popular Tags