diff options
Diffstat (limited to 'libunwindstack/tests/MemoryTest.cpp')
-rw-r--r-- | libunwindstack/tests/MemoryTest.cpp | 51 |
1 files changed, 46 insertions, 5 deletions
diff --git a/libunwindstack/tests/MemoryTest.cpp b/libunwindstack/tests/MemoryTest.cpp index 365598499..8a8eb24f5 100644 --- a/libunwindstack/tests/MemoryTest.cpp +++ b/libunwindstack/tests/MemoryTest.cpp @@ -59,10 +59,10 @@ TEST(MemoryTest, read_string) { memory.SetMemory(100, name.c_str(), name.size() + 1); std::string dst_name; - ASSERT_TRUE(memory.ReadString(100, &dst_name)); + ASSERT_TRUE(memory.ReadString(100, &dst_name, 100)); ASSERT_EQ("string_in_memory", dst_name); - ASSERT_TRUE(memory.ReadString(107, &dst_name)); + ASSERT_TRUE(memory.ReadString(107, &dst_name, 100)); ASSERT_EQ("in_memory", dst_name); // Set size greater than string. @@ -82,15 +82,56 @@ TEST(MemoryTest, read_string_error) { std::string dst_name; // Read from a non-existant address. - ASSERT_FALSE(memory.ReadString(100, &dst_name)); + ASSERT_FALSE(memory.ReadString(100, &dst_name, 100)); // This should fail because there is no terminating '\0'. - ASSERT_FALSE(memory.ReadString(0, &dst_name)); + ASSERT_FALSE(memory.ReadString(0, &dst_name, 100)); // This should pass because there is a terminating '\0'. memory.SetData8(name.size(), '\0'); - ASSERT_TRUE(memory.ReadString(0, &dst_name)); + ASSERT_TRUE(memory.ReadString(0, &dst_name, 100)); ASSERT_EQ("short", dst_name); } +TEST(MemoryTest, read_string_long) { + // This string should be greater than 768 characters long (greater than 3 times + // the buffer in the ReadString function) to read multiple blocks. + static constexpr char kLongString[] = + "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen " + "sixteen seventeen eightteen nineteen twenty twenty-one twenty-two twenty-three twenty-four " + "twenty-five twenty-six twenty-seven twenty-eight twenty-nine thirty thirty-one thirty-two " + "thirty-three thirty-four thirty-five thirty-six thirty-seven thirty-eight thirty-nine forty " + "forty-one forty-two forty-three forty-four forty-five forty-size forty-seven forty-eight " + "forty-nine fifty fifty-one fifty-two fifty-three fifty-four fifty-five fifty-six " + "fifty-seven fifty-eight fifty-nine sixty sixty-one sixty-two sixty-three sixty-four " + "sixty-five sixty-six sixty-seven sixty-eight sixty-nine seventy seventy-one seventy-two " + "seventy-three seventy-four seventy-five seventy-six seventy-seven seventy-eight " + "seventy-nine eighty"; + + MemoryFake memory; + + memory.SetMemory(100, kLongString, sizeof(kLongString)); + + std::string dst_name; + ASSERT_TRUE(memory.ReadString(100, &dst_name, sizeof(kLongString))); + ASSERT_EQ(kLongString, dst_name); + + std::string expected_str(kLongString, 255); + memory.SetMemory(100, expected_str.data(), expected_str.length() + 1); + ASSERT_TRUE(memory.ReadString(100, &dst_name, 256)); + ASSERT_EQ(expected_str, dst_name); + ASSERT_FALSE(memory.ReadString(100, &dst_name, 255)); + + expected_str = std::string(kLongString, 256); + memory.SetMemory(100, expected_str.data(), expected_str.length() + 1); + ASSERT_TRUE(memory.ReadString(100, &dst_name, 257)); + ASSERT_EQ(expected_str, dst_name); + ASSERT_FALSE(memory.ReadString(100, &dst_name, 256)); + + expected_str = std::string(kLongString, 299); + memory.SetMemory(100, expected_str.data(), expected_str.length() + 1); + ASSERT_TRUE(memory.ReadString(100, &dst_name, 300)); + ASSERT_EQ(expected_str, dst_name); +} + } // namespace unwindstack |