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 
25 /*
26  * @test
27  *
28  * @summary converted from VM Testbase nsk/jdi/VoidValue/equals/equals001.
29  * VM Testbase keywords: [quick, jpda, jdi]
30  * VM Testbase readme:
31  * DESCRIPTION:
32  *     The test for the implementation of an object of the type
33  *     VoidValue.
34  *     The test checks up that a result of the method
35  *     com.sun.jdi.VoidValue.equals()
36  *     complies with its spec:
37  *     public boolean equals(java.lang.Object obj)
38  *     Compares the specified Object with this VoidValue for equality.
39  *     Returns: true if the Object is a VoidValue; false otherwise.
40  *     The test works as follows:
41  *     A debugger program - nsk.jdi.VoidValue.equals.equals001;
42  *     a debuggee program - nsk.jdi.VoidValue.equals.equals001a.
43  *     Using nsk.jdi.share classes,
44  *     the debugger gets the debuggee running on another JavaVM,
45  *     creates the object debuggee.VM,
46  *     establishes a pipe with the debuggee program, and then
47  *     send to the programm commands, to which the debuggee replies
48  *     via the pipe. Upon getting reply,
49  *     the debugger calls corresponding debuggee.VM methods to get
50  *     needed data and compares the data got to the data expected.
51  *     In case of mismatch the test produces the return value 97 and
52  *     a corresponding error message(s).
53  *     Otherwise, the test is passed and produces
54  *     the return value 95 and no message.
55  * COMMENTS:
56  *
57  * @library /vmTestbase
58  *          /test/lib
59  * @run driver jdk.test.lib.FileInstaller . .
60  * @build nsk.jdi.VoidValue.equals.equals001.equals001
61  *        nsk.jdi.VoidValue.equals.equals001.equals001a
62  * @run main/othervm PropertyResolvingWrapper
63  *      nsk.jdi.VoidValue.equals.equals001.equals001
64  *      -verbose
65  *      -arch=${os.family}-${os.simpleArch}
66  *      -waittime=5
67  *      -debugee.vmkind=java
68  *      -transport.address=dynamic
69  *      "-debugee.vmkeys=${test.vm.opts} ${test.java.opts}"
70  */
71 
72 package nsk.jdi.VoidValue.equals.equals001;
73 
74 import nsk.share.*;
75 import nsk.share.jpda.*;
76 import nsk.share.jdi.*;
77 
78 import com.sun.jdi.*;
79 import com.sun.jdi.event.*;
80 import com.sun.jdi.request.*;
81 import java.util.*;
82 import java.io.*;
83 
84 /**
85  * The test for the implementation of an object of the type     <BR>
86  * VoidValue.                                                   <BR>
87  *                                                              <BR>
88  * The test checks up that results of the method                <BR>
89  * <code>com.sun.jdi.VoidValue.equals()</code>                  <BR>
90  * complies with its spec.                                      <BR>
91  * <BR>
92  * The cases for testing are as follows :               <BR>
93  *                                                      <BR>
94  * when a gebuggee is suspended at a breakpoint set up  <BR>
95  * by a debugger the debugger invokes two debuggee's methods:   <BR>
96  *    void vdValue1(); and  void vdValue2();            <BR>
97  * to form objects                                      <BR>
98  *    Value returnvdValue1, Value returnvdValue2;       <BR>
99  *                                                      <BR>
100  * then performs the following casts:                   <BR>
101  *    VoidValue vdValue1 = (VoidValue) returnvdValue1;  <BR>
102  *    VoidValue vdValue2 = (VoidValue) returnvdValue2;  <BR>
103  *                                                      <BR>
104  * and checks up that the following is true:            <BR>
105  *                                                      <BR>
106  * a)    vdValue1.equals(vdValue2)                      <BR>
107  *                                                      <BR>
108  * b)    Object testObj = null;                         <BR>
109  *      !vdValue1.equals(testObj)                       <BR>
110  * <BR>
111  */
112 
113 public class equals001 {
114 
115     //----------------------------------------------------- templete section
116     static final int PASSED = 0;
117     static final int FAILED = 2;
118     static final int PASS_BASE = 95;
119 
120     //----------------------------------------------------- templete parameters
121     static final String
122     sHeader1 = "\n==> nsk/jdi/VoidValue/equals/equals001",
123     sHeader2 = "--> debugger: ",
124     sHeader3 = "##> debugger: ";
125 
126     //----------------------------------------------------- main method
127 
main(String argv[])128     public static void main (String argv[]) {
129         int result = run(argv, System.out);
130         System.exit(result + PASS_BASE);
131     }
132 
run(String argv[], PrintStream out)133     public static int run (String argv[], PrintStream out) {
134         return new equals001().runThis(argv, out);
135     }
136 
137      //--------------------------------------------------   log procedures
138 
139     //private static boolean verbMode = false;
140 
141     private static Log  logHandler;
142 
log1(String message)143     private static void log1(String message) {
144         logHandler.display(sHeader1 + message);
145     }
log2(String message)146     private static void log2(String message) {
147         logHandler.display(sHeader2 + message);
148     }
log3(String message)149     private static void log3(String message) {
150         logHandler.complain(sHeader3 + message);
151     }
152 
153     //  ************************************************    test parameters
154 
155     private String debuggeeName =
156         "nsk.jdi.VoidValue.equals.equals001.equals001a";
157 
158     private String testedClassName =
159         "nsk.jdi.VoidValue.equals.equals001.Threadequals001a";
160 
161     //====================================================== test program
162     //------------------------------------------------------ common section
163 
164     static ArgumentHandler      argsHandler;
165 
166     static int waitTime;
167 
168     static VirtualMachine      vm            = null;
169     static EventRequestManager eventRManager = null;
170     static EventQueue          eventQueue    = null;
171     static EventSet            eventSet      = null;
172 
173     ReferenceType     testedclass  = null;
174     ThreadReference   thread2      = null;
175     ThreadReference   mainThread   = null;
176 
177     static int  testExitCode = PASSED;
178 
179     static final int returnCode0 = 0;
180     static final int returnCode1 = 1;
181     static final int returnCode2 = 2;
182     static final int returnCode3 = 3;
183     static final int returnCode4 = 4;
184 
185     //------------------------------------------------------ methods
186 
runThis(String argv[], PrintStream out)187     private int runThis (String argv[], PrintStream out) {
188 
189         Debugee debuggee;
190 
191         argsHandler     = new ArgumentHandler(argv);
192         logHandler      = new Log(out, argsHandler);
193         Binder binder   = new Binder(argsHandler, logHandler);
194 
195         if (argsHandler.verbose()) {
196             debuggee = binder.bindToDebugee(debuggeeName + " -vbs");  // *** tp
197         } else {
198             debuggee = binder.bindToDebugee(debuggeeName);            // *** tp
199         }
200 
201         waitTime = argsHandler.getWaitTime();
202 
203 
204         IOPipe pipe     = new IOPipe(debuggee);
205 
206         debuggee.redirectStderr(out);
207         log2("setvalue003a debuggee launched");
208         debuggee.resume();
209 
210         String line = pipe.readln();
211         if ((line == null) || !line.equals("ready")) {
212             log3("signal received is not 'ready' but: " + line);
213             return FAILED;
214         } else {
215             log2("'ready' recieved");
216         }
217 
218         VirtualMachine vm = debuggee.VM();
219 
220     //------------------------------------------------------  testing section
221         log1("      TESTING BEGINS");
222 
223         for (int i = 0; ; i++) {
224             pipe.println("newcheck");
225             line = pipe.readln();
226 
227             if (line.equals("checkend")) {
228                 log2("     : returned string is 'checkend'");
229                 break ;
230             } else if (!line.equals("checkready")) {
231                 log3("ERROR: returned string is not 'checkready'");
232                 testExitCode = FAILED;
233                 break ;
234             }
235 
236             log1("new check: #" + i);
237 
238             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ variable part
239 
240             int expresult = returnCode0;
241 
242             eventRManager = vm.eventRequestManager();
243             eventQueue    = vm.eventQueue();
244 
245             List            allThreads   = null;
246             ListIterator    listIterator = null;
247             List            classes      = null;
248 
249             String threadName = "Thread2";
250 
251             String breakpointMethod1 = "run";
252             String bpLine1          = "breakpointLineNumber1";
253 
254             BreakpointRequest breakpRequest1 = null;
255 
256 
257             label0: {
258 
259                 log2("getting ThreadReference object");
260                 try {
261                     allThreads  = vm.allThreads();
262                     classes     = vm.classesByName(testedClassName);
263                     testedclass = (ReferenceType) classes.get(0);
264                 } catch ( Exception e) {
265                     log3("ERROR: Exception at very beginning !? : " + e);
266                     expresult = returnCode1;
267                     break label0;
268                 }
269 
270                 listIterator = allThreads.listIterator();
271                 for (;;) {
272                     try {
273                         thread2 = (ThreadReference) listIterator.next();
274                         if (thread2.name().equals(threadName))
275                             break ;
276                     } catch ( NoSuchElementException e ) {
277                         log3("ERROR: NoSuchElementException for listIterator.next()");
278                         log3("ERROR: NO THREAD2 ?????????!!!!!!!");
279                         expresult = returnCode1;
280                         break label0;
281                     }
282                 }
283 
284                 log2("setting up breakpoint");
285 
286                 breakpRequest1 = settingBreakpoint(breakpointMethod1, bpLine1, "one");
287                 if (breakpRequest1 == null) {
288                     expresult = returnCode1;
289                     break label0;
290                 }
291             }
292 
293             label1: {
294 
295                 if (expresult != returnCode0)
296                        break label1;
297 
298                 log2("     enabling breakpRequest1");
299                 breakpRequest1.enable();
300 
301                 log2("       forcing the main thread to leave synchronized block");
302                 pipe.println("continue");
303                 line = pipe.readln();
304                 if (!line.equals("docontinue")) {
305                     log3("ERROR: returned string is not 'docontinue'");
306                     expresult = returnCode4;
307                     break label1;
308                 }
309 
310                 log2("      getting a breakpoint event");
311                 expresult = breakpoint();
312                 if (expresult != returnCode0)
313                     break label1;
314 
315 
316                 Value returnvdValue1 = null;
317                 Value returnvdValue2 = null;
318                 List  invokeMethods  = null;
319                 Method invokeMethod  = null;
320                 List<Value> argumentList    = null;
321 
322 
323                 invokeMethods  =  testedclass.methodsByName("vdValue1");
324                 invokeMethod = (Method) invokeMethods.get(0);
325 
326                 argumentList = Collections.<Value>emptyList();
327 
328                 try {
329                     log2("      invoking equals001a.vdValue1();  no Exception expected");
330                     returnvdValue1 = thread2.invokeMethod(thread2,
331                                       invokeMethod, argumentList, 0);
332                 } catch ( Exception e ) {
333                      log3("ERROR: Exception: " + e);
334                      expresult = returnCode1;
335                      break label1;
336                 }
337 
338                 invokeMethods = testedclass.methodsByName("vdValue2");
339                 invokeMethod = (Method) invokeMethods.get(0);
340 
341                 try {
342                     log2("      invoking equals001a.vdValue2();  no Exception expected");
343                     returnvdValue2 = thread2.invokeMethod(thread2,
344                                      invokeMethod, argumentList, 0);
345                 } catch ( Exception e ) {
346                      log3("ERROR: Exception: " + e);
347                      expresult = returnCode1;
348                      break label1;
349                 }
350 
351 
352                 VoidValue vdValue1 = null;
353                 VoidValue vdValue2 = null;
354 
355                 log2("      checking up cast: vdValue1 = (VoidValue) returnvdValue1;");
356                 try {
357                     vdValue1 = (VoidValue) returnvdValue1;
358                 } catch ( ClassCastException e ) {
359                     log3("ERROR : ClassCastException");
360                     expresult = returnCode1;
361                     break label1;
362                 }
363 
364                 log2("      checking up cast: vdValue2 = (VoidValue) returnvdValue2;");
365                 try {
366                     vdValue2 = (VoidValue) returnvdValue2;
367                 } catch ( ClassCastException e ) {
368                     log3("ERROR : ClassCastException");
369                     expresult = returnCode1;
370                     break label1;
371                 }
372 
373                 log2("      checking up equality: vdValue1.equals(vdValue2)");
374                 if (!vdValue1.equals(vdValue2)) {
375                     log3("ERROR: !vdValue1.equals(vdValue2)");
376                     expresult = returnCode1;
377                 }
378 
379                 Object testObj = null;
380                 log2("      checking up equality: vdValue1.equals(null)");
381                 if (vdValue1.equals(testObj)) {
382                     log3("ERROR: vdValue1.equals(null)");
383                     expresult = returnCode1;
384                 }
385             }
386             log2("      resuming the vm");
387             vm.resume();
388             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
389             log2("     the end of testing");
390             if (expresult != returnCode0)
391                 testExitCode = FAILED;
392         }
393         log1("      TESTING ENDS");
394 
395     //--------------------------------------------------   test summary section
396     //-------------------------------------------------    standard end section
397 
398         pipe.println("quit");
399         log2("waiting for the debuggee to finish ...");
400         debuggee.waitFor();
401 
402         int status = debuggee.getStatus();
403         if (status != PASSED + PASS_BASE) {
404             log3("debuggee returned UNEXPECTED exit status: " +
405                    status + " != PASS_BASE");
406             testExitCode = FAILED;
407         } else {
408             log2("debuggee returned expected exit status: " +
409                    status + " == PASS_BASE");
410         }
411 
412         if (testExitCode != PASSED) {
413             logHandler.complain("TEST FAILED");
414         }
415         return testExitCode;
416     }
417 
418 
419 
420    /*
421     * private BreakpointRequest settingBreakpoint(String, String, String)
422     *
423     * It sets up a breakpoint within a given method at given line number
424     * for the thread2 only.
425     * Third parameter is required for any case in future debugging, as if.
426     *
427     * Return codes:
428     *  = BreakpointRequest object  in case of success
429     *  = null   in case of an Exception thrown within the method
430     */
431 
settingBreakpoint( String methodName, String bpLine, String property)432     private BreakpointRequest settingBreakpoint ( String methodName,
433                                                   String bpLine,
434                                                   String property) {
435 
436         log2("setting up a breakpoint: method: '" + methodName + "' line: " + bpLine );
437 
438         List              alllineLocations = null;
439         Location          lineLocation     = null;
440         BreakpointRequest breakpRequest    = null;
441 
442         try {
443             Method  method  = (Method) testedclass.methodsByName(methodName).get(0);
444 
445             alllineLocations = method.allLineLocations();
446 
447             int n =
448                 ( (IntegerValue) testedclass.getValue(testedclass.fieldByName(bpLine) ) ).value();
449             if (n > alllineLocations.size()) {
450                 log3("ERROR:  TEST_ERROR_IN_settingBreakpoint(): number is out of bound of method's lines");
451             } else {
452                 lineLocation = (Location) alllineLocations.get(n);
453                 try {
454                     breakpRequest = eventRManager.createBreakpointRequest(lineLocation);
455                     breakpRequest.putProperty("number", property);
456                     breakpRequest.addThreadFilter(thread2);
457                     breakpRequest.setSuspendPolicy( EventRequest.SUSPEND_EVENT_THREAD);
458                 } catch ( Exception e1 ) {
459                     log3("ERROR: inner Exception within settingBreakpoint() : " + e1);
460                     breakpRequest    = null;
461                 }
462             }
463         } catch ( Exception e2 ) {
464             log3("ERROR: ATTENTION:  outer Exception within settingBreakpoint() : " + e2);
465             breakpRequest    = null;
466         }
467 
468         if (breakpRequest == null)
469             log2("      A BREAKPOINT HAS NOT BEEN SET UP");
470         else
471             log2("      a breakpoint has been set up");
472 
473         return breakpRequest;
474     }
475 
476 
477     /*
478      * private int breakpoint ()
479      *
480      * It removes events from EventQueue until gets first BreakpointEvent.
481      * To get next EventSet value, it uses the method
482      *    EventQueue.remove(int timeout)
483      * The timeout argument passed to the method, is "waitTime*60000".
484      * Note: the value of waitTime is set up with
485      *       the method ArgumentHandler.getWaitTime() at the beginning of the test.
486      *
487      * Return codes:
488      *  = returnCode0 - success;
489      *  = returnCode2 - Exception when "eventSet = eventQueue.remove()" is executed
490      *  = returnCode3 - default case when loop of processing an event, that is,
491      *                  an unspecified event was taken from the EventQueue
492      */
493 
breakpoint()494     private int breakpoint () {
495 
496         int returnCode = returnCode0;
497 
498         log2("       waiting for BreakpointEvent");
499 
500         labelBP:
501             for (;;) {
502 
503                 log2("       new:  eventSet = eventQueue.remove();");
504                 try {
505                     eventSet = eventQueue.remove(waitTime*60000);
506                     if (eventSet == null) {
507                         log3("ERROR:  timeout for waiting for a BreakpintEvent");
508                         returnCode = returnCode3;
509                         break labelBP;
510                     }
511                 } catch ( Exception e ) {
512                     log3("ERROR: Exception for  eventSet = eventQueue.remove(); : " + e);
513                     returnCode = 1;
514                     break labelBP;
515                 }
516 
517                 if (eventSet != null) {
518 
519                     log2("     :  eventSet != null;  size == " + eventSet.size());
520 
521                     EventIterator eIter = eventSet.eventIterator();
522                     Event         ev    = null;
523 
524                     for (; eIter.hasNext(); ) {
525 
526                         if (returnCode != returnCode0)
527                             break;
528 
529                         ev = eIter.nextEvent();
530 
531                     ll: for (int ifor =0;  ; ifor++) {
532 
533                         try {
534                           switch (ifor) {
535 
536                           case 0:  AccessWatchpointEvent awe = (AccessWatchpointEvent) ev;
537                                    log2("      AccessWatchpointEvent removed");
538                                    break ll;
539                           case 1:  BreakpointEvent be = (BreakpointEvent) ev;
540                                    log2("      BreakpointEvent removed");
541                                    break labelBP;
542                           case 2:  ClassPrepareEvent cpe = (ClassPrepareEvent) ev;
543                                    log2("      ClassPreparEvent removed");
544                                    break ll;
545                           case 3:  ClassUnloadEvent cue = (ClassUnloadEvent) ev;
546                                    log2("      ClassUnloadEvent removed");
547                                    break ll;
548                           case 4:  ExceptionEvent ee = (ExceptionEvent) ev;
549                                    log2("      ExceptionEvent removed");
550                                    break ll;
551                           case 5:  MethodEntryEvent mene = (MethodEntryEvent) ev;
552                                    log2("      MethodEntryEvent removed");
553                                    break ll;
554                           case 6:  MethodExitEvent mexe = (MethodExitEvent) ev;
555                                    log2("      MethodExiEvent removed");
556                                    break ll;
557                           case 7:  ModificationWatchpointEvent mwe = (ModificationWatchpointEvent) ev;
558                                    log2("      ModificationWatchpointEvent removed");
559                                    break ll;
560                           case 8:  StepEvent se = (StepEvent) ev;
561                                    log2("      StepEvent removed");
562                                    break ll;
563                           case 9:  ThreadDeathEvent tde = (ThreadDeathEvent) ev;
564                                    log2("      ThreadDeathEvent removed");
565                                    break ll;
566                           case 10: ThreadStartEvent tse = (ThreadStartEvent) ev;
567                                    log2("      ThreadStartEvent removed");
568                                    break ll;
569                           case 11: VMDeathEvent vmde = (VMDeathEvent) ev;
570                                    log2("      VMDeathEvent removed");
571                                    break ll;
572                           case 12: VMStartEvent vmse = (VMStartEvent) ev;
573                                    log2("      VMStartEvent removed");
574                                    break ll;
575                           case 13: WatchpointEvent we = (WatchpointEvent) ev;
576                                    log2("      WatchpointEvent removed");
577                                    break ll;
578 
579                           default: log3("ERROR:  default case for casting event");
580                                    returnCode = returnCode3;
581                                    break ll;
582                           } // switch
583                         } catch ( ClassCastException e ) {
584                         }   // try
585                     }       // ll: for (int ifor =0;  ; ifor++)
586                 }           // for (; ev.hasNext(); )
587             }
588         }
589         if (returnCode == returnCode0)
590             log2("     :  eventSet == null:  EventQueue is empty");
591 
592         return returnCode;
593     }
594 
595 }
596