Path: blob/master/test/jdk/java/rmi/invalidName/InvalidName.java
41152 views
/*1* Copyright (c) 1998, 2012, 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 413656325* @summary Naming.lookup fails to throw exception for invalid hostname26*27* @bug 420880128* @summary (1.x) java.rmi.Naming defaults to local hostname, not29* local IP address30*31* @author Laird Dornin32*33* @library ../testlibrary34* @modules java.rmi/sun.rmi.registry35* java.rmi/sun.rmi.server36* java.rmi/sun.rmi.transport37* java.rmi/sun.rmi.transport.tcp38* @build TestLibrary39* @run main/othervm InvalidName40*/4142import java.net.URL;43import java.net.InetAddress;44import java.net.MalformedURLException;45import java.rmi.registry.Registry;46import java.rmi.Naming;47import java.rmi.Remote;48import java.rmi.server.RemoteObject;49import java.rmi.server.UnicastRemoteObject;5051public class InvalidName {52public static void main(String[] args) {5354String testName;5556// ensure that an exception is thrown for Naming URL with '#'57try {5859System.err.println("\nRegression test for bugs " +60"4136563 and 4208801\n");6162testName = new String("rmi://#/MyRMI");6364Naming.lookup(testName);65System.err.println("no exception thrown for URL: " + testName);66throw new RuntimeException("test failed");6768} catch (MalformedURLException e) {69// should have received malformed URL exception70System.err.println("Correctly received instance of " +71"MalformedURLException:");72System.err.println(e.getMessage());73e.printStackTrace();7475} catch (Exception e) {76TestLibrary.bomb(e);77}7879// ensure a correct null pointer exception is thrown for null URL80// ensure that a registry stub with a default hostname and one with a81// the local host's ip address are .equals()82try {83String localAddress = InetAddress.getLocalHost().getHostAddress();8485Registry registry = (Registry) Naming.lookup("rmi:///");8687if (registry.toString().indexOf(localAddress) >= 0) {88System.err.println("verified registry endpoint contains ipaddress");89} else {90TestLibrary.bomb("registry endpoint does not contain ipaddress");91}92} catch (Exception e) {93TestLibrary.bomb(e);94}9596System.err.println("test passed");97}98}99100101