author | dsamersoff |
Tue, 20 May 2014 06:11:05 -0700 | |
changeset 24503 | fe24408289d7 |
parent 5506 | 202f599c92aa |
child 24973 | 8c4bc3fa4c4e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test OptionTest |
|
26 |
* @bug 5095072 |
|
27 |
* @summary Test for misc jdwp options, just that the option is parsed |
|
28 |
* @author Kelly O'Hair (copied from Tim Bell's NoLaunchOptionTest) |
|
29 |
* |
|
30 |
* @run compile -g OptionTest.java |
|
31 |
* @run compile -g HelloWorld.java |
|
32 |
* @run compile -g VMConnection.java |
|
33 |
* @run main/othervm OptionTest |
|
34 |
*/ |
|
4514 | 35 |
|
36 |
import java.net.ServerSocket; |
|
37 |
||
2 | 38 |
public class OptionTest extends Object { |
39 |
private Process subprocess; |
|
40 |
private int subprocessStatus; |
|
41 |
private static final String CR = System.getProperty("line.separator"); |
|
42 |
private static final int BUFFERSIZE = 4096; |
|
43 |
public static final int RETSTAT = 0; |
|
44 |
public static final int STDOUT = 1; |
|
45 |
public static final int STDERR = 2; |
|
46 |
||
47 |
/** |
|
48 |
* Run an arbitrary command and return the results to caller. |
|
49 |
* |
|
50 |
* @param an array of String containing the command |
|
51 |
* to run and any flags or parameters to the command. |
|
52 |
* |
|
53 |
* @return completion status, stderr and stdout as array of String |
|
54 |
* Look for: |
|
55 |
* return status in result[OptionTest.RETSTAT] |
|
56 |
* standard out in result[OptionTest.STDOUT] |
|
57 |
* standard err in result[OptionTest.STDERR] |
|
58 |
* |
|
59 |
*/ |
|
60 |
public String[] run (String[] cmdStrings) { |
|
61 |
StringBuffer stdoutBuffer = new StringBuffer(); |
|
62 |
StringBuffer stderrBuffer = new StringBuffer(); |
|
63 |
||
64 |
System.out.print(CR + "runCommand method about to execute: "); |
|
65 |
for (int iNdx = 0; iNdx < cmdStrings.length; iNdx++) { |
|
66 |
System.out.print(" "); |
|
67 |
System.out.print(cmdStrings[iNdx]); |
|
68 |
} |
|
69 |
System.out.println(CR); |
|
70 |
try { |
|
71 |
Process process = Runtime.getRuntime().exec(cmdStrings); |
|
72 |
/* |
|
73 |
* Gather up the output of the subprocess using non-blocking |
|
74 |
* reads so we can get both the subprocess stdout and the |
|
75 |
* subprocess stderr without overfilling any buffers. |
|
76 |
*/ |
|
77 |
java.io.BufferedInputStream is = |
|
78 |
new java.io.BufferedInputStream(process.getInputStream()); |
|
79 |
int isLen = 0; |
|
80 |
byte[] isBuf = new byte[BUFFERSIZE]; |
|
81 |
||
82 |
java.io.BufferedInputStream es = |
|
83 |
new java.io.BufferedInputStream(process.getErrorStream()); |
|
84 |
int esLen = 0; |
|
85 |
byte[] esBuf = new byte[BUFFERSIZE]; |
|
86 |
||
87 |
do { |
|
88 |
isLen = is.read(isBuf); |
|
89 |
if (isLen > 0) { |
|
90 |
stdoutBuffer.append( |
|
91 |
new String(isBuf, 0, isLen)); |
|
92 |
} |
|
93 |
esLen = es.read(esBuf); |
|
94 |
if (esLen > 0) { |
|
95 |
stderrBuffer.append( |
|
96 |
new String(esBuf, 0, esLen)); |
|
97 |
} |
|
98 |
} while ((isLen > -1) || (esLen > -1)); |
|
99 |
try { |
|
100 |
process.waitFor(); |
|
101 |
subprocessStatus = process.exitValue(); |
|
102 |
process = null; |
|
103 |
} catch(java.lang.InterruptedException e) { |
|
104 |
System.err.println("InterruptedException: " + e); |
|
105 |
} |
|
106 |
||
107 |
} catch(java.io.IOException ex) { |
|
108 |
System.err.println("IO error: " + ex); |
|
109 |
} |
|
110 |
String[] result = |
|
111 |
new String[] { |
|
112 |
Integer.toString(subprocessStatus), |
|
113 |
stdoutBuffer.toString(), |
|
114 |
stderrBuffer.toString() |
|
115 |
}; |
|
116 |
||
117 |
System.out.println(CR + "--- Return code was: " + |
|
118 |
CR + result[RETSTAT]); |
|
119 |
System.out.println(CR + "--- Return stdout was: " + |
|
120 |
CR + result[STDOUT]); |
|
121 |
System.out.println(CR + "--- Return stderr was: " + |
|
122 |
CR + result[STDERR]); |
|
123 |
||
124 |
return result; |
|
125 |
} |
|
126 |
||
127 |
public static void main(String[] args) throws Exception { |
|
4514 | 128 |
// find a free port |
129 |
ServerSocket ss = new ServerSocket(0); |
|
130 |
int port = ss.getLocalPort(); |
|
131 |
ss.close(); |
|
132 |
String address = String.valueOf(port); |
|
133 |
||
2 | 134 |
String javaExe = System.getProperty("java.home") + |
135 |
java.io.File.separator + "bin" + |
|
136 |
java.io.File.separator + "java"; |
|
137 |
String targetClass = "HelloWorld"; |
|
138 |
String baseOptions = "transport=dt_socket" + |
|
4514 | 139 |
",address=" + address + |
2 | 140 |
",server=y" + |
141 |
",suspend=n"; |
|
142 |
||
143 |
/* Option combinations to try (combos faster, fewer exec's) */ |
|
144 |
String options[] = { |
|
145 |
"timeout=0,mutf8=y,quiet=y,stdalloc=y,strict=n", |
|
146 |
"timeout=200000,mutf8=n,quiet=n,stdalloc=n,strict=y" |
|
147 |
}; |
|
148 |
||
149 |
for ( String option : options) { |
|
150 |
String cmds [] = {javaExe, |
|
151 |
"-agentlib:jdwp=" + baseOptions + "," + option, |
|
152 |
targetClass}; |
|
153 |
OptionTest myTest = new OptionTest(); |
|
154 |
String results [] = myTest.run(VMConnection.insertDebuggeeVMOptions(cmds)); |
|
155 |
if (!(results[RETSTAT].equals("0")) || |
|
156 |
(results[STDERR].startsWith("ERROR:"))) { |
|
157 |
throw new Exception("Test failed: jdwp doesn't like " + cmds[1]); |
|
158 |
} |
|
159 |
} |
|
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
160 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
161 |
System.out.println("Testing invalid address string"); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
162 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
163 |
// Test invalid addresses |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
164 |
String badAddresses[] = { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
165 |
":", |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
166 |
"localhost:", |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
167 |
"localhost:abc", |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
168 |
"localhost:65536", |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
169 |
"localhost:65F" |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
170 |
}; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
171 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
172 |
for (String badAddress : badAddresses) { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
173 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
174 |
String badOptions = "transport=dt_socket" + |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
175 |
",address=" + badAddress + |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
176 |
",server=y" + |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
177 |
",suspend=n"; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
178 |
String cmds[] = {javaExe, "-agentlib:jdwp=" + badOptions, targetClass}; |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
179 |
OptionTest myTest = new OptionTest(); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
180 |
String results[] = myTest.run(VMConnection.insertDebuggeeVMOptions(cmds)); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
181 |
|
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
182 |
if (!results[RETSTAT].equals("0") && results[STDERR].startsWith("ERROR:")) { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
183 |
// We got expected error, test passed |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
184 |
} |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
185 |
else { |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
186 |
throw new Exception("Test failed: jdwp accept invalid address '" + badAddress + "'"); |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
187 |
} |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
188 |
} |
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
189 |
|
2 | 190 |
System.out.println("Test passed: status = 0"); |
191 |
} |
|
192 |
} |
|
24503
fe24408289d7
8041435: Make JDWP socket connector accept only local connections by default
dsamersoff
parents:
5506
diff
changeset
|
193 |