jdk/test/com/sun/jdi/DoubleAgentTest.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 20201 50cc2d25a60b
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: 4342
diff changeset
     2
 * Copyright (c) 2005, 2006, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 6354345
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Check that a double agent request fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @build VMConnection DoubleAgentTest Exit0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main DoubleAgentTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class DoubleAgentTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static Object locker = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static String outputText = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Helper class to redirect process output/error
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static class IOHandler implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        IOHandler(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            this.in = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        static Thread handle(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            IOHandler handler = new IOHandler(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            Thread thr = new Thread(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            thr.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            return thr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    int n = in.read(b, 0, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    // The first thing that will get read is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    //    Listening for transport dt_socket at address: xxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    // which shows the debuggee is ready to accept connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    synchronized(locker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                        locker.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    String s = new String(b, 0, n, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    System.out.print(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    synchronized(outputText) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        outputText += s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Launch a server debuggee with the given address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static Process launch(String address, String class_name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        String exe =   System.getProperty("java.home")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                     + File.separator + "bin" + File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        String arch = System.getProperty("os.arch");
4342
9f3aa1172803 6905705: Fix broken exit code values in jdk/test/Makefile
ohair
parents: 4335
diff changeset
    95
        String osname = System.getProperty("os.name");
9f3aa1172803 6905705: Fix broken exit code values in jdk/test/Makefile
ohair
parents: 4335
diff changeset
    96
        if (osname.equals("SunOS") && arch.equals("sparcv9")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            exe += "sparcv9/java";
4342
9f3aa1172803 6905705: Fix broken exit code values in jdk/test/Makefile
ohair
parents: 4335
diff changeset
    98
        } else if (osname.equals("SunOS") && arch.equals("amd64")) {
4335
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 2
diff changeset
    99
            exe += "amd64/java";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            exe += "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        String jdwpOption = "-agentlib:jdwp=transport=dt_socket"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                         + ",server=y" + ",suspend=y" + ",address=" + address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        String cmd = exe + " " + VMConnection.getDebuggeeVMOptions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                         + " " + jdwpOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                         + " " + jdwpOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                         + " " + class_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.out.println("Starting: " + cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Process p = Runtime.getRuntime().exec(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * - pick a TCP port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * - Launch a server debuggee that should fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * - verify we saw error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // find a free port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        ServerSocket ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int port = ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        String address = String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // launch the server debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Process process = launch(address, "Exit0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Thread t1 = IOHandler.handle(process.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        Thread t2 = IOHandler.handle(process.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // wait for the debugge to be ready
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        synchronized(locker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            locker.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int exitCode = process.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            t1.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            t2.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } catch ( InterruptedException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new Exception("Debuggee failed InterruptedException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if ( outputText.contains("capabilities") ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new Exception(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                "Debuggee failed with ERROR about capabilities: " + outputText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if ( !outputText.contains("ERROR") ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new Exception(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                "Debuggee does not have ERROR in the output: " + outputText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if ( exitCode == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new Exception(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                "Debuggee should have failed with an non-zero exit code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}