1 /* 2 * Copyright (C) 2016 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 MemoryBufferTest : public ::testing::Test { 28 protected: 29 void SetUp() override { 30 ResetLogs(); 31 memory_.reset(new MemoryBuffer); 32 } 33 std::unique_ptr<MemoryBuffer> memory_; 34 }; 35 36 TEST_F(MemoryBufferTest, empty) { 37 ASSERT_EQ(0U, memory_->Size()); 38 std::vector<uint8_t> buffer(1024); 39 ASSERT_FALSE(memory_->ReadFully(0, buffer.data(), 1)); 40 ASSERT_EQ(nullptr, memory_->GetPtr(0)); 41 ASSERT_EQ(nullptr, memory_->GetPtr(1)); 42 } 43 44 TEST_F(MemoryBufferTest, write_read) { 45 memory_->Resize(256); 46 ASSERT_EQ(256U, memory_->Size()); 47 ASSERT_TRUE(memory_->GetPtr(0) != nullptr); 48 ASSERT_TRUE(memory_->GetPtr(1) != nullptr); 49 ASSERT_TRUE(memory_->GetPtr(255) != nullptr); 50 ASSERT_TRUE(memory_->GetPtr(256) == nullptr); 51 52 uint8_t* data = memory_->GetPtr(0); 53 for (size_t i = 0; i < memory_->Size(); i++) { 54 data[i] = i; 55 } 56 57 std::vector<uint8_t> buffer(memory_->Size()); 58 ASSERT_TRUE(memory_->ReadFully(0, buffer.data(), buffer.size())); 59 for (size_t i = 0; i < buffer.size(); i++) { 60 ASSERT_EQ(i, buffer[i]) << "Failed at byte " << i; 61 } 62 } 63 64 TEST_F(MemoryBufferTest, read_failures) { 65 memory_->Resize(100); 66 std::vector<uint8_t> buffer(200); 67 ASSERT_FALSE(memory_->ReadFully(0, buffer.data(), 101)); 68 ASSERT_FALSE(memory_->ReadFully(100, buffer.data(), 1)); 69 ASSERT_FALSE(memory_->ReadFully(101, buffer.data(), 2)); 70 ASSERT_FALSE(memory_->ReadFully(99, buffer.data(), 2)); 71 ASSERT_TRUE(memory_->ReadFully(99, buffer.data(), 1)); 72 } 73 74 TEST_F(MemoryBufferTest, read_failure_overflow) { 75 memory_->Resize(100); 76 std::vector<uint8_t> buffer(200); 77 78 ASSERT_FALSE(memory_->ReadFully(UINT64_MAX - 100, buffer.data(), 200)); 79 } 80 81 TEST_F(MemoryBufferTest, Read) { 82 memory_->Resize(256); 83 ASSERT_EQ(256U, memory_->Size()); 84 ASSERT_TRUE(memory_->GetPtr(0) != nullptr); 85 ASSERT_TRUE(memory_->GetPtr(1) != nullptr); 86 ASSERT_TRUE(memory_->GetPtr(255) != nullptr); 87 ASSERT_TRUE(memory_->GetPtr(256) == nullptr); 88 89 uint8_t* data = memory_->GetPtr(0); 90 for (size_t i = 0; i < memory_->Size(); i++) { 91 data[i] = i; 92 } 93 94 std::vector<uint8_t> buffer(memory_->Size()); 95 ASSERT_EQ(128U, memory_->Read(128, buffer.data(), buffer.size())); 96 for (size_t i = 0; i < 128; i++) { 97 ASSERT_EQ(128 + i, buffer[i]) << "Failed at byte " << i; 98 } 99 } 100 101 } // namespace unwindstack 102