jdk/test/com/sun/jdi/BadHandshakeTest.java
author sjiang
Fri, 17 Jan 2014 15:58:18 +0100
changeset 22295 f140aab86aed
parent 21660 3de47f8320ef
child 24509 5d02a6d25f78
permissions -rw-r--r--
8029378: com/sun/jdi/BadHandshakeTest.java failed with java.util.concurrent.TimeoutException Reviewed-by: dholmes, jbachorik
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22295
f140aab86aed 8029378: com/sun/jdi/BadHandshakeTest.java failed with java.util.concurrent.TimeoutException
sjiang
parents: 21660
diff changeset
     2
 * Copyright (c) 2005, 2014, 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: 4342
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4342
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4342
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
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 2
diff changeset
    25
 * @bug 6306165 6432567
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Check that a bad handshake doesn't cause a debuggee to abort
21660
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    27
 * @library /lib/testlibrary
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @build VMConnection BadHandshakeTest Exit0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main BadHandshakeTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jdi.Bootstrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.connect.Connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.jdi.connect.AttachingConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Iterator;
21660
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    43
import java.util.concurrent.TimeUnit;
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    44
import java.util.concurrent.atomic.AtomicBoolean;
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    45
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    46
import jdk.testlibrary.Utils;
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    47
import jdk.testlibrary.ProcessTools;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class BadHandshakeTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Find a connector by name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static Connector findConnector(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        List connectors = Bootstrap.virtualMachineManager().allConnectors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Iterator iter = connectors.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            Connector connector = (Connector)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            if (connector.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                return connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Launch a server debuggee with the given address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
21660
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    68
    private static Process launch(String address, String class_name) throws Exception {
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    69
        String[] args = VMConnection.insertDebuggeeVMOptions(new String[] {
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    70
            "-agentlib:jdwp=transport=dt_socket" +
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    71
            ",server=y" + ",suspend=y" + ",address=" + address,
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    72
            class_name
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    73
        });
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    74
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    75
        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
21660
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    77
        final AtomicBoolean success = new AtomicBoolean();
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    78
        Process p = ProcessTools.startProcess(
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    79
            class_name,
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    80
            pb,
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    81
            (line) -> {
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    82
                // The first thing that will get read is
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    83
                //    Listening for transport dt_socket at address: xxxxx
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    84
                // which shows the debuggee is ready to accept connections.
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    85
                success.set(line.contains("Listening for transport dt_socket at address:"));
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    86
                return true;
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    87
            },
22295
f140aab86aed 8029378: com/sun/jdi/BadHandshakeTest.java failed with java.util.concurrent.TimeoutException
sjiang
parents: 21660
diff changeset
    88
            Integer.MAX_VALUE,
21660
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    89
            TimeUnit.MILLISECONDS
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    90
        );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
21660
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
    92
        return success.get() ? p : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * - pick a TCP port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * - Launch a server debuggee: server=y,suspend=y,address=${port}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * - run it to VM death
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * - verify we saw no error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static void main(String args[]) throws Exception {
21660
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
   102
        int port = Utils.getFreePort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        String address = String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // launch the server debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Process process = launch(address, "Exit0");
21660
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
   108
        if (process == null) {
3de47f8320ef 8004126: TEST_BUG: com/sun/jdi/BadHandshakeTest.java fails intermittently
jbachorik
parents: 20201
diff changeset
   109
            throw new RuntimeException("Unable to start debugee");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // Connect to the debuggee and handshake with garbage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        Socket s = new Socket(InetAddress.getLocalHost(), port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        s.getOutputStream().write("Here's a poke in the eye".getBytes("UTF-8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // Re-connect and to a partial handshake - don't disconnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        s = new Socket(InetAddress.getLocalHost(), port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        s.getOutputStream().write("JDWP-".getBytes("UTF-8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // attach to server debuggee and resume it so it can exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Map conn_args = conn.defaultArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        Connector.IntegerArgument port_arg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            (Connector.IntegerArgument)conn_args.get("port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        port_arg.setValue(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        VirtualMachine vm = conn.attach(conn_args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // The first event is always a VMStartEvent, and it is always in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // an EventSet by itself.  Wait for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        EventSet evtSet = vm.eventQueue().remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        for (Event event: evtSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (event instanceof VMStartEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new RuntimeException("Test failed - debuggee did not start properly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        vm.eventRequestManager().deleteAllBreakpoints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        vm.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        process.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}