Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/HttpsURLConnection/HttpsSession.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 HttpsSession
33
*/
34
import com.sun.net.httpserver.*;
35
import java.net.*;
36
import java.io.*;
37
import javax.net.ssl.*;
38
import java.util.concurrent.*;
39
import java.util.Objects;
40
import jdk.test.lib.net.SimpleSSLContext;
41
42
public class HttpsSession {
43
44
static SSLContext sslContext;
45
46
public static void main(String[] args) throws Exception {
47
HttpsServer httpsServer = null;
48
ExecutorService executor = null;
49
try {
50
httpsServer = HttpsServer.create(new InetSocketAddress(0), 0);
51
HttpContext c2 =
52
httpsServer.createContext("/test", new HttpsHandler());
53
54
executor = Executors.newCachedThreadPool();
55
httpsServer.setExecutor(executor);
56
57
sslContext = new SimpleSSLContext().get();
58
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
59
httpsServer.start();
60
61
int httpsPort = httpsServer.getAddress().getPort();
62
System.out.println(
63
"Server address: " + httpsServer.getAddress());
64
65
runTest(httpsPort);
66
} finally {
67
if (httpsServer != null) {
68
httpsServer.stop(2);
69
}
70
if (executor != null) {
71
executor.shutdown();
72
}
73
}
74
}
75
76
private static class HttpsHandler implements HttpHandler {
77
public void handle(HttpExchange httpExchange) throws IOException {
78
InputStream is = httpExchange.getRequestBody();
79
80
while (is.read() != -1) {
81
// read to EOF
82
}
83
is.close();
84
85
httpExchange.sendResponseHeaders(200, 0);
86
httpExchange.close();
87
}
88
}
89
90
static void runTest(int port) throws Exception {
91
URL url = new URL(
92
String.format("https://localhost:%s/test/", port));
93
HttpsURLConnection urlc =
94
(HttpsURLConnection)url.openConnection();
95
96
urlc.setSSLSocketFactory(sslContext.getSocketFactory());
97
urlc.setHostnameVerifier(new HostnameVerifier() {
98
public boolean verify(String s, SSLSession s1) {
99
return true;
100
}
101
});
102
103
try {
104
urlc.getSSLSession();
105
throw new Exception(
106
"HttpsURLConnection.getSSLSession() should throw " +
107
"IllegalStateException before the connection established");
108
} catch (IllegalStateException ise) {
109
// That's the expected behavior, continue.
110
}
111
112
try (InputStream is = urlc.getInputStream()) {
113
while (is.read() != -1) {
114
// read to EOF
115
}
116
117
SSLSession session = urlc.getSSLSession().orElseThrow();
118
if (!Objects.equals(urlc.getCipherSuite(),
119
session.getCipherSuite())) {
120
throw new Exception(
121
"Incorrect SSLSession for HTTPsURLConnection: " +
122
urlc.getCipherSuite() + "/" + session.getCipherSuite());
123
}
124
}
125
}
126
}
127
128