2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 1234567
|
|
27 |
* @summary Use this template to help speed your client/server tests.
|
|
28 |
* @author Brad Wetmore
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.io.*;
|
|
32 |
import java.net.*;
|
|
33 |
import javax.net.ssl.*;
|
|
34 |
|
|
35 |
public class SSLSocketTemplate {
|
|
36 |
|
|
37 |
/*
|
|
38 |
* =============================================================
|
|
39 |
* Set the various variables needed for the tests, then
|
|
40 |
* specify what tests to run on each side.
|
|
41 |
*/
|
|
42 |
|
|
43 |
/*
|
|
44 |
* Should we run the client or server in a separate thread?
|
|
45 |
* Both sides can throw exceptions, but do you have a preference
|
|
46 |
* as to which side should be the main thread.
|
|
47 |
*/
|
|
48 |
static boolean separateServerThread = false;
|
|
49 |
|
|
50 |
/*
|
|
51 |
* Where do we find the keystores?
|
|
52 |
*/
|
|
53 |
static String pathToStores = "../etc";
|
|
54 |
static String keyStoreFile = "keystore";
|
|
55 |
static String trustStoreFile = "truststore";
|
|
56 |
static String passwd = "passphrase";
|
|
57 |
|
|
58 |
/*
|
|
59 |
* Is the server ready to serve?
|
|
60 |
*/
|
|
61 |
volatile static boolean serverReady = false;
|
|
62 |
|
|
63 |
/*
|
|
64 |
* Turn on SSL debugging?
|
|
65 |
*/
|
|
66 |
static boolean debug = false;
|
|
67 |
|
|
68 |
/*
|
|
69 |
* If the client or server is doing some kind of object creation
|
|
70 |
* that the other side depends on, and that thread prematurely
|
|
71 |
* exits, you may experience a hang. The test harness will
|
|
72 |
* terminate all hung threads after its timeout has expired,
|
|
73 |
* currently 3 minutes by default, but you might try to be
|
|
74 |
* smart about it....
|
|
75 |
*/
|
|
76 |
|
|
77 |
/*
|
|
78 |
* Define the server side of the test.
|
|
79 |
*
|
|
80 |
* If the server prematurely exits, serverReady will be set to true
|
|
81 |
* to avoid infinite hangs.
|
|
82 |
*/
|
|
83 |
void doServerSide() throws Exception {
|
|
84 |
SSLServerSocketFactory sslssf =
|
|
85 |
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
|
|
86 |
SSLServerSocket sslServerSocket =
|
|
87 |
(SSLServerSocket) sslssf.createServerSocket(serverPort);
|
|
88 |
|
|
89 |
serverPort = sslServerSocket.getLocalPort();
|
|
90 |
|
|
91 |
/*
|
|
92 |
* Signal Client, we're ready for his connect.
|
|
93 |
*/
|
|
94 |
serverReady = true;
|
|
95 |
|
|
96 |
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
|
|
97 |
InputStream sslIS = sslSocket.getInputStream();
|
|
98 |
OutputStream sslOS = sslSocket.getOutputStream();
|
|
99 |
|
|
100 |
sslIS.read();
|
|
101 |
sslOS.write(85);
|
|
102 |
sslOS.flush();
|
|
103 |
|
|
104 |
sslSocket.close();
|
|
105 |
}
|
|
106 |
|
|
107 |
/*
|
|
108 |
* Define the client side of the test.
|
|
109 |
*
|
|
110 |
* If the server prematurely exits, serverReady will be set to true
|
|
111 |
* to avoid infinite hangs.
|
|
112 |
*/
|
|
113 |
void doClientSide() throws Exception {
|
|
114 |
|
|
115 |
/*
|
|
116 |
* Wait for server to get started.
|
|
117 |
*/
|
|
118 |
while (!serverReady) {
|
|
119 |
Thread.sleep(50);
|
|
120 |
}
|
|
121 |
|
|
122 |
SSLSocketFactory sslsf =
|
|
123 |
(SSLSocketFactory) SSLSocketFactory.getDefault();
|
|
124 |
SSLSocket sslSocket = (SSLSocket)
|
|
125 |
sslsf.createSocket("localhost", serverPort);
|
|
126 |
|
|
127 |
InputStream sslIS = sslSocket.getInputStream();
|
|
128 |
OutputStream sslOS = sslSocket.getOutputStream();
|
|
129 |
|
|
130 |
sslOS.write(280);
|
|
131 |
sslOS.flush();
|
|
132 |
sslIS.read();
|
|
133 |
|
|
134 |
sslSocket.close();
|
|
135 |
}
|
|
136 |
|
|
137 |
/*
|
|
138 |
* =============================================================
|
|
139 |
* The remainder is just support stuff
|
|
140 |
*/
|
|
141 |
|
|
142 |
// use any free port by default
|
|
143 |
volatile int serverPort = 0;
|
|
144 |
|
|
145 |
volatile Exception serverException = null;
|
|
146 |
volatile Exception clientException = null;
|
|
147 |
|
|
148 |
public static void main(String[] args) throws Exception {
|
|
149 |
String keyFilename =
|
|
150 |
System.getProperty("test.src", ".") + "/" + pathToStores +
|
|
151 |
"/" + keyStoreFile;
|
|
152 |
String trustFilename =
|
|
153 |
System.getProperty("test.src", ".") + "/" + pathToStores +
|
|
154 |
"/" + trustStoreFile;
|
|
155 |
|
|
156 |
System.setProperty("javax.net.ssl.keyStore", keyFilename);
|
|
157 |
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
|
|
158 |
System.setProperty("javax.net.ssl.trustStore", trustFilename);
|
|
159 |
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
|
|
160 |
|
|
161 |
if (debug)
|
|
162 |
System.setProperty("javax.net.debug", "all");
|
|
163 |
|
|
164 |
/*
|
|
165 |
* Start the tests.
|
|
166 |
*/
|
|
167 |
new SSLSocketTemplate();
|
|
168 |
}
|
|
169 |
|
|
170 |
Thread clientThread = null;
|
|
171 |
Thread serverThread = null;
|
|
172 |
|
|
173 |
/*
|
|
174 |
* Primary constructor, used to drive remainder of the test.
|
|
175 |
*
|
|
176 |
* Fork off the other side, then do your work.
|
|
177 |
*/
|
|
178 |
SSLSocketTemplate() throws Exception {
|
|
179 |
try {
|
|
180 |
if (separateServerThread) {
|
|
181 |
startServer(true);
|
|
182 |
startClient(false);
|
|
183 |
} else {
|
|
184 |
startClient(true);
|
|
185 |
startServer(false);
|
|
186 |
}
|
|
187 |
} catch (Exception e) {
|
|
188 |
// swallow for now. Show later
|
|
189 |
}
|
|
190 |
|
|
191 |
/*
|
|
192 |
* Wait for other side to close down.
|
|
193 |
*/
|
|
194 |
if (separateServerThread) {
|
|
195 |
serverThread.join();
|
|
196 |
} else {
|
|
197 |
clientThread.join();
|
|
198 |
}
|
|
199 |
|
|
200 |
/*
|
|
201 |
* When we get here, the test is pretty much over.
|
|
202 |
* Which side threw the error?
|
|
203 |
*/
|
|
204 |
Exception local;
|
|
205 |
Exception remote;
|
|
206 |
String whichRemote;
|
|
207 |
|
|
208 |
if (separateServerThread) {
|
|
209 |
remote = serverException;
|
|
210 |
local = clientException;
|
|
211 |
whichRemote = "server";
|
|
212 |
} else {
|
|
213 |
remote = clientException;
|
|
214 |
local = serverException;
|
|
215 |
whichRemote = "client";
|
|
216 |
}
|
|
217 |
|
|
218 |
/*
|
|
219 |
* If both failed, return the curthread's exception, but also
|
|
220 |
* print the remote side Exception
|
|
221 |
*/
|
|
222 |
if ((local != null) && (remote != null)) {
|
|
223 |
System.out.println(whichRemote + " also threw:");
|
|
224 |
remote.printStackTrace();
|
|
225 |
System.out.println();
|
|
226 |
throw local;
|
|
227 |
}
|
|
228 |
|
|
229 |
if (remote != null) {
|
|
230 |
throw remote;
|
|
231 |
}
|
|
232 |
|
|
233 |
if (local != null) {
|
|
234 |
throw local;
|
|
235 |
}
|
|
236 |
}
|
|
237 |
|
|
238 |
void startServer(boolean newThread) throws Exception {
|
|
239 |
if (newThread) {
|
|
240 |
serverThread = new Thread() {
|
|
241 |
public void run() {
|
|
242 |
try {
|
|
243 |
doServerSide();
|
|
244 |
} catch (Exception e) {
|
|
245 |
/*
|
|
246 |
* Our server thread just died.
|
|
247 |
*
|
|
248 |
* Release the client, if not active already...
|
|
249 |
*/
|
|
250 |
System.err.println("Server died...");
|
|
251 |
serverReady = true;
|
|
252 |
serverException = e;
|
|
253 |
}
|
|
254 |
}
|
|
255 |
};
|
|
256 |
serverThread.start();
|
|
257 |
} else {
|
|
258 |
try {
|
|
259 |
doServerSide();
|
|
260 |
} catch (Exception e) {
|
|
261 |
serverException = e;
|
|
262 |
} finally {
|
|
263 |
serverReady = true;
|
|
264 |
}
|
|
265 |
}
|
|
266 |
}
|
|
267 |
|
|
268 |
void startClient(boolean newThread) throws Exception {
|
|
269 |
if (newThread) {
|
|
270 |
clientThread = new Thread() {
|
|
271 |
public void run() {
|
|
272 |
try {
|
|
273 |
doClientSide();
|
|
274 |
} catch (Exception e) {
|
|
275 |
/*
|
|
276 |
* Our client thread just died.
|
|
277 |
*/
|
|
278 |
System.err.println("Client died...");
|
|
279 |
clientException = e;
|
|
280 |
}
|
|
281 |
}
|
|
282 |
};
|
|
283 |
clientThread.start();
|
|
284 |
} else {
|
|
285 |
try {
|
|
286 |
doClientSide();
|
|
287 |
} catch (Exception e) {
|
|
288 |
clientException = e;
|
|
289 |
}
|
|
290 |
}
|
|
291 |
}
|
|
292 |
}
|