Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/SSLContextImpl/DefaultDTLSEnabledProtocols.java
41152 views
1
/*
2
* Copyright (c) 2013, 2020, 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
// SunJSSE does not support dynamic system properties, no way to re-use
25
// system properties in samevm/agentvm mode.
26
27
/*
28
* @test
29
* @bug 8237474
30
* @summary Test jdk.tls.client.protocols with DTLS
31
* @run main/othervm DefaultDTLSEnabledProtocols
32
*/
33
34
import java.security.Security;
35
import java.util.Arrays;
36
import java.util.HashSet;
37
import java.util.Set;
38
39
import javax.net.SocketFactory;
40
import javax.net.ssl.KeyManager;
41
import javax.net.ssl.SSLContext;
42
import javax.net.ssl.SSLEngine;
43
import javax.net.ssl.SSLParameters;
44
import javax.net.ssl.SSLServerSocket;
45
import javax.net.ssl.SSLServerSocketFactory;
46
import javax.net.ssl.SSLSocket;
47
import javax.net.ssl.TrustManager;
48
49
public class DefaultDTLSEnabledProtocols {
50
static enum ContextVersion {
51
TLS_CV_01("DTLS",
52
new String[] {"DTLSv1.0", "DTLSv1.2"}),
53
TLS_CV_02("DTLSv1.0",
54
new String[] {"DTLSv1.0"}),
55
TLS_CV_03("DTLSv1.2",
56
new String[] {"DTLSv1.0", "DTLSv1.2"});
57
58
final String contextVersion;
59
final String[] enabledProtocols;
60
final static String[] supportedProtocols = new String[] {
61
"DTLSv1.0", "DTLSv1.2"};
62
63
ContextVersion(String contextVersion, String[] enabledProtocols) {
64
this.contextVersion = contextVersion;
65
this.enabledProtocols = enabledProtocols;
66
}
67
}
68
69
private static boolean checkProtocols(String[] target, String[] expected) {
70
boolean success = true;
71
if (target.length == 0) {
72
System.out.println("\tError: No protocols");
73
success = false;
74
}
75
76
if (!protocolEquals(target, expected)) {
77
System.out.println("\tError: Expected to get protocols " +
78
Arrays.toString(expected));
79
success = false;
80
}
81
System.out.println("\t Protocols found " + Arrays.toString(target));
82
83
return success;
84
}
85
86
private static boolean protocolEquals(
87
String[] actualProtocols,
88
String[] expectedProtocols) {
89
if (actualProtocols.length != expectedProtocols.length) {
90
return false;
91
}
92
93
Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols));
94
for (String actual : actualProtocols) {
95
if (set.add(actual)) {
96
return false;
97
}
98
}
99
100
return true;
101
}
102
103
private static boolean checkCipherSuites(String[] target) {
104
boolean success = true;
105
if (target.length == 0) {
106
System.out.println("\tError: No cipher suites");
107
success = false;
108
}
109
110
return success;
111
}
112
113
public static void main(String[] args) throws Exception {
114
// reset the security property to make sure that the algorithms
115
// and keys used in this test are not disabled.
116
Security.setProperty("jdk.tls.disabledAlgorithms", "");
117
118
boolean failed = false;
119
for (ContextVersion cv : ContextVersion.values()) {
120
System.out.println("Checking SSLContext of " + cv.contextVersion);
121
SSLContext context = SSLContext.getInstance(cv.contextVersion);
122
123
// Default SSLContext is initialized automatically.
124
if (!cv.contextVersion.equals("Default")) {
125
// Use default TK, KM and random.
126
context.init((KeyManager[])null, (TrustManager[])null, null);
127
}
128
129
//
130
// Check SSLContext
131
//
132
// Check default SSLParameters of SSLContext
133
System.out.println("\tChecking default SSLParameters");
134
SSLParameters parameters = context.getDefaultSSLParameters();
135
136
String[] protocols = parameters.getProtocols();
137
failed |= !checkProtocols(protocols, cv.enabledProtocols);
138
139
String[] ciphers = parameters.getCipherSuites();
140
failed |= !checkCipherSuites(ciphers);
141
142
// Check supported SSLParameters of SSLContext
143
System.out.println("\tChecking supported SSLParameters");
144
parameters = context.getSupportedSSLParameters();
145
146
protocols = parameters.getProtocols();
147
failed |= !checkProtocols(protocols, cv.supportedProtocols);
148
149
ciphers = parameters.getCipherSuites();
150
failed |= !checkCipherSuites(ciphers);
151
152
//
153
// Check SSLEngine
154
//
155
// Check SSLParameters of SSLEngine
156
System.out.println();
157
System.out.println("\tChecking SSLEngine of this SSLContext");
158
System.out.println("\tChecking SSLEngine.getSSLParameters()");
159
SSLEngine engine = context.createSSLEngine();
160
engine.setUseClientMode(true);
161
parameters = engine.getSSLParameters();
162
163
protocols = parameters.getProtocols();
164
failed |= !checkProtocols(protocols, cv.enabledProtocols);
165
166
ciphers = parameters.getCipherSuites();
167
failed |= !checkCipherSuites(ciphers);
168
169
System.out.println("\tChecking SSLEngine.getEnabledProtocols()");
170
protocols = engine.getEnabledProtocols();
171
failed |= !checkProtocols(protocols, cv.enabledProtocols);
172
173
System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");
174
ciphers = engine.getEnabledCipherSuites();
175
failed |= !checkCipherSuites(ciphers);
176
177
System.out.println("\tChecking SSLEngine.getSupportedProtocols()");
178
protocols = engine.getSupportedProtocols();
179
failed |= !checkProtocols(protocols, cv.supportedProtocols);
180
181
System.out.println(
182
"\tChecking SSLEngine.getSupportedCipherSuites()");
183
ciphers = engine.getSupportedCipherSuites();
184
failed |= !checkCipherSuites(ciphers);
185
186
//
187
// Check SSLSocket
188
//
189
// Check SSLParameters of SSLSocket
190
System.out.println();
191
System.out.println("\tChecking SSLSocket of this SSLContext");
192
try {
193
context.getSocketFactory();
194
failed = true;
195
System.out.println("SSLSocket returned a socket for DTLS");
196
} catch (UnsupportedOperationException e) {
197
System.out.println("\t " + e.getMessage());
198
}
199
200
//
201
// Check SSLServerSocket
202
//
203
// Check SSLParameters of SSLServerSocket
204
System.out.println();
205
System.out.println("\tChecking SSLServerSocket of this SSLContext");
206
try {
207
context.getServerSocketFactory();
208
failed = true;
209
System.out.println("SSLServerSocket returned a socket for DTLS");
210
} catch (UnsupportedOperationException e) {
211
System.out.println("\t " + e.getMessage());
212
}
213
}
214
215
if (failed) {
216
throw new Exception("Run into problems, see log for more details");
217
} else {
218
System.out.println("\t... Success");
219
}
220
}
221
}
222
223