blob: e7899ecf31aceb4dca9222103f43a8b881309e91 [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
67 UniquePtr<File> file(OS::OpenFile(location.c_str(), true));
68 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);
78 return dex_file;
79}
80
Brian Carlstrom9f30b382011-08-28 22:41:38 -070081TEST_F(DexFileTest, Header) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070082 ScratchFile tmp;
83 UniquePtr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename()));
Elliott Hughes90a33692011-08-30 13:27:07 -070084 ASSERT_TRUE(raw.get() != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070085
Brian Carlstromf615a612011-07-23 12:50:34 -070086 const DexFile::Header& header = raw->GetHeader();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070087 // TODO: header.magic_
88 EXPECT_EQ(0x00d87910U, header.checksum_);
89 // TODO: header.signature_
90 EXPECT_EQ(904U, header.file_size_);
91 EXPECT_EQ(112U, header.header_size_);
92 EXPECT_EQ(0U, header.link_size_);
93 EXPECT_EQ(0U, header.link_off_);
94 EXPECT_EQ(15U, header.string_ids_size_);
95 EXPECT_EQ(112U, header.string_ids_off_);
96 EXPECT_EQ(7U, header.type_ids_size_);
97 EXPECT_EQ(172U, header.type_ids_off_);
98 EXPECT_EQ(2U, header.proto_ids_size_);
99 EXPECT_EQ(200U, header.proto_ids_off_);
100 EXPECT_EQ(1U, header.field_ids_size_);
101 EXPECT_EQ(224U, header.field_ids_off_);
102 EXPECT_EQ(3U, header.method_ids_size_);
103 EXPECT_EQ(232U, header.method_ids_off_);
104 EXPECT_EQ(2U, header.class_defs_size_);
105 EXPECT_EQ(256U, header.class_defs_off_);
106 EXPECT_EQ(584U, header.data_size_);
107 EXPECT_EQ(320U, header.data_off_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800108
109 EXPECT_EQ(header.checksum_, raw->GetLocationChecksum());
110}
111
112TEST_F(DexFileTest, GetLocationChecksum) {
Ian Rogers33e95662013-05-20 20:29:14 -0700113 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800114 const DexFile* raw(OpenTestDexFile("Main"));
115 EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum());
116}
117
118TEST_F(DexFileTest, GetChecksum) {
119 uint32_t checksum;
Ian Rogers33e95662013-05-20 20:29:14 -0700120 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800121 EXPECT_TRUE(DexFile::GetChecksum(GetLibCoreDexFileName(), checksum));
122 EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksum);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700123}
124
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700125TEST_F(DexFileTest, ClassDefs) {
Ian Rogers33e95662013-05-20 20:29:14 -0700126 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800127 const DexFile* raw(OpenTestDexFile("Nested"));
128 ASSERT_TRUE(raw != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700129 EXPECT_EQ(2U, raw->NumClassDefs());
130
Brian Carlstromf615a612011-07-23 12:50:34 -0700131 const DexFile::ClassDef& c0 = raw->GetClassDef(0);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700132 EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0));
133
Brian Carlstromf615a612011-07-23 12:50:34 -0700134 const DexFile::ClassDef& c1 = raw->GetClassDef(1);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700135 EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1));
Carl Shapiro1fb86202011-06-27 17:43:13 -0700136}
137
Ian Rogers0571d352011-11-03 19:51:38 -0700138TEST_F(DexFileTest, CreateMethodSignature) {
Ian Rogers33e95662013-05-20 20:29:14 -0700139 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800140 const DexFile* raw(OpenTestDexFile("CreateMethodSignature"));
141 ASSERT_TRUE(raw != NULL);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700142 EXPECT_EQ(1U, raw->NumClassDefs());
143
144 const DexFile::ClassDef& class_def = raw->GetClassDef(0);
Ian Rogers0571d352011-11-03 19:51:38 -0700145 ASSERT_STREQ("LCreateMethodSignature;", raw->GetClassDescriptor(class_def));
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700146
147 const byte* class_data = raw->GetClassData(class_def);
148 ASSERT_TRUE(class_data != NULL);
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800149 ClassDataItemIterator it(*raw, class_data);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700150
Ian Rogers0571d352011-11-03 19:51:38 -0700151 EXPECT_EQ(1u, it.NumDirectMethods());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700152
Ian Rogers0571d352011-11-03 19:51:38 -0700153 // Check the signature for the static initializer.
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700154 {
Ian Rogers0571d352011-11-03 19:51:38 -0700155 ASSERT_EQ(1U, it.NumDirectMethods());
156 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700157 uint32_t proto_idx = method_id.proto_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -0700158 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700159 ASSERT_STREQ("<init>", name);
160 int32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700161 std::string signature(raw->CreateMethodSignature(proto_idx, &length));
162 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
174 uint32_t proto_idx = method_id.proto_idx_;
175 int32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700176 std::string signature(raw->CreateMethodSignature(proto_idx, &length));
177 ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700178 }
179
180 {
Ian Rogers0571d352011-11-03 19:51:38 -0700181 it.Next();
182 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700183
Ian Rogers0571d352011-11-03 19:51:38 -0700184 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700185 ASSERT_STREQ("m2", name);
186
187 uint32_t proto_idx = method_id.proto_idx_;
188 int32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700189 std::string signature(raw->CreateMethodSignature(proto_idx, &length));
190 ASSERT_EQ("(ZSC)LCreateMethodSignature;", signature);
191 }
192}
193
194TEST_F(DexFileTest, FindStringId) {
Ian Rogers33e95662013-05-20 20:29:14 -0700195 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800196 const DexFile* raw(OpenTestDexFile("CreateMethodSignature"));
197 ASSERT_TRUE(raw != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700198 EXPECT_EQ(1U, raw->NumClassDefs());
199
200 const char* strings[] = { "LCreateMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;",
201 "D", "I", "J", NULL };
202 for (size_t i = 0; strings[i] != NULL; i++) {
203 const char* str = strings[i];
204 const DexFile::StringId* str_id = raw->FindStringId(str);
205 const char* dex_str = raw->GetStringData(*str_id);
206 EXPECT_STREQ(dex_str, str);
207 }
208}
209
210TEST_F(DexFileTest, FindTypeId) {
211 for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) {
212 const char* type_str = java_lang_dex_file_->StringByTypeIdx(i);
213 const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str);
214 ASSERT_TRUE(type_str_id != NULL);
215 uint32_t type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id);
216 const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx);
217 ASSERT_TRUE(type_id != NULL);
218 EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id), i);
219 }
220}
221
222TEST_F(DexFileTest, FindProtoId) {
223 for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) {
224 const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i);
225 const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find);
226 std::vector<uint16_t> to_find_types;
227 if (to_find_tl != NULL) {
228 for (size_t j = 0; j < to_find_tl->Size(); j++) {
229 to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_);
230 }
231 }
232 const DexFile::ProtoId* found =
233 java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types);
234 ASSERT_TRUE(found != NULL);
235 EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i);
236 }
237}
238
239TEST_F(DexFileTest, FindMethodId) {
240 for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) {
241 const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i);
242 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
243 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
244 const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_);
245 const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature);
246 int32_t length;
247 ASSERT_TRUE(found != NULL) << "Didn't find method " << i << ": "
248 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
249 << java_lang_dex_file_->GetStringData(name)
250 << java_lang_dex_file_->CreateMethodSignature(to_find.proto_idx_, &length);
251 EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700252 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700253}
254
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800255TEST_F(DexFileTest, FindFieldId) {
256 for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) {
257 const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i);
258 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
259 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
260 const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_);
261 const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type);
262 ASSERT_TRUE(found != NULL) << "Didn't find field " << i << ": "
263 << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " "
264 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
265 << java_lang_dex_file_->GetStringData(name);
266 EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i);
267 }
268}
269
Carl Shapiro1fb86202011-06-27 17:43:13 -0700270} // namespace art