KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > tcp > SslTransportFactoryTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.transport.tcp;
20
21 import junit.framework.TestCase;
22 import org.apache.activemq.openwire.OpenWireFormat;
23
24 import java.io.IOException JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 public class SslTransportFactoryTest extends TestCase {
30     private SslTransportFactory factory;
31     private boolean verbose;
32
33     protected void setUp() throws Exception JavaDoc {
34         factory = new SslTransportFactory();
35     }
36
37     protected void tearDown() throws Exception JavaDoc {
38         super.tearDown();
39     }
40
41     public void testBindServerOptions() throws IOException JavaDoc {
42
43         SslTransportServer sslTransportServer = null;
44
45         for (int i = 0; i < 4; ++i) {
46             final boolean wantClientAuth = ((i & 0x1) == 1);
47             final boolean needClientAuth = ((i & 0x2) == 1);
48
49             String JavaDoc options = "wantClientAuth=" + (wantClientAuth ? "true" : "false") +
50                     "&needClientAuth=" + (needClientAuth ? "true" : "false");
51
52             try {
53                 sslTransportServer = (SslTransportServer)
54                         factory.doBind("brokerId", new URI JavaDoc("ssl://localhost:61616?" + options));
55             }
56             catch (Exception JavaDoc e) {
57                 fail("Unable to bind to address: " + e.getMessage());
58             }
59
60             assertEquals("Created ServerSocket did not have correct wantClientAuth status.",
61                     sslTransportServer.getWantClientAuth(), wantClientAuth);
62
63             assertEquals("Created ServerSocket did not have correct needClientAuth status.",
64                     sslTransportServer.getNeedClientAuth(), needClientAuth);
65
66             try {
67                 sslTransportServer.stop();
68             }
69             catch (Exception JavaDoc e) {
70                 fail("Unable to stop TransportServer: " + e.getMessage());
71             }
72         }
73     }
74
75     private int getMthNaryDigit(int number, int digitIdx, int numBase) {
76         return (number / ((int) Math.pow(numBase, digitIdx))) % numBase;
77     }
78
79     public void testCompositeConfigure() throws IOException JavaDoc {
80         // The 5 options being tested.
81
int optionSettings[] = new int[5];
82
83         String JavaDoc optionNames[] = {
84                 "wantClientAuth",
85                 "needClientAuth",
86                 "socket.wantClientAuth",
87                 "socket.needClientAuth",
88                 "socket.useClientMode"
89         };
90
91         // Using a trinary interpretation of i to set all possible values of stub options for socket and transport.
92
// 2 transport options, 3 socket options, 3 settings for each option => 3^5 = 243 combos.
93
for (int i = 0; i < 243; ++i) {
94             Map JavaDoc options = new HashMap JavaDoc();
95
96             for (int j = 0; j < 5; ++j) {
97                 // -1 since the option range is [-1,1], not [0,2].
98
optionSettings[j] = getMthNaryDigit(i, j, 3) - 1;
99
100                 if (optionSettings[j] != -1) {
101                     options.put(optionNames[j], (optionSettings[j] == 1 ? "true" : "false"));
102                 }
103             }
104
105             StubSSLSocket socketStub = new StubSSLSocket(null);
106             StubSslTransport transport = null;
107
108             try {
109                 transport = new StubSslTransport(null, socketStub);
110             }
111             catch (Exception JavaDoc e) {
112                 fail("Unable to create StubSslTransport: " + e.getMessage());
113             }
114
115             if (verbose) {
116                 System.out.println();
117                 System.out.println("Iteration: " + i);
118                 System.out.println("Map settings: " + options);
119                 for (int x = 0; x < optionSettings.length; x++) {
120                     System.out.println("optionSetting[" + x + "] = " + optionSettings[x]);
121                 }
122             }
123
124             factory.compositeConfigure(transport, new OpenWireFormat(), options);
125
126             // lets start the transport to force the introspection
127
try {
128                 transport.start();
129             }
130             catch (Exception JavaDoc e) {
131                 // ignore bad connection
132
}
133
134             if (socketStub.getWantClientAuthStatus() != optionSettings[2]) {
135                 System.out.println("sheiite");
136             }
137
138             assertEquals("wantClientAuth was not properly set for iteration: " + i,
139                     optionSettings[0], transport.getWantClientAuthStatus());
140             assertEquals("needClientAuth was not properly set for iteration: " + i,
141                     optionSettings[1], transport.getNeedClientAuthStatus());
142             assertEquals("socket.wantClientAuth was not properly set for iteration: " + i,
143                     optionSettings[2], socketStub.getWantClientAuthStatus());
144             assertEquals("socket.needClientAuth was not properly set for iteration: " + i,
145                     optionSettings[3], socketStub.getNeedClientAuthStatus());
146             assertEquals("socket.useClientMode was not properly set for iteration: " + i,
147                     optionSettings[4], socketStub.getUseClientModeStatus());
148         }
149     }
150 }
151
Popular Tags