KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > hardtoken > TestHardToken


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.hardtoken;
15
16 import java.security.cert.X509Certificate JavaDoc;
17 import java.util.ArrayList 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.store.CertificateDataBean;
26 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionHome;
27 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionRemote;
28 import org.ejbca.core.ejb.hardtoken.IHardTokenSessionHome;
29 import org.ejbca.core.ejb.hardtoken.IHardTokenSessionRemote;
30 import org.ejbca.core.model.SecConst;
31 import org.ejbca.core.model.hardtoken.HardTokenData;
32 import org.ejbca.core.model.hardtoken.HardTokenExistsException;
33 import org.ejbca.core.model.hardtoken.types.SwedishEIDHardToken;
34 import org.ejbca.core.model.hardtoken.types.TurkishEIDHardToken;
35 import org.ejbca.core.model.log.Admin;
36 import org.ejbca.util.Base64;
37 import org.ejbca.util.CertTools;
38
39 /**
40  * Tests the hard token related entity beans.
41  *
42  * @version $Id: TestHardToken.java,v 1.6 2006/11/11 12:46:04 herrvendil Exp $
43  */

44 public class TestHardToken extends TestCase {
45     private static Logger log = Logger.getLogger(TestHardToken.class);
46     private IHardTokenSessionRemote cacheAdmin;
47     private ICertificateStoreSessionRemote certStore;
48
49
50     private static IHardTokenSessionHome cacheHome;
51     private static ICertificateStoreSessionHome storeHome;
52
53     private static final Admin admin = new Admin(Admin.TYPE_INTERNALUSER);
54
55     static byte[] testcert = Base64.decode(("MIICWzCCAcSgAwIBAgIIJND6Haa3NoAwDQYJKoZIhvcNAQEFBQAwLzEPMA0GA1UE"
56             + "AxMGVGVzdENBMQ8wDQYDVQQKEwZBbmFUb20xCzAJBgNVBAYTAlNFMB4XDTAyMDEw"
57             + "ODA5MTE1MloXDTA0MDEwODA5MjE1MlowLzEPMA0GA1UEAxMGMjUxMzQ3MQ8wDQYD"
58             + "VQQKEwZBbmFUb20xCzAJBgNVBAYTAlNFMIGdMA0GCSqGSIb3DQEBAQUAA4GLADCB"
59             + "hwKBgQCQ3UA+nIHECJ79S5VwI8WFLJbAByAnn1k/JEX2/a0nsc2/K3GYzHFItPjy"
60             + "Bv5zUccPLbRmkdMlCD1rOcgcR9mmmjMQrbWbWp+iRg0WyCktWb/wUS8uNNuGQYQe"
61             + "ACl11SAHFX+u9JUUfSppg7SpqFhSgMlvyU/FiGLVEHDchJEdGQIBEaOBgTB/MA8G"
62             + "A1UdEwEB/wQFMAMBAQAwDwYDVR0PAQH/BAUDAwegADAdBgNVHQ4EFgQUyxKILxFM"
63             + "MNujjNnbeFpnPgB76UYwHwYDVR0jBBgwFoAUy5k/bKQ6TtpTWhsPWFzafOFgLmsw"
64             + "GwYDVR0RBBQwEoEQMjUxMzQ3QGFuYXRvbS5zZTANBgkqhkiG9w0BAQUFAAOBgQAS"
65             + "5wSOJhoVJSaEGHMPw6t3e+CbnEL9Yh5GlgxVAJCmIqhoScTMiov3QpDRHOZlZ15c"
66             + "UlqugRBtORuA9xnLkrdxYNCHmX6aJTfjdIW61+o/ovP0yz6ulBkqcKzopAZLirX+"
67             + "XSWf2uI9miNtxYMVnbQ1KPdEAt7Za3OQR6zcS0lGKg==").getBytes());
68
69
70     /**
71      * Creates a new TestHardToken object.
72      *
73      * @param name name
74      */

75     public TestHardToken(String JavaDoc name) {
76         super(name);
77     }
78
79     protected void setUp() throws Exception JavaDoc {
80
81         log.debug(">setUp()");
82         CertTools.installBCProvider();
83         if (cacheAdmin == null) {
84             if (cacheHome == null) {
85                 Context JavaDoc jndiContext = getInitialContext();
86                 Object JavaDoc obj1 = jndiContext.lookup(IHardTokenSessionHome.JNDI_NAME);
87                 cacheHome = (IHardTokenSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, IHardTokenSessionHome.class);
88
89             }
90
91             cacheAdmin = cacheHome.create();
92         }
93         if (certStore == null) {
94             if (storeHome == null) {
95                 Context JavaDoc jndiContext = getInitialContext();
96                 Object JavaDoc obj1 = jndiContext.lookup(ICertificateStoreSessionHome.JNDI_NAME);
97                 storeHome = (ICertificateStoreSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, ICertificateStoreSessionHome.class);
98             }
99
100             certStore = storeHome.create();
101         }
102
103
104         log.debug("<setUp()");
105     }
106
107     protected void tearDown() throws Exception JavaDoc {
108     }
109
110     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
111         log.debug(">getInitialContext");
112
113         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
114         log.debug("<getInitialContext");
115
116         return ctx;
117     }
118
119
120     /**
121      * adds a token to the database
122      *
123      * @throws Exception error
124      */

