Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/net/ssl/TLS/TLSClientPropertyTest.java
41152 views
1
/*
2
* Copyright (c) 2014, 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
/*
25
* @test
26
* @bug 8049432 8069038 8234723 8202343
27
* @summary New tests for TLS property jdk.tls.client.protocols
28
* @summary javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be
29
* updated for JDK-8061210
30
* @modules java.security.jgss
31
* java.security.jgss/sun.security.jgss.krb5
32
* java.security.jgss/sun.security.krb5:+open
33
* java.security.jgss/sun.security.krb5.internal:+open
34
* java.security.jgss/sun.security.krb5.internal.ccache
35
* java.security.jgss/sun.security.krb5.internal.crypto
36
* java.security.jgss/sun.security.krb5.internal.ktab
37
* java.base/sun.security.util
38
* @run main/othervm TLSClientPropertyTest NoProperty
39
* @run main/othervm TLSClientPropertyTest SSLv3
40
* @run main/othervm TLSClientPropertyTest TLSv1
41
* @run main/othervm TLSClientPropertyTest TLSv11
42
* @run main/othervm TLSClientPropertyTest TLSv12
43
* @run main/othervm TLSClientPropertyTest TLSv13
44
* @run main/othervm TLSClientPropertyTest TLS
45
* @run main/othervm TLSClientPropertyTest WrongProperty
46
*/
47
48
import java.security.KeyManagementException;
49
import java.security.NoSuchAlgorithmException;
50
import java.util.Arrays;
51
import java.util.List;
52
import javax.net.ssl.SSLContext;
53
54
/**
55
* Sets the property jdk.tls.client.protocols to one of this protocols:
56
* SSLv3,TLSv1,TLSv1.1,TLSv1.2 and TLSV(invalid) or removes this
57
* property (if any),then validates the default, supported and current
58
* protocols in the SSLContext.
59
*/
60
public class TLSClientPropertyTest {
61
private final String[] expectedSupportedProtos = new String[] {
62
"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"
63
};
64
65
public static void main(String[] args) throws Exception {
66
67
if (args.length < 1) {
68
throw new RuntimeException(
69
"Incorrect arguments,expected arguments: testCase");
70
}
71
72
String[] expectedDefaultProtos;
73
String testCase = args[0];
74
String contextProtocol;
75
switch (testCase) {
76
case "NoProperty":
77
if (System.getProperty("jdk.tls.client.protocols") != null) {
78
System.getProperties().remove("jdk.tls.client.protocols");
79
}
80
contextProtocol = null;
81
expectedDefaultProtos = new String[] {
82
"TLSv1.2", "TLSv1.3"
83
};
84
break;
85
case "SSLv3":
86
contextProtocol = "SSLv3";
87
expectedDefaultProtos = new String[] {
88
};
89
break;
90
case "TLSv1":
91
contextProtocol = "TLSv1";
92
expectedDefaultProtos = new String[] {
93
};
94
break;
95
case "TLSv11":
96
contextProtocol = "TLSv1.1";
97
expectedDefaultProtos = new String[] {
98
};
99
break;
100
case "TLSv12":
101
contextProtocol = "TLSv1.2";
102
expectedDefaultProtos = new String[] {
103
"TLSv1.2"
104
};
105
break;
106
case "TLSv13":
107
case "TLS":
108
contextProtocol = "TLSv1.3";
109
expectedDefaultProtos = new String[] {
110
"TLSv1.2", "TLSv1.3"
111
};
112
break;
113
case "WrongProperty":
114
expectedDefaultProtos = new String[] {};
115
contextProtocol = "TLSV";
116
break;
117
default:
118
throw new RuntimeException("test case is wrong");
119
}
120
if (contextProtocol != null) {
121
System.setProperty("jdk.tls.client.protocols", contextProtocol);
122
}
123
try {
124
TLSClientPropertyTest test = new TLSClientPropertyTest();
125
test.test(contextProtocol, expectedDefaultProtos);
126
if (testCase.equals("WrongProperty")) {
127
throw new RuntimeException(
128
"Test failed: NoSuchAlgorithmException " +
129
"is expected when input wrong protocol");
130
} else {
131
System.out.println("Test " + contextProtocol + " passed");
132
}
133
} catch (NoSuchAlgorithmException nsae) {
134
if (testCase.equals("WrongProperty")) {
135
System.out.println("NoSuchAlgorithmException is expected,"
136
+ contextProtocol + " test passed");
137
} else {
138
throw nsae;
139
}
140
}
141
142
}
143
144
/**
145
* The parameter passed is the user enforced protocol. Does not catch
146
* NoSuchAlgorithmException, WrongProperty test will use it.
147
*/
148
public void test(String expectedContextProto,
149
String[] expectedDefaultProtos) throws NoSuchAlgorithmException {
150
151
SSLContext context = null;
152
try {
153
if (expectedContextProto != null) {
154
context = SSLContext.getInstance(expectedContextProto);
155
context.init(null, null, null);
156
} else {
157
context = SSLContext.getDefault();
158
}
159
printContextDetails(context);
160
} catch (KeyManagementException ex) {
161
error(null, ex);
162
}
163
164
validateContext(expectedContextProto, expectedDefaultProtos, context);
165
}
166
167
/**
168
* Simple print utility for SSLContext's protocol details.
169
*/
170
private void printContextDetails(SSLContext context) {
171
System.out.println("Default Protocols: "
172
+ Arrays.toString(context.getDefaultSSLParameters()
173
.getProtocols()));
174
System.out.println("Supported Protocols: "
175
+ Arrays.toString(context.getSupportedSSLParameters()
176
.getProtocols()));
177
System.out.println("Current Protocol : " + context.getProtocol());
178
179
}
180
181
/**
182
* Error handler.
183
*/
184
private void error(String msg, Throwable tble) {
185
String finalMsg = "FAILED " + (msg != null ? msg : "");
186
if (tble != null) {
187
throw new RuntimeException(finalMsg, tble);
188
}
189
throw new RuntimeException(finalMsg);
190
}
191
192
/**
193
* Validates the SSLContext's protocols against the user enforced protocol.
194
*/
195
private void validateContext(String expectedProto,
196
String[] expectedDefaultProtos, SSLContext context) {
197
if (expectedProto == null) {
198
expectedProto = "Default";
199
}
200
if (!context.getProtocol().equals(expectedProto)) {
201
error("Invalid current protocol: " + context.getProtocol()
202
+ ", Expected:" + expectedProto, null);
203
}
204
List<String> actualDefaultProtos = Arrays.asList(context
205
.getDefaultSSLParameters().getProtocols());
206
for (String p : expectedDefaultProtos) {
207
if (!actualDefaultProtos.contains(p)) {
208
error("Default protocol " + p + "missing", null);
209
}
210
}
211
List<String> actualSupportedProtos = Arrays.asList(context
212
.getSupportedSSLParameters().getProtocols());
213
214
for (String p : expectedSupportedProtos) {
215
if (!actualSupportedProtos.contains(p)) {
216
error("Expected to support protocol:" + p, null);
217
}
218
}
219
}
220
}
221
222