• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdint.h>
18 
19 #include <vector>
20 
21 #include <gtest/gtest.h>
22 
23 #include "LogFake.h"
24 #include "MemoryBuffer.h"
25 
26 namespace unwindstack {
27 
28 class MemoryBufferTest : public ::testing::Test {
29  protected:
SetUp()30   void SetUp() override {
31     ResetLogs();
32     memory_.reset(new MemoryBuffer);
33   }
34   std::unique_ptr<MemoryBuffer> memory_;
35 };
36 
TEST_F(MemoryBufferTest,empty)37 TEST_F(MemoryBufferTest, empty) {
38   ASSERT_EQ(0U, memory_->Size());
39   std::vector<uint8_t> buffer(1024);
40   ASSERT_FALSE(memory_->ReadFully(0, buffer.data(), 1));
41   ASSERT_EQ(nullptr, memory_->GetPtr(0));
42   ASSERT_EQ(nullptr, memory_->GetPtr(1));
43 }
44 
TEST_F(MemoryBufferTest,write_read)45 TEST_F(MemoryBufferTest, write_read) {
46   ASSERT_TRUE(memory_->Resize(256));
47   ASSERT_EQ(256U, memory_->Size());
48   ASSERT_TRUE(memory_->GetPtr(0) != nullptr);
49   ASSERT_TRUE(memory_->GetPtr(1) != nullptr);
50   ASSERT_TRUE(memory_->GetPtr(255) != nullptr);
51   ASSERT_TRUE(memory_->GetPtr(256) == nullptr);
52 
53   uint8_t* data = memory_->GetPtr(0);
54   for (size_t i = 0; i < memory_->Size(); i++) {
55     data[i] = i;
56   }
57 
58   std::vector<uint8_t> buffer(memory_->Size());
59   ASSERT_TRUE(memory_->ReadFully(0, buffer.data(), buffer.size()));
60   for (size_t i = 0; i < buffer.size(); i++) {
61     ASSERT_EQ(i, buffer[i]) << "Failed at byte " << i;
62   }
63 }
64 
TEST_F(MemoryBufferTest,read_failures)65 TEST_F(MemoryBufferTest, read_failures) {
66   ASSERT_TRUE(memory_->Resize(100));
67   std::vector<uint8_t> buffer(200);
68   ASSERT_FALSE(memory_->ReadFully(0, buffer.data(), 101));
69   ASSERT_FALSE(memory_->ReadFully(100, buffer.data(), 1));
70   ASSERT_FALSE(memory_->ReadFully(101, buffer.data(), 2));
71   ASSERT_FALSE(memory_->ReadFully(99, buffer.data(), 2));
72   ASSERT_TRUE(memory_->ReadFully(99, buffer.data(), 1));
73 }
74 
TEST_F(MemoryBufferTest,read_failure_overflow)75 TEST_F(MemoryBufferTest, read_failure_overflow) {
76   ASSERT_TRUE(memory_->Resize(100));
77   std::vector<uint8_t> buffer(200);
78 
79   ASSERT_FALSE(memory_->ReadFully(UINT64_MAX - 100, buffer.data(), 200));
80 }
81 
TEST_F(MemoryBufferTest,Read)82 TEST_F(MemoryBufferTest, Read) {
83   ASSERT_TRUE(memory_->Resize(256));
84   ASSERT_EQ(256U, memory_->Size());
85   ASSERT_TRUE(memory_->GetPtr(0) != nullptr);
86   ASSERT_TRUE(memory_->GetPtr(1) != nullptr);
87   ASSERT_TRUE(memory_->GetPtr(255) != nullptr);
88   ASSERT_TRUE(memory_->GetPtr(256) == nullptr);
89 
90   uint8_t* data = memory_->GetPtr(0);
91   for (size_t i = 0; i < memory_->Size(); i++) {
92     data[i] = i;
93   }
94 
95   std::vector<uint8_t> buffer(memory_->Size());
96   ASSERT_EQ(128U, memory_->Read(128, buffer.data(), buffer.size()));
97   for (size_t i = 0; i < 128; i++) {
98     ASSERT_EQ(128 + i, buffer[i]) << "Failed at byte " << i;
99   }
100 }
101 
TEST_F(MemoryBufferTest,Resize)102 TEST_F(MemoryBufferTest, Resize) {
103   ASSERT_TRUE(memory_->Resize(256));
104 
105   ASSERT_TRUE(memory_->Resize(1024));
106 }
107 
108 extern "C" void __hwasan_init() __attribute__((weak));
109 
TEST_F(MemoryBufferTest,Resize_too_large)110 TEST_F(MemoryBufferTest, Resize_too_large) {
111   if (&__hwasan_init != 0) {
112     GTEST_SKIP() << "Tests fails hwasan allocation size too large check.";
113   }
114   ASSERT_FALSE(memory_->Resize(SIZE_MAX));
115 }
116 
117 }  // namespace unwindstack
118