Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLSocketImpl/CloseSocket.java
38853 views
1
/*
2
* Copyright (c) 2002, 2016, 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
* @author Andreas Sterbenz
29
* @run main/othervm CloseSocket
30
*/
31
32
import javax.net.SocketFactory;
33
import javax.net.ssl.SSLSocket;
34
import javax.net.ssl.SSLSocketFactory;
35
import java.io.IOException;
36
import java.io.InputStream;
37
import java.io.OutputStream;
38
import java.net.ServerSocket;
39
import java.net.Socket;
40
import java.util.ArrayList;
41
42
public class CloseSocket {
43
44
private static ArrayList<TestCase> testCases = new ArrayList<>();
45
46
static {
47
testCases.add(socket -> socket.startHandshake());
48
testCases.add(socket -> {
49
InputStream in = socket.getInputStream();
50
in.read();
51
});
52
testCases.add(socket -> {
53
OutputStream out = socket.getOutputStream();
54
out.write(43);
55
});
56
}
57
58
public static void main(String[] args) throws Exception {
59
try (Server server = new Server()) {
60
new Thread(server).start();
61
62
SocketFactory factory = SSLSocketFactory.getDefault();
63
try (SSLSocket socket = (SSLSocket) factory.createSocket("localhost",
64
server.getPort())) {
65
socket.setSoTimeout(2000);
66
System.out.println("Client established TCP connection");
67
boolean failed = false;
68
for (TestCase testCase : testCases) {
69
try {
70
testCase.test(socket);
71
System.out.println("ERROR: no exception");
72
failed = true;
73
} catch (IOException e) {
74
System.out.println("Failed as expected: " + e);
75
}
76
}
77
if (failed) {
78
throw new Exception("One or more tests failed");
79
}
80
}
81
}
82
}
83
84
static class Server implements AutoCloseable, Runnable {
85
86
final ServerSocket serverSocket;
87
88
Server() throws IOException {
89
serverSocket = new ServerSocket(0);
90
}
91
92
public int getPort() {
93
return serverSocket.getLocalPort();
94
}
95
96
@Override
97
public void run() {
98
try (Socket s = serverSocket.accept()) {
99
System.out.println("Server accepted connection");
100
// wait a bit before closing the socket to give
101
// the client time to send its hello message
102
Thread.currentThread().sleep(100);
103
s.close();
104
System.out.println("Server closed socket, done.");
105
} catch (Exception e) {
106
throw new RuntimeException("Problem in test execution", e);
107
}
108
}
109
110
@Override
111
public void close() throws Exception {
112
if (!serverSocket.isClosed()) {
113
serverSocket.close();
114
}
115
}
116
}
117
118
interface TestCase {
119
void test(SSLSocket socket) throws IOException;
120
}
121
}
122
123