blob: 37c601cf363994bd8979111e3417e07827d8b1c1 [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>
8#include "gtest/gtest.h"
9
10namespace art {
11
Brian Carlstromf615a612011-07-23 12:50:34 -070012TEST(DexFileTest, Open) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070013 scoped_ptr<const DexFile> dex(OpenDexFileBase64(kNestedDex, "kNestedDex"));
Brian Carlstromf615a612011-07-23 12:50:34 -070014 ASSERT_TRUE(dex != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070015}
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016
Brian Carlstromf615a612011-07-23 12:50:34 -070017TEST(DexFileTest, Header) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070018 scoped_ptr<const DexFile> raw(OpenDexFileBase64(kNestedDex, "kNestedDex"));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070019 ASSERT_TRUE(raw != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070020
Brian Carlstromf615a612011-07-23 12:50:34 -070021 const DexFile::Header& header = raw->GetHeader();
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070022 // TODO: header.magic_
23 EXPECT_EQ(0x00d87910U, header.checksum_);
24 // TODO: header.signature_
25 EXPECT_EQ(904U, header.file_size_);
26 EXPECT_EQ(112U, header.header_size_);
27 EXPECT_EQ(0U, header.link_size_);
28 EXPECT_EQ(0U, header.link_off_);
29 EXPECT_EQ(15U, header.string_ids_size_);
30 EXPECT_EQ(112U, header.string_ids_off_);
31 EXPECT_EQ(7U, header.type_ids_size_);
32 EXPECT_EQ(172U, header.type_ids_off_);
33 EXPECT_EQ(2U, header.proto_ids_size_);
34 EXPECT_EQ(200U, header.proto_ids_off_);
35 EXPECT_EQ(1U, header.field_ids_size_);
36 EXPECT_EQ(224U, header.field_ids_off_);
37 EXPECT_EQ(3U, header.method_ids_size_);
38 EXPECT_EQ(232U, header.method_ids_off_);
39 EXPECT_EQ(2U, header.class_defs_size_);
40 EXPECT_EQ(256U, header.class_defs_off_);
41 EXPECT_EQ(584U, header.data_size_);
42 EXPECT_EQ(320U, header.data_off_);
43}
44
Brian Carlstromf615a612011-07-23 12:50:34 -070045TEST(DexFileTest, ClassDefs) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070046 scoped_ptr<const DexFile> raw(OpenDexFileBase64(kNestedDex, "kNestedDex"));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070047 ASSERT_TRUE(raw != NULL);
48 EXPECT_EQ(2U, raw->NumClassDefs());
49
Brian Carlstromf615a612011-07-23 12:50:34 -070050 const DexFile::ClassDef& c0 = raw->GetClassDef(0);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070051 EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0));
52
Brian Carlstromf615a612011-07-23 12:50:34 -070053 const DexFile::ClassDef& c1 = raw->GetClassDef(1);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070054 EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1));
Carl Shapiro1fb86202011-06-27 17:43:13 -070055}
56
Carl Shapiro419ec7b2011-08-03 14:48:33 -070057TEST(DexFileTest, CreateMethodDescriptor) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070058 scoped_ptr<const DexFile> raw(OpenDexFileBase64(kCreateMethodDescriptorDex, "kCreateMethodDescriptorDex"));
Carl Shapiro419ec7b2011-08-03 14:48:33 -070059 ASSERT_TRUE(raw != NULL);
60 EXPECT_EQ(1U, raw->NumClassDefs());
61
62 const DexFile::ClassDef& class_def = raw->GetClassDef(0);
63 ASSERT_STREQ("LCreateMethodDescriptor;", raw->GetClassDescriptor(class_def));
64
65 const byte* class_data = raw->GetClassData(class_def);
66 ASSERT_TRUE(class_data != NULL);
67 DexFile::ClassDataHeader header = raw->ReadClassDataHeader(&class_data);
68
69 EXPECT_EQ(1u, header.direct_methods_size_);
70
71 // Check the descriptor for the static initializer.
72 {
73 uint32_t last_idx = 0;
74 ASSERT_EQ(1U, header.direct_methods_size_);
75 DexFile::Method method;
76 raw->dexReadClassDataMethod(&class_data, &method, &last_idx);
77 const DexFile::MethodId& method_id = raw->GetMethodId(method.method_idx_);
78 uint32_t proto_idx = method_id.proto_idx_;
79 const char* name = raw->dexStringById(method_id.name_idx_);
80 ASSERT_STREQ("<init>", name);
81 int32_t length;
Elliott Hughes0c424cb2011-08-26 10:16:25 -070082 std::string descriptor(raw->CreateMethodDescriptor(proto_idx, &length));
83 ASSERT_EQ("()V", descriptor);
Carl Shapiro419ec7b2011-08-03 14:48:33 -070084 }
85
86 // Check both virtual methods.
87 ASSERT_EQ(2U, header.virtual_methods_size_);
88 uint32_t last_idx = 0;
89
90 {
91 DexFile::Method method;
92 raw->dexReadClassDataMethod(&class_data, &method, &last_idx);
93 const DexFile::MethodId& method_id = raw->GetMethodId(method.method_idx_);
94
95 const char* name = raw->dexStringById(method_id.name_idx_);
96 ASSERT_STREQ("m1", name);
97
98 uint32_t proto_idx = method_id.proto_idx_;
99 int32_t length;
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700100 std::string descriptor(raw->CreateMethodDescriptor(proto_idx, &length));
101 ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", descriptor);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700102 }
103
104 {
105 DexFile::Method method;
106 raw->dexReadClassDataMethod(&class_data, &method, &last_idx);
107 const DexFile::MethodId& method_id = raw->GetMethodId(method.method_idx_);
108
109 const char* name = raw->dexStringById(method_id.name_idx_);
110 ASSERT_STREQ("m2", name);
111
112 uint32_t proto_idx = method_id.proto_idx_;
113 int32_t length;
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700114 std::string descriptor(raw->CreateMethodDescriptor(proto_idx, &length));
115 ASSERT_EQ("(ZSC)LCreateMethodDescriptor;", descriptor);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700116 }
117
118}
119
Carl Shapiro1fb86202011-06-27 17:43:13 -0700120} // namespace art