CheckFQDN.java 6.2 KB
Newer Older
1
/*
2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35
  */

/* @test
 * @bug 4115683
 * @summary Endpoint hostnames should always be fully qualified or
 *          should be an ip address.  When references to remote
 *          objects are passed outside of the local domain their
 *          endpoints may contain hostnames that are not fully
 *          qualified.  Hence remote clients won't be able to contact
 *          the referenced remote obect.
 *
 * @author Laird Dornin
 *
 * @library ../../testlibrary
36
 * @build TestLibrary CheckFQDNClient CheckFQDN_Stub TellServerName
37
 * @run main/othervm/timeout=120 CheckFQDN
D
duke 已提交
38 39
 */

40
/**
D
duke 已提交
41 42 43 44
 * Get the hostname used by rmi using different rmi properities:
 *
 * if set java.rmi.server.hostname, hostname should equal this
 * property.
45
 *
D
duke 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 * if set java.rmi.server.useLocalHostname, hostname must contain a '.'
 *
 * if set no properties hostname should be an ipaddress.
 *
 * if set java.rmi.server.hostname, hostname should equal this
 * property even if set java.rmi.server.useLocalHostname is true.
 *
 */

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.io.*;

/**
61
 * Export a remote object through which the exec'ed client vm can
D
duke 已提交
62 63
 * inform the main test what its host name is.
 */
64
public class CheckFQDN extends UnicastRemoteObject
D
duke 已提交
65
    implements TellServerName {
D
dmocek 已提交
66
    public static int REGISTRY_PORT =-1;
D
duke 已提交
67 68 69 70 71
    static String propertyBeingTested = null;
    static String propertyBeingTestedValue = null;

    public static void main(String args[]) {

72 73 74 75 76 77 78 79
        Object dummy = new Object();
        CheckFQDN checkFQDN = null;
        try {
            checkFQDN = new CheckFQDN();

            System.err.println
                ("\nRegression test for bug/rfe 4115683\n");

D
dmocek 已提交
80 81
            Registry registry = TestLibrary.createRegistryOnUnusedPort();
            REGISTRY_PORT = TestLibrary.getRegistryPort(registry);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
            registry.bind("CheckFQDN", checkFQDN);

            /* test the host name scheme in different environments.*/
            testProperty("java.rmi.server.useLocalHostname", "true", "");
            testProperty("java.rmi.server.hostname", "thisIsJustAnRMITest", "");
            testProperty("java.rmi.server.hostname", "thisIsJustAnRMITest",
                         " -Djava.rmi.server.useLocalHostname=true ");
            testProperty("", "", "");

        } catch (Exception e) {
            TestLibrary.bomb(e);
        } finally {
            if (checkFQDN != null) {
                TestLibrary.unexport(checkFQDN);
            }
        }
        System.err.println("\nTest for bug/ref 4115683 passed.\n");
D
duke 已提交
99 100
    }

101
    /**
D
duke 已提交
102 103 104
     * Spawn a vm and feed it a property which sets the client's rmi
     * hostname.
     */
105 106 107
    public static void testProperty(String property,
                                    String propertyValue,
                                    String extraProp)
D
duke 已提交
108
    {
109 110 111 112 113 114 115 116
        try {
            String propOption = "";
            String equal = "";
            if (!property.equals("")) {
                propOption = " -D";
                equal = "=";
            }

117
            // create a client to tell checkFQDN what its rmi name is.
118 119 120
            JavaVM jvm = new JavaVM("CheckFQDNClient",
                                    propOption + property +
                                    equal +
D
dmocek 已提交
121 122 123
                                    propertyValue + extraProp +
                                    " -Drmi.registry.port=" +
                                    REGISTRY_PORT,
124 125 126 127 128
                                    "");

            propertyBeingTested=property;
            propertyBeingTestedValue=propertyValue;

129
            if (jvm.execute() != 0) {
130 131 132 133 134 135
                TestLibrary.bomb("Test failed, error in client.");
            }

        } catch (Exception e) {
            TestLibrary.bomb(e);
        }
D
duke 已提交
136 137 138 139
    }

    CheckFQDN() throws RemoteException { }

140 141
    /**
     * Remote method to allow client vm to tell the main test what its
D
duke 已提交
142 143
     * host name is .
     */
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    public void tellServerName(String serverName)
        throws RemoteException {

        if (propertyBeingTested.equals("java.rmi.server.hostname")) {
            if ( !propertyBeingTestedValue.equals(serverName)) {
                TestLibrary.bomb(propertyBeingTested +
                     ":\n Client rmi server name does " +
                     "not equal the one specified " +
                     "by java.rmi.server.hostname: " +
                     serverName +" != " +
                     propertyBeingTestedValue);
            }

            /** use local host name, must contain a '.' */
        } else if (propertyBeingTested.equals
                   ("java.rmi.server.useLocalHostname")) {
            if (serverName.indexOf('.') < 0) {
                TestLibrary.bomb(propertyBeingTested +
                     ":\nThe client servername contains no '.'");
            }
        } else {
            // no propety set, must be ip address
            if ((serverName.indexOf('.') < 0) ||
                (!Character.isDigit(serverName.charAt(0)))) {
                TestLibrary.bomb("Default name scheme:\n"+
                     " The client servername contains no '.'"+
                     "or is not an ip address");
            }
        }
        System.err.println("Servername used: " + serverName);
D
duke 已提交
174 175
    }
}