use the new session manager interface
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / common / RelyingPartyMapperTests.java
1 /*
2  * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package edu.internet2.middleware.shibboleth.common;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.parsers.ParserConfigurationException;
25
26 import junit.framework.TestCase;
27
28 import org.apache.log4j.BasicConfigurator;
29 import org.apache.log4j.Level;
30 import org.apache.log4j.Logger;
31 import org.opensaml.Configuration;
32 import org.opensaml.saml2.metadata.provider.FilesystemMetadataProvider;
33 import org.opensaml.saml2.metadata.provider.MetadataProvider;
34 import org.opensaml.saml2.metadata.provider.MetadataProviderException;
35 import org.w3c.dom.Element;
36 import org.w3c.dom.NodeList;
37 import org.xml.sax.InputSource;
38 import org.xml.sax.SAXException;
39
40 /**
41  * Validation suite for the <code>RelyingPartyMapper</code>.
42  * 
43  * @author Walter Hoehn
44  */
45
46 public class RelyingPartyMapperTests extends TestCase {
47
48         public RelyingPartyMapperTests(String name) {
49
50                 super(name);
51                 BasicConfigurator.resetConfiguration();
52                 BasicConfigurator.configure();
53                 Logger.getRootLogger().setLevel(Level.OFF);
54         }
55
56         public static void main(String[] args) {
57
58                 junit.textui.TestRunner.run(RelyingPartyMapperTests.class);
59                 BasicConfigurator.configure();
60                 Logger.getRootLogger().setLevel(Level.OFF);
61         }
62
63         protected void setUp() throws Exception {
64
65                 super.setUp();
66         }
67
68         public void testBasicFunction() {
69
70                 try {
71                         // Parse IdP config file
72                         String fileLocation = "data/relyingPartyMapper1.xml";
73                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
74                         factory.setValidating(false);
75                         factory.setNamespaceAware(true);
76
77                         // We have to get a credentials set in order to init the mapper
78                         NodeList credentialNodes = factory.newDocumentBuilder().parse(
79                                         new InputSource(new FileInputStream(fileLocation))).getDocumentElement().getElementsByTagNameNS(
80                                         Credentials.credentialsNamespace, "Credentials");
81                         Credentials credentials = new Credentials((Element) credentialNodes.item(0));
82
83                         RelyingPartyMapper mapper = new RelyingPartyMapper(factory.newDocumentBuilder().parse(
84                                         new InputSource(new FileInputStream(fileLocation))).getDocumentElement(), credentials);
85
86                         // Make sure we have anonymous support turned off
87                         assertFalse("Anonymous relying party support should be turned off.", mapper.anonymousSuported());
88
89                         // Make sure we have defaulting turned off
90                         assertNull("Expected no relying party.", mapper.getRelyingParty("foobar"));
91
92                         // Make sure we can lookup by providerId
93                         assertNotNull("Expected relying party lookup to succeed.", mapper.getRelyingParty("urn-x:test:1"));
94
95                         // Check the config data for the relying party
96                         assertEquals("Incorrect providerId for relying party.", "urn-x:test:id1", mapper.getRelyingParty(
97                                         "urn-x:test:1").getIdentityProvider().getProviderId());
98                         assertTrue("Incorrect passThruErrors value for relying party.", mapper.getRelyingParty("urn-x:test:1")
99                                         .passThruErrors());
100                         assertNotNull("Missing signing credential for relying party.", mapper.getRelyingParty("urn-x:test:1")
101                                         .getIdentityProvider().getSigningCredential());
102
103                 } catch (SAXException e) {
104                         fail("Error in test specification: " + e.getMessage());
105                 } catch (IOException e) {
106                         fail("Error in test specification: " + e.getMessage());
107                 } catch (RelyingPartyMapperException e) {
108                         fail("Unable to load relying party mapper: " + e.getMessage());
109                 } catch (ParserConfigurationException e) {
110                         fail("Unable to load XML parser: " + e.getMessage());
111                 }
112         }
113
114         public void testAnonymousRelyingParty() {
115
116                 try {
117                         // Parse IdP config file
118                         String fileLocation = "data/relyingPartyMapper2.xml";
119                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
120                         factory.setValidating(false);
121                         factory.setNamespaceAware(true);
122
123                         // We have to get a credentials set in order to init the mapper
124                         NodeList credentialNodes = factory.newDocumentBuilder().parse(
125                                         new InputSource(new FileInputStream(fileLocation))).getDocumentElement().getElementsByTagNameNS(
126                                         Credentials.credentialsNamespace, "Credentials");
127                         Credentials credentials = new Credentials((Element) credentialNodes.item(0));
128
129                         RelyingPartyMapper mapper = new RelyingPartyMapper(factory.newDocumentBuilder().parse(
130                                         new InputSource(new FileInputStream(fileLocation))).getDocumentElement(), credentials);
131
132                         // Make sure we have anonymous support turned on
133                         assertTrue("Anonymous relying party support should be turned on.", mapper.anonymousSuported());
134                         assertNotNull("Unable to lookup anonymous relying party.", mapper.getAnonymousRelyingParty());
135
136                         // Make sure we got the correct relying party
137                         assertEquals("Wrong providerId.", "urn-x:test:anonId", mapper.getAnonymousRelyingParty()
138                                         .getIdentityProvider().getProviderId());
139
140                 } catch (SAXException e) {
141                         fail("Error in test specification: " + e.getMessage());
142                 } catch (IOException e) {
143                         fail("Error in test specification: " + e.getMessage());
144                 } catch (RelyingPartyMapperException e) {
145                         fail("Unable to load relying party mapper: " + e.getMessage());
146                 } catch (ParserConfigurationException e) {
147                         fail("Unable to load XML parser: " + e.getMessage());
148                 }
149         }
150
151         public void testDefaultRelyingParty() {
152
153                 try {
154                         // Parse IdP config file
155                         String fileLocation = "data/relyingPartyMapper2.xml";
156                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
157                         factory.setValidating(false);
158                         factory.setNamespaceAware(true);
159
160                         // We have to get a credentials set in order to init the mapper
161                         NodeList credentialNodes = factory.newDocumentBuilder().parse(
162                                         new InputSource(new FileInputStream(fileLocation))).getDocumentElement().getElementsByTagNameNS(
163                                         Credentials.credentialsNamespace, "Credentials");
164                         Credentials credentials = new Credentials((Element) credentialNodes.item(0));
165
166                         RelyingPartyMapper mapper = new RelyingPartyMapper(factory.newDocumentBuilder().parse(
167                                         new InputSource(new FileInputStream(fileLocation))).getDocumentElement(), credentials);
168
169                         // Make sure we have defaulting turned on
170                         assertNotNull("Expected no relying party.", mapper.getRelyingParty("foobar"));
171
172                         // Make sure we got the correct relying party
173                         assertEquals("Wrong providerId.", "urn-x:test:defId", mapper.getRelyingParty("foobar")
174                                         .getIdentityProvider().getProviderId());
175
176                 } catch (SAXException e) {
177                         fail("Error in test specification: " + e.getMessage());
178                 } catch (IOException e) {
179                         fail("Error in test specification: " + e.getMessage());
180                 } catch (RelyingPartyMapperException e) {
181                         fail("Unable to load relying party mapper: " + e.getMessage());
182                 } catch (ParserConfigurationException e) {
183                         fail("Unable to load XML parser: " + e.getMessage());
184                 }
185         }
186
187         public void testGroupLookup() {
188
189                 try {
190                         // Parse IdP config file
191                         String fileLocation = "data/relyingPartyMapper2.xml";
192                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
193                         factory.setValidating(false);
194                         factory.setNamespaceAware(true);
195
196                         // We have to get a credentials set in order to init the mapper
197                         NodeList credentialNodes = factory.newDocumentBuilder().parse(
198                                         new InputSource(new FileInputStream(fileLocation))).getDocumentElement().getElementsByTagNameNS(
199                                         Credentials.credentialsNamespace, "Credentials");
200                         Credentials credentials = new Credentials((Element) credentialNodes.item(0));
201
202                         RelyingPartyMapper mapper = new RelyingPartyMapper(factory.newDocumentBuilder().parse(
203                                         new InputSource(new FileInputStream(fileLocation))).getDocumentElement(), credentials);
204
205                         Configuration.init();
206                         MetadataProvider metadata = new FilesystemMetadataProvider(new File("data/relyingParty-metadata.xml"));
207                         mapper.setMetadata(metadata);
208
209                         // Make sure we can lookup by group
210                         assertNotNull("Expected relying party lookup by group to succeed.", mapper.getRelyingParty("urn-x:test:1"));
211                         assertEquals("Expected relying party lookup by group to have correct providerId.", "urn-x:test:id1", mapper
212                                         .getRelyingParty("urn-x:test:1").getIdentityProvider().getProviderId());
213
214                 } catch (SAXException e) {
215                         fail("Error in test specification: " + e.getMessage());
216                 } catch (IOException e) {
217                         fail("Error in test specification: " + e.getMessage());
218                 } catch (RelyingPartyMapperException e) {
219                         fail("Unable to load relying party mapper: " + e.getMessage());
220                 } catch (MetadataProviderException e) {
221                         fail("Error in test specification: " + e.getMessage());
222                 } catch (ParserConfigurationException e) {
223                         fail("Unable to load XML parser: " + e.getMessage());
224                 }
225         }
226
227         public void testCustomAttributes() {
228
229                 try {
230                         // Parse IdP config file
231                         String fileLocation = "data/relyingPartyMapper3.xml";
232                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
233                         factory.setValidating(false);
234                         factory.setNamespaceAware(true);
235
236                         // We have to get a credentials set in order to init the mapper
237                         NodeList credentialNodes = factory.newDocumentBuilder().parse(
238                                         new InputSource(new FileInputStream(fileLocation))).getDocumentElement().getElementsByTagNameNS(
239                                         Credentials.credentialsNamespace, "Credentials");
240                         Credentials credentials = new Credentials((Element) credentialNodes.item(0));
241
242                         RelyingPartyMapper mapper = new RelyingPartyMapper(factory.newDocumentBuilder().parse(
243                                         new InputSource(new FileInputStream(fileLocation))).getDocumentElement(), credentials);
244
245                         // Make sure we can lookup by providerId
246                         assertNotNull("Expected relying party lookup to succeed.", mapper.getRelyingParty("urn-x:test:1"));
247
248                         // Check the extended config data for the relying party
249                         assertEquals("Incorrect extenstion attribute for relying party.", "foo", mapper.getRelyingParty(
250                                         "urn-x:test:1").getCustomAttribute("extension1"));
251                         assertEquals("Incorrect extension attribute for relying party.", "bar", mapper.getRelyingParty(
252                                         "urn-x:test:1").getCustomAttribute("extension2"));
253                         assertNull("Incorrect extension attribute for relying party.", mapper.getRelyingParty("urn-x:test:1")
254                                         .getCustomAttribute("extension3"));
255                         assertNull("Incorrect extension attribute for relying party.", mapper.getRelyingParty("urn-x:test:1")
256                                         .getCustomAttribute("providerId"));
257
258                 } catch (SAXException e) {
259                         fail("Error in test specification: " + e.getMessage());
260                 } catch (IOException e) {
261                         fail("Error in test specification: " + e.getMessage());
262                 } catch (RelyingPartyMapperException e) {
263                         fail("Unable to load relying party mapper: " + e.getMessage());
264                 } catch (ParserConfigurationException e) {
265                         fail("Unable to load XML parser: " + e.getMessage());
266                 }
267         }
268 }