test/jdk/java/net/InetAddress/ptr/Lookup.java
changeset 58378 98ea90226198
parent 47216 71c04702a3d5
equal deleted inserted replaced
58377:975f2d30f6b3 58378:98ea90226198
     1 /*
     1 /*
     2  * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    40  */
    40  */
    41 import java.io.IOException;
    41 import java.io.IOException;
    42 import java.net.InetAddress;
    42 import java.net.InetAddress;
    43 import java.net.UnknownHostException;
    43 import java.net.UnknownHostException;
    44 import java.util.List;
    44 import java.util.List;
       
    45 import java.util.stream.Stream;
       
    46 import java.util.stream.Collectors;
    45 
    47 
    46 import jdk.test.lib.JDKToolFinder;
    48 import jdk.test.lib.JDKToolFinder;
    47 import jdk.test.lib.process.OutputAnalyzer;
    49 import jdk.test.lib.process.OutputAnalyzer;
    48 
    50 
    49 public class Lookup {
    51 public class Lookup {
    53             "test.class.path");
    55             "test.class.path");
    54 
    56 
    55     public static void main(String args[]) throws IOException {
    57     public static void main(String args[]) throws IOException {
    56         String addr = null;
    58         String addr = null;
    57         String ipv4Name = null;
    59         String ipv4Name = null;
       
    60         String ipv4Reversed = null;
       
    61 
    58         if (args.length == 0) {
    62         if (args.length == 0) {
    59             // First check that host resolves to IPv4 address
    63             // called from lookupWithIPv4Prefer
       
    64             // obtain an IPv4 address from the hostname.
    60             try {
    65             try {
    61                 InetAddress ia = InetAddress.getByName(HOST);
    66                 InetAddress ia = InetAddress.getByName(HOST);
    62                 addr = ia.getHostAddress();
    67                 addr = ia.getHostAddress();
       
    68                 ia = InetAddress.getByName(addr);
       
    69                 System.out.print(addr + ":" + ia.getHostName());
       
    70                 return;
    63             } catch (UnknownHostException e) {
    71             } catch (UnknownHostException e) {
    64                 System.out.print(SKIP);
    72                 System.out.print(SKIP);
    65                 return;
    73                 return;
    66             }
    74             }
    67         } else {
    75         } else if (args.length == 2 && args[0].equals("reverse")) {
    68             String tmp = lookupWithIPv4Prefer();
    76             // called from reverseWithIPv4Prefer
    69             System.out.println("IPv4 lookup results: [" + tmp + "]");
    77             // Check that IPv4 address can be resolved to host
    70             if (SKIP.equals(tmp)) {
    78             // with -Djava.net.preferIPv4Stack=true
    71                 System.out.println(HOST + " can't be resolved - test skipped.");
    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);
    72                 return;
    87                 return;
    73             }
    88             }
    74 
    89         } else if (args.length != 1 || !args[0].equals("root")) {
    75             String[] strs = tmp.split(":");
    90             throw new IllegalArgumentException(Stream.of(args).collect(Collectors.joining(" ")));
    76             addr = strs[0];
       
    77             ipv4Name = strs[1];
       
    78         }
    91         }
    79 
    92 
    80         // reverse lookup
    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 a 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.
    81         InetAddress ia = InetAddress.getByName(addr);
   118         InetAddress ia = InetAddress.getByName(addr);
    82         String name = ia.getHostName();
   119         String name = ia.getHostName();
    83         if (args.length == 0) {
   120 
    84             System.out.print(addr + ":" + name);
   121         System.out.println("(default) " + addr + "--> " + name
    85             return;
   122                                + " (reversed IPv4: " + ipv4Reversed + ")");
    86         } else {
   123         if (!ipv4Name.equals(name)) {
    87             System.out.println("(default) " + addr + "--> " + name);
   124             // adding some diagnosting
    88             if (!ipv4Name.equals(name)) {
   125             System.err.println("name=" + name + " doesn't match expected=" + ipv4Name);
    89                 throw new RuntimeException("Mismatch between default"
   126             System.err.println("Listing all adresses:");
    90                         + " and java.net.preferIPv4Stack=true results");
   127             for (InetAddress any : InetAddress.getAllByName(HOST)) {
       
   128                 System.err.println("\t[" + any + "] address=" + any.getHostAddress()
       
   129                                    + ", host=" + any.getHostName());
    91             }
   130             }
       
   131             // make the test fail...
       
   132             throw new RuntimeException("Mismatch between default"
       
   133                     + " and java.net.preferIPv4Stack=true results");
    92         }
   134         }
    93     }
   135     }
    94 
   136 
    95     static String lookupWithIPv4Prefer() throws IOException {
   137     static String lookupWithIPv4Prefer() throws IOException {
    96         String java = JDKToolFinder.getTestJDKTool("java");
   138         String java = JDKToolFinder.getTestJDKTool("java");
    98         List<String> cmd = List.of(java, "-Djava.net.preferIPv4Stack=true",
   140         List<String> cmd = List.of(java, "-Djava.net.preferIPv4Stack=true",
    99                 "-cp", CLASS_PATH, testClz);
   141                 "-cp", CLASS_PATH, testClz);
   100         System.out.println("Executing: " + cmd);
   142         System.out.println("Executing: " + cmd);
   101         return new OutputAnalyzer(new ProcessBuilder(cmd).start()).getOutput();
   143         return new OutputAnalyzer(new ProcessBuilder(cmd).start()).getOutput();
   102     }
   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     }
   103 }
   154 }
   104