blob: ca1f247390c938a040dc84150c445262963af8a0 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
Lana Steuck657b9db2013-12-26 12:04:16 -08002 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
J. Duke319a3b92007-12-01 00:00:00 +00003 * 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 *
Kelly O'Hairfe008ae2010-05-25 15:58:33 -070019 * 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.
J. Duke319a3b92007-12-01 00:00:00 +000022 */
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
Mandy Chung53a97cb2015-05-28 10:54:48 -070035 * @modules java.rmi/sun.rmi.registry
36 * java.rmi/sun.rmi.server
37 * java.rmi/sun.rmi.transport
38 * java.rmi/sun.rmi.transport.tcp
Stuart Marks38887c72012-12-12 09:53:01 -080039 * @build TestLibrary
J. Duke319a3b92007-12-01 00:00:00 +000040 * @run main/othervm InvalidName
41 */
42
43import java.net.URL;
44import java.net.InetAddress;
45import java.net.MalformedURLException;
46import java.rmi.registry.Registry;
47import java.rmi.Naming;
48import java.rmi.Remote;
49import java.rmi.server.RemoteObject;
50import java.rmi.server.UnicastRemoteObject;
51
52public class InvalidName {
53 public static void main(String[] args) {
54
55 String testName;
56
57 // ensure that an exception is thrown for Naming URL with '#'
58 try {
59
60 System.err.println("\nRegression test for bugs " +
61 "4136563 and 4208801\n");
62
63 testName = new String("rmi://#/MyRMI");
64
65 Naming.lookup(testName);
66 System.err.println("no exception thrown for URL: " + testName);
67 throw new RuntimeException("test failed");
68
69 } catch (MalformedURLException e) {
70 // should have received malformed URL exception
71 System.err.println("Correctly received instance of " +
72 "MalformedURLException:");
73 System.err.println(e.getMessage());
74 e.printStackTrace();
75
76 } catch (Exception e) {
77 TestLibrary.bomb(e);
78 }
79
80 // ensure a correct null pointer exception is thrown for null URL
81 // ensure that a registry stub with a default hostname and one with a
82 // the local host's ip address are .equals()
83 try {
84 String localAddress = InetAddress.getLocalHost().getHostAddress();
85
86 Registry registry = (Registry) Naming.lookup("rmi:///");
87
88 if (registry.toString().indexOf(localAddress) >= 0) {
89 System.err.println("verified registry endpoint contains ipaddress");
90 } else {
91 TestLibrary.bomb("registry endpoint does not contain ipaddress");
92 }
93 } catch (Exception e) {
94 TestLibrary.bomb(e);
95 }
96
97 System.err.println("test passed");
98 }
99}