Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/LargePacketAfterHandshakeTest.java
41152 views
/*1* Copyright (c) 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// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728import java.io.BufferedReader;29import java.io.BufferedWriter;30import java.io.IOException;31import java.io.InputStreamReader;32import java.io.OutputStreamWriter;3334import javax.net.ssl.SSLServerSocket;35import javax.net.ssl.SSLServerSocketFactory;36import javax.net.ssl.SSLSocket;37import javax.net.ssl.SSLSocketFactory;3839/*40* @test41* @bug 814916942* @summary Test for BufferOverflowException during read from SSLSocket when43* large packet is coming from server after server initiated handshake44* @run main/othervm LargePacketAfterHandshakeTest45*/46public class LargePacketAfterHandshakeTest {47static String pathToStores = "../../../../javax/net/ssl/etc";48static String keyStoreFile = "keystore";49static String trustStoreFile = "truststore";50static String passwd = "passphrase";5152volatile static int serverport = -1;53volatile static boolean serverReady = false;54volatile static boolean clientDone = false;55volatile static Exception serverException = null;5657public static void runServer() {58try {59System.out.println("Server: Started server thread.");60SSLServerSocketFactory ssf =61(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();62SSLServerSocket s = (SSLServerSocket)ssf.createServerSocket(0);63serverport = s.getLocalPort();64System.out.println("Server: Started, listening on port " +65serverport + ".");66serverReady = true;67SSLSocket c = (SSLSocket)s.accept();68s.close();69System.out.println(70"Server: Accepted client connection and closed server socket.");71BufferedReader r = new BufferedReader(72new InputStreamReader(c.getInputStream()));73BufferedWriter w = new BufferedWriter(74new OutputStreamWriter(c.getOutputStream()));75String echostring = r.readLine();76System.out.println("Server: Read " + echostring.length() +77" chars of input data.");78c.startHandshake();79System.out.println("Server: Kicked new handshake.");80w.write(echostring);81w.newLine();82w.flush();83System.out.println("Server: Echoed " + echostring.length() +84" chars of input data.");85while (!clientDone) {86try {87Thread.sleep(10);88} catch (InterruptedException e) {89System.out.println("Server: Caught InterruptedException.");90}91}92r.close();93w.close();94c.close();95System.out.println(96"Server: Closed streams and client socket, exiting.");97} catch (Exception e) {98System.out.println("Server: Caught Exception.");99e.printStackTrace();100serverReady = true;101serverException = e;102}103}104105public static void runClient() throws IOException {106try {107SSLSocketFactory f =108(SSLSocketFactory)SSLSocketFactory.getDefault();109System.out.println("Client: Initialized.");110while (!serverReady) {111try {112Thread.sleep(10);113} catch (InterruptedException e) {114System.out.println("Client: Caught InterruptedException.");115}116}117SSLSocket c = (SSLSocket)f.createSocket("localhost", serverport);118BufferedWriter w = new BufferedWriter(119new OutputStreamWriter(c.getOutputStream()));120BufferedReader r = new BufferedReader(121new InputStreamReader(c.getInputStream()));122System.out.println("Client: Connected.");123String echoPattern = "Otto";124StringBuilder echoBuilder =125new StringBuilder(4500 + echoPattern.length());126while (echoBuilder.length() < 4500) {127echoBuilder.append(echoPattern);128}129String echostring = echoBuilder.toString();130w.write(echostring);131w.newLine();132w.flush();133System.out.println("Client: Sent " + echostring.length() +134" chars of data.");135String echoresponse = r.readLine();136clientDone = true;137System.out.println("Client: Read " + echoresponse.length() +138" chars of data.");139w.close();140r.close();141c.close();142System.out.println("Client: Closed streams and socket, exiting.");143} catch (IOException e) {144System.out.println("Client: Caught Exception.");145e.printStackTrace();146clientDone = true;147throw e;148}149}150151public static void main(String[] args) throws Exception {152String keyFilename = System.getProperty("test.src", "./") + "/" +153pathToStores + "/" + keyStoreFile;154String trustFilename = System.getProperty("test.src", "./") + "/" +155pathToStores + "/" + trustStoreFile;156157System.setProperty("javax.net.ssl.keyStore", keyFilename);158System.setProperty("javax.net.ssl.keyStorePassword", passwd);159System.setProperty("javax.net.ssl.trustStore", trustFilename);160System.setProperty("javax.net.ssl.trustStorePassword", passwd);161162Thread serverThread = new Thread() {163@Override164public void run() {165runServer();166}167};168serverThread.start();169runClient();170while (serverThread.isAlive()) {171try {172serverThread.join();173} catch (InterruptedException e) {174System.out.println("Main: Caught InterruptedException " +175" waiting for server Thread.");176}177}178if (serverException != null) {179throw serverException;180}181}182}183184185