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" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 20 | #include "common_runtime_test.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 21 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 22 | namespace art { |
| 23 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 24 | class DexFileTest : public CommonRuntimeTest {}; |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 25 | |
| 26 | TEST_F(DexFileTest, Open) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 27 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 28 | const DexFile* dex(OpenTestDexFile("Nested")); |
| 29 | ASSERT_TRUE(dex != NULL); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 30 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 31 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 32 | static const byte kBase64Map[256] = { |
| 33 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 34 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 35 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 36 | 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, |
| 37 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, |
| 38 | 255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, |
| 39 | 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, // NOLINT |
| 40 | 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, // NOLINT |
| 41 | 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 42 | 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // NOLINT |
| 43 | 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, // NOLINT |
| 44 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 45 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 46 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 47 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 48 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 49 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 50 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 51 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 52 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 53 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 54 | 255, 255, 255, 255 |
| 55 | }; |
| 56 | |
| 57 | static inline byte* DecodeBase64(const char* src, size_t* dst_size) { |
| 58 | std::vector<byte> tmp; |
| 59 | uint32_t t = 0, y = 0; |
| 60 | int g = 3; |
| 61 | for (size_t i = 0; src[i] != '\0'; ++i) { |
| 62 | byte c = kBase64Map[src[i] & 0xFF]; |
| 63 | if (c == 255) continue; |
| 64 | // the final = symbols are read and used to trim the remaining bytes |
| 65 | if (c == 254) { |
| 66 | c = 0; |
| 67 | // prevent g < 0 which would potentially allow an overflow later |
| 68 | if (--g < 0) { |
| 69 | *dst_size = 0; |
| 70 | return nullptr; |
| 71 | } |
| 72 | } else if (g != 3) { |
| 73 | // we only allow = to be at the end |
| 74 | *dst_size = 0; |
| 75 | return nullptr; |
| 76 | } |
| 77 | t = (t << 6) | c; |
| 78 | if (++y == 4) { |
| 79 | tmp.push_back((t >> 16) & 255); |
| 80 | if (g > 1) { |
| 81 | tmp.push_back((t >> 8) & 255); |
| 82 | } |
| 83 | if (g > 2) { |
| 84 | tmp.push_back(t & 255); |
| 85 | } |
| 86 | y = t = 0; |
| 87 | } |
| 88 | } |
| 89 | if (y != 0) { |
| 90 | *dst_size = 0; |
| 91 | return nullptr; |
| 92 | } |
| 93 | UniquePtr<byte[]> dst(new byte[tmp.size()]); |
| 94 | if (dst_size != nullptr) { |
| 95 | *dst_size = tmp.size(); |
| 96 | } else { |
| 97 | *dst_size = 0; |
| 98 | } |
| 99 | std::copy(tmp.begin(), tmp.end(), dst.get()); |
| 100 | return dst.release(); |
| 101 | } |
| 102 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 103 | // Although this is the same content logically as the Nested test dex, |
| 104 | // the DexFileHeader test is sensitive to subtle changes in the |
| 105 | // contents due to the checksum etc, so we embed the exact input here. |
| 106 | // |
| 107 | // class Nested { |
| 108 | // class Inner { |
| 109 | // } |
| 110 | // } |
| 111 | static const char kRawDex[] = |
| 112 | "ZGV4CjAzNQAQedgAe7gM1B/WHsWJ6L7lGAISGC7yjD2IAwAAcAAAAHhWNBIAAAAAAAAAAMQCAAAP" |
| 113 | "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAAMAAADoAAAAAgAAAAABAABIAgAAQAEAAK4B" |
| 114 | "AAC2AQAAvQEAAM0BAADXAQAA+wEAABsCAAA+AgAAUgIAAF8CAABiAgAAZgIAAHMCAAB5AgAAgQIA" |
| 115 | "AAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABgAAAAAAAAAKAAAABgAAAKgBAAAAAAEA" |
| 116 | "DQAAAAAAAQAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAIAAAAiAEAAKsCAAAA" |
| 117 | "AAAAAQAAAAAAAAAFAAAAAAAAAAgAAACYAQAAuAIAAAAAAAACAAAAlAIAAJoCAAABAAAAowIAAAIA" |
| 118 | "AgABAAAAiAIAAAYAAABbAQAAcBACAAAADgABAAEAAQAAAI4CAAAEAAAAcBACAAAADgBAAQAAAAAA" |
| 119 | "AAAAAAAAAAAATAEAAAAAAAAAAAAAAAAAAAEAAAABAAY8aW5pdD4ABUlubmVyAA5MTmVzdGVkJElu" |
| 120 | "bmVyOwAITE5lc3RlZDsAIkxkYWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2" |
| 121 | "aWsvYW5ub3RhdGlvbi9Jbm5lckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNz" |
| 122 | "ZXM7ABJMamF2YS9sYW5nL09iamVjdDsAC05lc3RlZC5qYXZhAAFWAAJWTAALYWNjZXNzRmxhZ3MA" |
| 123 | "BG5hbWUABnRoaXMkMAAFdmFsdWUAAgEABw4AAQAHDjwAAgIBDhgBAgMCCwQADBcBAgQBDhwBGAAA" |
| 124 | "AQEAAJAgAICABNQCAAABAAGAgATwAgAAEAAAAAAAAAABAAAAAAAAAAEAAAAPAAAAcAAAAAIAAAAH" |
| 125 | "AAAArAAAAAMAAAACAAAAyAAAAAQAAAABAAAA4AAAAAUAAAADAAAA6AAAAAYAAAACAAAAAAEAAAMQ" |
| 126 | "AAACAAAAQAEAAAEgAAACAAAAVAEAAAYgAAACAAAAiAEAAAEQAAABAAAAqAEAAAIgAAAPAAAArgEA" |
| 127 | "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA=="; |
| 128 | |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 129 | static const DexFile* OpenDexFileBase64(const char* base64, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 130 | const char* location) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 131 | // decode base64 |
| 132 | CHECK(base64 != NULL); |
| 133 | size_t length; |
| 134 | UniquePtr<byte[]> dex_bytes(DecodeBase64(base64, &length)); |
| 135 | CHECK(dex_bytes.get() != NULL); |
| 136 | |
| 137 | // write to provided file |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 138 | UniquePtr<File> file(OS::CreateEmptyFile(location)); |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 139 | CHECK(file.get() != NULL); |
| 140 | if (!file->WriteFully(dex_bytes.get(), length)) { |
| 141 | PLOG(FATAL) << "Failed to write base64 as dex file"; |
| 142 | } |
| 143 | file.reset(); |
| 144 | |
| 145 | // read dex file |
| 146 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 147 | std::string error_msg; |
| 148 | const DexFile* dex_file = DexFile::Open(location, location, &error_msg); |
| 149 | CHECK(dex_file != nullptr) << error_msg; |
Brian Carlstrom | e0948e1 | 2013-08-29 09:36:15 -0700 | [diff] [blame] | 150 | EXPECT_EQ(PROT_READ, dex_file->GetPermissions()); |
| 151 | EXPECT_TRUE(dex_file->IsReadOnly()); |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 152 | return dex_file; |
| 153 | } |
| 154 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 155 | TEST_F(DexFileTest, Header) { |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 156 | ScratchFile tmp; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 157 | UniquePtr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename().c_str())); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 158 | ASSERT_TRUE(raw.get() != NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 159 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 160 | const DexFile::Header& header = raw->GetHeader(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 161 | // TODO: header.magic_ |
| 162 | EXPECT_EQ(0x00d87910U, header.checksum_); |
| 163 | // TODO: header.signature_ |
| 164 | EXPECT_EQ(904U, header.file_size_); |
| 165 | EXPECT_EQ(112U, header.header_size_); |
| 166 | EXPECT_EQ(0U, header.link_size_); |
| 167 | EXPECT_EQ(0U, header.link_off_); |
| 168 | EXPECT_EQ(15U, header.string_ids_size_); |
| 169 | EXPECT_EQ(112U, header.string_ids_off_); |
| 170 | EXPECT_EQ(7U, header.type_ids_size_); |
| 171 | EXPECT_EQ(172U, header.type_ids_off_); |
| 172 | EXPECT_EQ(2U, header.proto_ids_size_); |
| 173 | EXPECT_EQ(200U, header.proto_ids_off_); |
| 174 | EXPECT_EQ(1U, header.field_ids_size_); |
| 175 | EXPECT_EQ(224U, header.field_ids_off_); |
| 176 | EXPECT_EQ(3U, header.method_ids_size_); |
| 177 | EXPECT_EQ(232U, header.method_ids_off_); |
| 178 | EXPECT_EQ(2U, header.class_defs_size_); |
| 179 | EXPECT_EQ(256U, header.class_defs_off_); |
| 180 | EXPECT_EQ(584U, header.data_size_); |
| 181 | EXPECT_EQ(320U, header.data_off_); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 182 | |
| 183 | EXPECT_EQ(header.checksum_, raw->GetLocationChecksum()); |
| 184 | } |
| 185 | |
| 186 | TEST_F(DexFileTest, GetLocationChecksum) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 187 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 188 | const DexFile* raw(OpenTestDexFile("Main")); |
| 189 | EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum()); |
| 190 | } |
| 191 | |
| 192 | TEST_F(DexFileTest, GetChecksum) { |
| 193 | uint32_t checksum; |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 194 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 195 | std::string error_msg; |
| 196 | EXPECT_TRUE(DexFile::GetChecksum(GetLibCoreDexFileName().c_str(), &checksum, &error_msg)) |
| 197 | << error_msg; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 198 | EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksum); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 201 | TEST_F(DexFileTest, ClassDefs) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 202 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 203 | const DexFile* raw(OpenTestDexFile("Nested")); |
| 204 | ASSERT_TRUE(raw != NULL); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 205 | EXPECT_EQ(2U, raw->NumClassDefs()); |
| 206 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 207 | const DexFile::ClassDef& c0 = raw->GetClassDef(0); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 208 | EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0)); |
| 209 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 210 | const DexFile::ClassDef& c1 = raw->GetClassDef(1); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 211 | EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1)); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 214 | TEST_F(DexFileTest, GetMethodSignature) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 215 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 216 | const DexFile* raw(OpenTestDexFile("GetMethodSignature")); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 217 | ASSERT_TRUE(raw != NULL); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 218 | EXPECT_EQ(1U, raw->NumClassDefs()); |
| 219 | |
| 220 | const DexFile::ClassDef& class_def = raw->GetClassDef(0); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 221 | ASSERT_STREQ("LGetMethodSignature;", raw->GetClassDescriptor(class_def)); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 222 | |
| 223 | const byte* class_data = raw->GetClassData(class_def); |
| 224 | ASSERT_TRUE(class_data != NULL); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 225 | ClassDataItemIterator it(*raw, class_data); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 226 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 227 | EXPECT_EQ(1u, it.NumDirectMethods()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 228 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 229 | // Check the signature for the static initializer. |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 230 | { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 231 | ASSERT_EQ(1U, it.NumDirectMethods()); |
| 232 | const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 233 | const char* name = raw->StringDataByIdx(method_id.name_idx_); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 234 | ASSERT_STREQ("<init>", name); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 235 | std::string signature(raw->GetMethodSignature(method_id).ToString()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 236 | ASSERT_EQ("()V", signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // Check both virtual methods. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 240 | ASSERT_EQ(2U, it.NumVirtualMethods()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 241 | { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 242 | it.Next(); |
| 243 | const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 244 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 245 | const char* name = raw->StringDataByIdx(method_id.name_idx_); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 246 | ASSERT_STREQ("m1", name); |
| 247 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 248 | std::string signature(raw->GetMethodSignature(method_id).ToString()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 249 | ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 253 | it.Next(); |
| 254 | const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex()); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 255 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 256 | const char* name = raw->StringDataByIdx(method_id.name_idx_); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 257 | ASSERT_STREQ("m2", name); |
| 258 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 259 | std::string signature(raw->GetMethodSignature(method_id).ToString()); |
| 260 | ASSERT_EQ("(ZSC)LGetMethodSignature;", signature); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | TEST_F(DexFileTest, FindStringId) { |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 265 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 266 | const DexFile* raw(OpenTestDexFile("GetMethodSignature")); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 267 | ASSERT_TRUE(raw != NULL); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 268 | EXPECT_EQ(1U, raw->NumClassDefs()); |
| 269 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 270 | const char* strings[] = { "LGetMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;", |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 271 | "D", "I", "J", NULL }; |
| 272 | for (size_t i = 0; strings[i] != NULL; i++) { |
| 273 | const char* str = strings[i]; |
| 274 | const DexFile::StringId* str_id = raw->FindStringId(str); |
| 275 | const char* dex_str = raw->GetStringData(*str_id); |
| 276 | EXPECT_STREQ(dex_str, str); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | TEST_F(DexFileTest, FindTypeId) { |
| 281 | for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) { |
| 282 | const char* type_str = java_lang_dex_file_->StringByTypeIdx(i); |
| 283 | const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str); |
| 284 | ASSERT_TRUE(type_str_id != NULL); |
| 285 | uint32_t type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id); |
| 286 | const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx); |
| 287 | ASSERT_TRUE(type_id != NULL); |
| 288 | EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id), i); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | TEST_F(DexFileTest, FindProtoId) { |
| 293 | for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) { |
| 294 | const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i); |
| 295 | const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find); |
| 296 | std::vector<uint16_t> to_find_types; |
| 297 | if (to_find_tl != NULL) { |
| 298 | for (size_t j = 0; j < to_find_tl->Size(); j++) { |
| 299 | to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_); |
| 300 | } |
| 301 | } |
| 302 | const DexFile::ProtoId* found = |
| 303 | java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types); |
| 304 | ASSERT_TRUE(found != NULL); |
| 305 | EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | TEST_F(DexFileTest, FindMethodId) { |
| 310 | for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) { |
| 311 | const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i); |
| 312 | const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); |
| 313 | const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); |
| 314 | const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_); |
| 315 | const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 316 | ASSERT_TRUE(found != NULL) << "Didn't find method " << i << ": " |
| 317 | << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." |
| 318 | << java_lang_dex_file_->GetStringData(name) |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 319 | << java_lang_dex_file_->GetMethodSignature(to_find); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 320 | EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 321 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Ian Rogers | 9b1a4f4 | 2011-11-14 18:35:10 -0800 | [diff] [blame] | 324 | TEST_F(DexFileTest, FindFieldId) { |
| 325 | for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) { |
| 326 | const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i); |
| 327 | const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_); |
| 328 | const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_); |
| 329 | const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_); |
| 330 | const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type); |
| 331 | ASSERT_TRUE(found != NULL) << "Didn't find field " << i << ": " |
| 332 | << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " " |
| 333 | << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "." |
| 334 | << java_lang_dex_file_->GetStringData(name); |
| 335 | EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i); |
| 336 | } |
| 337 | } |
| 338 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 339 | } // namespace art |