More name mapping updates.
[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.HSNameMapper;
76
77 /**
78  * Validation suite for the <code>NameMapper</code>.
79  * 
80  * @author Walter Hoehn(wassa@columbia.edu)
81  */
82
83 public class NameMapperTests extends TestCase {
84
85         private static Logger log = Logger.getLogger(NameMapperTests.class.getName());
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.DEBUG);
93         }
94
95         public static void main(String[] args) {
96                 junit.textui.TestRunner.run(NameMapperTests.class);
97                 BasicConfigurator.configure();
98                 Logger.getRootLogger().setLevel(Level.DEBUG);
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", false);
107                         parser.setFeature("http://apache.org/xml/features/validation/schema", false);
108                         parser.setEntityResolver(new EntityResolver() {
109                                 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
110
111                                         if (systemId.endsWith("shibboleth-arp-1.0.xsd")) {
112                                                 InputStream stream;
113                                                 try {
114                                                         stream = new FileInputStream("src/schemas/shibboleth-arp-1.0.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 id=\"cryptotest\" format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
154                                         + "             type=\"CryptoHandleGenerator\" handleTTL=\"1800\">"
155                                         + "             <KeyStorePath>"
156                                         + file.toURL().toString()
157                                         + "</KeyStorePath>"
158                                         + "             <KeyStorePassword>shibhs</KeyStorePassword>"
159                                         + "             <KeyStoreKeyAlias>handlekey</KeyStoreKeyAlias>"
160                                         + "             <KeyStoreKeyPassword>shibhs</KeyStoreKeyPassword>"
161                                         + "     </NameMapping>";
162
163                         parser.parse(new InputSource(new StringReader(rawConfig)));
164                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
165
166                         SAMLNameIdentifier nameId =
167                                 nameMapper.getNameIdentifierName("cryptotest", new AuthNPrincipal("testprincipal"), null, null);
168
169                         AuthNPrincipal principal = nameMapper.getPrincipal(nameId, null, null);
170                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
171
172                 } catch (MalformedURLException e) {
173                         fail("Error in test specification: " + e.getMessage());
174                 } catch (NameIdentifierMappingException e) {
175                         fail("Error exercising NameMaper: " + e.getMessage());
176                 } catch (Exception e) {
177                         fail("Error exercising NameMaper: " + e.getMessage());
178                 }
179
180         }
181         public void testDefaultConfig() {
182
183                 try {
184
185                         HSNameMapper nameMapper = new HSNameMapper();
186 /*
187                         String rawConfig =
188                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
189                                         + "<NameMapping id=\"memorytest\" format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
190                                         + "             type=\"SharedMemoryShibHandle\" handleTTL=\"1800\"/>";
191 */
192                         //parser.parse(new InputSource(new StringReader(rawConfig)));
193                         //nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
194                         
195                         //TODO fix
196                         SAMLNameIdentifier nameId =
197                                 nameMapper.getNameIdentifierName(
198                                         "",
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         public void testMemoryMapping() {
215
216                 try {
217
218                         HSNameMapper nameMapper = new HSNameMapper();
219
220                         String rawConfig =
221                         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
222                         + "<NameMapping id=\"memorytest\" format=\"urn:mace:shibboleth:1.0:nameIdentifier\""
223                         + "             type=\"SharedMemoryShibHandle\" handleTTL=\"1800\"/>";
224
225                         parser.parse(new InputSource(new StringReader(rawConfig)));
226                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
227
228                         SAMLNameIdentifier nameId =
229                         nameMapper.getNameIdentifierName(
230                                         "memorytest",
231                                         new AuthNPrincipal("testprincipal"),
232                                         new BasicServiceProvider(),
233                                         new BasicIdentityProvider("urn-x:testid"));
234
235                         AuthNPrincipal principal =
236                         nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
237
238                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
239
240                 } catch (MalformedURLException e) {
241                         fail("Error in test specification: " + e.getMessage());
242                 } catch (NameIdentifierMappingException e) {
243                         fail("Error exercising NameMaper: " + e.getMessage());
244                 } catch (Exception e) {
245                         fail("Error exercising NameMaper: " + e.getMessage());
246                 }
247         }
248
249         public void testPrincipalMapping() {
250
251                 try {
252
253                         NameMapper nameMapper = new NameMapper();
254
255                         String format = "urn-x:test:NameIdFormat1";
256                         String rawConfig =
257                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
258                                         + "<NameMapping format=\""
259                                         + format
260                                         + "\""
261                                         + "             type=\"Principal\"/>";
262
263                         parser.parse(new InputSource(new StringReader(rawConfig)));
264                         nameMapper.addNameMapping(parser.getDocument().getDocumentElement());
265
266                         SAMLNameIdentifier nameId = new SAMLNameIdentifier("testprincipal", "urn-x:testid", format);
267                         AuthNPrincipal principal =
268                                 nameMapper.getPrincipal(nameId, new BasicServiceProvider(), new BasicIdentityProvider("urn-x:testid"));
269
270                         assertEquals("Round-trip handle validation failed.", principal.getName(), "testprincipal");
271
272                 } catch (MalformedURLException e) {
273                         fail("Error in test specification: " + e.getMessage());
274                 } catch (NameIdentifierMappingException e) {
275                         fail("Error exercising NameMaper: " + e.getMessage());
276                 } catch (Exception e) {
277                         fail("Error exercising NameMaper: " + e.getMessage());
278                 }
279
280         }
281 }
282
283 class BasicIdentityProvider implements IdentityProvider {
284         String id;
285
286         public BasicIdentityProvider(String id) {
287                 this.id = id;
288         }
289
290         public String getId() {
291                 return id;
292         }
293
294 }
295 class BasicServiceProvider implements ServiceProvider {
296
297 }