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