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.ReferenceType.Interfaces;
 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 interfaces001 {
 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.ReferenceType.Interfaces";
 38     static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "interfaces001";
 39     static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";
 40 
 41     static final String JDWP_COMMAND_NAME = "ReferenceType.Interfaces";
 42     static final int JDWP_COMMAND_ID = JDWP.Command.ReferenceType.Interfaces;
 43 
 44     static final String TESTED_CLASS_NAME = DEBUGEE_CLASS_NAME + "$" + "TestedClass";
 45     static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";
 46 
 47     static final String class_interfaces [][] = {
 48                     { DEBUGEE_CLASS_NAME + "$" + "TestedClassInterface1", "" },
 49                     { DEBUGEE_CLASS_NAME + "$" + "TestedClassInterface2", "" }
 50                 };
 51     static final int DECLARED_INTERFACES = class_interfaces.length;
 52     static final long interfaceIDs[] = new long[DECLARED_INTERFACES];
 53 
 54     public static void main (String argv[]) {
 55         System.exit(run(argv,System.out) + JCK_STATUS_BASE);
 56     }
 57 
 58     public static int run(String argv[], PrintStream out) {
 59     return new interfaces001().runIt(argv, out);
 60     }
 61 
 62     public int runIt(String argv[], PrintStream out) {
 63 
 64         boolean success = true;
 65 
 66         try {
 67             ArgumentHandler argumentHandler = new ArgumentHandler(argv);
 68             Log log = new Log(out, argumentHandler);
 69 
 70             try {
 71 
 72                 Binder binder = new Binder(argumentHandler, log);
 73                 log.display("Start debugee VM");
 74                 Debugee debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);
 75                 Transport transport = debugee.getTransport();
 76                 IOPipe pipe = debugee.createIOPipe();
 77 
 78                 log.display("Waiting for VM_INIT event");
 79                 debugee.waitForVMInit();
 80 
 81                 log.display("Querying for IDSizes");
 82                 debugee.queryForIDSizes();
 83 
 84                 log.display("Resume debugee VM");
 85                 debugee.resume();
 86 
 87                 log.display("Waiting for command: " + "ready");
 88                 String cmd = pipe.readln();
 89                 log.display("Received command: " + cmd);
 90 
 91                 try {
 92 
 93                     log.display("Getting ReferenceTypeID for class signature: " + TESTED_CLASS_SIGNATURE);
 94                     long typeID = debugee.getReferenceTypeID(TESTED_CLASS_SIGNATURE);
 95 
 96                     for (int i = 0; i < DECLARED_INTERFACES; i++) {
 97                         class_interfaces[i][1] = "L" + class_interfaces[i][0].replace('.', '/') + ";";
 98                         log.display("Getting ReferenceTypeID for interface signature: " + class_interfaces[i][1]);
 99                         interfaceIDs[i] = debugee.getReferenceTypeID(class_interfaces[i][1]);
100                     }
101 
102                     // begin test of JDWP command
103 
104                     log.display("Create command " + JDWP_COMMAND_NAME
105                                 + " with ReferenceTypeID: " + typeID);
106                     CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);
107                     command.addReferenceTypeID(typeID);
108                     command.setLength();
109 
110                     log.display("Sending command packet:\n" + command);
111                     transport.write(command);
112 
113                     log.display("Waiting for reply packet");
114                     ReplyPacket reply = new ReplyPacket();
115                     transport.read(reply);
116                     log.display("Reply packet received:\n" + reply);
117 
118                     log.display("Checking reply packet header");
119                     reply.checkHeader(command.getPacketID());
120 
121                     log.display("Parsing reply packet:");
122                     reply.resetPosition();
123 
124                     int interfaces = reply.getInt();
125                     log.display("  interfaces: " + interfaces);
126 
127                     if (interfaces != DECLARED_INTERFACES) {
128                         log.complain("Unexpected number of declared interfaces in the reply packet:" + interfaces
129                                     + " (expected: " + DECLARED_INTERFACES + ")");
130                         success = false;
131                     }
132 
133                     for (int i = 0; i < interfaces; i++ ) {
134 
135                         log.display("  interface #" + i);
136 
137                         long interfaceID = reply.getReferenceTypeID();
138                         log.display("    interfaceID: " + interfaceID);
139 
140                         if (interfaceID != interfaceIDs[i]) {
141                             log.complain("Unexpected interface ID for interface #" + i + " in the reply packet: " + interfaceID
142                                         + " (expected: " + interfaceIDs[i] + ")");
143                             success = false;
144                         }
145 
146                     }
147 
148                     if (! reply.isParsed()) {
149                         log.complain("Extra trailing bytes found in reply packet at: " + reply.currentPosition());
150                         success = false;
151                     } else {
152                         log.display("Reply packet parsed successfully");
153                     }
154 
155                     // end test of JDWP command
156 
157                 } catch (Exception e) {
158                     log.complain("Caught exception while testing JDWP command: " + e);
159                     success = false;
160                 } finally {
161                     log.display("Sending command: " + "quit");
162                     pipe.println("quit");
163 
164                     log.display("Waiting for debugee exits");
165                     int code = debugee.waitFor();
166                     if (code == JCK_STATUS_BASE + PASSED) {
167                         log.display("Debugee PASSED with exit code: " + code);
168                     } else {
169                         log.complain("Debugee FAILED with exit code: " + code);
170                         success = false;
171                     }
172                 }
173 
174             } catch (Exception e) {
175                 log.complain("Caught unexpected exception while communicating with debugee: " + e);
176                 e.printStackTrace(out);
177                 success = false;
178             }
179 
180             if (!success) {
181                 log.complain("TEST FAILED");
182                 return FAILED;
183             }
184 
185         } catch (Exception e) {
186             out.println("Caught unexpected exception while starting the test: " + e);
187             e.printStackTrace(out);
188             out.println("TEST FAILED");
189             return FAILED;
190         }
191 
192         out.println("TEST PASSED");
193         return PASSED;
194 
195     }
196 
197 }