author | aeubanks |
Wed, 27 Mar 2019 09:06:43 -0700 | |
changeset 54314 | 46cf212cdcca |
parent 54086 | ccb4a50bee06 |
child 55031 | 37a077319427 |
permissions | -rw-r--r-- |
7271 | 1 |
/* |
54086
ccb4a50bee06
8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. |
7271 | 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 |
* |
|
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. |
|
22 |
*/ |
|
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 6725892 |
|
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
54086
diff
changeset
|
27 |
* @library /test/lib |
7271 | 28 |
* @run main/othervm -Dsun.net.httpserver.maxReqTime=2 Test |
29 |
* @summary |
|
30 |
*/ |
|
31 |
||
32 |
import com.sun.net.httpserver.*; |
|
33 |
||
34 |
import java.util.concurrent.*; |
|
35 |
import java.util.logging.*; |
|
36 |
import java.io.*; |
|
37 |
import java.net.*; |
|
38 |
import javax.net.ssl.*; |
|
39 |
||
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
54086
diff
changeset
|
40 |
import jdk.test.lib.net.URIBuilder; |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
54086
diff
changeset
|
41 |
|
7271 | 42 |
public class Test { |
43 |
||
44 |
static HttpServer s1; |
|
45 |
static int port; |
|
46 |
static URL url; |
|
47 |
static final String RESPONSE_BODY = "response"; |
|
48 |
static boolean failed = false; |
|
49 |
||
50 |
static class Handler implements HttpHandler { |
|
51 |
||
52 |
public void handle (HttpExchange t) |
|
53 |
throws IOException |
|
54 |
{ |
|
55 |
InputStream is = t.getRequestBody(); |
|
56 |
InetSocketAddress rem = t.getRemoteAddress(); |
|
57 |
System.out.println ("Request from: " + rem); |
|
58 |
while (is.read () != -1) ; |
|
59 |
is.close(); |
|
60 |
String requrl = t.getRequestURI().toString(); |
|
61 |
OutputStream os = t.getResponseBody(); |
|
62 |
t.sendResponseHeaders (200, RESPONSE_BODY.length()); |
|
63 |
os.write (RESPONSE_BODY.getBytes()); |
|
64 |
t.close(); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
public static void main (String[] args) throws Exception { |
|
69 |
||
70 |
ExecutorService exec = Executors.newCachedThreadPool(); |
|
71 |
||
72 |
try { |
|
73 |
InetSocketAddress addr = new InetSocketAddress (0); |
|
22995
453241790793
7052625: com/sun/net/httpserver/bugs/6725892/Test.java fails intermittently
chegar
parents:
7668
diff
changeset
|
74 |
s1 = HttpServer.create (addr, 100); |
7271 | 75 |
HttpHandler h = new Handler (); |
76 |
HttpContext c1 = s1.createContext ("/", h); |
|
77 |
s1.setExecutor(exec); |
|
78 |
s1.start(); |
|
79 |
||
80 |
port = s1.getAddress().getPort(); |
|
81 |
System.out.println ("Server on port " + port); |
|
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
54086
diff
changeset
|
82 |
url = URIBuilder.newBuilder() |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
54086
diff
changeset
|
83 |
.scheme("http") |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
54086
diff
changeset
|
84 |
.loopback() |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
54086
diff
changeset
|
85 |
.port(port) |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
54086
diff
changeset
|
86 |
.path("/foo") |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
54086
diff
changeset
|
87 |
.toURLUnchecked(); |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
54086
diff
changeset
|
88 |
System.out.println("URL: " + url); |
7271 | 89 |
test1(); |
90 |
test2(); |
|
91 |
test3(); |
|
92 |
Thread.sleep (2000); |
|
93 |
} catch (Exception e) { |
|
94 |
e.printStackTrace(); |
|
95 |
System.out.println ("FAIL"); |
|
96 |
throw new RuntimeException (); |
|
97 |
} finally { |
|
98 |
s1.stop(0); |
|
99 |
System.out.println ("After Shutdown"); |
|
100 |
exec.shutdown(); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
// open TCP connection without sending anything. Check server closes it. |
|
105 |
||
106 |
static void test1() throws IOException { |
|
107 |
failed = false; |
|
54086
ccb4a50bee06
8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents:
47216
diff
changeset
|
108 |
Socket s = new Socket (InetAddress.getLoopbackAddress(), port); |
7271 | 109 |
InputStream is = s.getInputStream(); |
110 |
// server should close connection after 2 seconds. We wait up to 10 |
|
111 |
s.setSoTimeout (10000); |
|
112 |
try { |
|
113 |
is.read(); |
|
114 |
} catch (SocketTimeoutException e) { |
|
115 |
failed = true; |
|
116 |
} |
|
117 |
s.close(); |
|
118 |
if (failed) { |
|
119 |
System.out.println ("test1: FAIL"); |
|
120 |
throw new RuntimeException (); |
|
121 |
} else { |
|
122 |
System.out.println ("test1: OK"); |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
// send request and don't read response. Check server closes connection |
|
127 |
||
128 |
static void test2() throws IOException { |
|
129 |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); |
|
130 |
urlc.setReadTimeout (20 * 1000); |
|
131 |
InputStream is = urlc.getInputStream(); |
|
132 |
// we won't read response and check if it times out |
|
133 |
// on server. If it timesout at client then there is a problem |
|
134 |
try { |
|
135 |
Thread.sleep (10 * 1000); |
|
136 |
while (is.read() != -1) ; |
|
137 |
} catch (InterruptedException e) { |
|
138 |
System.out.println (e); |
|
139 |
System.out.println ("test2: FAIL"); |
|
140 |
throw new RuntimeException ("unexpected error"); |
|
141 |
} catch (SocketTimeoutException e1) { |
|
142 |
System.out.println (e1); |
|
143 |
System.out.println ("test2: FAIL"); |
|
144 |
throw new RuntimeException ("client timedout"); |
|
145 |
} finally { |
|
146 |
is.close(); |
|
147 |
} |
|
148 |
System.out.println ("test2: OK"); |
|
149 |
} |
|
150 |
||
151 |
// same as test2, but repeated with multiple connections |
|
152 |
// including a number of valid request/responses |
|
153 |
||
154 |
// Worker: a thread opens a connection to the server in one of three modes. |
|
155 |
// NORMAL - sends a request, waits for response, and checks valid response |
|
156 |
// REQUEST - sends a partial request, and blocks, to see if |
|
157 |
// server closes the connection. |
|
158 |
// RESPONSE - sends a request, partially reads response and blocks, |
|
159 |
// to see if server closes the connection. |
|
160 |
||
161 |
static class Worker extends Thread { |
|
162 |
CountDownLatch latch; |
|
163 |
Mode mode; |
|
164 |
||
165 |
enum Mode { |
|
166 |
REQUEST, // block during sending of request |
|
167 |
RESPONSE, // block during reading of response |
|
168 |
NORMAL // don't block |
|
169 |
}; |
|
170 |
||
171 |
Worker (CountDownLatch latch, Mode mode) { |
|
172 |
this.latch = latch; |
|
173 |
this.mode = mode; |
|
174 |
} |
|
175 |
||
176 |
void fail(String msg) { |
|
177 |
System.out.println (msg); |
|
178 |
failed = true; |
|
179 |
} |
|
180 |
||
181 |
public void run () { |
|
182 |
HttpURLConnection urlc; |
|
183 |
InputStream is = null; |
|
184 |
||
185 |
try { |
|
186 |
urlc = (HttpURLConnection) url.openConnection(); |
|
187 |
urlc.setReadTimeout (20 * 1000); |
|
188 |
urlc.setDoOutput(true); |
|
189 |
} catch (IOException e) { |
|
190 |
fail("Worker: failed to connect to server"); |
|
191 |
latch.countDown(); |
|
192 |
return; |
|
193 |
} |
|
194 |
try { |
|
195 |
OutputStream os = urlc.getOutputStream(); |
|
196 |
os.write ("foo".getBytes()); |
|
197 |
if (mode == Mode.REQUEST) { |
|
198 |
Thread.sleep (3000); |
|
199 |
} |
|
200 |
os.close(); |
|
201 |
is = urlc.getInputStream(); |
|
202 |
if (mode == Mode.RESPONSE) { |
|
203 |
Thread.sleep (3000); |
|
204 |
} |
|
205 |
if (!checkResponse (is, RESPONSE_BODY)) { |
|
206 |
fail ("Worker: response"); |
|
207 |
} |
|
208 |
is.close(); |
|
209 |
return; |
|
210 |
} catch (InterruptedException e0) { |
|
211 |
fail("Worker: timedout"); |
|
212 |
} catch (SocketTimeoutException e1) { |
|
213 |
fail("Worker: timedout"); |
|
214 |
} catch (IOException e2) { |
|
215 |
switch (mode) { |
|
216 |
case NORMAL: |
|
217 |
fail ("Worker: " + e2.getMessage()); |
|
218 |
break; |
|
219 |
case RESPONSE: |
|
220 |
if (is == null) { |
|
221 |
fail ("Worker: " + e2.getMessage()); |
|
222 |
break; |
|
223 |
} |
|
224 |
// default: is ok |
|
225 |
} |
|
226 |
} finally { |
|
227 |
latch.countDown(); |
|
228 |
} |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
static final int NUM = 20; |
|
233 |
||
234 |
static void test3() throws Exception { |
|
235 |
failed = false; |
|
236 |
CountDownLatch l = new CountDownLatch (NUM*3); |
|
237 |
Worker[] workers = new Worker[NUM*3]; |
|
238 |
for (int i=0; i<NUM; i++) { |
|
239 |
workers[i*3] = new Worker (l, Worker.Mode.NORMAL); |
|
240 |
workers[i*3+1] = new Worker (l, Worker.Mode.REQUEST); |
|
241 |
workers[i*3+2] = new Worker (l, Worker.Mode.RESPONSE); |
|
242 |
workers[i*3].start(); |
|
243 |
workers[i*3+1].start(); |
|
244 |
workers[i*3+2].start(); |
|
245 |
} |
|
246 |
l.await(); |
|
247 |
for (int i=0; i<NUM*3; i++) { |
|
248 |
workers[i].join(); |
|
249 |
} |
|
250 |
if (failed) { |
|
251 |
throw new RuntimeException ("test3: failed"); |
|
252 |
} |
|
253 |
System.out.println ("test3: OK"); |
|
254 |
} |
|
255 |
||
256 |
static boolean checkResponse (InputStream is, String resp) { |
|
257 |
try { |
|
258 |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
|
259 |
byte[] buf = new byte [64]; |
|
260 |
int c; |
|
261 |
while ((c=is.read(buf)) != -1) { |
|
262 |
bos.write (buf, 0, c); |
|
263 |
} |
|
264 |
bos.close(); |
|
265 |
if (!bos.toString().equals(resp)) { |
|
266 |
System.out.println ("Wrong response: " + bos.toString()); |
|
267 |
return false; |
|
268 |
} |
|
269 |
} catch (IOException e) { |
|
270 |
System.out.println (e); |
|
271 |
return false; |
|
272 |
} |
|
273 |
return true; |
|
274 |
} |
|
275 |
} |