Update to sync with interface.
[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.Credential;
72 import edu.internet2.middleware.shibboleth.common.IdentityProvider;
73 import edu.internet2.middleware.shibboleth.common.NameIdentifierMappingException;
74 import edu.internet2.middleware.shibboleth.common.NameMapper;
75 import edu.internet2.middleware.shibboleth.common.ServiceProvider;
76 import edu.internet2.middleware.shibboleth.hs.HSNameIdentifierMapping;
77 import edu.internet2.middleware.shibboleth.hs.HSNameMapper;
78
79 /**
80  * Validation suite for the <code>NameMapper</code>.
81  * 
82  * @author Walter Hoehn(wassa@columbia.edu)
83  */
84
85 public class NameMapperTests extends TestCase {
86
87         private DOMParser parser = new DOMParser();
88
89         public NameMapperTests(String name) {
90                 super(name);
91                 BasicConfigurator.resetConfiguration();
92                 BasicConfigurator.configure();
93                 Logger.getRootLogger().setLevel(Level.OFF);
94         }
95
96         public static void main(String[] args) {
97                 junit.textui.TestRunner.run(NameMapperTests.class);
98                 BasicConfigurator.configure();
99                 Logger.getRootLogger().setLevel(Level.OFF);
100         }
101
102         protected void setUp() throws Exception {
103                 super.setUp();
104                 try {
105
106                         //TODO turn on validation
107                         parser.setFeature("http://xml.org/sax/features/validation", true);
108                         parser.setFeature("http://apache.org/xml/features/validation/schema", true);
109                         parser.setEntityResolver(new EntityResolver() {
110                                 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
111
112                                         if (systemId.endsWith("namemapper.xsd")) {
113                                                 InputStream stream;
114                                                 try {
115                                                         stream = new FileInputStream("src/schemas/namemapper.xsd");
116                                                         if (stream != null) {
117                                                                 return new InputSource(stream);
118                                                         }
119                                                         throw new SAXException("Could not load entity: Null input stream");
120                                                 } catch (FileNotFoundException e) {
121                                                         throw new SAXException("Could not load entity: " + e);
122                                                 }
123                                         } else {
124                                                 return null;
125                                         }
126                                 }
127                         });
128
129                         parser.setErrorHandler(new ErrorHandler() {
130                                 public void error(SAXParseException arg0) throws SAXException {
131                                         throw new SAXException("Error parsing xml file: " + arg0);
132                                 }
133                                 public void fatalError(SAXParseException arg0) throws SAXException {
134                                         throw new SAXException("Error parsing xml file: " + arg0);
135                                 }
136                                 public void warning(SAXParseException arg0) throws SAXException {
137                                         throw new SAXException("Error parsing xml file: " + arg0);
138                                 }
139                         });
140                 } catch (Exception e) {
141                         fail("Failed to setup xml parser: " + e);
142                 }
143         }
144         public void testCryptoMapping() {
145
146                 try {
147
148                         HSNameMapper nameMapper = new HSNameMapper();
149
150                         File file = new File("data/handle.jks");
151
152                         String rawConfig =
153                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
154                                         + "<NameMapping xmlns=\"urn:mace:shibboleth:namemapper:1.0\""
155                                         + "             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
156                                         + "             xsi:schemaLocation=\"urn:mace:shibboleth:namemapper:1.0 namemapper.xsd\" "
157                                         + "                     id=\"cryptotest\" format=\"urn:mace:shibboleth:1.0:nameIdentifier\" "
158                                         + "                     type=\"CryptoHandleGenerator\" handleTTL=\"1800\">"
159                                         + "             <KeyStorePath>"
160                                         + file.toURL().toString()
161                                         + "</KeyStorePath>"
162                                         + "             <KeyStorePassword>shibhs</KeyStorePassword>"
163                                         + "             <KeyStoreKeyAlias>handlekey</KeyStoreKeyAlias>"
164                                         + "             <KeyStoreKeyPassword>shibhs</KeyStoreKeyPassword>"
165                                         + "     </NameMapping>";
166
167                         parser.parse(new InputSource(new StringReader(rawConfig)));
168                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
169
170                         SAMLNameIdentifier nameId =
171                                 nameMapper.getNameIdentifierName(
172                                         "cryptotest",
173                                         new AuthNPrincipal("testprincipal"),
174                                         new BasicServiceProvider(),
175                                         new BasicIdentityProvider("urn-x:testid"));
176
177                         AuthNPrincipal principal =
178                                 nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
179                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
180
181                 } catch (MalformedURLException e) {
182                         fail("Error in test specification: " + e.getMessage());
183                 } catch (NameIdentifierMappingException e) {
184                         fail("Error exercising NameMaper: " + e.getMessage());
185                 } catch (Exception e) {
186                         fail("Error exercising NameMaper: " + e.getMessage());
187                 }
188
189         }
190         public void testDefaultConfig() {
191
192                 try {
193
194                         HSNameMapper nameMapper = new HSNameMapper();
195
196                         SAMLNameIdentifier nameId =
197                                 nameMapper.getNameIdentifierName(
198                                         null,
199                                         new AuthNPrincipal("testprincipal"),
200                                         new BasicServiceProvider(),
201                                         new BasicIdentityProvider("urn-x:testid"));
202
203                         AuthNPrincipal principal =
204                                 nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
205
206                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
207
208                 } catch (NameIdentifierMappingException e) {
209                         fail("Error exercising NameMaper: " + e.getMessage());
210                 } catch (Exception e) {
211                         fail("Error exercising NameMaper: " + e.getMessage());
212                 }
213         }
214
215         public void testDefaultingId() {
216
217                 try {
218
219                         HSNameMapper nameMapper = new HSNameMapper();
220
221                         File file = new File("data/handle.jks");
222
223                         String rawConfig =
224                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
225                                         + "<NameMapping xmlns=\"urn:mace:shibboleth:namemapper:1.0\""
226                                         + "             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
227                                         + "             xsi:schemaLocation=\"urn:mace:shibboleth:namemapper:1.0 namemapper.xsd\" "
228                                         + "                     format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
229                                         + "             type=\"CryptoHandleGenerator\" handleTTL=\"1800\">"
230                                         + "             <KeyStorePath>"
231                                         + file.toURL().toString()
232                                         + "</KeyStorePath>"
233                                         + "             <KeyStorePassword>shibhs</KeyStorePassword>"
234                                         + "             <KeyStoreKeyAlias>handlekey</KeyStoreKeyAlias>"
235                                         + "             <KeyStoreKeyPassword>shibhs</KeyStoreKeyPassword>"
236                                         + "     </NameMapping>";
237
238                         parser.parse(new InputSource(new StringReader(rawConfig)));
239                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
240
241                         SAMLNameIdentifier nameId =
242                                 nameMapper.getNameIdentifierName(
243                                         null,
244                                         new AuthNPrincipal("testprincipal"),
245                                         new BasicServiceProvider(),
246                                         new BasicIdentityProvider("urn-x:testid"));
247
248                         AuthNPrincipal principal =
249                                 nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
250
251                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
252
253                         HSNameIdentifierMapping nameMapping = nameMapper.getNameIdentifierMappingById(null);
254                         if (!(nameMapping instanceof CryptoShibHandle)) {
255                                 fail("HSNameMapper defaulted to incorrect name mapping.");
256                         }
257
258                 } catch (NameIdentifierMappingException e) {
259                         fail("Error exercising NameMaper: " + e.getMessage());
260                 } catch (Exception e) {
261                         fail("Error exercising NameMaper: " + e.getMessage());
262                 }
263         }
264
265         public void testDefaultingAmbiguousId() {
266
267                 try {
268
269                         HSNameMapper nameMapper = new HSNameMapper();
270
271                         File file = new File("data/handle.jks");
272
273                         String rawConfig =
274                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
275                                         + "<NameMapping xmlns=\"urn:mace:shibboleth:namemapper:1.0\""
276                                         + "             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
277                                         + "             xsi:schemaLocation=\"urn:mace:shibboleth:namemapper:1.0 namemapper.xsd\" "
278                                         + "                     format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
279                                         + "             type=\"CryptoHandleGenerator\" handleTTL=\"1800\">"
280                                         + "             <KeyStorePath>"
281                                         + file.toURL().toString()
282                                         + "</KeyStorePath>"
283                                         + "             <KeyStorePassword>shibhs</KeyStorePassword>"
284                                         + "             <KeyStoreKeyAlias>handlekey</KeyStoreKeyAlias>"
285                                         + "             <KeyStoreKeyPassword>shibhs</KeyStoreKeyPassword>"
286                                         + "     </NameMapping>";
287
288                         parser.parse(new InputSource(new StringReader(rawConfig)));
289                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
290
291                         String rawConfig2 =
292                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
293                                         + "<NameMapping xmlns=\"urn:mace:shibboleth:namemapper:1.0\""
294                                         + "             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
295                                         + "             xsi:schemaLocation=\"urn:mace:shibboleth:namemapper:1.0 namemapper.xsd\" "
296                                         + "                     format=\"urn-x:testNameIdentifier\""
297                                         + "             type=\"CryptoHandleGenerator\" handleTTL=\"1800\">"
298                                         + "             <KeyStorePath>"
299                                         + file.toURL().toString()
300                                         + "</KeyStorePath>"
301                                         + "             <KeyStorePassword>shibhs</KeyStorePassword>"
302                                         + "             <KeyStoreKeyAlias>handlekey</KeyStoreKeyAlias>"
303                                         + "             <KeyStoreKeyPassword>shibhs</KeyStoreKeyPassword>"
304                                         + "     </NameMapping>";
305
306                         parser.parse(new InputSource(new StringReader(rawConfig2)));
307
308                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
309
310                         nameMapper.getNameIdentifierName(
311                                 null,
312                                 new AuthNPrincipal("testprincipal"),
313                                 new BasicServiceProvider(),
314                                 new BasicIdentityProvider("urn-x:testid"));
315
316                         fail("HSNameMapper defaulted to incorrect name mapping.");
317
318                         //This is only a failure if we don't get this exception
319                 } catch (NameIdentifierMappingException e) {
320
321                 } catch (Exception e) {
322
323                         fail("Error exercising NameMaper: " + e.getMessage());
324                 }
325         }
326         public void testMemoryMapping() {
327
328                 try {
329
330                         HSNameMapper nameMapper = new HSNameMapper();
331
332                         String rawConfig =
333                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
334                                         + "<NameMapping xmlns=\"urn:mace:shibboleth:namemapper:1.0\""
335                                         + "             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
336                                         + "             xsi:schemaLocation=\"urn:mace:shibboleth:namemapper:1.0 namemapper.xsd\" "
337                                         + "                     id=\"memorytest\" "
338                                         + "             format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
339                                         + "             type=\"SharedMemoryShibHandle\" handleTTL=\"1800\"/>";
340
341                         parser.parse(new InputSource(new StringReader(rawConfig)));
342                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
343
344                         SAMLNameIdentifier nameId =
345                                 nameMapper.getNameIdentifierName(
346                                         "memorytest",
347                                         new AuthNPrincipal("testprincipal"),
348                                         new BasicServiceProvider(),
349                                         new BasicIdentityProvider("urn-x:testid"));
350
351                         AuthNPrincipal principal =
352                                 nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
353
354                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
355
356                 } catch (MalformedURLException e) {
357                         fail("Error in test specification: " + e.getMessage());
358                 } catch (NameIdentifierMappingException e) {
359                         fail("Error exercising NameMaper: " + e.getMessage());
360                 } catch (Exception e) {
361                         fail("Error exercising NameMaper: " + e.getMessage());
362                 }
363         }
364
365         public void testPrincipalMapping() {
366
367                 try {
368
369                         NameMapper nameMapper = new NameMapper();
370
371                         String format = "urn-x:test:NameIdFormat1";
372                         String rawConfig =
373                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
374                                         + "<NameMapping xmlns=\"urn:mace:shibboleth:namemapper:1.0\""
375                                         + "             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
376                                         + "             xsi:schemaLocation=\"urn:mace:shibboleth:namemapper:1.0 namemapper.xsd\" "
377                                         + "                     format=\""
378                                         + format
379                                         + "\""
380                                         + "             type=\"Principal\"/>";
381
382                         parser.parse(new InputSource(new StringReader(rawConfig)));
383                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
384
385                         SAMLNameIdentifier nameId = new SAMLNameIdentifier("testprincipal", "urn-x:testid", format);
386                         AuthNPrincipal principal =
387                                 nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
388
389                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
390
391                 } catch (MalformedURLException e) {
392                         fail("Error in test specification: " + e.getMessage());
393                 } catch (NameIdentifierMappingException e) {
394                         fail("Error exercising NameMaper: " + e.getMessage());
395                 } catch (Exception e) {
396                         fail("Error exercising NameMaper: " + e.getMessage());
397                 }
398
399         }
400 }
401
402 class BasicIdentityProvider implements IdentityProvider {
403         String id;
404
405         public BasicIdentityProvider(String id) {
406                 this.id = id;
407         }
408
409         public String getProviderId() {
410                 return id;
411         }
412
413         public Credential getResponseSigningCredential() {
414                 return null;
415         }
416
417         public Credential getAssertionSigningCredential() {
418                 return null;
419         }
420
421 }
422 class BasicServiceProvider implements ServiceProvider {
423
424         public String getProviderId() {
425
426                 return null;
427         }
428
429 }