2 * The Shibboleth License, Version 1.
4 * University Corporation for Advanced Internet Development, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
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.
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
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.
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.
50 package edu.internet2.middleware.shibboleth.aa.arp;
52 import java.io.FileInputStream;
53 import java.io.InputStream;
54 import java.net.MalformedURLException;
56 import java.net.URISyntaxException;
58 import java.util.Properties;
60 import junit.framework.TestCase;
62 import org.apache.log4j.BasicConfigurator;
63 import org.apache.xerces.parsers.DOMParser;
64 import org.xml.sax.InputSource;
67 * Validation suite for <code>Arp</code> processing.
69 * @ author Walter Hoehn(wassa@columbia.edu)
72 public class ArpTests extends TestCase {
74 public ArpTests(String name) {
76 BasicConfigurator.configure();
79 public static void main(String[] args) {
80 junit.textui.TestRunner.run(ArpTests.class);
81 BasicConfigurator.configure();
84 public void testArpMarshalling() {
86 //Test ARP description
88 InputStream inStream = new FileInputStream("test/arp1.xml");
89 DOMParser parser = new DOMParser();
90 parser.parse(new InputSource(inStream));
92 arp1.marshall(parser.getDocument().getDocumentElement());
94 "ARP Description not marshalled properly",
95 arp1.getDescription(),
96 "Simplest possible ARP.");
98 //Test Rule description
100 "ARP Rule Description not marshalled properly",
101 arp1.getAllRules()[0].getDescription(),
102 "Example Rule Description.");
103 } catch (Exception e) {
104 fail("Failed to marshall ARP.");
107 //Test case where ARP description does not exist
109 InputStream inStream = new FileInputStream("test/arp2.xml");
110 DOMParser parser = new DOMParser();
111 parser.parse(new InputSource(inStream));
112 Arp arp2 = new Arp();
113 arp2.marshall(parser.getDocument().getDocumentElement());
114 assertNull("ARP Description not marshalled properly", arp2.getDescription());
116 //Test case where ARP Rule description does not exist
118 "ARP Rule Description not marshalled properly",
119 arp2.getAllRules()[0].getDescription());
120 } catch (Exception e) {
121 fail("Failed to marshall ARP.");
126 public void testMatchingFunctions() {
131 * Test Arp Engine function retrieval
134 //Lookup a function that doesn't exist
135 MatchFunction noFunction =
136 ArpEngine.lookupMatchFunction(new URI("urn:mace:shibboleth:arp:matchFunction:dummy"));
137 assertNull("ArpEngine did not return null on dummy function.", noFunction);
139 //Lookup some real functions
140 MatchFunction exactSharFunction =
141 ArpEngine.lookupMatchFunction(new URI("urn:mace:shibboleth:arp:matchFunction:exactShar"));
142 assertNotNull("ArpEngine did not properly load the Exact SHAR function.", exactSharFunction);
143 MatchFunction resourceTreeFunction =
144 ArpEngine.lookupMatchFunction(new URI("urn:mace:shibboleth:arp:matchFunction:resourceTree"));
146 "ArpEngine did not properly load the Resource Tree SHAR function.",
147 resourceTreeFunction);
150 * Test the Exact SHAR function
154 "Exact SHAR function: false negative",
155 exactSharFunction.match("shar.example.edu", "shar.example.edu"));
157 "Exact SHAR function: false negative",
158 !exactSharFunction.match("shar.example.edu", "www.example.edu"));
160 "Exact SHAR function: false negative",
161 !exactSharFunction.match("example.edu", "shar.example.edu"));
163 //Make sure we properly handle bad input
165 exactSharFunction.match(null, null);
166 fail("Exact SHAR function seems to take improper input without throwing an exception.");
167 } catch (ArpException ie) {
168 //This is supposed to fail
172 * Test the Resource Tree function
175 URL requestURL1 = new URL("http://www.example.edu/test/");
176 URL requestURL2 = new URL("http://www.example.edu/test/index.html");
177 URL requestURL3 = new URL("http://www.example.edu/test2/index.html");
178 URL requestURL4 = new URL("http://www.example.edu/test2/index.html?test1=test1");
181 "Resource Tree function: false negative",
182 resourceTreeFunction.match("http://www.example.edu/", requestURL1));
184 "Resource Tree function: false positive",
185 !resourceTreeFunction.match("https://www.example.edu/", requestURL1));
187 "Resource Tree function: false negative",
188 resourceTreeFunction.match("http://www.example.edu:80/", requestURL1));
190 "Resource Tree function: false positive",
191 !resourceTreeFunction.match("http://www.example.edu:81/", requestURL1));
193 "Resource Tree function: false negative",
194 resourceTreeFunction.match("http://www.example.edu/test/", requestURL1));
196 "Resource Tree function: false negative",
197 resourceTreeFunction.match("http://www.example.edu/test/", requestURL2));
199 "Resource Tree function: false negative",
200 resourceTreeFunction.match("http://www.example.edu/", requestURL3));
202 "Resource Tree function: false positive",
203 !resourceTreeFunction.match("http://www.example.edu/test/", requestURL3));
205 "Resource Tree function: false negative",
206 resourceTreeFunction.match("http://www.example.edu/test2/index.html", requestURL3));
208 "Resource Tree function: false negative",
209 resourceTreeFunction.match("http://www.example.edu/test2/index.html", requestURL4));
211 "Resource Tree function: false negative",
212 resourceTreeFunction.match(
213 "http://www.example.edu/test2/index.html?test1=test1",
216 "Resource Tree function: false positive",
217 !resourceTreeFunction.match(
218 "http://www.example.edu/test2/index.html?test1=test1",
221 //Make sure we properly handle bad input
223 resourceTreeFunction.match(null, null);
224 fail("Resource Tree function seems to take improper input without throwing an exception.");
225 } catch (ArpException ie) {
226 //This is supposed to fail
229 resourceTreeFunction.match("Test", "Test");
230 fail("Resource Tree function seems to take improper input without throwing an exception.");
231 } catch (ArpException ie) {
232 //This is supposed to fail
235 } catch (ArpException e) {
236 fail("Encountered a problem loading match function: " + e);
237 } catch (URISyntaxException e) {
238 fail("Unable to create URI from test string.");
239 } catch (MalformedURLException e) {
240 fail("Couldn't create test URLs: " + e);
245 public void testRepositories() {
251 //Make sure we fail if no Repository is specified
252 Properties props = new Properties();
254 ArpRepositoryFactory.getInstance(props);
255 } catch (ArpRepositoryException e) {
256 //This is supposed to fail
259 // Make sure we can create an Arp Repository
261 "edu.internet2.middleware.shibboleth.aa.arp.ArpRepository.implementation",
262 "edu.internet2.middleware.shibboleth.aa.arp.provider.MemoryArpRepository");
263 ArpRepository repository = null;
265 repository = ArpRepositoryFactory.getInstance(props);
266 } catch (ArpRepositoryException e) {
267 fail("Failed to create memory-based Arp Repository" + e);
269 assertNotNull("Failed to create memory-based Arp Repository: Factory returned null.", repository);
272 * Exercise the Memory Arp Repository
275 //Set/retrieve/remove a Site ARP
276 Arp siteArp1 = new Arp();
277 siteArp1.setDescription("Test Site Arp 1.");
279 repository.update(siteArp1);
281 "Memory Repository does not store and retrieve Site ARPs properly.",
283 repository.getSitePolicy());
284 repository.remove(repository.getSitePolicy());
285 assertNull("Memorty Repository does not properly delete Site ARPs.", repository.getSitePolicy());
286 } catch (ArpRepositoryException e) {
287 fail("Error adding Site ARP to Memory Repository.");
290 //Set/retrieve/delete some user ARPs
291 Arp userArp1 = new Arp();
292 userArp1.setDescription("Broken User Arp 1.");
294 repository.update(userArp1);
296 "Memory Repository does not store and retrieve User ARPs properly.",
297 (!userArp1.equals(repository.getUserPolicy(userArp1.getPrincipal()))));
298 } catch (ArpRepositoryException e) {
299 fail("Error adding User ARP to Memory Repository.");
302 Arp userArp2 = new Arp(new AAPrincipal("TestPrincipal"));
303 userArp2.setDescription("Test User Arp 2.");
305 repository.update(userArp2);
307 "Memory Repository does not store and retrieve User ARPs properly.",
309 repository.getUserPolicy(userArp2.getPrincipal()));
310 repository.remove(repository.getUserPolicy(userArp2.getPrincipal()));
312 "Memorty Repository does not properly delete User ARPs.",
313 repository.getUserPolicy(userArp2.getPrincipal()));
314 } catch (ArpRepositoryException e) {
315 fail("Error adding User ARP to Memory Repository.");