blob: f075feafcb607282192d56988cd349f7dc97ac01 [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) {
Elliott Hughes4d6850c2012-01-18 15:55:06 -080027 const DexFile* dex(OpenTestDexFile("Nested"));
28 ASSERT_TRUE(dex != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070029}
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030
Brian Carlstrom9f30b382011-08-28 22:41:38 -070031// Although this is the same content logically as the Nested test dex,
32// the DexFileHeader test is sensitive to subtle changes in the
33// contents due to the checksum etc, so we embed the exact input here.
34//
35// class Nested {
36// class Inner {
37// }
38// }
39static const char kRawDex[] =
40 "ZGV4CjAzNQAQedgAe7gM1B/WHsWJ6L7lGAISGC7yjD2IAwAAcAAAAHhWNBIAAAAAAAAAAMQCAAAP"
41 "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAAMAAADoAAAAAgAAAAABAABIAgAAQAEAAK4B"
42 "AAC2AQAAvQEAAM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AAACAAAAqwIAAAAQAAABAAAAxAIAAA==";
56
57TEST_F(DexFileTest, Header) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070058 ScratchFile tmp;
59 UniquePtr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename()));
Elliott Hughes90a33692011-08-30 13:27:07 -070060 ASSERT_TRUE(raw.get() != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070061
Brian Carlstromf615a612011-07-23 12:50:34 -070062 const DexFile::Header& header = raw->GetHeader();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070063 // TODO: header.magic_
64 EXPECT_EQ(0x00d87910U, header.checksum_);
65 // TODO: header.signature_
66 EXPECT_EQ(904U, header.file_size_);
67 EXPECT_EQ(112U, header.header_size_);
68 EXPECT_EQ(0U, header.link_size_);
69 EXPECT_EQ(0U, header.link_off_);
70 EXPECT_EQ(15U, header.string_ids_size_);
71 EXPECT_EQ(112U, header.string_ids_off_);
72 EXPECT_EQ(7U, header.type_ids_size_);
73 EXPECT_EQ(172U, header.type_ids_off_);
74 EXPECT_EQ(2U, header.proto_ids_size_);
75 EXPECT_EQ(200U, header.proto_ids_off_);
76 EXPECT_EQ(1U, header.field_ids_size_);
77 EXPECT_EQ(224U, header.field_ids_off_);
78 EXPECT_EQ(3U, header.method_ids_size_);
79 EXPECT_EQ(232U, header.method_ids_off_);
80 EXPECT_EQ(2U, header.class_defs_size_);
81 EXPECT_EQ(256U, header.class_defs_off_);
82 EXPECT_EQ(584U, header.data_size_);
83 EXPECT_EQ(320U, header.data_off_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -080084
85 EXPECT_EQ(header.checksum_, raw->GetLocationChecksum());
86}
87
88TEST_F(DexFileTest, GetLocationChecksum) {
89 const DexFile* raw(OpenTestDexFile("Main"));
90 EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum());
91}
92
93TEST_F(DexFileTest, GetChecksum) {
94 uint32_t checksum;
95 EXPECT_TRUE(DexFile::GetChecksum(GetLibCoreDexFileName(), checksum));
96 EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksum);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070097}
98
Brian Carlstrom9f30b382011-08-28 22:41:38 -070099TEST_F(DexFileTest, ClassDefs) {
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800100 const DexFile* raw(OpenTestDexFile("Nested"));
101 ASSERT_TRUE(raw != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700102 EXPECT_EQ(2U, raw->NumClassDefs());
103
Brian Carlstromf615a612011-07-23 12:50:34 -0700104 const DexFile::ClassDef& c0 = raw->GetClassDef(0);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700105 EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0));
106
Brian Carlstromf615a612011-07-23 12:50:34 -0700107 const DexFile::ClassDef& c1 = raw->GetClassDef(1);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700108 EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1));
Carl Shapiro1fb86202011-06-27 17:43:13 -0700109}
110
Ian Rogers0571d352011-11-03 19:51:38 -0700111TEST_F(DexFileTest, CreateMethodSignature) {
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800112 const DexFile* raw(OpenTestDexFile("CreateMethodSignature"));
113 ASSERT_TRUE(raw != NULL);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700114 EXPECT_EQ(1U, raw->NumClassDefs());
115
116 const DexFile::ClassDef& class_def = raw->GetClassDef(0);
Ian Rogers0571d352011-11-03 19:51:38 -0700117 ASSERT_STREQ("LCreateMethodSignature;", raw->GetClassDescriptor(class_def));
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700118
119 const byte* class_data = raw->GetClassData(class_def);
120 ASSERT_TRUE(class_data != NULL);
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800121 ClassDataItemIterator it(*raw, class_data);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700122
Ian Rogers0571d352011-11-03 19:51:38 -0700123 EXPECT_EQ(1u, it.NumDirectMethods());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700124
Ian Rogers0571d352011-11-03 19:51:38 -0700125 // Check the signature for the static initializer.
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700126 {
Ian Rogers0571d352011-11-03 19:51:38 -0700127 ASSERT_EQ(1U, it.NumDirectMethods());
128 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700129 uint32_t proto_idx = method_id.proto_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -0700130 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700131 ASSERT_STREQ("<init>", name);
132 int32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700133 std::string signature(raw->CreateMethodSignature(proto_idx, &length));
134 ASSERT_EQ("()V", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700135 }
136
137 // Check both virtual methods.
Ian Rogers0571d352011-11-03 19:51:38 -0700138 ASSERT_EQ(2U, it.NumVirtualMethods());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700139 {
Ian Rogers0571d352011-11-03 19:51:38 -0700140 it.Next();
141 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700142
Ian Rogers0571d352011-11-03 19:51:38 -0700143 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700144 ASSERT_STREQ("m1", name);
145
146 uint32_t proto_idx = method_id.proto_idx_;
147 int32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700148 std::string signature(raw->CreateMethodSignature(proto_idx, &length));
149 ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700150 }
151
152 {
Ian Rogers0571d352011-11-03 19:51:38 -0700153 it.Next();
154 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700155
Ian Rogers0571d352011-11-03 19:51:38 -0700156 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700157 ASSERT_STREQ("m2", name);
158
159 uint32_t proto_idx = method_id.proto_idx_;
160 int32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700161 std::string signature(raw->CreateMethodSignature(proto_idx, &length));
162 ASSERT_EQ("(ZSC)LCreateMethodSignature;", signature);
163 }
164}
165
166TEST_F(DexFileTest, FindStringId) {
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800167 const DexFile* raw(OpenTestDexFile("CreateMethodSignature"));
168 ASSERT_TRUE(raw != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700169 EXPECT_EQ(1U, raw->NumClassDefs());
170
171 const char* strings[] = { "LCreateMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;",
172 "D", "I", "J", NULL };
173 for (size_t i = 0; strings[i] != NULL; i++) {
174 const char* str = strings[i];
175 const DexFile::StringId* str_id = raw->FindStringId(str);
176 const char* dex_str = raw->GetStringData(*str_id);
177 EXPECT_STREQ(dex_str, str);
178 }
179}
180
181TEST_F(DexFileTest, FindTypeId) {
182 for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) {
183 const char* type_str = java_lang_dex_file_->StringByTypeIdx(i);
184 const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str);
185 ASSERT_TRUE(type_str_id != NULL);
186 uint32_t type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id);
187 const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx);
188 ASSERT_TRUE(type_id != NULL);
189 EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id), i);
190 }
191}
192
193TEST_F(DexFileTest, FindProtoId) {
194 for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) {
195 const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i);
196 const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find);
197 std::vector<uint16_t> to_find_types;
198 if (to_find_tl != NULL) {
199 for (size_t j = 0; j < to_find_tl->Size(); j++) {
200 to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_);
201 }
202 }
203 const DexFile::ProtoId* found =
204 java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types);
205 ASSERT_TRUE(found != NULL);
206 EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i);
207 }
208}
209
210TEST_F(DexFileTest, FindMethodId) {
211 for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) {
212 const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i);
213 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
214 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
215 const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_);
216 const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature);
217 int32_t length;
218 ASSERT_TRUE(found != NULL) << "Didn't find method " << i << ": "
219 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
220 << java_lang_dex_file_->GetStringData(name)
221 << java_lang_dex_file_->CreateMethodSignature(to_find.proto_idx_, &length);
222 EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700223 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700224}
225
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800226TEST_F(DexFileTest, FindFieldId) {
227 for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) {
228 const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i);
229 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
230 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
231 const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_);
232 const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type);
233 ASSERT_TRUE(found != NULL) << "Didn't find field " << i << ": "
234 << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " "
235 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
236 << java_lang_dex_file_->GetStringData(name);
237 EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i);
238 }
239}
240
Carl Shapiro1fb86202011-06-27 17:43:13 -0700241} // namespace art