16cc4da7919546ae3f6657d1b4b36d47cddca551
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / aa / arp / ArpTests.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.arp;
51
52 import java.io.FileInputStream;
53 import java.io.InputStream;
54 import java.net.MalformedURLException;
55 import java.net.URI;
56 import java.net.URISyntaxException;
57 import java.net.URL;
58 import java.util.Properties;
59
60 import junit.framework.TestCase;
61
62 import org.apache.log4j.BasicConfigurator;
63 import org.apache.xerces.parsers.DOMParser;
64 import org.xml.sax.InputSource;
65
66 /**
67  * Validation suite for <code>Arp</code> processing.
68  * 
69  * @ author Walter Hoehn(wassa@columbia.edu)
70  */
71
72 public class ArpTests extends TestCase {
73
74         public ArpTests(String name) {
75                 super(name);
76                 BasicConfigurator.configure();
77         }
78
79         public static void main(String[] args) {
80                 junit.textui.TestRunner.run(ArpTests.class);
81                 BasicConfigurator.configure();
82         }
83
84         public void testArpMarshalling() {
85
86                 //Test ARP description
87                 try {
88                         InputStream inStream = new FileInputStream("test/arp1.xml");
89                         DOMParser parser = new DOMParser();
90                         parser.parse(new InputSource(inStream));
91                         Arp arp1 = new Arp();
92                         arp1.marshall(parser.getDocument().getDocumentElement());
93                         assertEquals(
94                                 "ARP Description not marshalled properly",
95                                 arp1.getDescription(),
96                                 "Simplest possible ARP.");
97
98                         //Test Rule description
99                         assertEquals(
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.");
105                 }
106
107                 //Test case where ARP description does not exist
108                 try {
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());
115
116                         //Test case where ARP Rule description does not exist   
117                         assertNull(
118                                 "ARP Rule Description not marshalled properly",
119                                 arp2.getAllRules()[0].getDescription());
120                 } catch (Exception e) {
121                         fail("Failed to marshall ARP.");
122                 }
123
124         }
125
126         public void testMatchingFunctions() {
127
128                 try {
129
130                         /*
131                          * Test Arp Engine function retrieval
132                          */
133
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);
138
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"));
145                         assertNotNull(
146                                 "ArpEngine did not properly load the Resource Tree SHAR function.",
147                                 resourceTreeFunction);
148
149                         /* 
150                          * Test the Exact SHAR function
151                          */
152
153                         assertTrue(
154                                 "Exact SHAR function: false negative",
155                                 exactSharFunction.match("shar.example.edu", "shar.example.edu"));
156                         assertTrue(
157                                 "Exact SHAR function: false negative",
158                                 !exactSharFunction.match("shar.example.edu", "www.example.edu"));
159                         assertTrue(
160                                 "Exact SHAR function: false negative",
161                                 !exactSharFunction.match("example.edu", "shar.example.edu"));
162
163                         //Make sure we properly handle bad input
164                         try {
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
169                         }
170
171                         /*
172                          * Test the Resource Tree function
173                          */
174
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");
179
180                         assertTrue(
181                                 "Resource Tree function: false negative",
182                                 resourceTreeFunction.match("http://www.example.edu/", requestURL1));
183                         assertTrue(
184                                 "Resource Tree function: false positive",
185                                 !resourceTreeFunction.match("https://www.example.edu/", requestURL1));
186                         assertTrue(
187                                 "Resource Tree function: false negative",
188                                 resourceTreeFunction.match("http://www.example.edu:80/", requestURL1));
189                         assertTrue(
190                                 "Resource Tree function: false positive",
191                                 !resourceTreeFunction.match("http://www.example.edu:81/", requestURL1));
192                         assertTrue(
193                                 "Resource Tree function: false negative",
194                                 resourceTreeFunction.match("http://www.example.edu/test/", requestURL1));
195                         assertTrue(
196                                 "Resource Tree function: false negative",
197                                 resourceTreeFunction.match("http://www.example.edu/test/", requestURL2));
198                         assertTrue(
199                                 "Resource Tree function: false negative",
200                                 resourceTreeFunction.match("http://www.example.edu/", requestURL3));
201                         assertTrue(
202                                 "Resource Tree function: false positive",
203                                 !resourceTreeFunction.match("http://www.example.edu/test/", requestURL3));
204                         assertTrue(
205                                 "Resource Tree function: false negative",
206                                 resourceTreeFunction.match("http://www.example.edu/test2/index.html", requestURL3));
207                         assertTrue(
208                                 "Resource Tree function: false negative",
209                                 resourceTreeFunction.match("http://www.example.edu/test2/index.html", requestURL4));
210                         assertTrue(
211                                 "Resource Tree function: false negative",
212                                 resourceTreeFunction.match(
213                                         "http://www.example.edu/test2/index.html?test1=test1",
214                                         requestURL4));
215                         assertTrue(
216                                 "Resource Tree function: false positive",
217                                 !resourceTreeFunction.match(
218                                         "http://www.example.edu/test2/index.html?test1=test1",
219                                         requestURL3));
220
221                         //Make sure we properly handle bad input
222                         try {
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
227                         }
228                         try {
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
233                         }
234
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);
241                 }
242
243         }
244
245         public void testRepositories() {
246
247                 /*
248                  * Test the Factory
249                  */
250
251                 //Make sure we fail if no Repository is specified
252                 Properties props = new Properties();
253                 try {
254                         ArpRepositoryFactory.getInstance(props);
255                 } catch (ArpRepositoryException e) {
256                         //This is supposed to fail
257                 }
258
259                 // Make sure we can create an Arp Repository
260                 props.setProperty(
261                         "edu.internet2.middleware.shibboleth.aa.arp.ArpRepository.implementation",
262                         "edu.internet2.middleware.shibboleth.aa.arp.provider.MemoryArpRepository");
263                 ArpRepository repository = null;
264                 try {
265                         repository = ArpRepositoryFactory.getInstance(props);
266                 } catch (ArpRepositoryException e) {
267                         fail("Failed to create memory-based Arp Repository" + e);
268                 }
269                 assertNotNull("Failed to create memory-based Arp Repository: Factory returned null.", repository);
270
271                 /*
272                  * Exercise the Memory Arp Repository
273                  */
274
275                 //Set/retrieve/remove a Site ARP
276                 Arp siteArp1 = new Arp();
277                 siteArp1.setDescription("Test Site Arp 1.");
278                 try {
279                         repository.update(siteArp1);
280                         assertEquals(
281                                 "Memory Repository does not store and retrieve Site ARPs properly.",
282                                 siteArp1,
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.");
288                 }
289
290                 //Set/retrieve/delete some user ARPs
291                 Arp userArp1 = new Arp();
292                 userArp1.setDescription("Broken User Arp 1.");
293                 try {
294                         repository.update(userArp1);
295                         assertTrue(
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.");
300                 }
301
302                 Arp userArp2 = new Arp(new AAPrincipal("TestPrincipal"));
303                 userArp2.setDescription("Test User Arp 2.");
304                 try {
305                         repository.update(userArp2);
306                         assertEquals(
307                                 "Memory Repository does not store and retrieve User ARPs properly.",
308                                 userArp2,
309                                 repository.getUserPolicy(userArp2.getPrincipal()));
310                         repository.remove(repository.getUserPolicy(userArp2.getPrincipal()));
311                         assertNull(
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.");
316                 }
317
318         }
319
320 }