Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLSocketImpl/CloseSocket.java
38853 views
/*1* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 467491326* @summary Verify that EOFException are correctly handled during the handshake27* @author Andreas Sterbenz28* @run main/othervm CloseSocket29*/3031import javax.net.SocketFactory;32import javax.net.ssl.SSLSocket;33import javax.net.ssl.SSLSocketFactory;34import java.io.IOException;35import java.io.InputStream;36import java.io.OutputStream;37import java.net.ServerSocket;38import java.net.Socket;39import java.util.ArrayList;4041public class CloseSocket {4243private static ArrayList<TestCase> testCases = new ArrayList<>();4445static {46testCases.add(socket -> socket.startHandshake());47testCases.add(socket -> {48InputStream in = socket.getInputStream();49in.read();50});51testCases.add(socket -> {52OutputStream out = socket.getOutputStream();53out.write(43);54});55}5657public static void main(String[] args) throws Exception {58try (Server server = new Server()) {59new Thread(server).start();6061SocketFactory factory = SSLSocketFactory.getDefault();62try (SSLSocket socket = (SSLSocket) factory.createSocket("localhost",63server.getPort())) {64socket.setSoTimeout(2000);65System.out.println("Client established TCP connection");66boolean failed = false;67for (TestCase testCase : testCases) {68try {69testCase.test(socket);70System.out.println("ERROR: no exception");71failed = true;72} catch (IOException e) {73System.out.println("Failed as expected: " + e);74}75}76if (failed) {77throw new Exception("One or more tests failed");78}79}80}81}8283static class Server implements AutoCloseable, Runnable {8485final ServerSocket serverSocket;8687Server() throws IOException {88serverSocket = new ServerSocket(0);89}9091public int getPort() {92return serverSocket.getLocalPort();93}9495@Override96public void run() {97try (Socket s = serverSocket.accept()) {98System.out.println("Server accepted connection");99// wait a bit before closing the socket to give100// the client time to send its hello message101Thread.currentThread().sleep(100);102s.close();103System.out.println("Server closed socket, done.");104} catch (Exception e) {105throw new RuntimeException("Problem in test execution", e);106}107}108109@Override110public void close() throws Exception {111if (!serverSocket.isClosed()) {112serverSocket.close();113}114}115}116117interface TestCase {118void test(SSLSocket socket) throws IOException;119}120}121122123