Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/Naming/LookupIPv6.java
38813 views
/*1* Copyright (c) 2001, 2012, 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*28* @run main/othervm -Djava.net.preferIPv6Addresses=true LookupIPv629*/3031import java.net.InetAddress;32import java.net.Inet6Address;33import java.net.MalformedURLException;34import java.rmi.Naming;35import java.rmi.registry.LocateRegistry;36import java.rmi.registry.Registry;3738public class LookupIPv6 {39public static void main(String[] args) throws Exception {40// use loopback IPv6 address to avoid lengthy socket connection delays41String[] urls = {42"rmi://[0000:0000:0000:0000:0000:0000:0000:0001]/foo",43"//[0:0:0:0:0:0:0:1]:88/foo",44"rmi://[0::0:0:0:1]/foo:bar",45"//[::1]:88"46};47for (int i = 0; i < urls.length; i++) {48try {49Naming.lookup(urls[i]);50} catch (MalformedURLException ex) {51throw ex;52} catch (Exception ex) {53// URLs are bogus, lookups expected to fail54}55}5657/* Attempt to use IPv6-based URL to look up object in local registry.58* Since not all platforms support IPv6, this portion of the test may59* be a no-op in some cases. On supporting platforms, the first60* element of the array returned by InetAddress.getAllByName should be61* an Inet6Address since this test is run with62* -Djava.net.preferIPv6Addresses=true.63*/64InetAddress localAddr = InetAddress.getAllByName(null)[0];65if (localAddr instanceof Inet6Address) {66System.out.println("IPv6 detected");67Registry reg;68try {69reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);70} catch (Exception ex) {71reg = LocateRegistry.getRegistry();72}73reg.rebind("foo", reg);74Naming.lookup("rmi://[" + localAddr.getHostAddress() + "]/foo");75}76}77}787980