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 <stdint.h> 18 #include <string.h> 19 20 #include <vector> 21 22 #include <gtest/gtest.h> 23 24 #include <unwindstack/Memory.h> 25 26 namespace unwindstack { 27 28 TEST(MemoryLocalTest, read) { 29 std::vector<uint8_t> src(1024); 30 memset(src.data(), 0x4c, 1024); 31 32 MemoryLocal local; 33 34 std::vector<uint8_t> dst(1024); 35 ASSERT_TRUE(local.Read(reinterpret_cast<uint64_t>(src.data()), dst.data(), 1024)); 36 ASSERT_EQ(0, memcmp(src.data(), dst.data(), 1024)); 37 for (size_t i = 0; i < 1024; i++) { 38 ASSERT_EQ(0x4cU, dst[i]); 39 } 40 41 memset(src.data(), 0x23, 512); 42 ASSERT_TRUE(local.Read(reinterpret_cast<uint64_t>(src.data()), dst.data(), 1024)); 43 ASSERT_EQ(0, memcmp(src.data(), dst.data(), 1024)); 44 for (size_t i = 0; i < 512; i++) { 45 ASSERT_EQ(0x23U, dst[i]); 46 } 47 for (size_t i = 512; i < 1024; i++) { 48 ASSERT_EQ(0x4cU, dst[i]); 49 } 50 } 51 52 TEST(MemoryLocalTest, read_illegal) { 53 MemoryLocal local; 54 55 std::vector<uint8_t> dst(100); 56 ASSERT_FALSE(local.Read(0, dst.data(), 1)); 57 ASSERT_FALSE(local.Read(0, dst.data(), 100)); 58 } 59 60 TEST(MemoryLocalTest, read_overflow) { 61 MemoryLocal local; 62 63 // On 32 bit this test doesn't necessarily cause an overflow. The 64 bit 64 // version will always go through the overflow check. 65 std::vector<uint8_t> dst(100); 66 uint64_t value; 67 ASSERT_FALSE(local.Read(reinterpret_cast<uint64_t>(&value), dst.data(), SIZE_MAX)); 68 } 69 70 } // namespace unwindstack 71