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/HttpsURLConnection/PostThruProxyWithAuth.java
38889 views
1
/*
2
* Copyright (c) 2001, 2016, 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
import java.io.*;
25
import java.net.*;
26
import java.security.KeyStore;
27
import javax.net.*;
28
import javax.net.ssl.*;
29
30
import jdk.testlibrary.OutputAnalyzer;
31
import jdk.testlibrary.ProcessTools;
32
33
/*
34
* @test
35
* @bug 4423074
36
* @summary This test case is written to test the https POST through a proxy
37
* with proxy authentication. It includes a simple server that serves
38
* http POST method requests in secure channel, and a client that
39
* makes https POST request through a proxy.
40
* @library /lib/testlibrary
41
* @compile OriginServer.java ProxyTunnelServer.java
42
* @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes= PostThruProxyWithAuth
43
*/
44
public class PostThruProxyWithAuth {
45
46
private static final String TEST_SRC = System.getProperty("test.src", ".");
47
private static final int TIMEOUT = 30000;
48
49
/*
50
* Where do we find the keystores?
51
*/
52
static String pathToStores = "../../../../../../javax/net/ssl/etc";
53
static String keyStoreFile = "keystore";
54
static String trustStoreFile = "truststore";
55
static String passwd = "passphrase";
56
57
volatile private static int serverPort = 0;
58
59
/*
60
* The TestServer implements a OriginServer that
61
* processes HTTP requests and responses.
62
*/
63
static class TestServer extends OriginServer {
64
public TestServer(ServerSocket ss) throws Exception {
65
super(ss);
66
}
67
68
/*
69
* Returns an array of bytes containing the bytes for
70
* the data sent in the response.
71
*
72
* @return bytes for the data in the response
73
*/
74
public byte[] getBytes() {
75
return
76
"Https POST thru proxy is successful with proxy authentication".
77
getBytes();
78
}
79
}
80
81
/*
82
* Main method to create the server and client
83
*/
84
public static void main(String args[]) throws Exception {
85
String keyFilename = TEST_SRC + "/" + pathToStores + "/" + keyStoreFile;
86
String trustFilename = TEST_SRC + "/" + pathToStores + "/"
87
+ trustStoreFile;
88
89
System.setProperty("javax.net.ssl.keyStore", keyFilename);
90
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
91
System.setProperty("javax.net.ssl.trustStore", trustFilename);
92
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
93
94
boolean useSSL = true;
95
/*
96
* setup the server
97
*/
98
try {
99
ServerSocketFactory ssf = getServerSocketFactory(useSSL);
100
ServerSocket ss = ssf.createServerSocket(serverPort);
101
ss.setSoTimeout(TIMEOUT); // 30 seconds
102
serverPort = ss.getLocalPort();
103
new TestServer(ss);
104
} catch (Exception e) {
105
System.out.println("Server side failed:" +
106
e.getMessage());
107
throw e;
108
}
109
// trigger the client
110
try {
111
doClientSide();
112
} catch (Exception e) {
113
System.out.println("Client side failed: " +
114
e.getMessage());
115
throw e;
116
}
117
}
118
119
private static ServerSocketFactory getServerSocketFactory
120
(boolean useSSL) throws Exception {
121
if (useSSL) {
122
// set up key manager to do server authentication
123
SSLContext ctx = SSLContext.getInstance("TLS");
124
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
125
KeyStore ks = KeyStore.getInstance("JKS");
126
char[] passphrase = passwd.toCharArray();
127
128
ks.load(new FileInputStream(System.getProperty(
129
"javax.net.ssl.keyStore")), passphrase);
130
kmf.init(ks, passphrase);
131
ctx.init(kmf.getKeyManagers(), null, null);
132
133
return ctx.getServerSocketFactory();
134
} else {
135
return ServerSocketFactory.getDefault();
136
}
137
}
138
139
/*
140
* Message to be posted
141
*/
142
static String postMsg = "Testing HTTP post on a https server";
143
144
static void doClientSide() throws Exception {
145
/*
146
* setup up a proxy
147
*/
148
SocketAddress pAddr = setupProxy();
149
150
/*
151
* we want to avoid URLspoofCheck failures in cases where the cert
152
* DN name does not match the hostname in the URL.
153
*/
154
HttpsURLConnection.setDefaultHostnameVerifier(
155
new NameVerifier());
156
URL url = new URL("https://" + getHostname() + ":" + serverPort);
157
158
Proxy p = new Proxy(Proxy.Type.HTTP, pAddr);
159
HttpsURLConnection https = (HttpsURLConnection)url.openConnection(p);
160
https.setConnectTimeout(TIMEOUT);
161
https.setReadTimeout(TIMEOUT);
162
https.setDoOutput(true);
163
https.setRequestMethod("POST");
164
PrintStream ps = null;
165
try {
166
ps = new PrintStream(https.getOutputStream());
167
ps.println(postMsg);
168
ps.flush();
169
if (https.getResponseCode() != 200) {
170
throw new RuntimeException("test Failed");
171
}
172
ps.close();
173
// clear the pipe
174
BufferedReader in = new BufferedReader(
175
new InputStreamReader(
176
https.getInputStream()));
177
String inputLine;
178
while ((inputLine = in.readLine()) != null)
179
System.out.println("Client received: " + inputLine);
180
in.close();
181
} catch (SSLException e) {
182
if (ps != null)
183
ps.close();
184
throw e;
185
} catch (SocketTimeoutException e) {
186
System.out.println("Client can not get response in time: "
187
+ e.getMessage());
188
}
189
}
190
191
static class NameVerifier implements HostnameVerifier {
192
public boolean verify(String hostname, SSLSession session) {
193
return true;
194
}
195
}
196
197
static SocketAddress setupProxy() throws IOException {
198
ProxyTunnelServer pserver = new ProxyTunnelServer();
199
200
/*
201
* register a system wide authenticator and setup the proxy for
202
* authentication
203
*/
204
Authenticator.setDefault(new TestAuthenticator());
205
206
// register with the username and password
207
pserver.needUserAuth(true);
208
pserver.setUserAuth("Test", "test123");
209
210
pserver.start();
211
212
return new InetSocketAddress("localhost", pserver.getPort());
213
}
214
215
public static class TestAuthenticator extends Authenticator {
216
public PasswordAuthentication getPasswordAuthentication() {
217
return new PasswordAuthentication("Test",
218
"test123".toCharArray());
219
}
220
}
221
222
private static String getHostname() {
223
try {
224
OutputAnalyzer oa = ProcessTools.executeCommand("hostname");
225
return oa.getOutput().trim();
226
} catch (Throwable e) {
227
throw new RuntimeException("Get hostname failed.", e);
228
}
229
}
230
}
231
232