8164533: sun/security/ssl/SSLSocketImpl/CloseSocket.java failed with "Error while cleaning up threads after test"
Reviewed-by: xuelei
This commit is contained in:
parent
10cbe0678a
commit
af8dc755fd
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
@ -26,22 +26,76 @@
|
|||||||
* @bug 4674913
|
* @bug 4674913
|
||||||
* @summary Verify that EOFException are correctly handled during the handshake
|
* @summary Verify that EOFException are correctly handled during the handshake
|
||||||
* @author Andreas Sterbenz
|
* @author Andreas Sterbenz
|
||||||
|
* @run main/othervm CloseSocket
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.io.*;
|
import javax.net.SocketFactory;
|
||||||
import java.net.*;
|
import javax.net.ssl.SSLSocket;
|
||||||
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
import javax.net.ssl.*;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class CloseSocket {
|
public class CloseSocket {
|
||||||
|
|
||||||
|
private static ArrayList<TestCase> testCases = new ArrayList<>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
testCases.add(socket -> socket.startHandshake());
|
||||||
|
testCases.add(socket -> {
|
||||||
|
InputStream in = socket.getInputStream();
|
||||||
|
in.read();
|
||||||
|
});
|
||||||
|
testCases.add(socket -> {
|
||||||
|
OutputStream out = socket.getOutputStream();
|
||||||
|
out.write(43);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
final ServerSocket serverSocket = new ServerSocket(0);
|
try (Server server = new Server()) {
|
||||||
int serverPort = serverSocket.getLocalPort();
|
new Thread(server).start();
|
||||||
new Thread() {
|
|
||||||
public void run() {
|
SocketFactory factory = SSLSocketFactory.getDefault();
|
||||||
|
try (SSLSocket socket = (SSLSocket) factory.createSocket("localhost",
|
||||||
|
server.getPort())) {
|
||||||
|
socket.setSoTimeout(2000);
|
||||||
|
System.out.println("Client established TCP connection");
|
||||||
|
boolean failed = false;
|
||||||
|
for (TestCase testCase : testCases) {
|
||||||
try {
|
try {
|
||||||
Socket s = serverSocket.accept();
|
testCase.test(socket);
|
||||||
|
System.out.println("ERROR: no exception");
|
||||||
|
failed = true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Failed as expected: " + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (failed) {
|
||||||
|
throw new Exception("One or more tests failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Server implements AutoCloseable, Runnable {
|
||||||
|
|
||||||
|
final ServerSocket serverSocket;
|
||||||
|
|
||||||
|
Server() throws IOException {
|
||||||
|
serverSocket = new ServerSocket(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPort() {
|
||||||
|
return serverSocket.getLocalPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try (Socket s = serverSocket.accept()) {
|
||||||
System.out.println("Server accepted connection");
|
System.out.println("Server accepted connection");
|
||||||
// wait a bit before closing the socket to give
|
// wait a bit before closing the socket to give
|
||||||
// the client time to send its hello message
|
// the client time to send its hello message
|
||||||
@ -49,44 +103,19 @@ public class CloseSocket {
|
|||||||
s.close();
|
s.close();
|
||||||
System.out.println("Server closed socket, done.");
|
System.out.println("Server closed socket, done.");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Server exception:");
|
throw new RuntimeException("Problem in test execution", e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.start();
|
|
||||||
SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
|
|
||||||
SSLSocket socket = (SSLSocket)factory.createSocket("localhost", serverPort);
|
|
||||||
System.out.println("Client established TCP connection");
|
|
||||||
boolean failed = false;
|
|
||||||
try {
|
|
||||||
System.out.println("Starting handshake...");
|
|
||||||
socket.startHandshake();
|
|
||||||
System.out.println("ERROR: no exception");
|
|
||||||
failed = true;
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("Failed as expected: " + e);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
System.out.println("Trying read...");
|
|
||||||
InputStream in = socket.getInputStream();
|
|
||||||
int b = in.read();
|
|
||||||
System.out.println("ERROR: no exception, read: " + b);
|
|
||||||
failed = true;
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("Failed as expected: " + e);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
System.out.println("Trying read...");
|
|
||||||
OutputStream out = socket.getOutputStream();
|
|
||||||
out.write(43);
|
|
||||||
System.out.println("ERROR: no exception");
|
|
||||||
failed = true;
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("Failed as expected: " + e);
|
|
||||||
}
|
|
||||||
if (failed) {
|
|
||||||
throw new Exception("One or more tests failed");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws Exception {
|
||||||
|
if (!serverSocket.isClosed()) {
|
||||||
|
serverSocket.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TestCase {
|
||||||
|
void test(SSLSocket socket) throws IOException;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user