blob: f76b17c154e193ddc49d68fd74fe1fcf64446d5a [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 6354345
26 * @summary Check that a double agent request fails
27 *
28 * @build VMConnection DoubleAgentTest Exit0
29 * @run main DoubleAgentTest
30 *
31 */
32import java.io.InputStream;
33import java.io.IOException;
34import java.io.File;
35import java.net.ServerSocket;
36import java.net.Socket;
37
38public class DoubleAgentTest {
39
40 static Object locker = new Object();
41 static String outputText = "";
42
43 /*
44 * Helper class to redirect process output/error
45 */
46 static class IOHandler implements Runnable {
47 InputStream in;
48
49 IOHandler(InputStream in) {
50 this.in = in;
51 }
52
53 static Thread handle(InputStream in) {
54 IOHandler handler = new IOHandler(in);
55 Thread thr = new Thread(handler);
56 thr.setDaemon(true);
57 thr.start();
58 return thr;
59 }
60
61 public void run() {
62 try {
63 byte b[] = new byte[100];
64 for (;;) {
65 int n = in.read(b, 0, 100);
66 // The first thing that will get read is
67 // Listening for transport dt_socket at address: xxxxx
68 // which shows the debuggee is ready to accept connections.
69 synchronized(locker) {
70 locker.notify();
71 }
72 if (n < 0) {
73 break;
74 }
75 String s = new String(b, 0, n, "UTF-8");
76 System.out.print(s);
77 synchronized(outputText) {
78 outputText += s;
79 }
80 }
81 } catch (IOException ioe) {
82 ioe.printStackTrace();
83 }
84 }
85
86 }
87
88 /*
89 * Launch a server debuggee with the given address
90 */
91 private static Process launch(String address, String class_name) throws IOException {
92 String exe = System.getProperty("java.home")
93 + File.separator + "bin" + File.separator;
94 String arch = System.getProperty("os.arch");
95 if (arch.equals("sparcv9")) {
96 exe += "sparcv9/java";
ohair2110ed02009-11-25 08:24:58 -080097 } else if (arch.equals("amd64")) {
98 exe += "amd64/java";
duke6e45e102007-12-01 00:00:00 +000099 } else {
100 exe += "java";
101 }
102 String jdwpOption = "-agentlib:jdwp=transport=dt_socket"
103 + ",server=y" + ",suspend=y" + ",address=" + address;
104 String cmd = exe + " " + VMConnection.getDebuggeeVMOptions()
105 + " " + jdwpOption
106 + " " + jdwpOption
107 + " " + class_name;
108
109 System.out.println("Starting: " + cmd);
110
111 Process p = Runtime.getRuntime().exec(cmd);
112
113 return p;
114 }
115
116 /*
117 * - pick a TCP port
118 * - Launch a server debuggee that should fail
119 * - verify we saw error
120 */
121 public static void main(String args[]) throws Exception {
122 // find a free port
123 ServerSocket ss = new ServerSocket(0);
124 int port = ss.getLocalPort();
125 ss.close();
126
127 String address = String.valueOf(port);
128
129 // launch the server debuggee
130 Process process = launch(address, "Exit0");
131 Thread t1 = IOHandler.handle(process.getInputStream());
132 Thread t2 = IOHandler.handle(process.getErrorStream());
133
134 // wait for the debugge to be ready
135 synchronized(locker) {
136 locker.wait();
137 }
138
139 int exitCode = process.waitFor();
140 try {
141 t1.join();
142 t2.join();
143 } catch ( InterruptedException e ) {
144 e.printStackTrace();
145 throw new Exception("Debuggee failed InterruptedException");
146 }
147
148 if ( outputText.contains("capabilities") ) {
149 throw new Exception(
150 "Debuggee failed with ERROR about capabilities: " + outputText);
151 }
152
153 if ( !outputText.contains("ERROR") ) {
154 throw new Exception(
155 "Debuggee does not have ERROR in the output: " + outputText);
156 }
157
158 if ( exitCode == 0 ) {
159 throw new Exception(
160 "Debuggee should have failed with an non-zero exit code");
161 }
162
163 }
164
165}