blob: 96d0477707d2439bf241f4cfffaf3eab7c2efb11 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "common_test.h"
4#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07005#include "scoped_ptr.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07006
7#include <stdio.h>
Carl Shapiro1fb86202011-06-27 17:43:13 -07008
9namespace art {
10
Brian Carlstrom9f30b382011-08-28 22:41:38 -070011class DexFileTest : public CommonTest {};
12
13TEST_F(DexFileTest, Open) {
14 scoped_ptr<const DexFile> dex(OpenTestDexFile("Nested"));
Brian Carlstromf615a612011-07-23 12:50:34 -070015 ASSERT_TRUE(dex != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070016}
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017
Brian Carlstrom9f30b382011-08-28 22:41:38 -070018// Although this is the same content logically as the Nested test dex,
19// the DexFileHeader test is sensitive to subtle changes in the
20// contents due to the checksum etc, so we embed the exact input here.
21//
22// class Nested {
23// class Inner {
24// }
25// }
26static const char kRawDex[] =
27 "ZGV4CjAzNQAQedgAe7gM1B/WHsWJ6L7lGAISGC7yjD2IAwAAcAAAAHhWNBIAAAAAAAAAAMQCAAAP"
28 "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAAMAAADoAAAAAgAAAAABAABIAgAAQAEAAK4B"
29 "AAC2AQAAvQEAAM0BAADXAQAA+wEAABsCAAA+AgAAUgIAAF8CAABiAgAAZgIAAHMCAAB5AgAAgQIA"
30 "AAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABgAAAAAAAAAKAAAABgAAAKgBAAAAAAEA"
31 "DQAAAAAAAQAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAIAAAAiAEAAKsCAAAA"
32 "AAAAAQAAAAAAAAAFAAAAAAAAAAgAAACYAQAAuAIAAAAAAAACAAAAlAIAAJoCAAABAAAAowIAAAIA"
33 "AgABAAAAiAIAAAYAAABbAQAAcBACAAAADgABAAEAAQAAAI4CAAAEAAAAcBACAAAADgBAAQAAAAAA"
34 "AAAAAAAAAAAATAEAAAAAAAAAAAAAAAAAAAEAAAABAAY8aW5pdD4ABUlubmVyAA5MTmVzdGVkJElu"
35 "bmVyOwAITE5lc3RlZDsAIkxkYWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2"
36 "aWsvYW5ub3RhdGlvbi9Jbm5lckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNz"
37 "ZXM7ABJMamF2YS9sYW5nL09iamVjdDsAC05lc3RlZC5qYXZhAAFWAAJWTAALYWNjZXNzRmxhZ3MA"
38 "BG5hbWUABnRoaXMkMAAFdmFsdWUAAgEABw4AAQAHDjwAAgIBDhgBAgMCCwQADBcBAgQBDhwBGAAA"
39 "AQEAAJAgAICABNQCAAABAAGAgATwAgAAEAAAAAAAAAABAAAAAAAAAAEAAAAPAAAAcAAAAAIAAAAH"
40 "AAAArAAAAAMAAAACAAAAyAAAAAQAAAABAAAA4AAAAAUAAAADAAAA6AAAAAYAAAACAAAAAAEAAAMQ"
41 "AAACAAAAQAEAAAEgAAACAAAAVAEAAAYgAAACAAAAiAEAAAEQAAABAAAAqAEAAAIgAAAPAAAArgEA"
42 "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA==";
43
44TEST_F(DexFileTest, Header) {
45 scoped_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, "kRawDex"));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070046 ASSERT_TRUE(raw != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070047
Brian Carlstromf615a612011-07-23 12:50:34 -070048 const DexFile::Header& header = raw->GetHeader();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070049 // TODO: header.magic_
50 EXPECT_EQ(0x00d87910U, header.checksum_);
51 // TODO: header.signature_
52 EXPECT_EQ(904U, header.file_size_);
53 EXPECT_EQ(112U, header.header_size_);
54 EXPECT_EQ(0U, header.link_size_);
55 EXPECT_EQ(0U, header.link_off_);
56 EXPECT_EQ(15U, header.string_ids_size_);
57 EXPECT_EQ(112U, header.string_ids_off_);
58 EXPECT_EQ(7U, header.type_ids_size_);
59 EXPECT_EQ(172U, header.type_ids_off_);
60 EXPECT_EQ(2U, header.proto_ids_size_);
61 EXPECT_EQ(200U, header.proto_ids_off_);
62 EXPECT_EQ(1U, header.field_ids_size_);
63 EXPECT_EQ(224U, header.field_ids_off_);
64 EXPECT_EQ(3U, header.method_ids_size_);
65 EXPECT_EQ(232U, header.method_ids_off_);
66 EXPECT_EQ(2U, header.class_defs_size_);
67 EXPECT_EQ(256U, header.class_defs_off_);
68 EXPECT_EQ(584U, header.data_size_);
69 EXPECT_EQ(320U, header.data_off_);
70}
71
Brian Carlstrom9f30b382011-08-28 22:41:38 -070072TEST_F(DexFileTest, ClassDefs) {
73 scoped_ptr<const DexFile> raw(OpenTestDexFile("Nested"));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070074 ASSERT_TRUE(raw != NULL);
75 EXPECT_EQ(2U, raw->NumClassDefs());
76
Brian Carlstromf615a612011-07-23 12:50:34 -070077 const DexFile::ClassDef& c0 = raw->GetClassDef(0);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070078 EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0));
79
Brian Carlstromf615a612011-07-23 12:50:34 -070080 const DexFile::ClassDef& c1 = raw->GetClassDef(1);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070081 EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1));
Carl Shapiro1fb86202011-06-27 17:43:13 -070082}
83
Brian Carlstrom9f30b382011-08-28 22:41:38 -070084TEST_F(DexFileTest, CreateMethodDescriptor) {
85 scoped_ptr<const DexFile> raw(OpenTestDexFile("CreateMethodDescriptor"));
Carl Shapiro419ec7b2011-08-03 14:48:33 -070086 ASSERT_TRUE(raw != NULL);
87 EXPECT_EQ(1U, raw->NumClassDefs());
88
89 const DexFile::ClassDef& class_def = raw->GetClassDef(0);
90 ASSERT_STREQ("LCreateMethodDescriptor;", raw->GetClassDescriptor(class_def));
91
92 const byte* class_data = raw->GetClassData(class_def);
93 ASSERT_TRUE(class_data != NULL);
94 DexFile::ClassDataHeader header = raw->ReadClassDataHeader(&class_data);
95
96 EXPECT_EQ(1u, header.direct_methods_size_);
97
98 // Check the descriptor for the static initializer.
99 {
100 uint32_t last_idx = 0;
101 ASSERT_EQ(1U, header.direct_methods_size_);
102 DexFile::Method method;
103 raw->dexReadClassDataMethod(&class_data, &method, &last_idx);
104 const DexFile::MethodId& method_id = raw->GetMethodId(method.method_idx_);
105 uint32_t proto_idx = method_id.proto_idx_;
106 const char* name = raw->dexStringById(method_id.name_idx_);
107 ASSERT_STREQ("<init>", name);
108 int32_t length;
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700109 std::string descriptor(raw->CreateMethodDescriptor(proto_idx, &length));
110 ASSERT_EQ("()V", descriptor);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700111 }
112
113 // Check both virtual methods.
114 ASSERT_EQ(2U, header.virtual_methods_size_);
115 uint32_t last_idx = 0;
116
117 {
118 DexFile::Method method;
119 raw->dexReadClassDataMethod(&class_data, &method, &last_idx);
120 const DexFile::MethodId& method_id = raw->GetMethodId(method.method_idx_);
121
122 const char* name = raw->dexStringById(method_id.name_idx_);
123 ASSERT_STREQ("m1", name);
124
125 uint32_t proto_idx = method_id.proto_idx_;
126 int32_t length;
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700127 std::string descriptor(raw->CreateMethodDescriptor(proto_idx, &length));
128 ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", descriptor);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700129 }
130
131 {
132 DexFile::Method method;
133 raw->dexReadClassDataMethod(&class_data, &method, &last_idx);
134 const DexFile::MethodId& method_id = raw->GetMethodId(method.method_idx_);
135
136 const char* name = raw->dexStringById(method_id.name_idx_);
137 ASSERT_STREQ("m2", name);
138
139 uint32_t proto_idx = method_id.proto_idx_;
140 int32_t length;
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700141 std::string descriptor(raw->CreateMethodDescriptor(proto_idx, &length));
142 ASSERT_EQ("(ZSC)LCreateMethodDescriptor;", descriptor);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700143 }
144
145}
146
Carl Shapiro1fb86202011-06-27 17:43:13 -0700147} // namespace art