Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Authenticator/B4722333.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 4722333
27
* @library /test/lib
28
* @run main/othervm B4722333
29
* @summary JRE Proxy Authentication Not Working with ISA2000
30
*/
31
32
import java.io.IOException;
33
import java.io.InputStream;
34
import java.io.PrintWriter;
35
import java.net.Authenticator;
36
import java.net.InetAddress;
37
import java.net.InetSocketAddress;
38
import java.net.PasswordAuthentication;
39
import java.net.URL;
40
import java.net.URLConnection;
41
import java.util.concurrent.Executors;
42
43
import com.sun.net.httpserver.HttpExchange;
44
import com.sun.net.httpserver.HttpHandler;
45
import com.sun.net.httpserver.HttpServer;
46
import jdk.test.lib.net.URIBuilder;
47
48
public class B4722333 implements HttpHandler {
49
50
static int count = 0;
51
52
static String[][] expected = {
53
/* scheme realm/prompt */
54
{"basic", "foo"},
55
{"basic", "foobar"},
56
{"digest", "biz"},
57
{"digest", "bizbar"},
58
{"digest", "foobiz"}
59
};
60
61
public void handle(HttpExchange req) {
62
try {
63
if (count % 2 == 1) {
64
req.sendResponseHeaders(200, 0);
65
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
66
pw.print("Hello .");
67
}
68
} else {
69
switch (count) {
70
case 0:
71
req.getResponseHeaders().set("Connection", "close");
72
req.getResponseHeaders().set("WWW-Authenticate", "Basic realm=\"foo\"");
73
req.getResponseHeaders().add("WWW-Authenticate", "Foo realm=\"bar\"");
74
req.sendResponseHeaders(401, -1);
75
break;
76
case 2:
77
req.getResponseHeaders().set("Connection", "close");
78
req.getResponseHeaders().set("WWW-Authenticate", "Basic realm=\"foobar\" Foo realm=\"bar\"");
79
req.sendResponseHeaders(401, -1);
80
break;
81
case 4:
82
req.getResponseHeaders().set("Connection", "close");
83
req.getResponseHeaders().set("WWW-Authenticate", "Digest realm=biz domain=/foo nonce=thisisanonce ");
84
req.getResponseHeaders().add("WWW-Authenticate", "Basic realm=bizbar");
85
req.sendResponseHeaders(401, -1);
86
break;
87
case 6:
88
req.getResponseHeaders().set("Connection", "close");
89
req.getResponseHeaders().set("WWW-Authenticate", "Digest realm=\"bizbar\" domain=/biz nonce=\"hereisanonce\" Basic realm=\"foobar\" Foo realm=\"bar\"");
90
req.sendResponseHeaders(401, -1);
91
break;
92
case 8:
93
req.getResponseHeaders().set("Connection", "close");
94
req.getResponseHeaders().set("WWW-Authenticate", "Foo p1=1 p2=2 p3=3 p4=4 p5=5 p6=6 p7=7 p8=8 p9=10 Digest realm=foobiz domain=/foobiz nonce=newnonce");
95
req.getResponseHeaders().add("WWW-Authenticate", "Basic realm=bizbar");
96
req.sendResponseHeaders(401, -1);
97
break;
98
}
99
}
100
count ++;
101
} catch (IOException e) {
102
e.printStackTrace();
103
}
104
}
105
106
static void read(InputStream is) throws IOException {
107
int c;
108
System.out.println("reading");
109
while ((c=is.read()) != -1) {
110
System.out.write(c);
111
}
112
System.out.println("");
113
System.out.println("finished reading");
114
}
115
116
117
static void client(String u) throws Exception {
118
URL url = new URL (u);
119
System.out.println("client opening connection to: " + u);
120
URLConnection urlc = url.openConnection ();
121
InputStream is = urlc.getInputStream ();
122
read(is);
123
is.close();
124
}
125
126
static HttpServer server;
127
128
public static void main(String[] args) throws Exception {
129
B4722333 b4722333 = new B4722333();
130
MyAuthenticator auth = new MyAuthenticator();
131
Authenticator.setDefault(auth);
132
try {
133
InetAddress loopback = InetAddress.getLoopbackAddress();
134
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
135
server.createContext("/", b4722333);
136
server.setExecutor(Executors.newSingleThreadExecutor());
137
server.start();
138
System.out.println("Server started: listening on port: " + server.getAddress().getPort());
139
String serverURL = URIBuilder.newBuilder()
140
.scheme("http")
141
.loopback()
142
.port(server.getAddress().getPort())
143
.path("/")
144
.build()
145
.toString();
146
client(serverURL + "d1/d2/d3/foo.html");
147
client(serverURL + "ASD/d3/x.html");
148
client(serverURL + "biz/d3/x.html");
149
client(serverURL + "bar/d3/x.html");
150
client(serverURL + "fuzz/d3/x.html");
151
} catch (Exception e) {
152
if (server != null) {
153
server.stop(1);
154
}
155
throw e;
156
}
157
int f = auth.getCount();
158
if (f != expected.length) {
159
except("Authenticator was called "+f+" times. Should be " + expected.length);
160
}
161
server.stop(1);
162
}
163
164
public static void except(String s) {
165
server.stop(1);
166
throw new RuntimeException(s);
167
}
168
169
static class MyAuthenticator extends Authenticator {
170
MyAuthenticator() {
171
super();
172
}
173
174
int count = 0;
175
176
public PasswordAuthentication getPasswordAuthentication() {
177
System.out.println("Auth called");
178
String scheme = getRequestingScheme();
179
System.out.println("getRequestingScheme() returns " + scheme);
180
String prompt = getRequestingPrompt();
181
System.out.println("getRequestingPrompt() returns " + prompt);
182
183
if (!scheme.equals(expected [count][0])) {
184
B4722333.except("wrong scheme received, " + scheme + " expected " + expected [count][0]);
185
}
186
if (!prompt.equals(expected [count][1])) {
187
B4722333.except("wrong realm received, " + prompt + " expected " + expected [count][1]);
188
}
189
count ++;
190
return (new PasswordAuthentication("user", "passwordNotCheckedAnyway".toCharArray()));
191
}
192
193
public int getCount () {
194
return count;
195
}
196
}
197
198
}
199
200