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: TCPSRequestInfoTest.java,v 1.3 2005/12/01 13:44:39 tanderson Exp $
44   */
45  package org.exolab.jms.net.tcp.connector;
46  
47  import junit.framework.TestCase;
48  
49  import org.exolab.jms.net.socket.SocketRequestInfo;
50  import org.exolab.jms.net.socket.SocketRequestInfoTest;
51  import org.exolab.jms.net.tcp.TCPSRequestInfo;
52  import org.exolab.jms.net.uri.URI;
53  import org.exolab.jms.net.util.Properties;
54  import org.exolab.jms.net.util.SSLProperties;
55  import org.exolab.jms.net.orb.ORB;
56  
57  
58  /***
59   * Tests the {@link TCPSRequestInfo} class.
60   *
61   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
62   * @version $Revision: 1.3 $ $Date: 2005/12/01 13:44:39 $
63   */
64  public class TCPSRequestInfoTest extends TestCase {
65  
66      /***
67       * Construct a new <code>TCPSRequestInfoTest</code>.
68       *
69       * @param name the name of the test to run
70       */
71      public TCPSRequestInfoTest(String name) {
72          super(name);
73      }
74  
75      /***
76       * Tests accessors.
77       * <p/>
78       * NOTE: accessors provided by {@link SocketRequestInfo} are tested in
79       * {@link SocketRequestInfoTest}.
80       *
81       * @throws Exception for any error
82       */
83      public void testAccessors() throws Exception {
84          final String uri = "tcps://localhost:8050";
85          final SSLProperties ssl = new SSLProperties();
86          final boolean clientAuthReqd = true;
87          final boolean clientAuthNotReqd = false;
88  
89          TCPSRequestInfo info = populate(uri, null, true, ssl, clientAuthReqd);
90  
91          assertEquals(ssl, info.getSSLProperties());
92          assertEquals(clientAuthReqd, info.getNeedClientAuth());
93  
94          info.setNeedClientAuth(clientAuthNotReqd);
95          assertEquals(clientAuthNotReqd, info.getNeedClientAuth());
96      }
97  
98      /***
99       * Tests {@link TCPSRequestInfo#equals}.
100      *
101      * @throws Exception for any error
102      */
103     public void testEquals() throws Exception {
104         final String uri = "tcp://localhost:8050";
105         final String alternativeURI = "tcp://foo.org:9090";
106         final boolean bindAll = true;
107         final SSLProperties ssl = new SSLProperties();
108         final boolean clientAuthReqd = true;
109         final boolean clientAuthNotReqd = false;
110 
111         TCPSRequestInfo info1 = populate(uri, alternativeURI, bindAll,
112                                          ssl, clientAuthReqd);
113         TCPSRequestInfo info2 = populate(uri, alternativeURI, bindAll,
114                                          ssl, clientAuthReqd);
115         assertEquals(info1, info2);
116 
117         TCPSRequestInfo info3 = populate(uri, alternativeURI, bindAll,
118                                          null, clientAuthReqd);
119         assertFalse(info1.equals(info3));
120 
121         TCPSRequestInfo info4 = populate(uri, alternativeURI, bindAll,
122                                          ssl, clientAuthNotReqd);
123         assertFalse(info1.equals(info4));
124     }
125 
126     /***
127      * Tests properties.
128      *
129      * @throws Exception for any error
130      */
131     public void testProperties() throws Exception {
132         final String prefix = "org.exolab.jms.net.tcps.";
133         final String uri = "tcps://exolab.org:4040/";
134         final String alternativeHost = "localhost";
135         final boolean bindAll = false;
136         final SSLProperties ssl = new SSLProperties();
137         final boolean clientAuthReqd = true;
138 
139         ssl.setKeyStore("keyStore");
140         ssl.setKeyStorePassword("keyStorePassword");
141         ssl.setKeyStoreType("JKS");
142         ssl.setTrustStore("trustStore");
143         ssl.setTrustStorePassword("trustStorePassword");
144         ssl.setTrustStoreType("PCKS12");
145 
146         Properties properties = new Properties(prefix);
147         TCPSRequestInfo info1 = populate(uri, alternativeHost, bindAll,
148                                          ssl, clientAuthReqd);
149         info1.export(properties);
150 
151         TCPSRequestInfo info2 = new TCPSRequestInfo(
152                 new URI(properties.get(ORB.PROVIDER_URI)),
153                 properties);
154 
155         assertEquals(info1, info2);
156 
157         assertEquals(ssl, info2.getSSLProperties());
158         assertEquals(clientAuthReqd, info2.getNeedClientAuth());
159     }
160 
161     /***
162      * Helper to populate an {@link TCPSRequestInfo}.
163      *
164      * @param uri            the URI
165      * @param alternativeHost the alternative URI
166      * @param bindAll        indicates how socket connections should be
167      *                       accepted, on a multi-homed host
168      * @param ssl            SSL properties
169      * @param needClientAuth if <code>true</code>, the clients must authenticate
170      *                       themselves.
171      * @return a new <code>TCPSRequestInfo</code>
172      * @throws Exception for any error
173      */
174     private TCPSRequestInfo populate(String uri, String alternativeHost,
175                                      boolean bindAll, SSLProperties ssl,
176                                      boolean needClientAuth)
177             throws Exception {
178         TCPSRequestInfo info = new TCPSRequestInfo(new URI(uri));
179         info.setAlternativeHost(alternativeHost);
180         info.setBindAll(bindAll);
181         info.setSSLProperties(ssl);
182         info.setNeedClientAuth(needClientAuth);
183         return info;
184     }
185 
186 }