Path: blob/master/test/jdk/sun/security/ssl/ServerHandshaker/GetPeerHostServer.java
41152 views
/*1* Copyright (c) 2000, 2012, 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*25* Test for bug 430202626* Summary: make sure the server side doesn't do DNS lookup.27* The server waits for a request from the client.28* Make sure that the SSLSession.getPeerHost()29* returns the peer's IP address instead of the peer's30* host name.31*/3233import java.io.*;34import java.security.*;35import java.net.*;36import javax.net.*;37import javax.net.ssl.*;3839class GetPeerHostServer extends Thread40{41private String host;42ServerSocket ss;43boolean isHostIPAddr = false;44int serverPort = 0;4546public GetPeerHostServer ()47{48try {49SSLContext ctx = SSLContext.getInstance("TLS");50KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");51KeyStore ks = KeyStore.getInstance("JKS");52char[] passphrase = "passphrase".toCharArray();53String testRoot = System.getProperty("test.src", ".");54ks.load(new FileInputStream(testRoot55+ "/../../../../javax/net/ssl/etc/keystore"),56passphrase);57kmf.init(ks, passphrase);58ctx.init(kmf.getKeyManagers(), null, null);59ServerSocketFactory ssf = ctx.getServerSocketFactory();60ss = ssf.createServerSocket(serverPort);61serverPort = ss.getLocalPort();62}catch (Exception e) {63System.err.println("Unexpected exceptions: " + e);64e.printStackTrace();65}66}6768public void run() {69try {70System.out.println("SERVER: waiting for requests...");71Socket socket = ss.accept();72System.out.println("SERVER: got a request!");73host = ((javax.net.ssl.SSLSocket)socket).getSession().getPeerHost();74System.out.println("SERVER: Host IP address (not the name): "75+ host);76} catch (Exception e) {77System.err.println("Unexpected exceptions: " + e);78e.printStackTrace();79}8081if (host != null && (host.charAt(0) > '9') ||82(host.charAt(0) < '0')) {83System.out.println("Error: bug 4302026 may not be fixed.");84} else {85isHostIPAddr = true;86System.out.println("Passed!");87}88}899091boolean getPassStatus () {92return isHostIPAddr;93}9495int getServerPort() {96return serverPort;97}98}99100101