Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "dex_file.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 4 | |
| 5 | #include <stdio.h> |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 6 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 7 | #include "UniquePtr.h" |
| 8 | #include "common_test.h" |
| 9 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 10 | namespace art { |
| 11 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 12 | class DexFileTest : public CommonTest {}; |
| 13 | |
| 14 | TEST_F(DexFileTest, Open) { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 15 | UniquePtr<const DexFile> dex(OpenTestDexFile("Nested")); |
| 16 | ASSERT_TRUE(dex.get() != NULL); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 17 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 19 | // Although this is the same content logically as the Nested test dex, |
| 20 | // the DexFileHeader test is sensitive to subtle changes in the |
| 21 | // contents due to the checksum etc, so we embed the exact input here. |
| 22 | // |
| 23 | // class Nested { |
| 24 | // class Inner { |
| 25 | // } |
| 26 | // } |
| 27 | static const char kRawDex[] = |
| 28 | "ZGV4CjAzNQAQedgAe7gM1B/WHsWJ6L7lGAISGC7yjD2IAwAAcAAAAHhWNBIAAAAAAAAAAMQCAAAP" |
| 29 | "AAAAcAAAAAcA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AAAAEAAAMQ" |
| 42 | "AAACAAAAQAEAAAEgAAACAAAAVAEAAAYgAAACAAAAiAEAAAEQAAABAAAAqAEAAAIgAAAPAAAArgEA" |
| 43 | "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA=="; |
| 44 | |
| 45 | TEST_F(DexFileTest, Header) { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 46 | UniquePtr<const DexFile> raw(OpenDexFileBase64(kRawDex, "kRawDex")); |
| 47 | ASSERT_TRUE(raw.get() != NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 48 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 49 | const DexFile::Header& header = raw->GetHeader(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 50 | // TODO: header.magic_ |
| 51 | EXPECT_EQ(0x00d87910U, header.checksum_); |
| 52 | // TODO: header.signature_ |
| 53 | EXPECT_EQ(904U, header.file_size_); |
| 54 | EXPECT_EQ(112U, header.header_size_); |
| 55 | EXPECT_EQ(0U, header.link_size_); |
| 56 | EXPECT_EQ(0U, header.link_off_); |
| 57 | EXPECT_EQ(15U, header.string_ids_size_); |
| 58 | EXPECT_EQ(112U, header.string_ids_off_); |
| 59 | EXPECT_EQ(7U, header.type_ids_size_); |
| 60 | EXPECT_EQ(172U, header.type_ids_off_); |
| 61 | EXPECT_EQ(2U, header.proto_ids_size_); |
| 62 | EXPECT_EQ(200U, header.proto_ids_off_); |
| 63 | EXPECT_EQ(1U, header.field_ids_size_); |
| 64 | EXPECT_EQ(224U, header.field_ids_off_); |
| 65 | EXPECT_EQ(3U, header.method_ids_size_); |
| 66 | EXPECT_EQ(232U, header.method_ids_off_); |
| 67 | EXPECT_EQ(2U, header.class_defs_size_); |
| 68 | EXPECT_EQ(256U, header.class_defs_off_); |
| 69 | EXPECT_EQ(584U, header.data_size_); |
| 70 | EXPECT_EQ(320U, header.data_off_); |
| 71 | } |
| 72 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 73 | TEST_F(DexFileTest, ClassDefs) { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 74 | UniquePtr<const DexFile> raw(OpenTestDexFile("Nested")); |
| 75 | ASSERT_TRUE(raw.get() != NULL); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 76 | EXPECT_EQ(2U, raw->NumClassDefs()); |
| 77 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 78 | const DexFile::ClassDef& c0 = raw->GetClassDef(0); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 79 | EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0)); |
| 80 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 81 | const DexFile::ClassDef& c1 = raw->GetClassDef(1); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 82 | EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1)); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 85 | TEST_F(DexFileTest, CreateMethodDescriptor) { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 86 | UniquePtr<const DexFile> raw(OpenTestDexFile("CreateMethodDescriptor")); |
| 87 | ASSERT_TRUE(raw.get() != NULL); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 88 | EXPECT_EQ(1U, raw->NumClassDefs()); |
| 89 | |
| 90 | const DexFile::ClassDef& class_def = raw->GetClassDef(0); |
| 91 | ASSERT_STREQ("LCreateMethodDescriptor;", raw->GetClassDescriptor(class_def)); |
| 92 | |
| 93 | const byte* class_data = raw->GetClassData(class_def); |
| 94 | ASSERT_TRUE(class_data != NULL); |
| 95 | DexFile::ClassDataHeader header = raw->ReadClassDataHeader(&class_data); |
| 96 | |
| 97 | EXPECT_EQ(1u, header.direct_methods_size_); |
| 98 | |
| 99 | // Check the descriptor for the static initializer. |
| 100 | { |
| 101 | uint32_t last_idx = 0; |
| 102 | ASSERT_EQ(1U, header.direct_methods_size_); |
| 103 | DexFile::Method method; |
| 104 | raw->dexReadClassDataMethod(&class_data, &method, &last_idx); |
| 105 | const DexFile::MethodId& method_id = raw->GetMethodId(method.method_idx_); |
| 106 | uint32_t proto_idx = method_id.proto_idx_; |
| 107 | const char* name = raw->dexStringById(method_id.name_idx_); |
| 108 | ASSERT_STREQ("<init>", name); |
| 109 | int32_t length; |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 110 | std::string descriptor(raw->CreateMethodDescriptor(proto_idx, &length)); |
| 111 | ASSERT_EQ("()V", descriptor); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Check both virtual methods. |
| 115 | ASSERT_EQ(2U, header.virtual_methods_size_); |
| 116 | uint32_t last_idx = 0; |
| 117 | |
| 118 | { |
| 119 | DexFile::Method method; |
| 120 | raw->dexReadClassDataMethod(&class_data, &method, &last_idx); |
| 121 | const DexFile::MethodId& method_id = raw->GetMethodId(method.method_idx_); |
| 122 | |
| 123 | const char* name = raw->dexStringById(method_id.name_idx_); |
| 124 | ASSERT_STREQ("m1", name); |
| 125 | |
| 126 | uint32_t proto_idx = method_id.proto_idx_; |
| 127 | int32_t length; |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 128 | std::string descriptor(raw->CreateMethodDescriptor(proto_idx, &length)); |
| 129 | ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", descriptor); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | { |
| 133 | DexFile::Method method; |
| 134 | raw->dexReadClassDataMethod(&class_data, &method, &last_idx); |
| 135 | const DexFile::MethodId& method_id = raw->GetMethodId(method.method_idx_); |
| 136 | |
| 137 | const char* name = raw->dexStringById(method_id.name_idx_); |
| 138 | ASSERT_STREQ("m2", name); |
| 139 | |
| 140 | uint32_t proto_idx = method_id.proto_idx_; |
| 141 | int32_t length; |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 142 | std::string descriptor(raw->CreateMethodDescriptor(proto_idx, &length)); |
| 143 | ASSERT_EQ("(ZSC)LCreateMethodDescriptor;", descriptor); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | } |
| 147 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 148 | } // namespace art |