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