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  * @library ../testlibrary
  29  * @build TestLibrary
  30  * @run main/othervm -Djava.net.preferIPv6Addresses=true LookupIPv6
  31  */
  32 
  33 import java.net.InetAddress;
  34 import java.net.Inet6Address;
  35 import java.net.MalformedURLException;
  36 import java.rmi.Naming;
  37 import java.rmi.registry.LocateRegistry;
  38 import java.rmi.registry.Registry;
  39 
  40 public 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         int port = TestLibrary.getUnusedRandomPort();
  67         InetAddress localAddr = InetAddress.getAllByName(null)[0];
  68         if (localAddr instanceof Inet6Address) {
  69             System.out.println("IPv6 detected");
  70             Registry reg;
  71             try {
  72                 reg = LocateRegistry.createRegistry(port);
  73             } catch (Exception ex) {
  74                 reg = LocateRegistry.getRegistry();
  75             }
  76             reg.rebind("foo", reg);
  77             Naming.lookup(String.format("rmi://[%s]:%d/foo",
  78                           localAddr.getHostAddress(), port));
  79         }
  80     }
  81 }