125     public void test01AddHardToken() throws Exception JavaDoc {
126         log.debug(">test01AddHardToken()");
127         boolean ret = false;
128         try {
129             SwedishEIDHardToken token = new SwedishEIDHardToken("1234", "1234", "123456", "123456", 1);
130
131             ArrayList JavaDoc certs = new ArrayList JavaDoc();
132
133             certs.add(CertTools.getCertfromByteArray(testcert));
134
135             cacheAdmin.addHardToken(admin, "1234", "TESTUSER", "CN=TEST", SecConst.TOKEN_SWEDISHEID, token, certs, null);
136             
137             TurkishEIDHardToken token2 = new TurkishEIDHardToken("1234", "123456", 1);
138
139             cacheAdmin.addHardToken(admin, "2345", "TESTUSER", "CN=TEST", SecConst.TOKEN_TURKISHEID, token2, certs, null);
140
141             ret = true;
142         } catch (HardTokenExistsException pee) {
143         }
144
145         assertTrue("Creating End Entity Profile failed", ret);
146         log.debug("<test01AddHardToken()");
147     }
148
149
150     /**
151      * edits token
152      *
153      * @throws Exception error
154      */

155     public void test02EditHardToken() throws Exception JavaDoc {
156         log.debug(">test02EditHardToken()");
157
158         boolean ret = false;
159
160         HardTokenData token = cacheAdmin.getHardToken(admin, "1234");
161
162         SwedishEIDHardToken swe = (SwedishEIDHardToken) token.getHardToken();
163
164         assertTrue("Retrieving HardToken failed", swe.getInitialAuthEncPIN().equals("1234"));
165
166         swe.setInitialAuthEncPIN("5678");
167
168         cacheAdmin.changeHardToken(admin, "1234", SecConst.TOKEN_SWEDISHEID, token.getHardToken());
169         ret = true;
170
171         assertTrue("Editing HardToken failed", ret);
172
173
174         log.debug("<test02EditHardToken()");
175     }
176
177     /**
178      * Test that tries to find a hardtokensn from is certificate
179      *
180      * @throws Exception error
181      */

182     public void test03FindHardTokenByCertificate() throws Exception JavaDoc {
183         log.debug(">test03FindHardTokenByCertificate()");
184
185         X509Certificate JavaDoc cert = CertTools.getCertfromByteArray(testcert);
186         // Store the dummy cert for test.
187
if(certStore.findCertificateByFingerprint(admin, CertTools.getFingerprintAsString(cert)) == null){
188           certStore.storeCertificate(admin,cert,"DUMMYUSER", CertTools.getFingerprintAsString(cert),CertificateDataBean.CERT_ACTIVE,CertificateDataBean.CERTTYPE_ENDENTITY);
189         }
190         String JavaDoc tokensn = cacheAdmin.findHardTokenByCertificateSNIssuerDN(admin, cert.getSerialNumber(), cert.getIssuerDN().toString());
191
192         assertTrue("Couldn't find right hardtokensn", tokensn.equals("1234"));
193
194         log.debug("<test03FindHardTokenByCertificate()");
195     }
196     
197     /**
198      * removes all profiles
199      *
200      * @throws Exception error
201      */

202     public void test04removeHardTokens() throws Exception JavaDoc {
203         log.debug(">test04removeHardTokens()");
204         boolean ret = false;
205         try {
206             cacheAdmin.removeHardToken(admin, "1234");
207             cacheAdmin.removeHardToken(admin, "2345");
208
209             ret = true;
210         } catch (Exception JavaDoc pee) {
211         }
212         assertTrue("Removing Hard Token failed", ret);
213
214         log.debug("<test04removeHardTokens()");
215     }
216
217
218 }
219
Popular Tags