blob: 33c43c900abc59661364caa5d1c8915b10749540 [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) {
13 scoped_ptr<DexFile> dex(OpenDexFileBase64(kNestedDex));
14 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) {
18 scoped_ptr<DexFile> raw(OpenDexFileBase64(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) {
46 scoped_ptr<DexFile> raw(OpenDexFileBase64(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) {
58 scoped_ptr<DexFile> raw(OpenDexFileBase64(kCreateMethodDescriptorDex));
59 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;
82 scoped_ptr<const char> descriptor(raw->CreateMethodDescriptor(proto_idx,
83 &length));
84 ASSERT_STREQ("()V", descriptor.get());
85 }
86
87 // Check both virtual methods.
88 ASSERT_EQ(2U, header.virtual_methods_size_);
89 uint32_t last_idx = 0;
90
91 {
92 DexFile::Method method;
93 raw->dexReadClassDataMethod(&class_data, &method, &last_idx);
94 const DexFile::MethodId& method_id = raw->GetMethodId(method.method_idx_);
95
96 const char* name = raw->dexStringById(method_id.name_idx_);
97 ASSERT_STREQ("m1", name);
98
99 uint32_t proto_idx = method_id.proto_idx_;
100 int32_t length;
101 scoped_ptr<const char> descriptor(raw->CreateMethodDescriptor(proto_idx,
102 &length));
103 ASSERT_STREQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", descriptor.get());
104 }
105
106 {
107 DexFile::Method method;
108 raw->dexReadClassDataMethod(&class_data, &method, &last_idx);
109 const DexFile::MethodId& method_id = raw->GetMethodId(method.method_idx_);
110
111 const char* name = raw->dexStringById(method_id.name_idx_);
112 ASSERT_STREQ("m2", name);
113
114 uint32_t proto_idx = method_id.proto_idx_;
115 int32_t length;
116 scoped_ptr<const char> descriptor(raw->CreateMethodDescriptor(proto_idx,
117 &length));
118 ASSERT_STREQ("(ZSC)LCreateMethodDescriptor;", descriptor.get());
119 }
120
121}
122
Carl Shapiro1fb86202011-06-27 17:43:13 -0700123} // namespace art