blob: b304779568533f2b9b67562aceea8e8896f31ecb [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "dex_file.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070018
Ian Rogers700a4022014-05-19 16:49:03 -070019#include <memory>
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "base/stl_util.h"
22#include "base/unix_file/fd_file.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080023#include "common_runtime_test.h"
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "os.h"
25#include "scoped_thread_state_change.h"
26#include "thread-inl.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070027
Carl Shapiro1fb86202011-06-27 17:43:13 -070028namespace art {
29
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030class DexFileTest : public CommonRuntimeTest {};
Brian Carlstrom9f30b382011-08-28 22:41:38 -070031
32TEST_F(DexFileTest, Open) {
Ian Rogers33e95662013-05-20 20:29:14 -070033 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes4d6850c2012-01-18 15:55:06 -080034 const DexFile* dex(OpenTestDexFile("Nested"));
35 ASSERT_TRUE(dex != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070036}
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070037
Ian Rogers13735952014-10-08 12:43:28 -070038static const uint8_t kBase64Map[256] = {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080039 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
40 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
41 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
42 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
43 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
44 255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,
45 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, // NOLINT
46 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, // NOLINT
47 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
48 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // NOLINT
49 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, // NOLINT
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, 255, 255, 255, 255, 255, 255, 255, 255,
56 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
57 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
58 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
59 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
60 255, 255, 255, 255
61};
62
Ian Rogers13735952014-10-08 12:43:28 -070063static inline uint8_t* DecodeBase64(const char* src, size_t* dst_size) {
64 std::vector<uint8_t> tmp;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080065 uint32_t t = 0, y = 0;
66 int g = 3;
67 for (size_t i = 0; src[i] != '\0'; ++i) {
Ian Rogers13735952014-10-08 12:43:28 -070068 uint8_t c = kBase64Map[src[i] & 0xFF];
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080069 if (c == 255) continue;
70 // the final = symbols are read and used to trim the remaining bytes
71 if (c == 254) {
72 c = 0;
73 // prevent g < 0 which would potentially allow an overflow later
74 if (--g < 0) {
75 *dst_size = 0;
76 return nullptr;
77 }
78 } else if (g != 3) {
79 // we only allow = to be at the end
80 *dst_size = 0;
81 return nullptr;
82 }
83 t = (t << 6) | c;
84 if (++y == 4) {
85 tmp.push_back((t >> 16) & 255);
86 if (g > 1) {
87 tmp.push_back((t >> 8) & 255);
88 }
89 if (g > 2) {
90 tmp.push_back(t & 255);
91 }
92 y = t = 0;
93 }
94 }
95 if (y != 0) {
96 *dst_size = 0;
97 return nullptr;
98 }
Ian Rogers13735952014-10-08 12:43:28 -070099 std::unique_ptr<uint8_t[]> dst(new uint8_t[tmp.size()]);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800100 if (dst_size != nullptr) {
101 *dst_size = tmp.size();
102 } else {
103 *dst_size = 0;
104 }
105 std::copy(tmp.begin(), tmp.end(), dst.get());
106 return dst.release();
107}
108
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700109// Although this is the same content logically as the Nested test dex,
110// the DexFileHeader test is sensitive to subtle changes in the
111// contents due to the checksum etc, so we embed the exact input here.
112//
113// class Nested {
114// class Inner {
115// }
116// }
117static const char kRawDex[] =
118 "ZGV4CjAzNQAQedgAe7gM1B/WHsWJ6L7lGAISGC7yjD2IAwAAcAAAAHhWNBIAAAAAAAAAAMQCAAAP"
119 "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAAMAAADoAAAAAgAAAAABAABIAgAAQAEAAK4B"
120 "AAC2AQAAvQEAAM0BAADXAQAA+wEAABsCAAA+AgAAUgIAAF8CAABiAgAAZgIAAHMCAAB5AgAAgQIA"
121 "AAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABgAAAAAAAAAKAAAABgAAAKgBAAAAAAEA"
122 "DQAAAAAAAQAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAIAAAAiAEAAKsCAAAA"
123 "AAAAAQAAAAAAAAAFAAAAAAAAAAgAAACYAQAAuAIAAAAAAAACAAAAlAIAAJoCAAABAAAAowIAAAIA"
124 "AgABAAAAiAIAAAYAAABbAQAAcBACAAAADgABAAEAAQAAAI4CAAAEAAAAcBACAAAADgBAAQAAAAAA"
125 "AAAAAAAAAAAATAEAAAAAAAAAAAAAAAAAAAEAAAABAAY8aW5pdD4ABUlubmVyAA5MTmVzdGVkJElu"
126 "bmVyOwAITE5lc3RlZDsAIkxkYWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2"
127 "aWsvYW5ub3RhdGlvbi9Jbm5lckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNz"
128 "ZXM7ABJMamF2YS9sYW5nL09iamVjdDsAC05lc3RlZC5qYXZhAAFWAAJWTAALYWNjZXNzRmxhZ3MA"
129 "BG5hbWUABnRoaXMkMAAFdmFsdWUAAgEABw4AAQAHDjwAAgIBDhgBAgMCCwQADBcBAgQBDhwBGAAA"
130 "AQEAAJAgAICABNQCAAABAAGAgATwAgAAEAAAAAAAAAABAAAAAAAAAAEAAAAPAAAAcAAAAAIAAAAH"
131 "AAAArAAAAAMAAAACAAAAyAAAAAQAAAABAAAA4AAAAAUAAAADAAAA6AAAAAYAAAACAAAAAAEAAAMQ"
132 "AAACAAAAQAEAAAEgAAACAAAAVAEAAAYgAAACAAAAiAEAAAEQAAABAAAAqAEAAAIgAAAPAAAArgEA"
133 "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA==";
134
Ian Rogers33e95662013-05-20 20:29:14 -0700135static const DexFile* OpenDexFileBase64(const char* base64,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700136 const char* location) {
Ian Rogers33e95662013-05-20 20:29:14 -0700137 // decode base64
138 CHECK(base64 != NULL);
139 size_t length;
Ian Rogers13735952014-10-08 12:43:28 -0700140 std::unique_ptr<uint8_t[]> dex_bytes(DecodeBase64(base64, &length));
Ian Rogers33e95662013-05-20 20:29:14 -0700141 CHECK(dex_bytes.get() != NULL);
142
143 // write to provided file
Ian Rogers700a4022014-05-19 16:49:03 -0700144 std::unique_ptr<File> file(OS::CreateEmptyFile(location));
Ian Rogers33e95662013-05-20 20:29:14 -0700145 CHECK(file.get() != NULL);
146 if (!file->WriteFully(dex_bytes.get(), length)) {
147 PLOG(FATAL) << "Failed to write base64 as dex file";
148 }
Andreas Gampe4303ba92014-11-06 01:00:46 -0800149 if (file->FlushCloseOrErase() != 0) {
150 PLOG(FATAL) << "Could not flush and close test file.";
151 }
Ian Rogers33e95662013-05-20 20:29:14 -0700152 file.reset();
153
154 // read dex file
155 ScopedObjectAccess soa(Thread::Current());
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700156 std::string error_msg;
Andreas Gampe833a4852014-05-21 18:46:59 -0700157 std::vector<const DexFile*> tmp;
158 bool success = DexFile::Open(location, location, &error_msg, &tmp);
159 CHECK(success) << error_msg;
160 EXPECT_EQ(1U, tmp.size());
161 const DexFile* dex_file = tmp[0];
Brian Carlstrome0948e12013-08-29 09:36:15 -0700162 EXPECT_EQ(PROT_READ, dex_file->GetPermissions());
163 EXPECT_TRUE(dex_file->IsReadOnly());
Ian Rogers33e95662013-05-20 20:29:14 -0700164 return dex_file;
165}
166
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700167TEST_F(DexFileTest, Header) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700168 ScratchFile tmp;
Ian Rogers700a4022014-05-19 16:49:03 -0700169 std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename().c_str()));
Elliott Hughes90a33692011-08-30 13:27:07 -0700170 ASSERT_TRUE(raw.get() != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700171
Brian Carlstromf615a612011-07-23 12:50:34 -0700172 const DexFile::Header& header = raw->GetHeader();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700173 // TODO: header.magic_
174 EXPECT_EQ(0x00d87910U, header.checksum_);
175 // TODO: header.signature_
176 EXPECT_EQ(904U, header.file_size_);
177 EXPECT_EQ(112U, header.header_size_);
178 EXPECT_EQ(0U, header.link_size_);
179 EXPECT_EQ(0U, header.link_off_);
180 EXPECT_EQ(15U, header.string_ids_size_);
181 EXPECT_EQ(112U, header.string_ids_off_);
182 EXPECT_EQ(7U, header.type_ids_size_);
183 EXPECT_EQ(172U, header.type_ids_off_);
184 EXPECT_EQ(2U, header.proto_ids_size_);
185 EXPECT_EQ(200U, header.proto_ids_off_);
186 EXPECT_EQ(1U, header.field_ids_size_);
187 EXPECT_EQ(224U, header.field_ids_off_);
188 EXPECT_EQ(3U, header.method_ids_size_);
189 EXPECT_EQ(232U, header.method_ids_off_);
190 EXPECT_EQ(2U, header.class_defs_size_);
191 EXPECT_EQ(256U, header.class_defs_off_);
192 EXPECT_EQ(584U, header.data_size_);
193 EXPECT_EQ(320U, header.data_off_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800194
195 EXPECT_EQ(header.checksum_, raw->GetLocationChecksum());
196}
197
198TEST_F(DexFileTest, GetLocationChecksum) {
Ian Rogers33e95662013-05-20 20:29:14 -0700199 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800200 const DexFile* raw(OpenTestDexFile("Main"));
201 EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum());
202}
203
204TEST_F(DexFileTest, GetChecksum) {
205 uint32_t checksum;
Ian Rogers33e95662013-05-20 20:29:14 -0700206 ScopedObjectAccess soa(Thread::Current());
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700207 std::string error_msg;
208 EXPECT_TRUE(DexFile::GetChecksum(GetLibCoreDexFileName().c_str(), &checksum, &error_msg))
209 << error_msg;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800210 EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksum);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700211}
212
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700213TEST_F(DexFileTest, ClassDefs) {
Ian Rogers33e95662013-05-20 20:29:14 -0700214 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800215 const DexFile* raw(OpenTestDexFile("Nested"));
216 ASSERT_TRUE(raw != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700217 EXPECT_EQ(2U, raw->NumClassDefs());
218
Brian Carlstromf615a612011-07-23 12:50:34 -0700219 const DexFile::ClassDef& c0 = raw->GetClassDef(0);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700220 EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0));
221
Brian Carlstromf615a612011-07-23 12:50:34 -0700222 const DexFile::ClassDef& c1 = raw->GetClassDef(1);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700223 EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1));
Carl Shapiro1fb86202011-06-27 17:43:13 -0700224}
225
Ian Rogersd91d6d62013-09-25 20:26:14 -0700226TEST_F(DexFileTest, GetMethodSignature) {
Ian Rogers33e95662013-05-20 20:29:14 -0700227 ScopedObjectAccess soa(Thread::Current());
Ian Rogersd91d6d62013-09-25 20:26:14 -0700228 const DexFile* raw(OpenTestDexFile("GetMethodSignature"));
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800229 ASSERT_TRUE(raw != NULL);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700230 EXPECT_EQ(1U, raw->NumClassDefs());
231
232 const DexFile::ClassDef& class_def = raw->GetClassDef(0);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700233 ASSERT_STREQ("LGetMethodSignature;", raw->GetClassDescriptor(class_def));
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700234
Ian Rogers13735952014-10-08 12:43:28 -0700235 const uint8_t* class_data = raw->GetClassData(class_def);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700236 ASSERT_TRUE(class_data != NULL);
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800237 ClassDataItemIterator it(*raw, class_data);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700238
Ian Rogers0571d352011-11-03 19:51:38 -0700239 EXPECT_EQ(1u, it.NumDirectMethods());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700240
Ian Rogers0571d352011-11-03 19:51:38 -0700241 // Check the signature for the static initializer.
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700242 {
Ian Rogers0571d352011-11-03 19:51:38 -0700243 ASSERT_EQ(1U, it.NumDirectMethods());
244 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Ian Rogers0571d352011-11-03 19:51:38 -0700245 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700246 ASSERT_STREQ("<init>", name);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700247 std::string signature(raw->GetMethodSignature(method_id).ToString());
Ian Rogers0571d352011-11-03 19:51:38 -0700248 ASSERT_EQ("()V", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700249 }
250
251 // Check both virtual methods.
Ian Rogers0571d352011-11-03 19:51:38 -0700252 ASSERT_EQ(2U, it.NumVirtualMethods());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700253 {
Ian Rogers0571d352011-11-03 19:51:38 -0700254 it.Next();
255 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700256
Ian Rogers0571d352011-11-03 19:51:38 -0700257 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700258 ASSERT_STREQ("m1", name);
259
Ian Rogersd91d6d62013-09-25 20:26:14 -0700260 std::string signature(raw->GetMethodSignature(method_id).ToString());
Ian Rogers0571d352011-11-03 19:51:38 -0700261 ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700262 }
263
264 {
Ian Rogers0571d352011-11-03 19:51:38 -0700265 it.Next();
266 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700267
Ian Rogers0571d352011-11-03 19:51:38 -0700268 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700269 ASSERT_STREQ("m2", name);
270
Ian Rogersd91d6d62013-09-25 20:26:14 -0700271 std::string signature(raw->GetMethodSignature(method_id).ToString());
272 ASSERT_EQ("(ZSC)LGetMethodSignature;", signature);
Ian Rogers0571d352011-11-03 19:51:38 -0700273 }
274}
275
276TEST_F(DexFileTest, FindStringId) {
Ian Rogers33e95662013-05-20 20:29:14 -0700277 ScopedObjectAccess soa(Thread::Current());
Ian Rogersd91d6d62013-09-25 20:26:14 -0700278 const DexFile* raw(OpenTestDexFile("GetMethodSignature"));
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800279 ASSERT_TRUE(raw != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700280 EXPECT_EQ(1U, raw->NumClassDefs());
281
Ian Rogersd91d6d62013-09-25 20:26:14 -0700282 const char* strings[] = { "LGetMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;",
Ian Rogers0571d352011-11-03 19:51:38 -0700283 "D", "I", "J", NULL };
284 for (size_t i = 0; strings[i] != NULL; i++) {
285 const char* str = strings[i];
286 const DexFile::StringId* str_id = raw->FindStringId(str);
287 const char* dex_str = raw->GetStringData(*str_id);
288 EXPECT_STREQ(dex_str, str);
289 }
290}
291
292TEST_F(DexFileTest, FindTypeId) {
293 for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) {
294 const char* type_str = java_lang_dex_file_->StringByTypeIdx(i);
295 const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str);
296 ASSERT_TRUE(type_str_id != NULL);
297 uint32_t type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id);
298 const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx);
299 ASSERT_TRUE(type_id != NULL);
300 EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id), i);
301 }
302}
303
304TEST_F(DexFileTest, FindProtoId) {
305 for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) {
306 const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i);
307 const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find);
308 std::vector<uint16_t> to_find_types;
309 if (to_find_tl != NULL) {
310 for (size_t j = 0; j < to_find_tl->Size(); j++) {
311 to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_);
312 }
313 }
314 const DexFile::ProtoId* found =
315 java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types);
316 ASSERT_TRUE(found != NULL);
317 EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i);
318 }
319}
320
321TEST_F(DexFileTest, FindMethodId) {
322 for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) {
323 const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i);
324 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
325 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
326 const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_);
327 const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature);
Ian Rogers0571d352011-11-03 19:51:38 -0700328 ASSERT_TRUE(found != NULL) << "Didn't find method " << i << ": "
329 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
330 << java_lang_dex_file_->GetStringData(name)
Ian Rogersd91d6d62013-09-25 20:26:14 -0700331 << java_lang_dex_file_->GetMethodSignature(to_find);
Ian Rogers0571d352011-11-03 19:51:38 -0700332 EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700333 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700334}
335
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800336TEST_F(DexFileTest, FindFieldId) {
337 for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) {
338 const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i);
339 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
340 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
341 const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_);
342 const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type);
343 ASSERT_TRUE(found != NULL) << "Didn't find field " << i << ": "
344 << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " "
345 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
346 << java_lang_dex_file_->GetStringData(name);
347 EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i);
348 }
349}
350
Calin Juravle4e1d5792014-07-15 23:56:47 +0100351TEST_F(DexFileTest, GetMultiDexClassesDexName) {
352 std::string dex_location_str = "/system/app/framework.jar";
353 const char* dex_location = dex_location_str.c_str();
354 ASSERT_EQ("/system/app/framework.jar", DexFile::GetMultiDexClassesDexName(0, dex_location));
355 ASSERT_EQ("/system/app/framework.jar:classes2.dex", DexFile::GetMultiDexClassesDexName(1, dex_location));
356 ASSERT_EQ("/system/app/framework.jar:classes101.dex", DexFile::GetMultiDexClassesDexName(100, dex_location));
357}
358
359TEST_F(DexFileTest, GetDexCanonicalLocation) {
360 ScratchFile file;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100361 UniqueCPtr<const char[]> dex_location_real(realpath(file.GetFilename().c_str(), nullptr));
362 std::string dex_location(dex_location_real.get());
Calin Juravle4e1d5792014-07-15 23:56:47 +0100363
Calin Juravle61281dc2014-08-07 11:54:36 +0100364 ASSERT_EQ(dex_location, DexFile::GetDexCanonicalLocation(dex_location.c_str()));
Calin Juravle4e1d5792014-07-15 23:56:47 +0100365 std::string multidex_location = DexFile::GetMultiDexClassesDexName(1, dex_location.c_str());
366 ASSERT_EQ(multidex_location, DexFile::GetDexCanonicalLocation(multidex_location.c_str()));
367
368 std::string dex_location_sym = dex_location + "symlink";
369 ASSERT_EQ(0, symlink(dex_location.c_str(), dex_location_sym.c_str()));
370
371 ASSERT_EQ(dex_location, DexFile::GetDexCanonicalLocation(dex_location_sym.c_str()));
372
373 std::string multidex_location_sym = DexFile::GetMultiDexClassesDexName(1, dex_location_sym.c_str());
374 ASSERT_EQ(multidex_location, DexFile::GetDexCanonicalLocation(multidex_location_sym.c_str()));
375
376 ASSERT_EQ(0, unlink(dex_location_sym.c_str()));
377}
378
Carl Shapiro1fb86202011-06-27 17:43:13 -0700379} // namespace art