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 17.02.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.ReplyPacket;
31 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
32 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
33 
34 
35 /**
36  * JDWP Unit test for ReferenceType.Modifiers command.
37  */
38 public class ModifiersTest extends JDWPSyncTestCase {
39 
40     static final int testStatusPassed = 0;
41     static final int testStatusFailed = -1;
42     static final String thisCommandName = "ReferenceType.Modifiers command";
43     static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/share/debuggee/HelloWorld;";
44     static final String debuggeeInterfaceSignature = "Lorg/apache/harmony/jpda/tests/jdwp/share/debuggee/HelloWorldInterface;";
45     static final String debuggeeInterfaceClassName = "org.apache.harmony.jpda.tests.jdwp.share.debuggee.HelloWorldInterface";
46 
getDebuggeeClassName()47     protected String getDebuggeeClassName() {
48         return "org.apache.harmony.jpda.tests.jdwp.share.debuggee.HelloWorld";
49     }
50 
51     /**
52      * This testcase exercises ReferenceType.Modifiers command.
53      * <BR>The test starts HelloWorld debuggee, requests referenceTypeId
54      * for it by VirtualMachine.ClassesBySignature command, then
55      * performs ReferenceType.Modifiers command and checks that returned
56      * Modifiers contain expected flags: ACC_PUBLIC, ACC_SUPER;
57      * but do NOT contain flags: ACC_FINAL, ACC_INTERFACE, ACC_ABSTRACT
58      */
testModifiers001()59     public void testModifiers001() {
60         String thisTestName = "testModifiers001";
61         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
62         String failMessage = "";
63         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
64 
65         long refTypeID = getClassIDBySignature(debuggeeSignature);
66 
67         logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
68         logWriter.println("=> referenceTypeID for Debuggee class = " + refTypeID);
69         logWriter.println("=> CHECK1: send " + thisCommandName + " and check reply...");
70 
71         CommandPacket modifiersCommand = new CommandPacket(
72                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
73                 JDWPCommands.ReferenceTypeCommandSet.ModifiersCommand);
74         modifiersCommand.setNextValueAsReferenceTypeID(refTypeID);
75 
76         ReplyPacket modifiersReply = debuggeeWrapper.vmMirror.performCommand(modifiersCommand);
77         modifiersCommand = null;
78         checkReplyPacket(modifiersReply, thisCommandName);
79 
80         int returnedModifiers = modifiersReply.getNextValueAsInt();
81 /*
82  * The value of the access_flags item is a mask of modifiers used with class and
83  * interface declarations. The access_flags modifiers are:
84  * Flag Name      Value   Meaning                                               Used By
85  * ACC_PUBLIC     0x0001  Is public; may be accessed from outside its package.  Class, interface
86  * ACC_FINAL      0x0010  Is final; no subclasses allowed.                      Class
87  * ACC_SUPER      0x0020  Treat superclass methods specially in invokespecial.  Class, interface
88  * ACC_INTERFACE  0x0200  Is an interface.                                      Interface
89  * ACC_ABSTRACT   0x0400  Is abstract; may not be instantiated.                 Class, interface
90  */
91         logWriter.println("=> Returned modifiers = 0x" + Integer.toHexString(returnedModifiers));
92 
93         int publicFlag = 0x0001; // expected
94         int finalFlag = 0x0010; // unexpected
95         int superFlag = 0x0020; // expected
96         int interfaceFlag = 0x0200; // unexpected
97         int abstractFlag = 0x0400; // unexpected
98 
99         if ( (returnedModifiers & publicFlag) == 0 ) {
100             logWriter.println
101                 ("## CHECK1: FAILURE: Returned modifiers do NOT contain expected ACC_PUBLIC flag(0x0001)");
102             failMessage = failMessage +
103                 "Returned modifiers do NOT contain expected ACC_PUBLIC flag(0x0001);\n";
104         }
105         if ( (returnedModifiers & superFlag) == 0 ) {
106             logWriter.println
107                 ("## CHECK1: FAILURE: Returned modifiers do NOT contain expected ACC_SUPER flag(0x0020)");
108             failMessage = failMessage +
109                 "Returned modifiers do NOT contain expected ACC_SUPER flag(0x0020);\n";
110         }
111         if ( (returnedModifiers & finalFlag) != 0 ) {
112             logWriter.println
113                 ("## CHECK1: FAILURE: Returned modifiers contain unexpected ACC_FINAL flag(0x0010)");
114             failMessage = failMessage +
115                 "Returned modifiers contain unexpected ACC_FINAL flag(0x0010);\n";
116         }
117         if ( (returnedModifiers & interfaceFlag) != 0 ) {
118             logWriter.println
119                 ("## CHECK1: FAILURE: Returned modifiers contain unexpected ACC_INTERFACE flag(0x0200)");
120             failMessage = failMessage +
121                 "Returned modifiers contain unexpected ACC_INTERFACE flag(0x0200);\n";
122         }
123         if ( (returnedModifiers & abstractFlag) != 0 ) {
124             logWriter.println
125                 ("## CHECK1: FAILURE: Returned modifiers contain unexpected ACC_ABSTRACT flag(0x0400)");
126             failMessage = failMessage +
127                 "Returned modifiers contain unexpected ACC_ABSTRACT flag(0x0400);\n";
128         }
129 
130         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
131         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
132 
133         if (failMessage.length() > 0) {
134             fail(failMessage);
135         } else {
136             logWriter.println
137             ("=> CHECK1: PASSED: expected modifiers are returned: ACC_PUBLIC flag(0x0001), ACC_SUPER flag(0x0020)");
138         }
139 
140         assertAllDataRead(modifiersReply);
141     }
142 
143     /**
144      * This testcase exercises ReferenceType.Modifiers command.
145      * <BR>The test starts HelloWorld debuggee, requests referenceTypeId
146      * for an interface HelloWorldInterface by VirtualMachine.ClassesBySignature command,
147      * then performs ReferenceType.Modifiers command and checks that returned
148      * Modifiers contain expected flags: ACC_ABSTRACT, ACC_INTERFACE;
149      * but do NOT contain flags: ACC_PUBLIC, ACC_FINAL, ACC_SUPER, ACC_ABSTRACT
150      */
testModifiers002()151     public void testModifiers002() {
152         String thisTestName = "testModifiers002";
153         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
154         String failMessage = "";
155         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
156 
157         long refTypeID = getClassIDBySignature(debuggeeInterfaceSignature);
158 
159         logWriter.println("=> Debuggee Interface class = " + debuggeeInterfaceClassName);
160         logWriter.println("=> referenceTypeID for Debuggee interface = " + refTypeID);
161         logWriter.println("=> CHECK1: send " + thisCommandName + " and check reply...");
162 
163         CommandPacket modifiersCommand = new CommandPacket(
164                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
165                 JDWPCommands.ReferenceTypeCommandSet.ModifiersCommand);
166         modifiersCommand.setNextValueAsReferenceTypeID(refTypeID);
167 
168         ReplyPacket modifiersReply = debuggeeWrapper.vmMirror.performCommand(modifiersCommand);
169         modifiersCommand = null;
170         checkReplyPacket(modifiersReply, thisCommandName);
171 
172         int returnedModifiers = modifiersReply.getNextValueAsInt();
173 /*
174  * The value of the access_flags item is a mask of modifiers used with class and
175  * interface declarations. The access_flags modifiers are:
176  * Flag Name      Value   Meaning                                               Used By
177  * ACC_PUBLIC     0x0001  Is public; may be accessed from outside its package.  Class, interface
178  * ACC_FINAL      0x0010  Is final; no subclasses allowed.                      Class
179  * ACC_SUPER      0x0020  Treat superclass methods specially in invokespecial.  Class, interface
180  * ACC_INTERFACE  0x0200  Is an interface.                                      Interface
181  * ACC_ABSTRACT   0x0400  Is abstract; may not be instantiated.                 Class, interface
182  */
183         logWriter.println("=> Returned modifiers = 0x" + Integer.toHexString(returnedModifiers));
184 
185         int publicFlag = 0x0001; // expected
186         int finalFlag = 0x0010; // unexpected
187         int superFlag = 0x0020; // unexpected
188         int interfaceFlag = 0x0200; // expected
189         int abstractFlag = 0x0400; // unexpected
190 
191         if ( (returnedModifiers & publicFlag) != 0 ) {
192             logWriter.println
193                 ("## CHECK1: FAILURE: Returned modifiers contain unexpected ACC_PUBLIC flag(0x0001)");
194             failMessage = failMessage +
195                 "Returned modifiers contain unexpected ACC_PUBLIC flag(0x0001);\n";
196         }
197         if ( (returnedModifiers & superFlag) != 0 ) {
198             logWriter.println
199                 ("## CHECK1: FAILURE: Returned modifiers contain unexpected ACC_SUPER flag(0x0020)");
200             failMessage = failMessage +
201                 "Returned modifiers contain unexpected ACC_SUPER flag(0x0020);\n";
202         }
203         if ( (returnedModifiers & finalFlag) != 0 ) {
204             logWriter.println
205                 ("## CHECK1: FAILURE: Returned modifiers contain unexpected ACC_FINAL flag(0x0010)");
206             failMessage = failMessage +
207                 "Returned modifiers contain unexpected ACC_FINAL flag(0x0010);\n";
208         }
209         if ( (returnedModifiers & interfaceFlag) == 0 ) {
210             logWriter.println
211                 ("## CHECK1: FAILURE: Returned modifiers do not contain expected ACC_INTERFACE flag(0x0200)");
212             failMessage = failMessage +
213                 "Returned modifiers do not contain expected ACC_INTERFACE flag(0x0200);\n";
214         }
215         if ( (returnedModifiers & abstractFlag) == 0 ) {
216             logWriter.println
217                 ("## CHECK1: FAILURE: Returned modifiers do not contain expected ACC_ABSTRACT flag(0x0400)");
218             failMessage = failMessage +
219                 "Returned modifiers do not contain expected ACC_ABSTRACT flag(0x0400);\n";
220         }
221 
222         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
223         logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
224 
225         if (failMessage.length() > 0) {
226             fail(failMessage);
227         } else {
228             logWriter.println
229             ("=> CHECK1: PASSED: expected modifiers are returned: ACC_INTERFACE flag(0x0200), ACC_ABSTRACT flag(0x0400)");
230         }
231 
232         assertAllDataRead(modifiersReply);
233     }
234 }
235