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 "common_test.h" |
| 4 | #include "dex_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 5 | #include "scoped_ptr.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 6 | |
| 7 | #include <stdio.h> |
| 8 | #include "gtest/gtest.h" |
| 9 | |
| 10 | namespace art { |
| 11 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 12 | TEST(DexFileTest, Open) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame^] | 13 | scoped_ptr<const DexFile> dex(OpenDexFileBase64(kNestedDex, "kNestedDex")); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 14 | ASSERT_TRUE(dex != NULL); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 15 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 17 | TEST(DexFileTest, Header) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame^] | 18 | scoped_ptr<const DexFile> raw(OpenDexFileBase64(kNestedDex, "kNestedDex")); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 19 | ASSERT_TRUE(raw != NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 20 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 21 | const DexFile::Header& header = raw->GetHeader(); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 22 | // 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 Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 45 | TEST(DexFileTest, ClassDefs) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame^] | 46 | scoped_ptr<const DexFile> raw(OpenDexFileBase64(kNestedDex, "kNestedDex")); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 47 | ASSERT_TRUE(raw != NULL); |
| 48 | EXPECT_EQ(2U, raw->NumClassDefs()); |
| 49 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 50 | const DexFile::ClassDef& c0 = raw->GetClassDef(0); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 51 | EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0)); |
| 52 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 53 | const DexFile::ClassDef& c1 = raw->GetClassDef(1); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 54 | EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1)); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 57 | TEST(DexFileTest, CreateMethodDescriptor) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame^] | 58 | scoped_ptr<const DexFile> raw(OpenDexFileBase64(kCreateMethodDescriptorDex, "kCreateMethodDescriptorDex")); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 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; |
Elliott Hughes | e0fc0ef | 2011-08-12 17:39:17 -0700 | [diff] [blame] | 82 | scoped_array<const char> descriptor(raw->CreateMethodDescriptor(proto_idx, |
| 83 | &length)); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 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; |
Elliott Hughes | e0fc0ef | 2011-08-12 17:39:17 -0700 | [diff] [blame] | 101 | scoped_array<const char> descriptor(raw->CreateMethodDescriptor(proto_idx, |
| 102 | &length)); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 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; |
Elliott Hughes | e0fc0ef | 2011-08-12 17:39:17 -0700 | [diff] [blame] | 116 | scoped_array<const char> descriptor(raw->CreateMethodDescriptor(proto_idx, |
| 117 | &length)); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 118 | ASSERT_STREQ("(ZSC)LCreateMethodDescriptor;", descriptor.get()); |
| 119 | } |
| 120 | |
| 121 | } |
| 122 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 123 | } // namespace art |