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