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 package org.apache.harmony.jpda.tests.jdwp.ReferenceType; 21 22 import java.io.BufferedInputStream; 23 import java.io.FileInputStream; 24 25 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket; 26 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands; 27 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket; 28 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase; 29 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer; 30 31 32 /** 33 * JDWP Unit test for ReferenceType.ConstantPool command. 34 */ 35 public class ConstantPoolTest extends JDWPSyncTestCase { 36 37 static final int testStatusPassed = 0; 38 39 static final int testStatusFailed = -1; 40 41 static final String thisCommandName = "ReferenceType.ConstantPool command"; 42 43 static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/ConstantPoolDebuggee;"; 44 45 static final String debuggeeClass = "org/apache/harmony/jpda/tests/jdwp/ReferenceType/ConstantPoolDebuggee.class"; 46 47 getDebuggeeClassName()48 protected String getDebuggeeClassName() { 49 return "org.apache.harmony.jpda.tests.jdwp.ReferenceType.ConstantPoolDebuggee"; 50 } 51 52 /** 53 * This testcase exercises ReferenceType.ConstantPool command. 54 * <BR>The test starts ConstantPoolDebuggee class, requests referenceTypeId 55 * for this class by VirtualMachine.ClassesBySignature command, then 56 * performs ReferenceType.ConstantPool command and checks that returned 57 * constant entry count and costant pool bytes are expected. 58 */ testConstantPool001()59 public void testConstantPool001() { 60 String thisTestName = "testConstantPool001"; 61 62 // Check capability, relevant for this test 63 logWriter.println("=> Check capability: canGetConstantPool"); 64 debuggeeWrapper.vmMirror.capabilities(); 65 boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canGetConstantPool; 66 if (!isCapability) { 67 logWriter 68 .println("##WARNING: this VM dosn't possess capability: canGetConstantPool"); 69 return; 70 } 71 72 logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START..."); 73 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY); 74 75 // Compose ConstantPool command 76 long refTypeID = getClassIDBySignature(debuggeeSignature); 77 78 logWriter.println("=> Debuggee class = " + getDebuggeeClassName()); 79 logWriter.println("=> referenceTypeID for Debuggee class = " + refTypeID); 80 logWriter.println("=> CHECK: send " + thisCommandName + " and check reply..."); 81 82 CommandPacket ConstantPoolCommand = new CommandPacket( 83 JDWPCommands.ReferenceTypeCommandSet.CommandSetID, 84 JDWPCommands.ReferenceTypeCommandSet.ConstantPoolCommand); 85 ConstantPoolCommand.setNextValueAsReferenceTypeID(refTypeID); 86 87 // Perform ConstantPool command and get reply package 88 ReplyPacket ConstantPoolReply = debuggeeWrapper.vmMirror.performCommand(ConstantPoolCommand); 89 ConstantPoolCommand = null; 90 checkReplyPacket(ConstantPoolReply, thisCommandName); 91 92 // Attain entry count and constant pool byte size from reply package 93 int returnedEntryCount = ConstantPoolReply.getNextValueAsInt(); 94 int returnedCpByteCount = ConstantPoolReply.getNextValueAsInt(); 95 96 // Attain entry count and constant pool content from class file 97 98 // Read constant pool content from class file 99 // length = magic num(4b) + major(2b) + minor(2b) + entry count(2b) + returnedCpByteCount 100 int length = 10 + returnedCpByteCount; 101 byte[] bytes = new byte[length]; 102 103 try { 104 BufferedInputStream in = new BufferedInputStream(new FileInputStream(debuggeeClass)); 105 int count = 0; 106 int index = 0; 107 while(length != 0){ 108 count = in.read(bytes,index,length); 109 index += count; 110 length -= count; 111 } 112 } catch (Exception e) { 113 printErrorAndFail(thisCommandName + "has error in reading target class file!"); 114 } 115 116 // Entry count is placed in byte 8 and byte 9 of class file 117 short expectedEntryCount = (short)(bytes[8] << 8 | bytes[9]); 118 119 // Compare entry count 120 assertEquals(thisCommandName + "returned invalid entry count,", expectedEntryCount, returnedEntryCount, null, null); 121 logWriter.println("=> CHECK: PASSED: expected entry count is returned:"); 122 logWriter.println("=> Signature = " + returnedEntryCount); 123 124 int startIndex = 10; 125 // Compare constant pool content 126 for (int i = 0; i < returnedCpByteCount; i++){ 127 byte returnedCpByte = ConstantPoolReply.getNextValueAsByte(); 128 assertEquals(thisCommandName + "returned invalid entry count,", bytes[startIndex+i], returnedCpByte, null, null); 129 } 130 131 logWriter.println("=> CHECK: PASSED: expected constant pool bytes are returned:"); 132 logWriter.println("=> Constant Pool Byte Count = " + returnedCpByteCount); 133 134 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 135 logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH"); 136 137 assertAllDataRead(ConstantPoolReply); 138 } 139 } 140