Updated test to work with new config strategy.
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / aa / attrresolv / ResolverTests.java
1 /* 
2  * The Shibboleth License, Version 1. 
3  * Copyright (c) 2002 
4  * University Corporation for Advanced Internet Development, Inc. 
5  * All rights reserved
6  * 
7  * 
8  * Redistribution and use in source and binary forms, with or without 
9  * modification, are permitted provided that the following conditions are met:
10  * 
11  * Redistributions of source code must retain the above copyright notice, this 
12  * list of conditions and the following disclaimer.
13  * 
14  * Redistributions in binary form must reproduce the above copyright notice, 
15  * this list of conditions and the following disclaimer in the documentation 
16  * and/or other materials provided with the distribution, if any, must include 
17  * the following acknowledgment: "This product includes software developed by 
18  * the University Corporation for Advanced Internet Development 
19  * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement 
20  * may appear in the software itself, if and wherever such third-party 
21  * acknowledgments normally appear.
22  * 
23  * Neither the name of Shibboleth nor the names of its contributors, nor 
24  * Internet2, nor the University Corporation for Advanced Internet Development, 
25  * Inc., nor UCAID may be used to endorse or promote products derived from this 
26  * software without specific prior written permission. For written permission, 
27  * please contact shibboleth@shibboleth.org
28  * 
29  * Products derived from this software may not be called Shibboleth, Internet2, 
30  * UCAID, or the University Corporation for Advanced Internet Development, nor 
31  * may Shibboleth appear in their name, without prior written permission of the 
32  * University Corporation for Advanced Internet Development.
33  * 
34  * 
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
36  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
38  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK 
39  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. 
40  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY 
41  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, 
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50 package edu.internet2.middleware.shibboleth.aa.attrresolv;
51
52 import java.io.File;
53 import java.net.MalformedURLException;
54
55 import junit.framework.TestCase;
56
57 import org.apache.log4j.BasicConfigurator;
58 import org.apache.log4j.Level;
59 import org.apache.log4j.Logger;
60 import org.opensaml.SAMLException;
61
62 import sun.security.acl.PrincipalImpl;
63 import edu.internet2.middleware.shibboleth.aa.AAAttribute;
64 import edu.internet2.middleware.shibboleth.aa.AAAttributeSet;
65 import edu.internet2.middleware.shibboleth.aa.attrresolv.provider.ScopedStringValueHandler;
66
67 /**
68  * Validation suite for the <code>AttributeResolver</code>.
69  * 
70  * @ author Walter Hoehn(wassa@columbia.edu)
71  */
72
73 public class ResolverTests extends TestCase {
74
75         public ResolverTests(String name) {
76                 super(name);
77                 BasicConfigurator.resetConfiguration();
78                 BasicConfigurator.configure();
79                 Logger.getRootLogger().setLevel(Level.OFF);
80         }
81
82         public static void main(String[] args) {
83                 junit.textui.TestRunner.run(ResolverTests.class);
84                 BasicConfigurator.configure();
85                 Logger.getRootLogger().setLevel(Level.OFF);
86         }
87
88         /**
89          * @see junit.framework.TestCase#setUp()
90          */
91         protected void setUp() throws Exception {
92                 super.setUp();
93         }
94
95         public void testBasic() {
96
97                 try {
98                         File file = new File("data/resolver1.xml");
99                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
100
101                         AAAttributeSet inputAttributes =
102                                 new AAAttributeSet(
103                                         new AAAttribute[] {
104                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonNickName"),
105                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonEntitlement")});
106
107                         AAAttributeSet outputAttributes =
108                                 new AAAttributeSet(
109                                         new AAAttribute[] {
110                                                  new AAAttribute(
111                                                         "urn:mace:dir:attribute-def:eduPersonEntitlement",
112                                                         new Object[] { "urn:mace:example.edu:exampleEntitlement" })
113                                                 });
114
115                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
116
117                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
118
119                 } catch (AttributeResolverException e) {
120                         fail("Couldn't load attribute resolver: " + e.getMessage());
121                 } catch (MalformedURLException e) {
122                         fail("Error in test specification: " + e.getMessage());
123                 } catch (SAMLException e) {
124                         fail("Error creating SAML Attribute: " + e.getMessage());
125                 }
126         }
127         
128         public void testSmartScoping() {
129
130                 try {
131
132                         File file = new File("data/resolver2.xml");
133                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
134
135                         AAAttributeSet inputAttributes =
136                                 new AAAttributeSet(
137                                         new AAAttribute[] {
138                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonPrincipalName"),
139                                                 new AAAttribute("foo")});
140
141                         AAAttributeSet outputAttributes = new AAAttributeSet(new AAAttribute[] {
142                                 //Attribute should have scope appended to connector output
143                                 new AAAttribute(
144                                         "urn:mace:dir:attribute-def:eduPersonPrincipalName",
145                                         new Object[] { "mytestuser@example.edu" },
146                                         new ScopedStringValueHandler("example.edu")),
147                                 //Attribute should retain scope from connector output
148                                 new AAAttribute(
149                                         "foo",
150                                         new Object[] { "bar@example.com" },
151                                         new ScopedStringValueHandler("example.edu"))
152                                 });
153
154                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
155                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
156
157                 } catch (AttributeResolverException e) {
158                         fail("Couldn't load attribute resolver: " + e.getMessage());
159                 } catch (MalformedURLException e) {
160                         fail("Error in test specification: " + e.getMessage());
161                 } catch (SAMLException e) {
162                         fail("Error creating SAML Attribute: " + e.getMessage());
163                 }
164         }
165         
166         public void testExceptionForNoPlugIns() {
167
168                 try {
169                         File file = new File("data/resolver3.xml");
170                         new AttributeResolver(file.toURL().toString());
171                         
172                         fail("Attribute Resolver loaded even when no PlugIns were configured.");
173                 } catch (AttributeResolverException e) {
174                         //This exception should be thrown, ignoring
175                 } catch (MalformedURLException e) {
176                         fail("Error in test specification: " + e.getMessage());
177                 }
178         }
179                 
180         public void testExceptionForNoValidPlugIns() {
181
182                 try {
183                         File file = new File("data/resolver4.xml");
184                         new AttributeResolver(file.toURL().toString());
185                         fail("Attribute Resolver loaded even when no PlugIns were successfully registered.");
186                 } catch (AttributeResolverException e) {
187                         //This exception should be thrown, ignoring
188                 } catch (MalformedURLException e) {
189                         fail("Error in test specification: " + e.getMessage());
190                 }
191         }
192                 
193         public void testFailToLoadCircularDependencies() {
194
195                 try {
196                         File file = new File("data/resolver5.xml");
197                         new AttributeResolver(file.toURL().toString());
198                         fail("Attribute Resolver loaded even when no only PlugIns with circular dependencies were configured.");
199                 } catch (AttributeResolverException e) {
200                         //This exception should be thrown, ignoring
201                 } catch (MalformedURLException e) {
202                         fail("Error in test specification: " + e.getMessage());
203                 }
204         }
205         
206         public void testFailToLoadCircularDependenciesDeeper() {
207
208                 try {
209                         File file = new File("data/resolver6.xml");
210                         new AttributeResolver(file.toURL().toString());
211                         fail("Attribute Resolver loaded even when no only PlugIns with circular dependencies were configured.");
212                 } catch (AttributeResolverException e) {
213                         //This exception should be thrown, ignoring
214                 } catch (MalformedURLException e) {
215                         fail("Error in test specification: " + e.getMessage());
216                 }
217         }
218
219         public void testSourceNameMapping() {
220
221                 try {
222                         File file = new File("data/resolver7.xml");
223                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
224
225                         AAAttributeSet inputAttributes = new AAAttributeSet(new AAAttribute[] { new AAAttribute("myAffiliation")});
226
227                         AAAttributeSet outputAttributes =
228                                 new AAAttributeSet(new AAAttribute[] { new AAAttribute("myAffiliation", new Object[] { "member" })
229                         });
230
231                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
232                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
233
234                 } catch (AttributeResolverException e) {
235                         fail("Couldn't load attribute resolver: " + e.getMessage());
236                 } catch (MalformedURLException e) {
237                         fail("Error in test specification: " + e.getMessage());
238                 } catch (SAMLException e) {
239                         fail("Error creating SAML Attribute: " + e.getMessage());
240                 }
241         }
242
243         public void testMultipleDataConnectors() {
244
245                 try {
246                         File file = new File("data/resolver8.xml");
247                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
248
249                         AAAttributeSet inputAttributes =
250                                 new AAAttributeSet(
251                                         new AAAttribute[] {
252                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonPrincipalName"),
253                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonAffiliation"),
254                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonEntitlement")});
255
256                         AAAttributeSet outputAttributes =
257                                 new AAAttributeSet(
258                                         new AAAttribute[] {
259                                                 new AAAttribute(
260                                                         "urn:mace:dir:attribute-def:eduPersonPrincipalName",
261                                                         new Object[] { "mytestuser@example.edu" },
262                                                         new ScopedStringValueHandler("example.edu")),
263                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonAffiliation", new Object[] { "member" }),
264                                                 new AAAttribute(
265                                                         "urn:mace:dir:attribute-def:eduPersonEntitlement",
266                                                         new Object[] { "urn:mace:example.edu:exampleEntitlement" })
267                                                 });
268
269                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
270
271                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
272
273                 } catch (AttributeResolverException e) {
274                         fail("Couldn't load attribute resolver: " + e.getMessage());
275                 } catch (MalformedURLException e) {
276                         fail("Error in test specification: " + e.getMessage());
277                 } catch (SAMLException e) {
278                         fail("Error creating SAML Attribute: " + e.getMessage());
279                 }
280         }
281         
282         public void testAttributeDependency() {
283
284                 try {
285                         File file = new File("data/resolver9.xml");
286                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
287
288                         AAAttributeSet inputAttributes =
289                                 new AAAttributeSet(
290                                         new AAAttribute[] { new AAAttribute("urn:mace:dir:attribute-def:eduPersonScopedAffiliation")});
291
292                         AAAttributeSet outputAttributes =
293                                 new AAAttributeSet(
294                                         new AAAttribute[] {
295                                                  new AAAttribute(
296                                                         "urn:mace:dir:attribute-def:eduPersonScopedAffiliation",
297                                                         new Object[] { "member@example.edu" },
298                                                         new ScopedStringValueHandler("example.edu"))
299                                                 });
300
301                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
302
303                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
304
305                 } catch (AttributeResolverException e) {
306                         fail("Couldn't load attribute resolver: " + e.getMessage());
307                 } catch (MalformedURLException e) {
308                         fail("Error in test specification: " + e.getMessage());
309                 } catch (SAMLException e) {
310                         fail("Error creating SAML Attribute: " + e.getMessage());
311                 }
312         }
313         
314         public void testMisLabeledDataConnector() {
315
316                 try {
317                         File file = new File("data/resolver11.xml");
318                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
319
320                         AAAttributeSet inputAttributes =
321                                 new AAAttributeSet(
322                                         new AAAttribute[] { new AAAttribute("urn:mace:dir:attribute-def:eduPersonScopedAffiliation")});
323
324                         AAAttributeSet outputAttributes = new AAAttributeSet();
325
326                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
327
328                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
329
330                 } catch (AttributeResolverException e) {
331                         fail("Couldn't load attribute resolver: " + e.getMessage());
332                 } catch (MalformedURLException e) {
333                         fail("Error in test specification: " + e.getMessage());
334                 } catch (SAMLException e) {
335                         fail("Error creating SAML Attribute: " + e.getMessage());
336                 }
337         }
338
339         public void testMisLabeledAttributeDefinition() {
340
341                 try {
342                         File file = new File("data/resolver10.xml");
343                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
344
345                         AAAttributeSet inputAttributes =
346                                 new AAAttributeSet(
347                                         new AAAttribute[] { new AAAttribute("urn:mace:dir:attribute-def:eduPersonScopedAffiliation")});
348
349                         AAAttributeSet outputAttributes = new AAAttributeSet();
350
351                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
352
353                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
354                 } catch (ClassCastException e) {
355                         fail("Failed to detect that an Attribute Definition was mislabeled as a Data Connector: " + e.getMessage());
356                 } catch (AttributeResolverException e) {
357                         fail("Couldn't load attribute resolver: " + e.getMessage());
358                 } catch (MalformedURLException e) {
359                         fail("Error in test specification: " + e.getMessage());
360                 } catch (SAMLException e) {
361                         fail("Error creating SAML Attribute: " + e.getMessage());
362                 }
363         }
364         public void testMultiLevelAttributeDependency() {
365
366                 try {
367                         File file = new File("data/resolver12.xml");
368                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
369
370                         AAAttributeSet inputAttributes =
371                                 new AAAttributeSet(
372                                         new AAAttribute[] {
373                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonScopedAffiliation"),
374                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonAffiliation"),
375                                                 new AAAttribute("urn:mace:shibboleth:test:eduPersonAffiliation")});
376
377                         AAAttributeSet outputAttributes =
378                                 new AAAttributeSet(
379                                         new AAAttribute[] {
380                                                 new AAAttribute(
381                                                         "urn:mace:dir:attribute-def:eduPersonScopedAffiliation",
382                                                         new Object[] { "member@example.edu" },
383                                                         new ScopedStringValueHandler("example.edu")),
384                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonAffiliation", new Object[] { "member" }),
385                                                 new AAAttribute("urn:mace:shibboleth:test:eduPersonAffiliation", new Object[] { "member" })
386                                         });
387
388                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
389
390                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
391
392                 } catch (AttributeResolverException e) {
393                         fail("Couldn't load attribute resolver: " + e.getMessage());
394                 } catch (MalformedURLException e) {
395                         fail("Error in test specification: " + e.getMessage());
396                 } catch (SAMLException e) {
397                         fail("Error creating SAML Attribute: " + e.getMessage());
398                 }
399         }
400
401
402 }