Better name for principal class.
[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
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 import edu.internet2.middleware.shibboleth.common.LocalPrincipal;
67
68 /**
69  * Validation suite for the <code>AttributeResolver</code>.
70  * 
71  * @ author Walter Hoehn(wassa@columbia.edu)
72  */
73
74 public class ResolverTests extends TestCase {
75
76         public ResolverTests(String name) {
77                 super(name);
78                 BasicConfigurator.resetConfiguration();
79                 BasicConfigurator.configure();
80                 Logger.getRootLogger().setLevel(Level.OFF);
81         }
82
83         public static void main(String[] args) {
84                 junit.textui.TestRunner.run(ResolverTests.class);
85                 BasicConfigurator.configure();
86                 Logger.getRootLogger().setLevel(Level.OFF);
87         }
88
89         /**
90          * @see junit.framework.TestCase#setUp()
91          */
92         protected void setUp() throws Exception {
93                 super.setUp();
94         }
95
96         public void testBasic() {
97
98                 try {
99                         File file = new File("data/resolver1.xml");
100                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
101
102                         AAAttributeSet inputAttributes =
103                                 new AAAttributeSet(
104                                         new AAAttribute[] {
105                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonNickName"),
106                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonEntitlement")});
107
108                         AAAttributeSet outputAttributes =
109                                 new AAAttributeSet(
110                                         new AAAttribute[] {
111                                                  new AAAttribute(
112                                                         "urn:mace:dir:attribute-def:eduPersonEntitlement",
113                                                         new Object[] { "urn:mace:example.edu:exampleEntitlement" })
114                                                 });
115
116                         ar.resolveAttributes(new LocalPrincipal("mytestuser"), "shar.example.edu", inputAttributes);
117
118                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
119
120                 } catch (AttributeResolverException e) {
121                         fail("Couldn't load attribute resolver: " + e.getMessage());
122                 } catch (MalformedURLException e) {
123                         fail("Error in test specification: " + e.getMessage());
124                 } catch (SAMLException e) {
125                         fail("Error creating SAML Attribute: " + e.getMessage());
126                 }
127         }
128         
129         public void testSmartScoping() {
130
131                 try {
132
133                         File file = new File("data/resolver2.xml");
134                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
135
136                         AAAttributeSet inputAttributes =
137                                 new AAAttributeSet(
138                                         new AAAttribute[] {
139                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonPrincipalName"),
140                                                 new AAAttribute("foo")});
141
142                         AAAttributeSet outputAttributes = new AAAttributeSet(new AAAttribute[] {
143                                 //Attribute should have scope appended to connector output
144                                 new AAAttribute(
145                                         "urn:mace:dir:attribute-def:eduPersonPrincipalName",
146                                         new Object[] { "mytestuser@example.edu" },
147                                         new ScopedStringValueHandler("example.edu")),
148                                 //Attribute should retain scope from connector output
149                                 new AAAttribute(
150                                         "foo",
151                                         new Object[] { "bar@example.com" },
152                                         new ScopedStringValueHandler("example.edu"))
153                                 });
154
155                         ar.resolveAttributes(new LocalPrincipal("mytestuser"), "shar.example.edu", inputAttributes);
156                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
157
158                 } catch (AttributeResolverException e) {
159                         fail("Couldn't load attribute resolver: " + e.getMessage());
160                 } catch (MalformedURLException e) {
161                         fail("Error in test specification: " + e.getMessage());
162                 } catch (SAMLException e) {
163                         fail("Error creating SAML Attribute: " + e.getMessage());
164                 }
165         }
166         
167         public void testExceptionForNoPlugIns() {
168
169                 try {
170                         File file = new File("data/resolver3.xml");
171                         new AttributeResolver(file.toURL().toString());
172                         
173                         fail("Attribute Resolver loaded even when no PlugIns were configured.");
174                 } catch (AttributeResolverException e) {
175                         //This exception should be thrown, ignoring
176                 } catch (MalformedURLException e) {
177                         fail("Error in test specification: " + e.getMessage());
178                 }
179         }
180                 
181         public void testExceptionForNoValidPlugIns() {
182
183                 try {
184                         File file = new File("data/resolver4.xml");
185                         new AttributeResolver(file.toURL().toString());
186                         fail("Attribute Resolver loaded even when no PlugIns were successfully registered.");
187                 } catch (AttributeResolverException e) {
188                         //This exception should be thrown, ignoring
189                 } catch (MalformedURLException e) {
190                         fail("Error in test specification: " + e.getMessage());
191                 }
192         }
193                 
194         public void testFailToLoadCircularDependencies() {
195
196                 try {
197                         File file = new File("data/resolver5.xml");
198                         new AttributeResolver(file.toURL().toString());
199                         fail("Attribute Resolver loaded even when no only PlugIns with circular dependencies were configured.");
200                 } catch (AttributeResolverException e) {
201                         //This exception should be thrown, ignoring
202                 } catch (MalformedURLException e) {
203                         fail("Error in test specification: " + e.getMessage());
204                 }
205         }
206         
207         public void testFailToLoadCircularDependenciesDeeper() {
208
209                 try {
210                         File file = new File("data/resolver6.xml");
211                         new AttributeResolver(file.toURL().toString());
212                         fail("Attribute Resolver loaded even when no only PlugIns with circular dependencies were configured.");
213                 } catch (AttributeResolverException e) {
214                         //This exception should be thrown, ignoring
215                 } catch (MalformedURLException e) {
216                         fail("Error in test specification: " + e.getMessage());
217                 }
218         }
219
220         public void testSourceNameMapping() {
221
222                 try {
223                         File file = new File("data/resolver7.xml");
224                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
225
226                         AAAttributeSet inputAttributes = new AAAttributeSet(new AAAttribute[] { new AAAttribute("myAffiliation")});
227
228                         AAAttributeSet outputAttributes =
229                                 new AAAttributeSet(new AAAttribute[] { new AAAttribute("myAffiliation", new Object[] { "member" })
230                         });
231
232                         ar.resolveAttributes(new LocalPrincipal("mytestuser"), "shar.example.edu", inputAttributes);
233                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
234
235                 } catch (AttributeResolverException e) {
236                         fail("Couldn't load attribute resolver: " + e.getMessage());
237                 } catch (MalformedURLException e) {
238                         fail("Error in test specification: " + e.getMessage());
239                 } catch (SAMLException e) {
240                         fail("Error creating SAML Attribute: " + e.getMessage());
241                 }
242         }
243
244         public void testMultipleDataConnectors() {
245
246                 try {
247                         File file = new File("data/resolver8.xml");
248                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
249
250                         AAAttributeSet inputAttributes =
251                                 new AAAttributeSet(
252                                         new AAAttribute[] {
253                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonPrincipalName"),
254                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonAffiliation"),
255                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonEntitlement")});
256
257                         AAAttributeSet outputAttributes =
258                                 new AAAttributeSet(
259                                         new AAAttribute[] {
260                                                 new AAAttribute(
261                                                         "urn:mace:dir:attribute-def:eduPersonPrincipalName",
262                                                         new Object[] { "mytestuser@example.edu" },
263                                                         new ScopedStringValueHandler("example.edu")),
264                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonAffiliation", new Object[] { "member" }),
265                                                 new AAAttribute(
266                                                         "urn:mace:dir:attribute-def:eduPersonEntitlement",
267                                                         new Object[] { "urn:mace:example.edu:exampleEntitlement" })
268                                                 });
269
270                         ar.resolveAttributes(new LocalPrincipal("mytestuser"), "shar.example.edu", inputAttributes);
271
272                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
273
274                 } catch (AttributeResolverException e) {
275                         fail("Couldn't load attribute resolver: " + e.getMessage());
276                 } catch (MalformedURLException e) {
277                         fail("Error in test specification: " + e.getMessage());
278                 } catch (SAMLException e) {
279                         fail("Error creating SAML Attribute: " + e.getMessage());
280                 }
281         }
282         
283         public void testAttributeDependency() {
284
285                 try {
286                         File file = new File("data/resolver9.xml");
287                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
288
289                         AAAttributeSet inputAttributes =
290                                 new AAAttributeSet(
291                                         new AAAttribute[] { new AAAttribute("urn:mace:dir:attribute-def:eduPersonScopedAffiliation")});
292
293                         AAAttributeSet outputAttributes =
294                                 new AAAttributeSet(
295                                         new AAAttribute[] {
296                                                  new AAAttribute(
297                                                         "urn:mace:dir:attribute-def:eduPersonScopedAffiliation",
298                                                         new Object[] { "member@example.edu" },
299                                                         new ScopedStringValueHandler("example.edu"))
300                                                 });
301
302                         ar.resolveAttributes(new LocalPrincipal("mytestuser"), "shar.example.edu", inputAttributes);
303
304                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
305
306                 } catch (AttributeResolverException e) {
307                         fail("Couldn't load attribute resolver: " + e.getMessage());
308                 } catch (MalformedURLException e) {
309                         fail("Error in test specification: " + e.getMessage());
310                 } catch (SAMLException e) {
311                         fail("Error creating SAML Attribute: " + e.getMessage());
312                 }
313         }
314         
315         public void testMisLabeledDataConnector() {
316
317                 try {
318                         File file = new File("data/resolver11.xml");
319                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
320
321                         AAAttributeSet inputAttributes =
322                                 new AAAttributeSet(
323                                         new AAAttribute[] { new AAAttribute("urn:mace:dir:attribute-def:eduPersonScopedAffiliation")});
324
325                         AAAttributeSet outputAttributes = new AAAttributeSet();
326
327                         ar.resolveAttributes(new LocalPrincipal("mytestuser"), "shar.example.edu", inputAttributes);
328
329                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
330
331                 } catch (AttributeResolverException e) {
332                         fail("Couldn't load attribute resolver: " + e.getMessage());
333                 } catch (MalformedURLException e) {
334                         fail("Error in test specification: " + e.getMessage());
335                 } catch (SAMLException e) {
336                         fail("Error creating SAML Attribute: " + e.getMessage());
337                 }
338         }
339
340         public void testMisLabeledAttributeDefinition() {
341
342                 try {
343                         File file = new File("data/resolver10.xml");
344                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
345
346                         AAAttributeSet inputAttributes =
347                                 new AAAttributeSet(
348                                         new AAAttribute[] { new AAAttribute("urn:mace:dir:attribute-def:eduPersonScopedAffiliation")});
349
350                         AAAttributeSet outputAttributes = new AAAttributeSet();
351
352                         ar.resolveAttributes(new LocalPrincipal("mytestuser"), "shar.example.edu", inputAttributes);
353
354                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
355                 } catch (ClassCastException e) {
356                         fail("Failed to detect that an Attribute Definition was mislabeled as a Data Connector: " + e.getMessage());
357                 } catch (AttributeResolverException e) {
358                         fail("Couldn't load attribute resolver: " + e.getMessage());
359                 } catch (MalformedURLException e) {
360                         fail("Error in test specification: " + e.getMessage());
361                 } catch (SAMLException e) {
362                         fail("Error creating SAML Attribute: " + e.getMessage());
363                 }
364         }
365         public void testMultiLevelAttributeDependency() {
366
367                 try {
368                         File file = new File("data/resolver12.xml");
369                         AttributeResolver ar = new AttributeResolver(file.toURL().toString());
370
371                         AAAttributeSet inputAttributes =
372                                 new AAAttributeSet(
373                                         new AAAttribute[] {
374                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonScopedAffiliation"),
375                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonAffiliation"),
376                                                 new AAAttribute("urn:mace:shibboleth:test:eduPersonAffiliation")});
377
378                         AAAttributeSet outputAttributes =
379                                 new AAAttributeSet(
380                                         new AAAttribute[] {
381                                                 new AAAttribute(
382                                                         "urn:mace:dir:attribute-def:eduPersonScopedAffiliation",
383                                                         new Object[] { "member@example.edu" },
384                                                         new ScopedStringValueHandler("example.edu")),
385                                                 new AAAttribute("urn:mace:dir:attribute-def:eduPersonAffiliation", new Object[] { "member" }),
386                                                 new AAAttribute("urn:mace:shibboleth:test:eduPersonAffiliation", new Object[] { "member" })
387                                         });
388
389                         ar.resolveAttributes(new LocalPrincipal("mytestuser"), "shar.example.edu", inputAttributes);
390
391                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
392
393                 } catch (AttributeResolverException e) {
394                         fail("Couldn't load attribute resolver: " + e.getMessage());
395                 } catch (MalformedURLException e) {
396                         fail("Error in test specification: " + e.getMessage());
397                 } catch (SAMLException e) {
398                         fail("Error creating SAML Attribute: " + e.getMessage());
399                 }
400         }
401
402
403 }