b5125829bfbde6b40483c987f7503b8b23218fff
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / hs / provider / HandleRepositoryTests.java
1 /* 
2  * The Shibboleth License, Version 1. 
3  * Copyright (c) 2002 
4  * University Corporation for Advanced Internet Development, Inc. 
5  * All rights reserved
6  * 
7  * 
8  * Redistribution and use in source and binary forms, with or without 
9  * modification, are permitted provided that the following conditions are met:
10  * 
11  * Redistributions of source code must retain the above copyright notice, this 
12  * list of conditions and the following disclaimer.
13  * 
14  * Redistributions in binary form must reproduce the above copyright notice, 
15  * this list of conditions and the following disclaimer in the documentation 
16  * and/or other materials provided with the distribution, if any, must include 
17  * the following acknowledgment: "This product includes software developed by 
18  * the University Corporation for Advanced Internet Development 
19  * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement 
20  * may appear in the software itself, if and wherever such third-party 
21  * acknowledgments normally appear.
22  * 
23  * Neither the name of Shibboleth nor the names of its contributors, nor 
24  * Internet2, nor the University Corporation for Advanced Internet Development, 
25  * Inc., nor UCAID may be used to endorse or promote products derived from this 
26  * software without specific prior written permission. For written permission, 
27  * please contact shibboleth@shibboleth.org
28  * 
29  * Products derived from this software may not be called Shibboleth, Internet2, 
30  * UCAID, or the University Corporation for Advanced Internet Development, nor 
31  * may Shibboleth appear in their name, without prior written permission of the 
32  * University Corporation for Advanced Internet Development.
33  * 
34  * 
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
36  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
38  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK 
39  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. 
40  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY 
41  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, 
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50 package edu.internet2.middleware.shibboleth.hs.provider;
51
52 import java.io.File;
53 import java.net.MalformedURLException;
54 import java.util.Date;
55 import java.util.Properties;
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
63 import edu.internet2.middleware.shibboleth.common.AuthNPrincipal;
64 import edu.internet2.middleware.shibboleth.hs.HandleRepository;
65 import edu.internet2.middleware.shibboleth.hs.HandleRepositoryException;
66 import edu.internet2.middleware.shibboleth.hs.HandleRepositoryFactory;
67
68 /**
69  * Validation suite for the <code>HandleRepository</code> implementations.
70  * 
71  * @ author Walter Hoehn(wassa@columbia.edu)
72  */
73
74 public class HandleRepositoryTests extends TestCase {
75
76         private static Logger log = Logger.getLogger(HandleRepositoryTests.class.getName());
77
78         public HandleRepositoryTests(String name) {
79                 super(name);
80                 BasicConfigurator.resetConfiguration();
81                 BasicConfigurator.configure();
82                 Logger.getRootLogger().setLevel(Level.OFF);
83         }
84
85         public static void main(String[] args) {
86                 junit.textui.TestRunner.run(HandleRepositoryTests.class);
87                 BasicConfigurator.configure();
88                 Logger.getRootLogger().setLevel(Level.OFF);
89         }
90
91         /**
92          * @see junit.framework.TestCase#setUp()
93          */
94         protected void setUp() throws Exception {
95                 super.setUp();
96         }
97
98         public void testBasicCryptoRepository() {
99
100                 try {
101                         Properties props = new Properties();
102                         File file = new File("data/handle.jks");
103
104                         props.setProperty(
105                                 "edu.internet2.middleware.shibboleth.hs.HandleRepository.implementation",
106                                 "edu.internet2.middleware.shibboleth.hs.provider.CryptoHandleRepository");
107                         props.setProperty(
108                                 "edu.internet2.middleware.shibboleth.hs.provider.CryptoHandleRepository.keyStorePath",
109                                 file.toURL().toString());
110                         props.setProperty(
111                                 "edu.internet2.middleware.shibboleth.hs.provider.CryptoHandleRepository.keyStorePassword",
112                                 "shibhs");
113                         props.setProperty(
114                                 "edu.internet2.middleware.shibboleth.hs.provider.CryptoHandleRepository.keyStoreKeyAlias",
115                                 "handleKey");
116                         props.setProperty(
117                                 "edu.internet2.middleware.shibboleth.hs.provider.CryptoHandleRepository.keyStoreKeyPassword",
118                                 "shibhs");
119                         props.setProperty("edu.internet2.middleware.shibboleth.hs.BaseHandleRepository.handleTTL", "1800");
120
121                         HandleRepository repository = HandleRepositoryFactory.getInstance(props);
122
123                         Date beforeGeneration = new Date();
124                         StringBuffer format = new StringBuffer();
125                         String handle = repository.getHandle(new AuthNPrincipal("foo"), format);
126                         Date afterGeneration = new Date();
127
128                         Date beforeMarshalling = new Date();
129                         AuthNPrincipal principal = repository.getPrincipal(handle, format.toString());
130                         assertEquals("Round-trip handle validation failed.", principal.getName(), "foo");
131                         Date afterMarshalling = new Date();
132
133                         log.debug("Before Handle Generation: " + beforeGeneration + "-" + beforeGeneration.getTime());
134                         log.debug("After Handle Generation:  " + afterGeneration + "-" + afterGeneration.getTime());
135                         log.debug("Before Handle Marshalling: " + beforeMarshalling + "-" + beforeMarshalling.getTime());
136                         log.debug("After Handle Marshalling:  " + afterMarshalling + "-" + afterMarshalling.getTime());
137
138                 } catch (MalformedURLException e) {
139                         fail("Error in test specification: " + e.getMessage());
140                 } catch (HandleRepositoryException e) {
141                         fail("Error exercising Handle Repository: " + e.getMessage());
142                 }
143         }
144
145 }