1/*
2 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 *
26 * Test for bug 4302026
27 * Summary: make sure the server side doesn't do DNS lookup.
28 * The server waits for a request from the client.
29 * Make sure that the SSLSession.getPeerHost()
30 * returns the peer's IP address instead of the peer's
31 * host name.
32 */
33
34import java.io.*;
35import java.security.*;
36import java.net.*;
37import javax.net.*;
38import javax.net.ssl.*;
39
40class GetPeerHostServer extends Thread
41{
42    private String host;
43    ServerSocket ss;
44    boolean isHostIPAddr = false;
45    int serverPort = 0;
46
47    public GetPeerHostServer ()
48    {
49        try {
50            SSLContext ctx = SSLContext.getInstance("TLS");
51            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
52            KeyStore ks = KeyStore.getInstance("JKS");
53            char[] passphrase = "passphrase".toCharArray();
54            String testRoot = System.getProperty("test.src", ".");
55            ks.load(new FileInputStream(testRoot
56                        + "/../../../../javax/net/ssl/etc/keystore"),
57                    passphrase);
58            kmf.init(ks, passphrase);
59            ctx.init(kmf.getKeyManagers(), null, null);
60            ServerSocketFactory ssf = ctx.getServerSocketFactory();
61            ss = ssf.createServerSocket(serverPort);
62            serverPort = ss.getLocalPort();
63        }catch (Exception e) {
64            System.err.println("Unexpected exceptions: " + e);
65            e.printStackTrace();
66        }
67    }
68
69    public void run() {
70        try {
71            System.out.println("SERVER: waiting for requests...");
72            Socket socket = ss.accept();
73            System.out.println("SERVER: got a request!");
74            host = ((javax.net.ssl.SSLSocket)socket).getSession().getPeerHost();
75            System.out.println("SERVER: Host IP address (not the name): "
76                                + host);
77        } catch (Exception e) {
78            System.err.println("Unexpected exceptions: " + e);
79            e.printStackTrace();
80          }
81
82        if (host != null && (host.charAt(0) > '9') ||
83                            (host.charAt(0) < '0')) {
84            System.out.println("Error: bug 4302026 may not be fixed.");
85        } else {
86             isHostIPAddr = true;
87             System.out.println("Passed!");
88         }
89    }
90
91
92    boolean getPassStatus () {
93        return isHostIPAddr;
94    }
95
96    int getServerPort() {
97        return serverPort;
98    }
99}
100