Path: blob/master/test/jdk/java/rmi/Naming/LookupIPv6.java
41149 views
/*1* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary Ensure that java.rmi.Naming.lookup can handle URLs containing25* IPv6 addresses.26* @bug 440270827* @library ../testlibrary28* @modules java.rmi/sun.rmi.registry29* java.rmi/sun.rmi.server30* java.rmi/sun.rmi.transport31* java.rmi/sun.rmi.transport.tcp32* @build RegistryVM33* @run main/othervm -Djava.net.preferIPv6Addresses=true LookupIPv634*/3536import java.io.Serializable;37import java.net.InetAddress;38import java.net.Inet6Address;39import java.net.MalformedURLException;40import java.rmi.Naming;41import java.rmi.Remote;4243public class LookupIPv6 {44public static void main(String[] args) throws Exception {45// use loopback IPv6 address to avoid lengthy socket connection delays46String[] urls = {47"rmi://[0000:0000:0000:0000:0000:0000:0000:0001]/foo",48"//[0:0:0:0:0:0:0:1]:88/foo",49"rmi://[0::0:0:0:1]/foo:bar",50"//[::1]:88"51};52for (int i = 0; i < urls.length; i++) {53try {54Naming.lookup(urls[i]);55} catch (MalformedURLException ex) {56throw ex;57} catch (Exception ex) {58// URLs are bogus, lookups expected to fail59}60}6162/* Attempt to use IPv6-based URL to look up object in local registry.63* Since not all platforms support IPv6, this portion of the test may64* be a no-op in some cases. On supporting platforms, the first65* element of the array returned by InetAddress.getAllByName should be66* an Inet6Address since this test is run with67* -Djava.net.preferIPv6Addresses=true.68*/69InetAddress localAddr = InetAddress.getAllByName(null)[0];70if (localAddr instanceof Inet6Address) {71System.out.println("IPv6 detected");72RegistryVM rvm = RegistryVM.createRegistryVM();73try {74rvm.start();75String name = String.format("rmi://[%s]:%d/foo",76localAddr.getHostAddress(), rvm.getPort());77Naming.rebind(name, new R());78Naming.lookup(name);79} finally {80rvm.cleanup();81}82}83}8485private static class R implements Remote, Serializable { }86}878889