KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > ca > store > TestCertificateProfile


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.store;
15
16 import java.util.ArrayList JavaDoc;
17
18 import javax.naming.Context JavaDoc;
19 import javax.naming.NamingException JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.apache.log4j.Logger;
24 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionHome;
25 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionRemote;
26 import org.ejbca.core.model.ca.certificateprofiles.CertificateProfile;
27 import org.ejbca.core.model.ca.certificateprofiles.CertificateProfileExistsException;
28 import org.ejbca.core.model.ca.certificateprofiles.EndUserCertificateProfile;
29 import org.ejbca.core.model.log.Admin;
30 import org.ejbca.util.dn.DNFieldExtractor;
31
32
33 /**
34  * Tests the certificate profile entity bean.
35  *
36  * @version $Id: TestCertificateProfile.java,v 1.8 2006/12/02 11:18:32 anatom Exp $
37  */

38 public class TestCertificateProfile extends TestCase {
39     private static Logger log = Logger.getLogger(TestCertificateProfile.class);
40
41     private static ICertificateStoreSessionRemote cacheAdmin;
42     private static ICertificateStoreSessionHome cacheHome;
43
44     private static final Admin admin = new Admin(Admin.TYPE_INTERNALUSER);
45
46     /**
47      * Creates a new TestCertificateProfile object.
48      *
49      * @param name name
50      */

51     public TestCertificateProfile(String JavaDoc name) {
52         super(name);
53     }
54
55     protected void setUp() throws Exception JavaDoc {
56         log.debug(">setUp()");
57         if (cacheAdmin == null) {
58             if (cacheHome == null) {
59                 Context JavaDoc jndiContext = getInitialContext();
60                 Object JavaDoc obj1 = jndiContext.lookup("CertificateStoreSession");
61                 cacheHome = (ICertificateStoreSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, ICertificateStoreSessionHome.class);
62             }
63             cacheAdmin = cacheHome.create();
64         }
65         log.debug("<setUp()");
66     }
67
68     protected void tearDown() throws Exception JavaDoc {
69     }
70
71     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
72         log.debug(">getInitialContext");
73         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
74         log.debug("<getInitialContext");
75         return ctx;
76     }
77
78
79     /**
80      * adds a profile to the database
81      *
82      * @throws Exception error
83      */

84     public void test01AddCertificateProfile() throws Exception JavaDoc {
85         log.debug(">test01AddCertificateProfile()");
86         boolean ret = false;
87         try {
88             CertificateProfile profile = new CertificateProfile();
89             profile.setCRLDistributionPointURI("TEST");
90             cacheAdmin.addCertificateProfile(admin, "TEST", profile);
91             ret = true;
92         } catch (CertificateProfileExistsException pee) {
93         }
94
95         assertTrue("Creating Certificate Profile failed", ret);
96         log.debug("<test01AddCertificateProfile()");
97     }
98
99     /**
100      * renames profile
101      *
102      * @throws Exception error
103      */

104     public void test02RenameCertificateProfile() throws Exception JavaDoc {
105         log.debug(">test02RenameCertificateProfile()");
106
107         boolean ret = false;
108         try {
109             cacheAdmin.renameCertificateProfile(admin, "TEST", "TEST2");
110             ret = true;
111         } catch (CertificateProfileExistsException pee) {
112         }
113         assertTrue("Renaming Certificate Profile failed", ret);
114
115         log.debug("<test02RenameCertificateProfile()");
116     }
117
118     /**
119      * clones profile
120      *
121      * @throws Exception error
122      */

123     public void test03CloneCertificateProfile() throws Exception JavaDoc {
124         log.debug(">test03CloneCertificateProfile()");
125         boolean ret = false;
126         try {
127             cacheAdmin.cloneCertificateProfile(admin, "TEST2", "TEST");
128             ret = true;
129         } catch (CertificateProfileExistsException pee) {
130         }
131         assertTrue("Cloning Certificate Profile failed", ret);
132         log.debug("<test03CloneCertificateProfile()");
133     }
134
135
136     /**
137      * edits profile
138      *
139      * @throws Exception error
140      */

141     public void test04EditCertificateProfile() throws Exception JavaDoc {
142         log.debug(">test04EditCertificateProfile()");
143
144         boolean ret = false;
145
146         CertificateProfile profile = cacheAdmin.getCertificateProfile(admin, "TEST");
147         assertTrue("Retrieving CertificateProfile failed", profile.getCRLDistributionPointURI().equals("TEST"));
148
149         profile.setCRLDistributionPointURI("TEST2");
150
151         cacheAdmin.changeCertificateProfile(admin, "TEST", profile);
152         ret = true;
153
154         assertTrue("Editing CertificateProfile failed", ret);
155
156
157         log.debug("<test04EditCertificateProfile()");
158     }
159
160
161     /**
162      * removes all profiles
163      *
164      * @throws Exception error
165      */

