1 /*
   2  * Copyright (c) 1998, 2012, 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 4136563
  26  * @summary Naming.lookup fails to throw exception for invalid hostname
  27  *
  28  * @bug 4208801
  29  * @summary (1.x) java.rmi.Naming defaults to local hostname, not
  30  *          local IP address
  31  *
  32  * @author Laird Dornin
  33  *
  34  * @library ../testlibrary
  35  * @build TestLibrary
  36  * @run main/othervm InvalidName
  37  */
  38 
  39 import java.net.URL;
  40 import java.net.InetAddress;
  41 import java.net.MalformedURLException;
  42 import java.rmi.registry.Registry;
  43 import java.rmi.Naming;
  44 import java.rmi.Remote;
  45 import java.rmi.server.RemoteObject;
  46 import java.rmi.server.UnicastRemoteObject;
  47 
  48 public class InvalidName {
  49     public static void main(String[] args) {
  50 
  51         String testName;
  52 
  53         // ensure that an exception is thrown for Naming URL with '#'
  54         try {
  55 
  56             System.err.println("\nRegression test for bugs " +
  57                                "4136563 and 4208801\n");
  58 
  59             testName = new String("rmi://#/MyRMI");
  60 
  61             Naming.lookup(testName);
  62             System.err.println("no exception thrown for URL: " + testName);
  63             throw new RuntimeException("test failed");
  64 
  65         } catch (MalformedURLException e) {
  66             // should have received malformed URL exception
  67             System.err.println("Correctly received instance of " +
  68                                "MalformedURLException:");
  69             System.err.println(e.getMessage());
  70             e.printStackTrace();
  71 
  72         } catch (Exception e) {
  73             TestLibrary.bomb(e);
  74         }
  75 
  76         // ensure a correct null pointer exception is thrown for null URL
  77         // ensure that a registry stub with a default hostname and one with a
  78         // the local host's ip address are .equals()
  79         try {
  80             String localAddress =  InetAddress.getLocalHost().getHostAddress();
  81 
  82             Registry registry = (Registry) Naming.lookup("rmi:///");
  83 
  84             if (registry.toString().indexOf(localAddress) >= 0) {
  85                 System.err.println("verified registry endpoint contains ipaddress");
  86             } else {
  87                 TestLibrary.bomb("registry endpoint does not contain ipaddress");
  88             }
  89         } catch (Exception e) {
  90             TestLibrary.bomb(e);
  91         }
  92 
  93         System.err.println("test passed");
  94     }
  95 }