2017-04-03 19:39:47 +00:00
|
|
|
/*
|
|
|
|
* 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 <vector>
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2017-07-14 17:37:19 +00:00
|
|
|
#include <unwindstack/Memory.h>
|
2017-04-03 19:39:47 +00:00
|
|
|
|
|
|
|
#include "LogFake.h"
|
|
|
|
|
2017-07-14 17:37:19 +00:00
|
|
|
namespace unwindstack {
|
|
|
|
|
2017-04-03 19:39:47 +00:00
|
|
|
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);
|
2017-10-18 18:44:51 +00:00
|
|
|
ASSERT_FALSE(memory_->ReadFully(0, buffer.data(), 1));
|
2017-04-03 19:39:47 +00:00
|
|
|
ASSERT_EQ(nullptr, memory_->GetPtr(0));
|
|
|
|
ASSERT_EQ(nullptr, memory_->GetPtr(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MemoryBufferTest, write_read) {
|
|
|
|
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());
|
2017-10-18 18:44:51 +00:00
|
|
|
ASSERT_TRUE(memory_->ReadFully(0, buffer.data(), buffer.size()));
|
2017-04-03 19:39:47 +00:00
|
|
|
for (size_t i = 0; i < buffer.size(); i++) {
|
|
|
|
ASSERT_EQ(i, buffer[i]) << "Failed at byte " << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MemoryBufferTest, read_failures) {
|
|
|
|
memory_->Resize(100);
|
|
|
|
std::vector<uint8_t> buffer(200);
|
2017-10-18 18:44:51 +00:00
|
|
|
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));
|
2017-04-03 19:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MemoryBufferTest, read_failure_overflow) {
|
|
|
|
memory_->Resize(100);
|
|
|
|
std::vector<uint8_t> buffer(200);
|
|
|
|
|
2017-10-18 18:44:51 +00:00
|
|
|
ASSERT_FALSE(memory_->ReadFully(UINT64_MAX - 100, buffer.data(), 200));
|
2017-04-03 19:39:47 +00:00
|
|
|
}
|
2017-07-14 17:37:19 +00:00
|
|
|
|
2017-10-18 18:52:53 +00:00
|
|
|
TEST_F(MemoryBufferTest, Read) {
|
2017-09-26 21:11:37 +00:00
|
|
|
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());
|
2017-10-18 18:52:53 +00:00
|
|
|
ASSERT_EQ(128U, memory_->Read(128, buffer.data(), buffer.size()));
|
2017-09-26 21:11:37 +00:00
|
|
|
for (size_t i = 0; i < 128; i++) {
|
|
|
|
ASSERT_EQ(128 + i, buffer[i]) << "Failed at byte " << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 17:37:19 +00:00
|
|
|
} // namespace unwindstack
|