1 /*
   2  * Copyright (c) 1999, 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 /*
  25  * @test
  26  * @bug 4254808
  27  * @summary Naming assumes '/' is present in relative URL; change in URL causes regression
  28  * @author Dana Burns
  29  * @library ../../testlibrary
  30  * @build TestLibrary Legal LegalRegistryNames_Stub
  31  * @run main LegalRegistryNames
  32  */
  33 
  34 import java.net.InetAddress;
  35 import java.net.UnknownHostException;
  36 import java.rmi.Naming;
  37 import java.rmi.RemoteException;
  38 import java.rmi.Remote;
  39 import java.rmi.registry.LocateRegistry;
  40 import java.rmi.registry.Registry;
  41 import java.rmi.server.UnicastRemoteObject;
  42 import java.util.Enumeration;
  43 import java.util.Vector;
  44 
  45 /**
  46  * Ensure that all legal forms of Naming URLs operate with the
  47  * java.rmi.Naming interface.  This test requires using the default RMI Registry
  48  * port as it tests all of the RMI naming URL's, including the ones which do not
  49  * take a port (and therefore uses the default port).
  50  */
  51 public class LegalRegistryNames extends UnicastRemoteObject
  52     implements Legal
  53 {
  54 
  55     public LegalRegistryNames() throws java.rmi.RemoteException {}
  56 
  57     public static void main(String args[]) throws RuntimeException {
  58 
  59         System.err.println("\nRegression test for bug/rfe 4254808\n");
  60 
  61         Registry registry = null;
  62         LegalRegistryNames legal = null;
  63 
  64         boolean oneFormFailed = false;
  65         String[] names = null;
  66         Vector legalForms = getLegalForms();
  67         Remote shouldFind = null;
  68 
  69         // create a registry and the test object
  70         try {
  71             legal = new LegalRegistryNames();
  72 
  73             System.err.println("Starting registry on default port");
  74             registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
  75         } catch (Exception e) {
  76             TestLibrary.bomb("registry already running on test port");
  77         }
  78 
  79         // enumerate through all legal URLs to verify that a remote
  80         // object can be bound and unbound
  81         String s = null;
  82         Enumeration en = legalForms.elements();
  83         while (en.hasMoreElements()) {
  84             s = (String) en.nextElement();
  85 
  86             System.err.println("\ntesting form: " + s);
  87 
  88             try {
  89                 Naming.rebind(s, legal);
  90                 names = registry.list();
  91 
  92                 // ensure that the name in the registry is what is expected
  93                 if ((names.length > 0) &&
  94                     (names[0].compareTo("MyName") != 0))
  95                 {
  96                     oneFormFailed = true;
  97                     System.err.println("\tRegistry entry for form: " +
  98                                        s + " is incorrect: " + names[0]);
  99                 }
 100 
 101                 // ensure that the object can be unbound under the URL string
 102                 shouldFind = Naming.lookup(s);
 103                 Naming.unbind(s);
 104                 System.err.println("\tform " + s + " OK");
 105 
 106             } catch (Exception e) {
 107 
 108                 e.printStackTrace();
 109                 oneFormFailed = true;
 110                 System.err.println("\tunexpected lookup or unbind " +
 111                                    "exception for form: " + s + e.getMessage() );
 112             }
 113         }
 114         if (oneFormFailed) {
 115             TestLibrary.bomb("Test failed");
 116         }
 117 
 118         // get the test to exit quickly
 119         TestLibrary.unexport(legal);
 120     }
 121 
 122     /**
 123      * return a vector of valid legal RMI naming URLs.
 124      */
 125     private static Vector getLegalForms() {
 126         String localHostAddress = null;
 127         String localHostName = null;
 128 
 129         // get the local host name and address
 130         try {
 131             localHostName = InetAddress.getLocalHost().getHostName();
 132             localHostAddress = InetAddress.getLocalHost().getHostAddress();
 133         } catch(UnknownHostException e) {
 134             TestLibrary.bomb("Test failed: unexpected exception", e);
 135         }
 136 
 137         Vector legalForms = new Vector();
 138         legalForms.add("///MyName");
 139         legalForms.add("//:" + Registry.REGISTRY_PORT + "/MyName");
 140         legalForms.add("//" + localHostAddress + "/MyName");
 141         legalForms.add("//" + localHostAddress + ":" +
 142                        Registry.REGISTRY_PORT + "/MyName");
 143         legalForms.add("//localhost/MyName");
 144         legalForms.add("//localhost:" + Registry.REGISTRY_PORT + "/MyName");
 145         legalForms.add("//" + localHostName + "/MyName");
 146         legalForms.add("//" + localHostName + ":" + Registry.REGISTRY_PORT +
 147                        "/MyName");
 148         legalForms.add("MyName");
 149         legalForms.add("/MyName");
 150         legalForms.add("rmi:///MyName");
 151         legalForms.add("rmi://:" + Registry.REGISTRY_PORT + "/MyName");
 152         legalForms.add("rmi://" + localHostAddress + "/MyName");
 153         legalForms.add("rmi://" + localHostAddress + ":" +
 154                        Registry.REGISTRY_PORT + "/MyName");
 155         legalForms.add("rmi://localhost/MyName");
 156         legalForms.add("rmi://localhost:" + Registry.REGISTRY_PORT + "/MyName");
 157         legalForms.add("rmi://" + localHostName + "/MyName");
 158         legalForms.add("rmi://" + localHostName + ":" +
 159                        Registry.REGISTRY_PORT + "/MyName");
 160         return legalForms;
 161     }
 162 }