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