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