Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Authenticator/B4678055.java
41149 views
1
/*
2
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
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 4678055
27
* @library /test/lib
28
* @run main/othervm B4678055
29
* @run main/othervm -Djava.net.preferIPv6Addresses=true B4678055
30
* @summary Basic Authentication fails with multiple realms
31
*/
32
33
import java.io.IOException;
34
import java.io.InputStream;
35
import java.io.PrintWriter;
36
import java.net.Authenticator;
37
import java.net.InetAddress;
38
import java.net.InetSocketAddress;
39
import java.net.PasswordAuthentication;
40
import java.net.ProxySelector;
41
import java.net.URL;
42
import java.net.URLConnection;
43
import java.util.concurrent.Executors;
44
45
import com.sun.net.httpserver.HttpExchange;
46
import com.sun.net.httpserver.HttpHandler;
47
import com.sun.net.httpserver.HttpServer;
48
import jdk.test.lib.net.URIBuilder;
49
50
public class B4678055 implements HttpHandler {
51
52
static volatile int count = 0;
53
static volatile String authstring;
54
55
private void errorReply(HttpExchange req, String reply) throws IOException {
56
req.getResponseHeaders().set("Connection", "close");
57
req.getResponseHeaders().set("WWW-Authenticate", reply);
58
req.sendResponseHeaders(401, -1);
59
}
60
61
private void okReply (HttpExchange req) throws IOException {
62
req.sendResponseHeaders(200, 0);
63
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
64
pw.print("Hello .");
65
}
66
}
67
68
public void handle (HttpExchange req) {
69
try {
70
System.out.println("Server handling case: "+ count);
71
if(req.getRequestHeaders().get("Authorization") != null) {
72
authstring = req.getRequestHeaders().get("Authorization").get(0);
73
System.out.println(authstring);
74
}
75
switch (count) {
76
case 0:
77
errorReply (req, "Basic realm=\"wallyworld\"");
78
break;
79
case 1:
80
/* client stores a username/pw for wallyworld
81
*/
82
okReply (req);
83
break;
84
case 2:
85
/* emulates a server that has configured a second
86
* realm, but by misconfiguration uses the same
87
* realm string as the previous one.
88
*
89
* An alternative (more likely) scenario that shows this behavior is
90
* the case where the password in the original realm has changed
91
*/
92
errorReply (req, "Basic realm=\"wallyworld\"");
93
break;
94
case 3:
95
/* The client replies with the username/password
96
* from the first realm, which is wrong (unexpectedly)
97
*/
98
errorReply (req, "Basic realm=\"wallyworld\"");
99
break;
100
case 4:
101
/* The client re-prompts for a password and
102
* we now reply with an OK. The client with the bug
103
* will throw NPE at this point.
104
*/
105
case 5:
106
/* Repeat the OK, to make sure the same new auth string is sent */
107
okReply (req);
108
break;
109
}
110
count ++;
111
} catch (IOException e) {
112
System.err.println("Unexpected exception for case " + count + ": " + e);
113
e.printStackTrace();
114
}
115
}
116
117
static void read (InputStream is) throws IOException {
118
int c;
119
System.out.println ("reading");
120
while ((c=is.read()) != -1) {
121
System.out.write (c);
122
}
123
System.out.println ("");
124
System.out.println ("finished reading");
125
}
126
127
static boolean checkFinalAuth () {
128
return authstring.equals ("Basic dXNlcjpwYXNzMg==");
129
}
130
131
static void client (String u) throws Exception {
132
URL url = new URL (u);
133
System.out.println ("client opening connection to: " + u);
134
URLConnection urlc = url.openConnection ();
135
InputStream is = urlc.getInputStream ();
136
read (is);
137
is.close();
138
}
139
140
static HttpServer server;
141
142
public static void main (String[] args) throws Exception {
143
B4678055 b4678055 = new B4678055();
144
MyAuthenticator auth = new MyAuthenticator ();
145
Authenticator.setDefault (auth);
146
ProxySelector.setDefault(ProxySelector.of(null)); // no proxy
147
try {
148
InetAddress loopback = InetAddress.getLoopbackAddress();
149
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
150
server.createContext("/", b4678055);
151
server.setExecutor(Executors.newSingleThreadExecutor());
152
server.start();
153
String serverURL = URIBuilder.newBuilder()
154
.scheme("http")
155
.loopback()
156
.port(server.getAddress().getPort())
157
.path("/")
158
.build()
159
.toString();
160
System.out.println("Server: listening at: " + serverURL);
161
client(serverURL + "d1/foo.html");
162
client(serverURL + "d2/foo.html");
163
client(serverURL + "d2/foo.html");
164
} catch (Exception e) {
165
System.out.println("Client got exception: " + e);
166
System.out.println("Terminating server");
167
if (server != null) {
168
server.stop(1);
169
}
170
throw e;
171
}
172
int f = auth.getCount();
173
if (f != 2) {
174
except ("Authenticator was called "+f+" times. Should be 2");
175
}
176
/* this checks the authorization string corresponding to second password "pass2"*/
177
if (!checkFinalAuth()) {
178
except ("Wrong authorization string received from client");
179
}
180
System.out.println("Terminating server");
181
server.stop(1);
182
}
183
184
public static void except (String s) {
185
System.out.println("Check failed: " + s);
186
System.out.println("Terminating server");
187
server.stop(1);
188
throw new RuntimeException (s);
189
}
190
191
static class MyAuthenticator extends Authenticator {
192
MyAuthenticator () {
193
super ();
194
}
195
196
volatile int count = 0;
197
198
public PasswordAuthentication getPasswordAuthentication () {
199
PasswordAuthentication pw;
200
if (count == 0) {
201
pw = new PasswordAuthentication ("user", "pass1".toCharArray());
202
} else {
203
pw = new PasswordAuthentication ("user", "pass2".toCharArray());
204
}
205
count ++;
206
return pw;
207
}
208
209
public int getCount () {
210
return (count);
211
}
212
}
213
}
214
215