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