author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 30912 | jdk/test/sun/net/www/protocol/https/ChunkedOutputStream.java@5c720b6875a2 |
child 55031 | 37a077319427 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13788
diff
changeset
|
2 |
* Copyright (c) 2004, 2012, 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 5026745 |
|
30820 | 27 |
* @modules java.base/sun.net.www |
13788 | 28 |
* @build TestHttpsServer HttpCallback |
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
14342
diff
changeset
|
29 |
* @run main/othervm ChunkedOutputStream |
10328 | 30 |
* |
31 |
* SunJSSE does not support dynamic system properties, no way to re-use |
|
32 |
* system properties in samevm/agentvm mode. |
|
2 | 33 |
* @summary Cannot flush output stream when writing to an HttpUrlConnection |
34 |
*/ |
|
35 |
||
36 |
import java.io.*; |
|
37 |
import java.net.*; |
|
38 |
import javax.net.ssl.*; |
|
39 |
||
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
14342
diff
changeset
|
40 |
public class ChunkedOutputStream implements HttpCallback { |
2 | 41 |
/* |
42 |
* Where do we find the keystores for ssl? |
|
43 |
*/ |
|
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
14342
diff
changeset
|
44 |
static String pathToStores = "../../../../../javax/net/ssl/etc"; |
2 | 45 |
static String keyStoreFile = "keystore"; |
46 |
static String trustStoreFile = "truststore"; |
|
47 |
static String passwd = "passphrase"; |
|
48 |
static int count = 0; |
|
49 |
||
50 |
static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+ |
|
51 |
"1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+ |
|
52 |
"1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"; |
|
53 |
||
54 |
static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+ |
|
55 |
"1234567890"; |
|
56 |
||
57 |
public void request (HttpTransaction req) { |
|
58 |
try { |
|
59 |
// this is needed (count++ doesn't work), 'cause we |
|
60 |
// are doing concurrent tests |
|
61 |
String path = req.getRequestURI().getPath(); |
|
62 |
if (path.equals("/d0")) { |
|
63 |
count = 0; |
|
64 |
} else if (path.equals("/d01")) { |
|
65 |
count = 1; |
|
66 |
} else if (path.equals("/d3")) { |
|
67 |
count = 2; |
|
68 |
} else if (path.equals("/d4") || path.equals("/d5")) { |
|
69 |
count = 3; |
|
70 |
} else if (path.equals("/d6")) { |
|
71 |
count = 3; |
|
72 |
} else if (path.equals("/d7")) { |
|
73 |
count = 4; |
|
74 |
} else if (path.equals("/d8")) { |
|
75 |
count = 5; |
|
76 |
} |
|
77 |
||
78 |
switch (count) { |
|
79 |
case 0: /* test1 -- keeps conn alive */ |
|
80 |
case 1: /* test2 -- closes conn */ |
|
81 |
String reqbody = req.getRequestEntityBody(); |
|
82 |
if (!reqbody.equals(str1)) { |
|
83 |
req.sendResponse (500, "Internal server error"); |
|
84 |
req.orderlyClose(); |
|
85 |
} |
|
86 |
String chunk = req.getRequestHeader ("Transfer-encoding"); |
|
87 |
if (!"chunked".equals (chunk)) { |
|
88 |
req.sendResponse (501, "Internal server error"); |
|
89 |
req.orderlyClose(); |
|
90 |
} |
|
91 |
req.setResponseEntityBody (reqbody); |
|
92 |
if (count == 1) { |
|
93 |
req.setResponseHeader ("Connection", "close"); |
|
94 |
} |
|
95 |
req.sendResponse (200, "OK"); |
|
96 |
if (count == 1) { |
|
97 |
req.orderlyClose(); |
|
98 |
} |
|
99 |
break; |
|
100 |
case 2: /* test 3 */ |
|
101 |
reqbody = req.getRequestEntityBody(); |
|
102 |
if (!reqbody.equals(str2)) { |
|
103 |
req.sendResponse (500, "Internal server error"); |
|
104 |
req.orderlyClose(); |
|
105 |
} |
|
106 |
int clen = Integer.parseInt ( |
|
107 |
req.getRequestHeader ("Content-length")); |
|
108 |
if (clen != str2.length()) { |
|
109 |
req.sendResponse (501, "Internal server error"); |
|
110 |
req.orderlyClose(); |
|
111 |
} |
|
112 |
req.setResponseEntityBody (reqbody); |
|
113 |
req.setResponseHeader ("Connection", "close"); |
|
114 |
req.sendResponse (200, "OK"); |
|
115 |
req.orderlyClose(); |
|
116 |
break; |
|
117 |
case 3: /* test 6 */ |
|
118 |
req.setResponseHeader ("Location", "https://foo.bar/"); |
|
119 |
req.setResponseHeader ("Connection", "close"); |
|
120 |
req.sendResponse (307, "Temporary Redirect"); |
|
121 |
req.orderlyClose(); |
|
122 |
break; |
|
123 |
case 4: /* test 7 */ |
|
124 |
case 5: /* test 8 */ |
|
125 |
reqbody = req.getRequestEntityBody(); |
|
126 |
if (reqbody != null && !"".equals (reqbody)) { |
|
127 |
req.sendResponse (501, "Internal server error"); |
|
128 |
req.orderlyClose(); |
|
129 |
} |
|
130 |
req.setResponseHeader ("Connection", "close"); |
|
131 |
req.sendResponse (200, "OK"); |
|
132 |
req.orderlyClose(); |
|
133 |
break; |
|
134 |
} |
|
135 |
} catch (IOException e) { |
|
136 |
e.printStackTrace(); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
static void readAndCompare (InputStream is, String cmp) throws IOException { |
|
141 |
int c; |
|
142 |
byte buf[] = new byte [1024]; |
|
143 |
int off = 0; |
|
144 |
int len = 1024; |
|
145 |
while ((c=is.read(buf, off, len)) != -1) { |
|
146 |
off += c; |
|
147 |
len -= c; |
|
148 |
} |
|
149 |
String s1 = new String (buf, 0, off, "ISO8859_1"); |
|
150 |
if (!cmp.equals(s1)) { |
|
151 |
throw new IOException ("strings not same"); |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
/* basic chunked test (runs twice) */ |
|
156 |
||
157 |
static void test1 (String u) throws Exception { |
|
158 |
URL url = new URL (u); |
|
159 |
System.out.println ("client opening connection to: " + u); |
|
160 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
|
161 |
urlc.setChunkedStreamingMode (20); |
|
162 |
urlc.setDoOutput(true); |
|
163 |
urlc.setRequestMethod ("POST"); |
|
164 |
OutputStream os = urlc.getOutputStream (); |
|
165 |
os.write (str1.getBytes()); |
|
166 |
os.close(); |
|
167 |
InputStream is = urlc.getInputStream(); |
|
168 |
readAndCompare (is, str1); |
|
169 |
is.close(); |
|
170 |
} |
|
171 |
||
172 |
/* basic fixed length test */ |
|
173 |
||
174 |
static void test3 (String u) throws Exception { |
|
175 |
URL url = new URL (u); |
|
176 |
System.out.println ("client opening connection to: " + u); |
|
177 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
|
178 |
urlc.setFixedLengthStreamingMode (str2.length()); |
|
179 |
urlc.setDoOutput(true); |
|
180 |
urlc.setRequestMethod ("POST"); |
|
181 |
OutputStream os = urlc.getOutputStream (); |
|
182 |
os.write (str2.getBytes()); |
|
183 |
os.close(); |
|
184 |
InputStream is = urlc.getInputStream(); |
|
185 |
readAndCompare (is, str2); |
|
186 |
is.close(); |
|
187 |
} |
|
188 |
||
189 |
/* write too few bytes */ |
|
190 |
||
191 |
static void test4 (String u) throws Exception { |
|
192 |
URL url = new URL (u); |
|
193 |
System.out.println ("client opening connection to: " + u); |
|
194 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
|
195 |
urlc.setFixedLengthStreamingMode (str2.length()+1); |
|
196 |
urlc.setDoOutput(true); |
|
197 |
urlc.setRequestMethod ("POST"); |
|
198 |
OutputStream os = urlc.getOutputStream (); |
|
199 |
os.write (str2.getBytes()); |
|
200 |
try { |
|
201 |
os.close(); |
|
202 |
throw new Exception ("should have thrown IOException"); |
|
203 |
} catch (IOException e) {} |
|
204 |
} |
|
205 |
||
206 |
/* write too many bytes */ |
|
207 |
||
208 |
static void test5 (String u) throws Exception { |
|
209 |
URL url = new URL (u); |
|
210 |
System.out.println ("client opening connection to: " + u); |
|
211 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
|
212 |
urlc.setFixedLengthStreamingMode (str2.length()-1); |
|
213 |
urlc.setDoOutput(true); |
|
214 |
urlc.setRequestMethod ("POST"); |
|
215 |
OutputStream os = urlc.getOutputStream (); |
|
216 |
try { |
|
217 |
os.write (str2.getBytes()); |
|
218 |
throw new Exception ("should have thrown IOException"); |
|
219 |
} catch (IOException e) {} |
|
220 |
} |
|
221 |
||
222 |
/* check for HttpRetryException on redirection */ |
|
223 |
||
224 |
static void test6 (String u) throws Exception { |
|
225 |
URL url = new URL (u); |
|
226 |
System.out.println ("client opening connection to: " + u); |
|
227 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
|
228 |
urlc.setChunkedStreamingMode (20); |
|
229 |
urlc.setDoOutput(true); |
|
230 |
urlc.setRequestMethod ("POST"); |
|
231 |
OutputStream os = urlc.getOutputStream (); |
|
232 |
os.write (str1.getBytes()); |
|
233 |
os.close(); |
|
234 |
try { |
|
235 |
InputStream is = urlc.getInputStream(); |
|
236 |
throw new Exception ("should have gotten HttpRetryException"); |
|
237 |
} catch (HttpRetryException e) { |
|
238 |
if (e.responseCode() != 307) { |
|
239 |
throw new Exception ("Wrong response code " + e.responseCode()); |
|
240 |
} |
|
241 |
if (!e.getLocation().equals ("https://foo.bar/")) { |
|
242 |
throw new Exception ("Wrong location " + e.getLocation()); |
|
243 |
} |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
/* next two tests send zero length posts */ |
|
248 |
||
249 |
static void test7 (String u) throws Exception { |
|
250 |
URL url = new URL (u); |
|
251 |
System.out.println ("client opening connection to: " + u); |
|
252 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
|
253 |
urlc.setChunkedStreamingMode (20); |
|
254 |
urlc.setDoOutput(true); |
|
255 |
urlc.setRequestMethod ("POST"); |
|
256 |
OutputStream os = urlc.getOutputStream (); |
|
257 |
os.close(); |
|
258 |
int ret = urlc.getResponseCode(); |
|
259 |
if (ret != 200) { |
|
260 |
throw new Exception ("Expected 200: got " + ret); |
|
261 |
} |
|
262 |
} |
|
263 |
||
264 |
static void test8 (String u) throws Exception { |
|
265 |
URL url = new URL (u); |
|
266 |
System.out.println ("client opening connection to: " + u); |
|
267 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
|
268 |
urlc.setFixedLengthStreamingMode (0); |
|
269 |
urlc.setDoOutput(true); |
|
270 |
urlc.setRequestMethod ("POST"); |
|
271 |
OutputStream os = urlc.getOutputStream (); |
|
272 |
os.close(); |
|
273 |
int ret = urlc.getResponseCode(); |
|
274 |
if (ret != 200) { |
|
275 |
throw new Exception ("Expected 200: got " + ret); |
|
276 |
} |
|
277 |
} |
|
278 |
||
13788 | 279 |
static TestHttpsServer server; |
2 | 280 |
|
281 |
public static void main (String[] args) throws Exception { |
|
282 |
// setup properties to do ssl |
|
283 |
String keyFilename = |
|
284 |
System.getProperty("test.src", "./") + "/" + pathToStores + |
|
285 |
"/" + keyStoreFile; |
|
286 |
String trustFilename = |
|
287 |
System.getProperty("test.src", "./") + "/" + pathToStores + |
|
288 |
"/" + trustStoreFile; |
|
289 |
||
10328 | 290 |
HostnameVerifier reservedHV = |
291 |
HttpsURLConnection.getDefaultHostnameVerifier(); |
|
2 | 292 |
try { |
10328 | 293 |
System.setProperty("javax.net.ssl.keyStore", keyFilename); |
294 |
System.setProperty("javax.net.ssl.keyStorePassword", passwd); |
|
295 |
System.setProperty("javax.net.ssl.trustStore", trustFilename); |
|
296 |
System.setProperty("javax.net.ssl.trustStorePassword", passwd); |
|
297 |
HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier()); |
|
298 |
||
299 |
try { |
|
23052
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
14342
diff
changeset
|
300 |
server = new TestHttpsServer( |
241885315119
8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents:
14342
diff
changeset
|
301 |
new ChunkedOutputStream(), 1, 10, 0); |
10328 | 302 |
System.out.println ("Server started: listening on port: " + server.getLocalPort()); |
303 |
// the test server doesn't support keep-alive yet |
|
304 |
// test1("http://localhost:"+server.getLocalPort()+"/d0"); |
|
305 |
test1("https://localhost:"+server.getLocalPort()+"/d01"); |
|
306 |
test3("https://localhost:"+server.getLocalPort()+"/d3"); |
|
307 |
test4("https://localhost:"+server.getLocalPort()+"/d4"); |
|
308 |
test5("https://localhost:"+server.getLocalPort()+"/d5"); |
|
309 |
test6("https://localhost:"+server.getLocalPort()+"/d6"); |
|
310 |
test7("https://localhost:"+server.getLocalPort()+"/d7"); |
|
311 |
test8("https://localhost:"+server.getLocalPort()+"/d8"); |
|
312 |
} catch (Exception e) { |
|
313 |
if (server != null) { |
|
314 |
server.terminate(); |
|
315 |
} |
|
316 |
throw e; |
|
2 | 317 |
} |
10328 | 318 |
server.terminate(); |
319 |
} finally { |
|
320 |
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV); |
|
2 | 321 |
} |
322 |
} |
|
323 |
||
324 |
static class NameVerifier implements HostnameVerifier { |
|
325 |
public boolean verify(String hostname, SSLSession session) { |
|
326 |
return true; |
|
327 |
} |
|
328 |
} |
|
329 |
||
330 |
public static void except (String s) { |
|
331 |
server.terminate(); |
|
332 |
throw new RuntimeException (s); |
|
333 |
} |
|
334 |
} |