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