Better handling when attribute PlugIns are mislabeled as DataConnector PlugIns. ...
[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 import java.util.Properties;
55
56 import junit.framework.TestCase;
57
58 import org.apache.log4j.BasicConfigurator;
59 import org.apache.log4j.Level;
60 import org.apache.log4j.Logger;
61 import org.opensaml.SAMLException;
62
63 import sun.security.acl.PrincipalImpl;
64 import edu.internet2.middleware.shibboleth.aa.AAAttribute;
65 import edu.internet2.middleware.shibboleth.aa.AAAttributeSet;
66 import edu.internet2.middleware.shibboleth.aa.attrresolv.provider.ScopedStringValueHandler;
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                         Properties props = new Properties();
100                         File file = new File("data/resolver1.xml");
101                         props.setProperty(
102                                 "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
103                                 file.toURL().toString());
104
105                         AttributeResolver ar = new AttributeResolver(props);
106
107                         AAAttributeSet inputAttributes =
108                                 new AAAttributeSet(
109                                         new AAAttribute[] {
110                                                 new AAAttribute("urn:mace:eduPerson:1.0:eduPersonNickName"),
111                                                 new AAAttribute("urn:mace:eduPerson:1.0:eduPersonEntitlement")});
112
113                         AAAttributeSet outputAttributes =
114                                 new AAAttributeSet(
115                                         new AAAttribute[] {
116                                                  new AAAttribute(
117                                                         "urn:mace:eduPerson:1.0:eduPersonEntitlement",
118                                                         new Object[] { "urn:mace:example.edu:exampleEntitlement" })
119                                                 });
120
121                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
122
123                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
124
125                 } catch (AttributeResolverException e) {
126                         fail("Couldn't load attribute resolver: " + e.getMessage());
127                 } catch (MalformedURLException e) {
128                         fail("Error in test specification: " + e.getMessage());
129                 } catch (SAMLException e) {
130                         fail("Error creating SAML Attribute: " + e.getMessage());
131                 }
132         }
133         
134         public void testSmartScoping() {
135
136                 try {
137                         Properties props = new Properties();
138                         File file = new File("data/resolver2.xml");
139                         props.setProperty(
140                                 "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
141                                 file.toURL().toString());
142
143                         AttributeResolver ar = new AttributeResolver(props);
144
145                         AAAttributeSet inputAttributes =
146                                 new AAAttributeSet(
147                                         new AAAttribute[] {
148                                                 new AAAttribute("urn:mace:eduPerson:1.0:eduPersonPrincipalName"),
149                                                 new AAAttribute("foo")});
150
151                         AAAttributeSet outputAttributes = new AAAttributeSet(new AAAttribute[] {
152                                 //Attribute should have scope appended to connector output
153                                 new AAAttribute(
154                                         "urn:mace:eduPerson:1.0:eduPersonPrincipalName",
155                                         new Object[] { "mytestuser@example.edu" },
156                                         new ScopedStringValueHandler("example.edu")),
157                                 //Attribute should retain scope from connector output
158                                 new AAAttribute(
159                                         "foo",
160                                         new Object[] { "bar@example.com" },
161                                         new ScopedStringValueHandler("example.edu"))
162                                 });
163
164                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
165                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
166
167                 } catch (AttributeResolverException e) {
168                         fail("Couldn't load attribute resolver: " + e.getMessage());
169                 } catch (MalformedURLException e) {
170                         fail("Error in test specification: " + e.getMessage());
171                 } catch (SAMLException e) {
172                         fail("Error creating SAML Attribute: " + e.getMessage());
173                 }
174         }
175         
176         public void testExceptionForNoPlugIns() {
177
178                 try {
179                         Properties props = new Properties();
180                         File file = new File("data/resolver3.xml");
181                         props.setProperty(
182                                 "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
183                                 file.toURL().toString());
184
185                         AttributeResolver ar = new AttributeResolver(props);
186                         fail("Attribute Resolver loaded even when no PlugIns were configured.");
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 testExceptionForNoValidPlugIns() {
195
196                 try {
197                         Properties props = new Properties();
198                         File file = new File("data/resolver4.xml");
199                         props.setProperty(
200                                 "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
201                                 file.toURL().toString());
202
203                         AttributeResolver ar = new AttributeResolver(props);
204                         fail("Attribute Resolver loaded even when no PlugIns were successfully registered.");
205                 } catch (AttributeResolverException e) {
206                         //This exception should be thrown, ignoring
207                 } catch (MalformedURLException e) {
208                         fail("Error in test specification: " + e.getMessage());
209                 }
210         }
211                 
212         public void testFailToLoadCircularDependencies() {
213
214                 try {
215                         Properties props = new Properties();
216                         File file = new File("data/resolver5.xml");
217                         props.setProperty(
218                                 "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
219                                 file.toURL().toString());
220
221                         AttributeResolver ar = new AttributeResolver(props);
222                         fail("Attribute Resolver loaded even when no only PlugIns with circular dependencies were configured.");
223                 } catch (AttributeResolverException e) {
224                         //This exception should be thrown, ignoring
225                 } catch (MalformedURLException e) {
226                         fail("Error in test specification: " + e.getMessage());
227                 }
228         }
229         
230         public void testFailToLoadCircularDependenciesDeeper() {
231
232                 try {
233                         Properties props = new Properties();
234                         File file = new File("data/resolver6.xml");
235                         props.setProperty(
236                                 "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
237                                 file.toURL().toString());
238
239                         AttributeResolver ar = new AttributeResolver(props);
240                         fail("Attribute Resolver loaded even when no only PlugIns with circular dependencies were configured.");
241                 } catch (AttributeResolverException e) {
242                         //This exception should be thrown, ignoring
243                 } catch (MalformedURLException e) {
244                         fail("Error in test specification: " + e.getMessage());
245                 }
246         }
247
248         public void testSourceNameMapping() {
249
250                 try {
251                         Properties props = new Properties();
252                         File file = new File("data/resolver7.xml");
253                         props.setProperty(
254                                 "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
255                                 file.toURL().toString());
256
257                         AttributeResolver ar = new AttributeResolver(props);
258
259                         AAAttributeSet inputAttributes = new AAAttributeSet(new AAAttribute[] { new AAAttribute("myAffiliation")});
260
261                         AAAttributeSet outputAttributes =
262                                 new AAAttributeSet(new AAAttribute[] { new AAAttribute("myAffiliation", new Object[] { "member" })
263                         });
264
265                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
266                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
267
268                 } catch (AttributeResolverException e) {
269                         fail("Couldn't load attribute resolver: " + e.getMessage());
270                 } catch (MalformedURLException e) {
271                         fail("Error in test specification: " + e.getMessage());
272                 } catch (SAMLException e) {
273                         fail("Error creating SAML Attribute: " + e.getMessage());
274                 }
275         }
276
277         public void testMultipleDataConnectors() {
278
279                 try {
280                         Properties props = new Properties();
281                         File file = new File("data/resolver8.xml");
282                         props.setProperty(
283                                 "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
284                                 file.toURL().toString());
285
286                         AttributeResolver ar = new AttributeResolver(props);
287
288                         AAAttributeSet inputAttributes =
289                                 new AAAttributeSet(
290                                         new AAAttribute[] {
291                                                 new AAAttribute("urn:mace:eduPerson:1.0:eduPersonPrincipalName"),
292                                                 new AAAttribute("urn:mace:eduPerson:1.0:eduPersonAffiliation"),
293                                                 new AAAttribute("urn:mace:eduPerson:1.0:eduPersonEntitlement")});
294
295                         AAAttributeSet outputAttributes =
296                                 new AAAttributeSet(
297                                         new AAAttribute[] {
298                                                 new AAAttribute(
299                                                         "urn:mace:eduPerson:1.0:eduPersonPrincipalName",
300                                                         new Object[] { "mytestuser@example.edu" },
301                                                         new ScopedStringValueHandler("example.edu")),
302                                                 new AAAttribute("urn:mace:eduPerson:1.0:eduPersonAffiliation", new Object[] { "member" }),
303                                                 new AAAttribute(
304                                                         "urn:mace:eduPerson:1.0:eduPersonEntitlement",
305                                                         new Object[] { "urn:mace:example.edu:exampleEntitlement" })
306                                                 });
307
308                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
309
310                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
311
312                 } catch (AttributeResolverException e) {
313                         fail("Couldn't load attribute resolver: " + e.getMessage());
314                 } catch (MalformedURLException e) {
315                         fail("Error in test specification: " + e.getMessage());
316                 } catch (SAMLException e) {
317                         fail("Error creating SAML Attribute: " + e.getMessage());
318                 }
319         }
320         
321         public void testAttributeDependency() {
322
323                 try {
324                         Properties props = new Properties();
325                         File file = new File("data/resolver9.xml");
326                         props.setProperty(
327                                 "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
328                                 file.toURL().toString());
329
330                         AttributeResolver ar = new AttributeResolver(props);
331
332                         AAAttributeSet inputAttributes =
333                                 new AAAttributeSet(
334                                         new AAAttribute[] { new AAAttribute("urn:mace:eduPerson:1.0:eduPersonScopedAffiliation")});
335
336                         AAAttributeSet outputAttributes =
337                                 new AAAttributeSet(
338                                         new AAAttribute[] {
339                                                  new AAAttribute(
340                                                         "urn:mace:eduPerson:1.0:eduPersonScopedAffiliation",
341                                                         new Object[] { "member@example.edu" },
342                                                         new ScopedStringValueHandler("example.edu"))
343                                                 });
344
345                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
346
347                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
348
349                 } catch (AttributeResolverException e) {
350                         fail("Couldn't load attribute resolver: " + e.getMessage());
351                 } catch (MalformedURLException e) {
352                         fail("Error in test specification: " + e.getMessage());
353                 } catch (SAMLException e) {
354                         fail("Error creating SAML Attribute: " + e.getMessage());
355                 }
356         }
357         
358         public void testMisLabeledDataConnector() {
359
360                 try {
361                         Properties props = new Properties();
362                         File file = new File("data/resolver11.xml");
363                         props.setProperty(
364                                 "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
365                                 file.toURL().toString());
366
367                         AttributeResolver ar = new AttributeResolver(props);
368
369                         AAAttributeSet inputAttributes =
370                                 new AAAttributeSet(
371                                         new AAAttribute[] { new AAAttribute("urn:mace:eduPerson:1.0:eduPersonScopedAffiliation")});
372
373                         AAAttributeSet outputAttributes = new AAAttributeSet();
374
375                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
376
377                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
378
379                 } catch (AttributeResolverException e) {
380                         fail("Couldn't load attribute resolver: " + e.getMessage());
381                 } catch (MalformedURLException e) {
382                         fail("Error in test specification: " + e.getMessage());
383                 } catch (SAMLException e) {
384                         fail("Error creating SAML Attribute: " + e.getMessage());
385                 }
386         }
387
388         public void testMisLabeledAttributeDefinition() {
389
390                 try {
391                         Properties props = new Properties();
392                         File file = new File("data/resolver10.xml");
393                         props.setProperty(
394                                 "edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver.ResolverConfig",
395                                 file.toURL().toString());
396
397                         AttributeResolver ar = new AttributeResolver(props);
398
399                         AAAttributeSet inputAttributes =
400                                 new AAAttributeSet(
401                                         new AAAttribute[] { new AAAttribute("urn:mace:eduPerson:1.0:eduPersonScopedAffiliation")});
402
403                         AAAttributeSet outputAttributes = new AAAttributeSet();
404
405                         ar.resolveAttributes(new PrincipalImpl("mytestuser"), "shar.example.edu", inputAttributes);
406
407                         assertEquals("Attribute Resolver returned unexpected attribute set.", inputAttributes, outputAttributes);
408                 } catch (ClassCastException e) {
409                         fail("Failed to detect that an Attribute Definition was mislabeled as a Data Connector: " + e.getMessage());
410                 } catch (AttributeResolverException e) {
411                         fail("Couldn't load attribute resolver: " + e.getMessage());
412                 } catch (MalformedURLException e) {
413                         fail("Error in test specification: " + e.getMessage());
414                 } catch (SAMLException e) {
415                         fail("Error creating SAML Attribute: " + e.getMessage());
416                 }
417         }
418
419
420 }