1 /*
2  * Copyright (c) 2007, 2018, 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 package nsk.jdi.AttachingConnector.attach.attach004;
24 
25 import java.io.*;
26 import java.util.*;
27 import nsk.share.*;
28 import nsk.share.jdi.ArgumentHandler;
29 import nsk.share.jpda.SocketIOPipe;
30 
31 public class attach004t {
32 
33     static String testWorkDir;
34 
parseArgs(String[] args)35     static String[] parseArgs(String[] args) {
36         ArrayList<String> standardArgs = new ArrayList<String>();
37 
38         for (int i = 0; i < args.length; i++) {
39             if (args[i].equals("-testWorkDir") && (i < args.length - 1)) {
40                 testWorkDir = args[i + 1];
41 
42                 if (testWorkDir.endsWith(File.separator)) {
43                     testWorkDir = testWorkDir.substring(0, testWorkDir.length() - 1);
44                 }
45 
46                 i++;
47             } else
48                 standardArgs.add(args[i]);
49         }
50 
51         return standardArgs.toArray(new String[] {});
52     }
53 
readPortNumber(Log log)54     static int readPortNumber(Log log) {
55         try {
56             String fileName = testWorkDir + File.separator + attach004.tempFileName;
57             File file = new File(fileName);
58 
59             while (!file.exists()) {
60                 log.display("File '" + file + "' doesn't exists, sleep some");
61                 Thread.sleep(1000);
62             }
63 
64             LineNumberReader reader = null;
65 
66             try {
67                 FileInputStream fileInputStream = new FileInputStream(file);
68                 while (fileInputStream.available() == 0) {
69                     log.display("File '" + file + "' is empty, sleep some");
70                     Thread.sleep(1000);
71                 }
72 
73                 reader = new LineNumberReader(new InputStreamReader(fileInputStream));
74                 String portString = reader.readLine();
75 
76                 log.display("Port number was read: " + portString);
77 
78                 return Integer.parseInt(portString);
79             } finally {
80                 if (reader != null)
81                     reader.close();
82             }
83 
84         } catch (Exception e) {
85             log.complain("Unexpected exception: " + e);
86             e.printStackTrace(log.getOutStream());
87             throw new Failure("Unexpected exception: " + e);
88         }
89     }
90 
main(String args[])91     public static void main(String args[]) throws InterruptedException {
92         ArgumentHandler argHandler = new ArgumentHandler(parseArgs(args));
93 
94         if (testWorkDir == null) {
95             throw new TestBug("'testWorkDir' parameter wasn't specified");
96         }
97 
98         Log log = argHandler.createDebugeeLog();
99 
100         log.display("attach004t was started");
101 
102         SocketIOPipe pipe = null;
103         try {
104             int portNumber = readPortNumber(log);
105             pipe = SocketIOPipe.createClientIOPipe(log, "localhost", portNumber, 0);
106             log.display("Send message to debugger: " + attach004.messageOK);
107             pipe.println(attach004.messageOK);
108 
109             String message = pipe.readln();
110             log.display("Received from debugger: " + message);
111             if (!message.equals(attach004.messageQuit)) {
112                 throw new TestBug("Unexpected debugger message: " + message);
113             }
114 
115             final long sleepTime = 10000;
116 
117             log.display("Sleep for " + sleepTime + "ms");
118             try {
119                 // debugee VM should wait here, otherwise test script doesn't have time to obtain debuggee exit code
120                 Thread.sleep(sleepTime);
121             } catch (InterruptedException e) {
122                 log.complain("Unexpected exception: " + e);
123                 e.printStackTrace(log.getOutStream());
124                 throw new Failure("Unexpected exception: " + e);
125             }
126 
127             log.display("attach004t finished execution");
128         } finally {
129             if (pipe != null)
130                 pipe.close();
131         }
132 
133         System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_PASSED);
134     }
135 }
136