b36a836831ee47d3cf960c9e3a4c51520c020f59
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / metadata / MetadataTests.java
1 /*
2  * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation for Advanced Internet Development, Inc.
3  * All rights reserved Redistribution and use in source and binary forms, with or without modification, are permitted
4  * provided that the following conditions are met: Redistributions of source code must retain the above copyright
5  * notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the
6  * above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other
7  * materials provided with the distribution, if any, must include the following acknowledgment: "This product includes
8  * software developed by the University Corporation for Advanced Internet Development <http://www.ucaid.edu> Internet2
9  * Project. Alternately, this acknowledegement may appear in the software itself, if and wherever such third-party
10  * acknowledgments normally appear. Neither the name of Shibboleth nor the names of its contributors, nor Internet2,
11  * nor the University Corporation for Advanced Internet Development, Inc., nor UCAID may be used to endorse or promote
12  * products derived from this software without specific prior written permission. For written permission, please
13  * contact shibboleth@shibboleth.org Products derived from this software may not be called Shibboleth, Internet2,
14  * UCAID, or the University Corporation for Advanced Internet Development, nor may Shibboleth appear in their name,
15  * without prior written permission of the University Corporation for Advanced Internet Development. THIS SOFTWARE IS
16  * PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
18  * NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS
19  * WITH LICENSEE. IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY CORPORATION FOR ADVANCED
20  * INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
23  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 package edu.internet2.middleware.shibboleth.metadata;
28
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.InputStream;
33 import java.util.Arrays;
34
35 import junit.framework.TestCase;
36
37 import org.apache.log4j.BasicConfigurator;
38 import org.apache.log4j.Level;
39 import org.apache.log4j.Logger;
40 import org.apache.xerces.parsers.DOMParser;
41 import org.xml.sax.EntityResolver;
42 import org.xml.sax.ErrorHandler;
43 import org.xml.sax.InputSource;
44 import org.xml.sax.SAXException;
45 import org.xml.sax.SAXParseException;
46
47 import edu.internet2.middleware.shibboleth.metadata.provider.XMLMetadataLoadWrapper;
48
49 /**
50  * Validation suite for the <code>Metadata</code> interface.
51  * 
52  * @author Walter Hoehn
53  */
54
55 public class MetadataTests extends TestCase {
56
57         private DOMParser       parser  = new DOMParser();
58
59         public MetadataTests(String name) {
60                 super(name);
61                 BasicConfigurator.resetConfiguration();
62                 BasicConfigurator.configure();
63                 Logger.getRootLogger().setLevel(Level.OFF);
64         }
65
66         public static void main(String[] args) {
67                 junit.textui.TestRunner.run(MetadataTests.class);
68                 BasicConfigurator.configure();
69                 Logger.getRootLogger().setLevel(Level.OFF);
70         }
71
72         protected void setUp() throws Exception {
73                 super.setUp();
74                 try {
75                         parser.setFeature("http://xml.org/sax/features/validation", true);
76                         parser.setFeature("http://apache.org/xml/features/validation/schema", true);
77                         parser.setEntityResolver(new EntityResolver() {
78
79                                 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
80
81                                         if (systemId.endsWith("credentials.xsd")) {
82                                                 InputStream stream;
83                                                 try {
84                                                         stream = new FileInputStream("src/schemas/shibboleth.xsd");
85                                                         if (stream != null) {
86                                                                 return new InputSource(stream);
87                                                         }
88                                                         throw new SAXException("Could not load entity: Null input stream");
89                                                 } catch (FileNotFoundException e) {
90                                                         throw new SAXException("Could not load entity: " + e);
91                                                 }
92                                         } else if (systemId.endsWith("xmldsig-core-schema.xsd")) {
93                                                 InputStream stream;
94                                                 try {
95                                                         stream = new FileInputStream("src/schemas/xmldsig-core-schema.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 {
104                                                 return null;
105                                         }
106                                 }
107                         });
108
109                         parser.setErrorHandler(new ErrorHandler() {
110
111                                 public void error(SAXParseException arg0) throws SAXException {
112                                         throw new SAXException("Error parsing xml file: " + arg0);
113                                 }
114
115                                 public void fatalError(SAXParseException arg0) throws SAXException {
116                                         throw new SAXException("Error parsing xml file: " + arg0);
117                                 }
118
119                                 public void warning(SAXParseException arg0) throws SAXException {
120                                         throw new SAXException("Error parsing xml file: " + arg0);
121                                 }
122                         });
123                 } catch (Exception e) {
124                         fail("Failed to setup xml parser: " + e);
125                 }
126
127         }
128
129         public void testBasicShibbolethXML() {
130
131                 try {
132                         Metadata metadata = new XMLMetadataLoadWrapper(new File("data/sites1.xml").toURL().toString());
133
134                         assertNotNull("Unable to find test provider", metadata.lookup("bahsite"));
135                         assertNotNull("Unable to find test provider", metadata.lookup("rootsite"));
136
137                         assertTrue("Group list is incorrect or out of order.", Arrays.equals(new String[]{"urn:mace:inqueue",
138                                         "foofed", "bahfed"}, metadata.lookup("bahsite").getGroups()));
139
140                         //This should probably be made more robust at some point
141                         assertTrue("Incorrect provider role.", metadata.lookup("bahsite").getRoles()[0] instanceof SPProviderRole);
142                         assertTrue("Incorrect provider role.",
143                                         metadata.lookup("bahsite").getRoles()[0] instanceof AttributeConsumerRole);
144
145                         assertEquals("Incorrect parsing of assertion consumer URL.", ((SPProviderRole) metadata.lookup("bahsite")
146                                         .getRoles()[0]).getAssertionConsumerServiceURLs()[0].getLocation(), "http://foo.com/SHIRE");
147
148                         assertTrue("Incorrect attribute requester parsing.", metadata.lookup("rootsite").getRoles()[0]
149                                         .getKeyDescriptors().length == 2);
150
151                         String[] control = new String[]{
152                                         "C=US, ST=Tennessee, L=Memphis, O=The University of Memphis, OU=Information Systems, CN=test2.memphis.edu",
153                                         "C=US, ST=Tennessee, L=Memphis, O=The University of Memphis, OU=Information Systems, CN=test1.memphis.edu"};
154                         String[] meta = new String[]{
155                                         metadata.lookup("rootsite").getRoles()[0].getKeyDescriptors()[0].getKeyInfo()[0].itemKeyName(0)
156                                                         .getKeyName(),
157                                         metadata.lookup("rootsite").getRoles()[0].getKeyDescriptors()[1].getKeyInfo()[0].itemKeyName(0)
158                                                         .getKeyName()};
159                         Arrays.sort(meta);
160                         Arrays.sort(control);
161                         assertTrue("Encountered unexpected key names", Arrays.equals(control, meta));
162                 } catch (Exception e) {
163                         fail("Failed to correctly load metadata: " + e);
164                 }
165
166         }
167 }