53067
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, 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 |
// Please run in othervm mode. SunJSSE does not support dynamic system
|
|
26 |
// properties, no way to re-use system properties in samevm/agentvm mode.
|
|
27 |
//
|
|
28 |
|
|
29 |
/*
|
|
30 |
* @test
|
|
31 |
* @bug 8209333
|
|
32 |
* @summary Socket reset issue for TLS 1.3 socket close
|
|
33 |
* @library /javax/net/ssl/templates
|
|
34 |
* @run main/othervm SSLSocketClose
|
|
35 |
*/
|
|
36 |
|
|
37 |
import javax.net.ssl.*;
|
|
38 |
import java.io.*;
|
|
39 |
import java.net.InetAddress;
|
|
40 |
|
|
41 |
public class SSLSocketClose implements SSLContextTemplate {
|
|
42 |
|
|
43 |
public static void main(String[] args) throws Exception {
|
|
44 |
for (int i = 0; i<= 10; i++) {
|
|
45 |
System.err.println("===================================");
|
|
46 |
System.err.println("loop " + i);
|
|
47 |
System.err.println("===================================");
|
|
48 |
new SSLSocketClose().test();
|
|
49 |
}
|
|
50 |
}
|
|
51 |
|
|
52 |
private void test() throws Exception {
|
|
53 |
SSLServerSocket listenSocket = null;
|
|
54 |
SSLSocket serverSocket = null;
|
|
55 |
ClientSocket clientSocket = null;
|
|
56 |
try {
|
|
57 |
SSLServerSocketFactory serversocketfactory =
|
|
58 |
createServerSSLContext().getServerSocketFactory();
|
|
59 |
listenSocket =
|
|
60 |
(SSLServerSocket)serversocketfactory.createServerSocket(0);
|
|
61 |
listenSocket.setNeedClientAuth(false);
|
|
62 |
listenSocket.setEnableSessionCreation(true);
|
|
63 |
listenSocket.setUseClientMode(false);
|
|
64 |
|
|
65 |
|
|
66 |
System.err.println("Starting client");
|
|
67 |
clientSocket = new ClientSocket(listenSocket.getLocalPort());
|
|
68 |
clientSocket.start();
|
|
69 |
|
|
70 |
System.err.println("Accepting client requests");
|
|
71 |
serverSocket = (SSLSocket) listenSocket.accept();
|
|
72 |
|
|
73 |
System.err.println("Reading data from client");
|
|
74 |
BufferedReader serverReader = new BufferedReader(
|
|
75 |
new InputStreamReader(serverSocket.getInputStream()));
|
|
76 |
String data = serverReader.readLine();
|
|
77 |
System.err.println("Received data from client: " + data);
|
|
78 |
|
|
79 |
System.err.println("Sending data to client ...");
|
|
80 |
String serverData = "Hi, I am server";
|
|
81 |
BufferedWriter os = new BufferedWriter(
|
|
82 |
new OutputStreamWriter(serverSocket.getOutputStream()));
|
|
83 |
os.write(serverData, 0, serverData.length());
|
|
84 |
os.newLine();
|
|
85 |
os.flush();
|
|
86 |
|
|
87 |
System.err.println("Reading more data from client");
|
|
88 |
data = serverReader.readLine();
|
|
89 |
System.err.println("Received data from client: " + data);
|
|
90 |
} finally {
|
|
91 |
if (listenSocket != null) {
|
|
92 |
listenSocket.close();
|
|
93 |
}
|
|
94 |
|
|
95 |
if (serverSocket != null) {
|
|
96 |
serverSocket.close();
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
if (clientSocket != null && clientSocket.clientException != null) {
|
|
101 |
throw clientSocket.clientException;
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
private class ClientSocket extends Thread{
|
|
106 |
int serverPort = 0;
|
|
107 |
Exception clientException;
|
|
108 |
|
|
109 |
public ClientSocket(int serverPort) {
|
|
110 |
this.serverPort = serverPort;
|
|
111 |
}
|
|
112 |
|
|
113 |
@Override
|
|
114 |
public void run() {
|
|
115 |
SSLSocket clientSocket = null;
|
|
116 |
String clientData = "Hi, I am client";
|
|
117 |
try {
|
|
118 |
System.err.println(
|
|
119 |
"Connecting to server at port " + serverPort);
|
|
120 |
SSLSocketFactory sslSocketFactory =
|
|
121 |
createClientSSLContext().getSocketFactory();
|
|
122 |
clientSocket = (SSLSocket)sslSocketFactory.createSocket(
|
|
123 |
InetAddress.getLocalHost(), serverPort);
|
|
124 |
clientSocket.setSoLinger(true, 3);
|
|
125 |
|
|
126 |
System.err.println("Sending data to server ...");
|
|
127 |
|
|
128 |
BufferedWriter os = new BufferedWriter(
|
|
129 |
new OutputStreamWriter(clientSocket.getOutputStream()));
|
|
130 |
os.write(clientData, 0, clientData.length());
|
|
131 |
os.newLine();
|
|
132 |
os.flush();
|
|
133 |
|
|
134 |
System.err.println("Reading data from server");
|
|
135 |
BufferedReader is = new BufferedReader(
|
|
136 |
new InputStreamReader(clientSocket.getInputStream()));
|
|
137 |
String data = is.readLine();
|
|
138 |
System.err.println("Received Data from server: " + data);
|
|
139 |
|
|
140 |
System.err.println("Sending more data to server ...");
|
|
141 |
os.write(clientData, 0, clientData.length());
|
|
142 |
os.newLine();
|
|
143 |
os.flush();
|
|
144 |
} catch (Exception e) {
|
|
145 |
clientException = e;
|
|
146 |
} finally {
|
|
147 |
if (clientSocket != null) {
|
|
148 |
try{
|
|
149 |
clientSocket.close();
|
|
150 |
System.err.println("client socket closed");
|
|
151 |
} catch (IOException ioe) {
|
|
152 |
clientException = ioe;
|
|
153 |
}
|
|
154 |
}
|
|
155 |
}
|
|
156 |
}
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|