blob: 1e876bab0385de1352f435cd518d5da0a9f72bb3 [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 "AAC2AQAAvQ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AAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA==";
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_);
84}
85
Brian Carlstrom9f30b382011-08-28 22:41:38 -070086TEST_F(DexFileTest, ClassDefs) {
Elliott Hughes4d6850c2012-01-18 15:55:06 -080087 const DexFile* raw(OpenTestDexFile("Nested"));
88 ASSERT_TRUE(raw != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070089 EXPECT_EQ(2U, raw->NumClassDefs());
90
Brian Carlstromf615a612011-07-23 12:50:34 -070091 const DexFile::ClassDef& c0 = raw->GetClassDef(0);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070092 EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0));
93
Brian Carlstromf615a612011-07-23 12:50:34 -070094 const DexFile::ClassDef& c1 = raw->GetClassDef(1);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070095 EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1));
Carl Shapiro1fb86202011-06-27 17:43:13 -070096}
97
Ian Rogers0571d352011-11-03 19:51:38 -070098TEST_F(DexFileTest, CreateMethodSignature) {
Elliott Hughes4d6850c2012-01-18 15:55:06 -080099 const DexFile* raw(OpenTestDexFile("CreateMethodSignature"));
100 ASSERT_TRUE(raw != NULL);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700101 EXPECT_EQ(1U, raw->NumClassDefs());
102
103 const DexFile::ClassDef& class_def = raw->GetClassDef(0);
Ian Rogers0571d352011-11-03 19:51:38 -0700104 ASSERT_STREQ("LCreateMethodSignature;", raw->GetClassDescriptor(class_def));
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700105
106 const byte* class_data = raw->GetClassData(class_def);
107 ASSERT_TRUE(class_data != NULL);
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800108 ClassDataItemIterator it(*raw, class_data);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700109
Ian Rogers0571d352011-11-03 19:51:38 -0700110 EXPECT_EQ(1u, it.NumDirectMethods());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700111
Ian Rogers0571d352011-11-03 19:51:38 -0700112 // Check the signature for the static initializer.
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700113 {
Ian Rogers0571d352011-11-03 19:51:38 -0700114 ASSERT_EQ(1U, it.NumDirectMethods());
115 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700116 uint32_t proto_idx = method_id.proto_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -0700117 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700118 ASSERT_STREQ("<init>", name);
119 int32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700120 std::string signature(raw->CreateMethodSignature(proto_idx, &length));
121 ASSERT_EQ("()V", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700122 }
123
124 // Check both virtual methods.
Ian Rogers0571d352011-11-03 19:51:38 -0700125 ASSERT_EQ(2U, it.NumVirtualMethods());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700126 {
Ian Rogers0571d352011-11-03 19:51:38 -0700127 it.Next();
128 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700129
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("m1", name);
132
133 uint32_t proto_idx = method_id.proto_idx_;
134 int32_t length;
Ian Rogers0571d352011-11-03 19:51:38 -0700135 std::string signature(raw->CreateMethodSignature(proto_idx, &length));
136 ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700137 }
138
139 {
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("m2", 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("(ZSC)LCreateMethodSignature;", signature);
150 }
151}
152
153TEST_F(DexFileTest, FindStringId) {
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800154 const DexFile* raw(OpenTestDexFile("CreateMethodSignature"));
155 ASSERT_TRUE(raw != NULL);
Ian Rogers0571d352011-11-03 19:51:38 -0700156 EXPECT_EQ(1U, raw->NumClassDefs());
157
158 const char* strings[] = { "LCreateMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;",
159 "D", "I", "J", NULL };
160 for (size_t i = 0; strings[i] != NULL; i++) {
161 const char* str = strings[i];
162 const DexFile::StringId* str_id = raw->FindStringId(str);
163 const char* dex_str = raw->GetStringData(*str_id);
164 EXPECT_STREQ(dex_str, str);
165 }
166}
167
168TEST_F(DexFileTest, FindTypeId) {
169 for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) {
170 const char* type_str = java_lang_dex_file_->StringByTypeIdx(i);
171 const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str);
172 ASSERT_TRUE(type_str_id != NULL);
173 uint32_t type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id);
174 const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx);
175 ASSERT_TRUE(type_id != NULL);
176 EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id), i);
177 }
178}
179
180TEST_F(DexFileTest, FindProtoId) {
181 for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) {
182 const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i);
183 const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find);
184 std::vector<uint16_t> to_find_types;
185 if (to_find_tl != NULL) {
186 for (size_t j = 0; j < to_find_tl->Size(); j++) {
187 to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_);
188 }
189 }
190 const DexFile::ProtoId* found =
191 java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types);
192 ASSERT_TRUE(found != NULL);
193 EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i);
194 }
195}
196
197TEST_F(DexFileTest, FindMethodId) {
198 for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) {
199 const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i);
200 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
201 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
202 const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_);
203 const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature);
204 int32_t length;
205 ASSERT_TRUE(found != NULL) << "Didn't find method " << i << ": "
206 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
207 << java_lang_dex_file_->GetStringData(name)
208 << java_lang_dex_file_->CreateMethodSignature(to_find.proto_idx_, &length);
209 EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700210 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700211}
212
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800213TEST_F(DexFileTest, FindFieldId) {
214 for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) {
215 const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i);
216 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
217 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
218 const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_);
219 const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type);
220 ASSERT_TRUE(found != NULL) << "Didn't find field " << i << ": "
221 << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " "
222 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
223 << java_lang_dex_file_->GetStringData(name);
224 EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i);
225 }
226}
227
Carl Shapiro1fb86202011-06-27 17:43:13 -0700228} // namespace art