jdk/test/com/sun/jdi/ListenAddress.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4932074
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test that startListening(Map) method of the com.sun.jdi.SocketListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          Connector returns an address that is usable for the address option on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          remove debuggees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.Inet4Address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.connect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class ListenAddress {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Find Connector by name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static Connector findConnector(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        List connectors = Bootstrap.virtualMachineManager().allConnectors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        Iterator iter = connectors.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            Connector connector = (Connector)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            if (connector.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                return connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Failure count
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Start listening with the specified Connector and check that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * returned address includes a host component. If 'addr' is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * then set the localAddress argument to be the address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static void check(ListeningConnector connector, InetAddress addr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        throws IOException, IllegalConnectorArgumentsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Map args = connector.defaultArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (addr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            Connector.StringArgument addr_arg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
              (Connector.StringArgument)args.get("localAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            addr_arg.setValue(addr.getHostAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        String address = connector.startListening(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (address.indexOf(':') < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            System.out.println(address + " => Failed - no host component!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.out.println(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        connector.stopListening(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        ListeningConnector connector = (ListeningConnector)findConnector("com.sun.jdi.SocketListen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // check wildcard address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        check(connector, (InetAddress)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // iterate over all IPv4 addresses and check that binding to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // that address results in the correct result from startListening(Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Enumeration nifs = NetworkInterface.getNetworkInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        while (nifs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            NetworkInterface ni = (NetworkInterface)nifs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            Enumeration addrs = ni.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            while (addrs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                InetAddress addr = (InetAddress)addrs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                // JPDA implementation only currently supports IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                if (!(addr instanceof Inet4Address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                check(connector, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new RuntimeException(failures + " test(s) failed - see output for details.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
}