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