blob: 7575d4a43c6c357bfc18632173728c6736f76b99 [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
Elliott Hughes90a33692011-08-30 13:27:07 -070019#include "UniquePtr.h"
20#include "common_test.h"
21
Carl Shapiro1fb86202011-06-27 17:43:13 -070022namespace art {
23
Brian Carlstrom9f30b382011-08-28 22:41:38 -070024class DexFileTest : public CommonTest {};
25
26TEST_F(DexFileTest, Open) {
Ian Rogers33e95662013-05-20 20:29:14 -070027 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes4d6850c2012-01-18 15:55:06 -080028 const DexFile* dex(OpenTestDexFile("Nested"));
29 ASSERT_TRUE(dex != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070030}
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031
Brian Carlstrom9f30b382011-08-28 22:41:38 -070032// 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 "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAA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AAAPAAAArgEA"
56 "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA==";
57
Ian Rogers33e95662013-05-20 20:29:14 -070058static 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
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070067 UniquePtr<File> file(OS::CreateEmptyFile(location.c_str()));
Ian Rogers33e95662013-05-20 20:29:14 -070068 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);
Brian Carlstrome0948e12013-08-29 09:36:15 -070078 EXPECT_EQ(PROT_READ, dex_file->GetPermissions());
79 EXPECT_TRUE(dex_file->IsReadOnly());
Ian Rogers33e95662013-05-20 20:29:14 -070080 return dex_file;
81}
82
Brian Carlstrom9f30b382011-08-28 22:41:38 -070083TEST_F(DexFileTest, Header) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070084 ScratchFile tmp;
85 UniquePtr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename()));
Elliott Hughes90a33692011-08-30 13:27:07 -070086 ASSERT_TRUE(raw.get() != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070087
Brian Carlstromf615a612011-07-23 12:50:34 -070088 const DexFile::Header& header = raw->GetHeader();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070089 // TODO: header.magic_
90 EXPECT_EQ(0x00d87910U, header.checksum_);
91 // TODO: header.signature_
92 EXPECT_EQ(904U, header.file_size_);
93 EXPECT_EQ(112U, header.header_size_);
94 EXPECT_EQ(0U, header.link_size_);
95 EXPECT_EQ(0U, header.link_off_);
96 EXPECT_EQ(15U, header.string_ids_size_);
97 EXPECT_EQ(112U, header.string_ids_off_);
98 EXPECT_EQ(7U, header.type_ids_size_);
99 EXPECT_EQ(172U, header.type_ids_off_);
100 EXPECT_EQ(2U, header.proto_ids_size_);
101 EXPECT_EQ(200U, header.proto_ids_off_);
102 EXPECT_EQ(1U, header.field_ids_size_);
103 EXPECT_EQ(224U, header.field_ids_off_);
104 EXPECT_EQ(3U, header.method_ids_size_);
105 EXPECT_EQ(232U, header.method_ids_off_);
106 EXPECT_EQ(2U, header.class_defs_size_);
107 EXPECT_EQ(256U, header.class_defs_off_);
108 EXPECT_EQ(584U, header.data_size_);
109 EXPECT_EQ(320U, header.data_off_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800110
111 EXPECT_EQ(header.checksum_, raw->GetLocationChecksum());
112}
113
114TEST_F(DexFileTest, GetLocationChecksum) {
Ian Rogers33e95662013-05-20 20:29:14 -0700115 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800116 const DexFile* raw(OpenTestDexFile("Main"));
117 EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum());
118}
119
120TEST_F(DexFileTest, GetChecksum) {
121 uint32_t checksum;
Ian Rogers33e95662013-05-20 20:29:14 -0700122 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -0700123 EXPECT_TRUE(DexFile::GetChecksum(GetLibCoreDexFileName(), &checksum));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800124 EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksum);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700125}
126
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700127TEST_F(DexFileTest, ClassDefs) {
Ian Rogers33e95662013-05-20 20:29:14 -0700128 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800129 const DexFile* raw(OpenTestDexFile("Nested"));
130 ASSERT_TRUE(raw != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700131 EXPECT_EQ(2U, raw->NumClassDefs());
132
Brian Carlstromf615a612011-07-23 12:50:34 -0700133 const DexFile::ClassDef& c0 = raw->GetClassDef(0);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700134 EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0));
135
Brian Carlstromf615a612011-07-23 12:50:34 -0700136 const DexFile::ClassDef& c1 = raw->GetClassDef(1);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700137 EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1));
Carl Shapiro1fb86202011-06-27 17:43:13 -0700138}
139
Ian Rogersd91d6d62013-09-25 20:26:14 -0700140TEST_F(DexFileTest, GetMethodSignature) {
Ian Rogers33e95662013-05-20 20:29:14 -0700141 ScopedObjectAccess soa(Thread::Current());
Ian Rogersd91d6d62013-09-25 20:26:14 -0700142 const DexFile* raw(OpenTestDexFile("GetMethodSignature"));
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800143 ASSERT_TRUE(raw != NULL);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700144 EXPECT_EQ(1U, raw->NumClassDefs());
145
146 const DexFile::ClassDef& class_def = raw->GetClassDef(0);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700147 ASSERT_STREQ("LGetMethodSignature;", raw->GetClassDescriptor(class_def));
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700148
149 const byte* class_data = raw->GetClassData(class_def);
150 ASSERT_TRUE(class_data != NULL);
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800151 ClassDataItemIterator it(*raw, class_data);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700152
Ian Rogers0571d352011-11-03 19:51:38 -0700153 EXPECT_EQ(1u, it.NumDirectMethods());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700154
Ian Rogers0571d352011-11-03 19:51:38 -0700155 // Check the signature for the static initializer.
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700156 {
Ian Rogers0571d352011-11-03 19:51:38 -0700157 ASSERT_EQ(1U, it.NumDirectMethods());
158 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Ian Rogers0571d352011-11-03 19:51:38 -0700159 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700160 ASSERT_STREQ("<init>", name);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700161 std::string signature(raw->GetMethodSignature(method_id).ToString());
Ian Rogers0571d352011-11-03 19:51:38 -0700162 ASSERT_EQ("()V", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700163 }
164
165 // Check both virtual methods.
Ian Rogers0571d352011-11-03 19:51:38 -0700166 ASSERT_EQ(2U, it.NumVirtualMethods());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700167 {
Ian Rogers0571d352011-11-03 19:51:38 -0700168 it.Next();
169 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700170
Ian Rogers0571d352011-11-03 19:51:38 -0700171 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700172 ASSERT_STREQ("m1", name);
173
Ian Rogersd91d6d62013-09-25 20:26:14 -0700174 std::string signature(raw->GetMethodSignature(method_id).ToString());
Ian Rogers0571d352011-11-03 19:51:38 -0700175 ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700176 }
177
178 {
Ian Rogers0571d352011-11-03 19:51:38 -0700179 it.Next();
180 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700181
Ian Rogers0571d352011-11-03 19:51:38 -0700182 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700183 ASSERT_STREQ("m2", name);
184
Ian Rogersd91d6d62013-09-25 20:26:14 -0700185 std::string signature(raw->GetMethodSignature(method_id).ToString());
186 ASSERT_EQ("(ZSC)LGetMethodSignature;", signature);
Ian Rogers0571d352011-11-03 19:51:38 -0700187 }
188}
189
190TEST_F(DexFileTest, FindStringId) {
Ian Rogers33e95662013-05-20 20:29:14 -0700191 ScopedObjectAccess soa(Thread::Current());
Ian Rogersd91d6d62013-09-25 20:26:14 -0700192 const DexFile* raw(OpenTestDexFile("GetMethodSignature"));
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800193 ASSERT_TRUE(raw != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700194 EXPECT_EQ(1U, raw->NumClassDefs());
195
Ian Rogersd91d6d62013-09-25 20:26:14 -0700196 const char* strings[] = { "LGetMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;",
Ian Rogers0571d352011-11-03 19:51:38 -0700197 "D", "I", "J", NULL };
198 for (size_t i = 0; strings[i] != NULL; i++) {
199 const char* str = strings[i];
200 const DexFile::StringId* str_id = raw->FindStringId(str);
201 const char* dex_str = raw->GetStringData(*str_id);
202 EXPECT_STREQ(dex_str, str);
203 }
204}
205
206TEST_F(DexFileTest, FindTypeId) {
207 for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) {
208 const char* type_str = java_lang_dex_file_->StringByTypeIdx(i);
209 const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str);
210 ASSERT_TRUE(type_str_id != NULL);
211 uint32_t type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id);
212 const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx);
213 ASSERT_TRUE(type_id != NULL);
214 EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id), i);
215 }
216}
217
218TEST_F(DexFileTest, FindProtoId) {
219 for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) {
220 const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i);
221 const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find);
222 std::vector<uint16_t> to_find_types;
223 if (to_find_tl != NULL) {
224 for (size_t j = 0; j < to_find_tl->Size(); j++) {
225 to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_);
226 }
227 }
228 const DexFile::ProtoId* found =
229 java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types);
230 ASSERT_TRUE(found != NULL);
231 EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i);
232 }
233}
234
235TEST_F(DexFileTest, FindMethodId) {
236 for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) {
237 const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i);
238 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
239 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
240 const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_);
241 const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature);
Ian Rogers0571d352011-11-03 19:51:38 -0700242 ASSERT_TRUE(found != NULL) << "Didn't find method " << i << ": "
243 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
244 << java_lang_dex_file_->GetStringData(name)
Ian Rogersd91d6d62013-09-25 20:26:14 -0700245 << java_lang_dex_file_->GetMethodSignature(to_find);
Ian Rogers0571d352011-11-03 19:51:38 -0700246 EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700247 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700248}
249
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800250TEST_F(DexFileTest, FindFieldId) {
251 for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) {
252 const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i);
253 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
254 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
255 const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_);
256 const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type);
257 ASSERT_TRUE(found != NULL) << "Didn't find field " << i << ": "
258 << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " "
259 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
260 << java_lang_dex_file_->GetStringData(name);
261 EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i);
262 }
263}
264
Carl Shapiro1fb86202011-06-27 17:43:13 -0700265} // namespace art