166     public void test05removeCertificateProfiles() throws Exception JavaDoc {
167         log.debug(">test05removeCertificateProfiles()");
168         boolean ret = false;
169         try {
170             cacheAdmin.removeCertificateProfile(admin, "TEST");
171             cacheAdmin.removeCertificateProfile(admin, "TEST2");
172             ret = true;
173         } catch (Exception JavaDoc pee) {
174         }
175         assertTrue("Removing Certificate Profile failed", ret);
176
177         log.debug("<test05removeCertificateProfiles()");
178     }
179     
180     public void test06createSubjectDNSubSet() throws Exception JavaDoc{
181         log.debug(">test06createSubjectDNSubSet()");
182         CertificateProfile profile = new CertificateProfile();
183         
184         ArrayList JavaDoc dnsubset = new ArrayList JavaDoc();
185         dnsubset.add(new Integer JavaDoc(DNFieldExtractor.CN));
186         dnsubset.add(new Integer JavaDoc(DNFieldExtractor.UID));
187         dnsubset.add(new Integer JavaDoc(DNFieldExtractor.GIVENNAME));
188         dnsubset.add(new Integer JavaDoc(DNFieldExtractor.SURNAME));
189         profile.setSubjectDNSubSet(dnsubset);
190         
191         String JavaDoc indn1 = "UID=PVE,CN=Philip Vendil,SN=123435,GIVENNAME=Philip,SURNAME=Vendil";
192         String JavaDoc outdn1 = profile.createSubjectDNSubSet(indn1);
193         String JavaDoc expecteddn1 = "UID=PVE,CN=Philip Vendil,GIVENNAME=Philip,SURNAME=Vendil";
194         assertTrue("createSubjectDNSubSet doesn't work" + outdn1 + " != "+ expecteddn1, expecteddn1.equalsIgnoreCase(outdn1));
195         
196         String JavaDoc indn2 = "UID=PVE,CN=Philip Vendil,CN=SecondUsername,SN=123435,SN=54321,GIVENNAME=Philip,SURNAME=Vendil";
197         String JavaDoc outdn2 = profile.createSubjectDNSubSet(indn2);
198         String JavaDoc expecteddn2 = "UID=PVE,CN=Philip Vendil,CN=SecondUsername,GIVENNAME=Philip,SURNAME=Vendil";
199         assertTrue("createSubjectDNSubSet doesn't work" + outdn2 + " != "+ expecteddn2, expecteddn2.equalsIgnoreCase(outdn2));
200         
201         log.debug(">test06createSubjectDNSubSet()");
202     }
203
204     public void test07createSubjectAltNameSubSet() throws Exception JavaDoc{
205         log.debug(">test07createSubjectAltNameSubSet()");
206
207         CertificateProfile profile = new CertificateProfile();
208         
209         ArrayList JavaDoc altnamesubset = new ArrayList JavaDoc();
210         altnamesubset.add(new Integer JavaDoc(DNFieldExtractor.RFC822NAME));
211         altnamesubset.add(new Integer JavaDoc(DNFieldExtractor.UPN));
212         profile.setSubjectAltNameSubSet(altnamesubset);
213         
214         String JavaDoc inaltname1 = "RFC822NAME=test@test.se,UPN=testacc@test.se,IPADDRESS=10.1.1.0";
215         String JavaDoc outaltname1 = profile.createSubjectAltNameSubSet(inaltname1);
216         String JavaDoc expectedaltname1 = "RFC822NAME=test@test.se,UPN=testacc@test.se";
217         assertTrue("createSubjectAltNameSubSet doesn't work" + outaltname1 + " != "+ expectedaltname1, expectedaltname1.equalsIgnoreCase(outaltname1));
218         
219         String JavaDoc inaltname2 = "RFC822NAME=test@test.se,RFC822NAME=test2@test2.se,UPN=testacc@test.se,IPADDRESS=10.1.1.0,IPADDRESS=10.1.1.2";
220         String JavaDoc outaltname2 = profile.createSubjectAltNameSubSet(inaltname2);
221         String JavaDoc expectedaltname2 = "RFC822NAME=test@test.se,RFC822NAME=test2@test2.se,UPN=testacc@test.se";
222         assertTrue("createSubjectAltNameSubSet doesn't work" + outaltname2 + " != "+ expectedaltname2, expectedaltname2.equalsIgnoreCase(outaltname2));
223         
224         log.debug(">test07createSubjectAltNameSubSet()");
225     }
226     
227     public void test08CertificateProfileValues() throws Exception JavaDoc {
228         CertificateProfile ep = new EndUserCertificateProfile();
229         assertEquals("2.5.29.32.0", ep.getCertificatePolicyId());
230         assertEquals(CertificateProfile.LATEST_VERSION, ep.getLatestVersion(),0);
231         String JavaDoc qcId = ep.getQCSemanticsId();
232         assertEquals("", qcId);
233         CertificateProfile cp = new CertificateProfile();
234         assertEquals("2.5.29.32.0", cp.getCertificatePolicyId());
235         assertEquals(CertificateProfile.LATEST_VERSION, cp.getLatestVersion(),0);
236         assertEquals("", cp.getQCSemanticsId());
237         cp.setQCSemanticsId("1.1.1.2");
238         assertEquals("1.1.1.2", cp.getQCSemanticsId());
239     }
240     
241 }
242
Popular Tags