dex_file_test.cc revision 414af10d719603fb4d8d972f5a022c17957b44e1
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "dex_file.h" 18 19#include "UniquePtr.h" 20#include "common_test.h" 21 22namespace art { 23 24class DexFileTest : public CommonTest {}; 25 26TEST_F(DexFileTest, Open) { 27 ScopedObjectAccess soa(Thread::Current()); 28 const DexFile* dex(OpenTestDexFile("Nested")); 29 ASSERT_TRUE(dex != NULL); 30} 31 32// Although this is the same content logically as the Nested test dex, 33// the DexFileHeader test is sensitive to subtle changes in the 34// contents due to the checksum etc, so we embed the exact input here. 35// 36// class Nested { 37// class Inner { 38// } 39// } 40static const char kRawDex[] = 41 "ZGV4CjAzNQAQedgAe7gM1B/WHsWJ6L7lGAISGC7yjD2IAwAAcAAAAHhWNBIAAAAAAAAAAMQCAAAP" 42 "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAAMAAADoAAAAAgAAAAABAABIAgAAQAEAAK4B" 43 "AAC2AQAAvQEAAM0BAADXAQAA+wEAABsCAAA+AgAAUgIAAF8CAABiAgAAZgIAAHMCAAB5AgAAgQIA" 44 "AAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABgAAAAAAAAAKAAAABgAAAKgBAAAAAAEA" 45 "DQAAAAAAAQAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAIAAAAiAEAAKsCAAAA" 46 "AAAAAQAAAAAAAAAFAAAAAAAAAAgAAACYAQAAuAIAAAAAAAACAAAAlAIAAJoCAAABAAAAowIAAAIA" 47 "AgABAAAAiAIAAAYAAABbAQAAcBACAAAADgABAAEAAQAAAI4CAAAEAAAAcBACAAAADgBAAQAAAAAA" 48 "AAAAAAAAAAAATAEAAAAAAAAAAAAAAAAAAAEAAAABAAY8aW5pdD4ABUlubmVyAA5MTmVzdGVkJElu" 49 "bmVyOwAITE5lc3RlZDsAIkxkYWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2" 50 "aWsvYW5ub3RhdGlvbi9Jbm5lckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNz" 51 "ZXM7ABJMamF2YS9sYW5nL09iamVjdDsAC05lc3RlZC5qYXZhAAFWAAJWTAALYWNjZXNzRmxhZ3MA" 52 "BG5hbWUABnRoaXMkMAAFdmFsdWUAAgEABw4AAQAHDjwAAgIBDhgBAgMCCwQADBcBAgQBDhwBGAAA" 53 "AQEAAJAgAICABNQCAAABAAGAgATwAgAAEAAAAAAAAAABAAAAAAAAAAEAAAAPAAAAcAAAAAIAAAAH" 54 "AAAArAAAAAMAAAACAAAAyAAAAAQAAAABAAAA4AAAAAUAAAADAAAA6AAAAAYAAAACAAAAAAEAAAMQ" 55 "AAACAAAAQAEAAAEgAAACAAAAVAEAAAYgAAACAAAAiAEAAAEQAAABAAAAqAEAAAIgAAAPAAAArgEA" 56 "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA=="; 57 58static const DexFile* OpenDexFileBase64(const char* base64, 59 const std::string& location) { 60 // decode base64 61 CHECK(base64 != NULL); 62 size_t length; 63 UniquePtr<byte[]> dex_bytes(DecodeBase64(base64, &length)); 64 CHECK(dex_bytes.get() != NULL); 65 66 // write to provided file 67 UniquePtr<File> file(OS::CreateEmptyFile(location.c_str())); 68 CHECK(file.get() != NULL); 69 if (!file->WriteFully(dex_bytes.get(), length)) { 70 PLOG(FATAL) << "Failed to write base64 as dex file"; 71 } 72 file.reset(); 73 74 // read dex file 75 ScopedObjectAccess soa(Thread::Current()); 76 const DexFile* dex_file = DexFile::Open(location, location); 77 CHECK(dex_file != NULL); 78 return dex_file; 79} 80 81TEST_F(DexFileTest, Header) { 82 ScratchFile tmp; 83 UniquePtr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename())); 84 ASSERT_TRUE(raw.get() != NULL); 85 86 const DexFile::Header& header = raw->GetHeader(); 87 // TODO: header.magic_ 88 EXPECT_EQ(0x00d87910U, header.checksum_); 89 // TODO: header.signature_ 90 EXPECT_EQ(904U, header.file_size_); 91 EXPECT_EQ(112U, header.header_size_); 92 EXPECT_EQ(0U, header.link_size_); 93 EXPECT_EQ(0U, header.link_off_); 94 EXPECT_EQ(15U, header.string_ids_size_); 95 EXPECT_EQ(112U, header.string_ids_off_); 96 EXPECT_EQ(7U, header.type_ids_size_); 97 EXPECT_EQ(172U, header.type_ids_off_); 98 EXPECT_EQ(2U, header.proto_ids_size_); 99 EXPECT_EQ(200U, header.proto_ids_off_); 100 EXPECT_EQ(1U, header.field_ids_size_); 101 EXPECT_EQ(224U, header.field_ids_off_); 102 EXPECT_EQ(3U, header.method_ids_size_); 103 EXPECT_EQ(232U, header.method_ids_off_); 104 EXPECT_EQ(2U, header.class_defs_size_); 105 EXPECT_EQ(256U, header.class_defs_off_); 106 EXPECT_EQ(584U, header.data_size_); 107 EXPECT_EQ(320U, header.data_off_); 108 109 EXPECT_EQ(header.checksum_, raw->GetLocationChecksum()); 110} 111 112TEST_F(DexFileTest, GetLocationChecksum) { 113 ScopedObjectAccess soa(Thread::Current()); 114 const DexFile* raw(OpenTestDexFile("Main")); 115 EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum()); 116} 117 118TEST_F(DexFileTest, GetChecksum) { 119 uint32_t checksum; 120 ScopedObjectAccess soa(Thread::Current()); 121 EXPECT_TRUE(DexFile::GetChecksum(GetLibCoreDexFileName(), checksum)); 122 EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksum); 123} 124 125TEST_F(DexFileTest, ClassDefs) { 126 ScopedObjectAccess soa(Thread::Current()); 127 const DexFile* raw(OpenTestDexFile("Nested")); 128 ASSERT_TRUE(raw != NULL); 129 EXPECT_EQ(2U, raw->NumClassDefs()); 130 131 const DexFile::ClassDef& c0 = raw->GetClassDef(0); 132 EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0)); 133 134 const DexFile::ClassDef& c1 = raw->GetClassDef(1); 135 EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1)); 136} 137 138TEST_F(DexFileTest, CreateMethodSignature) { 139 ScopedObjectAccess soa(Thread::Current()); 140 const DexFile* raw(OpenTestDexFile("CreateMethodSignature")); 141 ASSERT_TRUE(raw != NULL); 142 EXPECT_EQ(1U, raw->NumClassDefs()); 143 144 const DexFile::ClassDef& class_def = raw->GetClassDef(0); 145 ASSERT_STREQ("LCreateMethodSignature;", raw->GetClassDescriptor(class_def)); 146 147 const byte* class_data = raw->GetClassData(class_def); 148 ASSERT_TRUE(class_data != NULL); 149 ClassDataItemIterator it(*raw, class_data); 150 151 EXPECT_EQ(1u, it.NumDirectMethods()); 152 153 // Check the signature for the static initializer. 154 { 155 ASSERT_EQ(1U, it.NumDirectMethods()); 156 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); 157 uint32_t proto_idx = method_id.proto_idx_; 158 const char* name = raw->StringDataByIdx(method_id.name_idx_); 159 ASSERT_STREQ("<init>", name); 160 int32_t length; 161 std::string signature(raw->CreateMethodSignature(proto_idx, &length)); 162 ASSERT_EQ("()V", signature); 163 } 164 165 // Check both virtual methods. 166 ASSERT_EQ(2U, it.NumVirtualMethods()); 167 { 168 it.Next(); 169 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); 170 171 const char* name = raw->StringDataByIdx(method_id.name_idx_); 172 ASSERT_STREQ("m1", name); 173 174 uint32_t proto_idx = method_id.proto_idx_; 175 int32_t length; 176 std::string signature(raw->CreateMethodSignature(proto_idx, &length)); 177 ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", signature); 178 } 179 180 { 181 it.Next(); 182 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); 183 184 const char* name = raw->StringDataByIdx(method_id.name_idx_); 185 ASSERT_STREQ("m2", name); 186 187 uint32_t proto_idx = method_id.proto_idx_; 188 int32_t length; 189 std::string signature(raw->CreateMethodSignature(proto_idx, &length)); 190 ASSERT_EQ("(ZSC)LCreateMethodSignature;", signature); 191 } 192} 193 194TEST_F(DexFileTest, FindStringId) { 195 ScopedObjectAccess soa(Thread::Current()); 196 const DexFile* raw(OpenTestDexFile("CreateMethodSignature")); 197 ASSERT_TRUE(raw != NULL); 198 EXPECT_EQ(1U, raw->NumClassDefs()); 199 200 const char* strings[] = { "LCreateMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;", 201 "D", "I", "J", NULL }; 202 for (size_t i = 0; strings[i] != NULL; i++) { 203 const char* str = strings[i]; 204 const DexFile::StringId* str_id = raw->FindStringId(str); 205 const char* dex_str = raw->GetStringData(*str_id); 206 EXPECT_STREQ(dex_str, str); 207 } 208} 209 210TEST_F(DexFileTest, FindTypeId) { 211 for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) { 212 const char* type_str = java_lang_dex_file_->StringByTypeIdx(i); 213 const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str); 214 ASSERT_TRUE(type_str_id != NULL); 215 uint32_t type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id); 216 const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx); 217 ASSERT_TRUE(type_id != NULL); 218 EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id), i); 219 } 220} 221 222TEST_F(DexFileTest, FindProtoId) { 223 for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) { 224 const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i); 225 const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find); 226 std::vector<uint16_t> to_find_types; 227 if (to_find_tl != NULL) { 228 for (size_t j = 0; j < to_find_tl->Size(); j++) { 229 to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_); 230 } 231 } 232 const DexFile::ProtoId* found = 233 java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types); 234 ASSERT_TRUE(found != NULL); 235 EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i); 236 } 237} 238 239TEST_F(DexFileTest, FindMethodId) { 240 for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) { 241 const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i); 242 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); 243 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); 244 const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_); 245 const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature); 246 int32_t length; 247 ASSERT_TRUE(found != NULL) << "Didn't find method " << i << ": " 248 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." 249 << java_lang_dex_file_->GetStringData(name) 250 << java_lang_dex_file_->CreateMethodSignature(to_find.proto_idx_, &length); 251 EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i); 252 } 253} 254 255TEST_F(DexFileTest, FindFieldId) { 256 for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) { 257 const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i); 258 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); 259 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); 260 const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_); 261 const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type); 262 ASSERT_TRUE(found != NULL) << "Didn't find field " << i << ": " 263 << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " " 264 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." 265 << java_lang_dex_file_->GetStringData(name); 266 EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i); 267 } 268} 269 270} // namespace art 271