blob: 32a8354d017b70a3a35ad7c7ea610ecc6c05adc6 [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 "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4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AAAIgAAAPAAAArgEA"
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 Carlstrom5b332c82012-02-01 15:02:31 -0800123 EXPECT_TRUE(DexFile::GetChecksum(GetLibCoreDexFileName(), checksum));
124 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 Rogers0571d352011-11-03 19:51:38 -0700140TEST_F(DexFileTest, CreateMethodSignature) {
Ian Rogers33e95662013-05-20 20:29:14 -0700141 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800142 const DexFile* raw(OpenTestDexFile("CreateMethodSignature"));
143 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 Rogers0571d352011-11-03 19:51:38 -0700147 ASSERT_STREQ("LCreateMethodSignature;", 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());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700159 uint32_t proto_idx = method_id.proto_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -0700160 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700161 ASSERT_STREQ("<init>", name);
162 int32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700163 std::string signature(raw->CreateMethodSignature(proto_idx, &length));
164 ASSERT_EQ("()V", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700165 }
166
167 // Check both virtual methods.
Ian Rogers0571d352011-11-03 19:51:38 -0700168 ASSERT_EQ(2U, it.NumVirtualMethods());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700169 {
Ian Rogers0571d352011-11-03 19:51:38 -0700170 it.Next();
171 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700172
Ian Rogers0571d352011-11-03 19:51:38 -0700173 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700174 ASSERT_STREQ("m1", name);
175
176 uint32_t proto_idx = method_id.proto_idx_;
177 int32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700178 std::string signature(raw->CreateMethodSignature(proto_idx, &length));
179 ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700180 }
181
182 {
Ian Rogers0571d352011-11-03 19:51:38 -0700183 it.Next();
184 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700185
Ian Rogers0571d352011-11-03 19:51:38 -0700186 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700187 ASSERT_STREQ("m2", name);
188
189 uint32_t proto_idx = method_id.proto_idx_;
190 int32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700191 std::string signature(raw->CreateMethodSignature(proto_idx, &length));
192 ASSERT_EQ("(ZSC)LCreateMethodSignature;", signature);
193 }
194}
195
196TEST_F(DexFileTest, FindStringId) {
Ian Rogers33e95662013-05-20 20:29:14 -0700197 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800198 const DexFile* raw(OpenTestDexFile("CreateMethodSignature"));
199 ASSERT_TRUE(raw != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700200 EXPECT_EQ(1U, raw->NumClassDefs());
201
202 const char* strings[] = { "LCreateMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;",
203 "D", "I", "J", NULL };
204 for (size_t i = 0; strings[i] != NULL; i++) {
205 const char* str = strings[i];
206 const DexFile::StringId* str_id = raw->FindStringId(str);
207 const char* dex_str = raw->GetStringData(*str_id);
208 EXPECT_STREQ(dex_str, str);
209 }
210}
211
212TEST_F(DexFileTest, FindTypeId) {
213 for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) {
214 const char* type_str = java_lang_dex_file_->StringByTypeIdx(i);
215 const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str);
216 ASSERT_TRUE(type_str_id != NULL);
217 uint32_t type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id);
218 const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx);
219 ASSERT_TRUE(type_id != NULL);
220 EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id), i);
221 }
222}
223
224TEST_F(DexFileTest, FindProtoId) {
225 for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) {
226 const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i);
227 const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find);
228 std::vector<uint16_t> to_find_types;
229 if (to_find_tl != NULL) {
230 for (size_t j = 0; j < to_find_tl->Size(); j++) {
231 to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_);
232 }
233 }
234 const DexFile::ProtoId* found =
235 java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types);
236 ASSERT_TRUE(found != NULL);
237 EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i);
238 }
239}
240
241TEST_F(DexFileTest, FindMethodId) {
242 for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) {
243 const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i);
244 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
245 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
246 const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_);
247 const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature);
248 int32_t length;
249 ASSERT_TRUE(found != NULL) << "Didn't find method " << i << ": "
250 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
251 << java_lang_dex_file_->GetStringData(name)
252 << java_lang_dex_file_->CreateMethodSignature(to_find.proto_idx_, &length);
253 EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700254 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700255}
256
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800257TEST_F(DexFileTest, FindFieldId) {
258 for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) {
259 const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i);
260 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
261 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
262 const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_);
263 const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type);
264 ASSERT_TRUE(found != NULL) << "Didn't find field " << i << ": "
265 << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " "
266 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
267 << java_lang_dex_file_->GetStringData(name);
268 EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i);
269 }
270}
271
Carl Shapiro1fb86202011-06-27 17:43:13 -0700272} // namespace art