1 /* 2 * Copyright (c) 2002, 2016, 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 46 import jdk.test.lib.JDKToolFinder; 47 import jdk.test.lib.process.OutputAnalyzer; 48 49 public class Lookup { 50 private static final String HOST = "icann.org"; 51 private static final String SKIP = "SKIP"; 52 private static final String CLASS_PATH = System.getProperty( 53 "test.class.path"); 54 55 public static void main(String args[]) throws IOException { 56 String addr = null; 57 String ipv4Name = null; 58 if (args.length == 0) { 59 // First check that host resolves to IPv4 address 60 try { 61 InetAddress ia = InetAddress.getByName(HOST); 62 addr = ia.getHostAddress(); 63 } catch (UnknownHostException e) { 64 System.out.print(SKIP); 65 return; 66 } 67 } else { 68 String tmp = lookupWithIPv4Prefer(); 69 System.out.println("IPv4 lookup results: [" + tmp + "]"); 70 if (SKIP.equals(tmp)) { 71 System.out.println(HOST + " can't be resolved - test skipped."); 72 return; 73 } 74 75 String[] strs = tmp.split(":"); 76 addr = strs[0]; 77 ipv4Name = strs[1]; 78 } 79 80 // reverse lookup 81 InetAddress ia = InetAddress.getByName(addr); 82 String name = ia.getHostName(); 83 if (args.length == 0) { 84 System.out.print(addr + ":" + name); 85 return; 86 } else { 87 System.out.println("(default) " + addr + "--> " + name); 88 if (!ipv4Name.equals(name)) { 89 throw new RuntimeException("Mismatch between default" 90 + " and java.net.preferIPv4Stack=true results"); 91 } 92 } 93 } 94 95 static String lookupWithIPv4Prefer() throws IOException { 96 String java = JDKToolFinder.getTestJDKTool("java"); 97 String testClz = Lookup.class.getName(); 98 List<String> cmd = List.of(java, "-Djava.net.preferIPv4Stack=true", 99 "-cp", CLASS_PATH, testClz); 100 System.out.println("Executing: " + cmd); 101 return new OutputAnalyzer(new ProcessBuilder(cmd).start()).getOutput(); 102 } 103 } 104