Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/CloseSocket.java
41152 views
1
/*
2
* Copyright (c) 2002, 2021, 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 4674913
27
* @summary Verify that EOFException are correctly handled during the handshake
28
* @library /javax/net/ssl/templates
29
* @author Andreas Sterbenz
30
* @run main/othervm CloseSocket
31
*/
32
33
import javax.net.ssl.SSLSocket;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.OutputStream;
37
import java.util.ArrayList;
38
import java.util.Arrays;
39
import java.util.List;
40
import java.util.concurrent.TimeUnit;
41
42
43
public class CloseSocket extends SSLSocketTemplate {
44
45
/*
46
* SSLSocketImpl::startHandshake internally checks that the socket is not closed or
47
* broken and still connected, so this test needs the server to close the socket
48
* after those verifications are performed to reproduce the scenario. Using a
49
* CountDownLatch in the test before calling startHandshake does not guarantee that.
50
* Using a CountDownLatch after startHandshake does not work either since the client
51
* keeps waiting for a server response, which is blocked waiting for the latch.
52
*
53
* Therefore, we can only guarantee the socket is not yet closed when the handshake
54
* is requested by looking at the client thread stack
55
*/
56
private volatile Thread clientThread = null;
57
58
@Override
59
protected void runClientApplication(SSLSocket socket) throws Exception {
60
clientThread = Thread.currentThread();
61
boolean failed = false;
62
for (TestCase testCase : getTestCases()) {
63
try {
64
testCase.test(socket);
65
System.out.println("ERROR: no exception");
66
failed = true;
67
} catch (IOException e) {
68
System.out.println("Failed as expected: " + e);
69
}
70
}
71
if (failed) {
72
throw new Exception("One or more tests failed");
73
}
74
}
75
76
@Override
77
protected void runServerApplication(SSLSocket socket) throws Exception {
78
System.out.println("Server accepted connection");
79
while (!isHandshakeStarted()) {
80
// wait for a short time before checking again if handshake started
81
TimeUnit.MILLISECONDS.sleep(100);
82
}
83
84
socket.close();
85
System.out.println("Server closed socket, done.");
86
}
87
88
private List<TestCase> getTestCases() {
89
List<TestCase> testCases = new ArrayList<>();
90
91
testCases.add(SSLSocket::startHandshake);
92
testCases.add(socket -> {
93
InputStream in = socket.getInputStream();
94
in.read();
95
});
96
testCases.add(socket -> {
97
OutputStream out = socket.getOutputStream();
98
out.write(43);
99
});
100
101
return testCases;
102
}
103
104
private boolean isHandshakeStarted() {
105
if (clientThread == null) {
106
return false;
107
} else {
108
StackTraceElement[] traces = clientThread.getStackTrace();
109
return Arrays.stream(traces).anyMatch(stackElement ->
110
stackElement.getMethodName().equals("readHandshakeRecord"));
111
}
112
}
113
114
public static void main(String[] args) throws Exception {
115
new CloseSocket().run();
116
}
117
118
interface TestCase {
119
void test(SSLSocket socket) throws IOException;
120
}
121
}
122
123