Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/SSLEngineImpl/DelegatedTaskWrongException.java
41152 views
1
/*
2
* Copyright (c) 2003, 2014, 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
// SunJSSE does not support dynamic system properties, no way to re-use
26
// system properties in samevm/agentvm mode.
27
//
28
29
/*
30
* @test
31
* @bug 4969459
32
* @summary Delegated tasks are not reflecting the subclasses of SSLException
33
* @run main/othervm DelegatedTaskWrongException
34
*/
35
36
import javax.net.ssl.*;
37
import javax.net.ssl.SSLEngineResult.*;
38
import java.io.*;
39
import java.security.*;
40
import java.nio.*;
41
42
public class DelegatedTaskWrongException {
43
44
private static boolean debug = false;
45
46
private SSLContext sslc;
47
private SSLEngine ssle1; // client
48
private SSLEngine ssle2; // server
49
50
private static String pathToStores = "../../../../javax/net/ssl/etc";
51
private static String keyStoreFile = "keystore";
52
private static String trustStoreFile = "truststore";
53
private static String passwd = "passphrase";
54
55
private static String keyFilename =
56
System.getProperty("test.src", "./") + "/" + pathToStores +
57
"/" + keyStoreFile;
58
private static String trustFilename =
59
System.getProperty("test.src", "./") + "/" + pathToStores +
60
"/" + trustStoreFile;
61
62
private ByteBuffer appOut1; // write side of ssle1
63
private ByteBuffer appIn1; // read side of ssle1
64
private ByteBuffer appOut2; // write side of ssle2
65
private ByteBuffer appIn2; // read side of ssle2
66
67
private ByteBuffer oneToTwo; // "reliable" transport ssle1->ssle2
68
private ByteBuffer twoToOne; // "reliable" transport ssle2->ssle1
69
70
/*
71
* Majority of the test case is here, setup is done below.
72
*/
73
private void createSSLEngines() throws Exception {
74
ssle1 = sslc.createSSLEngine("client", 1);
75
ssle1.setUseClientMode(true);
76
77
ssle2 = sslc.createSSLEngine();
78
ssle2.setUseClientMode(false);
79
80
ssle1.setEnabledProtocols(new String [] { "SSLv3" });
81
ssle2.setEnabledProtocols(new String [] { "TLSv1" });
82
}
83
84
private void runTest() throws Exception {
85
boolean dataDone = false;
86
87
createSSLEngines();
88
createBuffers();
89
90
SSLEngineResult result1; // ssle1's results from last operation
91
SSLEngineResult result2; // ssle2's results from last operation
92
93
result1 = ssle1.wrap(appOut1, oneToTwo);
94
oneToTwo.flip();
95
96
result2 = ssle2.unwrap(oneToTwo, appIn2);
97
98
runDelegatedTasks(result2, ssle2);
99
100
try {
101
/*
102
* We should be getting a SSLHandshakeException.
103
* If this changes, we'll need to update this test.
104
* Anything else and we fail.
105
*/
106
result2 = ssle2.unwrap(oneToTwo, appIn2);
107
throw new Exception(
108
"TEST FAILED: Didn't generate any exception");
109
} catch (SSLHandshakeException e) {
110
System.out.println("TEST PASSED: Caught right exception");
111
} catch (SSLException e) {
112
System.out.println("TEST FAILED: Generated wrong exception");
113
throw e;
114
}
115
}
116
117
public static void main(String args[]) throws Exception {
118
// reset the security property to make sure that the algorithms
119
// and keys used in this test are not disabled.
120
Security.setProperty("jdk.tls.disabledAlgorithms", "");
121
122
DelegatedTaskWrongException test;
123
124
test = new DelegatedTaskWrongException();
125
126
test.createSSLEngines();
127
128
test.runTest();
129
130
System.out.println("Test Passed.");
131
}
132
133
/*
134
* **********************************************************
135
* Majority of the test case is above, below is just setup stuff
136
* **********************************************************
137
*/
138
139
public DelegatedTaskWrongException() throws Exception {
140
sslc = getSSLContext(keyFilename, trustFilename);
141
}
142
143
/*
144
* Create an initialized SSLContext to use for this test.
145
*/
146
private SSLContext getSSLContext(String keyFile, String trustFile)
147
throws Exception {
148
149
KeyStore ks = KeyStore.getInstance("JKS");
150
KeyStore ts = KeyStore.getInstance("JKS");
151
152
char[] passphrase = "passphrase".toCharArray();
153
154
ks.load(new FileInputStream(keyFile), passphrase);
155
ts.load(new FileInputStream(trustFile), passphrase);
156
157
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
158
kmf.init(ks, passphrase);
159
160
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
161
tmf.init(ts);
162
163
SSLContext sslCtx = SSLContext.getInstance("TLS");
164
165
sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
166
167
return sslCtx;
168
}
169
170
private void createBuffers() {
171
// Size the buffers as appropriate.
172
173
SSLSession session = ssle1.getSession();
174
int appBufferMax = session.getApplicationBufferSize();
175
int netBufferMax = session.getPacketBufferSize();
176
177
appIn1 = ByteBuffer.allocateDirect(appBufferMax + 50);
178
appIn2 = ByteBuffer.allocateDirect(appBufferMax + 50);
179
180
oneToTwo = ByteBuffer.allocateDirect(netBufferMax);
181
twoToOne = ByteBuffer.allocateDirect(netBufferMax);
182
183
appOut1 = ByteBuffer.wrap("Hi Engine2, I'm SSLEngine1".getBytes());
184
appOut2 = ByteBuffer.wrap("Hello Engine1, I'm SSLEngine2".getBytes());
185
186
log("AppOut1 = " + appOut1);
187
log("AppOut2 = " + appOut2);
188
log("");
189
}
190
191
private static void runDelegatedTasks(SSLEngineResult result,
192
SSLEngine engine) throws Exception {
193
194
if (result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
195
Runnable runnable;
196
while ((runnable = engine.getDelegatedTask()) != null) {
197
log("running delegated task...");
198
runnable.run();
199
}
200
}
201
}
202
203
private static boolean isEngineClosed(SSLEngine engine) {
204
return (engine.isOutboundDone() && engine.isInboundDone());
205
}
206
207
private static void checkTransfer(ByteBuffer a, ByteBuffer b)
208
throws Exception {
209
a.flip();
210
b.flip();
211
212
if (!a.equals(b)) {
213
throw new Exception("Data didn't transfer cleanly");
214
} else {
215
log("Data transferred cleanly");
216
}
217
218
a.position(a.limit());
219
b.position(b.limit());
220
a.limit(a.capacity());
221
b.limit(b.capacity());
222
}
223
224
private static void log(String str) {
225
if (debug) {
226
System.out.println(str);
227
}
228
}
229
}
230
231