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