Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsClient/ProxyAuthTest.java
38889 views
1
/*
2
* Copyright (c) 2001, 2017, 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
// SunJSSE does not support dynamic system properties, no way to re-use
26
// system properties in samevm/agentvm mode.
27
//
28
29
/*
30
* @test
31
* @bug 4323990 4413069 8160838
32
* @summary HttpsURLConnection doesn't send Proxy-Authorization on CONNECT
33
* Incorrect checking of proxy server response
34
* @library /javax/net/ssl/templates
35
* @run main/othervm ProxyAuthTest fail
36
* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Basic
37
* ProxyAuthTest fail
38
* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Basic,
39
* ProxyAuthTest fail
40
* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=BAsIc
41
* ProxyAuthTest fail
42
* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Basic,Digest
43
* ProxyAuthTest fail
44
* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=Unknown,bAsIc
45
* ProxyAuthTest fail
46
* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=
47
* ProxyAuthTest succeed
48
* @run main/othervm
49
* -Djdk.http.auth.tunneling.disabledSchemes=Digest,NTLM,Negotiate
50
* ProxyAuthTest succeed
51
* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes=UNKNOWN,notKnown
52
* ProxyAuthTest succeed
53
*/
54
55
import java.io.BufferedReader;
56
import java.io.DataOutputStream;
57
import java.io.IOException;
58
import java.io.InputStreamReader;
59
import java.net.Authenticator;
60
import java.net.InetSocketAddress;
61
import java.net.PasswordAuthentication;
62
import java.net.Proxy;
63
import java.net.URL;
64
import javax.net.ssl.HostnameVerifier;
65
import javax.net.ssl.HttpsURLConnection;
66
import javax.net.ssl.SSLSession;
67
import javax.net.ssl.SSLSocket;
68
import javax.net.ssl.SSLContext;
69
import static java.nio.charset.StandardCharsets.US_ASCII;
70
71
/*
72
* ProxyAuthTest.java -- includes a simple server that can serve
73
* Http get request in both clear and secure channel, and a client
74
* that makes https requests behind the firewall through an
75
* authentication proxy
76
*/
77
78
public class ProxyAuthTest extends SSLSocketTemplate {
79
private static boolean expectSuccess;
80
81
/*
82
* Run the test case.
83
*/
84
public static void main(String[] args) throws Exception {
85
// Get the customized arguments.
86
parseArguments(args);
87
88
(new ProxyAuthTest()).run();
89
}
90
91
@Override
92
protected boolean isCustomizedClientConnection() {
93
return true;
94
}
95
96
@Override
97
protected void runServerApplication(SSLSocket socket) throws Exception {
98
String response = "Proxy authentication for tunneling succeeded ..";
99
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
100
try {
101
BufferedReader in = new BufferedReader(
102
new InputStreamReader(socket.getInputStream()));
103
104
// read the request
105
readRequest(in);
106
107
// retrieve bytecodes
108
byte[] bytecodes = response.getBytes(US_ASCII);
109
110
// send bytecodes in response (assumes HTTP/1.0 or later)
111
out.writeBytes("HTTP/1.0 200 OK\r\n");
112
out.writeBytes("Content-Length: " + bytecodes.length + "\r\n");
113
out.writeBytes("Content-Type: text/html\r\n\r\n");
114
out.write(bytecodes);
115
out.flush();
116
} catch (IOException e) {
117
// write out error response
118
out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");
119
out.writeBytes("Content-Type: text/html\r\n\r\n");
120
out.flush();
121
}
122
}
123
124
@Override
125
protected void runClientApplication(int serverPort) throws Exception {
126
/*
127
* Set the default SSLSocketFactory.
128
*/
129
SSLContext context = createClientSSLContext();
130
HttpsURLConnection.setDefaultSSLSocketFactory(
131
context.getSocketFactory());
132
133
/*
134
* setup up a proxy with authentication information
135
*/
136
ProxyTunnelServer ps = setupProxy();
137
138
/*
139
* we want to avoid URLspoofCheck failures in cases where the cert
140
* DN name does not match the hostname in the URL.
141
*/
142
HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
143
144
InetSocketAddress paddr =
145
new InetSocketAddress("localhost", ps.getPort());
146
Proxy proxy = new Proxy(Proxy.Type.HTTP, paddr);
147
148
URL url = new URL(
149
"https://" + "localhost:" + serverPort + "/index.html");
150
BufferedReader in = null;
151
HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(proxy);
152
try {
153
in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
154
String inputLine;
155
System.out.print("Client recieved from the server: ");
156
while ((inputLine = in.readLine()) != null) {
157
System.out.println(inputLine);
158
}
159
if (!expectSuccess) {
160
throw new RuntimeException(
161
"Expected exception/failure to connect, but succeeded.");
162
}
163
} catch (IOException e) {
164
if (expectSuccess) {
165
System.out.println("Client side failed: " + e.getMessage());
166
throw e;
167
}
168
169
// Assert that the error stream is not accessible from the failed
170
// tunnel setup.
171
if (uc.getErrorStream() != null) {
172
throw new RuntimeException("Unexpected error stream.");
173
}
174
175
if (!e.getMessage().contains("Unable to tunnel through proxy") ||
176
!e.getMessage().contains("407")) {
177
178
throw new RuntimeException(
179
"Expected exception about cannot tunnel, " +
180
"407, etc, but got", e);
181
} else {
182
// Informative
183
System.out.println(
184
"Caught expected exception: " + e.getMessage());
185
}
186
} finally {
187
if (in != null) {
188
in.close();
189
}
190
}
191
}
192
193
194
private static void parseArguments(String[] args) {
195
if (args[0].equals("succeed")) {
196
expectSuccess = true;
197
} else {
198
expectSuccess = false;
199
}
200
}
201
202
/**
203
* read the response, don't care for the syntax of the request-line
204
* for this testing
205
*/
206
private static void readRequest(BufferedReader in) throws IOException {
207
String line = null;
208
System.out.println("Server received: ");
209
do {
210
if (line != null) {
211
System.out.println(line);
212
}
213
line = in.readLine();
214
} while ((line.length() != 0) &&
215
(line.charAt(0) != '\r') && (line.charAt(0) != '\n'));
216
}
217
218
private static class NameVerifier implements HostnameVerifier {
219
220
@Override
221
public boolean verify(String hostname, SSLSession session) {
222
return true;
223
}
224
}
225
226
private static ProxyTunnelServer setupProxy() throws IOException {
227
ProxyTunnelServer pserver = new ProxyTunnelServer();
228
229
/*
230
* register a system wide authenticator and setup the proxy for
231
* authentication
232
*/
233
Authenticator.setDefault(new TestAuthenticator());
234
235
// register with the username and password
236
pserver.needUserAuth(true);
237
pserver.setUserAuth("Test", "test123");
238
239
pserver.start();
240
return pserver;
241
}
242
243
private static class TestAuthenticator extends Authenticator {
244
245
@Override
246
public PasswordAuthentication getPasswordAuthentication() {
247
return new PasswordAuthentication("Test", "test123".toCharArray());
248
}
249
}
250
}
251
252