Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/NewSocketMethods.java
41152 views
/*1* Copyright (c) 2001, 2020, 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 442917626* @summary need to sync up SSL sockets with merlin java.net changes27* @run main/othervm NewSocketMethods28*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.*;36import javax.net.ssl.*;3738/**39* There are new methods for java.net.Socket class added to merlin40* This test case checks the behaviour of these new methods when overriden41* by methods of SSLSocket. The following methods are covered in this42* test case.43*44* public void sendUrgentData (int data) throws IOException45* public void setOOBInline(boolean on) throws SocketException46* public boolean getOOBInline() throws SocketException47* public SocketChannel getChannel() -- call on plain socket48* public void setTrafficClass(int tc) -- call on plain socket49* throws SocketException50* public int getTrafficClass() -- call on plain socket51* throws SocketException52* public void setReuseAddress(boolean on) -- call on plain socket53* throws SocketException54* public boolean getReuseAddress() -- call on plain socket55* throws SocketException56* public boolean isInputShutdown()57* public boolean isOutputShutdown()58*59* The methods below are covered by the test case located at:60* ../SocketCreation/SocketCreation.java61* public boolean isConnected()62* public boolean isBound()63*64*/6566public class NewSocketMethods {6768/*69* =============================================================70* Set the various variables needed for the tests, then71* specify what tests to run on each side.72*/7374/*75* Should we run the client or server in a separate thread?76* Both sides can throw exceptions, but do you have a preference77* as to which side should be the main thread.78*/79static boolean separateServerThread = true;8081/*82* If some one quickly wants to check the plain socket behaviour83* as a reference84*/85static boolean useSSL = true;8687/*88* Where do we find the keystores?89*/90static String pathToStores = "../../../../javax/net/ssl/etc";91static String keyStoreFile = "keystore";92static String trustStoreFile = "truststore";93static String passwd = "passphrase";9495/*96* Is the server ready to serve?97*/98volatile static boolean serverReady = false;99100/*101* Turn on SSL debugging?102*/103static boolean debug = false;104105/*106* If the client or server is doing some kind of object creation107* that the other side depends on, and that thread prematurely108* exits, you may experience a hang. The test harness will109* terminate all hung threads after its timeout has expired,110* currently 3 minutes by default, but you might try to be111* smart about it....112*/113114/*115* Define the server side of the test.116*117* If the server prematurely exits, serverReady will be set to true118* to avoid infinite hangs.119*/120void doServerSide() throws Exception {121Socket socket;122ServerSocket serverSocket;123if (useSSL) {124SSLServerSocketFactory sslssf =125(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();126serverSocket =127(SSLServerSocket) sslssf.createServerSocket(serverPort);128} else {129serverSocket = (ServerSocket) ServerSocketFactory.130getDefault().createServerSocket(serverPort);131}132serverPort = serverSocket.getLocalPort();133134/*135* Signal Client, we're ready for his connect.136*/137serverReady = true;138try {139socket = serverSocket.accept();140InputStream is = socket.getInputStream();141OutputStream os = socket.getOutputStream();142143/**144* Test some new methods of java.net.Socket added to merlin145*/146System.out.println("Server getChannel(): "147+ socket.getChannel());148try {149socket.setOOBInline(true);150} catch (IOException success) {151// Currently we throw an IOException if this method is called152}153try {154System.out.println("Server getOOBInline(): "155+ socket.getOOBInline());156} catch (IOException success) {157// Currently we throw an IOException if this method is called158}159System.out.println("Server read: " + is.read());160os.write(85);161os.flush();162socket.close();163} catch (Exception unexpected) {164throw new Exception(" test failed, caught exception: "165+ unexpected);166}167}168169/*170* Define the client side of the test.171*172* If the server prematurely exits, serverReady will be set to true173* to avoid infinite hangs.174*/175void doClientSide() throws Exception {176/*177* Wait for server to get started.178*/179while (!serverReady) {180Thread.sleep(50);181}182Socket socket;183if (useSSL) {184SSLSocketFactory sslsf =185(SSLSocketFactory) SSLSocketFactory.getDefault();186Socket plainSocket = new Socket("localhost", serverPort);187socket = (SSLSocket)188sslsf.createSocket(plainSocket, "localhost", serverPort, true);189}190else191socket = new Socket("localhost", serverPort);192try {193InputStream is = socket.getInputStream();194OutputStream os = socket.getOutputStream();195196/**197* test some new methods of java.net.Socket added to merlin.198*/199System.out.println("Client isInputShutdown() "200+ socket.isInputShutdown());201socket.setReuseAddress(true);202System.out.println("Client getReuseAddress(): "203+ socket.getReuseAddress());204205socket.setTrafficClass(8);206System.out.println("Client getTrafficClass(): "207+ socket.getTrafficClass());208209os.write(237);210os.flush();211System.out.println("Client read: " + is.read());212socket.close();213System.out.println("Client isOutputShutdown() "214+ socket.isOutputShutdown());215} catch (Exception unexpected) {216throw new Exception(" test failed, caught exception: "217+ unexpected);218}219}220221/*222* =============================================================223* The remainder is just support stuff224*/225226// use any free port by default227volatile int serverPort = 0;228229volatile Exception serverException = null;230volatile Exception clientException = null;231232public static void main(String[] args) throws Exception {233String keyFilename =234System.getProperty("test.src", "./") + "/" + pathToStores +235"/" + keyStoreFile;236String trustFilename =237System.getProperty("test.src", "./") + "/" + pathToStores +238"/" + trustStoreFile;239240System.setProperty("javax.net.ssl.keyStore", keyFilename);241System.setProperty("javax.net.ssl.keyStorePassword", passwd);242System.setProperty("javax.net.ssl.trustStore", trustFilename);243System.setProperty("javax.net.ssl.trustStorePassword", passwd);244245if (debug)246System.setProperty("javax.net.debug", "all");247248/*249* Start the tests.250*/251new NewSocketMethods();252}253254Thread clientThread = null;255Thread serverThread = null;256257/*258* Primary constructor, used to drive remainder of the test.259*260* Fork off the other side, then do your work.261*/262NewSocketMethods() throws Exception {263if (separateServerThread) {264startServer(true);265startClient(false);266} else {267startClient(true);268startServer(false);269}270271/*272* Wait for other side to close down.273*/274if (separateServerThread) {275serverThread.join();276} else {277clientThread.join();278}279280/*281* When we get here, the test is pretty much over.282*283* If the main thread excepted, that propagates back284* immediately. If the other thread threw an exception, we285* should report back.286*/287if (serverException != null)288throw serverException;289if (clientException != null)290throw clientException;291}292293void startServer(boolean newThread) throws Exception {294if (newThread) {295serverThread = new Thread() {296public void run() {297try {298doServerSide();299} catch (Exception e) {300/*301* Our server thread just died.302*303* Release the client, if not active already...304*/305System.err.println("Server died... ");306e.printStackTrace();307serverReady = true;308serverException = e;309}310}311};312serverThread.start();313} else {314doServerSide();315}316}317318void startClient(boolean newThread) throws Exception {319if (newThread) {320clientThread = new Thread() {321public void run() {322try {323doClientSide();324} catch (Exception e) {325/*326* Our client thread just died.327*/328System.err.println("Client died...");329clientException = e;330}331}332};333clientThread.start();334} else {335doClientSide();336}337}338}339340341