Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/HttpsURLConnection/DummyCacheResponse.java
41152 views
1
/*
2
* Copyright (c) 2018, 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 8212261
27
* @summary Add SSLSession accessors to HttpsURLConnection and
28
* SecureCacheResponse
29
* @library /test/lib
30
* @modules jdk.httpserver
31
* @build jdk.test.lib.net.SimpleSSLContext
32
* @run main/othervm DummyCacheResponse
33
*/
34
35
import java.io.*;
36
import java.net.*;
37
import javax.net.ssl.*;
38
import java.util.*;
39
import java.util.concurrent.*;
40
import java.security.Principal;
41
import java.security.cert.Certificate;
42
import jdk.test.lib.net.SimpleSSLContext;
43
import com.sun.net.httpserver.*;
44
45
public class DummyCacheResponse extends SecureCacheResponse {
46
static SSLContext sslContext;
47
private final SSLSession cachedSession;
48
private final Map<String, List<String>> rqstHeaders;
49
50
public static void main(String[] args) throws Exception {
51
ResponseCache reservedResponseCache = ResponseCache.getDefault();
52
HttpsServer httpsServer = null;
53
ExecutorService executor = null;
54
try {
55
ResponseCache.setDefault(new DummyResponseCache());
56
57
httpsServer = HttpsServer.create(new InetSocketAddress(0), 0);
58
HttpContext c2 =
59
httpsServer.createContext("/test", new HttpsHandler());
60
61
executor = Executors.newCachedThreadPool();
62
httpsServer.setExecutor(executor);
63
64
sslContext = new SimpleSSLContext().get();
65
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
66
httpsServer.start();
67
68
int httpsPort = httpsServer.getAddress().getPort();
69
System.out.println(
70
"Server address: " + httpsServer.getAddress());
71
72
// the 1st connection
73
runTest(httpsPort, false);
74
75
// the 2nd connection that use the cache
76
runTest(httpsPort, true);
77
} finally {
78
if (httpsServer != null) {
79
httpsServer.stop(2);
80
}
81
if (executor != null) {
82
executor.shutdown();
83
}
84
85
ResponseCache.setDefault(reservedResponseCache);
86
}
87
}
88
89
private static class HttpsHandler implements HttpHandler {
90
public void handle(HttpExchange httpExchange) throws IOException {
91
InputStream is = httpExchange.getRequestBody();
92
93
while (is.read() != -1) {
94
// read to EOF
95
}
96
is.close();
97
98
httpExchange.sendResponseHeaders(200, 0);
99
httpExchange.close();
100
}
101
}
102
103
static void runTest(int port, boolean useCache) throws Exception {
104
URL url = new URL(
105
String.format("https://localhost:%s/test/", port));
106
HttpsURLConnection urlc =
107
(HttpsURLConnection)url.openConnection();
108
109
urlc.setSSLSocketFactory(sslContext.getSocketFactory());
110
urlc.setHostnameVerifier(new HostnameVerifier() {
111
public boolean verify(String s, SSLSession s1) {
112
return true;
113
}
114
});
115
116
try (InputStream is = urlc.getInputStream()) {
117
while (is.read() != -1) {
118
// read to EOF
119
}
120
121
SSLSession session = urlc.getSSLSession().orElseThrow();
122
if (!Objects.equals(urlc.getCipherSuite(),
123
session.getCipherSuite())) {
124
throw new Exception(
125
"Incorrect SSLSession for HTTPsURLConnection: " +
126
urlc.getCipherSuite() + "/" + session.getCipherSuite());
127
}
128
129
// Make sure the cache implementation is used.
130
try {
131
urlc.getServerCertificates();
132
if (useCache) {
133
throw new Exception(
134
"The SecureCacheResponse impl should be used");
135
}
136
} catch (UnsupportedOperationException uoe) {
137
if (!useCache) {
138
throw new Exception(
139
"The SecureCacheResponse impl should not be used");
140
}
141
}
142
}
143
}
144
145
DummyCacheResponse(SSLSession sslSession,
146
Map<String, List<String>> rqstHeaders) {
147
this.rqstHeaders = rqstHeaders;
148
this.cachedSession = sslSession;
149
}
150
151
@Override
152
public String getCipherSuite() {
153
return cachedSession.getCipherSuite();
154
}
155
156
@Override
157
public List<Certificate> getLocalCertificateChain() {
158
throw new UnsupportedOperationException("Not supported yet.");
159
}
160
161
@Override
162
public List<Certificate> getServerCertificateChain()
163
throws SSLPeerUnverifiedException {
164
throw new UnsupportedOperationException("Not supported yet.");
165
}
166
167
@Override
168
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
169
throw new UnsupportedOperationException("Not supported yet.");
170
}
171
172
@Override
173
public Principal getLocalPrincipal() {
174
throw new UnsupportedOperationException("Not supported yet.");
175
}
176
177
@Override
178
public Map<String, List<String>> getHeaders() throws IOException {
179
return rqstHeaders;
180
}
181
182
@Override
183
public InputStream getBody() throws IOException {
184
return new ByteArrayInputStream(new byte[0]);
185
}
186
187
@Override
188
public Optional<SSLSession> getSSLSession() {
189
return Optional.of(cachedSession);
190
}
191
192
private static class DummyResponseCache extends ResponseCache {
193
Map<URI, SSLSession> httpsConnections = new HashMap<>();
194
195
@Override
196
public CacheResponse get(URI uri, String rqstMethod,
197
Map<String, List<String>> rqstHeaders) throws IOException {
198
if (httpsConnections.containsKey(uri)) {
199
return new DummyCacheResponse(
200
httpsConnections.get(uri), rqstHeaders);
201
}
202
203
return null;
204
}
205
206
@Override
207
public CacheRequest put(URI uri,
208
URLConnection conn) throws IOException {
209
if (conn instanceof HttpsURLConnection) {
210
HttpsURLConnection httpsConn = (HttpsURLConnection)conn;
211
httpsConnections.putIfAbsent(
212
uri, httpsConn.getSSLSession().orElseThrow());
213
}
214
215
return null;
216
}
217
}
218
}
219
220