0N/A/*
2362N/A * Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test OptionTest
0N/A * @bug 5095072
0N/A * @summary Test for misc jdwp options, just that the option is parsed
0N/A * @author Kelly O'Hair (copied from Tim Bell's NoLaunchOptionTest)
0N/A *
0N/A * @run compile -g OptionTest.java
0N/A * @run compile -g HelloWorld.java
0N/A * @run compile -g VMConnection.java
0N/A * @run main/othervm OptionTest
0N/A */
2017N/A
2017N/Aimport java.net.ServerSocket;
2017N/A
0N/Apublic class OptionTest extends Object {
0N/A private Process subprocess;
0N/A private int subprocessStatus;
0N/A private static final String CR = System.getProperty("line.separator");
0N/A private static final int BUFFERSIZE = 4096;
0N/A public static final int RETSTAT = 0;
0N/A public static final int STDOUT = 1;
0N/A public static final int STDERR = 2;
0N/A
0N/A /**
0N/A * Run an arbitrary command and return the results to caller.
0N/A *
0N/A * @param an array of String containing the command
0N/A * to run and any flags or parameters to the command.
0N/A *
0N/A * @return completion status, stderr and stdout as array of String
0N/A * Look for:
0N/A * return status in result[OptionTest.RETSTAT]
0N/A * standard out in result[OptionTest.STDOUT]
0N/A * standard err in result[OptionTest.STDERR]
0N/A *
0N/A */
0N/A public String[] run (String[] cmdStrings) {
0N/A StringBuffer stdoutBuffer = new StringBuffer();
0N/A StringBuffer stderrBuffer = new StringBuffer();
0N/A
0N/A System.out.print(CR + "runCommand method about to execute: ");
0N/A for (int iNdx = 0; iNdx < cmdStrings.length; iNdx++) {
0N/A System.out.print(" ");
0N/A System.out.print(cmdStrings[iNdx]);
0N/A }
0N/A System.out.println(CR);
0N/A try {
0N/A Process process = Runtime.getRuntime().exec(cmdStrings);
0N/A /*
0N/A * Gather up the output of the subprocess using non-blocking
0N/A * reads so we can get both the subprocess stdout and the
0N/A * subprocess stderr without overfilling any buffers.
0N/A */
0N/A java.io.BufferedInputStream is =
0N/A new java.io.BufferedInputStream(process.getInputStream());
0N/A int isLen = 0;
0N/A byte[] isBuf = new byte[BUFFERSIZE];
0N/A
0N/A java.io.BufferedInputStream es =
0N/A new java.io.BufferedInputStream(process.getErrorStream());
0N/A int esLen = 0;
0N/A byte[] esBuf = new byte[BUFFERSIZE];
0N/A
0N/A do {
0N/A isLen = is.read(isBuf);
0N/A if (isLen > 0) {
0N/A stdoutBuffer.append(
0N/A new String(isBuf, 0, isLen));
0N/A }
0N/A esLen = es.read(esBuf);
0N/A if (esLen > 0) {
0N/A stderrBuffer.append(
0N/A new String(esBuf, 0, esLen));
0N/A }
0N/A } while ((isLen > -1) || (esLen > -1));
0N/A try {
0N/A process.waitFor();
0N/A subprocessStatus = process.exitValue();
0N/A process = null;
0N/A } catch(java.lang.InterruptedException e) {
0N/A System.err.println("InterruptedException: " + e);
0N/A }
0N/A
0N/A } catch(java.io.IOException ex) {
0N/A System.err.println("IO error: " + ex);
0N/A }
0N/A String[] result =
0N/A new String[] {
0N/A Integer.toString(subprocessStatus),
0N/A stdoutBuffer.toString(),
0N/A stderrBuffer.toString()
0N/A };
0N/A
0N/A System.out.println(CR + "--- Return code was: " +
0N/A CR + result[RETSTAT]);
0N/A System.out.println(CR + "--- Return stdout was: " +
0N/A CR + result[STDOUT]);
0N/A System.out.println(CR + "--- Return stderr was: " +
0N/A CR + result[STDERR]);
0N/A
0N/A return result;
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
2017N/A // find a free port
2017N/A ServerSocket ss = new ServerSocket(0);
2017N/A int port = ss.getLocalPort();
2017N/A ss.close();
2017N/A String address = String.valueOf(port);
2017N/A
0N/A String javaExe = System.getProperty("java.home") +
0N/A java.io.File.separator + "bin" +
0N/A java.io.File.separator + "java";
0N/A String targetClass = "HelloWorld";
0N/A String baseOptions = "transport=dt_socket" +
2017N/A ",address=" + address +
0N/A ",server=y" +
0N/A ",suspend=n";
0N/A
0N/A /* Option combinations to try (combos faster, fewer exec's) */
0N/A String options[] = {
0N/A "timeout=0,mutf8=y,quiet=y,stdalloc=y,strict=n",
0N/A "timeout=200000,mutf8=n,quiet=n,stdalloc=n,strict=y"
0N/A };
0N/A
0N/A for ( String option : options) {
0N/A String cmds [] = {javaExe,
0N/A "-agentlib:jdwp=" + baseOptions + "," + option,
0N/A targetClass};
0N/A OptionTest myTest = new OptionTest();
0N/A String results [] = myTest.run(VMConnection.insertDebuggeeVMOptions(cmds));
0N/A if (!(results[RETSTAT].equals("0")) ||
0N/A (results[STDERR].startsWith("ERROR:"))) {
0N/A throw new Exception("Test failed: jdwp doesn't like " + cmds[1]);
0N/A }
0N/A }
0N/A System.out.println("Test passed: status = 0");
0N/A }
0N/A}