Path: blob/master/test/jdk/java/net/InetAddress/ptr/Lookup.java
41152 views
/*1* Copyright (c) 2002, 2019, 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/*24* Lookup/reverse lookup class for regression test 477352125* @test26* @bug 477352127* @summary Test that reverse lookups of IPv4 addresses work when IPv628* is enabled29* @library /test/lib30* @build jdk.test.lib.Utils31* jdk.test.lib.Asserts32* jdk.test.lib.JDKToolFinder33* jdk.test.lib.JDKToolLauncher34* jdk.test.lib.Platform35* jdk.test.lib.process.*36* Lookup37* @run main Lookup root38*39*/40import java.io.IOException;41import java.net.InetAddress;42import java.net.UnknownHostException;43import java.util.List;44import java.util.stream.Stream;45import java.util.stream.Collectors;4647import jdk.test.lib.JDKToolFinder;48import jdk.test.lib.process.OutputAnalyzer;4950public class Lookup {51private static final String HOST = "icann.org";52private static final String SKIP = "SKIP";53private static final String CLASS_PATH = System.getProperty(54"test.class.path");5556public static void main(String args[]) throws IOException {57String addr = null;58String ipv4Name = null;59String ipv4Reversed = null;6061if (args.length == 0) {62// called from lookupWithIPv4Prefer63// obtain an IPv4 address from the hostname.64try {65InetAddress ia = InetAddress.getByName(HOST);66addr = ia.getHostAddress();67ia = InetAddress.getByName(addr);68System.out.print(addr + ":" + ia.getHostName());69return;70} catch (UnknownHostException e) {71System.out.print(SKIP);72return;73}74} else if (args.length == 2 && args[0].equals("reverse")) {75// called from reverseWithIPv4Prefer76// Check that IPv4 address can be resolved to host77// with -Djava.net.preferIPv4Stack=true78try {79InetAddress ia = InetAddress.getByName(args[1]);80addr = ia.getHostAddress();81ipv4Reversed = ia.getHostName();82System.out.print(addr + ":" + ipv4Reversed);83return;84} catch (UnknownHostException e) {85System.out.print(SKIP);86return;87}88} else if (args.length != 1 || !args[0].equals("root")) {89throw new IllegalArgumentException(Stream.of(args).collect(Collectors.joining(" ")));90}9192// spawn a subprocess to obtain the IPv4 address93String tmp = lookupWithIPv4Prefer();94System.out.println("IPv4 lookup results: [" + tmp + "]");95if (SKIP.equals(tmp)) {96System.out.println(HOST + " can't be resolved - test skipped.");97return;98}99100String[] strs = tmp.split(":");101addr = strs[0];102ipv4Name = strs[1];103104// check that the a reverse lookup of the IPv4 address105// will succeed with the IPv4 only stack106tmp = reverseWithIPv4Prefer(addr);107System.out.println("IPv4 reverse lookup results: [" + tmp + "]");108if (SKIP.equals(tmp)) {109System.out.println(addr + " can't be resolved with preferIPv4 - test skipped.");110return;111}112113strs = tmp.split(":");114ipv4Reversed = strs[1];115116// Now check that a reverse lookup will succeed with the dual stack.117InetAddress ia = InetAddress.getByName(addr);118String name = ia.getHostName();119120System.out.println("(default) " + addr + "--> " + name121+ " (reversed IPv4: " + ipv4Reversed + ")");122if (!ipv4Name.equals(name)) {123// adding some diagnosting124System.err.println("name=" + name + " doesn't match expected=" + ipv4Name);125System.err.println("Listing all adresses:");126for (InetAddress any : InetAddress.getAllByName(HOST)) {127System.err.println("\t[" + any + "] address=" + any.getHostAddress()128+ ", host=" + any.getHostName());129}130// make the test fail...131throw new RuntimeException("Mismatch between default"132+ " and java.net.preferIPv4Stack=true results");133}134}135136static String lookupWithIPv4Prefer() throws IOException {137String java = JDKToolFinder.getTestJDKTool("java");138String testClz = Lookup.class.getName();139List<String> cmd = List.of(java, "-Djava.net.preferIPv4Stack=true",140"-cp", CLASS_PATH, testClz);141System.out.println("Executing: " + cmd);142return new OutputAnalyzer(new ProcessBuilder(cmd).start()).getOutput();143}144145static String reverseWithIPv4Prefer(String addr) throws IOException {146String java = JDKToolFinder.getTestJDKTool("java");147String testClz = Lookup.class.getName();148List<String> cmd = List.of(java, "-Djava.net.preferIPv4Stack=true",149"-cp", CLASS_PATH, testClz, "reverse", addr);150System.out.println("Executing: " + cmd);151return new OutputAnalyzer(new ProcessBuilder(cmd).start()).getOutput();152}153}154155156