author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58009 | 0daf32316b47 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55399
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
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 4678055 |
|
30820 | 27 |
* @modules java.base/sun.net.www |
55399
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
28 |
* @library ../../../sun/net/www/httptest/ /test/lib |
13788 | 29 |
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction |
55399
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
30 |
* @run main/othervm B4678055 |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
31 |
* @run main/othervm -Djava.net.preferIPv6Addresses=true B4678055 |
2 | 32 |
* @summary Basic Authentication fails with multiple realms |
33 |
*/ |
|
34 |
||
35 |
import java.io.*; |
|
36 |
import java.net.*; |
|
55399
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
37 |
import jdk.test.lib.net.URIBuilder; |
2 | 38 |
|
39 |
public class B4678055 implements HttpCallback { |
|
40 |
||
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55399
diff
changeset
|
41 |
static volatile int count = 0; |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55399
diff
changeset
|
42 |
static volatile String authstring; |
2 | 43 |
|
44 |
void errorReply (HttpTransaction req, String reply) throws IOException { |
|
45 |
req.addResponseHeader ("Connection", "close"); |
|
46 |
req.addResponseHeader ("WWW-Authenticate", reply); |
|
47 |
req.sendResponse (401, "Unauthorized"); |
|
48 |
req.orderlyClose(); |
|
49 |
} |
|
50 |
||
51 |
void okReply (HttpTransaction req) throws IOException { |
|
52 |
req.setResponseEntityBody ("Hello ."); |
|
53 |
req.sendResponse (200, "Ok"); |
|
54 |
req.orderlyClose(); |
|
55 |
} |
|
56 |
||
57 |
public void request (HttpTransaction req) { |
|
58 |
try { |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55399
diff
changeset
|
59 |
System.out.println("Server handling case: "+ count); |
2 | 60 |
authstring = req.getRequestHeader ("Authorization"); |
61 |
System.out.println (authstring); |
|
62 |
switch (count) { |
|
63 |
case 0: |
|
64 |
errorReply (req, "Basic realm=\"wallyworld\""); |
|
65 |
break; |
|
66 |
case 1: |
|
67 |
/* client stores a username/pw for wallyworld |
|
68 |
*/ |
|
69 |
okReply (req); |
|
70 |
break; |
|
71 |
case 2: |
|
72 |
/* emulates a server that has configured a second |
|
73 |
* realm, but by misconfiguration uses the same |
|
74 |
* realm string as the previous one. |
|
75 |
* |
|
76 |
* An alternative (more likely) scenario that shows this behavior is |
|
77 |
* the case where the password in the original realm has changed |
|
78 |
*/ |
|
79 |
errorReply (req, "Basic realm=\"wallyworld\""); |
|
80 |
break; |
|
81 |
case 3: |
|
82 |
/* The client replies with the username/password |
|
83 |
* from the first realm, which is wrong (unexpectedly) |
|
84 |
*/ |
|
85 |
errorReply (req, "Basic realm=\"wallyworld\""); |
|
86 |
break; |
|
87 |
case 4: |
|
88 |
/* The client re-prompts for a password and |
|
89 |
* we now reply with an OK. The client with the bug |
|
90 |
* will throw NPE at this point. |
|
91 |
*/ |
|
92 |
case 5: |
|
93 |
/* Repeat the OK, to make sure the same new auth string is sent */ |
|
94 |
okReply (req); |
|
95 |
break; |
|
96 |
} |
|
97 |
count ++; |
|
98 |
} catch (IOException e) { |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55399
diff
changeset
|
99 |
System.err.println("Unexpected exception for case " + count + ": " + e); |
2 | 100 |
e.printStackTrace(); |
101 |
} |
|
102 |
} |
|
103 |
||
104 |
static void read (InputStream is) throws IOException { |
|
105 |
int c; |
|
106 |
System.out.println ("reading"); |
|
107 |
while ((c=is.read()) != -1) { |
|
108 |
System.out.write (c); |
|
109 |
} |
|
110 |
System.out.println (""); |
|
111 |
System.out.println ("finished reading"); |
|
112 |
} |
|
113 |
||
114 |
static boolean checkFinalAuth () { |
|
115 |
return authstring.equals ("Basic dXNlcjpwYXNzMg=="); |
|
116 |
} |
|
117 |
||
118 |
static void client (String u) throws Exception { |
|
119 |
URL url = new URL (u); |
|
120 |
System.out.println ("client opening connection to: " + u); |
|
121 |
URLConnection urlc = url.openConnection (); |
|
122 |
InputStream is = urlc.getInputStream (); |
|
123 |
read (is); |
|
124 |
is.close(); |
|
125 |
} |
|
126 |
||
13788 | 127 |
static TestHttpServer server; |
2 | 128 |
|
129 |
public static void main (String[] args) throws Exception { |
|
130 |
MyAuthenticator auth = new MyAuthenticator (); |
|
131 |
Authenticator.setDefault (auth); |
|
55399
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
132 |
ProxySelector.setDefault(ProxySelector.of(null)); // no proxy |
2 | 133 |
try { |
55399
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
134 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
135 |
server = new TestHttpServer(new B4678055(), 1, 10, loopback, 0); |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
136 |
String serverURL = URIBuilder.newBuilder() |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
137 |
.scheme("http") |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
138 |
.loopback() |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
139 |
.port(server.getLocalPort()) |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
140 |
.path("/") |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
141 |
.build() |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
142 |
.toString(); |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
143 |
System.out.println("Server: listening at: " + serverURL); |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
144 |
client(serverURL + "d1/foo.html"); |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
145 |
client(serverURL + "d2/foo.html"); |
46049b8a5658
8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents:
47216
diff
changeset
|
146 |
client(serverURL + "d2/foo.html"); |
2 | 147 |
} catch (Exception e) { |
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55399
diff
changeset
|
148 |
System.out.println("Client got exception: " + e); |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55399
diff
changeset
|
149 |
System.out.println("Terminating server"); |
2 | 150 |
if (server != null) { |
151 |
server.terminate(); |
|
152 |
} |
|
153 |
throw e; |
|
154 |
} |
|
155 |
int f = auth.getCount(); |
|
156 |
if (f != 2) { |
|
157 |
except ("Authenticator was called "+f+" times. Should be 2"); |
|
158 |
} |
|
159 |
/* this checks the authorization string corresponding to second password "pass2"*/ |
|
160 |
if (!checkFinalAuth()) { |
|
161 |
except ("Wrong authorization string received from client"); |
|
162 |
} |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55399
diff
changeset
|
163 |
System.out.println("Terminating server"); |
2 | 164 |
server.terminate(); |
165 |
} |
|
166 |
||
167 |
public static void except (String s) { |
|
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55399
diff
changeset
|
168 |
System.out.println("Check failed: " + s); |
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55399
diff
changeset
|
169 |
System.out.println("Terminating server"); |
2 | 170 |
server.terminate(); |
171 |
throw new RuntimeException (s); |
|
172 |
} |
|
173 |
||
174 |
static class MyAuthenticator extends Authenticator { |
|
175 |
MyAuthenticator () { |
|
176 |
super (); |
|
177 |
} |
|
178 |
||
58009
0daf32316b47
8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents:
55399
diff
changeset
|
179 |
volatile int count = 0; |
2 | 180 |
|
181 |
public PasswordAuthentication getPasswordAuthentication () { |
|
182 |
PasswordAuthentication pw; |
|
183 |
if (count == 0) { |
|
184 |
pw = new PasswordAuthentication ("user", "pass1".toCharArray()); |
|
185 |
} else { |
|
186 |
pw = new PasswordAuthentication ("user", "pass2".toCharArray()); |
|
187 |
} |
|
188 |
count ++; |
|
189 |
return pw; |
|
190 |
} |
|
191 |
||
192 |
public int getCount () { |
|
193 |
return (count); |
|
194 |
} |
|
195 |
} |
|
196 |
} |