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 "utils/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, 100));
63 ASSERT_EQ("string_in_memory", dst_name);
64
65 ASSERT_TRUE(memory.ReadString(107, &dst_name, 100));
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, 100));
86
87 // This should fail because there is no terminating '\0'.
88 ASSERT_FALSE(memory.ReadString(0, &dst_name, 100));
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, 100));
93 ASSERT_EQ("short", dst_name);
94 }
95
TEST(MemoryTest,read_string_long)96 TEST(MemoryTest, read_string_long) {
97 // This string should be greater than 768 characters long (greater than 3 times
98 // the buffer in the ReadString function) to read multiple blocks.
99 static constexpr char kLongString[] =
100 "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen "
101 "sixteen seventeen eightteen nineteen twenty twenty-one twenty-two twenty-three twenty-four "
102 "twenty-five twenty-six twenty-seven twenty-eight twenty-nine thirty thirty-one thirty-two "
103 "thirty-three thirty-four thirty-five thirty-six thirty-seven thirty-eight thirty-nine forty "
104 "forty-one forty-two forty-three forty-four forty-five forty-size forty-seven forty-eight "
105 "forty-nine fifty fifty-one fifty-two fifty-three fifty-four fifty-five fifty-six "
106 "fifty-seven fifty-eight fifty-nine sixty sixty-one sixty-two sixty-three sixty-four "
107 "sixty-five sixty-six sixty-seven sixty-eight sixty-nine seventy seventy-one seventy-two "
108 "seventy-three seventy-four seventy-five seventy-six seventy-seven seventy-eight "
109 "seventy-nine eighty";
110
111 MemoryFake memory;
112
113 memory.SetMemory(100, kLongString, sizeof(kLongString));
114
115 std::string dst_name;
116 ASSERT_TRUE(memory.ReadString(100, &dst_name, sizeof(kLongString)));
117 ASSERT_EQ(kLongString, dst_name);
118
119 std::string expected_str(kLongString, 255);
120 memory.SetMemory(100, expected_str.data(), expected_str.length() + 1);
121 ASSERT_TRUE(memory.ReadString(100, &dst_name, 256));
122 ASSERT_EQ(expected_str, dst_name);
123 ASSERT_FALSE(memory.ReadString(100, &dst_name, 255));
124
125 expected_str = std::string(kLongString, 256);
126 memory.SetMemory(100, expected_str.data(), expected_str.length() + 1);
127 ASSERT_TRUE(memory.ReadString(100, &dst_name, 257));
128 ASSERT_EQ(expected_str, dst_name);
129 ASSERT_FALSE(memory.ReadString(100, &dst_name, 256));
130
131 expected_str = std::string(kLongString, 299);
132 memory.SetMemory(100, expected_str.data(), expected_str.length() + 1);
133 ASSERT_TRUE(memory.ReadString(100, &dst_name, 300));
134 ASSERT_EQ(expected_str, dst_name);
135 }
136
137 } // namespace unwindstack
138