Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jdi/DoubleAgentTest.java
38855 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 635434525* @summary Check that a double agent request fails26*27* @build VMConnection DoubleAgentTest Exit028* @run main DoubleAgentTest29*30*/31import java.io.InputStream;32import java.io.IOException;33import java.io.File;34import java.net.ServerSocket;35import java.net.Socket;3637public class DoubleAgentTest {3839static Object locker = new Object();40static String outputText = "";4142/*43* Helper class to redirect process output/error44*/45static class IOHandler implements Runnable {46InputStream in;4748IOHandler(InputStream in) {49this.in = in;50}5152static Thread handle(InputStream in) {53IOHandler handler = new IOHandler(in);54Thread thr = new Thread(handler);55thr.setDaemon(true);56thr.start();57return thr;58}5960public void run() {61try {62byte b[] = new byte[100];63for (;;) {64int n = in.read(b, 0, 100);65// The first thing that will get read is66// Listening for transport dt_socket at address: xxxxx67// which shows the debuggee is ready to accept connections.68synchronized(locker) {69locker.notify();70}71if (n < 0) {72break;73}74String s = new String(b, 0, n, "UTF-8");75System.out.print(s);76synchronized(outputText) {77outputText += s;78}79}80} catch (IOException ioe) {81ioe.printStackTrace();82}83}8485}8687/*88* Launch a server debuggee with the given address89*/90private static Process launch(String address, String class_name) throws IOException {91String exe = System.getProperty("java.home")92+ File.separator + "bin" + File.separator + "java";93String jdwpOption = "-agentlib:jdwp=transport=dt_socket"94+ ",server=y" + ",suspend=y" + ",address=" + address;95String cmd = exe + " " + VMConnection.getDebuggeeVMOptions()96+ " " + jdwpOption97+ " " + jdwpOption98+ " " + class_name;99100System.out.println("Starting: " + cmd);101102Process p = Runtime.getRuntime().exec(cmd);103104return p;105}106107/*108* - pick a TCP port109* - Launch a server debuggee that should fail110* - verify we saw error111*/112public static void main(String args[]) throws Exception {113// find a free port114ServerSocket ss = new ServerSocket(0);115int port = ss.getLocalPort();116ss.close();117118String address = String.valueOf(port);119120// launch the server debuggee121Process process = launch(address, "Exit0");122Thread t1 = IOHandler.handle(process.getInputStream());123Thread t2 = IOHandler.handle(process.getErrorStream());124125// wait for the debugge to be ready126synchronized(locker) {127locker.wait();128}129130int exitCode = process.waitFor();131try {132t1.join();133t2.join();134} catch ( InterruptedException e ) {135e.printStackTrace();136throw new Exception("Debuggee failed InterruptedException");137}138139if ( outputText.contains("capabilities") ) {140throw new Exception(141"Debuggee failed with ERROR about capabilities: " + outputText);142}143144if ( !outputText.contains("ERROR") ) {145throw new Exception(146"Debuggee does not have ERROR in the output: " + outputText);147}148149if ( exitCode == 0 ) {150throw new Exception(151"Debuggee should have failed with an non-zero exit code");152}153154}155156}157158159