1/*
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 */
16
17#include "dex_file.h"
18
19#include "UniquePtr.h"
20#include "common_test.h"
21
22namespace art {
23
24class DexFileTest : public CommonTest {};
25
26TEST_F(DexFileTest, Open) {
27  ScopedObjectAccess soa(Thread::Current());
28  const DexFile* dex(OpenTestDexFile("Nested"));
29  ASSERT_TRUE(dex != NULL);
30}
31
32// 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  "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAAMAAADoAAAAAgAAAAABAABIAgAAQAEAAK4B"
43  "AAC2AQAAvQEAAM0BAADXAQAA+wEAABsCAAA+AgAAUgIAAF8CAABiAgAAZgIAAHMCAAB5AgAAgQIA"
44  "AAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABgAAAAAAAAAKAAAABgAAAKgBAAAAAAEA"
45  "DQAAAAAAAQAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAIAAAAiAEAAKsCAAAA"
46  "AAAAAQAAAAAAAAAFAAAAAAAAAAgAAACYAQAAuAIAAAAAAAACAAAAlAIAAJoCAAABAAAAowIAAAIA"
47  "AgABAAAAiAIAAAYAAABbAQAAcBACAAAADgABAAEAAQAAAI4CAAAEAAAAcBACAAAADgBAAQAAAAAA"
48  "AAAAAAAAAAAATAEAAAAAAAAAAAAAAAAAAAEAAAABAAY8aW5pdD4ABUlubmVyAA5MTmVzdGVkJElu"
49  "bmVyOwAITE5lc3RlZDsAIkxkYWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2"
50  "aWsvYW5ub3RhdGlvbi9Jbm5lckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNz"
51  "ZXM7ABJMamF2YS9sYW5nL09iamVjdDsAC05lc3RlZC5qYXZhAAFWAAJWTAALYWNjZXNzRmxhZ3MA"
52  "BG5hbWUABnRoaXMkMAAFdmFsdWUAAgEABw4AAQAHDjwAAgIBDhgBAgMCCwQADBcBAgQBDhwBGAAA"
53  "AQEAAJAgAICABNQCAAABAAGAgATwAgAAEAAAAAAAAAABAAAAAAAAAAEAAAAPAAAAcAAAAAIAAAAH"
54  "AAAArAAAAAMAAAACAAAAyAAAAAQAAAABAAAA4AAAAAUAAAADAAAA6AAAAAYAAAACAAAAAAEAAAMQ"
55  "AAACAAAAQAEAAAEgAAACAAAAVAEAAAYgAAACAAAAiAEAAAEQAAABAAAAqAEAAAIgAAAPAAAArgEA"
56  "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA==";
57
58static 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::CreateEmptyFile(location.c_str()));
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  EXPECT_EQ(PROT_READ, dex_file->GetPermissions());
79  EXPECT_TRUE(dex_file->IsReadOnly());
80  return dex_file;
81}
82
83TEST_F(DexFileTest, Header) {
84  ScratchFile tmp;
85  UniquePtr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename()));
86  ASSERT_TRUE(raw.get() != NULL);
87
88  const DexFile::Header& header = raw->GetHeader();
89  // 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_);
110
111  EXPECT_EQ(header.checksum_, raw->GetLocationChecksum());
112}
113
114TEST_F(DexFileTest, GetLocationChecksum) {
115  ScopedObjectAccess soa(Thread::Current());
116  const DexFile* raw(OpenTestDexFile("Main"));
117  EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum());
118}
119
120TEST_F(DexFileTest, GetChecksum) {
121  uint32_t checksum;
122  ScopedObjectAccess soa(Thread::Current());
123  EXPECT_TRUE(DexFile::GetChecksum(GetLibCoreDexFileName(), &checksum));
124  EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksum);
125}
126
127TEST_F(DexFileTest, ClassDefs) {
128  ScopedObjectAccess soa(Thread::Current());
129  const DexFile* raw(OpenTestDexFile("Nested"));
130  ASSERT_TRUE(raw != NULL);
131  EXPECT_EQ(2U, raw->NumClassDefs());
132
133  const DexFile::ClassDef& c0 = raw->GetClassDef(0);
134  EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0));
135
136  const DexFile::ClassDef& c1 = raw->GetClassDef(1);
137  EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1));
138}
139
140TEST_F(DexFileTest, CreateMethodSignature) {
141  ScopedObjectAccess soa(Thread::Current());
142  const DexFile* raw(OpenTestDexFile("CreateMethodSignature"));
143  ASSERT_TRUE(raw != NULL);
144  EXPECT_EQ(1U, raw->NumClassDefs());
145
146  const DexFile::ClassDef& class_def = raw->GetClassDef(0);
147  ASSERT_STREQ("LCreateMethodSignature;", raw->GetClassDescriptor(class_def));
148
149  const byte* class_data = raw->GetClassData(class_def);
150  ASSERT_TRUE(class_data != NULL);
151  ClassDataItemIterator it(*raw, class_data);
152
153  EXPECT_EQ(1u, it.NumDirectMethods());
154
155  // Check the signature for the static initializer.
156  {
157    ASSERT_EQ(1U, it.NumDirectMethods());
158    const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
159    uint32_t proto_idx = method_id.proto_idx_;
160    const char* name = raw->StringDataByIdx(method_id.name_idx_);
161    ASSERT_STREQ("<init>", name);
162    int32_t length;
163    std::string signature(raw->CreateMethodSignature(proto_idx, &length));
164    ASSERT_EQ("()V", signature);
165  }
166
167  // Check both virtual methods.
168  ASSERT_EQ(2U, it.NumVirtualMethods());
169  {
170    it.Next();
171    const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
172
173    const char* name = raw->StringDataByIdx(method_id.name_idx_);
174    ASSERT_STREQ("m1", name);
175
176    uint32_t proto_idx = method_id.proto_idx_;
177    int32_t length;
178    std::string signature(raw->CreateMethodSignature(proto_idx, &length));
179    ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", signature);
180  }
181
182  {
183    it.Next();
184    const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
185
186    const char* name = raw->StringDataByIdx(method_id.name_idx_);
187    ASSERT_STREQ("m2", name);
188
189    uint32_t proto_idx = method_id.proto_idx_;
190    int32_t length;
191    std::string signature(raw->CreateMethodSignature(proto_idx, &length));
192    ASSERT_EQ("(ZSC)LCreateMethodSignature;", signature);
193  }
194}
195
196TEST_F(DexFileTest, FindStringId) {
197  ScopedObjectAccess soa(Thread::Current());
198  const DexFile* raw(OpenTestDexFile("CreateMethodSignature"));
199  ASSERT_TRUE(raw != NULL);
200  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);
254  }
255}
256
257TEST_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
272}  // namespace art
273