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
TEST(MemoryTest,read32)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
TEST(MemoryTest,read64)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
TEST(MemoryTest,read_string)54 TEST(MemoryTest, read_string) {
55 std::string name("string_in_memory");
56
57 MemoryFake memory;
58
59 memory.SetMemory(100, name.c_str(), name.size() + 1);
60
61 std::string dst_name;
62 ASSERT_TRUE(memory.ReadString(100, &dst_name));
63 ASSERT_EQ("string_in_memory", dst_name);
64
65 ASSERT_TRUE(memory.ReadString(107, &dst_name));
66 ASSERT_EQ("in_memory", dst_name);
67
68 // Set size greater than string.
69 ASSERT_TRUE(memory.ReadString(107, &dst_name, 10));
70 ASSERT_EQ("in_memory", dst_name);
71
72 ASSERT_FALSE(memory.ReadString(107, &dst_name, 9));
73 }
74
TEST(MemoryTest,read_string_error)75 TEST(MemoryTest, read_string_error) {
76 std::string name("short");
77
78 MemoryFake memory;
79
80 // Save everything except the terminating '\0'.
81 memory.SetMemory(0, name.c_str(), name.size());
82
83 std::string dst_name;
84 // Read from a non-existant address.
85 ASSERT_FALSE(memory.ReadString(100, &dst_name));
86
87 // This should fail because there is no terminating '\0'.
88 ASSERT_FALSE(memory.ReadString(0, &dst_name));
89
90 // This should pass because there is a terminating '\0'.
91 memory.SetData8(name.size(), '\0');
92 ASSERT_TRUE(memory.ReadString(0, &dst_name));
93 ASSERT_EQ("short", dst_name);
94 }
95
96 } // namespace unwindstack
97