test/hotspot/jtreg/vmTestbase/nsk/jdi/ListeningConnector/startListening/startlis001.java
changeset 54884 8a6093c186a6
parent 50018 3ba0d8631f24
--- a/test/hotspot/jtreg/vmTestbase/nsk/jdi/ListeningConnector/startListening/startlis001.java	Fri May 10 17:13:02 2019 -0700
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jdi/ListeningConnector/startListening/startlis001.java	Wed May 15 11:06:33 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -32,9 +32,12 @@
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 
+import java.util.Arrays;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import nsk.share.*;
 import nsk.share.jpda.*;
@@ -85,7 +88,6 @@
     private int runIt(String argv[], PrintStream out) {
         String port;
         String addr;
-        InetAddress inetAddr = null;
         ArgumentHandler argHandler = new ArgumentHandler(argv);
 
 // pass if CONNECTOR_NAME is not implemented
@@ -97,34 +99,40 @@
 
         long timeout = argHandler.getWaitTime() * 60 * 1000;
 
-/* Check that listening address returned by ListeningConnector.startListening()
-   matches the address which was set via connector's arguments */
+        /* Check that listening address returned by ListeningConnector.startListening()
+         * matches the address which was set via connector's arguments.
+         * Empty host address causes listening for local connections only (loopback interface).
+         * */
+        String hostname = "localhost";
+        List<String> validAddresses = new LinkedList<>();
+        validAddresses.add(hostname);
         try {
-            inetAddr = InetAddress.getLocalHost();
+            Arrays.stream(InetAddress.getAllByName(hostname))
+                    .forEach(address -> validAddresses.add(address.getHostAddress()));
         } catch (UnknownHostException e) {
             log.complain("FAILURE: caught UnknownHostException " +
-                e.getMessage());
+                    e.getMessage());
             totalRes = false;
         }
-        String hostname = inetAddr.getHostName();
-        String ip = inetAddr.getHostAddress();
+
         port = argHandler.getTransportPortIfNotDynamic();
 
         initConnector(port);
         if ((addr = startListen()) == null) {
             log.complain("Test case #1 FAILED: unable to start listening");
             totalRes = false;
-        }
-        else {
+        } else {
+            String validAddrList = validAddresses.stream()
+                    .map(value -> value + ":" + port)
+                    .collect(Collectors.joining(" or "));
             log.display("Test case #1: start listening the address " + addr);
-            log.display("Expected address: "+ hostname + ":" + port +
-                "\n\tor "+ ip + ":" + port);
-            if ( (!addr.startsWith(hostname) && !addr.startsWith(ip)) ||
-                 (port != null && !addr.endsWith(port)) ) {
+            log.display("Expected addresses: " + validAddrList);
+            final String listenAddr = addr;
+            boolean isValid = validAddresses.stream()
+                    .anyMatch(value -> listenAddr.startsWith(value) && (port == null || listenAddr.endsWith(port)));
+            if (!isValid) {
                 log.complain("Test case #1 FAILED: listening address " + addr +
-                    "\ndoes not match expected address:\n" +
-                    hostname + ":" + port + " or " +
-                    ip + ":" + port);
+                    "\ndoes not match expected address:\n" + validAddrList);
                 totalRes = false;
             }
             if (!stopListen()) {
@@ -135,8 +143,8 @@
                log.display("Test case #1 PASSED: listening address matches expected address");
         }
 
-/* Check that an address generated by ListeningConnector.startListening()
-   is valid i.e. debugee VM is accessible via this address */
+        /* Check that an address generated by ListeningConnector.startListening()
+           is valid i.e. debugee VM is accessible via this address */
         initConnector(null);
         if ((addr = startListen()) == null) {
             log.complain("Test case #2 FAILED: unable to start listening");