Create parser pool interface and move current pool to an implementation of this inter...
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / idp / provider / DNHostNameExtractionTests.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.idp.provider;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.security.KeyStore;
23 import java.security.KeyStoreException;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.cert.CertificateException;
26 import java.security.cert.X509Certificate;
27
28 import javax.security.auth.x500.X500Principal;
29
30 import junit.framework.TestCase;
31
32 import org.apache.log4j.BasicConfigurator;
33 import org.apache.log4j.Level;
34 import org.apache.log4j.Logger;
35
36 import edu.internet2.middleware.shibboleth.common.ShibResource;
37 import edu.internet2.middleware.shibboleth.common.ShibResource.ResourceNotAvailableException;
38
39 /**
40  * Validation suite for hack to pull hostnames out of a subject DN.
41  * 
42  * @author Walter Hoehn(wassa@columbia.edu)
43  */
44 public class DNHostNameExtractionTests extends TestCase {
45
46         // Basic
47         String dn1 = "CN=wayf.internet2.edu,OU=TSG,O=University Corporation for Advanced Internet Development,L=Ann Arbor,ST=Michigan,C=US";
48
49         // lowercase CN
50         String dn2 = "cn=wayf.internet2.edu,OU=TSG,O=University Corporation for Advanced Internet Development,L=Ann Arbor,ST=Michigan,C=US";
51
52         // Multiple CNs
53         String dn4 = "CN=wayf.internet2.edu,OU=TSG, CN=foo, O=University Corporation for Advanced Internet Development,L=Ann Arbor,ST=Michigan,C=US";
54
55         public DNHostNameExtractionTests(String name) {
56
57                 super(name);
58                 BasicConfigurator.resetConfiguration();
59                 BasicConfigurator.configure();
60                 Logger.getRootLogger().setLevel(Level.OFF);
61         }
62
63         public static void main(String[] args) {
64
65                 junit.textui.TestRunner.run(DNHostNameExtractionTests.class);
66                 BasicConfigurator.configure();
67                 Logger.getRootLogger().setLevel(Level.OFF);
68         }
69
70         protected void setUp() throws Exception {
71
72                 super.setUp();
73
74         }
75
76         public void testBasicExtraction() {
77
78                 try {
79                         assertEquals("Round-trip handle validation failed on DN.", BaseHandler.getHostNameFromDN(new X500Principal(
80                                         dn1)), "wayf.internet2.edu");
81
82                 } catch (Exception e) {
83                         fail("Error in test specification: " + e.getMessage());
84                 }
85         }
86
87         public void testExtractionWithLowerCaseAttrName() {
88
89                 try {
90                         assertEquals("Round-trip handle validation failed on DN.", BaseHandler.getHostNameFromDN(new X500Principal(
91                                         dn2)), "wayf.internet2.edu");
92
93                 } catch (Exception e) {
94                         fail("Error in test specification: " + e.getMessage());
95                 }
96         }
97
98         public void testExtractionWithMultipleCNs() {
99
100                 try {
101                         assertEquals("Round-trip handle validation failed on DN.", BaseHandler.getHostNameFromDN(new X500Principal(
102                                         dn4)), "wayf.internet2.edu");
103
104                 } catch (Exception e) {
105                         fail("Error in test specification: " + e.getMessage());
106                 }
107         }
108
109         public void testExtractionWithStrangeDN() {
110
111                 try {
112                         // Use the cert referenced in bugzilla #143
113                         // This cert was breaking previously because of java's conversion of the dn to string form
114                         KeyStore keyStore = KeyStore.getInstance("JKS");
115                         keyStore.load(new ShibResource(new File("data/cnextract.jks").toURL().toString()).getInputStream(),
116                                         new char[]{'t', 'e', 's', 't', '1', '2', '3'});
117                         X509Certificate cert = (X509Certificate) keyStore.getCertificate("scott");
118
119                         FileOutputStream output = new FileOutputStream("/tmp/principal.der");
120                         output.write(cert.getSubjectX500Principal().getEncoded());
121                         output.close();
122
123                         assertEquals("Round-trip handle validation failed on DN.", BaseHandler.getHostNameFromDN(cert
124                                         .getSubjectX500Principal()), "asd3.ais.ucla.edu");
125
126                 } catch (ResourceNotAvailableException e) {
127                         fail("Error in test specification: " + e);
128                 } catch (IOException e) {
129                         fail("Error in test specification: " + e);
130                 } catch (NoSuchAlgorithmException e) {
131                         fail("Error in test specification: " + e);
132                 } catch (CertificateException e) {
133                         fail("Error in test specification: " + e);
134                 } catch (KeyStoreException e) {
135                         fail("Error in test specification: " + e);
136                 } catch (Exception e) {
137                         fail("Error in test specification: " + e.getMessage());
138                 }
139         }
140
141 }