Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Yong Li | 489c3a8 | 2020-03-19 18:43:06 +0000 | [diff] [blame] | 17 | #include <malloc.h> |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <unordered_map> |
| 23 | |
Sim Sun | 273d3f0 | 2020-04-07 02:21:32 -0700 | [diff] [blame^] | 24 | #include <MemoryLocal.h> |
Mark Salyzyn | 9f1cf25 | 2018-11-12 12:45:59 -0800 | [diff] [blame] | 25 | #include <android-base/file.h> |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 27 | #include <unwindstack/MapInfo.h> |
| 28 | #include <unwindstack/Memory.h> |
| 29 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 30 | #include "DexFile.h" |
Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 31 | #include "DexFileData.h" |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 32 | #include "MemoryFake.h" |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 33 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 34 | namespace unwindstack { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 35 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 36 | TEST(DexFileTest, from_file_open_non_exist) { |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 37 | EXPECT_TRUE(DexFileFromFile::Create(0, "/file/does/not/exist") == nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 40 | TEST(DexFileTest, from_file_open_too_small) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 41 | TemporaryFile tf; |
| 42 | ASSERT_TRUE(tf.fd != -1); |
| 43 | |
Martin Stjernholm | 2677b80 | 2019-01-09 23:26:33 +0000 | [diff] [blame] | 44 | ASSERT_EQ(size_t{10}, static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, 10)))); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 45 | |
| 46 | // Header too small. |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 47 | EXPECT_TRUE(DexFileFromFile::Create(0, tf.path) == nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 48 | |
| 49 | // Header correct, file too small. |
| 50 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)); |
Martin Stjernholm | 2677b80 | 2019-01-09 23:26:33 +0000 | [diff] [blame] | 51 | ASSERT_EQ(sizeof(kDexData) - 1, |
| 52 | static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData) - 1)))); |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 53 | EXPECT_TRUE(DexFileFromFile::Create(0, tf.path) == nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 56 | TEST(DexFileTest, from_file_open) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 57 | TemporaryFile tf; |
| 58 | ASSERT_TRUE(tf.fd != -1); |
| 59 | |
| 60 | ASSERT_EQ(sizeof(kDexData), |
| 61 | static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData))))); |
| 62 | |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 63 | EXPECT_TRUE(DexFileFromFile::Create(0, tf.path) != nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 66 | TEST(DexFileTest, from_file_open_non_zero_offset) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 67 | TemporaryFile tf; |
| 68 | ASSERT_TRUE(tf.fd != -1); |
| 69 | |
| 70 | ASSERT_EQ(0x100, lseek(tf.fd, 0x100, SEEK_SET)); |
| 71 | ASSERT_EQ(sizeof(kDexData), |
| 72 | static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData))))); |
| 73 | |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 74 | EXPECT_TRUE(DexFileFromFile::Create(0x100, tf.path) != nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Yong Li | 489c3a8 | 2020-03-19 18:43:06 +0000 | [diff] [blame] | 77 | static constexpr size_t kNumLeakLoops = 5000; |
| 78 | static constexpr size_t kMaxAllowedLeakBytes = 1024; |
| 79 | |
| 80 | static void CheckForLeak(size_t loop, size_t* first_allocated_bytes, size_t* last_allocated_bytes) { |
| 81 | size_t allocated_bytes = mallinfo().uordblks; |
| 82 | if (*first_allocated_bytes == 0) { |
| 83 | *first_allocated_bytes = allocated_bytes; |
| 84 | } else if (*last_allocated_bytes > *first_allocated_bytes) { |
| 85 | // Check that the total memory did not increase too much over the first loop. |
| 86 | ASSERT_LE(*last_allocated_bytes - *first_allocated_bytes, kMaxAllowedLeakBytes) |
| 87 | << "Failed in loop " << loop << " first_allocated_bytes " << *first_allocated_bytes |
| 88 | << " last_allocated_bytes " << *last_allocated_bytes; |
| 89 | } |
| 90 | *last_allocated_bytes = allocated_bytes; |
| 91 | } |
| 92 | |
| 93 | TEST(DexFileTest, from_file_no_leak) { |
| 94 | TemporaryFile tf; |
| 95 | ASSERT_TRUE(tf.fd != -1); |
| 96 | |
| 97 | ASSERT_EQ(sizeof(kDexData), |
| 98 | static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData))))); |
| 99 | |
| 100 | size_t first_allocated_bytes = 0; |
| 101 | size_t last_allocated_bytes = 0; |
| 102 | for (size_t i = 0; i < kNumLeakLoops; i++) { |
| 103 | EXPECT_TRUE(DexFileFromFile::Create(0, tf.path) != nullptr); |
| 104 | ASSERT_NO_FATAL_FAILURE(CheckForLeak(i, &first_allocated_bytes, &last_allocated_bytes)); |
| 105 | } |
| 106 | } |
| 107 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 108 | TEST(DexFileTest, from_memory_fail_too_small_for_header) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 109 | MemoryFake memory; |
| 110 | |
Martin Stjernholm | 2677b80 | 2019-01-09 23:26:33 +0000 | [diff] [blame] | 111 | memory.SetMemory(0x1000, kDexData, 10); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 112 | |
Sim Sun | 273d3f0 | 2020-04-07 02:21:32 -0700 | [diff] [blame^] | 113 | EXPECT_TRUE(DexFileFromMemory::Create(0x1000, &memory, "", sizeof(kDexData)) == nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 116 | TEST(DexFileTest, from_memory_fail_too_small_for_data) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 117 | MemoryFake memory; |
| 118 | |
| 119 | memory.SetMemory(0x1000, kDexData, sizeof(kDexData) - 2); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 120 | |
Sim Sun | 273d3f0 | 2020-04-07 02:21:32 -0700 | [diff] [blame^] | 121 | EXPECT_TRUE(DexFileFromMemory::Create(0x1000, &memory, "", sizeof(kDexData)) == nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 124 | TEST(DexFileTest, from_memory_open) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 125 | MemoryFake memory; |
| 126 | |
| 127 | memory.SetMemory(0x1000, kDexData, sizeof(kDexData)); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 128 | |
Sim Sun | 273d3f0 | 2020-04-07 02:21:32 -0700 | [diff] [blame^] | 129 | EXPECT_TRUE(DexFileFromMemory::Create(0x1000, &memory, "", sizeof(kDexData)) != nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Yong Li | 489c3a8 | 2020-03-19 18:43:06 +0000 | [diff] [blame] | 132 | TEST(DexFileTest, from_memory_no_leak) { |
| 133 | MemoryFake memory; |
| 134 | |
| 135 | memory.SetMemory(0x1000, kDexData, sizeof(kDexData)); |
| 136 | |
| 137 | size_t first_allocated_bytes = 0; |
| 138 | size_t last_allocated_bytes = 0; |
| 139 | for (size_t i = 0; i < kNumLeakLoops; i++) { |
Sim Sun | 273d3f0 | 2020-04-07 02:21:32 -0700 | [diff] [blame^] | 140 | EXPECT_TRUE(DexFileFromMemory::Create(0x1000, &memory, "", sizeof(kDexData)) != nullptr); |
Yong Li | 489c3a8 | 2020-03-19 18:43:06 +0000 | [diff] [blame] | 141 | ASSERT_NO_FATAL_FAILURE(CheckForLeak(i, &first_allocated_bytes, &last_allocated_bytes)); |
| 142 | } |
| 143 | } |
| 144 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 145 | TEST(DexFileTest, create_using_file) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 146 | TemporaryFile tf; |
| 147 | ASSERT_TRUE(tf.fd != -1); |
| 148 | |
| 149 | ASSERT_EQ(0x500, lseek(tf.fd, 0x500, SEEK_SET)); |
| 150 | ASSERT_EQ(sizeof(kDexData), |
| 151 | static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData))))); |
| 152 | |
| 153 | MemoryFake memory; |
Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 154 | MapInfo info(nullptr, nullptr, 0, 0x10000, 0, 0x5, tf.path); |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 155 | EXPECT_TRUE(DexFile::Create(0x500, &memory, &info) != nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 158 | TEST(DexFileTest, create_using_file_non_zero_start) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 159 | TemporaryFile tf; |
| 160 | ASSERT_TRUE(tf.fd != -1); |
| 161 | |
| 162 | ASSERT_EQ(0x500, lseek(tf.fd, 0x500, SEEK_SET)); |
| 163 | ASSERT_EQ(sizeof(kDexData), |
| 164 | static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData))))); |
| 165 | |
| 166 | MemoryFake memory; |
Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 167 | MapInfo info(nullptr, nullptr, 0x100, 0x10000, 0, 0x5, tf.path); |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 168 | EXPECT_TRUE(DexFile::Create(0x600, &memory, &info) != nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 171 | TEST(DexFileTest, create_using_file_non_zero_offset) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 172 | TemporaryFile tf; |
| 173 | ASSERT_TRUE(tf.fd != -1); |
| 174 | |
| 175 | ASSERT_EQ(0x500, lseek(tf.fd, 0x500, SEEK_SET)); |
| 176 | ASSERT_EQ(sizeof(kDexData), |
| 177 | static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData))))); |
| 178 | |
| 179 | MemoryFake memory; |
Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 180 | MapInfo info(nullptr, nullptr, 0x100, 0x10000, 0x200, 0x5, tf.path); |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 181 | EXPECT_TRUE(DexFile::Create(0x400, &memory, &info) != nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 184 | TEST(DexFileTest, create_using_memory_empty_file) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 185 | MemoryFake memory; |
| 186 | memory.SetMemory(0x4000, kDexData, sizeof(kDexData)); |
Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 187 | MapInfo info(nullptr, nullptr, 0x100, 0x10000, 0x200, 0x5, ""); |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 188 | EXPECT_TRUE(DexFile::Create(0x4000, &memory, &info) != nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 191 | TEST(DexFileTest, create_using_memory_file_does_not_exist) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 192 | MemoryFake memory; |
| 193 | memory.SetMemory(0x4000, kDexData, sizeof(kDexData)); |
Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 194 | MapInfo info(nullptr, nullptr, 0x100, 0x10000, 0x200, 0x5, "/does/not/exist"); |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 195 | EXPECT_TRUE(DexFile::Create(0x4000, &memory, &info) != nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 198 | TEST(DexFileTest, create_using_memory_file_is_malformed) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 199 | TemporaryFile tf; |
| 200 | ASSERT_TRUE(tf.fd != -1); |
| 201 | |
| 202 | ASSERT_EQ(sizeof(kDexData) - 10, |
| 203 | static_cast<size_t>(TEMP_FAILURE_RETRY(write(tf.fd, kDexData, sizeof(kDexData) - 10)))); |
| 204 | |
| 205 | MemoryFake memory; |
| 206 | memory.SetMemory(0x4000, kDexData, sizeof(kDexData)); |
Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 207 | MapInfo info(nullptr, nullptr, 0x4000, 0x10000, 0x200, 0x5, "/does/not/exist"); |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 208 | std::unique_ptr<DexFile> dex_file = DexFile::Create(0x4000, &memory, &info); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 209 | ASSERT_TRUE(dex_file != nullptr); |
| 210 | |
| 211 | // Check it came from memory by clearing memory and verifying it fails. |
| 212 | memory.Clear(); |
Martin Stjernholm | bb4f2b4 | 2018-12-19 14:28:33 +0000 | [diff] [blame] | 213 | dex_file = DexFile::Create(0x4000, &memory, &info); |
| 214 | EXPECT_TRUE(dex_file == nullptr); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Sim Sun | 273d3f0 | 2020-04-07 02:21:32 -0700 | [diff] [blame^] | 217 | TEST(DexFileTest, create_using_memory_size_too_small) { |
| 218 | MemoryFake memory; |
| 219 | memory.SetMemory(0x4000, kDexData, sizeof(kDexData)); |
| 220 | MapInfo info(nullptr, nullptr, 0x100, sizeof(kDexData) - 2, 0x200, 0x5, "/does/not/exist"); |
| 221 | EXPECT_TRUE(DexFile::Create(0x4000, &memory, &info) != nullptr); |
| 222 | } |
| 223 | |
| 224 | class MemoryLocalFake : public MemoryLocal { |
| 225 | public: |
| 226 | MemoryLocalFake(size_t memory_size) : backing_(memory_size) {} |
| 227 | virtual ~MemoryLocalFake() = default; |
| 228 | |
| 229 | void* Data() { return backing_.data(); } |
| 230 | |
| 231 | private: |
| 232 | std::vector<void*> backing_; |
| 233 | }; |
| 234 | |
| 235 | TEST(DexFileTest, create_using_local_memory) { |
| 236 | MemoryLocalFake memory(sizeof(kDexData)); |
| 237 | |
| 238 | memcpy(memory.Data(), kDexData, sizeof(kDexData)); |
| 239 | uint64_t start = reinterpret_cast<uint64_t>(memory.Data()); |
| 240 | MapInfo info(nullptr, nullptr, start, start + 0x1000, 0x200, 0x5, "/does/not/exist"); |
| 241 | EXPECT_TRUE(DexFile::Create(start, &memory, &info) != nullptr); |
| 242 | } |
| 243 | |
| 244 | TEST(DexFileTest, create_using_local_memory_size_too_small) { |
| 245 | MemoryLocalFake memory(sizeof(kDexData)); |
| 246 | |
| 247 | memcpy(memory.Data(), kDexData, sizeof(kDexData)); |
| 248 | uint64_t start = reinterpret_cast<uint64_t>(memory.Data()); |
| 249 | MapInfo info(nullptr, nullptr, start, start + sizeof(kDexData) - 2, 0x200, 0x5, |
| 250 | "/does/not/exist"); |
| 251 | EXPECT_TRUE(DexFile::Create(start, &memory, &info) == nullptr); |
| 252 | } |
| 253 | |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 254 | TEST(DexFileTest, get_method) { |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 255 | MemoryFake memory; |
| 256 | memory.SetMemory(0x4000, kDexData, sizeof(kDexData)); |
Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 257 | MapInfo info(nullptr, nullptr, 0x100, 0x10000, 0x200, 0x5, ""); |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 258 | std::unique_ptr<DexFile> dex_file(DexFile::Create(0x4000, &memory, &info)); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 259 | ASSERT_TRUE(dex_file != nullptr); |
| 260 | |
| 261 | std::string method; |
| 262 | uint64_t method_offset; |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 263 | ASSERT_TRUE(dex_file->GetMethodInformation(0x102, &method, &method_offset)); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 264 | EXPECT_EQ("Main.<init>", method); |
| 265 | EXPECT_EQ(2U, method_offset); |
| 266 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 267 | ASSERT_TRUE(dex_file->GetMethodInformation(0x118, &method, &method_offset)); |
David Srbecky | 02d0f79 | 2018-03-24 00:29:14 +0000 | [diff] [blame] | 268 | EXPECT_EQ("Main.main", method); |
| 269 | EXPECT_EQ(0U, method_offset); |
David Srbecky | 02d0f79 | 2018-03-24 00:29:14 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | TEST(DexFileTest, get_method_empty) { |
| 273 | MemoryFake memory; |
| 274 | memory.SetMemory(0x4000, kDexData, sizeof(kDexData)); |
Christopher Ferris | 0f40a05 | 2020-01-22 12:17:06 -0800 | [diff] [blame] | 275 | MapInfo info(nullptr, nullptr, 0x100, 0x10000, 0x200, 0x5, ""); |
David Srbecky | 02d0f79 | 2018-03-24 00:29:14 +0000 | [diff] [blame] | 276 | std::unique_ptr<DexFile> dex_file(DexFile::Create(0x4000, &memory, &info)); |
| 277 | ASSERT_TRUE(dex_file != nullptr); |
| 278 | |
| 279 | std::string method; |
| 280 | uint64_t method_offset; |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 281 | EXPECT_FALSE(dex_file->GetMethodInformation(0x100000, &method, &method_offset)); |
David Srbecky | 02d0f79 | 2018-03-24 00:29:14 +0000 | [diff] [blame] | 282 | |
David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 283 | EXPECT_FALSE(dex_file->GetMethodInformation(0x98, &method, &method_offset)); |
Christopher Ferris | 0b06a59 | 2018-01-19 10:26:36 -0800 | [diff] [blame] | 284 | } |
Christopher Ferris | d70ea5e | 2018-01-30 19:47:24 -0800 | [diff] [blame] | 285 | |
| 286 | } // namespace unwindstack |