author | dholmes |
Wed, 06 Nov 2019 21:18:42 -0500 | |
changeset 58956 | 9a0a5e70eeb2 |
parent 55330 | 1fef7d9309a9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2006, 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 6393710 |
|
27 |
* @summary Non authenticated call followed by authenticated call never returns |
|
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
28 |
* @run main B6393710 |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
29 |
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6393710 |
2 | 30 |
*/ |
31 |
||
32 |
import com.sun.net.httpserver.*; |
|
33 |
||
34 |
import java.util.*; |
|
35 |
import java.util.concurrent.*; |
|
36 |
import java.io.*; |
|
37 |
import java.net.*; |
|
38 |
||
39 |
/* |
|
40 |
* Test checks for following bug(s) when a POST containing a request body |
|
41 |
* needs to be authenticated |
|
42 |
* |
|
43 |
* 1) we were not reading the request body |
|
44 |
* |
|
45 |
* 2) we were not re-enabling the interestops for the socket channel |
|
46 |
*/ |
|
47 |
||
48 |
public class B6393710 { |
|
49 |
||
50 |
static String CRLF = "\r\n"; |
|
51 |
||
52 |
/* Two post requests containing data. The second one |
|
53 |
* has the expected authorization credentials |
|
54 |
*/ |
|
55 |
static String cmd = |
|
56 |
"POST /test/foo HTTP/1.1"+CRLF+ |
|
57 |
"Content-Length: 22"+CRLF+ |
|
58 |
"Pragma: no-cache"+CRLF+ |
|
59 |
"Cache-Control: no-cache"+CRLF+ CRLF+ |
|
60 |
"<item desc=\"excuse\" />"+ |
|
61 |
"POST /test/foo HTTP/1.1"+CRLF+ |
|
62 |
"Content-Length: 22"+CRLF+ |
|
63 |
"Pragma: no-cache"+CRLF+ |
|
64 |
"Authorization: Basic ZnJlZDpmcmVkcGFzc3dvcmQ="+CRLF+ |
|
65 |
"Cache-Control: no-cache"+CRLF+ CRLF+ |
|
66 |
"<item desc=\"excuse\" />"; |
|
67 |
||
68 |
public static void main (String[] args) throws Exception { |
|
69 |
Handler handler = new Handler(); |
|
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
70 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
71 |
InetSocketAddress addr = new InetSocketAddress (loopback, 0); |
2 | 72 |
HttpServer server = HttpServer.create (addr, 0); |
73 |
HttpContext ctx = server.createContext ("/test", handler); |
|
74 |
ctx.setAuthenticator (new BasicAuthenticator ("test") { |
|
75 |
public boolean checkCredentials (String user, String pass) { |
|
76 |
return user.equals ("fred") && pass.equals("fredpassword"); |
|
77 |
} |
|
78 |
}); |
|
79 |
||
80 |
server.start (); |
|
81 |
||
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
82 |
Socket s = new Socket (loopback, server.getAddress().getPort()); |
2 | 83 |
s.setSoTimeout (5000); |
84 |
||
85 |
OutputStream os = s.getOutputStream(); |
|
86 |
os.write (cmd.getBytes()); |
|
87 |
InputStream is = s.getInputStream (); |
|
88 |
try { |
|
89 |
ok = readAndCheck (is, "401 Unauthorized") && |
|
90 |
readAndCheck (is, "200 OK"); |
|
91 |
} catch (SocketTimeoutException e) { |
|
92 |
System.out.println ("Did not received expected data"); |
|
93 |
ok = false; |
|
94 |
} finally { |
|
95 |
s.close(); |
|
96 |
server.stop(2); |
|
97 |
} |
|
98 |
||
99 |
if (requests != 1) { |
|
100 |
throw new RuntimeException ("server handler did not receive the request"); |
|
101 |
} |
|
102 |
if (!ok) { |
|
103 |
throw new RuntimeException ("did not get 200 OK"); |
|
104 |
} |
|
105 |
System.out.println ("OK"); |
|
106 |
} |
|
107 |
||
108 |
/* check for expected string and return true if found in stream */ |
|
109 |
||
110 |
static boolean readAndCheck (InputStream is, String expected) throws IOException { |
|
111 |
int c; |
|
112 |
int count = 0; |
|
113 |
int expLen = expected.length(); |
|
114 |
expected = expected.toLowerCase(); |
|
115 |
||
116 |
while ((c=is.read()) != -1) { |
|
117 |
c = Character.toLowerCase (c); |
|
118 |
if (c == expected.charAt (count)) { |
|
119 |
count ++; |
|
120 |
if (count == expLen) { |
|
121 |
return true; |
|
122 |
} |
|
123 |
} else { |
|
124 |
count = 0; |
|
125 |
} |
|
126 |
} |
|
127 |
return false; |
|
128 |
} |
|
129 |
||
130 |
public static boolean ok = false; |
|
131 |
static int requests = 0; |
|
132 |
||
133 |
static class Handler implements HttpHandler { |
|
134 |
int invocation = 1; |
|
135 |
public void handle (HttpExchange t) |
|
136 |
throws IOException |
|
137 |
{ |
|
138 |
int count = 0; |
|
139 |
InputStream is = t.getRequestBody(); |
|
140 |
Headers map = t.getRequestHeaders(); |
|
141 |
Headers rmap = t.getResponseHeaders(); |
|
142 |
while (is.read () != -1) { |
|
143 |
count ++; |
|
144 |
} |
|
145 |
if (count != 22) { |
|
146 |
System.out.println ("Handler expected 22. got " + count); |
|
147 |
ok = false; |
|
148 |
} |
|
149 |
is.close(); |
|
150 |
t.sendResponseHeaders (200, -1); |
|
151 |
t.close(); |
|
152 |
requests ++; |
|
153 |
} |
|
154 |
} |
|
155 |
} |