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
17 #include <vector>
18
19 #include <gtest/gtest.h>
20
21 #include "LogFake.h"
22 #include "MemoryOfflineBuffer.h"
23
24 namespace unwindstack {
25
26 class MemoryOfflineBufferTest : public ::testing::Test {
27 protected:
SetUp()28 void SetUp() override {
29 ResetLogs();
30 memory_.reset(new MemoryOfflineBuffer(buffer_.data(), kStart, kEnd));
31 }
32
SetUpTestSuite()33 static void SetUpTestSuite() {
34 buffer_.resize(kLength);
35 for (size_t i = 0; i < kLength; i++) {
36 buffer_[i] = i % 189;
37 }
38 }
39
40 std::unique_ptr<MemoryOfflineBuffer> memory_;
41
42 static constexpr size_t kLength = 0x2000;
43 static constexpr uint64_t kStart = 0x1000;
44 static constexpr uint64_t kEnd = kStart + kLength;
45 static std::vector<uint8_t> buffer_;
46 };
47
48 std::vector<uint8_t> MemoryOfflineBufferTest::buffer_;
49
VerifyBuffer(uint8_t * buffer,size_t start_value,size_t length)50 static void VerifyBuffer(uint8_t* buffer, size_t start_value, size_t length) {
51 for (size_t i = 0; i < length; i++) {
52 ASSERT_EQ((start_value + i) % 189, buffer[i]) << "Failed at byte " << i;
53 }
54 }
55
TEST_F(MemoryOfflineBufferTest,read_out_of_bounds)56 TEST_F(MemoryOfflineBufferTest, read_out_of_bounds) {
57 std::vector<uint8_t> buffer(1024);
58 ASSERT_FALSE(memory_->ReadFully(0, buffer.data(), 1));
59 ASSERT_FALSE(memory_->ReadFully(0xfff, buffer.data(), 1));
60 ASSERT_FALSE(memory_->ReadFully(0xfff, buffer.data(), 2));
61 ASSERT_FALSE(memory_->ReadFully(0x3000, buffer.data(), 1));
62 ASSERT_FALSE(memory_->ReadFully(0x3001, buffer.data(), 1));
63 }
64
TEST_F(MemoryOfflineBufferTest,read)65 TEST_F(MemoryOfflineBufferTest, read) {
66 std::vector<uint8_t> buffer(1024);
67 ASSERT_TRUE(memory_->ReadFully(kStart, buffer.data(), 10));
68 ASSERT_NO_FATAL_FAILURE(VerifyBuffer(buffer.data(), 0, 10));
69
70 ASSERT_TRUE(memory_->ReadFully(kStart + 555, buffer.data(), 40));
71 ASSERT_NO_FATAL_FAILURE(VerifyBuffer(buffer.data(), 555, 40));
72
73 ASSERT_TRUE(memory_->ReadFully(kStart + kLength - 105, buffer.data(), 105));
74 ASSERT_NO_FATAL_FAILURE(VerifyBuffer(buffer.data(), kLength - 105, 105));
75 }
76
TEST_F(MemoryOfflineBufferTest,read_past_end)77 TEST_F(MemoryOfflineBufferTest, read_past_end) {
78 std::vector<uint8_t> buffer(1024);
79 ASSERT_EQ(100U, memory_->Read(kStart + kLength - 100, buffer.data(), buffer.size()));
80 VerifyBuffer(buffer.data(), kLength - 100, 100);
81 }
82
TEST_F(MemoryOfflineBufferTest,read_after_reset)83 TEST_F(MemoryOfflineBufferTest, read_after_reset) {
84 std::vector<uint8_t> buffer(1024);
85 ASSERT_TRUE(memory_->ReadFully(kStart, buffer.data(), 100));
86 ASSERT_NO_FATAL_FAILURE(VerifyBuffer(buffer.data(), 0, 100));
87
88 memory_->Reset(&buffer_[10], 0x12000, 0x13000);
89 ASSERT_TRUE(memory_->ReadFully(0x12000, buffer.data(), 100));
90 ASSERT_NO_FATAL_FAILURE(VerifyBuffer(buffer.data(), 10, 100));
91
92 ASSERT_EQ(50U, memory_->Read(0x13000 - 50, buffer.data(), buffer.size()));
93 ASSERT_NO_FATAL_FAILURE(VerifyBuffer(buffer.data(), 0x1000 - 50 + 10, 50));
94 }
95
96 } // namespace unwindstack
97