|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, 2020, Amazon 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 | + * @test |
| 26 | + * @bug 8214339 |
| 27 | + * @summary When a SocketException is thrown by the underlying layer, It |
| 28 | + * should be thrown as is and not be transformed to an SSLException. |
| 29 | + * @library /javax/net/ssl/templates |
| 30 | + * @run main/othervm SSLSocketShouldThrowSocketException |
| 31 | + */ |
| 32 | + |
| 33 | +import java.io.*; |
| 34 | +import java.net.*; |
| 35 | +import java.util.*; |
| 36 | +import java.security.*; |
| 37 | +import javax.net.ssl.*; |
| 38 | + |
| 39 | +import java.util.concurrent.CountDownLatch; |
| 40 | +import java.util.concurrent.TimeUnit; |
| 41 | + |
| 42 | +public class SSLSocketShouldThrowSocketException extends SSLSocketTemplate { |
| 43 | + |
| 44 | + boolean handshake; |
| 45 | + |
| 46 | + private final CountDownLatch clientTerminatedCondition = new CountDownLatch(1); |
| 47 | + |
| 48 | + SSLSocketShouldThrowSocketException(boolean handshake) { |
| 49 | + this.handshake = handshake; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected boolean isCustomizedClientConnection() { |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected void runServerApplication(SSLSocket socket) throws Exception { |
| 59 | + clientTerminatedCondition.await(30L, TimeUnit.SECONDS); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void runClientApplication(int serverPort) throws Exception { |
| 64 | + Socket baseSocket = new Socket("localhost", this.serverPort); |
| 65 | + |
| 66 | + SSLSocketFactory sslsf = |
| 67 | + (SSLSocketFactory) SSLSocketFactory.getDefault(); |
| 68 | + SSLSocket sslSocket = (SSLSocket) |
| 69 | + sslsf.createSocket(baseSocket, "localhost", serverPort, false); |
| 70 | + |
| 71 | + if (this.handshake) { |
| 72 | + testHandshakeClose(baseSocket, sslSocket); |
| 73 | + } else { |
| 74 | + testDataClose(baseSocket, sslSocket); |
| 75 | + } |
| 76 | + |
| 77 | + clientTerminatedCondition.countDown(); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + private void testHandshakeClose(Socket baseSocket, SSLSocket sslSocket) throws Exception { |
| 82 | + Thread aborter = new Thread() { |
| 83 | + @Override |
| 84 | + public void run() { |
| 85 | + |
| 86 | + try { |
| 87 | + Thread.sleep(10); |
| 88 | + System.err.println("Closing the client socket : " + System.nanoTime()); |
| 89 | + baseSocket.close(); |
| 90 | + } catch (Exception ieo) { |
| 91 | + ieo.printStackTrace(); |
| 92 | + } |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + aborter.start(); |
| 97 | + |
| 98 | + try { |
| 99 | + // handshaking |
| 100 | + System.err.println("Client starting handshake: " + System.nanoTime()); |
| 101 | + sslSocket.startHandshake(); |
| 102 | + throw new Exception("Start handshake did not throw an exception"); |
| 103 | + } catch (SocketException se) { |
| 104 | + System.err.println("Caught Expected SocketException"); |
| 105 | + } |
| 106 | + |
| 107 | + aborter.join(); |
| 108 | + } |
| 109 | + |
| 110 | + private void testDataClose(Socket baseSocket, SSLSocket sslSocket) throws Exception{ |
| 111 | + |
| 112 | + CountDownLatch handshakeCondition = new CountDownLatch(1); |
| 113 | + |
| 114 | + Thread aborter = new Thread() { |
| 115 | + @Override |
| 116 | + public void run() { |
| 117 | + |
| 118 | + try { |
| 119 | + handshakeCondition.await(10L, TimeUnit.SECONDS); |
| 120 | + System.err.println("Closing the client socket : " + System.nanoTime()); |
| 121 | + baseSocket.close(); |
| 122 | + } catch (Exception ieo) { |
| 123 | + ieo.printStackTrace(); |
| 124 | + } |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + aborter.start(); |
| 129 | + |
| 130 | + try { |
| 131 | + // handshaking |
| 132 | + System.err.println("Client starting handshake: " + System.nanoTime()); |
| 133 | + sslSocket.startHandshake(); |
| 134 | + handshakeCondition.countDown(); |
| 135 | + System.err.println("Reading data from server"); |
| 136 | + BufferedReader is = new BufferedReader( |
| 137 | + new InputStreamReader(sslSocket.getInputStream())); |
| 138 | + String data = is.readLine(); |
| 139 | + throw new Exception("Start handshake did not throw an exception"); |
| 140 | + } catch (SocketException se) { |
| 141 | + System.err.println("Caught Expected SocketException"); |
| 142 | + } |
| 143 | + |
| 144 | + aborter.join(); |
| 145 | + } |
| 146 | + |
| 147 | + public static void main(String[] args) throws Exception { |
| 148 | + // SocketException should be throws during a handshake phase. |
| 149 | + (new SSLSocketShouldThrowSocketException(true)).run(); |
| 150 | + // SocketException should be throw during the application data phase. |
| 151 | + (new SSLSocketShouldThrowSocketException(false)).run(); |
| 152 | + } |
| 153 | +} |
0 commit comments