KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Arrays 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.hardtoken.IHardTokenSessionHome;
25 import org.ejbca.core.ejb.hardtoken.IHardTokenSessionRemote;
26 import org.ejbca.core.model.hardtoken.HardTokenProfileExistsException;
27 import org.ejbca.core.model.hardtoken.profiles.EnhancedEIDProfile;
28 import org.ejbca.core.model.hardtoken.profiles.HardTokenProfile;
29 import org.ejbca.core.model.hardtoken.profiles.SwedishEIDProfile;
30 import org.ejbca.core.model.hardtoken.profiles.TurkishEIDProfile;
31 import org.ejbca.core.model.log.Admin;
32 import org.ejbca.util.CertTools;
33
34
35 /**
36  * Tests the hard token profile entity bean.
37  *
38  * @version $Id: TestHardTokenProfile.java,v 1.4 2006/11/14 08:42:08 anatom Exp $
39  */

40 public class TestHardTokenProfile extends TestCase {
41     private static Logger log = Logger.getLogger(TestHardTokenProfile.class);
42     private IHardTokenSessionRemote cacheAdmin;
43
44     private static int SVGFILESIZE = 512 * 1024; // 1/2 Mega char
45

46
47     private static IHardTokenSessionHome cacheHome;
48
49     private static final Admin admin = new Admin(Admin.TYPE_INTERNALUSER);
50
51     /**
52      * Creates a new TestHardTokenProfile object.
53      *
54      * @param name name
55      */

56     public TestHardTokenProfile(String JavaDoc name) {
57         super(name);
58     }
59
60     protected void setUp() throws Exception JavaDoc {
61
62         log.debug(">setUp()");
63         CertTools.installBCProvider();
64
65         if (cacheAdmin == null) {
66             if (cacheHome == null) {
67                 Context JavaDoc jndiContext = getInitialContext();
68                 Object JavaDoc obj1 = jndiContext.lookup("HardTokenSession");
69                 cacheHome = (IHardTokenSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, IHardTokenSessionHome.class);
70
71             }
72
73             cacheAdmin = cacheHome.create();
74         }
75
76
77         log.debug("<setUp()");
78     }
79
80     protected void tearDown() throws Exception JavaDoc {
81     }
82
83     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
84         log.debug(">getInitialContext");
85
86         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
87         log.debug("<getInitialContext");
88
89         return ctx;
90     }
91
92
93     /**
94      * adds a profile to the database
95      *
96      * @throws Exception error
97      */

98     public void test01AddHardTokenProfile() throws Exception JavaDoc {
99         log.debug(">test01AddHardTokenProfile()");
100         boolean ret = false;
101         try {
102             SwedishEIDProfile profile = new SwedishEIDProfile();
103             EnhancedEIDProfile profile2 = new EnhancedEIDProfile();
104             TurkishEIDProfile turprofile = new TurkishEIDProfile();
105
106
107             String JavaDoc svgdata = createSVGData();
108             profile.setPINEnvelopeData(svgdata);
109             profile2.setIsKeyRecoverable(EnhancedEIDProfile.CERTUSAGE_ENC, true);
110
111
112             cacheAdmin.addHardTokenProfile(admin, "SWETEST", profile);
113             cacheAdmin.addHardTokenProfile(admin, "ENHTEST", profile2);
114             cacheAdmin.addHardTokenProfile(admin, "TURTEST", turprofile);
115
116             SwedishEIDProfile profile3 = (SwedishEIDProfile) cacheAdmin.getHardTokenProfile(admin, "SWETEST");
117             EnhancedEIDProfile profile4 = (EnhancedEIDProfile) cacheAdmin.getHardTokenProfile(admin, "ENHTEST");
118             TurkishEIDProfile turprofile2 = (TurkishEIDProfile) cacheAdmin.getHardTokenProfile(admin, "TURTEST");
119
120             String JavaDoc svgdata2 = profile3.getPINEnvelopeData();
121
122             assertTrue("Saving SVG Data failed", svgdata.equals(svgdata2));
123             assertTrue("Saving Hard Token Profile failed", profile4.getIsKeyRecoverable(EnhancedEIDProfile.CERTUSAGE_ENC));
124             assertTrue("Saving Turkish Hard Token Profile failed", (turprofile2 != null));
125
126             ret = true;
127         } catch (HardTokenProfileExistsException pee) {
128         }
129
130         assertTrue("Creating Hard Token Profile failed", ret);
131         log.debug("<test01AddHardTokenProfile()");
132     }
133
134     /**
135      * renames profile
136      *
137      * @throws Exception error
138      */

139     public void test02RenameHardTokenProfile() throws Exception JavaDoc {
140         log.debug(">test02RenameHardTokenProfile()");
141
142         boolean ret = false;
143         try {
144             cacheAdmin.renameHardTokenProfile(admin, "SWETEST", "SWETEST2");
145             ret = true;
146         } catch (HardTokenProfileExistsException pee) {
147         }
148         assertTrue("Renaming Hard Token Profile failed", ret);
149
150         log.debug("<test02RenameHardTokenProfile()");
151     }
152
153     /**
154      * clones profile
155      *
156      * @throws Exception error
157      */

158     public void test03CloneHardTokenProfile() throws Exception JavaDoc {
159         log.debug(">test03CloneHardTokenProfile()");
160
161         boolean ret = false;
162         try {
163             cacheAdmin.cloneHardTokenProfile(admin, "SWETEST2", "SWETEST");
164             ret = true;
165         } catch (HardTokenProfileExistsException pee) {
166         }
167         assertTrue("Cloning Hard Token Profile failed", ret);
168
169         log.debug("<test03CloneHardTokenProfile()");
170     }
171
172
173     /**
174      * edits profile
175      *
176      * @throws Exception error
177      */

178     public void test04EditHardTokenProfile() throws Exception JavaDoc {
179         log.debug(">test04EditHardTokenProfile()");
180
181         boolean ret = false;
182
183         HardTokenProfile profile = cacheAdmin.getHardTokenProfile(admin, "ENHTEST");
184
185
186         profile.setHardTokenSNPrefix("11111");
187
188         cacheAdmin.changeHardTokenProfile(admin, "ENHTEST", profile);
189         ret = true;
190
191         assertTrue("Editing HardTokenProfile failed", ret);
192
193
194         log.debug("<test04EditHardTokenProfile()");
195     }
196
197
198     /**
199      * removes all profiles
200      *
201      * @throws Exception error
202      */

203     public void test05removeHardTokenProfiles() throws Exception JavaDoc {
204         log.debug(">test05removeHardTokenProfiles()");
205         boolean ret = false;
206         try {
207             // Remove all except ENHTEST
208
cacheAdmin.removeHardTokenProfile(admin, "SWETEST");
209             cacheAdmin.removeHardTokenProfile(admin, "SWETEST2");
210             cacheAdmin.removeHardTokenProfile(admin, "ENHTEST");
211             cacheAdmin.removeHardTokenProfile(admin, "TURTEST");
212             ret = true;
213         } catch (Exception JavaDoc pee) {
214         }
215         assertTrue("Removing Hard Token Profile failed", ret);
216
217         log.debug("<test05removeHardTokenProfiles()");
218     }
219
220     private String JavaDoc createSVGData() {
221         char[] chararray = new char[SVGFILESIZE];
222         Arrays.fill(chararray, 'a');
223
224         return new String JavaDoc(chararray);
225     }
226
227
228 }
229
Popular Tags