1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 /**
20  * @author Anatoly F. Bondarenko
21  */
22 
23 /**
24  * Created on 04.03.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.ReferenceType;
27 
28 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
29 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
30 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
31 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32 import org.apache.harmony.jpda.tests.framework.jdwp.Value;
33 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
34 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
35 
36 
37 /**
38  * JDWP Unit test for ReferenceType..Signature command with incorrect ReferenceTypeIDs.
39  */
40 public class Signature002Test extends JDWPSyncTestCase {
41 
42     static final String thisCommandName = "ReferenceType.Signature command";
43     static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/Signature002Debuggee;";
44 
getDebuggeeClassName()45     protected String getDebuggeeClassName() {
46         return "org.apache.harmony.jpda.tests.jdwp.ReferenceType.Signature002Debuggee";
47     }
48 
49     /**
50      * This testcase exercises ReferenceType.Signature command with incorrect ReferenceTypeIDs.
51      * <BR>The test starts >Signature002Debuggee class, gets objectID
52      * as value of static field of this class which (field) represents checked object.
53      * Then the test performs three variants of ReferenceType.Signature commands
54      * and checks that commands return:
55      * <BR>&nbsp;&nbsp; - JDWP_ERROR_INVALID_CLASS, if objectID is passed as ReferenceTypeID;
56      * <BR>&nbsp;&nbsp; - JDWP_ERROR_INVALID_OBJECT, if fieldID is passed as ReferenceTypeID;
57      * <BR>&nbsp;&nbsp; - JDWP_ERROR_INVALID_OBJECT, if unknown ID is passed as ReferenceTypeID;
58      */
testSignature002()59     public void testSignature002() {
60         String thisTestName = "testSignature002";
61         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
62         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
63         finalSyncMessage = "TO_FINISH";
64 
65         long debuggeeRefTypeID = getClassIDBySignature(debuggeeSignature);
66 
67         logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
68         logWriter.println("=> referenceTypeID for Debuggee class = " + debuggeeRefTypeID);
69 
70         long checkedFieldID = checkField(debuggeeRefTypeID, "checkedObject");
71 
72         logWriter.println
73         ("=> Send ReferenceType::GetValues command for received fieldID and get ObjectID to check...");
74 
75         CommandPacket getValuesCommand = new CommandPacket(
76                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
77                 JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
78         getValuesCommand.setNextValueAsReferenceTypeID(debuggeeRefTypeID);
79         getValuesCommand.setNextValueAsInt(1);
80         getValuesCommand.setNextValueAsFieldID(checkedFieldID);
81 
82         ReplyPacket getValuesReply = debuggeeWrapper.vmMirror.performCommand(getValuesCommand);
83         getValuesCommand = null;
84         checkReplyPacket(getValuesReply, "ReferenceType::GetValues command");
85 
86         int returnedValuesNumber = getValuesReply.getNextValueAsInt();
87         logWriter.println("=> Returned values number = " + returnedValuesNumber);
88         if ( returnedValuesNumber != 1 ) {
89             logWriter.println
90             ("\n## FAILURE: ReferenceType::GetValues command returned unexpected number of values:");
91             logWriter.println("## Expected number = 1");
92             logWriter.println("## Returned number = " + returnedValuesNumber);
93             logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
94             synchronizer.sendMessage("TO_FINISH");
95             assertEquals("ReferenceType::GetValues command returned unexpected number of values,",
96                     1, returnedValuesNumber);
97         }
98 
99         Value checkedObjectFieldValue = getValuesReply.getNextValueAsValue();
100         byte checkedObjectFieldTag = checkedObjectFieldValue.getTag();
101         logWriter.println("=> Returned field value tag for checked object= " + checkedObjectFieldTag
102             + "(" + JDWPConstants.Tag.getName(checkedObjectFieldTag) + ")");
103         if ( checkedObjectFieldTag != JDWPConstants.Tag.OBJECT_TAG ) {
104             finalSyncMessage = "TO_FINISH";
105             printErrorAndFail(
106             "ReferenceType::GetValues command returned unexpected tag: " +
107             checkedObjectFieldTag + "(" +
108             JDWPConstants.Tag.getName(checkedObjectFieldTag) + ")" +
109             ", Expected tag = " + JDWPConstants.Tag.OBJECT_TAG + "(OBJECT_TAG)");
110         }
111 
112         long checkedObjectID = checkedObjectFieldValue.getLongValue();
113         logWriter.println("=> Returned checked ObjectID = " + checkedObjectID);
114 
115         logWriter.println
116             ("\n=> CHECK: send " + thisCommandName + " for checked ObjectID: INVALID_CLASS is expected...");
117 
118         CommandPacket checkedCommand = new CommandPacket(
119                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
120                 JDWPCommands.ReferenceTypeCommandSet.SignatureCommand);
121         checkedCommand.setNextValueAsReferenceTypeID(checkedObjectID);
122 
123         ReplyPacket checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
124         checkedCommand = null;
125 
126         short errorCode = checkedReply.getErrorCode();
127         if ( errorCode != JDWPConstants.Error.NONE ) {
128             if ( errorCode != JDWPConstants.Error.INVALID_CLASS ) {
129                 logWriter.println("## CHECK: FAILURE: " +  thisCommandName
130                     + " returns unexpected ERROR = " + errorCode
131                     + "(" + JDWPConstants.Error.getName(errorCode) + ")");
132                 fail(thisCommandName
133                         + " returns unexpected ERROR = " + errorCode
134                         + "(" + JDWPConstants.Error.getName(errorCode) + ")");
135             } else {
136                 logWriter.println("=> CHECK PASSED: Expected error (INVALID_CLASS) is returned");
137             }
138         } else {
139             logWriter.println
140             ("\n## FAILURE: " + thisCommandName + " does NOT return expected error - INVALID_CLASS");
141             fail(thisCommandName + " does NOT return expected error - INVALID_CLASS");
142         }
143 
144         logWriter.println
145         ("\n=> CHECK: send " + thisCommandName + " for checked fieldID: INVALID_OBJECT is expected...");
146 
147         checkedCommand = new CommandPacket(
148                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
149                 JDWPCommands.ReferenceTypeCommandSet.SignatureCommand);
150         checkedCommand.setNextValueAsReferenceTypeID(checkedFieldID);
151 
152         checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
153         checkedCommand = null;
154 
155         errorCode = checkedReply.getErrorCode();
156         if ( errorCode != JDWPConstants.Error.NONE ) {
157             if ( errorCode != JDWPConstants.Error.INVALID_OBJECT ) {
158                 logWriter.println("## CHECK: FAILURE: " +  thisCommandName
159                     + " returns unexpected ERROR = " + errorCode
160                     + "(" + JDWPConstants.Error.getName(errorCode) + ")");
161                 fail(thisCommandName
162                         + " returns unexpected ERROR = " + errorCode
163                         + "(" + JDWPConstants.Error.getName(errorCode) + ")");
164             } else {
165                 logWriter.println("=> CHECK PASSED: Expected error (INVALID_OBJECT) is returned");
166             }
167         } else {
168             logWriter.println
169             ("\n## FAILURE: " + thisCommandName + " does NOT return expected error - INVALID_OBJECT");
170             fail(thisCommandName + " does NOT return expected error - INVALID_OBJECT");
171         }
172 
173         logWriter.println
174         ("\n=> CHECK: send " + thisCommandName + " for unknown ID: INVALID_OBJECT is expected...");
175 
176         long unknownID = debuggeeRefTypeID + 100;
177         logWriter.println("=> unknown ID = " + unknownID);
178         checkedCommand = new CommandPacket(
179                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
180                 JDWPCommands.ReferenceTypeCommandSet.SignatureCommand);
181         checkedCommand.setNextValueAsReferenceTypeID(unknownID);
182 
183         checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
184         checkedCommand = null;
185 
186         errorCode = checkedReply.getErrorCode();
187         if ( errorCode != JDWPConstants.Error.NONE ) {
188             if ( errorCode != JDWPConstants.Error.INVALID_OBJECT ) {
189                 logWriter.println("## CHECK: FAILURE: " +  thisCommandName
190                     + " returns unexpected ERROR = " + errorCode
191                     + "(" + JDWPConstants.Error.getName(errorCode) + ")");
192                 fail(thisCommandName
193                         + " returns unexpected ERROR = " + errorCode
194                         + "(" + JDWPConstants.Error.getName(errorCode) + ")");
195             } else {
196                 logWriter.println("=> CHECK PASSED: Expected error (INVALID_OBJECT) is returned");
197             }
198         } else {
199             logWriter.println
200             ("\n## FAILURE: " + thisCommandName + " does NOT return expected error - INVALID_OBJECT");
201             fail(thisCommandName + " does NOT return expected error - INVALID_OBJECT");
202         }
203 
204         finalSyncMessage = null;
205         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
206         logWriter.println("\n==> " + thisTestName + " for " + thisCommandName + ": FINISH");
207     }
208 }
209