2222 */
2323
2424/*
25- * @test
25+ * @test id="separateServerThread"
2626 * @bug 6618387
2727 * @summary SSL client sessions do not close cleanly. A TCP reset occurs
2828 * instead of a close_notify alert.
3737 * if "Keep-Alive-Timer: called close()", the test failed.
3838 */
3939
40+ /*
41+ * @test id="separateClientThread"
42+ * @bug 6618387
43+ * @summary SSL client sessions do not close cleanly. A TCP reset occurs
44+ * instead of a close_notify alert.
45+ * @library /test/lib
46+ *
47+ * @run main/othervm -Dtest.separateThreads=false CloseKeepAliveCached false
48+ * @run main/othervm -Dtest.separateThreads=false CloseKeepAliveCached true
49+ *
50+ * @comment SunJSSE does not support dynamic system properties, no way to re-use
51+ * system properties in samevm/agentvm mode.
52+ * if "MainThread, called close()" found, the test passed. Otherwise,
53+ * if "Keep-Alive-Timer: called close()", the test failed.
54+ */
55+
4056import jdk .test .lib .Asserts ;
4157
4258import java .io .BufferedOutputStream ;
@@ -95,47 +111,48 @@ public class CloseKeepAliveCached {
95111 void doServerSide () throws Exception {
96112 SSLServerSocketFactory sslSsf =
97113 (SSLServerSocketFactory ) SSLServerSocketFactory .getDefault ();
98- sslServerSocket =
99- (SSLServerSocket ) sslSsf .createServerSocket (serverPort );
100- serverPort = sslServerSocket .getLocalPort ();
101114
102- /*
103- * Signal Client, we're ready for his connect.
104- */
105- serverReady = true ;
106- SSLSocket sslSocket = (SSLSocket ) sslServerSocket .accept ();
107-
108- // getting input and output streams
109- InputStream is = sslSocket .getInputStream ();
110- BufferedReader r = new BufferedReader (
111- new InputStreamReader (is ));
112- PrintStream out = new PrintStream (
113- new BufferedOutputStream (
114- sslSocket .getOutputStream ()));
115-
116- for (int i = 0 ; i < 3 && !sslSocket .isClosed (); i ++) {
117- // read request
118- String x ;
119- while ((x = r .readLine ()) != null ) {
120- if (x .isEmpty ()) {
121- break ;
115+ // autoclose sslServerSocket, but keeping it global to be used by the client
116+ try (var _ = this .sslServerSocket = (SSLServerSocket ) sslSsf .createServerSocket (serverPort )) {
117+ serverPort = sslServerSocket .getLocalPort ();
118+
119+ /*
120+ * Signal Client, we're ready for his connect.
121+ */
122+ serverReady = true ;
123+ try (SSLSocket sslSocket = (SSLSocket ) sslServerSocket .accept ()) {
124+
125+ // getting input and output streams
126+ InputStream is = sslSocket .getInputStream ();
127+ BufferedReader r = new BufferedReader (
128+ new InputStreamReader (is ));
129+ PrintStream out = new PrintStream (
130+ new BufferedOutputStream (
131+ sslSocket .getOutputStream ()));
132+
133+ for (int i = 0 ; i < 3 && !sslSocket .isClosed (); i ++) {
134+ // read request
135+ String x ;
136+ while ((x = r .readLine ()) != null ) {
137+ if (x .isEmpty ()) {
138+ break ;
139+ }
140+ }
141+
142+ /* send the response headers and body */
143+ out .print ("HTTP/1.1 200 OK\r \n " );
144+ out .print ("Keep-Alive: timeout=15, max=100\r \n " );
145+ out .print ("Connection: Keep-Alive\r \n " );
146+ out .print ("Content-Type: text/html; charset=iso-8859-1\r \n " );
147+ out .print ("Content-Length: 9\r \n " );
148+ out .print ("\r \n " );
149+ out .print ("Testing\r \n " );
150+ out .flush ();
151+
122152 }
123153 }
124-
125- /* send the response headers and body */
126- out .print ("HTTP/1.1 200 OK\r \n " );
127- out .print ("Keep-Alive: timeout=15, max=100\r \n " );
128- out .print ("Connection: Keep-Alive\r \n " );
129- out .print ("Content-Type: text/html; charset=iso-8859-1\r \n " );
130- out .print ("Content-Length: 9\r \n " );
131- out .print ("\r \n " );
132- out .print ("Testing\r \n " );
133- out .flush ();
134-
135- Thread .sleep (50 );
136154 }
137- sslSocket .close ();
138- sslServerSocket .close ();
155+
139156 }
140157
141158 /*
@@ -161,13 +178,13 @@ void doClientSide() throws Exception {
161178 URL url = new URI ("https://localhost:" + serverPort + "/" ).toURL ();
162179 HttpsURLConnection .setDefaultHostnameVerifier (new NameVerifier ());
163180 http = (HttpsURLConnection ) url .openConnection ();
164- InputStream is = http .getInputStream ();
165- while (is .read () != -1 ) ;
166- is . close ();
181+ try ( InputStream is = http .getInputStream ()) {
182+ while (is .read () != -1 ) ;
183+ }
167184
168185 url = new URI ("https://localhost:" + serverPort + "/" ).toURL ();
169186 http = (HttpsURLConnection ) url .openConnection ();
170- is = http .getInputStream ();
187+ InputStream is = http .getInputStream (); // not closed by design
171188 while (is .read () != -1 ) ;
172189
173190 // if inputstream.close() called, the http.disconnect() will
0 commit comments