Path: blob/master/test/jdk/sun/security/ssl/AppInputStream/RemoveMarkReset.java
41152 views
/*1* Copyright (c) 2001, 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 441366426* @summary remove mark/reset functionality from AppInputStream27* @run main/othervm RemoveMarkReset28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31* @author Brad Wetmore32*/3334import java.io.*;35import java.net.*;36import javax.net.ssl.*;3738public class RemoveMarkReset {3940/*41* =============================================================42* Set the various variables needed for the tests, then43* specify what tests to run on each side.44*/4546/*47* Should we run the client or server in a separate thread?48* Both sides can throw exceptions, but do you have a preference49* as to which side should be the main thread.50*/51static boolean separateServerThread = false;5253/*54* Where do we find the keystores?55*/56static String pathToStores = "../../../../javax/net/ssl/etc";57static String keyStoreFile = "keystore";58static String trustStoreFile = "truststore";59static String passwd = "passphrase";6061/*62* Is the server ready to serve?63*/64volatile static boolean serverReady = false;6566/*67* Turn on SSL debugging?68*/69static boolean debug = false;707172/*73* Define the server side of the test.74*/75void doServerSide() throws Exception {76SSLServerSocketFactory sslssf =77(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();78SSLServerSocket sslServerSocket =79(SSLServerSocket) sslssf.createServerSocket(serverPort);8081serverPort = sslServerSocket.getLocalPort();8283/*84* Signal Client, we're ready for his connect.85*/86serverReady = true;8788SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();89InputStream sslIS = sslSocket.getInputStream();90OutputStream sslOS = sslSocket.getOutputStream();9192sslIS.read();93sslOS.write(85);94sslOS.flush();9596if (sslIS.markSupported())97throw new Exception("sslIS.markSupported() reported true");98sslIS.mark(10);99try {100sslIS.reset();101throw new Exception("sslIS.reset() didn't throw exception");102} catch (IOException e) {103/*104* swallow. This worked correctly.105*/106}107108sslIS.close();109sslOS.close();110sslSocket.close();111}112113/*114* Define the client side of the test.115*/116void doClientSide() throws Exception {117118/*119* Wait for server to get started.120*/121while (!serverReady) {122Thread.sleep(50);123}124125SSLSocketFactory sslsf =126(SSLSocketFactory) SSLSocketFactory.getDefault();127SSLSocket sslSocket = (SSLSocket)128sslsf.createSocket("localhost", serverPort);129130InputStream sslIS = sslSocket.getInputStream();131OutputStream sslOS = sslSocket.getOutputStream();132133sslOS.write(280);134sslOS.flush();135sslIS.read();136137sslIS.close();138sslOS.close();139sslSocket.close();140}141142/*143* =============================================================144* The remainder is just support stuff145*/146147// use any free port by default148volatile int serverPort = 0;149150volatile Exception serverException = null;151volatile Exception clientException = null;152153public static void main(String[] args) throws Exception {154String keyFilename =155System.getProperty("test.src", "./") + "/" + pathToStores +156"/" + keyStoreFile;157String trustFilename =158System.getProperty("test.src", "./") + "/" + pathToStores +159"/" + trustStoreFile;160161System.setProperty("javax.net.ssl.keyStore", keyFilename);162System.setProperty("javax.net.ssl.keyStorePassword", passwd);163System.setProperty("javax.net.ssl.trustStore", trustFilename);164System.setProperty("javax.net.ssl.trustStorePassword", passwd);165166if (debug)167System.setProperty("javax.net.debug", "all");168169/*170* Start the tests.171*/172new RemoveMarkReset();173}174175Thread clientThread = null;176Thread serverThread = null;177178/*179* Primary constructor, used to drive remainder of the test.180*181* Fork off the other side, then do your work.182*/183RemoveMarkReset () throws Exception {184if (separateServerThread) {185startServer(true);186startClient(false);187} else {188startClient(true);189startServer(false);190}191192/*193* Wait for other side to close down.194*/195if (separateServerThread) {196serverThread.join();197} else {198clientThread.join();199}200201/*202* When we get here, the test is pretty much over.203*204* If the main thread excepted, that propagates back205* immediately. If the other thread threw an exception, we206* should report back.207*/208if (serverException != null)209throw serverException;210if (clientException != null)211throw clientException;212}213214void startServer(boolean newThread) throws Exception {215if (newThread) {216serverThread = new Thread() {217public void run() {218try {219doServerSide();220} catch (Exception e) {221/*222* Our server thread just died.223*224* Release the client, if not already active...225*/226System.out.println("Server died...");227serverReady = true;228serverException = e;229}230}231};232serverThread.start();233} else {234doServerSide();235}236}237238void startClient(boolean newThread) throws Exception {239if (newThread) {240clientThread = new Thread() {241public void run() {242try {243doClientSide();244} catch (Exception e) {245/*246* Our client thread just died.247*/248System.out.println("Client died...");249clientException = e;250}251}252};253clientThread.start();254} else {255doClientSide();256}257}258}259260261