use the new session manager interface
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / common / CredentialsTests.java
1 /*
2  * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package edu.internet2.middleware.shibboleth.common;
18
19 import java.io.FileInputStream;
20 import java.io.InputStream;
21
22 import javax.xml.parsers.DocumentBuilderFactory;
23
24 import junit.framework.TestCase;
25
26 import org.apache.log4j.BasicConfigurator;
27 import org.apache.log4j.Level;
28 import org.apache.log4j.Logger;
29 import org.xml.sax.InputSource;
30
31 /**
32  * Validation suite for the <code>Credentials</code> interface.
33  * 
34  * @author Walter Hoehn
35  */
36
37 public class CredentialsTests extends TestCase {
38
39         public CredentialsTests(String name) {
40
41                 super(name);
42                 BasicConfigurator.resetConfiguration();
43                 BasicConfigurator.configure();
44                 Logger.getRootLogger().setLevel(Level.OFF);
45         }
46
47         public static void main(String[] args) {
48
49                 junit.textui.TestRunner.run(CredentialsTests.class);
50                 BasicConfigurator.configure();
51                 Logger.getRootLogger().setLevel(Level.OFF);
52         }
53
54         /**
55          * @see junit.framework.TestCase#setUp()
56          */
57         protected void setUp() throws Exception {
58
59                 super.setUp();
60         }
61
62         public void testKeyStoreX509CompleteChain() {
63
64                 try {
65                         InputStream inStream = new FileInputStream("data/credentials1.xml");
66                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
67                         factory.setValidating(false);
68                         factory.setNamespaceAware(true);
69
70                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
71                                         .getDocumentElement());
72
73                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
74                         Credential credential = credentials.getCredential("test");
75
76                         assertTrue("Credential was loaded with an incorrect type.",
77                                         credential.getCredentialType() == Credential.RSA);
78                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
79                         assertEquals("Unexpected X509 certificate found.",
80                                         credential.getX509Certificate().getSubjectDN().getName(),
81                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
82                         assertEquals("Unexpected certificate chain length.", new Integer(
83                                         credential.getX509CertificateChain().length), new Integer(3));
84                         assertEquals(
85                                         "Unexpected X509 certificate found.",
86                                         credential.getX509CertificateChain()[1].getSubjectDN().getName(),
87                                         "CN=HEPKI Server CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
88                         assertEquals(
89                                         "Unexpected X509 certificate found.",
90                                         credential.getX509CertificateChain()[2].getSubjectDN().getName(),
91                                         "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
92                 } catch (Exception e) {
93                         fail("Failed to load credentials: " + e);
94                 }
95         }
96
97         public void testFileX509EndOnly() {
98
99                 try {
100                         InputStream inStream = new FileInputStream("data/credentials16.xml");
101                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
102                         factory.setValidating(false);
103                         factory.setNamespaceAware(true);
104
105                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
106                                         .getDocumentElement());
107                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
108                         Credential credential = credentials.getCredential("test");
109
110                         assertTrue("Credential was loaded with an incorrect type.",
111                                         credential.getCredentialType() == Credential.RSA);
112                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
113                         assertEquals("Unexpected X509 certificate found.",
114                                         credential.getX509Certificate().getSubjectDN().getName(),
115                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
116                         assertEquals("Unexpected certificate chain length.", new Integer(
117                                         credential.getX509CertificateChain().length), new Integer(1));
118                 } catch (Exception e) {
119                         fail("Failed to load credentials: " + e);
120                 }
121         }
122
123         public void testFileX509IncompleteChain() {
124
125                 try {
126                         InputStream inStream = new FileInputStream("data/credentials17.xml");
127                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
128                         factory.setValidating(false);
129                         factory.setNamespaceAware(true);
130
131                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
132                                         .getDocumentElement());
133
134                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
135                         Credential credential = credentials.getCredential("test");
136
137                         assertTrue("Credential was loaded with an incorrect type.",
138                                         credential.getCredentialType() == Credential.RSA);
139                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
140                         assertEquals("Unexpected X509 certificate found.",
141                                         credential.getX509Certificate().getSubjectDN().getName(),
142                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
143                         assertEquals("Unexpected certificate chain length.", new Integer(
144                                         credential.getX509CertificateChain().length), new Integer(2));
145                         assertEquals(
146                                         "Unexpected X509 certificate found.",
147                                         credential.getX509CertificateChain()[1].getSubjectDN().getName(),
148                                         "CN=HEPKI Server CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
149                 } catch (Exception e) {
150                         fail("Failed to load credentials: " + e);
151                 }
152         }
153
154         public void testFileX509RSANoCert() {
155
156                 try {
157                         InputStream inStream = new FileInputStream("data/credentials18.xml");
158                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
159                         factory.setValidating(false);
160                         factory.setNamespaceAware(true);
161
162                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
163                                         .getDocumentElement());
164
165                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
166                         Credential credential = credentials.getCredential("test");
167
168                         assertTrue("Credential was loaded with an incorrect type.",
169                                         credential.getCredentialType() == Credential.RSA);
170                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
171                         assertEquals("Unexpected X509 certificate found.", credential.hasX509Certificate(), false);
172                         assertEquals("Unexpected certificate chain length.", new Integer(
173                                         credential.getX509CertificateChain().length), new Integer(0));
174                 } catch (Exception e) {
175                         fail("Failed to load credentials: " + e);
176                 }
177         }
178
179         public void testKeyStoreX509AliasDefaulting() {
180
181                 try {
182                         InputStream inStream = new FileInputStream("data/credentials3.xml");
183                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
184                         factory.setValidating(false);
185                         factory.setNamespaceAware(true);
186
187                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
188                                         .getDocumentElement());
189
190                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
191                         Credential credential = credentials.getCredential("test");
192
193                         assertTrue("Credential was loaded with an incorrect type.",
194                                         credential.getCredentialType() == Credential.RSA);
195                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
196                         assertEquals("Unexpected X509 certificate found.",
197                                         credential.getX509Certificate().getSubjectDN().getName(),
198                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
199                         assertEquals("Unexpected certificate chain length.", new Integer(
200                                         credential.getX509CertificateChain().length), new Integer(3));
201                         assertEquals(
202                                         "Unexpected X509 certificate found.",
203                                         credential.getX509CertificateChain()[2].getSubjectDN().getName(),
204                                         "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
205                 } catch (Exception e) {
206                         fail("Failed to load credentials: " + e);
207                 }
208         }
209
210         public void testFileX509NoPassword() {
211
212                 try {
213                         InputStream inStream = new FileInputStream("data/credentials2.xml");
214                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
215                         factory.setValidating(false);
216                         factory.setNamespaceAware(true);
217
218                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
219                                         .getDocumentElement());
220
221                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
222                         Credential credential = credentials.getCredential("test");
223
224                         assertTrue("Credential was loaded with an incorrect type.",
225                                         credential.getCredentialType() == Credential.RSA);
226                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
227                         assertEquals("Unexpected X509 certificate found.",
228                                         credential.getX509Certificate().getSubjectDN().getName(),
229                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
230                         assertEquals("Unexpected certificate chain length.", new Integer(
231                                         credential.getX509CertificateChain().length), new Integer(3));
232                         assertEquals(
233                                         "Unexpected X509 certificate found.",
234                                         credential.getX509CertificateChain()[2].getSubjectDN().getName(),
235                                         "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
236                 } catch (Exception e) {
237                         fail("Failed to load credentials: " + e);
238                 }
239         }
240
241         public void testFileX509withCABundles() {
242
243                 try {
244                         InputStream inStream = new FileInputStream("data/credentials4.xml");
245                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
246                         factory.setValidating(false);
247                         factory.setNamespaceAware(true);
248
249                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
250                                         .getDocumentElement());
251
252                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
253                         Credential credential = credentials.getCredential("test");
254
255                         assertTrue("Credential was loaded with an incorrect type.",
256                                         credential.getCredentialType() == Credential.RSA);
257                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
258                         assertEquals("Unexpected X509 certificate found.",
259                                         credential.getX509Certificate().getSubjectDN().getName(),
260                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
261                         assertEquals("Unexpected certificate chain length.", new Integer(
262                                         credential.getX509CertificateChain().length), new Integer(3));
263                         assertEquals(
264                                         "Unexpected X509 certificate found.",
265                                         credential.getX509CertificateChain()[2].getSubjectDN().getName(),
266                                         "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
267                 } catch (Exception e) {
268                         fail("Failed to load credentials: " + e);
269                 }
270         }
271
272         public void testFileX509_PEM_PKCS8Key() {
273
274                 try {
275                         InputStream inStream = new FileInputStream("data/credentials5.xml");
276                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
277                         factory.setValidating(false);
278                         factory.setNamespaceAware(true);
279
280                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
281                                         .getDocumentElement());
282                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
283                         Credential credential = credentials.getCredential("test");
284
285                         assertTrue("Credential was loaded with an incorrect type.",
286                                         credential.getCredentialType() == Credential.RSA);
287                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
288                         assertEquals("Unexpected X509 certificate found.",
289                                         credential.getX509Certificate().getSubjectDN().getName(),
290                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
291                         assertEquals("Unexpected certificate chain length.", new Integer(
292                                         credential.getX509CertificateChain().length), new Integer(3));
293                         assertEquals(
294                                         "Unexpected X509 certificate found.",
295                                         credential.getX509CertificateChain()[2].getSubjectDN().getName(),
296                                         "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
297                 } catch (Exception e) {
298                         fail("Failed to load credentials: " + e);
299                 }
300         }
301
302         public void testFileX509_DER_RSA_Key() {
303
304                 try {
305                         InputStream inStream = new FileInputStream("data/credentials6.xml");
306                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
307                         factory.setValidating(false);
308                         factory.setNamespaceAware(true);
309
310                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
311                                         .getDocumentElement());
312
313                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
314                         Credential credential = credentials.getCredential("test");
315
316                         assertTrue("Credential was loaded with an incorrect type.",
317                                         credential.getCredentialType() == Credential.RSA);
318                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
319                         assertEquals("Unexpected X509 certificate found.",
320                                         credential.getX509Certificate().getSubjectDN().getName(),
321                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
322                         assertEquals("Unexpected certificate chain length.", new Integer(
323                                         credential.getX509CertificateChain().length), new Integer(3));
324                         assertEquals(
325                                         "Unexpected X509 certificate found.",
326                                         credential.getX509CertificateChain()[2].getSubjectDN().getName(),
327                                         "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
328                 } catch (Exception e) {
329                         fail("Failed to load credentials: " + e);
330                 }
331         }
332
333         public void testFileX509_PEM_RSA_Key() {
334
335                 try {
336                         InputStream inStream = new FileInputStream("data/credentials7.xml");
337                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
338                         factory.setValidating(false);
339                         factory.setNamespaceAware(true);
340
341                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
342                                         .getDocumentElement());
343
344                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
345                         Credential credential = credentials.getCredential("test");
346
347                         assertTrue("Credential was loaded with an incorrect type.",
348                                         credential.getCredentialType() == Credential.RSA);
349                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
350                         assertEquals("Unexpected X509 certificate found.",
351                                         credential.getX509Certificate().getSubjectDN().getName(),
352                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
353                         assertEquals("Unexpected certificate chain length.", new Integer(
354                                         credential.getX509CertificateChain().length), new Integer(3));
355                         assertEquals(
356                                         "Unexpected X509 certificate found.",
357                                         credential.getX509CertificateChain()[2].getSubjectDN().getName(),
358                                         "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
359                 } catch (Exception e) {
360                         fail("Failed to load credentials: " + e);
361                 }
362         }
363
364         public void testFileX509_DER_DSA_Key() {
365
366                 try {
367                         InputStream inStream = new FileInputStream("data/credentials8.xml");
368                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
369                         factory.setValidating(false);
370                         factory.setNamespaceAware(true);
371
372                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
373                                         .getDocumentElement());
374                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
375                         Credential credential = credentials.getCredential("test");
376
377                         assertTrue("Credential was loaded with an incorrect type.",
378                                         credential.getCredentialType() == Credential.DSA);
379                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
380                         assertEquals("Unexpected X509 certificate found.",
381                                         credential.getX509Certificate().getSubjectDN().getName(),
382                                         "CN=test.columbia.edu, OU=ACIS, O=Columbia University, L=New York, ST=NY, C=US");
383                         assertEquals("Unexpected certificate chain length.", new Integer(
384                                         credential.getX509CertificateChain().length), new Integer(1));
385                 } catch (Exception e) {
386                         fail("Failed to load credentials: " + e);
387                 }
388         }
389
390         public void testFileX509_PEM_DSA_Key() {
391
392                 try {
393                         InputStream inStream = new FileInputStream("data/credentials9.xml");
394                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
395                         factory.setValidating(false);
396                         factory.setNamespaceAware(true);
397
398                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
399                                         .getDocumentElement());
400
401                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
402                         Credential credential = credentials.getCredential("test");
403
404                         assertTrue("Credential was loaded with an incorrect type.",
405                                         credential.getCredentialType() == Credential.DSA);
406                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
407                         assertEquals("Unexpected X509 certificate found.",
408                                         credential.getX509Certificate().getSubjectDN().getName(),
409                                         "CN=test.columbia.edu, OU=ACIS, O=Columbia University, L=New York, ST=NY, C=US");
410                         assertEquals("Unexpected certificate chain length.", new Integer(
411                                         credential.getX509CertificateChain().length), new Integer(1));
412                 } catch (Exception e) {
413                         fail("Failed to load credentials: " + e);
414                 }
415         }
416
417         public void testFileX509_PEM_PKCS8_DSA_Key() {
418
419                 try {
420                         InputStream inStream = new FileInputStream("data/credentials10.xml");
421                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
422                         factory.setValidating(false);
423                         factory.setNamespaceAware(true);
424
425                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
426                                         .getDocumentElement());
427
428                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
429                         Credential credential = credentials.getCredential("test");
430
431                         assertTrue("Credential was loaded with an incorrect type.",
432                                         credential.getCredentialType() == Credential.DSA);
433                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
434                         assertEquals("Unexpected X509 certificate found.",
435                                         credential.getX509Certificate().getSubjectDN().getName(),
436                                         "CN=test.columbia.edu, OU=ACIS, O=Columbia University, L=New York, ST=NY, C=US");
437                         assertEquals("Unexpected certificate chain length.", new Integer(
438                                         credential.getX509CertificateChain().length), new Integer(1));
439                 } catch (Exception e) {
440                         fail("Failed to load credentials: " + e);
441                 }
442         }
443
444         public void testFileX509_DER_PKCS8_Encrypted_RSA_Key() {
445
446                 try {
447                         InputStream inStream = new FileInputStream("data/credentials11.xml");
448                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
449                         factory.setValidating(false);
450                         factory.setNamespaceAware(true);
451
452                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
453                                         .getDocumentElement());
454
455                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
456                         Credential credential = credentials.getCredential("test");
457
458                         assertTrue("Credential was loaded with an incorrect type.",
459                                         credential.getCredentialType() == Credential.RSA);
460                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
461                         assertEquals("Unexpected X509 certificate found.",
462                                         credential.getX509Certificate().getSubjectDN().getName(),
463                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
464                         assertEquals("Unexpected certificate chain length.", new Integer(
465                                         credential.getX509CertificateChain().length), new Integer(3));
466                         assertEquals(
467                                         "Unexpected X509 certificate found.",
468                                         credential.getX509CertificateChain()[2].getSubjectDN().getName(),
469                                         "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
470                 } catch (Exception e) {
471                         fail("Failed to load credentials: " + e);
472                 }
473         }
474
475         public void testFileX509_PEM_PKCS8_Encrypted_RSA_Key() {
476
477                 try {
478                         InputStream inStream = new FileInputStream("data/credentials12.xml");
479                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
480                         factory.setValidating(false);
481                         factory.setNamespaceAware(true);
482
483                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
484                                         .getDocumentElement());
485
486                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
487                         Credential credential = credentials.getCredential("test");
488
489                         assertTrue("Credential was loaded with an incorrect type.",
490                                         credential.getCredentialType() == Credential.RSA);
491                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
492                         assertEquals("Unexpected X509 certificate found.",
493                                         credential.getX509Certificate().getSubjectDN().getName(),
494                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
495                         assertEquals("Unexpected certificate chain length.", new Integer(
496                                         credential.getX509CertificateChain().length), new Integer(3));
497                         assertEquals(
498                                         "Unexpected X509 certificate found.",
499                                         credential.getX509CertificateChain()[2].getSubjectDN().getName(),
500                                         "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
501                 } catch (Exception e) {
502                         fail("Failed to load credentials: " + e);
503                 }
504         }
505
506         public void testFileX509_PEM_Encrypted_DES_RSA_Key() {
507
508                 try {
509                         InputStream inStream = new FileInputStream("data/credentials14.xml");
510                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
511                         factory.setValidating(false);
512                         factory.setNamespaceAware(true);
513
514                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
515                                         .getDocumentElement());
516
517                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
518                         Credential credential = credentials.getCredential("test");
519
520                         assertTrue("Credential was loaded with an incorrect type.",
521                                         credential.getCredentialType() == Credential.RSA);
522                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
523                         assertEquals("Unexpected X509 certificate found.",
524                                         credential.getX509Certificate().getSubjectDN().getName(),
525                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
526                         assertEquals("Unexpected certificate chain length.", new Integer(
527                                         credential.getX509CertificateChain().length), new Integer(3));
528                         assertEquals(
529                                         "Unexpected X509 certificate found.",
530                                         credential.getX509CertificateChain()[2].getSubjectDN().getName(),
531                                         "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
532                 } catch (Exception e) {
533                         fail("Failed to load credentials: " + e);
534                 }
535         }
536
537         public void testFileX509_PEM_Encrypted_TripeDES_RSA_Key() {
538
539                 try {
540                         InputStream inStream = new FileInputStream("data/credentials13.xml");
541                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
542                         factory.setValidating(false);
543                         factory.setNamespaceAware(true);
544
545                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
546                                         .getDocumentElement());
547
548                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
549                         Credential credential = credentials.getCredential("test");
550
551                         assertTrue("Credential was loaded with an incorrect type.",
552                                         credential.getCredentialType() == Credential.RSA);
553                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
554                         assertEquals("Unexpected X509 certificate found.",
555                                         credential.getX509Certificate().getSubjectDN().getName(),
556                                         "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
557                         assertEquals("Unexpected certificate chain length.", new Integer(
558                                         credential.getX509CertificateChain().length), new Integer(3));
559                         assertEquals(
560                                         "Unexpected X509 certificate found.",
561                                         credential.getX509CertificateChain()[2].getSubjectDN().getName(),
562                                         "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
563                 } catch (Exception e) {
564                         fail("Failed to load credentials: " + e);
565                 }
566         }
567
568         public void testFileX509_PEM_Encrypted_TripeDES_DSA_Key() {
569
570                 try {
571                         InputStream inStream = new FileInputStream("data/credentials15.xml");
572                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
573                         factory.setValidating(false);
574                         factory.setNamespaceAware(true);
575
576                         Credentials credentials = new Credentials(factory.newDocumentBuilder().parse(new InputSource(inStream))
577                                         .getDocumentElement());
578
579                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
580                         Credential credential = credentials.getCredential("test");
581
582                         assertTrue("Credential was loaded with an incorrect type.",
583                                         credential.getCredentialType() == Credential.DSA);
584                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
585                         assertEquals("Unexpected X509 certificate found.",
586                                         credential.getX509Certificate().getSubjectDN().getName(),
587                                         "CN=test.columbia.edu, OU=ACIS, O=Columbia University, L=New York, ST=NY, C=US");
588                         assertEquals("Unexpected certificate chain length.", new Integer(
589                                         credential.getX509CertificateChain().length), new Integer(1));
590                 } catch (Exception e) {
591                         fail("Failed to load credentials: " + e);
592                 }
593         }
594
595 }