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