Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLSocketImpl/ReverseNameLookup.java
38853 views
/*1* Copyright (c) 2002, 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 474829226* @summary Prevent/Disable reverse name lookups with JSSE SSL sockets27* @run main/othervm ReverseNameLookup28*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 ReverseNameLookup {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 = true;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* If the client or server is doing some kind of object creation72* that the other side depends on, and that thread prematurely73* exits, you may experience a hang. The test harness will74* terminate all hung threads after its timeout has expired,75* currently 3 minutes by default, but you might try to be76* smart about it....77*/7879/*80* Define the server side of the test.81*82* If the server prematurely exits, serverReady will be set to true83* to avoid infinite hangs.84*/85void doServerSide() throws Exception {86SSLServerSocketFactory sslssf =87(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();88SSLServerSocket sslServerSocket =89(SSLServerSocket) sslssf.createServerSocket(serverPort);9091serverPort = sslServerSocket.getLocalPort();9293/*94* Signal Client, we're ready for his connect.95*/96serverReady = true;9798SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();99InputStream sslIS = sslSocket.getInputStream();100OutputStream sslOS = sslSocket.getOutputStream();101102sslIS.read();103sslOS.write(85);104sslOS.flush();105106sslSocket.close();107}108109/*110* Define the client side of the test.111*112* If the server prematurely exits, serverReady will be set to true113* to avoid infinite hangs.114*/115void doClientSide() throws Exception {116117/*118* Wait for server to get started.119*/120while (!serverReady) {121Thread.sleep(50);122}123124SSLSocketFactory sslsf =125(SSLSocketFactory) SSLSocketFactory.getDefault();126SSLSocket sslSocket = (SSLSocket)127sslsf.createSocket("127.0.0.1", serverPort);128129InputStream sslIS = sslSocket.getInputStream();130OutputStream sslOS = sslSocket.getOutputStream();131132sslOS.write(280);133sslOS.flush();134sslIS.read();135SSLSession session = sslSocket.getSession();136if (!session.getPeerHost().equals("127.0.0.1")) {137throw new RuntimeException("we shouldn't do reverse name lookup");138}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 ReverseNameLookup();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*/183ReverseNameLookup() 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 active already...225*/226System.err.println("Server died...");227serverReady = true;228serverException = e;229}230}231};232serverThread.start();233} else {234try {235doServerSide();236} finally {237serverReady = true;238}239}240}241242void startClient(boolean newThread) throws Exception {243if (newThread) {244clientThread = new Thread() {245public void run() {246try {247doClientSide();248} catch (Exception e) {249/*250* Our client thread just died.251*/252System.err.println("Client died...");253clientException = e;254}255}256};257clientThread.start();258} else {259doClientSide();260}261}262}263264265