test/jdk/com/sun/jdi/JdwpListenTest.java
changeset 58876 1a8d65e71a66
parent 54884 8a6093c186a6
child 59066 439a147b2c0c
equal deleted inserted replaced
58875:8c0e8cff877f 58876:1a8d65e71a66
    32 import java.net.Inet4Address;
    32 import java.net.Inet4Address;
    33 import java.net.Inet6Address;
    33 import java.net.Inet6Address;
    34 import java.net.InetAddress;
    34 import java.net.InetAddress;
    35 import java.net.NetworkInterface;
    35 import java.net.NetworkInterface;
    36 import java.net.SocketException;
    36 import java.net.SocketException;
       
    37 import java.net.UnknownHostException;
    37 import java.util.Enumeration;
    38 import java.util.Enumeration;
    38 import java.util.Iterator;
    39 import java.util.Iterator;
    39 import java.util.LinkedList;
    40 import java.util.LinkedList;
    40 import java.util.List;
    41 import java.util.List;
    41 import java.util.Map;
    42 import java.util.Map;
    48  *
    49  *
    49  * @build HelloWorld JdwpListenTest
    50  * @build HelloWorld JdwpListenTest
    50  * @run main/othervm JdwpListenTest
    51  * @run main/othervm JdwpListenTest
    51  */
    52  */
    52 public class JdwpListenTest {
    53 public class JdwpListenTest {
       
    54 
       
    55     private static final boolean IsWindows = System.getProperty("os.name").toLowerCase().contains("windows");
    53 
    56 
    54     public static void main(String[] args) throws Exception {
    57     public static void main(String[] args) throws Exception {
    55         List<InetAddress> addresses = getAddresses();
    58         List<InetAddress> addresses = getAddresses();
    56 
    59 
    57         boolean ipv4EnclosedTested = false;
    60         boolean ipv4EnclosedTested = false;
    58         boolean ipv6EnclosedTested = false;
    61         boolean ipv6EnclosedTested = false;
    59         for (InetAddress listen: addresses) {
    62         for (InetAddress listen: addresses) {
    60             for (InetAddress attach: addresses) {
    63             for (InetAddress attach: addresses) {
    61                 // can connect only from the same address
    64                 // can connect only from the same address
    62                 // IPv6 cannot connect to IPv4 (::1 to 127.0.0.1) and vice versa.
    65                 // IPv6 cannot connect to IPv4 (::1 to 127.0.0.1) and vice versa.
       
    66                 // Note: for IPv6 addresses equals() does not compare scopes
       
    67                 // (so addresses with symbolic and numeric scopes are equals).
    63                 listenTest(listen.getHostAddress(), attach.getHostAddress(), attach.equals(listen));
    68                 listenTest(listen.getHostAddress(), attach.getHostAddress(), attach.equals(listen));
    64             }
    69             }
    65             // test that addresses enclosed in square brackets are supported.
    70             // test that addresses enclosed in square brackets are supported.
    66             if (listen instanceof Inet4Address && !ipv4EnclosedTested) {
    71             if (listen instanceof Inet4Address && !ipv4EnclosedTested) {
    67                 listenTest("[" + listen.getHostAddress() + "]", "[" + listen.getHostAddress() + "]", true);
    72                 listenTest("[" + listen.getHostAddress() + "]", "[" + listen.getHostAddress() + "]", true);
    78         }
    83         }
    79     }
    84     }
    80 
    85 
    81     private static void listenTest(String listenAddress, String connectAddress, boolean expectedResult)
    86     private static void listenTest(String listenAddress, String connectAddress, boolean expectedResult)
    82             throws IOException {
    87             throws IOException {
       
    88         log("\nTest: listen at " + listenAddress + ", attaching from " + connectAddress
       
    89                 + ", expected: " + (expectedResult ? "SUCCESS" : "FAILURE"));
    83         log("Starting listening debuggee at " + listenAddress);
    90         log("Starting listening debuggee at " + listenAddress);
    84         try (Debuggee debuggee = Debuggee.launcher("HelloWorld").setAddress(listenAddress + ":0").launch()) {
    91         try (Debuggee debuggee = Debuggee.launcher("HelloWorld").setAddress(listenAddress + ":0").launch()) {
    85             log("Debuggee is listening on " + listenAddress + ":" + debuggee.getAddress());
    92             log("Debuggee is listening on " + listenAddress + ":" + debuggee.getAddress());
    86             log("Connecting from " + connectAddress + ", expected: " + (expectedResult ? "SUCCESS" : "FAILURE"));
    93             log("Connecting from " + connectAddress + ", expected: " + (expectedResult ? "SUCCESS" : "FAILURE"));
    87             try {
    94             try {
    96                 }
   103                 }
    97             }
   104             }
    98         }
   105         }
    99     }
   106     }
   100 
   107 
       
   108     private static void addAddr(List<InetAddress> list, InetAddress addr) {
       
   109         log(" - (" + addr.getClass().getSimpleName() + ") " + addr.getHostAddress());
       
   110         list.add(addr);
       
   111     }
       
   112 
   101     private static List<InetAddress> getAddresses() {
   113     private static List<InetAddress> getAddresses() {
   102         List<InetAddress> result = new LinkedList<>();
   114         List<InetAddress> result = new LinkedList<>();
   103         try {
   115         try {
   104             Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
   116             Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
   105             while (networkInterfaces.hasMoreElements()) {
   117             while (networkInterfaces.hasMoreElements()) {
   107                 try {
   119                 try {
   108                     if (iface.isUp()) {
   120                     if (iface.isUp()) {
   109                         Enumeration<InetAddress> addresses = iface.getInetAddresses();
   121                         Enumeration<InetAddress> addresses = iface.getInetAddresses();
   110                         while (addresses.hasMoreElements()) {
   122                         while (addresses.hasMoreElements()) {
   111                             InetAddress addr = addresses.nextElement();
   123                             InetAddress addr = addresses.nextElement();
   112                             // Java reports link local addresses with named scope,
   124                             // Java reports link local addresses with symbolic scope,
   113                             // but Windows sockets routines support only numeric scope id.
   125                             // but on Windows java.net.NetworkInterface generates its own scope names
   114                             // skip such addresses.
   126                             // which are incompatible with native Windows routines.
       
   127                             // So on Windows test only addresses with numeric scope.
       
   128                             // On other platforms test both symbolic and numeric scopes.
   115                             if (addr instanceof Inet6Address) {
   129                             if (addr instanceof Inet6Address) {
   116                                 Inet6Address addr6 = (Inet6Address)addr;
   130                                 Inet6Address addr6 = (Inet6Address)addr;
   117                                 if (addr6.getScopedInterface() != null) {
   131                                 NetworkInterface scopeIface = addr6.getScopedInterface();
   118                                     continue;
   132                                 if (scopeIface != null && scopeIface.getName() != null) {
       
   133                                     // On some test machines VPN creates link local addresses
       
   134                                     // which we cannot connect to.
       
   135                                     // Skip them.
       
   136                                     if (scopeIface.isPointToPoint()) {
       
   137                                         continue;
       
   138                                     }
       
   139 
       
   140                                     try {
       
   141                                         // the same address with numeric scope
       
   142                                         addAddr(result, Inet6Address.getByAddress(null, addr6.getAddress(), addr6.getScopeId()));
       
   143                                     } catch (UnknownHostException e) {
       
   144                                         // cannot happen!
       
   145                                         throw new RuntimeException("Unexpected", e);
       
   146                                     }
       
   147 
       
   148                                     if (IsWindows) {
       
   149                                         // don't add addresses with symbolic scope
       
   150                                         continue;
       
   151                                     }
   119                                 }
   152                                 }
   120                             }
   153                             }
   121                             log(" - (" + addr.getClass().getSimpleName() + ") " + addr.getHostAddress());
   154                             addAddr(result, addr);
   122                             result.add(addr);
       
   123                         }
   155                         }
   124                     }
   156                     }
   125                 } catch (SocketException e) {
   157                 } catch (SocketException e) {
   126                     log("Interface " + iface.getDisplayName() + ": failed to get addresses");
   158                     log("Interface " + iface.getDisplayName() + ": failed to get addresses");
   127                 }
   159                 }