fb41181d38f5fb416c88672f539b277a7050437a
[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                 //TODO turn this off later
71                 Logger.getRootLogger().setLevel(Level.INFO);
72         }
73
74         public static void main(String[] args) {
75                 junit.textui.TestRunner.run(CredentialsTests.class);
76                 BasicConfigurator.configure();
77                 //TODO turn this off later
78                 Logger.getRootLogger().setLevel(Level.INFO);
79         }
80
81         /**
82          * @see junit.framework.TestCase#setUp()
83          */
84         protected void setUp() throws Exception {
85                 super.setUp();
86                 try {
87                         parser.setFeature("http://xml.org/sax/features/validation", true);
88                         parser.setFeature("http://apache.org/xml/features/validation/schema", true);
89                         parser.setEntityResolver(new EntityResolver() {
90                                 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
91
92                                         if (systemId.endsWith("credentials.xsd")) {
93                                                 InputStream stream;
94                                                 try {
95                                                         stream = new FileInputStream("src/schemas/credentials.xsd");
96                                                         if (stream != null) {
97                                                                 return new InputSource(stream);
98                                                         }
99                                                         throw new SAXException("Could not load entity: Null input stream");
100                                                 } catch (FileNotFoundException e) {
101                                                         throw new SAXException("Could not load entity: " + e);
102                                                 }
103                                         } else if (systemId.endsWith("xmldsig-core-schema.xsd")) {
104                                                 InputStream stream;
105                                                 try {
106                                                         stream = new FileInputStream("src/schemas/xmldsig-core-schema.xsd");
107                                                         if (stream != null) {
108                                                                 return new InputSource(stream);
109                                                         }
110                                                         throw new SAXException("Could not load entity: Null input stream");
111                                                 } catch (FileNotFoundException e) {
112                                                         throw new SAXException("Could not load entity: " + e);
113                                                 }
114                                         } else {
115                                                 return null;
116                                         }
117                                 }
118                         });
119
120                         parser.setErrorHandler(new ErrorHandler() {
121                                 public void error(SAXParseException arg0) throws SAXException {
122                                         throw new SAXException("Error parsing xml file: " + arg0);
123                                 }
124                                 public void fatalError(SAXParseException arg0) throws SAXException {
125                                         throw new SAXException("Error parsing xml file: " + arg0);
126                                 }
127                                 public void warning(SAXParseException arg0) throws SAXException {
128                                         throw new SAXException("Error parsing xml file: " + arg0);
129                                 }
130                         });
131                 } catch (Exception e) {
132                         fail("Failed to setup xml parser: " + e);
133                 }
134
135         }
136
137         public void testKeyStoreX509CompleteChain() {
138
139                 try {
140                         InputStream inStream = new FileInputStream("data/credentials1.xml");
141                         parser.parse(new InputSource(inStream));
142                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
143
144                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
145                         Credential credential = credentials.getCredential("test");
146
147                         assertTrue(
148                                 "Credential was loaded with an incorrect type.",
149                                 credential.getCredentialType() == Credential.X509);
150                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
151                         assertEquals(
152                                 "Unexpected X509 certificate found.",
153                                 credential.getX509Certificate().getSubjectDN().getName(),
154                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
155                         assertEquals(
156                                 "Unexpected certificate chain length.",
157                                 new Integer(credential.getX509CertificateChain().length),
158                                 new Integer(3));
159                         assertEquals(
160                                 "Unexpected X509 certificate found.",
161                                 credential.getX509CertificateChain()[1].getSubjectDN().getName(),
162                                 "CN=HEPKI Server CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
163                         assertEquals(
164                                 "Unexpected X509 certificate found.",
165                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
166                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
167                 } catch (Exception e) {
168                         fail("Failed to load credentials: " + e);
169                 }
170         }
171
172         public void testFileX509EndOnly() {
173
174                 try {
175                         InputStream inStream = new FileInputStream("data/credentials16.xml");
176                         parser.parse(new InputSource(inStream));
177                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
178
179                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
180                         Credential credential = credentials.getCredential("test");
181
182                         assertTrue(
183                                 "Credential was loaded with an incorrect type.",
184                                 credential.getCredentialType() == Credential.X509);
185                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
186                         assertEquals(
187                                 "Unexpected X509 certificate found.",
188                                 credential.getX509Certificate().getSubjectDN().getName(),
189                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
190                         assertEquals(
191                                 "Unexpected certificate chain length.",
192                                 new Integer(credential.getX509CertificateChain().length),
193                                 new Integer(1));
194                 } catch (Exception e) {
195                         fail("Failed to load credentials: " + e);
196                 }
197         }
198
199         public void testFileX509IncompleteChain() {
200
201                 try {
202                         InputStream inStream = new FileInputStream("data/credentials17.xml");
203                         parser.parse(new InputSource(inStream));
204                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
205
206                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
207                         Credential credential = credentials.getCredential("test");
208
209                         assertTrue(
210                                 "Credential was loaded with an incorrect type.",
211                                 credential.getCredentialType() == Credential.X509);
212                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
213                         assertEquals(
214                                 "Unexpected X509 certificate found.",
215                                 credential.getX509Certificate().getSubjectDN().getName(),
216                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
217                         assertEquals(
218                                 "Unexpected certificate chain length.",
219                                 new Integer(credential.getX509CertificateChain().length),
220                                 new Integer(2));
221                         assertEquals(
222                                 "Unexpected X509 certificate found.",
223                                 credential.getX509CertificateChain()[1].getSubjectDN().getName(),
224                                 "CN=HEPKI Server CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
225                 } catch (Exception e) {
226                         fail("Failed to load credentials: " + e);
227                 }
228         }
229         
230         public void testFileX509RSANoCert() {
231
232                 try {
233                         InputStream inStream = new FileInputStream("data/credentials18.xml");
234                         parser.parse(new InputSource(inStream));
235                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
236
237                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
238                         Credential credential = credentials.getCredential("test");
239
240                         assertTrue(
241                                 "Credential was loaded with an incorrect type.",
242                                 credential.getCredentialType() == Credential.X509);
243                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
244                         assertEquals(
245                                 "Unexpected X509 certificate found.",
246                                 credential.hasX509Certificate(),
247                                 false);
248                         assertEquals(
249                                 "Unexpected certificate chain length.",
250                                 new Integer(credential.getX509CertificateChain().length),
251                                 new Integer(0));
252                 } catch (Exception e) {
253                         fail("Failed to load credentials: " + e);
254                 }
255         }
256
257         public void testKeyStoreX509AliasDefaulting() {
258
259                 try {
260                         InputStream inStream = new FileInputStream("data/credentials3.xml");
261                         parser.parse(new InputSource(inStream));
262                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
263
264                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
265                         Credential credential = credentials.getCredential("test");
266
267                         assertTrue(
268                                 "Credential was loaded with an incorrect type.",
269                                 credential.getCredentialType() == Credential.X509);
270                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
271                         assertEquals(
272                                 "Unexpected X509 certificate found.",
273                                 credential.getX509Certificate().getSubjectDN().getName(),
274                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
275                         assertEquals(
276                                 "Unexpected certificate chain length.",
277                                 new Integer(credential.getX509CertificateChain().length),
278                                 new Integer(3));
279                         assertEquals(
280                                 "Unexpected X509 certificate found.",
281                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
282                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
283                 } catch (Exception e) {
284                         fail("Failed to load credentials: " + e);
285                 }
286         }
287
288         public void testFileX509NoPassword() {
289
290                 try {
291                         InputStream inStream = new FileInputStream("data/credentials2.xml");
292                         parser.parse(new InputSource(inStream));
293                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
294
295                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
296                         Credential credential = credentials.getCredential("test");
297
298                         assertTrue(
299                                 "Credential was loaded with an incorrect type.",
300                                 credential.getCredentialType() == Credential.X509);
301                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
302                         assertEquals(
303                                 "Unexpected X509 certificate found.",
304                                 credential.getX509Certificate().getSubjectDN().getName(),
305                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
306                         assertEquals(
307                                 "Unexpected certificate chain length.",
308                                 new Integer(credential.getX509CertificateChain().length),
309                                 new Integer(3));
310                         assertEquals(
311                                 "Unexpected X509 certificate found.",
312                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
313                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
314                 } catch (Exception e) {
315                         fail("Failed to load credentials: " + e);
316                 }
317         }
318
319         public void testFileX509withCABundles() {
320
321                 try {
322                         InputStream inStream = new FileInputStream("data/credentials4.xml");
323                         parser.parse(new InputSource(inStream));
324                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
325
326                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
327                         Credential credential = credentials.getCredential("test");
328
329                         assertTrue(
330                                 "Credential was loaded with an incorrect type.",
331                                 credential.getCredentialType() == Credential.X509);
332                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
333                         assertEquals(
334                                 "Unexpected X509 certificate found.",
335                                 credential.getX509Certificate().getSubjectDN().getName(),
336                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
337                         assertEquals(
338                                 "Unexpected certificate chain length.",
339                                 new Integer(credential.getX509CertificateChain().length),
340                                 new Integer(3));
341                         assertEquals(
342                                 "Unexpected X509 certificate found.",
343                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
344                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
345                 } catch (Exception e) {
346                         fail("Failed to load credentials: " + e);
347                 }
348         }
349
350         public void testFileX509_PEM_PKCS8Key() {
351
352                 try {
353                         InputStream inStream = new FileInputStream("data/credentials5.xml");
354                         parser.parse(new InputSource(inStream));
355                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
356
357                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
358                         Credential credential = credentials.getCredential("test");
359
360                         assertTrue(
361                                 "Credential was loaded with an incorrect type.",
362                                 credential.getCredentialType() == Credential.X509);
363                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
364                         assertEquals(
365                                 "Unexpected X509 certificate found.",
366                                 credential.getX509Certificate().getSubjectDN().getName(),
367                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
368                         assertEquals(
369                                 "Unexpected certificate chain length.",
370                                 new Integer(credential.getX509CertificateChain().length),
371                                 new Integer(3));
372                         assertEquals(
373                                 "Unexpected X509 certificate found.",
374                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
375                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
376                 } catch (Exception e) {
377                         fail("Failed to load credentials: " + e);
378                 }
379         }
380
381         public void testFileX509_DER_RSA_Key() {
382
383                 try {
384                         InputStream inStream = new FileInputStream("data/credentials6.xml");
385                         parser.parse(new InputSource(inStream));
386                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
387
388                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
389                         Credential credential = credentials.getCredential("test");
390
391                         assertTrue(
392                                 "Credential was loaded with an incorrect type.",
393                                 credential.getCredentialType() == Credential.X509);
394                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
395                         assertEquals(
396                                 "Unexpected X509 certificate found.",
397                                 credential.getX509Certificate().getSubjectDN().getName(),
398                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
399                         assertEquals(
400                                 "Unexpected certificate chain length.",
401                                 new Integer(credential.getX509CertificateChain().length),
402                                 new Integer(3));
403                         assertEquals(
404                                 "Unexpected X509 certificate found.",
405                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
406                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
407                 } catch (Exception e) {
408                         fail("Failed to load credentials: " + e);
409                 }
410         }
411
412         public void testFileX509_PEM_RSA_Key() {
413
414                 try {
415                         InputStream inStream = new FileInputStream("data/credentials7.xml");
416                         parser.parse(new InputSource(inStream));
417                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
418
419                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
420                         Credential credential = credentials.getCredential("test");
421
422                         assertTrue(
423                                 "Credential was loaded with an incorrect type.",
424                                 credential.getCredentialType() == Credential.X509);
425                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
426                         assertEquals(
427                                 "Unexpected X509 certificate found.",
428                                 credential.getX509Certificate().getSubjectDN().getName(),
429                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
430                         assertEquals(
431                                 "Unexpected certificate chain length.",
432                                 new Integer(credential.getX509CertificateChain().length),
433                                 new Integer(3));
434                         assertEquals(
435                                 "Unexpected X509 certificate found.",
436                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
437                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
438                 } catch (Exception e) {
439                         fail("Failed to load credentials: " + e);
440                 }
441         }
442
443         public void testFileX509_DER_DSA_Key() {
444
445                 try {
446                         InputStream inStream = new FileInputStream("data/credentials8.xml");
447                         parser.parse(new InputSource(inStream));
448                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
449
450                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
451                         Credential credential = credentials.getCredential("test");
452
453                         assertTrue(
454                                 "Credential was loaded with an incorrect type.",
455                                 credential.getCredentialType() == Credential.X509);
456                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
457                         assertEquals(
458                                 "Unexpected X509 certificate found.",
459                                 credential.getX509Certificate().getSubjectDN().getName(),
460                                 "CN=test.columbia.edu, OU=ACIS, O=Columbia University, L=New York, ST=NY, C=US");
461                         assertEquals(
462                                 "Unexpected certificate chain length.",
463                                 new Integer(credential.getX509CertificateChain().length),
464                                 new Integer(1));
465                 } catch (Exception e) {
466                         fail("Failed to load credentials: " + e);
467                 }
468         }
469
470         public void testFileX509_PEM_DSA_Key() {
471
472                 try {
473                         InputStream inStream = new FileInputStream("data/credentials9.xml");
474                         parser.parse(new InputSource(inStream));
475                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
476
477                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
478                         Credential credential = credentials.getCredential("test");
479
480                         assertTrue(
481                                 "Credential was loaded with an incorrect type.",
482                                 credential.getCredentialType() == Credential.X509);
483                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
484                         assertEquals(
485                                 "Unexpected X509 certificate found.",
486                                 credential.getX509Certificate().getSubjectDN().getName(),
487                                 "CN=test.columbia.edu, OU=ACIS, O=Columbia University, L=New York, ST=NY, C=US");
488                         assertEquals(
489                                 "Unexpected certificate chain length.",
490                                 new Integer(credential.getX509CertificateChain().length),
491                                 new Integer(1));
492                 } catch (Exception e) {
493                         fail("Failed to load credentials: " + e);
494                 }
495         }
496
497         public void testFileX509_PEM_PKCS8_DSA_Key() {
498
499                 try {
500                         InputStream inStream = new FileInputStream("data/credentials10.xml");
501                         parser.parse(new InputSource(inStream));
502                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
503
504                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
505                         Credential credential = credentials.getCredential("test");
506
507                         assertTrue(
508                                 "Credential was loaded with an incorrect type.",
509                                 credential.getCredentialType() == Credential.X509);
510                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
511                         assertEquals(
512                                 "Unexpected X509 certificate found.",
513                                 credential.getX509Certificate().getSubjectDN().getName(),
514                                 "CN=test.columbia.edu, OU=ACIS, O=Columbia University, L=New York, ST=NY, C=US");
515                         assertEquals(
516                                 "Unexpected certificate chain length.",
517                                 new Integer(credential.getX509CertificateChain().length),
518                                 new Integer(1));
519                 } catch (Exception e) {
520                         fail("Failed to load credentials: " + e);
521                 }
522         }
523
524         public void testFileX509_DER_PKCS8_Encrypted_RSA_Key() {
525
526                 try {
527                         InputStream inStream = new FileInputStream("data/credentials11.xml");
528                         parser.parse(new InputSource(inStream));
529                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
530
531                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
532                         Credential credential = credentials.getCredential("test");
533
534                         assertTrue(
535                                 "Credential was loaded with an incorrect type.",
536                                 credential.getCredentialType() == Credential.X509);
537                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
538                         assertEquals(
539                                 "Unexpected X509 certificate found.",
540                                 credential.getX509Certificate().getSubjectDN().getName(),
541                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
542                         assertEquals(
543                                 "Unexpected certificate chain length.",
544                                 new Integer(credential.getX509CertificateChain().length),
545                                 new Integer(3));
546                         assertEquals(
547                                 "Unexpected X509 certificate found.",
548                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
549                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
550                 } catch (Exception e) {
551                         fail("Failed to load credentials: " + e);
552                 }
553         }
554
555         public void testFileX509_PEM_PKCS8_Encrypted_RSA_Key() {
556
557                 try {
558                         InputStream inStream = new FileInputStream("data/credentials12.xml");
559                         parser.parse(new InputSource(inStream));
560                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
561
562                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
563                         Credential credential = credentials.getCredential("test");
564
565                         assertTrue(
566                                 "Credential was loaded with an incorrect type.",
567                                 credential.getCredentialType() == Credential.X509);
568                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
569                         assertEquals(
570                                 "Unexpected X509 certificate found.",
571                                 credential.getX509Certificate().getSubjectDN().getName(),
572                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
573                         assertEquals(
574                                 "Unexpected certificate chain length.",
575                                 new Integer(credential.getX509CertificateChain().length),
576                                 new Integer(3));
577                         assertEquals(
578                                 "Unexpected X509 certificate found.",
579                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
580                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
581                 } catch (Exception e) {
582                         fail("Failed to load credentials: " + e);
583                 }
584         }
585
586         public void testFileX509_PEM_Encrypted_DES_RSA_Key() {
587
588                 try {
589                         InputStream inStream = new FileInputStream("data/credentials14.xml");
590                         parser.parse(new InputSource(inStream));
591                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
592
593                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
594                         Credential credential = credentials.getCredential("test");
595
596                         assertTrue(
597                                 "Credential was loaded with an incorrect type.",
598                                 credential.getCredentialType() == Credential.X509);
599                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
600                         assertEquals(
601                                 "Unexpected X509 certificate found.",
602                                 credential.getX509Certificate().getSubjectDN().getName(),
603                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
604                         assertEquals(
605                                 "Unexpected certificate chain length.",
606                                 new Integer(credential.getX509CertificateChain().length),
607                                 new Integer(3));
608                         assertEquals(
609                                 "Unexpected X509 certificate found.",
610                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
611                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
612                 } catch (Exception e) {
613                         fail("Failed to load credentials: " + e);
614                 }
615         }
616
617         public void testFileX509_PEM_Encrypted_TripeDES_RSA_Key() {
618
619                 try {
620                         InputStream inStream = new FileInputStream("data/credentials13.xml");
621                         parser.parse(new InputSource(inStream));
622                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
623
624                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
625                         Credential credential = credentials.getCredential("test");
626
627                         assertTrue(
628                                 "Credential was loaded with an incorrect type.",
629                                 credential.getCredentialType() == Credential.X509);
630                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
631                         assertEquals(
632                                 "Unexpected X509 certificate found.",
633                                 credential.getX509Certificate().getSubjectDN().getName(),
634                                 "CN=shib2.internet2.edu, OU=Unknown, O=Unknown, ST=Unknown, C=Unknown");
635                         assertEquals(
636                                 "Unexpected certificate chain length.",
637                                 new Integer(credential.getX509CertificateChain().length),
638                                 new Integer(3));
639                         assertEquals(
640                                 "Unexpected X509 certificate found.",
641                                 credential.getX509CertificateChain()[2].getSubjectDN().getName(),
642                                 "CN=HEPKI Master CA -- 20020701A, OU=Division of Information Technology, O=University of Wisconsin, L=Madison, ST=Wisconsin, C=US");
643                 } catch (Exception e) {
644                         fail("Failed to load credentials: " + e);
645                 }
646         }
647
648         public void testFileX509_PEM_Encrypted_TripeDES_DSA_Key() {
649
650                 try {
651                         InputStream inStream = new FileInputStream("data/credentials15.xml");
652                         parser.parse(new InputSource(inStream));
653                         Credentials credentials = new Credentials(parser.getDocument().getDocumentElement());
654
655                         assertTrue("Credential could not be found.", credentials.containsCredential("test"));
656                         Credential credential = credentials.getCredential("test");
657
658                         assertTrue(
659                                 "Credential was loaded with an incorrect type.",
660                                 credential.getCredentialType() == Credential.X509);
661                         assertNotNull("Private key was not loaded correctly.", credential.getPrivateKey());
662                         assertEquals(
663                                 "Unexpected X509 certificate found.",
664                                 credential.getX509Certificate().getSubjectDN().getName(),
665                                 "CN=test.columbia.edu, OU=ACIS, O=Columbia University, L=New York, ST=NY, C=US");
666                         assertEquals(
667                                 "Unexpected certificate chain length.",
668                                 new Integer(credential.getX509CertificateChain().length),
669                                 new Integer(1));
670                 } catch (Exception e) {
671                         fail("Failed to load credentials: " + e);
672                 }
673         }
674
675 }