1 /*
  2  * Copyright (c) 2002, 2024, 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 /*
 25  * Lookup/reverse lookup class for regression test 4773521
 26  * @test
 27  * @bug 4773521
 28  * @summary Test that reverse lookups of IPv4 addresses work when IPv6
 29  *          is enabled
 30  * @library /test/lib
 31  * @build jdk.test.lib.Utils
 32  *        jdk.test.lib.Asserts
 33  *        jdk.test.lib.JDKToolFinder
 34  *        jdk.test.lib.JDKToolLauncher
 35  *        jdk.test.lib.Platform
 36  *        jdk.test.lib.process.*
 37  *        Lookup
 38  * @run main Lookup root
 39  *
 40  */
 41 import java.io.IOException;
 42 import java.net.InetAddress;
 43 import java.net.UnknownHostException;
 44 import java.util.stream.Stream;
 45 import java.util.stream.Collectors;
 46 
 47 import jdk.test.lib.process.OutputAnalyzer;
 48 import jdk.test.lib.process.ProcessTools;
 49 
 50 public class Lookup {
 51     private static final String HOST = "icann.org";
 52     private static final String SKIP = "SKIP";
 53 
 54     public static void main(String args[]) throws IOException {
 55         String addr = null;
 56         String ipv4Name = null;
 57         String ipv4Reversed = null;
 58 
 59         if (args.length == 0) {
 60             // called from lookupWithIPv4Prefer
 61             // obtain an IPv4 address from the hostname.
 62             try {
 63                 InetAddress ia = InetAddress.getByName(HOST);
 64                 addr = ia.getHostAddress();
 65                 ia = InetAddress.getByName(addr);
 66                 System.out.print(addr + ":" + ia.getHostName());
 67                 return;
 68             } catch (UnknownHostException e) {
 69                 System.out.print(SKIP);
 70                 return;
 71             }
 72         } else if (args.length == 2 && args[0].equals("reverse")) {
 73             // called from reverseWithIPv4Prefer
 74             // Check that IPv4 address can be resolved to host
 75             // with -Djava.net.preferIPv4Stack=true
 76             try {
 77                 InetAddress ia = InetAddress.getByName(args[1]);
 78                 addr = ia.getHostAddress();
 79                 ipv4Reversed = ia.getHostName();
 80                 System.out.print(addr + ":" + ipv4Reversed);
 81                 return;
 82             } catch (UnknownHostException e) {
 83                 System.out.print(SKIP);
 84                 return;
 85             }
 86         } else if (args.length != 1 || !args[0].equals("root")) {
 87             throw new IllegalArgumentException(Stream.of(args).collect(Collectors.joining(" ")));
 88         }
 89 
 90         // spawn a subprocess to obtain the IPv4 address
 91         String tmp = lookupWithIPv4Prefer();
 92         System.out.println("IPv4 lookup results: [" + tmp + "]");
 93         if (SKIP.equals(tmp)) {
 94             System.out.println(HOST + " can't be resolved - test skipped.");
 95             return;
 96         }
 97 
 98         String[] strs = tmp.split(":");
 99         addr = strs[0];
100         ipv4Name = strs[1];
101 
102         // check that the reverse lookup of the IPv4 address
103         // will succeed with the IPv4 only stack
104         tmp = reverseWithIPv4Prefer(addr);
105         System.out.println("IPv4 reverse lookup results: [" + tmp + "]");
106         if (SKIP.equals(tmp)) {
107             System.out.println(addr + " can't be resolved with preferIPv4 - test skipped.");
108             return;
109         }
110 
111         strs = tmp.split(":");
112         ipv4Reversed = strs[1];
113 
114         // Now check that a reverse lookup will succeed with the dual stack.
115         InetAddress ia = InetAddress.getByName(addr);
116         String name = ia.getHostName();
117 
118         System.out.println("(default) " + addr + "--> " + name
119                                + " (reversed IPv4: " + ipv4Reversed + ")");
120         if (!ipv4Name.equals(name)) {
121             // adding some diagnosting
122             System.err.println("name=" + name + " doesn't match expected=" + ipv4Name);
123             System.err.println("Listing all adresses:");
124             for (InetAddress any : InetAddress.getAllByName(HOST)) {
125                 System.err.println("\t[" + any + "] address=" + any.getHostAddress()
126                                    + ", host=" + any.getHostName());
127             }
128             // make the test fail...
129             throw new RuntimeException("Mismatch between default"
130                     + " and java.net.preferIPv4Stack=true results");
131         }
132     }
133 
134     static String lookupWithIPv4Prefer() throws IOException {
135         String testClz = Lookup.class.getName();
136         ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(
137                 "-Djava.net.preferIPv4Stack=true", testClz);
138         return new OutputAnalyzer(pb.start()).getOutput();
139     }
140 
141     static String reverseWithIPv4Prefer(String addr) throws IOException {
142         String testClz = Lookup.class.getName();
143         ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(
144                 "-Djava.net.preferIPv4Stack=true", testClz, "reverse", addr);
145         return new OutputAnalyzer(pb.start()).getOutput();
146     }
147 }