1 /*
   2  * Copyright (c) 2001, 2018, 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  * @modules java.rmi/sun.rmi.registry
  30  *          java.rmi/sun.rmi.server
  31  *          java.rmi/sun.rmi.transport
  32  *          java.rmi/sun.rmi.transport.tcp
  33  * @build RegistryVM
  34  * @run main/othervm -Djava.net.preferIPv6Addresses=true LookupIPv6
  35  */
  36 
  37 import java.io.Serializable;
  38 import java.net.InetAddress;
  39 import java.net.Inet6Address;
  40 import java.net.MalformedURLException;
  41 import java.rmi.Naming;
  42 import java.rmi.Remote;
  43 
  44 public class LookupIPv6 {
  45     public static void main(String[] args) throws Exception {
  46         // use loopback IPv6 address to avoid lengthy socket connection delays
  47         String[] urls = {
  48             "rmi://[0000:0000:0000:0000:0000:0000:0000:0001]/foo",
  49             "//[0:0:0:0:0:0:0:1]:88/foo",
  50             "rmi://[0::0:0:0:1]/foo:bar",
  51             "//[::1]:88"
  52         };
  53         for (int i = 0; i < urls.length; i++) {
  54             try {
  55                 Naming.lookup(urls[i]);
  56             } catch (MalformedURLException ex) {
  57                 throw ex;
  58             } catch (Exception ex) {
  59                 // URLs are bogus, lookups expected to fail
  60             }
  61         }
  62 
  63         /* Attempt to use IPv6-based URL to look up object in local registry.
  64          * Since not all platforms support IPv6, this portion of the test may
  65          * be a no-op in some cases.  On supporting platforms, the first
  66          * element of the array returned by InetAddress.getAllByName should be
  67          * an Inet6Address since this test is run with
  68          * -Djava.net.preferIPv6Addresses=true.
  69          */
  70         InetAddress localAddr = InetAddress.getAllByName(null)[0];
  71         if (localAddr instanceof Inet6Address) {
  72             System.out.println("IPv6 detected");
  73             RegistryVM rvm = RegistryVM.createRegistryVM();
  74             try {
  75                 rvm.start();
  76                 String name = String.format("rmi://[%s]:%d/foo",
  77                         localAddr.getHostAddress(), rvm.getPort());
  78                 Naming.rebind(name, new R());
  79                 Naming.lookup(name);
  80             } finally {
  81                 rvm.cleanup();
  82             }
  83         }
  84     }
  85 
  86     private static class R implements Remote, Serializable { }
  87 }