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