Fix XML in test case.
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / hs / provider / NameMapperTests.java
1 /*
2  * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation
3  * for Advanced Internet Development, Inc. All rights reserved
4  * 
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * 
9  * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 
12  * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution, if any, must include
15  * the following acknowledgment: "This product includes software developed by
16  * the University Corporation for Advanced Internet Development
17  * <http://www.ucaid.edu> Internet2 Project. Alternately, this acknowledegement
18  * may appear in the software itself, if and wherever such third-party
19  * acknowledgments normally appear.
20  * 
21  * Neither the name of Shibboleth nor the names of its contributors, nor
22  * Internet2, nor the University Corporation for Advanced Internet Development,
23  * Inc., nor UCAID may be used to endorse or promote products derived from this
24  * software without specific prior written permission. For written permission,
25  * please contact shibboleth@shibboleth.org
26  * 
27  * Products derived from this software may not be called Shibboleth, Internet2,
28  * UCAID, or the University Corporation for Advanced Internet Development, nor
29  * may Shibboleth appear in their name, without prior written permission of the
30  * University Corporation for Advanced Internet Development.
31  * 
32  * 
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
36  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
37  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
38  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
39  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY
40  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  */
47
48 package edu.internet2.middleware.shibboleth.hs.provider;
49
50 import java.io.File;
51 import java.io.FileInputStream;
52 import java.io.FileNotFoundException;
53 import java.io.InputStream;
54 import java.io.StringReader;
55 import java.net.MalformedURLException;
56
57 import junit.framework.TestCase;
58
59 import org.apache.log4j.BasicConfigurator;
60 import org.apache.log4j.Level;
61 import org.apache.log4j.Logger;
62 import org.apache.xerces.parsers.DOMParser;
63 import org.opensaml.SAMLNameIdentifier;
64 import org.xml.sax.EntityResolver;
65 import org.xml.sax.ErrorHandler;
66 import org.xml.sax.InputSource;
67 import org.xml.sax.SAXException;
68 import org.xml.sax.SAXParseException;
69
70 import edu.internet2.middleware.shibboleth.common.AuthNPrincipal;
71 import edu.internet2.middleware.shibboleth.common.IdentityProvider;
72 import edu.internet2.middleware.shibboleth.common.NameIdentifierMappingException;
73 import edu.internet2.middleware.shibboleth.common.NameMapper;
74 import edu.internet2.middleware.shibboleth.common.ServiceProvider;
75 import edu.internet2.middleware.shibboleth.hs.HSNameIdentifierMapping;
76 import edu.internet2.middleware.shibboleth.hs.HSNameMapper;
77
78 /**
79  * Validation suite for the <code>NameMapper</code>.
80  * 
81  * @author Walter Hoehn(wassa@columbia.edu)
82  */
83
84 public class NameMapperTests extends TestCase {
85
86         private DOMParser parser = new DOMParser();
87
88         public NameMapperTests(String name) {
89                 super(name);
90                 BasicConfigurator.resetConfiguration();
91                 BasicConfigurator.configure();
92                 Logger.getRootLogger().setLevel(Level.OFF);
93         }
94
95         public static void main(String[] args) {
96                 junit.textui.TestRunner.run(NameMapperTests.class);
97                 BasicConfigurator.configure();
98                 Logger.getRootLogger().setLevel(Level.OFF);
99         }
100
101         protected void setUp() throws Exception {
102                 super.setUp();
103                 try {
104
105                         //TODO turn on validation
106                         parser.setFeature("http://xml.org/sax/features/validation", true);
107                         parser.setFeature("http://apache.org/xml/features/validation/schema", true);
108                         parser.setEntityResolver(new EntityResolver() {
109                                 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
110
111                                         if (systemId.endsWith("namemapper.xsd")) {
112                                                 InputStream stream;
113                                                 try {
114                                                         stream = new FileInputStream("src/schemas/namemapper.xsd");
115                                                         if (stream != null) {
116                                                                 return new InputSource(stream);
117                                                         }
118                                                         throw new SAXException("Could not load entity: Null input stream");
119                                                 } catch (FileNotFoundException e) {
120                                                         throw new SAXException("Could not load entity: " + e);
121                                                 }
122                                         } else {
123                                                 return null;
124                                         }
125                                 }
126                         });
127
128                         parser.setErrorHandler(new ErrorHandler() {
129                                 public void error(SAXParseException arg0) throws SAXException {
130                                         throw new SAXException("Error parsing xml file: " + arg0);
131                                 }
132                                 public void fatalError(SAXParseException arg0) throws SAXException {
133                                         throw new SAXException("Error parsing xml file: " + arg0);
134                                 }
135                                 public void warning(SAXParseException arg0) throws SAXException {
136                                         throw new SAXException("Error parsing xml file: " + arg0);
137                                 }
138                         });
139                 } catch (Exception e) {
140                         fail("Failed to setup xml parser: " + e);
141                 }
142         }
143         public void testCryptoMapping() {
144
145                 try {
146
147                         HSNameMapper nameMapper = new HSNameMapper();
148
149                         File file = new File("data/handle.jks");
150
151                         String rawConfig =
152                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
153                                         + "<NameMapping xmlns=\"urn:mace:shibboleth:namemapper:1.0\""
154                                         + "             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
155                                         + "             xsi:schemaLocation=\"urn:mace:shibboleth:namemapper:1.0 namemapper.xsd\" "
156                                         + "                     id=\"cryptotest\" format=\"urn:mace:shibboleth:1.0:nameIdentifier\" "
157                                         + "                     type=\"CryptoHandleGenerator\" handleTTL=\"1800\">"
158                                         + "             <KeyStorePath>"
159                                         + file.toURL().toString()
160                                         + "</KeyStorePath>"
161                                         + "             <KeyStorePassword>shibhs</KeyStorePassword>"
162                                         + "             <KeyStoreKeyAlias>handlekey</KeyStoreKeyAlias>"
163                                         + "             <KeyStoreKeyPassword>shibhs</KeyStoreKeyPassword>"
164                                         + "     </NameMapping>";
165
166                         parser.parse(new InputSource(new StringReader(rawConfig)));
167                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
168
169                         SAMLNameIdentifier nameId =
170                                 nameMapper.getNameIdentifierName(
171                                         "cryptotest",
172                                         new AuthNPrincipal("testprincipal"),
173                                         new BasicServiceProvider(),
174                                         new BasicIdentityProvider("urn-x:testid"));
175
176                         AuthNPrincipal principal =
177                                 nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
178                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
179
180                 } catch (MalformedURLException e) {
181                         fail("Error in test specification: " + e.getMessage());
182                 } catch (NameIdentifierMappingException e) {
183                         fail("Error exercising NameMaper: " + e.getMessage());
184                 } catch (Exception e) {
185                         fail("Error exercising NameMaper: " + e.getMessage());
186                 }
187
188         }
189         public void testDefaultConfig() {
190
191                 try {
192
193                         HSNameMapper nameMapper = new HSNameMapper();
194
195                         SAMLNameIdentifier nameId =
196                                 nameMapper.getNameIdentifierName(
197                                         null,
198                                         new AuthNPrincipal("testprincipal"),
199                                         new BasicServiceProvider(),
200                                         new BasicIdentityProvider("urn-x:testid"));
201
202                         AuthNPrincipal principal =
203                                 nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
204
205                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
206
207                 } catch (NameIdentifierMappingException e) {
208                         fail("Error exercising NameMaper: " + e.getMessage());
209                 } catch (Exception e) {
210                         fail("Error exercising NameMaper: " + e.getMessage());
211                 }
212         }
213
214         public void testDefaultingId() {
215
216                 try {
217
218                         HSNameMapper nameMapper = new HSNameMapper();
219
220                         File file = new File("data/handle.jks");
221
222                         String rawConfig =
223                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
224                                         + "<NameMapping xmlns=\"urn:mace:shibboleth:namemapper:1.0\""
225                                         + "             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
226                                         + "             xsi:schemaLocation=\"urn:mace:shibboleth:namemapper:1.0 namemapper.xsd\" "
227                                         + "                     format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
228                                         + "             type=\"CryptoHandleGenerator\" handleTTL=\"1800\">"
229                                         + "             <KeyStorePath>"
230                                         + file.toURL().toString()
231                                         + "</KeyStorePath>"
232                                         + "             <KeyStorePassword>shibhs</KeyStorePassword>"
233                                         + "             <KeyStoreKeyAlias>handlekey</KeyStoreKeyAlias>"
234                                         + "             <KeyStoreKeyPassword>shibhs</KeyStoreKeyPassword>"
235                                         + "     </NameMapping>";
236
237                         parser.parse(new InputSource(new StringReader(rawConfig)));
238                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
239
240                         SAMLNameIdentifier nameId =
241                                 nameMapper.getNameIdentifierName(
242                                         null,
243                                         new AuthNPrincipal("testprincipal"),
244                                         new BasicServiceProvider(),
245                                         new BasicIdentityProvider("urn-x:testid"));
246
247                         AuthNPrincipal principal =
248                                 nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
249
250                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
251
252                         HSNameIdentifierMapping nameMapping = nameMapper.getNameIdentifierMappingById(null);
253                         if (!(nameMapping instanceof CryptoShibHandle)) {
254                                 fail("HSNameMapper defaulted to incorrect name mapping.");
255                         }
256
257                 } catch (NameIdentifierMappingException e) {
258                         fail("Error exercising NameMaper: " + e.getMessage());
259                 } catch (Exception e) {
260                         fail("Error exercising NameMaper: " + e.getMessage());
261                 }
262         }
263
264         public void testDefaultingAmbiguousId() {
265
266                 try {
267
268                         HSNameMapper nameMapper = new HSNameMapper();
269
270                         File file = new File("data/handle.jks");
271
272                         String rawConfig =
273                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
274                                         + "<NameMapping xmlns=\"urn:mace:shibboleth:namemapper:1.0\""
275                                         + "             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
276                                         + "             xsi:schemaLocation=\"urn:mace:shibboleth:namemapper:1.0 namemapper.xsd\" "
277                                         + "                     format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
278                                         + "             type=\"CryptoHandleGenerator\" handleTTL=\"1800\">"
279                                         + "             <KeyStorePath>"
280                                         + file.toURL().toString()
281                                         + "</KeyStorePath>"
282                                         + "             <KeyStorePassword>shibhs</KeyStorePassword>"
283                                         + "             <KeyStoreKeyAlias>handlekey</KeyStoreKeyAlias>"
284                                         + "             <KeyStoreKeyPassword>shibhs</KeyStoreKeyPassword>"
285                                         + "     </NameMapping>";
286
287                         parser.parse(new InputSource(new StringReader(rawConfig)));
288                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
289
290                         String rawConfig2 =
291                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
292                                         + "<NameMapping xmlns=\"urn:mace:shibboleth:namemapper:1.0\""
293                                         + "             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
294                                         + "             xsi:schemaLocation=\"urn:mace:shibboleth:namemapper:1.0 namemapper.xsd\" "
295                                         + "                     format=\"urn-x:testNameIdentifier\""
296                                         + "             type=\"CryptoHandleGenerator\" handleTTL=\"1800\">"
297                                         + "             <KeyStorePath>"
298                                         + file.toURL().toString()
299                                         + "</KeyStorePath>"
300                                         + "             <KeyStorePassword>shibhs</KeyStorePassword>"
301                                         + "             <KeyStoreKeyAlias>handlekey</KeyStoreKeyAlias>"
302                                         + "             <KeyStoreKeyPassword>shibhs</KeyStoreKeyPassword>"
303                                         + "     </NameMapping>";
304
305                         parser.parse(new InputSource(new StringReader(rawConfig2)));
306
307                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
308
309                         nameMapper.getNameIdentifierName(
310                                 null,
311                                 new AuthNPrincipal("testprincipal"),
312                                 new BasicServiceProvider(),
313                                 new BasicIdentityProvider("urn-x:testid"));
314
315                         fail("HSNameMapper defaulted to incorrect name mapping.");
316
317                         //This is only a failure if we don't get this exception
318                 } catch (NameIdentifierMappingException e) {
319
320                 } catch (Exception e) {
321
322                         fail("Error exercising NameMaper: " + e.getMessage());
323                 }
324         }
325         public void testMemoryMapping() {
326
327                 try {
328
329                         HSNameMapper nameMapper = new HSNameMapper();
330
331                         String rawConfig =
332                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
333                                         + "<NameMapping xmlns=\"urn:mace:shibboleth:namemapper:1.0\""
334                                         + "             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
335                                         + "             xsi:schemaLocation=\"urn:mace:shibboleth:namemapper:1.0 namemapper.xsd\" "
336                                         + "                     id=\"memorytest\" "
337                                         + "             format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
338                                         + "             type=\"SharedMemoryShibHandle\" handleTTL=\"1800\"/>";
339
340                         parser.parse(new InputSource(new StringReader(rawConfig)));
341                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
342
343                         SAMLNameIdentifier nameId =
344                                 nameMapper.getNameIdentifierName(
345                                         "memorytest",
346                                         new AuthNPrincipal("testprincipal"),
347                                         new BasicServiceProvider(),
348                                         new BasicIdentityProvider("urn-x:testid"));
349
350                         AuthNPrincipal principal =
351                                 nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
352
353                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
354
355                 } catch (MalformedURLException e) {
356                         fail("Error in test specification: " + e.getMessage());
357                 } catch (NameIdentifierMappingException e) {
358                         fail("Error exercising NameMaper: " + e.getMessage());
359                 } catch (Exception e) {
360                         fail("Error exercising NameMaper: " + e.getMessage());
361                 }
362         }
363
364         public void testPrincipalMapping() {
365
366                 try {
367
368                         NameMapper nameMapper = new NameMapper();
369
370                         String format = "urn-x:test:NameIdFormat1";
371                         String rawConfig =
372                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
373                                         + "<NameMapping xmlns=\"urn:mace:shibboleth:namemapper:1.0\""
374                                         + "             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
375                                         + "             xsi:schemaLocation=\"urn:mace:shibboleth:namemapper:1.0 namemapper.xsd\" "
376                                         + "                     format=\""
377                                         + format
378                                         + "\""
379                                         + "             type=\"Principal\"/>";
380
381                         parser.parse(new InputSource(new StringReader(rawConfig)));
382                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
383
384                         SAMLNameIdentifier nameId = new SAMLNameIdentifier("testprincipal", "urn-x:testid", format);
385                         AuthNPrincipal principal =
386                                 nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
387
388                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
389
390                 } catch (MalformedURLException e) {
391                         fail("Error in test specification: " + e.getMessage());
392                 } catch (NameIdentifierMappingException e) {
393                         fail("Error exercising NameMaper: " + e.getMessage());
394                 } catch (Exception e) {
395                         fail("Error exercising NameMaper: " + e.getMessage());
396                 }
397
398         }
399 }
400
401 class BasicIdentityProvider implements IdentityProvider {
402         String id;
403
404         public BasicIdentityProvider(String id) {
405                 this.id = id;
406         }
407
408         public String getId() {
409                 return id;
410         }
411
412 }
413 class BasicServiceProvider implements ServiceProvider {
414
415 }