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