1 /* 2 * Copyright (C) 2017 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 <android-base/file.h> 22 #include <android-base/test_utils.h> 23 #include <unwindstack/Memory.h> 24 25 namespace unwindstack { 26 27 class MemoryOfflineTest : public ::testing::Test { 28 protected: 29 void SetUp() override { 30 for (size_t i = 0; i < 1024; ++i) { 31 data.push_back(i & 0xff); 32 } 33 34 ASSERT_TRUE(android::base::WriteFully(temp_file.fd, &offset, sizeof(offset))); 35 ASSERT_TRUE(android::base::WriteFully(temp_file.fd, data.data(), data.size())); 36 37 memory = std::make_unique<MemoryOffline>(); 38 ASSERT_TRUE(memory != nullptr); 39 40 ASSERT_TRUE(memory->Init(temp_file.path, 0)); 41 } 42 43 TemporaryFile temp_file; 44 uint64_t offset = 4096; 45 std::vector<char> data; 46 std::unique_ptr<MemoryOffline> memory; 47 }; 48 49 TEST_F(MemoryOfflineTest, read_boundaries) { 50 char buf = '\0'; 51 ASSERT_EQ(0U, memory->Read(offset - 1, &buf, 1)); 52 ASSERT_EQ(0U, memory->Read(offset + data.size(), &buf, 1)); 53 ASSERT_EQ(1U, memory->Read(offset, &buf, 1)); 54 ASSERT_EQ(buf, data.front()); 55 ASSERT_EQ(1U, memory->Read(offset + data.size() - 1, &buf, 1)); 56 ASSERT_EQ(buf, data.back()); 57 } 58 59 TEST_F(MemoryOfflineTest, read_values) { 60 std::vector<char> buf; 61 buf.resize(2 * data.size()); 62 ASSERT_EQ(data.size(), memory->Read(offset, buf.data(), buf.size())); 63 buf.resize(data.size()); 64 ASSERT_EQ(buf, data); 65 } 66 67 } // namespace unwindstack 68