1 /*
2 * Copyright (C) 2017 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
17 #include <stdint.h>
18
19 #include <memory>
20 #include <vector>
21
22 #include <gtest/gtest.h>
23
24 #include "MemoryFake.h"
25 #include "MemoryRange.h"
26
27 namespace unwindstack {
28
29 class MemoryRangeTest : public ::testing::Test {
30 protected:
SetUp()31 void SetUp() override {
32 process_memory_.reset();
33 memory_fake_ = new MemoryFake;
34 process_memory_.reset(memory_fake_);
35 }
36
37 std::shared_ptr<Memory> process_memory_;
38 MemoryFake* memory_fake_ = nullptr;
39 };
40
TEST_F(MemoryRangeTest,read_fully)41 TEST_F(MemoryRangeTest, read_fully) {
42 memory_fake_->SetMemoryBlock(9000, 2048, 0x4c);
43
44 MemoryRange range(process_memory_, 9001, 1024, 0);
45
46 std::vector<uint8_t> dst(1024);
47 ASSERT_TRUE(range.ReadFully(0, dst.data(), dst.size()));
48 for (size_t i = 0; i < dst.size(); i++) {
49 ASSERT_EQ(0x4cU, dst[i]) << "Failed at byte " << i;
50 }
51 }
52
TEST_F(MemoryRangeTest,read_fully_near_limit)53 TEST_F(MemoryRangeTest, read_fully_near_limit) {
54 memory_fake_->SetMemoryBlock(0, 8192, 0x4c);
55
56 MemoryRange range(process_memory_, 1000, 1024, 0);
57
58 std::vector<uint8_t> dst(1024);
59 ASSERT_TRUE(range.ReadFully(1020, dst.data(), 4));
60 for (size_t i = 0; i < 4; i++) {
61 ASSERT_EQ(0x4cU, dst[i]) << "Failed at byte " << i;
62 }
63
64 // Verify that reads outside of the range will fail.
65 ASSERT_FALSE(range.ReadFully(1020, dst.data(), 5));
66 ASSERT_FALSE(range.ReadFully(1024, dst.data(), 1));
67 ASSERT_FALSE(range.ReadFully(1024, dst.data(), 1024));
68
69 // Verify that reading up to the end works.
70 ASSERT_TRUE(range.ReadFully(1020, dst.data(), 4));
71 }
72
TEST_F(MemoryRangeTest,read_fully_overflow)73 TEST_F(MemoryRangeTest, read_fully_overflow) {
74 std::vector<uint8_t> buffer(100);
75
76 std::shared_ptr<Memory> process_memory(new MemoryFakeAlwaysReadZero);
77 std::unique_ptr<MemoryRange> overflow(new MemoryRange(process_memory, 100, 200, 0));
78 ASSERT_FALSE(overflow->ReadFully(UINT64_MAX - 10, buffer.data(), 100));
79 }
80
TEST_F(MemoryRangeTest,read)81 TEST_F(MemoryRangeTest, read) {
82 memory_fake_->SetMemoryBlock(0, 4096, 0x4c);
83
84 MemoryRange range(process_memory_, 1000, 1024, 0);
85
86 std::vector<uint8_t> dst(1024);
87 ASSERT_EQ(4U, range.Read(1020, dst.data(), dst.size()));
88 for (size_t i = 0; i < 4; i++) {
89 ASSERT_EQ(0x4cU, dst[i]) << "Failed at byte " << i;
90 }
91 }
92
TEST_F(MemoryRangeTest,read_non_zero_offset)93 TEST_F(MemoryRangeTest, read_non_zero_offset) {
94 memory_fake_->SetMemoryBlock(1000, 1024, 0x12);
95
96 MemoryRange range(process_memory_, 1000, 1024, 400);
97
98 std::vector<uint8_t> dst(1024);
99 ASSERT_EQ(1024U, range.Read(400, dst.data(), dst.size()));
100 for (size_t i = 0; i < dst.size(); i++) {
101 ASSERT_EQ(0x12U, dst[i]) << "Failed at byte " << i;
102 }
103 }
104
105 } // namespace unwindstack
106