2
|
1 |
/*
|
|
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 |
*/
|
|
32 |
import java.io.InputStream;
|
|
33 |
import java.io.IOException;
|
|
34 |
import java.io.File;
|
|
35 |
import java.net.ServerSocket;
|
|
36 |
import java.net.Socket;
|
|
37 |
|
|
38 |
public 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";
|
|
97 |
} else {
|
|
98 |
exe += "java";
|
|
99 |
}
|
|
100 |
String jdwpOption = "-agentlib:jdwp=transport=dt_socket"
|
|
101 |
+ ",server=y" + ",suspend=y" + ",address=" + address;
|
|
102 |
String cmd = exe + " " + VMConnection.getDebuggeeVMOptions()
|
|
103 |
+ " " + jdwpOption
|
|
104 |
+ " " + jdwpOption
|
|
105 |
+ " " + class_name;
|
|
106 |
|
|
107 |
System.out.println("Starting: " + cmd);
|
|
108 |
|
|
109 |
Process p = Runtime.getRuntime().exec(cmd);
|
|
110 |
|
|
111 |
return p;
|
|
112 |
}
|
|
113 |
|
|
114 |
/*
|
|
115 |
* - pick a TCP port
|
|
116 |
* - Launch a server debuggee that should fail
|
|
117 |
* - verify we saw error
|
|
118 |
*/
|
|
119 |
public static void main(String args[]) throws Exception {
|
|
120 |
// find a free port
|
|
121 |
ServerSocket ss = new ServerSocket(0);
|
|
122 |
int port = ss.getLocalPort();
|
|
123 |
ss.close();
|
|
124 |
|
|
125 |
String address = String.valueOf(port);
|
|
126 |
|
|
127 |
// launch the server debuggee
|
|
128 |
Process process = launch(address, "Exit0");
|
|
129 |
Thread t1 = IOHandler.handle(process.getInputStream());
|
|
130 |
Thread t2 = IOHandler.handle(process.getErrorStream());
|
|
131 |
|
|
132 |
// wait for the debugge to be ready
|
|
133 |
synchronized(locker) {
|
|
134 |
locker.wait();
|
|
135 |
}
|
|
136 |
|
|
137 |
int exitCode = process.waitFor();
|
|
138 |
try {
|
|
139 |
t1.join();
|
|
140 |
t2.join();
|
|
141 |
} catch ( InterruptedException e ) {
|
|
142 |
e.printStackTrace();
|
|
143 |
throw new Exception("Debuggee failed InterruptedException");
|
|
144 |
}
|
|
145 |
|
|
146 |
if ( outputText.contains("capabilities") ) {
|
|
147 |
throw new Exception(
|
|
148 |
"Debuggee failed with ERROR about capabilities: " + outputText);
|
|
149 |
}
|
|
150 |
|
|
151 |
if ( !outputText.contains("ERROR") ) {
|
|
152 |
throw new Exception(
|
|
153 |
"Debuggee does not have ERROR in the output: " + outputText);
|
|
154 |
}
|
|
155 |
|
|
156 |
if ( exitCode == 0 ) {
|
|
157 |
throw new Exception(
|
|
158 |
"Debuggee should have failed with an non-zero exit code");
|
|
159 |
}
|
|
160 |
|
|
161 |
}
|
|
162 |
|
|
163 |
}
|