1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: SSLPropertiesTest.java,v 1.1 2005/05/03 13:46:03 tanderson Exp $
44   */
45  package org.exolab.jms.net.util;
46  
47  import junit.framework.TestCase;
48  
49  
50  /***
51   * Tests the {@link SSLProperties} class.
52   *
53   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
54   * @version $Revision: 1.1 $ $Date: 2005/05/03 13:46:03 $
55   */
56  public class SSLPropertiesTest extends TestCase {
57  
58      final String keyStore = "keyStore";
59      final String keyStorePassword = "keyStorePassword";
60      final String keyStoreType = "JKS";
61      final String trustStore = "trustStore";
62      final String trustStorePassword = "trustStorePassword";
63      final String trustStoreType = "PCKS12";
64      
65  
66      /***
67       * Construct a new <code>SSLPropertiesTest</code>.
68       *
69       * @param name the name of the test to run
70       */
71      public SSLPropertiesTest(String name) {
72          super(name);
73      }
74  
75      /***
76       * Tests accessors.
77       */
78      public void testAccessors() {
79          SSLProperties properties = populate(keyStore, keyStorePassword,
80                                              keyStoreType, trustStore,
81                                              trustStorePassword, trustStoreType);
82  
83          assertEquals(keyStore, properties.getKeyStore());
84          assertEquals(keyStorePassword, properties.getKeyStorePassword());
85          assertEquals(keyStoreType, properties.getKeyStoreType());
86          assertEquals(trustStore, properties.getTrustStore());
87          assertEquals(trustStorePassword, properties.getTrustStorePassword());
88          assertEquals(trustStoreType, properties.getTrustStoreType());
89      }
90  
91      /***
92       * Tests {@link SSLProperties#isEmpty}.
93       */
94      public void testIsEmpty() {
95          SSLProperties properties = new SSLProperties();
96          assertTrue(properties.isEmpty());
97  
98          properties.setKeyStore("foo");
99          assertFalse(properties.isEmpty());
100 
101         properties = new SSLProperties();
102         properties.setKeyStorePassword("bar");
103         assertFalse(properties.isEmpty());
104 
105         properties = new SSLProperties();
106         properties.setKeyStoreType("JKS");
107         assertFalse(properties.isEmpty());
108 
109         properties = new SSLProperties();
110         properties.setTrustStore("foo");
111         assertFalse(properties.isEmpty());
112 
113         properties = new SSLProperties();
114         properties.setTrustStorePassword("bar");
115         assertFalse(properties.isEmpty());
116 
117         properties = new SSLProperties();
118         properties.setTrustStoreType("PCKS12");
119         assertFalse(properties.isEmpty());
120     }
121 
122     /***
123      * Tests {@link SSLProperties#equals}.
124      */
125     public void testEquals() {
126 
127         SSLProperties empty = new SSLProperties();
128         assertEquals(empty, empty);
129 
130         SSLProperties ssl1 = populate(keyStore, keyStorePassword, keyStoreType,
131                                       trustStore, trustStorePassword,
132                                       trustStoreType);
133         assertFalse(ssl1.equals(empty));
134 
135         SSLProperties ssl2 = populate(keyStore, keyStorePassword, keyStoreType,
136                                       trustStore, trustStorePassword,
137                                       trustStoreType);
138         assertEquals(ssl1, ssl2);
139 
140         SSLProperties ssl3 = populate(null, keyStorePassword, keyStoreType,
141                                       trustStore, trustStorePassword,
142                                       trustStoreType);
143         assertFalse(ssl1.equals(ssl3));
144 
145         SSLProperties ssl4 = populate(keyStore, null, keyStoreType,
146                                       trustStore, trustStorePassword,
147                                       trustStoreType);
148         assertFalse(ssl1.equals(ssl4));
149 
150         SSLProperties ssl5 = populate(keyStore, keyStorePassword, null,
151                                       trustStore, trustStorePassword,
152                                       trustStoreType);
153         assertFalse(ssl1.equals(ssl5));
154 
155         SSLProperties ssl6 = populate(keyStore, keyStorePassword,
156                                       keyStoreType, null, trustStorePassword,
157                                       trustStoreType);
158         assertFalse(ssl1.equals(ssl6));
159 
160         SSLProperties ssl7 = populate(keyStore, keyStorePassword, keyStoreType,
161                                       trustStore, null, trustStoreType);
162         assertFalse(ssl1.equals(ssl7));
163 
164         SSLProperties ssl8 = populate(keyStore, keyStorePassword, keyStoreType,
165                                       trustStore, trustStorePassword, null);
166         assertFalse(ssl1.equals(ssl8));
167     }
168 
169     /***
170      * Tests properties.
171      *
172      * @throws Exception for any error
173      */
174     public void testProperties() throws Exception {
175         final String prefix = "org.exolab.jms.net.https.";
176         SSLProperties ssl1 = populate(keyStore, keyStorePassword, keyStoreType,
177                                       trustStore, trustStorePassword,
178                                       trustStoreType);
179 
180         Properties properties = new Properties(prefix);
181         ssl1.export(properties);
182 
183         SSLProperties ssl2 = new SSLProperties(properties);
184         assertEquals(ssl1, ssl2);
185 
186         assertEquals(keyStore, ssl2.getKeyStore());
187         assertEquals(keyStorePassword, ssl2.getKeyStorePassword());
188         assertEquals(keyStoreType, ssl2.getKeyStoreType());
189         assertEquals(trustStore, ssl2.getTrustStore());
190         assertEquals(trustStorePassword, ssl2.getTrustStorePassword());
191         assertEquals(trustStoreType, ssl2.getTrustStoreType());
192     }
193 
194     /***
195      * Helper to populate an {@link SSLProperties}.
196      *
197      * @param keyStore           the keystore
198      * @param keyStorePassword   the keystore password
199      * @param keyStoreType       the keystore type
200      * @param trustStore         the truststore
201      * @param trustStorePassword the truststore password
202      * @param trustStoreType     the truststore type
203      * @return a new <code>SSLProperties</code>
204      */
205     private SSLProperties populate(String keyStore, String keyStorePassword,
206                                    String keyStoreType, String trustStore,
207                                    String trustStorePassword,
208                                    String trustStoreType) {
209         SSLProperties result = new SSLProperties();
210         result.setKeyStore(keyStore);
211         result.setKeyStorePassword(keyStorePassword);
212         result.setKeyStoreType(keyStoreType);
213         result.setTrustStore(trustStore);
214         result.setTrustStorePassword(trustStorePassword);
215         result.setTrustStoreType(trustStoreType);
216         return result;
217     }
218 
219 }