Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/HttpsURLConnection/GetResponseCode.java
41152 views
1
/*
2
* Copyright (c) 2001, 2011, 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 4482187
27
* @summary HttpsClient tests are failing for build 71
28
* @run main/othervm GetResponseCode
29
*
30
* SunJSSE does not support dynamic system properties, no way to re-use
31
* system properties in samevm/agentvm mode.
32
*
33
* @author Yingxian Wang
34
*/
35
import java.io.*;
36
import java.net.*;
37
import javax.net.ssl.*;
38
import java.security.cert.Certificate;
39
40
public class GetResponseCode implements HostnameVerifier {
41
/*
42
* =============================================================
43
* Set the various variables needed for the tests, then
44
* specify what tests to run on each side.
45
*/
46
47
/*
48
* Should we run the client or server in a separate thread?
49
* Both sides can throw exceptions, but do you have a preference
50
* as to which side should be the main thread.
51
*/
52
static boolean separateServerThread = true;
53
54
/*
55
* Where do we find the keystores?
56
*/
57
static String pathToStores = "../etc";
58
static String keyStoreFile = "keystore";
59
static String trustStoreFile = "truststore";
60
static String passwd = "passphrase";
61
62
/*
63
* Is the server ready to serve?
64
*/
65
volatile static boolean serverReady = false;
66
67
/*
68
* Turn on SSL debugging?
69
*/
70
static boolean debug = false;
71
72
/*
73
* If the client or server is doing some kind of object creation
74
* that the other side depends on, and that thread prematurely
75
* exits, you may experience a hang. The test harness will
76
* terminate all hung threads after its timeout has expired,
77
* currently 3 minutes by default, but you might try to be
78
* smart about it....
79
*/
80
81
/*
82
* Define the server side of the test.
83
*
84
* If the server prematurely exits, serverReady will be set to true
85
* to avoid infinite hangs.
86
*/
87
void doServerSide() throws Exception {
88
SSLServerSocketFactory sslssf =
89
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
90
SSLServerSocket sslServerSocket =
91
(SSLServerSocket) sslssf.createServerSocket(serverPort);
92
serverPort = sslServerSocket.getLocalPort();
93
94
/*
95
* Signal Client, we're ready for his connect.
96
*/
97
serverReady = true;
98
99
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
100
OutputStream sslOS = sslSocket.getOutputStream();
101
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sslOS));
102
bw.write("HTTP/1.1 200 OK\r\n\r\n\r\n");
103
bw.flush();
104
Thread.sleep(5000);
105
sslSocket.close();
106
}
107
108
/*
109
* Define the client side of the test.
110
*
111
* If the server prematurely exits, serverReady will be set to true
112
* to avoid infinite hangs.
113
*/
114
void doClientSide() throws Exception {
115
116
/*
117
* Wait for server to get started.
118
*/
119
while (!serverReady) {
120
Thread.sleep(50);
121
}
122
123
URL url = new URL("https://localhost:"+serverPort+"/index.html");
124
HttpsURLConnection urlc = (HttpsURLConnection)url.openConnection();
125
urlc.setHostnameVerifier(this);
126
urlc.getInputStream();
127
128
if (urlc.getResponseCode() == -1) {
129
throw new RuntimeException("getResponseCode() returns -1");
130
}
131
}
132
133
/*
134
* =============================================================
135
* The remainder is just support stuff
136
*/
137
138
// use any free port by default
139
volatile int serverPort = 0;
140
141
volatile Exception serverException = null;
142
volatile Exception clientException = null;
143
144
public static void main(String[] args) throws Exception {
145
String keyFilename =
146
System.getProperty("test.src", "./") + "/" + pathToStores +
147
"/" + keyStoreFile;
148
String trustFilename =
149
System.getProperty("test.src", "./") + "/" + pathToStores +
150
"/" + trustStoreFile;
151
152
System.setProperty("javax.net.ssl.keyStore", keyFilename);
153
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
154
System.setProperty("javax.net.ssl.trustStore", trustFilename);
155
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
156
157
if (debug)
158
System.setProperty("javax.net.debug", "all");
159
160
/*
161
* Start the tests.
162
*/
163
new GetResponseCode();
164
}
165
166
Thread clientThread = null;
167
Thread serverThread = null;
168
169
/*
170
* Primary constructor, used to drive remainder of the test.
171
*
172
* Fork off the other side, then do your work.
173
*/
174
GetResponseCode() throws Exception {
175
if (separateServerThread) {
176
startServer(true);
177
startClient(false);
178
} else {
179
startClient(true);
180
startServer(false);
181
}
182
183
/*
184
* Wait for other side to close down.
185
*/
186
if (separateServerThread) {
187
serverThread.join();
188
} else {
189
clientThread.join();
190
}
191
192
/*
193
* When we get here, the test is pretty much over.
194
*
195
* If the main thread excepted, that propagates back
196
* immediately. If the other thread threw an exception, we
197
* should report back.
198
*/
199
if (serverException != null)
200
throw serverException;
201
if (clientException != null)
202
throw clientException;
203
}
204
205
void startServer(boolean newThread) throws Exception {
206
if (newThread) {
207
serverThread = new Thread() {
208
public void run() {
209
try {
210
doServerSide();
211
} catch (Exception e) {
212
/*
213
* Our server thread just died.
214
*
215
* Release the client, if not active already...
216
*/
217
System.err.println("Server died...");
218
serverReady = true;
219
serverException = e;
220
}
221
}
222
};
223
serverThread.start();
224
} else {
225
doServerSide();
226
}
227
}
228
229
void startClient(boolean newThread) throws Exception {
230
if (newThread) {
231
clientThread = new Thread() {
232
public void run() {
233
try {
234
doClientSide();
235
} catch (Exception e) {
236
/*
237
* Our client thread just died.
238
*/
239
System.err.println("Client died...");
240
clientException = e;
241
}
242
}
243
};
244
clientThread.start();
245
} else {
246
doClientSide();
247
}
248
}
249
250
// Simple test method to blindly agree that hostname and certname match
251
public boolean verify(String hostname, SSLSession session) {
252
return true;
253
}
254
255
}
256
257