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