Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/CloseSocket.java
41152 views
/*1* Copyright (c) 2002, 2021, 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* @library /javax/net/ssl/templates28* @author Andreas Sterbenz29* @run main/othervm CloseSocket30*/3132import javax.net.ssl.SSLSocket;33import java.io.IOException;34import java.io.InputStream;35import java.io.OutputStream;36import java.util.ArrayList;37import java.util.Arrays;38import java.util.List;39import java.util.concurrent.TimeUnit;404142public class CloseSocket extends SSLSocketTemplate {4344/*45* SSLSocketImpl::startHandshake internally checks that the socket is not closed or46* broken and still connected, so this test needs the server to close the socket47* after those verifications are performed to reproduce the scenario. Using a48* CountDownLatch in the test before calling startHandshake does not guarantee that.49* Using a CountDownLatch after startHandshake does not work either since the client50* keeps waiting for a server response, which is blocked waiting for the latch.51*52* Therefore, we can only guarantee the socket is not yet closed when the handshake53* is requested by looking at the client thread stack54*/55private volatile Thread clientThread = null;5657@Override58protected void runClientApplication(SSLSocket socket) throws Exception {59clientThread = Thread.currentThread();60boolean failed = false;61for (TestCase testCase : getTestCases()) {62try {63testCase.test(socket);64System.out.println("ERROR: no exception");65failed = true;66} catch (IOException e) {67System.out.println("Failed as expected: " + e);68}69}70if (failed) {71throw new Exception("One or more tests failed");72}73}7475@Override76protected void runServerApplication(SSLSocket socket) throws Exception {77System.out.println("Server accepted connection");78while (!isHandshakeStarted()) {79// wait for a short time before checking again if handshake started80TimeUnit.MILLISECONDS.sleep(100);81}8283socket.close();84System.out.println("Server closed socket, done.");85}8687private List<TestCase> getTestCases() {88List<TestCase> testCases = new ArrayList<>();8990testCases.add(SSLSocket::startHandshake);91testCases.add(socket -> {92InputStream in = socket.getInputStream();93in.read();94});95testCases.add(socket -> {96OutputStream out = socket.getOutputStream();97out.write(43);98});99100return testCases;101}102103private boolean isHandshakeStarted() {104if (clientThread == null) {105return false;106} else {107StackTraceElement[] traces = clientThread.getStackTrace();108return Arrays.stream(traces).anyMatch(stackElement ->109stackElement.getMethodName().equals("readHandshakeRecord"));110}111}112113public static void main(String[] args) throws Exception {114new CloseSocket().run();115}116117interface TestCase {118void test(SSLSocket socket) throws IOException;119}120}121122123