author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58365 | 73950479184b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54746
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2002, 2019, 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 4635698 |
|
27 |
* @summary Check that HttpURLConnection.getResponseCode returns -1 for |
|
28 |
* malformed status-lines in the http response. |
|
29 |
*/ |
|
30 |
import java.net.*; |
|
31 |
import java.io.*; |
|
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
58009
diff
changeset
|
32 |
import static java.net.Proxy.NO_PROXY; |
2 | 33 |
|
34 |
public class Responses { |
|
35 |
||
36 |
/* |
|
37 |
* Test cases :- |
|
38 |
* "Response from server" expected getResponse() and |
|
39 |
* getResponseMessage() results |
|
40 |
*/ |
|
41 |
static Object[][] getTests() { |
|
42 |
return new Object[][] { |
|
43 |
{ "HTTP/1.1 200 OK", "200", "OK" }, |
|
44 |
{ "HTTP/1.1 404 ", "404", null }, |
|
45 |
{ "HTTP/1.1 200", "200", null }, |
|
46 |
{ "HTTP/1.1", "-1", null }, |
|
47 |
{ "Invalid", "-1", null }, |
|
48 |
{ null, "-1" , null }, |
|
49 |
}; |
|
50 |
} |
|
51 |
||
52 |
/* |
|
53 |
* Simple http server used by test |
|
54 |
* |
|
55 |
* GET /<n> HTTP/1.x results in http response with the status line |
|
56 |
* set to geTests()[<n>][0] -- eg: GET /2 results in a response of |
|
57 |
* "HTTP/1.1 404 " |
|
58 |
*/ |
|
59 |
static class HttpServer implements Runnable { |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54746
diff
changeset
|
60 |
final ServerSocket ss; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54746
diff
changeset
|
61 |
volatile boolean shutdown; |
2 | 62 |
|
63 |
public HttpServer() { |
|
64 |
try { |
|
54746
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
65 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
66 |
ss = new ServerSocket(); |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
67 |
ss.bind(new InetSocketAddress(loopback, 0)); |
2 | 68 |
} catch (IOException ioe) { |
69 |
throw new Error("Unable to create ServerSocket: " + ioe); |
|
70 |
} |
|
71 |
} |
|
72 |
||
73 |
public int port() { |
|
74 |
return ss.getLocalPort(); |
|
75 |
} |
|
76 |
||
54746
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
77 |
public String authority() { |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
78 |
InetAddress address = ss.getInetAddress(); |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
79 |
String hostaddr = address.isAnyLocalAddress() |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
80 |
? "localhost" : address.getHostAddress(); |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
81 |
if (hostaddr.indexOf(':') > -1) { |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
82 |
hostaddr = "[" + hostaddr + "]"; |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
83 |
} |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
84 |
return hostaddr + ":" + port(); |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
85 |
} |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
86 |
|
2 | 87 |
public void shutdown() throws IOException { |
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54746
diff
changeset
|
88 |
shutdown = true; |
2 | 89 |
ss.close(); |
90 |
} |
|
91 |
||
92 |
public void run() { |
|
93 |
Object[][] tests = getTests(); |
|
94 |
||
95 |
try { |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54746
diff
changeset
|
96 |
while(!shutdown) { |
2 | 97 |
Socket s = ss.accept(); |
98 |
||
99 |
BufferedReader in = new BufferedReader( |
|
100 |
new InputStreamReader( |
|
101 |
s.getInputStream())); |
|
102 |
String req = in.readLine(); |
|
103 |
int pos1 = req.indexOf(' '); |
|
104 |
int pos2 = req.indexOf(' ', pos1+1); |
|
105 |
||
106 |
int i = Integer.parseInt(req.substring(pos1+2, pos2)); |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54746
diff
changeset
|
107 |
System.out.println("Server replying to >" + tests[i][0] + "<"); |
2 | 108 |
|
109 |
PrintStream out = new PrintStream( |
|
110 |
new BufferedOutputStream( |
|
111 |
s.getOutputStream() )); |
|
112 |
||
113 |
out.print( tests[i][0] ); |
|
114 |
out.print("\r\n"); |
|
115 |
out.print("Content-Length: 0\r\n"); |
|
116 |
out.print("Connection: close\r\n"); |
|
117 |
out.print("\r\n"); |
|
118 |
out.flush(); |
|
119 |
||
120 |
s.shutdownOutput(); |
|
121 |
s.close(); |
|
122 |
} |
|
123 |
} catch (Exception e) { |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54746
diff
changeset
|
124 |
if (!shutdown) { |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54746
diff
changeset
|
125 |
e.printStackTrace(); |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54746
diff
changeset
|
126 |
} |
2 | 127 |
} |
128 |
} |
|
129 |
} |
|
130 |
||
131 |
||
132 |
public static void main(String args[]) throws Exception { |
|
133 |
||
134 |
/* start the http server */ |
|
135 |
HttpServer svr = new HttpServer(); |
|
136 |
(new Thread(svr)).start(); |
|
137 |
||
54746
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
138 |
String authority = svr.authority(); |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
139 |
System.out.println("Server listening on: " + authority); |
2 | 140 |
|
141 |
/* |
|
142 |
* Iterate through each test case and check that getResponseCode |
|
143 |
* returns the expected result. |
|
144 |
*/ |
|
145 |
int failures = 0; |
|
146 |
Object tests[][] = getTests(); |
|
147 |
for (int i=0; i<tests.length; i++) { |
|
148 |
||
149 |
System.out.println("******************"); |
|
150 |
System.out.println("Test with response: >" + tests[i][0] + "<"); |
|
151 |
||
54746
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
47216
diff
changeset
|
152 |
URL url = new URL("http://" + authority + "/" + i); |
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
58009
diff
changeset
|
153 |
HttpURLConnection http = (HttpURLConnection)url.openConnection(NO_PROXY); |
2 | 154 |
|
155 |
try { |
|
156 |
||
157 |
// test getResponseCode |
|
158 |
// |
|
159 |
int expectedCode = Integer.parseInt((String)tests[i][1]); |
|
160 |
int actualCode = http.getResponseCode(); |
|
161 |
if (actualCode != expectedCode) { |
|
162 |
System.out.println("getResponseCode returned: " + actualCode + |
|
163 |
", expected: " + expectedCode); |
|
164 |
failures++; |
|
165 |
continue; |
|
166 |
} |
|
167 |
||
168 |
// test getResponseMessage |
|
169 |
// |
|
170 |
String expectedPhrase = (String)tests[i][2]; |
|
171 |
String actualPhrase = http.getResponseMessage(); |
|
172 |
if (actualPhrase == null && expectedPhrase == null) { |
|
173 |
continue; |
|
174 |
} |
|
175 |
if (!actualPhrase.equals(expectedPhrase)) { |
|
176 |
System.out.println("getResponseMessage returned: " + |
|
177 |
actualPhrase + ", expected: " + expectedPhrase); |
|
178 |
} |
|
179 |
} catch (IOException e) { |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
54746
diff
changeset
|
180 |
System.err.println("Test failed for >" + tests[i][0] + "<: " + e); |
2 | 181 |
e.printStackTrace(); |
182 |
failures++; |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
/* shutdown http server */ |
|
187 |
svr.shutdown(); |
|
188 |
||
189 |
if (failures > 0) { |
|
190 |
throw new Exception(failures + " sub-test(s) failed."); |
|
191 |
} |
|
192 |
} |
|
193 |
} |