1 /*
2  * Copyright (c) 2001, 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 
24 package nsk.jdi.VirtualMachineManager.connectedVirtualMachines;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 import com.sun.jdi.*;
31 import com.sun.jdi.connect.*;
32 import java.util.*;
33 import java.io.*;
34 
35 import java.net.*;
36 
37 import com.sun.jdi.event.*;
38 import com.sun.jdi.request.*;
39 
40 /**
41  * The test for the implementation of an object of the type     <BR>
42  * VirtualMachineManager.                                       <BR>
43  *                                                              <BR>
44  * The test checks up that results of the method                <BR>
45  * <code>com.sun.jdi.VirtualMachineManager.connectedVirtualMachines()</code> <BR>
46  * complies with its specification.                             <BR>
47  * <BR>
48  * Test case includes only one debuggee.                        <BR>
49  * The test checks up that :                                    <BR>
50  * - after launching debugged Virtual Machine the tested method <BR>
51  *   returns List containing one element of                     <BR>
52  *   a VirtualMachine type which is equal to a mirror of        <BR>
53  *   the debuggee already got after launching;                  <BR>
54  * - after the deguggee normally ends via its exit() method,    <BR>
55  *   the call to the method returns an empty List.              <BR>
56  */
57 
58 public class convm001 {
59 
60     //----------------------------------------------------- templete section
61     static final int PASSED = 0;
62     static final int FAILED = 2;
63     static final int PASS_BASE = 95;
64 
65     //----------------------------------------------------- templete parameters
66     static final String
67     sHeader1 = "\n==> nsk/jdi/VirtualMachineManager/connectedVirtualMachines/convm001  ",
68     sHeader2 = "--> debugger: ",
69     sHeader3 = "##> debugger: ";
70 
71     //----------------------------------------------------- main method
72 
main(String argv[])73     public static void main (String argv[]) {
74         int result = run(argv, System.out);
75         System.exit(result + PASS_BASE);
76     }
77 
run(String argv[], PrintStream out)78     public static int run (String argv[], PrintStream out) {
79         return new convm001().runThis(argv, out);
80     }
81 
82     //--------------------------------------------------   log procedures
83 
84     private static Log  logHandler;
85 
log1(String message)86     private static void log1(String message) {
87         logHandler.display(sHeader1 + message);
88     }
log2(String message)89     private static void log2(String message) {
90         logHandler.display(sHeader2 + message);
91     }
log3(String message)92     private static void log3(String message) {
93         logHandler.complain(sHeader3 + message);
94     }
95 
96     //  ************************************************    test parameters
97 
98     private String debuggeeName =
99         "nsk.jdi.VirtualMachineManager.connectedVirtualMachines.convm001a";
100 
101     //String mName = "nsk.jdi.VirtualMachineManager.connectedVirtualMachines";
102 
103     //====================================================== test program
104     //------------------------------------------------------ common section
105 
106     static ArgumentHandler      argsHandler;
107 
108     static int waitTime;
109 
110     static VirtualMachine      vm            = null;
111 
112     static EventRequestManager eventRManager = null;
113     static EventQueue          eventQueue    = null;
114     static EventSet            eventSet      = null;
115 
116     static int  testExitCode = PASSED;
117 
118     static final int returnCode0 = 0;
119     static final int returnCode1 = 1;
120     static final int returnCode2 = 2;
121     static final int returnCode3 = 3;
122     static final int returnCode4 = 4;
123 
124     //------------------------------------------------------ methods
125 
runThis(String argv[], PrintStream out)126     private int runThis (String argv[], PrintStream out) {
127 
128         Debugee debuggee;
129 
130         argsHandler     = new ArgumentHandler(argv);
131         logHandler      = new Log(out, argsHandler);
132         Binder binder   = new Binder(argsHandler, logHandler);
133 
134         if (argsHandler.verbose()) {
135             debuggee = binder.bindToDebugee(debuggeeName + " -vbs");
136         } else {
137             debuggee = binder.bindToDebugee(debuggeeName);
138         }
139 
140         waitTime = argsHandler.getWaitTime();
141 
142 
143         IOPipe pipe     = new IOPipe(debuggee);
144 
145         debuggee.redirectStderr(out);
146         log2("issuspended002a debuggee launched");
147         debuggee.resume();
148 
149         String line = pipe.readln();
150         if ((line == null) || !line.equals("ready")) {
151             log3("signal received is not 'ready' but: " + line);
152             return FAILED;
153         } else {
154             log2("'ready' recieved");
155         }
156 
157         vm = debuggee.VM();
158 
159     //------------------------------------------------------  testing section
160         log1("      TESTING BEGINS");
161 
162         VirtualMachineManager vmm = null;
163 
164         List connectedVM;
165         VirtualMachine vm1 = null;
166 
167         int size = 0;
168 
169 
170         for (int i = 0; ; i++) {
171 
172             pipe.println("newcheck");
173             line = pipe.readln();
174 
175             if (line.equals("checkend")) {
176                 log2("     : returned string is 'checkend'");
177                 break ;
178             } else if (!line.equals("checkready")) {
179                 log3("ERROR: returned string is not 'checkready'");
180                 testExitCode = FAILED;
181                 break ;
182             }
183 
184             log1("new checkready: #" + i);
185 
186             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part
187 
188             log2("......call to Bootstrap.virtualMachineManager()");
189 
190             vmm = Bootstrap.virtualMachineManager();
191             if (vmm == null) {
192                 log3("ERROR: null returned");
193                 testExitCode = FAILED;
194             } else {
195 
196                 log2("......call to vmm.connectedVirtualMachines()");
197                 connectedVM = vmm.connectedVirtualMachines();
198 
199                 size = connectedVM.size();
200                 if (size != 1) {
201                     log3("ERROR: connectedVM.size() != 1 : " + size);
202                     testExitCode = FAILED;
203                 } else {
204                     log2("       connectedVM.size() == 1");
205                     log2("......getting: VirtualMachine vm1 = connectedVM.get(0);");
206                     vm1 = (VirtualMachine) connectedVM.get(0);
207 
208                     log2("......comparing: vm.equals(vm1)");
209                     if (!vm.equals(vm1)) {
210                         log3("ERROR: !vm.equals(vm1)");
211                         testExitCode = FAILED;
212                     }
213                 }
214             }
215 
216             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
217         }
218 
219         eventQueue = vm.eventQueue();
220         eventSet = null;
221 
222     //--------------------------------------------------   test summary section
223     //-------------------------------------------------    standard end section
224 
225         pipe.println("quit");
226         log2("waiting for the debuggee to finish ...");
227         debuggee.waitFor();
228 
229         int status = debuggee.getStatus();
230         if (status != PASSED + PASS_BASE) {
231             log3("debuggee returned UNEXPECTED exit status: " +
232                     status + " != PASS_BASE");
233             testExitCode = FAILED;
234         } else {
235             log2("debuggee returned expected exit status: " +
236                     status + " == PASS_BASE");
237 
238             if (vmDisconnect() == 0 ) {
239                 log2("......connectedVM = vmm.connectedVirtualMachines();");
240                 connectedVM = vmm.connectedVirtualMachines();
241 
242                 size = connectedVM.size();
243                 if (size != 0) {
244                     log3("ERROR: connectedVM.size() != 0 : " + size);
245                     testExitCode = FAILED;
246 
247                     log2("......since a returned list is not empty");
248                     log2("......try to get first element and compare to the mirror of the vm tested");
249                     try {
250                         log2("        getting: vm1 = (VirtualMachine) connectedVM.get(0);");
251                         vm1 = (VirtualMachine) connectedVM.get(0);
252 
253                         log2("         vm.name()== " + vm1.name());
254                         if (vm.equals(vm1))
255                             log2("vm.equals(vm1)");
256                         else
257                             log2("!vm.equals(vm1)");
258                     } catch ( Exception e ) {
259                         log2("......failure because of Exception:  " + e);
260                     }
261 
262                 } else
263                     log2("       connectedVM.size() == 0");
264             } else {
265                 log3("ERROR: threadDeath != 0");
266                 testExitCode = FAILED;
267             }
268 
269         }
270 
271         if (testExitCode != PASSED) {
272             logHandler.complain("TEST FAILED");
273         }
274         return testExitCode;
275     }
276 
vmDisconnect()277     private int vmDisconnect () {
278 
279         int returnCode = returnCode0;
280 
281         log2("       waiting for VMDisconnectEvent");
282 
283         labelBP:
284             for (;;) {
285 
286                 log2("       new:  eventSet = eventQueue.remove();");
287                 try {
288                     eventSet = eventQueue.remove(waitTime*60000);
289                     if (eventSet == null) {
290                         log3("ERROR:  timeout for waiting for a ThreadDeathEvent");
291                         returnCode = returnCode3;
292                         break labelBP;
293                     }
294                 } catch ( Exception e ) {
295                     log3("ERROR: Exception for  eventSet = eventQueue.remove(); : " + e);
296                     returnCode = 1;
297                     break labelBP;
298                 }
299 
300                 if (eventSet != null) {
301 
302                     log2("     :  eventSet != null;  size == " + eventSet.size());
303 
304                     EventIterator eIter = eventSet.eventIterator();
305                     Event         ev    = null;
306 
307                     for (; eIter.hasNext(); ) {
308 
309                         if (returnCode != returnCode0)
310                             break;
311 
312                         ev = eIter.nextEvent();
313 
314                     ll: for (int ifor =0;  ; ifor++) {
315 
316                         try {
317                           switch (ifor) {
318 
319                           case 0:  AccessWatchpointEvent awe = (AccessWatchpointEvent) ev;
320                                    log2("      AccessWatchpointEvent removed");
321                                    break ll;
322                           case 1:  BreakpointEvent be = (BreakpointEvent) ev;
323                                    log2("      BreakpointEvent removed");
324                                    break ll;
325                           case 2:  ClassPrepareEvent cpe = (ClassPrepareEvent) ev;
326                                    log2("      ClassPreparEvent removed");
327                                    break ll;
328                           case 3:  ClassUnloadEvent cue = (ClassUnloadEvent) ev;
329                                    log2("      ClassUnloadEvent removed");
330                                    break ll;
331                           case 4:  ExceptionEvent ee = (ExceptionEvent) ev;
332                                    log2("      ExceptionEvent removed");
333                                    break ll;
334                           case 5:  MethodEntryEvent mene = (MethodEntryEvent) ev;
335                                    log2("      MethodEntryEvent removed");
336                                    break ll;
337                           case 6:  MethodExitEvent mexe = (MethodExitEvent) ev;
338                                    log2("      MethodExiEvent removed");
339                                    break ll;
340                           case 7:  ModificationWatchpointEvent mwe = (ModificationWatchpointEvent) ev;
341                                    log2("      ModificationWatchpointEvent removed");
342                                    break ll;
343                           case 8:  StepEvent se = (StepEvent) ev;
344                                    log2("      StepEvent removed");
345                                    break ll;
346                           case 9:  ThreadDeathEvent tde = (ThreadDeathEvent) ev;
347                                    log2("      ThreadDeathEvent removed");
348                                    break ll;
349                           case 10: ThreadStartEvent tse = (ThreadStartEvent) ev;
350                                    log2("      ThreadStartEvent removed");
351                                    break ll;
352                           case 11: VMDeathEvent vmde = (VMDeathEvent) ev;
353                                    log2("      VMDeathEvent removed");
354                                    break ll;
355                           case 12: VMStartEvent vmse = (VMStartEvent) ev;
356                                    log2("      VMStartEvent removed");
357                                    break ll;
358                           case 13: WatchpointEvent we = (WatchpointEvent) ev;
359                                    log2("      WatchpointEvent removed");
360                                    break ll;
361 
362                           case 14: VMDisconnectEvent wmde = (VMDisconnectEvent) ev;
363                                    log2("      VMDisconnectEvent removed");
364                                    break labelBP;
365 
366                           default: log3("ERROR:  default case for casting event");
367                                    returnCode = returnCode3;
368                                    break ll;
369                           } // switch
370                         } catch ( ClassCastException e ) {
371                         }   // try
372                     }       // ll: for (int ifor =0;  ; ifor++)
373                 }           // for (; ev.hasNext(); )
374             }
375         }
376         if (returnCode == returnCode0)
377             log2("     :  eventSet == null:  EventQueue is empty");
378 
379         return returnCode;
380     }
381 
382 
383 }
384