1 /*
   2  * Copyright (c) 2004, 2015, Oracle and/or its affiliates. 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 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.
  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  *  @modules jdk.jdi
  31  *  @run compile -g OptionTest.java
  32  *  @run compile -g HelloWorld.java
  33  *  @run compile -g VMConnection.java
  34  *  @run driver OptionTest
  35  */
  36 
  37 import java.net.ServerSocket;
  38 import java.util.regex.Matcher;
  39 import java.util.regex.Pattern;
  40 
  41 public class OptionTest extends Object {
  42     private static final Pattern TRANSPORT_ERROR_PTRN = Pattern.compile("^ERROR: transport error .+$", Pattern.MULTILINE);
  43     private int subprocessStatus;
  44     private static final String CR = System.getProperty("line.separator");
  45     private static final int BUFFERSIZE = 4096;
  46     public static final int RETSTAT = 0;
  47     public static final int STDOUT = 1;
  48     public static final int STDERR = 2;
  49 
  50     /**
  51      * Run an arbitrary command and return the results to caller.
  52      *
  53      * @param an array of String containing the command
  54      *        to run and any flags or parameters to the command.
  55      *
  56      * @return completion status, stderr and stdout as array of String
  57      *  Look for:
  58      *    return status in result[OptionTest.RETSTAT]
  59      *    standard out in result[OptionTest.STDOUT]
  60      *    standard err in result[OptionTest.STDERR]
  61      *
  62      */
  63     public String[] run (String[] cmdStrings) {
  64         StringBuffer stdoutBuffer = new StringBuffer();
  65         StringBuffer stderrBuffer = new StringBuffer();
  66 
  67         System.out.print(CR + "runCommand method about to execute: ");
  68         for (int iNdx = 0; iNdx < cmdStrings.length; iNdx++) {
  69             System.out.print(" ");
  70             System.out.print(cmdStrings[iNdx]);
  71         }
  72         System.out.println(CR);
  73         try {
  74             Process process = Runtime.getRuntime().exec(cmdStrings);
  75             /*
  76              * Gather up the output of the subprocess using non-blocking
  77              * reads so we can get both the subprocess stdout and the
  78              * subprocess stderr without overfilling any buffers.
  79              */
  80             java.io.BufferedInputStream is =
  81                 new java.io.BufferedInputStream(process.getInputStream());
  82             int isLen = 0;
  83             byte[] isBuf = new byte[BUFFERSIZE];
  84 
  85             java.io.BufferedInputStream es =
  86                 new java.io.BufferedInputStream(process.getErrorStream());
  87             int esLen = 0;
  88             byte[] esBuf = new byte[BUFFERSIZE];
  89 
  90             do {
  91                 isLen = is.read(isBuf);
  92                 if (isLen > 0) {
  93                     stdoutBuffer.append(
  94                                         new String(isBuf, 0, isLen));
  95                 }
  96                 esLen = es.read(esBuf);
  97                 if (esLen > 0) {
  98                     stderrBuffer.append(
  99                                         new String(esBuf, 0, esLen));
 100                 }
 101             } while ((isLen > -1) || (esLen > -1));
 102             try {
 103                 process.waitFor();
 104                 subprocessStatus = process.exitValue();
 105                 process = null;
 106             } catch(java.lang.InterruptedException e) {
 107                 System.err.println("InterruptedException: " + e);
 108             }
 109 
 110         } catch(java.io.IOException ex) {
 111             System.err.println("IO error: " + ex);
 112         }
 113         String[] result =
 114             new String[] {
 115                 Integer.toString(subprocessStatus),
 116                 stdoutBuffer.toString(),
 117                 stderrBuffer.toString()
 118         };
 119 
 120         System.out.println(CR + "--- Return code was: " +
 121                            CR + result[RETSTAT]);
 122         System.out.println(CR + "--- Return stdout was: " +
 123                            CR + result[STDOUT]);
 124         System.out.println(CR + "--- Return stderr was: " +
 125                            CR + result[STDERR]);
 126 
 127         return result;
 128     }
 129 
 130     public static void main(String[] args) throws Exception {
 131         // find a free port
 132         ServerSocket ss = new ServerSocket(0);
 133         int port = ss.getLocalPort();
 134         ss.close();
 135         String address = String.valueOf(port);
 136 
 137         String javaExe = System.getProperty("java.home") +
 138             java.io.File.separator + "bin" +
 139             java.io.File.separator + "java";
 140         String targetClass = "HelloWorld";
 141         String baseOptions = "transport=dt_socket" +
 142                               ",address=" + address +
 143                               ",server=y" +
 144                               ",suspend=n";
 145 
 146         /* Option combinations to try (combos faster, fewer exec's) */
 147         String options[] =  {
 148                 "timeout=0,mutf8=y,quiet=y,stdalloc=y,strict=n",
 149                 "timeout=200000,mutf8=n,quiet=n,stdalloc=n,strict=y"
 150                 };
 151 
 152         for ( String option : options) {
 153             String cmds [] = {javaExe,
 154                               "-agentlib:jdwp=" + baseOptions + "," + option,
 155                               targetClass};
 156             OptionTest myTest = new OptionTest();
 157             String results [] = myTest.run(VMConnection.insertDebuggeeVMOptions(cmds));
 158             if (!(results[RETSTAT].equals("0")) ||
 159                 (TRANSPORT_ERROR_PTRN.matcher(results[STDERR]).find())) {
 160                 throw new Exception("Test failed: jdwp doesn't like " + cmds[1]);
 161             }
 162         }
 163 
 164         System.out.println("Testing invalid address string");
 165 
 166         // Test invalid addresses
 167         String badAddresses[] = {
 168             ":",
 169             "localhost:",
 170             "localhost:abc",
 171             "localhost:65536",
 172             "localhost:65F"
 173         };
 174 
 175         for (String badAddress : badAddresses) {
 176 
 177             String badOptions = "transport=dt_socket" +
 178                               ",address=" + badAddress +
 179                               ",server=y" +
 180                               ",suspend=n";
 181             String cmds[] = {javaExe, "-agentlib:jdwp=" + badOptions, targetClass};
 182             OptionTest myTest = new OptionTest();
 183             String results[] = myTest.run(VMConnection.insertDebuggeeVMOptions(cmds));
 184 
 185             if (!results[RETSTAT].equals("0") && TRANSPORT_ERROR_PTRN.matcher(results[STDERR]).find()) {
 186                 // We got expected error, test passed
 187             }
 188             else {
 189                 throw new Exception("Test failed: jdwp accept invalid address '" + badAddress + "'");
 190             }
 191         }
 192 
 193         System.out.println("Test passed: status = 0");
 194     }
 195 }
 196