Path: blob/master/test/jdk/sun/security/ssl/AppInputStream/ReadZeroBytes.java
41152 views
/*1* Copyright (c) 2009, 2011, 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 669727026* @summary Inputstream dosent behave correct27* @run main/othervm ReadZeroBytes28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31*/3233import java.io.*;34import java.net.*;35import javax.net.ssl.*;3637public class ReadZeroBytes {3839/*40* =============================================================41* Set the various variables needed for the tests, then42* specify what tests to run on each side.43*/4445/*46* Should we run the client or server in a separate thread?47* Both sides can throw exceptions, but do you have a preference48* as to which side should be the main thread.49*/50static boolean separateServerThread = false;5152/*53* Where do we find the keystores?54*/55static String pathToStores = "../../../../javax/net/ssl/etc";56static String keyStoreFile = "keystore";57static String trustStoreFile = "truststore";58static String passwd = "passphrase";5960/*61* Is the server ready to serve?62*/63volatile static boolean serverReady = false;6465/*66* Turn on SSL debugging?67*/68static boolean debug = false;6970/*71* Define the server side of the test.72*/73void doServerSide() throws Exception {74SSLServerSocketFactory sslssf =75(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();76SSLServerSocket sslServerSocket =77(SSLServerSocket) sslssf.createServerSocket(serverPort);7879serverPort = sslServerSocket.getLocalPort();8081/*82* Signal Client, we're ready for his connect.83*/84serverReady = true;8586SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();87InputStream sslIS = sslSocket.getInputStream();88OutputStream sslOS = sslSocket.getOutputStream();8990// no read, no write.91SSLSession sess = sslSocket.getSession();92if (!sess.isValid()) {93throw new Exception("Error occurs during the initial handshake");94}9596sslIS.close();97sslOS.close();98sslSocket.close();99}100101/*102* Define the client side of the test.103*/104void doClientSide() throws Exception {105106/*107* Wait for server to get started.108*/109while (!serverReady) {110Thread.sleep(50);111}112113SSLSocketFactory sslsf =114(SSLSocketFactory) SSLSocketFactory.getDefault();115SSLSocket sslSocket = (SSLSocket)116sslsf.createSocket("localhost", serverPort);117118InputStream sslIS = sslSocket.getInputStream();119OutputStream sslOS = sslSocket.getOutputStream();120121// read zero byte, write zero byte.122sslIS.read(new byte[0], 0, 0);123sslOS.write(new byte[0], 0, 0);124125// if byte array length matters.126sslIS.read(new byte[1], 0, 0);127sslOS.write(new byte[1], 0, 0);128129// note that the above read/write should not kickoff handshaking.130SSLSession sess = sslSocket.getSession();131if (!sess.isValid()) {132throw new Exception("Error occurs during the initial handshake");133}134135sslIS.close();136sslOS.close();137sslSocket.close();138}139140/*141* =============================================================142* The remainder is just support stuff143*/144145// use any free port by default146volatile int serverPort = 0;147148volatile Exception serverException = null;149volatile Exception clientException = null;150151public static void main(String[] args) throws Exception {152String keyFilename =153System.getProperty("test.src", "./") + "/" + pathToStores +154"/" + keyStoreFile;155String trustFilename =156System.getProperty("test.src", "./") + "/" + pathToStores +157"/" + trustStoreFile;158159System.setProperty("javax.net.ssl.keyStore", keyFilename);160System.setProperty("javax.net.ssl.keyStorePassword", passwd);161System.setProperty("javax.net.ssl.trustStore", trustFilename);162System.setProperty("javax.net.ssl.trustStorePassword", passwd);163164if (debug)165System.setProperty("javax.net.debug", "all");166167/*168* Start the tests.169*/170new ReadZeroBytes();171}172173Thread clientThread = null;174Thread serverThread = null;175176/*177* Primary constructor, used to drive remainder of the test.178*179* Fork off the other side, then do your work.180*/181ReadZeroBytes () throws Exception {182if (separateServerThread) {183startServer(true);184startClient(false);185} else {186startClient(true);187startServer(false);188}189190/*191* Wait for other side to close down.192*/193if (separateServerThread) {194serverThread.join();195} else {196clientThread.join();197}198199/*200* When we get here, the test is pretty much over.201*202* If the main thread excepted, that propagates back203* immediately. If the other thread threw an exception, we204* should report back.205*/206if (serverException != null)207throw serverException;208if (clientException != null)209throw clientException;210}211212void startServer(boolean newThread) throws Exception {213if (newThread) {214serverThread = new Thread() {215public void run() {216try {217doServerSide();218} catch (Exception e) {219/*220* Our server thread just died.221*222* Release the client, if not already active...223*/224System.out.println("Server died...");225serverReady = true;226serverException = e;227}228}229};230serverThread.start();231} else {232doServerSide();233}234}235236void startClient(boolean newThread) throws Exception {237if (newThread) {238clientThread = new Thread() {239public void run() {240try {241doClientSide();242} catch (Exception e) {243/*244* Our client thread just died.245*/246System.out.println("Client died...");247clientException = e;248}249}250};251clientThread.start();252} else {253doClientSide();254}255}256}257258259260