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.jdwp.VirtualMachine.AllClasses;
25 
26 import java.io.*;
27 import java.util.*;
28 
29 import nsk.share.*;
30 import nsk.share.jpda.*;
31 import nsk.share.jdwp.*;
32 
33 public class allclasses001 {
34     static final int JCK_STATUS_BASE = 95;
35     static final int PASSED = 0;
36     static final int FAILED = 2;
37     static final String PACKAGE_NAME = "nsk.jdwp.VirtualMachine.AllClasses";
38     static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "allclasses001";
39     static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";
40 
41     static final String JDWP_COMMAND_NAME = "VirtualMachine.AllClasses";
42     static final int JDWP_COMMAND_ID = JDWP.Command.VirtualMachine.AllClasses;
43 
44     static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME;
45 
main(String argv[])46     public static void main (String argv[]) {
47         System.exit(run(argv,System.out) + JCK_STATUS_BASE);
48     }
49 
run(String argv[], PrintStream out)50     public static int run(String argv[], PrintStream out) {
51     return new allclasses001().runIt(argv, out);
52     }
53 
runIt(String argv[], PrintStream out)54     public int runIt(String argv[], PrintStream out) {
55 
56         boolean success = true;
57 
58         try {
59             ArgumentHandler argumentHandler = new ArgumentHandler(argv);
60             Log log = new Log(out, argumentHandler);
61 
62             try {
63 
64                 Binder binder = new Binder(argumentHandler, log);
65                 log.display("Start debugee VM");
66                 Debugee debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);
67                 Transport transport = debugee.getTransport();
68                 IOPipe pipe = debugee.createIOPipe();
69 
70                 log.display("Waiting for VM_INIT event");
71                 debugee.waitForVMInit();
72 
73                 log.display("Querying for IDSizes");
74                 debugee.queryForIDSizes();
75 
76                 log.display("Resume debugee VM");
77                 debugee.resume();
78 
79                 log.display("Waiting for command: " + "ready");
80                 String cmd = pipe.readln();
81                 log.display("Received command: " + cmd);
82 
83                 String classSignature = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";
84 
85                 // begin test of JDWP command
86 
87                 try {
88                     CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);
89 
90                     log.display("Sending command packet:\n" + command);
91                     transport.write(command);
92 
93                     log.display("Waiting for reply packet");
94                     ReplyPacket reply = new ReplyPacket();
95                     transport.read(reply);
96                     log.display("Reply packet received:\n" + reply);
97 
98                     log.display("Checking reply packet header");
99                     reply.checkHeader(command.getPacketID());
100 
101                     log.display("Parsing reply packet:");
102                     reply.resetPosition();
103 
104                     int classes = reply.getInt();
105                     log.display("  classes: " + classes);
106 
107                     int found = 0;
108                     for (int i = 0; i < classes; i++) {
109 
110                         byte refTypeTag = reply.getByte();
111                         log.display("  " + i + " refTypeTag: " + refTypeTag);
112 
113                         long typeID = reply.getReferenceTypeID();
114                         log.display("  " + i + " typeID:     " + typeID);
115 
116                         String signature = reply.getString();
117                         log.display("  " + i + " signature:  " + signature);
118 
119                         int status = reply.getInt();
120                         log.display("  " + i + " status:     " + status);
121 
122                         if (signature.equals(classSignature)) {
123                             found++;
124                             log.display("FOUND: expected class signature: " + found + " time");
125                         }
126                     }
127 
128                     if (! reply.isParsed()) {
129                         log.complain("Extra bytes in reply packet at: " + reply.currentPosition());
130                         success = false;
131                     }
132 
133                     if (classes < 0) {
134                         log.complain("Negative number of returned classes: " + classes);
135                         success = false;
136                     }
137 
138                     if (classes == 0) {
139                         log.complain("No class returned");
140                         success = false;
141                     }
142 
143                     if (found <= 0) {
144                         log.complain("No expected class signature found: " + classSignature);
145                         success = false;
146                     }
147 
148                     if (found > 1) {
149                         log.complain("Too many classes (" + found + ") found for signature: " + classSignature);
150                         success = false;
151                     }
152 
153                 } catch (Exception e) {
154                     log.complain("Exception catched: " + e);
155                     success = false;
156                 }
157 
158                 // end test of JDWP command
159 
160                 log.display("Sending command: " + "quit");
161                 pipe.println("quit");
162 
163                 log.display("Waiting for debugee exits");
164                 int code = debugee.waitFor();
165                 if (code == JCK_STATUS_BASE + PASSED) {
166                     log.display("Debugee PASSED: " + code);
167                 } else {
168                     log.complain("Debugee FAILED: " + code);
169                     success = false;
170                 }
171 
172             } catch (Exception e) {
173                 log.complain("Unexpected exception: " + e);
174                 e.printStackTrace(out);
175                 success = false;
176             }
177 
178             if (!success) {
179                 log.complain("TEST FAILED");
180                 return FAILED;
181             }
182 
183         } catch (Exception e) {
184             out.println("Unexpected exception: " + e);
185             e.printStackTrace(out);
186             out.println("TEST FAILED");
187             return FAILED;
188         }
189 
190         out.println("TEST PASSED");
191         return PASSED;
192 
193     }
194 
195 }
196