1f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao/* 2f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * Copyright (C) 2017 The Android Open Source Project 3f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * 4f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * Licensed under the Apache License, Version 2.0 (the "License"); 5f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * you may not use this file except in compliance with the License. 6f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * You may obtain a copy of the License at 7f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * 8f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * http://www.apache.org/licenses/LICENSE-2.0 9f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * 10f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * Unless required by applicable law or agreed to in writing, software 11f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * distributed under the License is distributed on an "AS IS" BASIS, 12f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * See the License for the specific language governing permissions and 14f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao * limitations under the License. 15f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao */ 16f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao 17f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao#include <vector> 18f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao 19f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao#include <gtest/gtest.h> 20f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao 21f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao#include <android-base/file.h> 22f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao#include <android-base/test_utils.h> 23f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao#include <unwindstack/Memory.h> 24f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao 25f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gaonamespace unwindstack { 26f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao 27f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gaoclass MemoryOfflineTest : public ::testing::Test { 28f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao protected: 29f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao void SetUp() override { 30f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao for (size_t i = 0; i < 1024; ++i) { 31f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao data.push_back(i & 0xff); 32f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao } 33f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao 34f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao ASSERT_TRUE(android::base::WriteFully(temp_file.fd, &offset, sizeof(offset))); 35f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao ASSERT_TRUE(android::base::WriteFully(temp_file.fd, data.data(), data.size())); 36f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao 37f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao memory = std::make_unique<MemoryOffline>(); 38f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao ASSERT_TRUE(memory != nullptr); 39f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao 40f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao ASSERT_TRUE(memory->Init(temp_file.path, 0)); 41f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao } 42f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao 43f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao TemporaryFile temp_file; 44f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao uint64_t offset = 4096; 45f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao std::vector<char> data; 46f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao std::unique_ptr<MemoryOffline> memory; 47f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao}; 48f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao 49f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh GaoTEST_F(MemoryOfflineTest, read_boundaries) { 50f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao char buf = '\0'; 51f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao ASSERT_EQ(0U, memory->Read(offset - 1, &buf, 1)); 52f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao ASSERT_EQ(0U, memory->Read(offset + data.size(), &buf, 1)); 53f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao ASSERT_EQ(1U, memory->Read(offset, &buf, 1)); 54f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao ASSERT_EQ(buf, data.front()); 55f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao ASSERT_EQ(1U, memory->Read(offset + data.size() - 1, &buf, 1)); 56f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao ASSERT_EQ(buf, data.back()); 57f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao} 58f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao 59f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh GaoTEST_F(MemoryOfflineTest, read_values) { 60f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao std::vector<char> buf; 61f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao buf.resize(2 * data.size()); 62f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao ASSERT_EQ(data.size(), memory->Read(offset, buf.data(), buf.size())); 63f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao buf.resize(data.size()); 64f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao ASSERT_EQ(buf, data); 65f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao} 66f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao 67f4a94c44b6979719ce7a734d8e03ba0f853bf8cbJosh Gao} // namespace unwindstack 68