1 /*
   2  * Copyright (c) 2003, 2015, 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 /* @test
  25  * @bug 4932074
  26  * @summary Test that startListening(Map) method of the com.sun.jdi.SocketListen
  27  *          Connector returns an address that is usable for the address option on
  28  *          remove debuggees.
  29  */
  30 import java.net.InetAddress;
  31 import java.net.Inet4Address;
  32 import java.net.NetworkInterface;
  33 import java.io.IOException;
  34 import java.util.*;
  35 import com.sun.jdi.*;
  36 import com.sun.jdi.connect.*;
  37 
  38 public class ListenAddress {
  39 
  40     /*
  41      * Find Connector by name
  42      */
  43     private static Connector findConnector(String name) {
  44         List connectors = Bootstrap.virtualMachineManager().allConnectors();
  45         Iterator iter = connectors.iterator();
  46         while (iter.hasNext()) {
  47             Connector connector = (Connector)iter.next();
  48             if (connector.name().equals(name)) {
  49                 return connector;
  50             }
  51         }
  52         return null;
  53     }
  54 
  55     /*
  56      * Failure count
  57      */
  58     static int failures = 0;
  59 
  60     /*
  61      * Start listening with the specified Connector and check that the
  62      * returned address includes a host component. If 'addr' is not null
  63      * then set the localAddress argument to be the address.
  64      */
  65     private static void check(ListeningConnector connector, InetAddress addr)
  66         throws IOException, IllegalConnectorArgumentsException
  67     {
  68         Map args = connector.defaultArguments();
  69         if (addr != null) {
  70             Connector.StringArgument addr_arg =
  71               (Connector.StringArgument)args.get("localAddress");
  72             addr_arg.setValue(addr.getHostAddress());
  73         }
  74 
  75         String address = connector.startListening(args);
  76         if (address.indexOf(':') < 0) {
  77             System.out.println(address + " => Failed - no host component!");
  78             failures++;
  79         } else {
  80             System.out.println(address);
  81         }
  82         connector.stopListening(args);
  83     }
  84 
  85     public static void main(String args[]) throws Exception {
  86         ListeningConnector connector = (ListeningConnector)findConnector("com.sun.jdi.SocketListen");
  87 
  88         // check wildcard address
  89         check(connector, (InetAddress)null);
  90 
  91         // iterate over all IPv4 addresses and check that binding to
  92         // that address results in the correct result from startListening(Map)
  93         Enumeration nifs = NetworkInterface.getNetworkInterfaces();
  94         while (nifs.hasMoreElements()) {
  95             NetworkInterface ni = (NetworkInterface)nifs.nextElement();
  96             Enumeration addrs = ni.getInetAddresses();
  97             while (addrs.hasMoreElements()) {
  98                 InetAddress addr = (InetAddress)addrs.nextElement();
  99 
 100                 // JPDA implementation only currently supports IPv4
 101                 if (!(addr instanceof Inet4Address)) {
 102                     continue;
 103                 }
 104 
 105                 check(connector, addr);
 106             }
 107         }
 108 
 109         if (failures > 0) {
 110             throw new RuntimeException(failures + " test(s) failed - see output for details.");
 111         }
 112     }
 113 }