1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Lookup/reverse lookup class for regression test 4773521
* @test
* @bug 4773521
* @summary Test that reverse lookups of IPv4 addresses work when IPv6
* is enabled
* @library /test/lib
* @build jdk.test.lib.Utils
* jdk.test.lib.Asserts
* jdk.test.lib.JDKToolFinder
* jdk.test.lib.JDKToolLauncher
* jdk.test.lib.Platform
* jdk.test.lib.process.*
* Lookup
* @run main Lookup root
*
*/
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.stream.Stream;
import java.util.stream.Collectors;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
public class Lookup {
private static final String HOST = "icann.org";
private static final String SKIP = "SKIP";
public static void main(String args[]) throws IOException {
String addr = null;
String ipv4Name = null;
String ipv4Reversed = null;
if (args.length == 0) {
// called from lookupWithIPv4Prefer
// obtain an IPv4 address from the hostname.
try {
InetAddress ia = InetAddress.getByName(HOST);
addr = ia.getHostAddress();
ia = InetAddress.getByName(addr);
System.out.print(addr + ":" + ia.getHostName());
return;
} catch (UnknownHostException e) {
System.out.print(SKIP);
return;
}
} else if (args.length == 2 && args[0].equals("reverse")) {
// called from reverseWithIPv4Prefer
// Check that IPv4 address can be resolved to host
// with -Djava.net.preferIPv4Stack=true
try {
InetAddress ia = InetAddress.getByName(args[1]);
addr = ia.getHostAddress();
ipv4Reversed = ia.getHostName();
System.out.print(addr + ":" + ipv4Reversed);
return;
} catch (UnknownHostException e) {
System.out.print(SKIP);
return;
}
} else if (args.length != 1 || !args[0].equals("root")) {
throw new IllegalArgumentException(Stream.of(args).collect(Collectors.joining(" ")));
}
// spawn a subprocess to obtain the IPv4 address
String tmp = lookupWithIPv4Prefer();
System.out.println("IPv4 lookup results: [" + tmp + "]");
if (SKIP.equals(tmp)) {
System.out.println(HOST + " can't be resolved - test skipped.");
return;
}
String[] strs = tmp.split(":");
addr = strs[0];
ipv4Name = strs[1];
// check that the reverse lookup of the IPv4 address
// will succeed with the IPv4 only stack
tmp = reverseWithIPv4Prefer(addr);
System.out.println("IPv4 reverse lookup results: [" + tmp + "]");
if (SKIP.equals(tmp)) {
System.out.println(addr + " can't be resolved with preferIPv4 - test skipped.");
return;
}
strs = tmp.split(":");
ipv4Reversed = strs[1];
// Now check that a reverse lookup will succeed with the dual stack.
InetAddress ia = InetAddress.getByName(addr);
String name = ia.getHostName();
// output details of dual stack lookup by address
System.out.println("dual stack lookup for addr " + addr + " returned IP address " + ia);
System.out.println(" with hostname " + name);
System.out.println("(default) " + addr + "--> " + name
+ " (reversed IPv4: " + ipv4Reversed + ")");
if (!ipv4Name.equals(name)) {
// adding some diagnosting
System.err.println("name=" + name + " doesn't match expected=" + ipv4Name);
System.err.println("Listing all adresses:");
for (InetAddress any : InetAddress.getAllByName(HOST)) {
System.err.println("\t[" + any + "] address=" + any.getHostAddress()
+ ", host=" + any.getHostName());
}
// make the test fail...
throw new RuntimeException("Mismatch between default"
+ " and java.net.preferIPv4Stack=true results");
}
}
static String lookupWithIPv4Prefer() throws IOException {
String testClz = Lookup.class.getName();
ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(
"-Djava.net.preferIPv4Stack=true", testClz);
return new OutputAnalyzer(pb.start()).getOutput();
}
static String reverseWithIPv4Prefer(String addr) throws IOException {
String testClz = Lookup.class.getName();
ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(
"-Djava.net.preferIPv4Stack=true", testClz, "reverse", addr);
return new OutputAnalyzer(pb.start()).getOutput();
}
}
|