Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "dex_file.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 19 | #include "UniquePtr.h" |
| 20 | #include "common_test.h" |
| 21 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 22 | namespace art { |
| 23 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 24 | class DexFileTest : public CommonTest {}; |
| 25 | |
| 26 | TEST_F(DexFileTest, Open) { |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 27 | const DexFile* dex(OpenTestDexFile("Nested")); |
| 28 | ASSERT_TRUE(dex != NULL); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 29 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 30 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 31 | // Although this is the same content logically as the Nested test dex, |
| 32 | // the DexFileHeader test is sensitive to subtle changes in the |
| 33 | // contents due to the checksum etc, so we embed the exact input here. |
| 34 | // |
| 35 | // class Nested { |
| 36 | // class Inner { |
| 37 | // } |
| 38 | // } |
| 39 | static const char kRawDex[] = |
| 40 | "ZGV4CjAzNQAQedgAe7gM1B/WHsWJ6L7lGAISGC7yjD2IAwAAcAAAAHhWNBIAAAAAAAAAAMQCAAAP" |
| 41 | "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAAMAAADoAAAAAgAAAAABAABIAgAAQAEAAK4B" |
| 42 | "AAC2AQAAvQEAAM0BAADXAQAA+wEAABsCAAA+AgAAUgIAAF8CAABiAgAAZgIAAHMCAAB5AgAAgQIA" |
| 43 | "AAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABgAAAAAAAAAKAAAABgAAAKgBAAAAAAEA" |
| 44 | "DQAAAAAAAQAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAIAAAAiAEAAKsCAAAA" |
| 45 | "AAAAAQAAAAAAAAAFAAAAAAAAAAgAAACYAQAAuAIAAAAAAAACAAAAlAIAAJoCAAABAAAAowIAAAIA" |
| 46 | "AgABAAAAiAIAAAYAAABbAQAAcBACAAAADgABAAEAAQAAAI4CAAAEAAAAcBACAAAADgBAAQAAAAAA" |
| 47 | "AAAAAAAAAAAATAEAAAAAAAAAAAAAAAAAAAEAAAABAAY8aW5pdD4ABUlubmVyAA5MTmVzdGVkJElu" |
| 48 | "bmVyOwAITE5lc3RlZDsAIkxkYWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2" |
| 49 | "aWsvYW5ub3RhdGlvbi9Jbm5lckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNz" |
| 50 | "ZXM7ABJMamF2YS9sYW5nL09iamVjdDsAC05lc3RlZC5qYXZhAAFWAAJWTAALYWNjZXNzRmxhZ3MA" |
| 51 | "BG5hbWUABnRoaXMkMAAFdmFsdWUAAgEABw4AAQAHDjwAAgIBDhgBAgMCCwQADBcBAgQBDhwBGAAA" |
| 52 | "AQEAAJAgAICABNQCAAABAAGAgATwAgAAEAAAAAAAAAABAAAAAAAAAAEAAAAPAAAAcAAAAAIAAAAH" |
| 53 | "AAAArAAAAAMAAAACAAAAyAAAAAQAAAABAAAA4AAAAAUAAAADAAAA6AAAAAYAAAACAAAAAAEAAAMQ" |
| 54 | "AAACAAAAQAEAAAEgAAACAAAAVAEAAAYgAAACAAAAiAEAAAEQAAABAAAAqAEAAAIgAAAPAAAArgEA" |
| 55 | "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA=="; |
| 56 | |
| 57 | TEST_F(DexFileTest, Header) { |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 58 | ScratchFile tmp; |
| 59 | UniquePtr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename())); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 60 | ASSERT_TRUE(raw.get() != NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 61 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 62 | const DexFile::Header& header = raw->GetHeader(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 63 | // TODO: header.magic_ |
| 64 | EXPECT_EQ(0x00d87910U, header.checksum_); |
| 65 | // TODO: header.signature_ |
| 66 | EXPECT_EQ(904U, header.file_size_); |
| 67 | EXPECT_EQ(112U, header.header_size_); |
| 68 | EXPECT_EQ(0U, header.link_size_); |
| 69 | EXPECT_EQ(0U, header.link_off_); |
| 70 | EXPECT_EQ(15U, header.string_ids_size_); |
| 71 | EXPECT_EQ(112U, header.string_ids_off_); |
| 72 | EXPECT_EQ(7U, header.type_ids_size_); |
| 73 | EXPECT_EQ(172U, header.type_ids_off_); |
| 74 | EXPECT_EQ(2U, header.proto_ids_size_); |
| 75 | EXPECT_EQ(200U, header.proto_ids_off_); |
| 76 | EXPECT_EQ(1U, header.field_ids_size_); |
| 77 | EXPECT_EQ(224U, header.field_ids_off_); |
| 78 | EXPECT_EQ(3U, header.method_ids_size_); |
| 79 | EXPECT_EQ(232U, header.method_ids_off_); |
| 80 | EXPECT_EQ(2U, header.class_defs_size_); |
| 81 | EXPECT_EQ(256U, header.class_defs_off_); |
| 82 | EXPECT_EQ(584U, header.data_size_); |
| 83 | EXPECT_EQ(320U, header.data_off_); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 84 | |
| 85 | EXPECT_EQ(header.checksum_, raw->GetLocationChecksum()); |
| 86 | } |
| 87 | |
| 88 | TEST_F(DexFileTest, GetLocationChecksum) { |
| 89 | const DexFile* raw(OpenTestDexFile("Main")); |
| 90 | EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum()); |
| 91 | } |
| 92 | |
| 93 | TEST_F(DexFileTest, GetChecksum) { |
| 94 | uint32_t checksum; |
| 95 | EXPECT_TRUE(DexFile::GetChecksum(GetLibCoreDexFileName(), checksum)); |
| 96 | EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksum); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 99 | TEST_F(DexFileTest, ClassDefs) { |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 100 | const DexFile* raw(OpenTestDexFile("Nested")); |
| 101 | ASSERT_TRUE(raw != NULL); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 102 | EXPECT_EQ(2U, raw->NumClassDefs()); |
| 103 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 104 | const DexFile::ClassDef& c0 = raw->GetClassDef(0); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 105 | EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0)); |
| 106 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 107 | const DexFile::ClassDef& c1 = raw->GetClassDef(1); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 108 | EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1)); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 111 | TEST_F(DexFileTest, CreateMethodSignature) { |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 112 | const DexFile* raw(OpenTestDexFile("CreateMethodSignature")); |
| 113 | ASSERT_TRUE(raw != NULL); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 114 | EXPECT_EQ(1U, raw->NumClassDefs()); |
| 115 | |
| 116 | const DexFile::ClassDef& class_def = raw->GetClassDef(0); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 117 | ASSERT_STREQ("LCreateMethodSignature;", raw->GetClassDescriptor(class_def)); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 118 | |
| 119 | const byte* class_data = raw->GetClassData(class_def); |
| 120 | ASSERT_TRUE(class_data != NULL); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 121 | ClassDataItemIterator it(*raw, class_data); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 122 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 123 | EXPECT_EQ(1u, it.NumDirectMethods()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 124 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 125 | // Check the signature for the static initializer. |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 126 | { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 127 | ASSERT_EQ(1U, it.NumDirectMethods()); |
| 128 | const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 129 | uint32_t proto_idx = method_id.proto_idx_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 130 | const char* name = raw->StringDataByIdx(method_id.name_idx_); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 131 | ASSERT_STREQ("<init>", name); |
| 132 | int32_t length; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 133 | std::string signature(raw->CreateMethodSignature(proto_idx, &length)); |
| 134 | ASSERT_EQ("()V", signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Check both virtual methods. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 138 | ASSERT_EQ(2U, it.NumVirtualMethods()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 139 | { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 140 | it.Next(); |
| 141 | const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 142 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 143 | const char* name = raw->StringDataByIdx(method_id.name_idx_); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 144 | ASSERT_STREQ("m1", name); |
| 145 | |
| 146 | uint32_t proto_idx = method_id.proto_idx_; |
| 147 | int32_t length; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 148 | std::string signature(raw->CreateMethodSignature(proto_idx, &length)); |
| 149 | ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 153 | it.Next(); |
| 154 | const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 155 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 156 | const char* name = raw->StringDataByIdx(method_id.name_idx_); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 157 | ASSERT_STREQ("m2", name); |
| 158 | |
| 159 | uint32_t proto_idx = method_id.proto_idx_; |
| 160 | int32_t length; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 161 | std::string signature(raw->CreateMethodSignature(proto_idx, &length)); |
| 162 | ASSERT_EQ("(ZSC)LCreateMethodSignature;", signature); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | TEST_F(DexFileTest, FindStringId) { |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 167 | const DexFile* raw(OpenTestDexFile("CreateMethodSignature")); |
| 168 | ASSERT_TRUE(raw != NULL); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 169 | EXPECT_EQ(1U, raw->NumClassDefs()); |
| 170 | |
| 171 | const char* strings[] = { "LCreateMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;", |
| 172 | "D", "I", "J", NULL }; |
| 173 | for (size_t i = 0; strings[i] != NULL; i++) { |
| 174 | const char* str = strings[i]; |
| 175 | const DexFile::StringId* str_id = raw->FindStringId(str); |
| 176 | const char* dex_str = raw->GetStringData(*str_id); |
| 177 | EXPECT_STREQ(dex_str, str); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | TEST_F(DexFileTest, FindTypeId) { |
| 182 | for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) { |
| 183 | const char* type_str = java_lang_dex_file_->StringByTypeIdx(i); |
| 184 | const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str); |
| 185 | ASSERT_TRUE(type_str_id != NULL); |
| 186 | uint32_t type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id); |
| 187 | const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx); |
| 188 | ASSERT_TRUE(type_id != NULL); |
| 189 | EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id), i); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | TEST_F(DexFileTest, FindProtoId) { |
| 194 | for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) { |
| 195 | const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i); |
| 196 | const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find); |
| 197 | std::vector<uint16_t> to_find_types; |
| 198 | if (to_find_tl != NULL) { |
| 199 | for (size_t j = 0; j < to_find_tl->Size(); j++) { |
| 200 | to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_); |
| 201 | } |
| 202 | } |
| 203 | const DexFile::ProtoId* found = |
| 204 | java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types); |
| 205 | ASSERT_TRUE(found != NULL); |
| 206 | EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | TEST_F(DexFileTest, FindMethodId) { |
| 211 | for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) { |
| 212 | const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i); |
| 213 | const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); |
| 214 | const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); |
| 215 | const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_); |
| 216 | const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature); |
| 217 | int32_t length; |
| 218 | ASSERT_TRUE(found != NULL) << "Didn't find method " << i << ": " |
| 219 | << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." |
| 220 | << java_lang_dex_file_->GetStringData(name) |
| 221 | << java_lang_dex_file_->CreateMethodSignature(to_find.proto_idx_, &length); |
| 222 | EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 223 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 226 | TEST_F(DexFileTest, FindFieldId) { |
| 227 | for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) { |
| 228 | const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i); |
| 229 | const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); |
| 230 | const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); |
| 231 | const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_); |
| 232 | const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type); |
| 233 | ASSERT_TRUE(found != NULL) << "Didn't find field " << i << ": " |
| 234 | << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " " |
| 235 | << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." |
| 236 | << java_lang_dex_file_->GetStringData(name); |
| 237 | EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i); |
| 238 | } |
| 239 | } |
| 240 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 241 | } // namespace art |