blob: d54b8ae7b896c99425fd5511baca3dca78425b01 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001-2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 4402708
26 *
27 * @run main/othervm -Djava.net.preferIPv6Addresses=true LookupIPv6
28 *
29 * @summary Ensure that java.rmi.Naming.lookup can handle URLs containing
30 * IPv6 addresses.
31 */
32
33import java.net.InetAddress;
34import java.net.Inet6Address;
35import java.net.MalformedURLException;
36import java.rmi.Naming;
37import java.rmi.registry.LocateRegistry;
38import java.rmi.registry.Registry;
39
40public class LookupIPv6 {
41 public static void main(String[] args) throws Exception {
42 // use loopback IPv6 address to avoid lengthy socket connection delays
43 String[] urls = {
44 "rmi://[0000:0000:0000:0000:0000:0000:0000:0001]/foo",
45 "//[0:0:0:0:0:0:0:1]:88/foo",
46 "rmi://[0::0:0:0:1]/foo:bar",
47 "//[::1]:88"
48 };
49 for (int i = 0; i < urls.length; i++) {
50 try {
51 Naming.lookup(urls[i]);
52 } catch (MalformedURLException ex) {
53 throw ex;
54 } catch (Exception ex) {
55 // URLs are bogus, lookups expected to fail
56 }
57 }
58
59 /* Attempt to use IPv6-based URL to look up object in local registry.
60 * Since not all platforms support IPv6, this portion of the test may
61 * be a no-op in some cases. On supporting platforms, the first
62 * element of the array returned by InetAddress.getAllByName should be
63 * an Inet6Address since this test is run with
64 * -Djava.net.preferIPv6Addresses=true.
65 */
66 InetAddress localAddr = InetAddress.getAllByName(null)[0];
67 if (localAddr instanceof Inet6Address) {
68 System.out.println("IPv6 detected");
69 Registry reg;
70 try {
71 reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
72 } catch (Exception ex) {
73 reg = LocateRegistry.getRegistry();
74 }
75 reg.rebind("foo", reg);
76 Naming.lookup("rmi://[" + localAddr.getHostAddress() + "]/foo");
77 }
78 }
79}