1 /*
   2  * Copyright (c) 1998, 2015, 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 /* @test
  25  * @bug 4115683
  26  * @summary Endpoint hostnames should always be fully qualified or
  27  *          should be an ip address.  When references to remote
  28  *          objects are passed outside of the local domain their
  29  *          endpoints may contain hostnames that are not fully
  30  *          qualified.  Hence remote clients won't be able to contact
  31  *          the referenced remote obect.
  32  *
  33  * @author Laird Dornin
  34  *
  35  * @library ../../testlibrary
  36  * @modules java.rmi/sun.rmi.registry
  37  *          java.rmi/sun.rmi.server
  38  *          java.rmi/sun.rmi.transport
  39  *          java.rmi/sun.rmi.transport.tcp
  40  * @build TestLibrary CheckFQDNClient CheckFQDN_Stub TellServerName
  41  * @run main/othervm/timeout=120 CheckFQDN
  42  */
  43 
  44 /**
  45  * Get the hostname used by rmi using different rmi properities:
  46  *
  47  * if set java.rmi.server.hostname, hostname should equal this
  48  * property.
  49  *
  50  * if set java.rmi.server.useLocalHostname, hostname must contain a '.'
  51  *
  52  * if set no properties hostname should be an ipaddress.
  53  *
  54  * if set java.rmi.server.hostname, hostname should equal this
  55  * property even if set java.rmi.server.useLocalHostname is true.
  56  *
  57  */
  58 
  59 import java.rmi.*;
  60 import java.rmi.registry.*;
  61 import java.rmi.server.*;
  62 import java.io.*;
  63 
  64 /**
  65  * Export a remote object through which the exec'ed client vm can
  66  * inform the main test what its host name is.
  67  */
  68 public class CheckFQDN extends UnicastRemoteObject
  69     implements TellServerName {
  70     public static int REGISTRY_PORT =-1;
  71     static String propertyBeingTested = null;
  72     static String propertyBeingTestedValue = null;
  73 
  74     public static void main(String args[]) {
  75 
  76         Object dummy = new Object();
  77         CheckFQDN checkFQDN = null;
  78         try {
  79             checkFQDN = new CheckFQDN();
  80 
  81             System.err.println
  82                 ("\nRegression test for bug/rfe 4115683\n");
  83 
  84             Registry registry = TestLibrary.createRegistryOnUnusedPort();
  85             REGISTRY_PORT = TestLibrary.getRegistryPort(registry);
  86             registry.bind("CheckFQDN", checkFQDN);
  87 
  88             /* test the host name scheme in different environments.*/
  89             testProperty("java.rmi.server.useLocalHostname", "true", "");
  90             testProperty("java.rmi.server.hostname", "thisIsJustAnRMITest", "");
  91             testProperty("java.rmi.server.hostname", "thisIsJustAnRMITest",
  92                          " -Djava.rmi.server.useLocalHostname=true ");
  93             testProperty("", "", "");
  94 
  95         } catch (Exception e) {
  96             TestLibrary.bomb(e);
  97         } finally {
  98             if (checkFQDN != null) {
  99                 TestLibrary.unexport(checkFQDN);
 100             }
 101         }
 102         System.err.println("\nTest for bug/ref 4115683 passed.\n");
 103     }
 104 
 105     /**
 106      * Spawn a vm and feed it a property which sets the client's rmi
 107      * hostname.
 108      */
 109     public static void testProperty(String property,
 110                                     String propertyValue,
 111                                     String extraProp)
 112     {
 113         try {
 114             String propOption = "";
 115             String equal = "";
 116             if (!property.equals("")) {
 117                 propOption = " -D";
 118                 equal = "=";
 119             }
 120 
 121             // create a client to tell checkFQDN what its rmi name is.
 122             JavaVM jvm = new JavaVM("CheckFQDNClient",
 123                                     propOption + property +
 124                                     equal +
 125                                     propertyValue + extraProp +
 126                                     " --add-exports=java.rmi/sun.rmi.registry=ALL-UNNAMED" +
 127                                     " --add-exports=java.rmi/sun.rmi.server=ALL-UNNAMED" +
 128                                     " --add-exports=java.rmi/sun.rmi.transport=ALL-UNNAMED" +
 129                                     " --add-exports=java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED" +
 130                                     " -Drmi.registry.port=" +
 131                                     REGISTRY_PORT,
 132                                     "");
 133 
 134             propertyBeingTested=property;
 135             propertyBeingTestedValue=propertyValue;
 136 
 137             if (jvm.execute() != 0) {
 138                 TestLibrary.bomb("Test failed, error in client.");
 139             }
 140 
 141         } catch (Exception e) {
 142             TestLibrary.bomb(e);
 143         }
 144     }
 145 
 146     CheckFQDN() throws RemoteException { }
 147 
 148     /**
 149      * Remote method to allow client vm to tell the main test what its
 150      * host name is .
 151      */
 152     public void tellServerName(String serverName)
 153         throws RemoteException {
 154 
 155         if (propertyBeingTested.equals("java.rmi.server.hostname")) {
 156             if ( !propertyBeingTestedValue.equals(serverName)) {
 157                 TestLibrary.bomb(propertyBeingTested +
 158                      ":\n Client rmi server name does " +
 159                      "not equal the one specified " +
 160                      "by java.rmi.server.hostname: " +
 161                      serverName +" != " +
 162                      propertyBeingTestedValue);
 163             }
 164 
 165             /** use local host name, must contain a '.' */
 166         } else if (propertyBeingTested.equals
 167                    ("java.rmi.server.useLocalHostname")) {
 168             if (serverName.indexOf('.') < 0) {
 169                 TestLibrary.bomb(propertyBeingTested +
 170                      ":\nThe client servername contains no '.'");
 171             }
 172         } else {
 173             // no propety set, must be ip address
 174             if ((serverName.indexOf('.') < 0) ||
 175                 (!Character.isDigit(serverName.charAt(0)))) {
 176                 TestLibrary.bomb("Default name scheme:\n"+
 177                      " The client servername contains no '.'"+
 178                      "or is not an ip address");
 179             }
 180         }
 181         System.err.println("Servername used: " + serverName);
 182     }
 183 }