001: /*************************************************************************
002: * *
003: * EJBCA: The OpenSource Certificate Authority *
004: * *
005: * This software is free software; you can redistribute it and/or *
006: * modify it under the terms of the GNU Lesser General Public *
007: * License as published by the Free Software Foundation; either *
008: * version 2.1 of the License, or any later version. *
009: * *
010: * See terms of license at gnu.org. *
011: * *
012: *************************************************************************/package se.anatom.ejbca.hardtoken;
013:
014: import java.util.Arrays;
015:
016: import javax.naming.Context;
017: import javax.naming.NamingException;
018:
019: import junit.framework.TestCase;
020:
021: import org.apache.log4j.Logger;
022: import org.ejbca.core.ejb.hardtoken.IHardTokenSessionHome;
023: import org.ejbca.core.ejb.hardtoken.IHardTokenSessionRemote;
024: import org.ejbca.core.model.hardtoken.HardTokenProfileExistsException;
025: import org.ejbca.core.model.hardtoken.profiles.EnhancedEIDProfile;
026: import org.ejbca.core.model.hardtoken.profiles.HardTokenProfile;
027: import org.ejbca.core.model.hardtoken.profiles.SwedishEIDProfile;
028: import org.ejbca.core.model.hardtoken.profiles.TurkishEIDProfile;
029: import org.ejbca.core.model.log.Admin;
030: import org.ejbca.util.CertTools;
031:
032: /**
033: * Tests the hard token profile entity bean.
034: *
035: * @version $Id: TestHardTokenProfile.java,v 1.4 2006/11/14 08:42:08 anatom Exp $
036: */
037: public class TestHardTokenProfile extends TestCase {
038: private static Logger log = Logger
039: .getLogger(TestHardTokenProfile.class);
040: private IHardTokenSessionRemote cacheAdmin;
041:
042: private static int SVGFILESIZE = 512 * 1024; // 1/2 Mega char
043:
044: private static IHardTokenSessionHome cacheHome;
045:
046: private static final Admin admin = new Admin(
047: Admin.TYPE_INTERNALUSER);
048:
049: /**
050: * Creates a new TestHardTokenProfile object.
051: *
052: * @param name name
053: */
054: public TestHardTokenProfile(String name) {
055: super (name);
056: }
057:
058: protected void setUp() throws Exception {
059:
060: log.debug(">setUp()");
061: CertTools.installBCProvider();
062:
063: if (cacheAdmin == null) {
064: if (cacheHome == null) {
065: Context jndiContext = getInitialContext();
066: Object obj1 = jndiContext.lookup("HardTokenSession");
067: cacheHome = (IHardTokenSessionHome) javax.rmi.PortableRemoteObject
068: .narrow(obj1, IHardTokenSessionHome.class);
069:
070: }
071:
072: cacheAdmin = cacheHome.create();
073: }
074:
075: log.debug("<setUp()");
076: }
077:
078: protected void tearDown() throws Exception {
079: }
080:
081: private Context getInitialContext() throws NamingException {
082: log.debug(">getInitialContext");
083:
084: Context ctx = new javax.naming.InitialContext();
085: log.debug("<getInitialContext");
086:
087: return ctx;
088: }
089:
090: /**
091: * adds a profile to the database
092: *
093: * @throws Exception error
094: */
095: public void test01AddHardTokenProfile() throws Exception {
096: log.debug(">test01AddHardTokenProfile()");
097: boolean ret = false;
098: try {
099: SwedishEIDProfile profile = new SwedishEIDProfile();
100: EnhancedEIDProfile profile2 = new EnhancedEIDProfile();
101: TurkishEIDProfile turprofile = new TurkishEIDProfile();
102:
103: String svgdata = createSVGData();
104: profile.setPINEnvelopeData(svgdata);
105: profile2.setIsKeyRecoverable(
106: EnhancedEIDProfile.CERTUSAGE_ENC, true);
107:
108: cacheAdmin.addHardTokenProfile(admin, "SWETEST", profile);
109: cacheAdmin.addHardTokenProfile(admin, "ENHTEST", profile2);
110: cacheAdmin
111: .addHardTokenProfile(admin, "TURTEST", turprofile);
112:
113: SwedishEIDProfile profile3 = (SwedishEIDProfile) cacheAdmin
114: .getHardTokenProfile(admin, "SWETEST");
115: EnhancedEIDProfile profile4 = (EnhancedEIDProfile) cacheAdmin
116: .getHardTokenProfile(admin, "ENHTEST");
117: TurkishEIDProfile turprofile2 = (TurkishEIDProfile) cacheAdmin
118: .getHardTokenProfile(admin, "TURTEST");
119:
120: String svgdata2 = profile3.getPINEnvelopeData();
121:
122: assertTrue("Saving SVG Data failed", svgdata
123: .equals(svgdata2));
124: assertTrue(
125: "Saving Hard Token Profile failed",
126: profile4
127: .getIsKeyRecoverable(EnhancedEIDProfile.CERTUSAGE_ENC));
128: assertTrue("Saving Turkish Hard Token Profile failed",
129: (turprofile2 != null));
130:
131: ret = true;
132: } catch (HardTokenProfileExistsException pee) {
133: }
134:
135: assertTrue("Creating Hard Token Profile failed", ret);
136: log.debug("<test01AddHardTokenProfile()");
137: }
138:
139: /**
140: * renames profile
141: *
142: * @throws Exception error
143: */
144: public void test02RenameHardTokenProfile() throws Exception {
145: log.debug(">test02RenameHardTokenProfile()");
146:
147: boolean ret = false;
148: try {
149: cacheAdmin.renameHardTokenProfile(admin, "SWETEST",
150: "SWETEST2");
151: ret = true;
152: } catch (HardTokenProfileExistsException pee) {
153: }
154: assertTrue("Renaming Hard Token Profile failed", ret);
155:
156: log.debug("<test02RenameHardTokenProfile()");
157: }
158:
159: /**
160: * clones profile
161: *
162: * @throws Exception error
163: */
164: public void test03CloneHardTokenProfile() throws Exception {
165: log.debug(">test03CloneHardTokenProfile()");
166:
167: boolean ret = false;
168: try {
169: cacheAdmin.cloneHardTokenProfile(admin, "SWETEST2",
170: "SWETEST");
171: ret = true;
172: } catch (HardTokenProfileExistsException pee) {
173: }
174: assertTrue("Cloning Hard Token Profile failed", ret);
175:
176: log.debug("<test03CloneHardTokenProfile()");
177: }
178:
179: /**
180: * edits profile
181: *
182: * @throws Exception error
183: */
184: public void test04EditHardTokenProfile() throws Exception {
185: log.debug(">test04EditHardTokenProfile()");
186:
187: boolean ret = false;
188:
189: HardTokenProfile profile = cacheAdmin.getHardTokenProfile(
190: admin, "ENHTEST");
191:
192: profile.setHardTokenSNPrefix("11111");
193:
194: cacheAdmin.changeHardTokenProfile(admin, "ENHTEST", profile);
195: ret = true;
196:
197: assertTrue("Editing HardTokenProfile failed", ret);
198:
199: log.debug("<test04EditHardTokenProfile()");
200: }
201:
202: /**
203: * removes all profiles
204: *
205: * @throws Exception error
206: */
207: public void test05removeHardTokenProfiles() throws Exception {
208: log.debug(">test05removeHardTokenProfiles()");
209: boolean ret = false;
210: try {
211: // Remove all except ENHTEST
212: cacheAdmin.removeHardTokenProfile(admin, "SWETEST");
213: cacheAdmin.removeHardTokenProfile(admin, "SWETEST2");
214: cacheAdmin.removeHardTokenProfile(admin, "ENHTEST");
215: cacheAdmin.removeHardTokenProfile(admin, "TURTEST");
216: ret = true;
217: } catch (Exception pee) {
218: }
219: assertTrue("Removing Hard Token Profile failed", ret);
220:
221: log.debug("<test05removeHardTokenProfiles()");
222: }
223:
224: private String createSVGData() {
225: char[] chararray = new char[SVGFILESIZE];
226: Arrays.fill(chararray, 'a');
227:
228: return new String(chararray);
229: }
230:
231: }
|