1 /***
2 *
3 * Creates a CertStore with certificates from a flat file, cert.in.x.
4 * For a specified flag that identifies CertSelectors returns files, ldapcerts.out.x,
5 * with the result certificates returned from LDAPCertStore.
6 *
7 * Flags:
8 * 'a' -> AuthCertSelector,
9 * 's' -> SubjectCertSelector
10 * 'c' -> CompatibleCertSelector
11 * 'n' -> NameCertSelector
12 *
13 * @see LDAPLoader
14 *
15 * @author Lu�s Pedro
16 * @author Sean Radford
17 * @version $Revision: 1.3 $ $Date: 2004/03/27 19:18:14 $
18 */
19 package jsdsi.ldap;
20
21 import java.io.*;
22 import java.util.*;
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26 import jsdsi.*;
27
28 public class LDAPTest extends TestCase {
29
30 /***
31 * JVM property name to indicate to skip this test.
32 */
33 private static final String LDAP_SKIP_TESTS = "test.ldap.skiptests";
34
35 /***
36 * JVM property name for the LDAP server name
37 */
38 private static final String SERVER_NAME ="test.ldap.servername";
39
40 /***
41 * JVM property name for the LDAP server port
42 */
43 private static final String SERVER_PORT ="test.ldap.port";
44
45 /***
46 * JVM property name for the LDAP server base distinguished name
47 */
48 private static final String SERVER_BASEDN ="test.ldap.baseDN";
49
50 /***
51 * JVM property name for the LDAP server login
52 */
53 private static final String LOGIN ="test.ldap.login";
54
55 /***
56 * JVM property name for the LDAP server login password
57 */
58 private static final String PASSWORD ="test.ldap.password";
59
60 /***
61 * JVM property name for the jsdsi selector to use
62 */
63 private static final String SELECTOR ="test.ldap.selector";
64
65 LDAPParameters params;
66
67
68 public LDAPTest(String name, LDAPParameters params) {
69 super(name);
70 assert(params != null);
71 this.params = params;
72 }
73
74 public static Test suite() {
75 if (skipTest()) {
76 // skips the LDAP test (set using a JVM Property of LDAP_SKIP_TESTS )
77 return new NamedTestSuite("LDAPTest skipped");
78 }
79 LDAPParameters params = getLDAPParameters();
80
81 TestSuite s = new NamedTestSuite("LDAPTest");
82 String flag = getSelectorType();
83
84 s.addTest(ldapCertStore(params, flag));
85 return s;
86 }
87
88 private static Test ldapCertStore(LDAPParameters params, String flag) {
89 try {
90 TestSuite s = new TestSuite();
91 Provider.install();
92 LDAPOperations ldap = new LDAPOperations(params);
93 LDAPCertStoreParameters storeParams = new LDAPCertStoreParameters(params.getLDAPserver(), params.getLDAPport(), params.getLDAPbaseDN());
94 java.security.cert.CertStore store = java.security.cert.CertStore.getInstance("SPKI/LDAP", storeParams);
95 File cwd = new File("src/test/jsdsi");
96 String[] in = cwd.list(new FilenameFilter() {
97 public boolean accept(File dir, String name) {
98 return name.startsWith("certs.in")
99 && !name.endsWith("~");}});
100
101 for (int i = 0; i < in.length; i++) {
102 try {
103 ldap.deleteAllCertificates(); // delete previous stored certs
104 } catch(LDAPSearchException e) {
105 // used to save time and prevent thread deadlocks
106 // test will continue
107 }
108 finally {
109 String out = cwd + File.separator + "ldapcerts.out" + in[i].substring("certs.in".length());
110 LDAPLoader inLoad = new LDAPLoader(cwd + File.separator + in[i], params);
111 s.addTest(getLDAPCerts(store, inLoad, out, flag));
112 }
113 }
114 return s;
115 }catch(java.security.NoSuchAlgorithmException e) {
116 throw new Error(e);
117 }catch(java.security.InvalidAlgorithmParameterException e) {
118 throw new Error(e);
119 }catch(IOException e) {
120 throw new Error(e);
121 }
122 }
123
124 private static Test getLDAPCerts(java.security.cert.CertStore store,
125 LDAPLoader in, String out, String flag) {
126 try {
127 TestSuite s = new TestSuite();
128 List certsList = new ArrayList();
129 if(flag == "AuthCertSelector") {
130 Iterator auth = in.getKeys().iterator();
131 while(auth.hasNext()) {
132 AuthCertSelector auths = new AuthCertSelector((Principal)auth.next());
133 Iterator certs = store.getCertificates(auths).iterator();
134 while(certs.hasNext()) {
135 Certificate cert = (Certificate)certs.next();
136 if(cert.getCert() instanceof AuthCert)
137 certsList.add(cert);
138 }
139 }
140 }
141 if(flag == "NameCertSelector") {
142 Iterator name = in.getNames().iterator();
143 while(name.hasNext()) {
144 Name n = (Name)name.next();
145 NameCertSelector names = new NameCertSelector(n.getIssuer(),
146 n.getNames()[0]);
147 Iterator certs = store.getCertificates(names).iterator();
148 while(certs.hasNext())
149 certsList.add(certs.next());
150 }
151 }
152 if(flag == "SubjectCertSelector") {
153 Iterator subject = in.getKeys().iterator();
154 while(subject.hasNext()) {
155 SubjectCertSelector subjects = new SubjectCertSelector((Subject)subject.next());
156 Iterator certs = store.getCertificates(subjects).iterator();
157 while(certs.hasNext())
158 certsList.add(certs.next());
159 }
160 }
161 if(flag == "CompatibleCertSelector") {
162 Iterator compatible = in.getNames().iterator();
163 while(compatible.hasNext()) {
164 Name c = (Name)compatible.next();
165 CompatibleCertSelector compatibles = new CompatibleCertSelector(c.getIssuer(), c.getNames()[0]);
166 Iterator certs = store.getCertificates(compatibles).iterator();
167 while(certs.hasNext())
168 certsList.add(certs.next());
169 }
170 }
171 in.loaderOut(out, certsList);
172 return s;
173 }catch(java.security.cert.CertStoreException e) {
174 throw new Error(e);
175 }
176 }
177
178 private static LDAPParameters getLDAPParameters() {
179 String servername = getServerName();
180 if (servername==null || servername.equals("")) {
181 return new LDAPParameters( getServerBaseDN(),
182 getLogin(), getPassword());
183 } else {
184 String port = getServerPort();
185 if (port==null || port.equals("")) {
186 return new LDAPParameters( servername,
187 getServerBaseDN(),
188 getLogin(), getPassword());
189 } else {
190 int p = Integer.parseInt(port);
191 return new LDAPParameters( servername,
192 p,
193 getServerBaseDN(),
194 getLogin(), getPassword());
195 }
196 }
197 }
198
199 /***
200 * Informs whether to skip this LDAP test by examing the <code>LDAP_SKIP_TESTS</code> JVM property
201 * @return
202 */
203 private static boolean skipTest() {
204 return Boolean.getBoolean(LDAP_SKIP_TESTS);
205 }
206
207 /***
208 * Retrieves LDAP server name to connect to from the <code>SERVER_NAME</code> JVM property
209 * @return
210 */
211 private static String getServerName() {
212 return System.getProperty(SERVER_NAME);
213 }
214
215 /***
216 * Retrieves the LDAP server port to use from the <code>SERVER_PORT</code> JVM property
217 * @return
218 */
219 private static String getServerPort() {
220 return System.getProperty(SERVER_PORT);
221 }
222
223 /***
224 * Retrieves the LDAP server base distinguished name from the <code>SERVER_BASEDN</code> JVM property
225 * @return the base server disinguished name
226 */
227 private static String getServerBaseDN() {
228 String v = System.getProperty(SERVER_BASEDN);
229 if (v==null || v.equals("")) {
230 System.out.println("***** PLEASE SPECIFY A SERVER BASE DISTINGUISHED NAME ******");
231 System.out.println("***** SET IT USING JVM PROPERTY NAMED: "+SERVER_BASEDN+" *****");
232 System.out.println("");
233 throw new Error("No LDAP Server Base Distinguished Name specified");
234 } else {
235 return v;
236 }
237 }
238
239 /***
240 * Retrieves the LDAP login 'name' from the <code>LOGIN</code> JVM property
241 * @return
242 */
243 private static String getLogin() {
244 String v = System.getProperty(LOGIN);
245 if (v==null || v.equals("")) {
246 System.out.println("***** PLEASE SPECIFY A LOGIN NAME ******");
247 System.out.println("***** SET IT USING JVM PROPERTY NAMED: "+LOGIN+" *****");
248 System.out.println("");
249 throw new Error("No LDAP Server Login Name specified");
250 } else {
251 return v;
252 }
253 }
254
255 /***
256 * Retrieves the LDAP login password from the <code>PASSWORD</code> JVM property
257 * @return
258 */
259 private static String getPassword() {
260 String v = System.getProperty(PASSWORD);
261 if (v==null || v.equals("")) {
262 System.out.println("***** PLEASE SPECIFY A PASSWORD ******");
263 System.out.println("***** SET IT USING JVM PROPERTY NAMED: "+PASSWORD+" *****");
264 System.out.println("");
265 throw new Error("No LDAP Server Password specified");
266 } else {
267 return v;
268 }
269 }
270
271 /***
272 * Retrieves the jsdsi selector type to use from the <code>SELECTOR</code> JVM property
273 * @return
274 */
275 private static String getSelectorType() {
276 String v = System.getProperty(SELECTOR);
277 if (v==null || v.equals("")) {
278 System.out.println("***** PLEASE SPECIFY A SELECTOR TYPE ******");
279 System.out.println("***** SET IT USING JVM PROPERTY NAMED: "+SELECTOR+" *****");
280 System.out.println("");
281 throw new Error("No jsdsi.CertSelector specified");
282 } else {
283 return v;
284 }
285 }
286 }
This page was automatically generated by Maven