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 <string> 21 #include <vector> 22 23 #include <gtest/gtest.h> 24 25 #include <unwindstack/Memory.h> 26 27 #include "MemoryFake.h" 28 29 namespace unwindstack { 30 31 TEST(MemoryTest, read32) { 32 MemoryFakeAlwaysReadZero memory; 33 34 uint32_t data = 0xffffffff; 35 ASSERT_TRUE(memory.Read32(0, &data)); 36 ASSERT_EQ(0U, data); 37 } 38 39 TEST(MemoryTest, read64) { 40 MemoryFakeAlwaysReadZero memory; 41 42 uint64_t data = 0xffffffffffffffffULL; 43 ASSERT_TRUE(memory.Read64(0, &data)); 44 ASSERT_EQ(0U, data); 45 } 46 47 struct FakeStruct { 48 int one; 49 bool two; 50 uint32_t three; 51 uint64_t four; 52 }; 53 54 TEST(MemoryTest, read_field) { 55 MemoryFakeAlwaysReadZero memory; 56 57 FakeStruct data; 58 memset(&data, 0xff, sizeof(data)); 59 ASSERT_TRUE(memory.ReadField(0, &data, &data.one, sizeof(data.one))); 60 ASSERT_EQ(0, data.one); 61 62 memset(&data, 0xff, sizeof(data)); 63 ASSERT_TRUE(memory.ReadField(0, &data, &data.two, sizeof(data.two))); 64 ASSERT_FALSE(data.two); 65 66 memset(&data, 0xff, sizeof(data)); 67 ASSERT_TRUE(memory.ReadField(0, &data, &data.three, sizeof(data.three))); 68 ASSERT_EQ(0U, data.three); 69 70 memset(&data, 0xff, sizeof(data)); 71 ASSERT_TRUE(memory.ReadField(0, &data, &data.four, sizeof(data.four))); 72 ASSERT_EQ(0U, data.four); 73 } 74 75 TEST(MemoryTest, read_field_fails) { 76 MemoryFakeAlwaysReadZero memory; 77 78 FakeStruct data; 79 memset(&data, 0xff, sizeof(data)); 80 81 ASSERT_FALSE(memory.ReadField(UINT64_MAX, &data, &data.three, sizeof(data.three))); 82 83 // Field and start reversed, should fail. 84 ASSERT_FALSE(memory.ReadField(100, &data.two, &data, sizeof(data.two))); 85 ASSERT_FALSE(memory.ReadField(0, &data.two, &data, sizeof(data.two))); 86 } 87 88 TEST(MemoryTest, read_string) { 89 std::string name("string_in_memory"); 90 91 MemoryFake memory; 92 93 memory.SetMemory(100, name.c_str(), name.size() + 1); 94 95 std::string dst_name; 96 ASSERT_TRUE(memory.ReadString(100, &dst_name)); 97 ASSERT_EQ("string_in_memory", dst_name); 98 99 ASSERT_TRUE(memory.ReadString(107, &dst_name)); 100 ASSERT_EQ("in_memory", dst_name); 101 102 // Set size greater than string. 103 ASSERT_TRUE(memory.ReadString(107, &dst_name, 10)); 104 ASSERT_EQ("in_memory", dst_name); 105 106 ASSERT_FALSE(memory.ReadString(107, &dst_name, 9)); 107 } 108 109 TEST(MemoryTest, read_string_error) { 110 std::string name("short"); 111 112 MemoryFake memory; 113 114 // Save everything except the terminating '\0'. 115 memory.SetMemory(0, name.c_str(), name.size()); 116 117 std::string dst_name; 118 // Read from a non-existant address. 119 ASSERT_FALSE(memory.ReadString(100, &dst_name)); 120 121 // This should fail because there is no terminating '\0'. 122 ASSERT_FALSE(memory.ReadString(0, &dst_name)); 123 124 // This should pass because there is a terminating '\0'. 125 memory.SetData8(name.size(), '\0'); 126 ASSERT_TRUE(memory.ReadString(0, &dst_name)); 127 ASSERT_EQ("short", dst_name); 128 } 129 130 } // namespace unwindstack 131