2698d9ca805df716c52f1d8a2a35c1d79249bee0
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / common / CredentialsTests.java
1 /*
2  * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation for Advanced Internet Development, Inc.
3  * All rights reserved
4  * 
5  * 
6  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 
9  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
10  * disclaimer.
11  * 
12  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials provided with the distribution, if any, must include the
14  * following acknowledgment: "This product includes software developed by the University Corporation for Advanced
15  * Internet Development <http://www.ucaid.edu> Internet2 Project. Alternately, this acknowledegement may appear in the
16  * software itself, if and wherever such third-party acknowledgments normally appear.
17  * 
18  * Neither the name of Shibboleth nor the names of its contributors, nor Internet2, nor the University Corporation for
19  * Advanced Internet Development, Inc., nor UCAID may be used to endorse or promote products derived from this software
20  * without specific prior written permission. For written permission, please contact shibboleth@shibboleth.org
21  * 
22  * Products derived from this software may not be called Shibboleth, Internet2, UCAID, or the University Corporation
23  * for Advanced Internet Development, nor may Shibboleth appear in their name, without prior written permission of the
24  * University Corporation for Advanced Internet Development.
25  * 
26  * 
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND WITH ALL FAULTS. ANY EXPRESS OR
28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
29  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK OF SATISFACTORY QUALITY, PERFORMANCE,
30  * ACCURACY, AND EFFORT IS WITH LICENSEE. IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
31  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 package edu.internet2.middleware.shibboleth.common;
39
40 import java.io.FileInputStream;
41 import java.io.FileNotFoundException;
42 import java.io.InputStream;
43
44 import junit.framework.TestCase;
45
46 import org.apache.log4j.BasicConfigurator;
47 import org.apache.log4j.Level;
48 import org.apache.log4j.Logger;
49 import org.apache.xerces.parsers.DOMParser;
50 import org.xml.sax.EntityResolver;
51 import org.xml.sax.ErrorHandler;
52 import org.xml.sax.InputSource;
53 import org.xml.sax.SAXException;
54 import org.xml.sax.SAXParseException;
55
56 /**
57  * Validation suite for the <code>Credentials</code> interface.
58  * 
59  * @author Walter Hoehn
60  */
61
62 public class CredentialsTests extends TestCase {
63
64         private DOMParser parser = new DOMParser();
65
66         public CredentialsTests(String name) {
67                 super(name);
68                 BasicConfigurator.resetConfiguration();
69                 BasicConfigurator.configure();
70                 Logger.getRootLogger().setLevel(Level.OFF);
71         }
72
73         public static void main(String[] args) {
74                 junit.textui.TestRunner.run(CredentialsTests.class);
75                 BasicConfigurator.configure();
76                 Logger.getRootLogger().setLevel(Level.OFF);
77         }
78
79         /**
80          * @see junit.framework.TestCase#setUp()
81          */
82         protected void setUp() throws Exception {
83                 super.setUp();
84                 try {
85                         parser.setFeature("http://xml.org/sax/features/validation", true);
86                         parser.setFeature("http://apache.org/xml/features/validation/schema", true);
87                         parser.setEntityResolver(new EntityResolver() {
88                                 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
89
90                                         if (systemId.endsWith("credentials.xsd")) {
91                                                 InputStream stream;
92                                                 try {
93                                                         stream = new FileInputStream("src/schemas/credentials.xsd");
94                                                         if (stream != null) {
95                                                                 return new InputSource(stream);
96                                                         }
97                                                         throw new SAXException("Could not load entity: Null input stream");
98                                                 } catch (FileNotFoundException e) {
99                                                         throw new SAXException("Could not load entity: " + e);
100                                                 }
101                                         } else if (systemId.endsWith("xmldsig-core-schema.xsd")) {
102                                                 InputStream stream;
103                                                 try {
104                                                         stream = new FileInputStream("src/schemas/xmldsig-core-schema.xsd");
105                                                         if (stream != null) {
106                                                                 return new InputSource(stream);
107                                                         }
108                                                         throw new SAXException("Could not load entity: Null input stream");
109                                                 } catch (FileNotFoundException e) {
110                                                         throw new SAXException("Could not load entity: " + e);
111                                                 }
112                                         } else {
113                                                 return null;
114                                         }
115                                 }
116                         });
117
118                         parser.setErrorHandler(new ErrorHandler() {
119                                 public void error(SAXParseException arg0) throws SAXException {
120                                         throw new SAXException("Error parsing xml file: " + arg0);
121                                 }
122                                 public void fatalError(SAXParseException arg0) throws SAXException {
123                                         throw new SAXException("Error parsing xml file: " + arg0);
124                                 }
125                                 public void warning(SAXParseException arg0) throws SAXException {
126                                         throw new SAXException("Error parsing xml file: " + arg0);
127                                 }
128                         });
129                 } catch (Exception e) {
130                         fail("Failed to setup xml parser: " + e);
131                 }
132
133         }
134
135         public void testKeyStoreX509CompleteChain() {
136
137                 try {
138                         InputStream inStream = new FileInputStream("data/credentials1.xml");
139                         parser.parse(new InputSource(inStream));
140                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
141
142                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
143                         Credential credential = credentials.getCredential("test");
144
145                         assertTrue(
146                                 "Credential was loaded with an incorrect type.",
147                                 credential.getCredentialType() == Credential.RSA);
148                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
149                         assertEquals(
150                                 "Unexpected X509 certificate found.",
151                                 credential.getX509Certificate().getSubjectDN().getName(),
152                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
153                         assertEquals(
154                                 "Unexpected certificate chain length.",
155                                 new Integer(credential.getX509CertificateChain().length),
156                                 new Integer(3));
157                         assertEquals(
158                                 "Unexpected X509 certificate found.",
159                                 credential.getX509CertificateChain()[1].getSubjectDN().getName(),
160                                 "CN=HEPKI Server CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
161                         assertEquals(
162                                 "Unexpected X509 certificate found.",
163                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
164                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
165                 } catch (Exception e) {
166                         fail("Failed to load credentials: " + e);
167                 }
168         }
169
170         public void testFileX509EndOnly() {
171
172                 try {
173                         InputStream inStream = new FileInputStream("data/credentials16.xml");
174                         parser.parse(new InputSource(inStream));
175                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
176
177                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
178                         Credential credential = credentials.getCredential("test");
179
180                         assertTrue(
181                                 "Credential was loaded with an incorrect type.",
182                                 credential.getCredentialType() == Credential.RSA);
183                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
184                         assertEquals(
185                                 "Unexpected X509 certificate found.",
186                                 credential.getX509Certificate().getSubjectDN().getName(),
187                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
188                         assertEquals(
189                                 "Unexpected certificate chain length.",
190                                 new Integer(credential.getX509CertificateChain().length),
191                                 new Integer(1));
192                 } catch (Exception e) {
193                         fail("Failed to load credentials: " + e);
194                 }
195         }
196
197         public void testFileX509IncompleteChain() {
198
199                 try {
200                         InputStream inStream = new FileInputStream("data/credentials17.xml");
201                         parser.parse(new InputSource(inStream));
202                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
203
204                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
205                         Credential credential = credentials.getCredential("test");
206
207                         assertTrue(
208                                 "Credential was loaded with an incorrect type.",
209                                 credential.getCredentialType() == Credential.RSA);
210                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
211                         assertEquals(
212                                 "Unexpected X509 certificate found.",
213                                 credential.getX509Certificate().getSubjectDN().getName(),
214                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
215                         assertEquals(
216                                 "Unexpected certificate chain length.",
217                                 new Integer(credential.getX509CertificateChain().length),
218                                 new Integer(2));
219                         assertEquals(
220                                 "Unexpected X509 certificate found.",
221                                 credential.getX509CertificateChain()[1].getSubjectDN().getName(),
222                                 "CN=HEPKI Server CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
223                 } catch (Exception e) {
224                         fail("Failed to load credentials: " + e);
225                 }
226         }
227         
228         public void testFileX509RSANoCert() {
229
230                 try {
231                         InputStream inStream = new FileInputStream("data/credentials18.xml");
232                         parser.parse(new InputSource(inStream));
233                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
234
235                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
236                         Credential credential = credentials.getCredential("test");
237
238                         assertTrue(
239                                 "Credential was loaded with an incorrect type.",
240                                 credential.getCredentialType() == Credential.RSA);
241                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
242                         assertEquals(
243                                 "Unexpected X509 certificate found.",
244                                 credential.hasX509Certificate(),
245                                 false);
246                         assertEquals(
247                                 "Unexpected certificate chain length.",
248                                 new Integer(credential.getX509CertificateChain().length),
249                                 new Integer(0));
250                 } catch (Exception e) {
251                         fail("Failed to load credentials: " + e);
252                 }
253         }
254
255         public void testKeyStoreX509AliasDefaulting() {
256
257                 try {
258                         InputStream inStream = new FileInputStream("data/credentials3.xml");
259                         parser.parse(new InputSource(inStream));
260                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
261
262                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
263                         Credential credential = credentials.getCredential("test");
264
265                         assertTrue(
266                                 "Credential was loaded with an incorrect type.",
267                                 credential.getCredentialType() == Credential.RSA);
268                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
269                         assertEquals(
270                                 "Unexpected X509 certificate found.",
271                                 credential.getX509Certificate().getSubjectDN().getName(),
272                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
273                         assertEquals(
274                                 "Unexpected certificate chain length.",
275                                 new Integer(credential.getX509CertificateChain().length),
276                                 new Integer(3));
277                         assertEquals(
278                                 "Unexpected X509 certificate found.",
279                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
280                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
281                 } catch (Exception e) {
282                         fail("Failed to load credentials: " + e);
283                 }
284         }
285
286         public void testFileX509NoPassword() {
287
288                 try {
289                         InputStream inStream = new FileInputStream("data/credentials2.xml");
290                         parser.parse(new InputSource(inStream));
291                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
292
293                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
294                         Credential credential = credentials.getCredential("test");
295
296                         assertTrue(
297                                 "Credential was loaded with an incorrect type.",
298                                 credential.getCredentialType() == Credential.RSA);
299                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
300                         assertEquals(
301                                 "Unexpected X509 certificate found.",
302                                 credential.getX509Certificate().getSubjectDN().getName(),
303                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
304                         assertEquals(
305                                 "Unexpected certificate chain length.",
306                                 new Integer(credential.getX509CertificateChain().length),
307                                 new Integer(3));
308                         assertEquals(
309                                 "Unexpected X509 certificate found.",
310                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
311                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
312                 } catch (Exception e) {
313                         fail("Failed to load credentials: " + e);
314                 }
315         }
316
317         public void testFileX509withCABundles() {
318
319                 try {
320                         InputStream inStream = new FileInputStream("data/credentials4.xml");
321                         parser.parse(new InputSource(inStream));
322                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
323
324                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
325                         Credential credential = credentials.getCredential("test");
326
327                         assertTrue(
328                                 "Credential was loaded with an incorrect type.",
329                                 credential.getCredentialType() == Credential.RSA);
330                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
331                         assertEquals(
332                                 "Unexpected X509 certificate found.",
333                                 credential.getX509Certificate().getSubjectDN().getName(),
334                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
335                         assertEquals(
336                                 "Unexpected certificate chain length.",
337                                 new Integer(credential.getX509CertificateChain().length),
338                                 new Integer(3));
339                         assertEquals(
340                                 "Unexpected X509 certificate found.",
341                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
342                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
343                 } catch (Exception e) {
344                         fail("Failed to load credentials: " + e);
345                 }
346         }
347
348         public void testFileX509_PEM_PKCS8Key() {
349
350                 try {
351                         InputStream inStream = new FileInputStream("data/credentials5.xml");
352                         parser.parse(new InputSource(inStream));
353                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
354
355                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
356                         Credential credential = credentials.getCredential("test");
357
358                         assertTrue(
359                                 "Credential was loaded with an incorrect type.",
360                                 credential.getCredentialType() == Credential.RSA);
361                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
362                         assertEquals(
363                                 "Unexpected X509 certificate found.",
364                                 credential.getX509Certificate().getSubjectDN().getName(),
365                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
366                         assertEquals(
367                                 "Unexpected certificate chain length.",
368                                 new Integer(credential.getX509CertificateChain().length),
369                                 new Integer(3));
370                         assertEquals(
371                                 "Unexpected X509 certificate found.",
372                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
373                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
374                 } catch (Exception e) {
375                         fail("Failed to load credentials: " + e);
376                 }
377         }
378
379         public void testFileX509_DER_RSA_Key() {
380
381                 try {
382                         InputStream inStream = new FileInputStream("data/credentials6.xml");
383                         parser.parse(new InputSource(inStream));
384                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
385
386                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
387                         Credential credential = credentials.getCredential("test");
388
389                         assertTrue(
390                                 "Credential was loaded with an incorrect type.",
391                                 credential.getCredentialType() == Credential.RSA);
392                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
393                         assertEquals(
394                                 "Unexpected X509 certificate found.",
395                                 credential.getX509Certificate().getSubjectDN().getName(),
396                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
397                         assertEquals(
398                                 "Unexpected certificate chain length.",
399                                 new Integer(credential.getX509CertificateChain().length),
400                                 new Integer(3));
401                         assertEquals(
402                                 "Unexpected X509 certificate found.",
403                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
404                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
405                 } catch (Exception e) {
406                         fail("Failed to load credentials: " + e);
407                 }
408         }
409
410         public void testFileX509_PEM_RSA_Key() {
411
412                 try {
413                         InputStream inStream = new FileInputStream("data/credentials7.xml");
414                         parser.parse(new InputSource(inStream));
415                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
416
417                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
418                         Credential credential = credentials.getCredential("test");
419
420                         assertTrue(
421                                 "Credential was loaded with an incorrect type.",
422                                 credential.getCredentialType() == Credential.RSA);
423                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
424                         assertEquals(
425                                 "Unexpected X509 certificate found.",
426                                 credential.getX509Certificate().getSubjectDN().getName(),
427                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
428                         assertEquals(
429                                 "Unexpected certificate chain length.",
430                                 new Integer(credential.getX509CertificateChain().length),
431                                 new Integer(3));
432                         assertEquals(
433                                 "Unexpected X509 certificate found.",
434                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
435                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
436                 } catch (Exception e) {
437                         fail("Failed to load credentials: " + e);
438                 }
439         }
440
441         public void testFileX509_DER_DSA_Key() {
442
443                 try {
444                         InputStream inStream = new FileInputStream("data/credentials8.xml");
445                         parser.parse(new InputSource(inStream));
446                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
447
448                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
449                         Credential credential = credentials.getCredential("test");
450
451                         assertTrue(
452                                 "Credential was loaded with an incorrect type.",
453                                 credential.getCredentialType() == Credential.DSA);
454                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
455                         assertEquals(
456                                 "Unexpected X509 certificate found.",
457                                 credential.getX509Certificate().getSubjectDN().getName(),
458                                 "CN=test.columbia.edu, OU=ACIS, O=Columbia University, L=New York, ST=NY, C=US");
459                         assertEquals(
460                                 "Unexpected certificate chain length.",
461                                 new Integer(credential.getX509CertificateChain().length),
462                                 new Integer(1));
463                 } catch (Exception e) {
464                         fail("Failed to load credentials: " + e);
465                 }
466         }
467
468         public void testFileX509_PEM_DSA_Key() {
469
470                 try {
471                         InputStream inStream = new FileInputStream("data/credentials9.xml");
472                         parser.parse(new InputSource(inStream));
473                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
474
475                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
476                         Credential credential = credentials.getCredential("test");
477
478                         assertTrue(
479                                 "Credential was loaded with an incorrect type.",
480                                 credential.getCredentialType() == Credential.DSA);
481                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
482                         assertEquals(
483                                 "Unexpected X509 certificate found.",
484                                 credential.getX509Certificate().getSubjectDN().getName(),
485                                 "CN=test.columbia.edu, OU=ACIS, O=Columbia University, L=New York, ST=NY, C=US");
486                         assertEquals(
487                                 "Unexpected certificate chain length.",
488                                 new Integer(credential.getX509CertificateChain().length),
489                                 new Integer(1));
490                 } catch (Exception e) {
491                         fail("Failed to load credentials: " + e);
492                 }
493         }
494
495         public void testFileX509_PEM_PKCS8_DSA_Key() {
496
497                 try {
498                         InputStream inStream = new FileInputStream("data/credentials10.xml");
499                         parser.parse(new InputSource(inStream));
500                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
501
502                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
503                         Credential credential = credentials.getCredential("test");
504
505                         assertTrue(
506                                 "Credential was loaded with an incorrect type.",
507                                 credential.getCredentialType() == Credential.DSA);
508                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
509                         assertEquals(
510                                 "Unexpected X509 certificate found.",
511                                 credential.getX509Certificate().getSubjectDN().getName(),
512                                 "CN=test.columbia.edu, OU=ACIS, O=Columbia University, L=New York, ST=NY, C=US");
513                         assertEquals(
514                                 "Unexpected certificate chain length.",
515                                 new Integer(credential.getX509CertificateChain().length),
516                                 new Integer(1));
517                 } catch (Exception e) {
518                         fail("Failed to load credentials: " + e);
519                 }
520         }
521
522         public void testFileX509_DER_PKCS8_Encrypted_RSA_Key() {
523
524                 try {
525                         InputStream inStream = new FileInputStream("data/credentials11.xml");
526                         parser.parse(new InputSource(inStream));
527                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
528
529                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
530                         Credential credential = credentials.getCredential("test");
531
532                         assertTrue(
533                                 "Credential was loaded with an incorrect type.",
534                                 credential.getCredentialType() == Credential.RSA);
535                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
536                         assertEquals(
537                                 "Unexpected X509 certificate found.",
538                                 credential.getX509Certificate().getSubjectDN().getName(),
539                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
540                         assertEquals(
541                                 "Unexpected certificate chain length.",
542                                 new Integer(credential.getX509CertificateChain().length),
543                                 new Integer(3));
544                         assertEquals(
545                                 "Unexpected X509 certificate found.",
546                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
547                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
548                 } catch (Exception e) {
549                         fail("Failed to load credentials: " + e);
550                 }
551         }
552
553         public void testFileX509_PEM_PKCS8_Encrypted_RSA_Key() {
554
555                 try {
556                         InputStream inStream = new FileInputStream("data/credentials12.xml");
557                         parser.parse(new InputSource(inStream));
558                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
559
560                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
561                         Credential credential = credentials.getCredential("test");
562
563                         assertTrue(
564                                 "Credential was loaded with an incorrect type.",
565                                 credential.getCredentialType() == Credential.RSA);
566                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
567                         assertEquals(
568                                 "Unexpected X509 certificate found.",
569                                 credential.getX509Certificate().getSubjectDN().getName(),
570                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
571                         assertEquals(
572                                 "Unexpected certificate chain length.",
573                                 new Integer(credential.getX509CertificateChain().length),
574                                 new Integer(3));
575                         assertEquals(
576                                 "Unexpected X509 certificate found.",
577                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
578                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
579                 } catch (Exception e) {
580                         fail("Failed to load credentials: " + e);
581                 }
582         }
583
584         public void testFileX509_PEM_Encrypted_DES_RSA_Key() {
585
586                 try {
587                         InputStream inStream = new FileInputStream("data/credentials14.xml");
588                         parser.parse(new InputSource(inStream));
589                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
590
591                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
592                         Credential credential = credentials.getCredential("test");
593
594                         assertTrue(
595                                 "Credential was loaded with an incorrect type.",
596                                 credential.getCredentialType() == Credential.RSA);
597                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
598                         assertEquals(
599                                 "Unexpected X509 certificate found.",
600                                 credential.getX509Certificate().getSubjectDN().getName(),
601                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
602                         assertEquals(
603                                 "Unexpected certificate chain length.",
604                                 new Integer(credential.getX509CertificateChain().length),
605                                 new Integer(3));
606                         assertEquals(
607                                 "Unexpected X509 certificate found.",
608                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
609                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
610                 } catch (Exception e) {
611                         fail("Failed to load credentials: " + e);
612                 }
613         }
614
615         public void testFileX509_PEM_Encrypted_TripeDES_RSA_Key() {
616
617                 try {
618                         InputStream inStream = new FileInputStream("data/credentials13.xml");
619                         parser.parse(new InputSource(inStream));
620                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
621
622                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
623                         Credential credential = credentials.getCredential("test");
624
625                         assertTrue(
626                                 "Credential was loaded with an incorrect type.",
627                                 credential.getCredentialType() == Credential.RSA);
628                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
629                         assertEquals(
630                                 "Unexpected X509 certificate found.",
631                                 credential.getX509Certificate().getSubjectDN().getName(),
632                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
633                         assertEquals(
634                                 "Unexpected certificate chain length.",
635                                 new Integer(credential.getX509CertificateChain().length),
636                                 new Integer(3));
637                         assertEquals(
638                                 "Unexpected X509 certificate found.",
639                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
640                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
641                 } catch (Exception e) {
642                         fail("Failed to load credentials: " + e);
643                 }
644         }
645
646         public void testFileX509_PEM_Encrypted_TripeDES_DSA_Key() {
647
648                 try {
649                         InputStream inStream = new FileInputStream("data/credentials15.xml");
650                         parser.parse(new InputSource(inStream));
651                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
652
653                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
654                         Credential credential = credentials.getCredential("test");
655
656                         assertTrue(
657                                 "Credential was loaded with an incorrect type.",
658                                 credential.getCredentialType() == Credential.DSA);
659                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
660                         assertEquals(
661                                 "Unexpected X509 certificate found.",
662                                 credential.getX509Certificate().getSubjectDN().getName(),
663                                 "CN=test.columbia.edu, OU=ACIS, O=Columbia University, L=New York, ST=NY, C=US");
664                         assertEquals(
665                                 "Unexpected certificate chain length.",
666                                 new Integer(credential.getX509CertificateChain().length),
667                                 new Integer(1));
668                 } catch (Exception e) {
669                         fail("Failed to load credentials: " + e);
670                 }
671         }
672
673 }