summaryrefslogtreecommitdiff
path: root/libunwindstack/tests/MemoryBufferTest.cpp
blob: 91b20dd409bd4c85c233a86c00d5429f4495eb03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdint.h>

#include <vector>

#include <gtest/gtest.h>

#include "LogFake.h"
#include "MemoryBuffer.h"

namespace unwindstack {

class MemoryBufferTest : public ::testing::Test {
 protected:
  void SetUp() override {
    ResetLogs();
    memory_.reset(new MemoryBuffer);
  }
  std::unique_ptr<MemoryBuffer> memory_;
};

TEST_F(MemoryBufferTest, empty) {
  ASSERT_EQ(0U, memory_->Size());
  std::vector<uint8_t> buffer(1024);
  ASSERT_FALSE(memory_->ReadFully(0, buffer.data(), 1));
  ASSERT_EQ(nullptr, memory_->GetPtr(0));
  ASSERT_EQ(nullptr, memory_->GetPtr(1));
}

TEST_F(MemoryBufferTest, write_read) {
  ASSERT_TRUE(memory_->Resize(256));
  ASSERT_EQ(256U, memory_->Size());
  ASSERT_TRUE(memory_->GetPtr(0) != nullptr);
  ASSERT_TRUE(memory_->GetPtr(1) != nullptr);
  ASSERT_TRUE(memory_->GetPtr(255) != nullptr);
  ASSERT_TRUE(memory_->GetPtr(256) == nullptr);

  uint8_t* data = memory_->GetPtr(0);
  for (size_t i = 0; i < memory_->Size(); i++) {
    data[i] = i;
  }

  std::vector<uint8_t> buffer(memory_->Size());
  ASSERT_TRUE(memory_->ReadFully(0, buffer.data(), buffer.size()));
  for (size_t i = 0; i < buffer.size(); i++) {
    ASSERT_EQ(i, buffer[i]) << "Failed at byte " << i;
  }
}

TEST_F(MemoryBufferTest, read_failures) {
  ASSERT_TRUE(memory_->Resize(100));
  std::vector<uint8_t> buffer(200);
  ASSERT_FALSE(memory_->ReadFully(0, buffer.data(), 101));
  ASSERT_FALSE(memory_->ReadFully(100, buffer.data(), 1));
  ASSERT_FALSE(memory_->ReadFully(101, buffer.data(), 2));
  ASSERT_FALSE(memory_->ReadFully(99, buffer.data(), 2));
  ASSERT_TRUE(memory_->ReadFully(99, buffer.data(), 1));
}

TEST_F(MemoryBufferTest, read_failure_overflow) {
  ASSERT_TRUE(memory_->Resize(100));
  std::vector<uint8_t> buffer(200);

  ASSERT_FALSE(memory_->ReadFully(UINT64_MAX - 100, buffer.data(), 200));
}

TEST_F(MemoryBufferTest, Read) {
  ASSERT_TRUE(memory_->Resize(256));
  ASSERT_EQ(256U, memory_->Size());
  ASSERT_TRUE(memory_->GetPtr(0) != nullptr);
  ASSERT_TRUE(memory_->GetPtr(1) != nullptr);
  ASSERT_TRUE(memory_->GetPtr(255) != nullptr);
  ASSERT_TRUE(memory_->GetPtr(256) == nullptr);

  uint8_t* data = memory_->GetPtr(0);
  for (size_t i = 0; i < memory_->Size(); i++) {
    data[i] = i;
  }

  std::vector<uint8_t> buffer(memory_->Size());
  ASSERT_EQ(128U, memory_->Read(128, buffer.data(), buffer.size()));
  for (size_t i = 0; i < 128; i++) {
    ASSERT_EQ(128 + i, buffer[i]) << "Failed at byte " << i;
  }
}

TEST_F(MemoryBufferTest, Resize) {
  ASSERT_TRUE(memory_->Resize(256));

  ASSERT_TRUE(memory_->Resize(1024));
}

extern "C" void __hwasan_init() __attribute__((weak));

TEST_F(MemoryBufferTest, Resize_too_large) {
  if (&__hwasan_init != 0) {
    GTEST_SKIP() << "Tests fails hwasan allocation size too large check.";
  }
  ASSERT_FALSE(memory_->Resize(SIZE_MAX));
}

}  // namespace unwindstack