Path: blob/master/test/jdk/java/rmi/transport/checkFQDN/CheckFQDN.java
41154 views
/*1* Copyright (c) 1998, 2017, 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/* @test24* @bug 411568325* @summary Endpoint hostnames should always be fully qualified or26* should be an ip address. When references to remote27* objects are passed outside of the local domain their28* endpoints may contain hostnames that are not fully29* qualified. Hence remote clients won't be able to contact30* the referenced remote obect.31*32* @author Laird Dornin33*34* @library ../../testlibrary35* @modules java.rmi/sun.rmi.registry36* java.rmi/sun.rmi.server37* java.rmi/sun.rmi.transport38* java.rmi/sun.rmi.transport.tcp39* @build TestLibrary CheckFQDNClient CheckFQDN_Stub TellServerName40* @run main/othervm/timeout=120 CheckFQDN41*/4243/**44* Get the hostname used by rmi using different rmi properities:45*46* if set java.rmi.server.hostname, hostname should equal this47* property.48*49* if set java.rmi.server.useLocalHostname, hostname must contain a '.'50*51* if set no properties hostname should be an ipaddress.52*53* if set java.rmi.server.hostname, hostname should equal this54* property even if set java.rmi.server.useLocalHostname is true.55*56*/5758import java.rmi.*;59import java.rmi.registry.*;60import java.rmi.server.*;61import java.io.*;6263/**64* Export a remote object through which the exec'ed client vm can65* inform the main test what its host name is.66*/67public class CheckFQDN extends UnicastRemoteObject68implements TellServerName {69public static int REGISTRY_PORT =-1;70static String propertyBeingTested = null;71static String propertyBeingTestedValue = null;7273public static void main(String args[]) {7475Object dummy = new Object();76CheckFQDN checkFQDN = null;77try {78checkFQDN = new CheckFQDN();7980System.err.println81("\nRegression test for bug/rfe 4115683\n");8283Registry registry = TestLibrary.createRegistryOnEphemeralPort();84REGISTRY_PORT = TestLibrary.getRegistryPort(registry);85registry.bind("CheckFQDN", checkFQDN);8687/* test the host name scheme in different environments.*/88testProperty("java.rmi.server.useLocalHostname", "true", "");89testProperty("java.rmi.server.hostname", "thisIsJustAnRMITest", "");90testProperty("java.rmi.server.hostname", "thisIsJustAnRMITest",91" -Djava.rmi.server.useLocalHostname=true ");92testProperty("", "", "");9394} catch (Exception e) {95TestLibrary.bomb(e);96} finally {97if (checkFQDN != null) {98TestLibrary.unexport(checkFQDN);99}100}101System.err.println("\nTest for bug/ref 4115683 passed.\n");102}103104/**105* Spawn a vm and feed it a property which sets the client's rmi106* hostname.107*/108public static void testProperty(String property,109String propertyValue,110String extraProp)111{112JavaVM jvm = null;113try {114String propOption = "";115String equal = "";116if (!property.equals("")) {117propOption = " -D";118equal = "=";119}120121// create a client to tell checkFQDN what its rmi name is.122jvm = new JavaVM("CheckFQDNClient",123propOption + property +124equal +125propertyValue + 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=" +131REGISTRY_PORT,132"");133134propertyBeingTested=property;135propertyBeingTestedValue=propertyValue;136137if (jvm.execute() != 0) {138TestLibrary.bomb("Test failed, error in client.");139}140141} catch (Exception e) {142TestLibrary.bomb(e);143} finally {144if (jvm != null) {145jvm.destroy();146}147}148}149150CheckFQDN() throws RemoteException { }151152/**153* Remote method to allow client vm to tell the main test what its154* host name is .155*/156public void tellServerName(String serverName)157throws RemoteException {158159if (propertyBeingTested.equals("java.rmi.server.hostname")) {160if ( !propertyBeingTestedValue.equals(serverName)) {161TestLibrary.bomb(propertyBeingTested +162":\n Client rmi server name does " +163"not equal the one specified " +164"by java.rmi.server.hostname: " +165serverName +" != " +166propertyBeingTestedValue);167}168169/** use local host name, must contain a '.' */170} else if (propertyBeingTested.equals171("java.rmi.server.useLocalHostname")) {172if (serverName.indexOf('.') < 0) {173TestLibrary.bomb(propertyBeingTested +174":\nThe client servername contains no '.'");175}176} else {177// no propety set, must be ip address178if ((serverName.indexOf('.') < 0) ||179(!Character.isDigit(serverName.charAt(0)))) {180TestLibrary.bomb("Default name scheme:\n"+181" The client servername contains no '.'"+182"or is not an ip address");183}184}185System.err.println("Servername used: " + serverName);186}187}188189190