author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 5506 | 202f599c92aa |
child 10328 | 06c93c42bca0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2001, 2010, 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 4423074 |
|
27 |
* @summary Need to rebase all the duplicated classes from Merlin. |
|
28 |
* This test will check out http POST |
|
29 |
*/ |
|
30 |
||
31 |
import java.io.*; |
|
32 |
import java.net.*; |
|
33 |
import javax.net.ssl.*; |
|
34 |
||
35 |
public class HttpsPost { |
|
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 = true; |
|
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 |
/* |
|
5289
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
64 |
* Is the connection ready to close? |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
65 |
*/ |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
66 |
volatile static boolean closeReady = false; |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
67 |
|
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
68 |
/* |
2 | 69 |
* Turn on SSL debugging? |
70 |
*/ |
|
71 |
static boolean debug = false; |
|
72 |
||
73 |
/* |
|
74 |
* Message posted |
|
75 |
*/ |
|
76 |
static String postMsg = "Testing HTTP post on a https server"; |
|
77 |
||
78 |
/* |
|
79 |
* If the client or server is doing some kind of object creation |
|
80 |
* that the other side depends on, and that thread prematurely |
|
81 |
* exits, you may experience a hang. The test harness will |
|
82 |
* terminate all hung threads after its timeout has expired, |
|
83 |
* currently 3 minutes by default, but you might try to be |
|
84 |
* smart about it.... |
|
85 |
*/ |
|
86 |
||
87 |
/* |
|
88 |
* Define the server side of the test. |
|
89 |
* |
|
90 |
* If the server prematurely exits, serverReady will be set to true |
|
91 |
* to avoid infinite hangs. |
|
92 |
*/ |
|
93 |
void doServerSide() throws Exception { |
|
94 |
SSLServerSocketFactory sslssf = |
|
95 |
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); |
|
96 |
SSLServerSocket sslServerSocket = |
|
97 |
(SSLServerSocket) sslssf.createServerSocket(serverPort); |
|
98 |
serverPort = sslServerSocket.getLocalPort(); |
|
99 |
||
100 |
/* |
|
101 |
* Signal Client, we're ready for his connect. |
|
102 |
*/ |
|
103 |
serverReady = true; |
|
104 |
||
105 |
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept(); |
|
5289
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
106 |
try { |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
107 |
InputStream sslIS = sslSocket.getInputStream(); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
108 |
OutputStream sslOS = sslSocket.getOutputStream(); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
109 |
BufferedReader br = |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
110 |
new BufferedReader(new InputStreamReader(sslIS)); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
111 |
PrintStream ps = new PrintStream(sslOS); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
112 |
|
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
113 |
// process HTTP POST request from client |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
114 |
System.out.println("status line: "+br.readLine()); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
115 |
String msg = null; |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
116 |
while ((msg = br.readLine()) != null && msg.length() > 0); |
2 | 117 |
|
5289
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
118 |
msg = br.readLine(); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
119 |
if (msg.equals(postMsg)) { |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
120 |
ps.println("HTTP/1.1 200 OK\n\n"); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
121 |
} else { |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
122 |
ps.println("HTTP/1.1 500 Not OK\n\n"); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
123 |
} |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
124 |
ps.flush(); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
125 |
|
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
126 |
// close the socket |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
127 |
while (!closeReady) { |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
128 |
Thread.sleep(50); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
129 |
} |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
130 |
} finally { |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
131 |
sslSocket.close(); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
132 |
sslServerSocket.close(); |
2 | 133 |
} |
134 |
} |
|
135 |
||
136 |
/* |
|
137 |
* Define the client side of the test. |
|
138 |
* |
|
139 |
* If the server prematurely exits, serverReady will be set to true |
|
140 |
* to avoid infinite hangs. |
|
141 |
*/ |
|
142 |
void doClientSide() throws Exception { |
|
143 |
||
144 |
/* |
|
145 |
* Wait for server to get started. |
|
146 |
*/ |
|
147 |
while (!serverReady) { |
|
148 |
Thread.sleep(50); |
|
149 |
} |
|
150 |
||
151 |
// Send HTTP POST request to server |
|
152 |
URL url = new URL("https://localhost:"+serverPort); |
|
153 |
||
154 |
HttpsURLConnection.setDefaultHostnameVerifier( |
|
155 |
new NameVerifier()); |
|
156 |
HttpsURLConnection http = (HttpsURLConnection)url.openConnection(); |
|
157 |
http.setDoOutput(true); |
|
158 |
||
159 |
http.setRequestMethod("POST"); |
|
160 |
PrintStream ps = new PrintStream(http.getOutputStream()); |
|
5289
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
161 |
try { |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
162 |
ps.println(postMsg); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
163 |
ps.flush(); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
164 |
if (http.getResponseCode() != 200) { |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
165 |
throw new RuntimeException("test Failed"); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
166 |
} |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
167 |
} finally { |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
168 |
ps.close(); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
169 |
http.disconnect(); |
18ef72e8f2d6
6941936: Broken pipe error of test case DNSIdentities.java
xuelei
parents:
2
diff
changeset
|
170 |
closeReady = true; |
2 | 171 |
} |
172 |
} |
|
173 |
||
174 |
static class NameVerifier implements HostnameVerifier { |
|
175 |
public boolean verify(String hostname, SSLSession session) { |
|
176 |
return true; |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
/* |
|
181 |
* ============================================================= |
|
182 |
* The remainder is just support stuff |
|
183 |
*/ |
|
184 |
||
185 |
// use any free port by default |
|
186 |
volatile int serverPort = 0; |
|
187 |
||
188 |
volatile Exception serverException = null; |
|
189 |
volatile Exception clientException = null; |
|
190 |
||
191 |
public static void main(String[] args) throws Exception { |
|
192 |
String keyFilename = |
|
193 |
System.getProperty("test.src", "./") + "/" + pathToStores + |
|
194 |
"/" + keyStoreFile; |
|
195 |
String trustFilename = |
|
196 |
System.getProperty("test.src", "./") + "/" + pathToStores + |
|
197 |
"/" + trustStoreFile; |
|
198 |
||
199 |
System.setProperty("javax.net.ssl.keyStore", keyFilename); |
|
200 |
System.setProperty("javax.net.ssl.keyStorePassword", passwd); |
|
201 |
System.setProperty("javax.net.ssl.trustStore", trustFilename); |
|
202 |
System.setProperty("javax.net.ssl.trustStorePassword", passwd); |
|
203 |
||
204 |
if (debug) |
|
205 |
System.setProperty("javax.net.debug", "all"); |
|
206 |
||
207 |
/* |
|
208 |
* Start the tests. |
|
209 |
*/ |
|
210 |
new HttpsPost(); |
|
211 |
} |
|
212 |
||
213 |
Thread clientThread = null; |
|
214 |
Thread serverThread = null; |
|
215 |
||
216 |
/* |
|
217 |
* Primary constructor, used to drive remainder of the test. |
|
218 |
* |
|
219 |
* Fork off the other side, then do your work. |
|
220 |
*/ |
|
221 |
HttpsPost() throws Exception { |
|
222 |
if (separateServerThread) { |
|
223 |
startServer(true); |
|
224 |
startClient(false); |
|
225 |
} else { |
|
226 |
startClient(true); |
|
227 |
startServer(false); |
|
228 |
} |
|
229 |
||
230 |
/* |
|
231 |
* Wait for other side to close down. |
|
232 |
*/ |
|
233 |
if (separateServerThread) { |
|
234 |
serverThread.join(); |
|
235 |
} else { |
|
236 |
clientThread.join(); |
|
237 |
} |
|
238 |
||
239 |
/* |
|
240 |
* When we get here, the test is pretty much over. |
|
241 |
* |
|
242 |
* If the main thread excepted, that propagates back |
|
243 |
* immediately. If the other thread threw an exception, we |
|
244 |
* should report back. |
|
245 |
*/ |
|
246 |
if (serverException != null) |
|
247 |
throw serverException; |
|
248 |
if (clientException != null) |
|
249 |
throw clientException; |
|
250 |
} |
|
251 |
||
252 |
void startServer(boolean newThread) throws Exception { |
|
253 |
if (newThread) { |
|
254 |
serverThread = new Thread() { |
|
255 |
public void run() { |
|
256 |
try { |
|
257 |
doServerSide(); |
|
258 |
} catch (Exception e) { |
|
259 |
/* |
|
260 |
* Our server thread just died. |
|
261 |
* |
|
262 |
* Release the client, if not active already... |
|
263 |
*/ |
|
264 |
System.err.println("Server died..."); |
|
265 |
serverReady = true; |
|
266 |
serverException = e; |
|
267 |
} |
|
268 |
} |
|
269 |
}; |
|
270 |
serverThread.start(); |
|
271 |
} else { |
|
272 |
doServerSide(); |
|
273 |
} |
|
274 |
} |
|
275 |
||
276 |
void startClient(boolean newThread) throws Exception { |
|
277 |
if (newThread) { |
|
278 |
clientThread = new Thread() { |
|
279 |
public void run() { |
|
280 |
try { |
|
281 |
doClientSide(); |
|
282 |
} catch (Exception e) { |
|
283 |
/* |
|
284 |
* Our client thread just died. |
|
285 |
*/ |
|
286 |
System.err.println("Client died..."); |
|
287 |
clientException = e; |
|
288 |
} |
|
289 |
} |
|
290 |
}; |
|
291 |
clientThread.start(); |
|
292 |
} else { |
|
293 |
doClientSide(); |
|
294 |
} |
|
295 |
} |
|
296 |
} |