diff options
Diffstat (limited to 'memory_replay/tests')
-rw-r--r-- | memory_replay/tests/ActionTest.cpp | 168 | ||||
-rw-r--r-- | memory_replay/tests/AllocTest.cpp | 146 | ||||
-rw-r--r-- | memory_replay/tests/FileTest.cpp | 112 | ||||
-rw-r--r-- | memory_replay/tests/LineBufferTest.cpp | 241 | ||||
-rw-r--r-- | memory_replay/tests/NativeInfoTest.cpp | 91 | ||||
-rw-r--r-- | memory_replay/tests/ThreadTest.cpp | 17 | ||||
-rw-r--r-- | memory_replay/tests/ThreadsTest.cpp | 29 | ||||
-rw-r--r-- | memory_replay/tests/test.txt | 2 | ||||
-rw-r--r-- | memory_replay/tests/test.zip | bin | 201 -> 0 bytes |
9 files changed, 470 insertions, 336 deletions
diff --git a/memory_replay/tests/ActionTest.cpp b/memory_replay/tests/ActionTest.cpp new file mode 100644 index 00000000..cd72c24e --- /dev/null +++ b/memory_replay/tests/ActionTest.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> +#include <stdint.h> +#include <string.h> + +#include "Action.h" +#include "Pointers.h" + +TEST(ActionTest, malloc) { + uint8_t memory[Action::MaxActionSize()]; + const char* line = "1024"; + Action* action = Action::CreateAction(0x1234, "malloc", line, memory); + ASSERT_TRUE(action != NULL); + ASSERT_FALSE(action->DoesFree()); + ASSERT_FALSE(action->EndThread()); + + Pointers pointers(1); + action->Execute(&pointers); + void* pointer = pointers.Remove(0x1234); + ASSERT_TRUE(pointer != nullptr); + free(pointer); +} + +TEST(ActionTest, malloc_malformed) { + uint8_t memory[128]; + const char* line = ""; + Action* action = Action::CreateAction(0x1234, "malloc", line, memory); + ASSERT_FALSE(action != NULL); +} + +TEST(ActionTest, free) { + uint8_t memory[128]; + const char* line = ""; + Action* action = Action::CreateAction(0x1234, "free", line, memory); + ASSERT_TRUE(action != NULL); + ASSERT_TRUE(action->DoesFree()); + ASSERT_FALSE(action->EndThread()); + + Pointers pointers(1); + pointers.Add(0x1234, malloc(10)); + action->Execute(&pointers); +} + +TEST(ActionTest, calloc) { + uint8_t memory[128]; + const char* line = "100 10"; + Action* action = Action::CreateAction(0x1234, "calloc", line, memory); + ASSERT_TRUE(action != NULL); + ASSERT_FALSE(action->DoesFree()); + ASSERT_FALSE(action->EndThread()); + + Pointers pointers(1); + action->Execute(&pointers); + void* pointer = pointers.Remove(0x1234); + ASSERT_TRUE(pointer != nullptr); + free(pointer); +} + +TEST(ActionTest, free_zero) { + uint8_t memory[128]; + const char* line = ""; + Action* action = Action::CreateAction(0, "free", line, memory); + ASSERT_TRUE(action != NULL); + ASSERT_FALSE(action->DoesFree()); + ASSERT_FALSE(action->EndThread()); + // Should be a nop. + action->Execute(nullptr); +} + +TEST(ActionTest, calloc_malformed) { + uint8_t memory[128]; + const char* line1 = "100"; + Action* action = Action::CreateAction(0x1234, "calloc", line1, memory); + ASSERT_FALSE(action != NULL); + + const char* line2 = ""; + action = Action::CreateAction(0x1234, "calloc", line2, memory); + ASSERT_FALSE(action != NULL); +} + +TEST(ActionTest, realloc) { + uint8_t memory[128]; + const char* line = "0xabcd 100"; + Action* action = Action::CreateAction(0x1234, "realloc", line, memory); + ASSERT_TRUE(action != NULL); + ASSERT_TRUE(action->DoesFree()); + ASSERT_FALSE(action->EndThread()); + + Pointers pointers(1); + pointers.Add(0xabcd, malloc(10)); + action->Execute(&pointers); + void* pointer = pointers.Remove(0x1234); + ASSERT_TRUE(pointer != nullptr); + free(pointer); + + const char* null_line = "0x0 100"; + action = Action::CreateAction(0x1234, "realloc", null_line, memory); + ASSERT_FALSE(action->DoesFree()); + ASSERT_FALSE(action->EndThread()); + + action->Execute(&pointers); + pointer = pointers.Remove(0x1234); + ASSERT_TRUE(pointer != nullptr); + free(pointer); +} + +TEST(ActionTest, realloc_malformed) { + uint8_t memory[128]; + const char* line1 = "0x100"; + Action* action = Action::CreateAction(0x1234, "realloc", line1, memory); + ASSERT_FALSE(action != NULL); + + const char* line2 = ""; + action = Action::CreateAction(0x1234, "realloc", line2, memory); + ASSERT_FALSE(action != NULL); +} + +TEST(ActionTest, memalign) { + uint8_t memory[128]; + const char* line = "16 300"; + Action* action = Action::CreateAction(0x1234, "memalign", line, memory); + ASSERT_TRUE(action != NULL); + ASSERT_FALSE(action->DoesFree()); + ASSERT_FALSE(action->EndThread()); + + Pointers pointers(1); + action->Execute(&pointers); + void* pointer = pointers.Remove(0x1234); + ASSERT_TRUE(pointer != nullptr); + free(pointer); +} + +TEST(ActionTest, memalign_malformed) { + uint8_t memory[128]; + const char* line1 = "100"; + Action* action = Action::CreateAction(0x1234, "memalign", line1, memory); + ASSERT_FALSE(action != NULL); + + const char* line2 = ""; + action = Action::CreateAction(0x1234, "memalign", line2, memory); + ASSERT_FALSE(action != NULL); +} + +TEST(ActionTest, endthread) { + uint8_t memory[128]; + const char* line = ""; + Action* action = Action::CreateAction(0x0, "thread_done", line, memory); + ASSERT_TRUE(action != NULL); + ASSERT_FALSE(action->DoesFree()); + ASSERT_TRUE(action->EndThread()); + + action->Execute(nullptr); +} diff --git a/memory_replay/tests/AllocTest.cpp b/memory_replay/tests/AllocTest.cpp deleted file mode 100644 index d5dd0573..00000000 --- a/memory_replay/tests/AllocTest.cpp +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <stdint.h> - -#include <string> - -#include <gtest/gtest.h> - -#include "Alloc.h" - -TEST(AllocTest, malloc_valid) { - std::string line = "1234: malloc 0xabd0000 20"; - AllocEntry entry; - AllocGetData(line, &entry); - EXPECT_EQ(MALLOC, entry.type); - EXPECT_EQ(1234, entry.tid); - EXPECT_EQ(0xabd0000U, entry.ptr); - EXPECT_EQ(20U, entry.size); - EXPECT_EQ(0U, entry.u.align); -} - -TEST(AllocTest, malloc_invalid) { - std::string line = "1234: malloc 0xabd0000"; - AllocEntry entry; - EXPECT_DEATH(AllocGetData(line, &entry), ""); - - line = "1234: malloc"; - EXPECT_DEATH(AllocGetData(line, &entry), ""); -} - -TEST(AllocTest, free_valid) { - std::string line = "1235: free 0x5000"; - AllocEntry entry; - AllocGetData(line, &entry); - EXPECT_EQ(FREE, entry.type); - EXPECT_EQ(1235, entry.tid); - EXPECT_EQ(0x5000U, entry.ptr); - EXPECT_EQ(0U, entry.size); - EXPECT_EQ(0U, entry.u.align); -} - -TEST(AllocTest, free_invalid) { - std::string line = "1234: free"; - AllocEntry entry; - EXPECT_DEATH(AllocGetData(line, &entry), ""); -} - -TEST(AllocTest, calloc_valid) { - std::string line = "1236: calloc 0x8000 50 30"; - AllocEntry entry; - AllocGetData(line, &entry); - EXPECT_EQ(CALLOC, entry.type); - EXPECT_EQ(1236, entry.tid); - EXPECT_EQ(0x8000U, entry.ptr); - EXPECT_EQ(30U, entry.size); - EXPECT_EQ(50U, entry.u.n_elements); -} - -TEST(AllocTest, calloc_invalid) { - std::string line = "1236: calloc 0x8000 50"; - AllocEntry entry; - EXPECT_DEATH(AllocGetData(line, &entry), ""); - - line = "1236: calloc 0x8000"; - EXPECT_DEATH(AllocGetData(line, &entry), ""); - - line = "1236: calloc"; - EXPECT_DEATH(AllocGetData(line, &entry), ""); -} - -TEST(AllocTest, realloc_valid) { - std::string line = "1237: realloc 0x9000 0x4000 80"; - AllocEntry entry; - AllocGetData(line, &entry); - EXPECT_EQ(REALLOC, entry.type); - EXPECT_EQ(1237, entry.tid); - EXPECT_EQ(0x9000U, entry.ptr); - EXPECT_EQ(80U, entry.size); - EXPECT_EQ(0x4000U, entry.u.old_ptr); -} - -TEST(AllocTest, realloc_invalid) { - std::string line = "1237: realloc 0x9000 0x4000"; - AllocEntry entry; - EXPECT_DEATH(AllocGetData(line, &entry), ""); - - line = "1237: realloc 0x9000"; - EXPECT_DEATH(AllocGetData(line, &entry), ""); - - line = "1237: realloc"; - EXPECT_DEATH(AllocGetData(line, &entry), ""); -} - -TEST(AllocTest, memalign_valid) { - std::string line = "1238: memalign 0xa000 16 89"; - AllocEntry entry; - AllocGetData(line, &entry); - EXPECT_EQ(MEMALIGN, entry.type); - EXPECT_EQ(1238, entry.tid); - EXPECT_EQ(0xa000U, entry.ptr); - EXPECT_EQ(89U, entry.size); - EXPECT_EQ(16U, entry.u.align); -} - -TEST(AllocTest, memalign_invalid) { - std::string line = "1238: memalign 0xa000 16"; - AllocEntry entry; - EXPECT_DEATH(AllocGetData(line, &entry), ""); - - line = "1238: memalign 0xa000"; - EXPECT_DEATH(AllocGetData(line, &entry), ""); - - line = "1238: memalign"; - EXPECT_DEATH(AllocGetData(line, &entry), ""); -} - -TEST(AllocTest, thread_done_valid) { - std::string line = "1239: thread_done 0x0"; - AllocEntry entry; - AllocGetData(line, &entry); - EXPECT_EQ(THREAD_DONE, entry.type); - EXPECT_EQ(1239, entry.tid); - EXPECT_EQ(0U, entry.ptr); - EXPECT_EQ(0U, entry.size); - EXPECT_EQ(0U, entry.u.old_ptr); -} - -TEST(AllocTest, thread_done_invalid) { - std::string line = "1240: thread_done"; - AllocEntry entry; - EXPECT_DEATH(AllocGetData(line, &entry), ""); -} diff --git a/memory_replay/tests/FileTest.cpp b/memory_replay/tests/FileTest.cpp deleted file mode 100644 index 77c0593d..00000000 --- a/memory_replay/tests/FileTest.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <malloc.h> -#include <stdint.h> - -#include <string> - -#include <android-base/file.h> -#include <gtest/gtest.h> - -#include "Alloc.h" -#include "File.h" - -static std::string GetTestDirectory() { - return android::base::GetExecutableDirectory() + "/tests"; -} - -static std::string GetTestZip() { - return GetTestDirectory() + "/test.zip"; -} - -TEST(FileTest, zip_get_contents) { - EXPECT_EQ("12345: malloc 0x1000 16\n12345: free 0x1000\n", ZipGetContents(GetTestZip().c_str())); -} - -TEST(FileTest, zip_get_contents_bad_file) { - EXPECT_EQ("", ZipGetContents("/does/not/exist.zip")); -} - -TEST(FileTest, get_unwind_info_from_zip_file) { - // This will allocate, so do it before getting mallinfo. - std::string file_name = GetTestZip(); - - size_t mallinfo_before = mallinfo().uordblks; - AllocEntry* entries; - size_t num_entries; - GetUnwindInfo(file_name.c_str(), &entries, &num_entries); - size_t mallinfo_after = mallinfo().uordblks; - - // Verify no memory is allocated. - EXPECT_EQ(mallinfo_after, mallinfo_before); - - ASSERT_EQ(2U, num_entries); - EXPECT_EQ(12345, entries[0].tid); - EXPECT_EQ(MALLOC, entries[0].type); - EXPECT_EQ(0x1000U, entries[0].ptr); - EXPECT_EQ(16U, entries[0].size); - EXPECT_EQ(0U, entries[0].u.old_ptr); - - EXPECT_EQ(12345, entries[1].tid); - EXPECT_EQ(FREE, entries[1].type); - EXPECT_EQ(0x1000U, entries[1].ptr); - EXPECT_EQ(0U, entries[1].size); - EXPECT_EQ(0U, entries[1].u.old_ptr); - - FreeEntries(entries, num_entries); -} - -TEST(FileTest, get_unwind_info_bad_zip_file) { - AllocEntry* entries; - size_t num_entries; - EXPECT_DEATH(GetUnwindInfo("/does/not/exist.zip", &entries, &num_entries), ""); -} - -TEST(FileTest, get_unwind_info_from_text_file) { - // This will allocate, so do it before getting mallinfo. - std::string file_name = GetTestDirectory() + "/test.txt"; - - size_t mallinfo_before = mallinfo().uordblks; - AllocEntry* entries; - size_t num_entries; - GetUnwindInfo(file_name.c_str(), &entries, &num_entries); - size_t mallinfo_after = mallinfo().uordblks; - - // Verify no memory is allocated. - EXPECT_EQ(mallinfo_after, mallinfo_before); - - ASSERT_EQ(2U, num_entries); - EXPECT_EQ(98765, entries[0].tid); - EXPECT_EQ(MEMALIGN, entries[0].type); - EXPECT_EQ(0xa000U, entries[0].ptr); - EXPECT_EQ(124U, entries[0].size); - EXPECT_EQ(16U, entries[0].u.align); - - EXPECT_EQ(98765, entries[1].tid); - EXPECT_EQ(FREE, entries[1].type); - EXPECT_EQ(0xa000U, entries[1].ptr); - EXPECT_EQ(0U, entries[1].size); - EXPECT_EQ(0U, entries[1].u.old_ptr); - - FreeEntries(entries, num_entries); -} - -TEST(FileTest, get_unwind_info_bad_file) { - AllocEntry* entries; - size_t num_entries; - EXPECT_DEATH(GetUnwindInfo("/does/not/exist", &entries, &num_entries), ""); -} diff --git a/memory_replay/tests/LineBufferTest.cpp b/memory_replay/tests/LineBufferTest.cpp new file mode 100644 index 00000000..1a310226 --- /dev/null +++ b/memory_replay/tests/LineBufferTest.cpp @@ -0,0 +1,241 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +#include <string> + +#include <android-base/file.h> + +#include "LineBuffer.h" + +class LineBufferTest : public ::testing::Test { + protected: + void SetUp() override { + tmp_file_ = new TemporaryFile(); + ASSERT_TRUE(tmp_file_->fd != -1); + } + + void TearDown() override { + delete tmp_file_; + } + + TemporaryFile* tmp_file_ = nullptr; +}; + +TEST_F(LineBufferTest, single_line) { + std::string line_data; + line_data += "Single line with newline.\n"; + ASSERT_TRUE(TEMP_FAILURE_RETRY( + write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1); + ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1)); + + char buffer[100]; + LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer)); + + char* line; + size_t line_len; + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("Single line with newline.", line); + ASSERT_EQ(sizeof("Single line with newline.") - 1, line_len); + + ASSERT_FALSE(line_buf.GetLine(&line, &line_len)); +} + +TEST_F(LineBufferTest, single_line_no_newline) { + std::string line_data; + line_data += "Single line with no newline."; + ASSERT_TRUE(TEMP_FAILURE_RETRY( + write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1); + ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1)); + + char buffer[100]; + LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer)); + + char* line; + size_t line_len; + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("Single line with no newline.", line); + ASSERT_EQ(sizeof("Single line with no newline.") - 1, line_len); + + ASSERT_FALSE(line_buf.GetLine(&line, &line_len)); +} + +TEST_F(LineBufferTest, single_read) { + std::string line_data; + line_data += "The first line.\n"; + line_data += "Second line here.\n"; + line_data += "Third line is last.\n"; + ASSERT_TRUE(TEMP_FAILURE_RETRY( + write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1); + ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1)); + + char buffer[100]; + LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer)); + + char* line; + size_t line_len; + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("The first line.", line); + ASSERT_EQ(sizeof("The first line.") - 1, line_len); + + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("Second line here.", line); + ASSERT_EQ(sizeof("Second line here.") - 1, line_len); + + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("Third line is last.", line); + ASSERT_EQ(sizeof("Third line is last.") - 1, line_len); + + ASSERT_FALSE(line_buf.GetLine(&line, &line_len)); +} + +TEST_F(LineBufferTest, single_read_no_end_newline) { + std::string line_data; + line_data += "The first line.\n"; + line_data += "Second line here.\n"; + line_data += "Third line is last no newline."; + ASSERT_TRUE(TEMP_FAILURE_RETRY( + write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1); + ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1)); + + char buffer[100]; + LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer)); + + char* line; + size_t line_len; + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("The first line.", line); + ASSERT_EQ(sizeof("The first line.") - 1, line_len); + + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("Second line here.", line); + ASSERT_EQ(sizeof("Second line here.") - 1, line_len); + + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("Third line is last no newline.", line); + ASSERT_EQ(sizeof("Third line is last no newline.") - 1, line_len); + + ASSERT_FALSE(line_buf.GetLine(&line, &line_len)); +} + +TEST_F(LineBufferTest, one_line_per_read) { + std::string line_data; + line_data += "The first line.\n"; + line_data += "Second line here.\n"; + line_data += "Third line is last.\n"; + line_data += "The fourth line.\n"; + ASSERT_TRUE(TEMP_FAILURE_RETRY( + write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1); + ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1)); + + char buffer[24]; + LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer)); + + char* line; + size_t line_len; + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("The first line.", line); + ASSERT_EQ(sizeof("The first line.") - 1, line_len); + + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("Second line here.", line); + ASSERT_EQ(sizeof("Second line here.") - 1, line_len); + + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("Third line is last.", line); + ASSERT_EQ(sizeof("Third line is last.") - 1, line_len); + + line_data += "The fourth line.\n"; + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("The fourth line.", line); + ASSERT_EQ(sizeof("The fourth line.") - 1, line_len); + + ASSERT_FALSE(line_buf.GetLine(&line, &line_len)); +} + +TEST_F(LineBufferTest, multiple_line_per_read_multiple_reads) { + std::string line_data; + line_data += "The first line.\n"; + line_data += "Second line here.\n"; + line_data += "Third line is last.\n"; + line_data += "The fourth line.\n"; + ASSERT_TRUE(TEMP_FAILURE_RETRY( + write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1); + ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1)); + + char buffer[60]; + LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer)); + + char* line; + size_t line_len; + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("The first line.", line); + ASSERT_EQ(sizeof("The first line.") - 1, line_len); + + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("Second line here.", line); + ASSERT_EQ(sizeof("Second line here.") - 1, line_len); + + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("Third line is last.", line); + ASSERT_EQ(sizeof("Third line is last.") - 1, line_len); + + line_data += "The fourth line.\n"; + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("The fourth line.", line); + ASSERT_EQ(sizeof("The fourth line.") - 1, line_len); + + ASSERT_FALSE(line_buf.GetLine(&line, &line_len)); +} + +TEST_F(LineBufferTest, line_larger_than_buffer) { + std::string line_data; + line_data += "The first line.\n"; + line_data += "Second line here.\n"; + line_data += "This is a really, really, really, kind of long.\n"; + line_data += "The fourth line.\n"; + ASSERT_TRUE(TEMP_FAILURE_RETRY( + write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1); + ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1)); + + char buffer[25]; + LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer)); + + char* line; + size_t line_len; + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("The first line.", line); + ASSERT_EQ(sizeof("The first line.") - 1, line_len); + + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("Second line here.", line); + ASSERT_EQ(sizeof("Second line here.") - 1, line_len); + + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("This is a really, really", line); + ASSERT_EQ(sizeof(buffer) - 1, line_len); + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ(", really, kind of long.", line); + ASSERT_EQ(sizeof(", really, kind of long.") - 1, line_len); + + line_data += "The fourth line.\n"; + ASSERT_TRUE(line_buf.GetLine(&line, &line_len)); + ASSERT_STREQ("The fourth line.", line); + ASSERT_EQ(sizeof("The fourth line.") - 1, line_len); + + ASSERT_FALSE(line_buf.GetLine(&line, &line_len)); +} diff --git a/memory_replay/tests/NativeInfoTest.cpp b/memory_replay/tests/NativeInfoTest.cpp index 845ec043..e0dea509 100644 --- a/memory_replay/tests/NativeInfoTest.cpp +++ b/memory_replay/tests/NativeInfoTest.cpp @@ -14,12 +14,12 @@ * limitations under the License. */ +#include <gtest/gtest.h> #include <stdint.h> #include <string> #include <android-base/file.h> -#include <gtest/gtest.h> #include "NativeInfo.h" @@ -41,8 +41,8 @@ TEST_F(NativeInfoTest, no_matching) { std::string smaps_data = "b6f1a000-b6f1c000 rw-p 00000000 00:00 0 [anon:thread signal stack]\n" "Size: 8 kB\n" - "Rss: 12 kB\n" - "Pss: 0 kB\n" + "Rss: 0 kB\n" + "Pss: 12 kB\n" "Shared_Clean: 0 kB\n" "Shared_Dirty: 0 kB\n" "Private_Clean: 0 kB\n" @@ -59,10 +59,10 @@ TEST_F(NativeInfoTest, no_matching) { write(tmp_file_->fd, smaps_data.c_str(), smaps_data.size())) != -1); ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1)); - size_t rss_bytes = 1; + size_t pss_bytes = 1; size_t va_bytes = 1; - NativeGetInfo(tmp_file_->fd, &rss_bytes, &va_bytes); - ASSERT_EQ(0U, rss_bytes); + GetNativeInfo(tmp_file_->fd, &pss_bytes, &va_bytes); + ASSERT_EQ(0U, pss_bytes); ASSERT_EQ(0U, va_bytes); } @@ -70,8 +70,8 @@ TEST_F(NativeInfoTest, multiple_anons) { std::string smaps_data = "b6f1a000-b6f1c000 rw-p 00000000 00:00 0 [anon:libc_malloc]\n" "Size: 8 kB\n" - "Rss: 12 kB\n" - "Pss: 0 kB\n" + "Rss: 0 kB\n" + "Pss: 12 kB\n" "Shared_Clean: 0 kB\n" "Shared_Dirty: 0 kB\n" "Private_Clean: 0 kB\n" @@ -86,8 +86,8 @@ TEST_F(NativeInfoTest, multiple_anons) { "Name: [anon:libc_malloc]\n" "b6f1e000-b6f1f000 rw-p 00000000 00:00 0 [anon:libc_malloc]\n" "Size: 8 kB\n" - "Rss: 20 kB\n" - "Pss: 0 kB\n" + "Rss: 0 kB\n" + "Pss: 20 kB\n" "Shared_Clean: 0 kB\n" "Shared_Dirty: 0 kB\n" "Private_Clean: 0 kB\n" @@ -102,8 +102,8 @@ TEST_F(NativeInfoTest, multiple_anons) { "Name: [anon:libc_malloc]\n" "b6f2e000-b6f2f000 rw-p 00000000 00:00 0\n" "Size: 8 kB\n" - "Rss: 24 kB\n" - "Pss: 0 kB\n" + "Rss: 0 kB\n" + "Pss: 24 kB\n" "Shared_Clean: 0 kB\n" "Shared_Dirty: 0 kB\n" "Private_Clean: 0 kB\n" @@ -120,10 +120,10 @@ TEST_F(NativeInfoTest, multiple_anons) { write(tmp_file_->fd, smaps_data.c_str(), smaps_data.size())) != -1); ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1)); - size_t rss_bytes = 1; + size_t pss_bytes = 1; size_t va_bytes = 1; - NativeGetInfo(tmp_file_->fd, &rss_bytes, &va_bytes); - ASSERT_EQ(32768U, rss_bytes); + GetNativeInfo(tmp_file_->fd, &pss_bytes, &va_bytes); + ASSERT_EQ(32768U, pss_bytes); ASSERT_EQ(12288U, va_bytes); } @@ -131,8 +131,8 @@ TEST_F(NativeInfoTest, multiple_heaps) { std::string smaps_data = "b6f1a000-b6f1c000 rw-p 00000000 00:00 0 [heap]\n" "Size: 8 kB\n" - "Rss: 24 kB\n" - "Pss: 0 kB\n" + "Rss: 0 kB\n" + "Pss: 24 kB\n" "Shared_Clean: 0 kB\n" "Shared_Dirty: 0 kB\n" "Private_Clean: 0 kB\n" @@ -147,8 +147,8 @@ TEST_F(NativeInfoTest, multiple_heaps) { "Name: [heap]\n" "b6f1e000-b6f1f000 rw-p 00000000 00:00 0 [heap]\n" "Size: 8 kB\n" - "Rss: 20 kB\n" - "Pss: 0 kB\n" + "Rss: 0 kB\n" + "Pss: 20 kB\n" "Shared_Clean: 0 kB\n" "Shared_Dirty: 0 kB\n" "Private_Clean: 0 kB\n" @@ -163,8 +163,8 @@ TEST_F(NativeInfoTest, multiple_heaps) { "Name: [heap]\n" "b6f2e000-b6f2f000 rw-p 00000000 00:00 0\n" "Size: 8 kB\n" - "Rss: 24 kB\n" - "Pss: 0 kB\n" + "Rss: 0 kB\n" + "Pss: 24 kB\n" "Shared_Clean: 0 kB\n" "Shared_Dirty: 0 kB\n" "Private_Clean: 0 kB\n" @@ -181,10 +181,10 @@ TEST_F(NativeInfoTest, multiple_heaps) { write(tmp_file_->fd, smaps_data.c_str(), smaps_data.size())) != -1); ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1)); - size_t rss_bytes = 1; + size_t pss_bytes = 1; size_t va_bytes = 1; - NativeGetInfo(tmp_file_->fd, &rss_bytes, &va_bytes); - ASSERT_EQ(45056U, rss_bytes); + GetNativeInfo(tmp_file_->fd, &pss_bytes, &va_bytes); + ASSERT_EQ(45056U, pss_bytes); ASSERT_EQ(12288U, va_bytes); } @@ -192,8 +192,8 @@ TEST_F(NativeInfoTest, mix_heap_anon) { std::string smaps_data = "b6f1a000-b6f1c000 rw-p 00000000 00:00 0 [heap]\n" "Size: 8 kB\n" - "Rss: 32 kB\n" - "Pss: 0 kB\n" + "Rss: 0 kB\n" + "Pss: 32 kB\n" "Shared_Clean: 0 kB\n" "Shared_Dirty: 0 kB\n" "Private_Clean: 0 kB\n" @@ -208,8 +208,8 @@ TEST_F(NativeInfoTest, mix_heap_anon) { "Name: [heap]\n" "b6f1e000-b6f1f000 rw-p 00000000 00:00 0 [anon:skip]\n" "Size: 8 kB\n" - "Rss: 32 kB\n" - "Pss: 0 kB\n" + "Rss: 0 kB\n" + "Pss: 32 kB\n" "Shared_Clean: 0 kB\n" "Shared_Dirty: 0 kB\n" "Private_Clean: 0 kB\n" @@ -224,8 +224,8 @@ TEST_F(NativeInfoTest, mix_heap_anon) { "Name: [anon:skip]\n" "b6f2e000-b6f2f000 rw-p 00000000 00:00 0 [anon:libc_malloc]\n" "Size: 8 kB\n" - "Rss: 40 kB\n" - "Pss: 0 kB\n" + "Rss: 0 kB\n" + "Pss: 40 kB\n" "Shared_Clean: 0 kB\n" "Shared_Dirty: 0 kB\n" "Private_Clean: 0 kB\n" @@ -240,8 +240,8 @@ TEST_F(NativeInfoTest, mix_heap_anon) { "Name: [anon:libc_malloc]\n" "b6f3e000-b6f3f000 rw-p 00000000 00:00 0\n" "Size: 8 kB\n" - "Rss: 24 kB\n" - "Pss: 0 kB\n" + "Rss: 0 kB\n" + "Pss: 24 kB\n" "Shared_Clean: 0 kB\n" "Shared_Dirty: 0 kB\n" "Private_Clean: 0 kB\n" @@ -253,31 +253,14 @@ TEST_F(NativeInfoTest, mix_heap_anon) { "KernelPageSize: 4 kB\n" "MMUPageSize: 4 kB\n" "Locked: 0 kB\n" - "Name:\n" - "b6f4e000-b6f6f000 rw-p 00000000 00:00 0 [anon:scudo:test]\n" - "Size: 8 kB\n" - "Rss: 52 kB\n" - "Pss: 0 kB\n" - "Shared_Clean: 0 kB\n" - "Shared_Dirty: 0 kB\n" - "Private_Clean: 0 kB\n" - "Private_Dirty: 0 kB\n" - "Referenced: 0 kB\n" - "Anonymous: 0 kB\n" - "AnonHugePages: 0 kB\n" - "Swap: 0 kB\n" - "KernelPageSize: 4 kB\n" - "MMUPageSize: 4 kB\n" - "Locked: 0 kB\n" - "Name: [anon:scudo:test]\n"; - + "Name:\n"; ASSERT_TRUE(TEMP_FAILURE_RETRY( write(tmp_file_->fd, smaps_data.c_str(), smaps_data.size())) != -1); ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1)); - size_t rss_bytes = 1; + size_t pss_bytes = 1; size_t va_bytes = 1; - NativeGetInfo(tmp_file_->fd, &rss_bytes, &va_bytes); - EXPECT_EQ(126976U, rss_bytes); - EXPECT_EQ(147456U, va_bytes); + GetNativeInfo(tmp_file_->fd, &pss_bytes, &va_bytes); + ASSERT_EQ(73728U, pss_bytes); + ASSERT_EQ(12288U, va_bytes); } diff --git a/memory_replay/tests/ThreadTest.cpp b/memory_replay/tests/ThreadTest.cpp index 4cecf189..72492905 100644 --- a/memory_replay/tests/ThreadTest.cpp +++ b/memory_replay/tests/ThreadTest.cpp @@ -14,13 +14,13 @@ * limitations under the License. */ +#include <gtest/gtest.h> #include <pthread.h> #include <unistd.h> #include <utility> -#include <gtest/gtest.h> - +#include "Action.h" #include "Pointers.h" #include "Thread.h" @@ -99,3 +99,16 @@ TEST(ThreadTest, pointers) { thread.set_pointers(&pointers); ASSERT_TRUE(thread.pointers() == &pointers); } + +TEST(ThreadTest, action) { + Thread thread; + + Action* action = thread.CreateAction(0x1234, "thread_done", ""); + ASSERT_EQ(action, thread.GetAction()); + + // Verify the action object is not garbage. + action->Execute(nullptr); + + ASSERT_TRUE(action->EndThread()); + ASSERT_FALSE(action->DoesFree()); +} diff --git a/memory_replay/tests/ThreadsTest.cpp b/memory_replay/tests/ThreadsTest.cpp index 990c9130..c2ba023c 100644 --- a/memory_replay/tests/ThreadsTest.cpp +++ b/memory_replay/tests/ThreadsTest.cpp @@ -16,7 +16,7 @@ #include <gtest/gtest.h> -#include "Alloc.h" +#include "Action.h" #include "Pointers.h" #include "Thread.h" #include "Threads.h" @@ -32,8 +32,7 @@ TEST(ThreadsTest, single_thread) { Thread* found_thread = threads.FindThread(900); ASSERT_EQ(thread, found_thread); - AllocEntry thread_done = {.type = THREAD_DONE}; - thread->SetAllocEntry(&thread_done); + thread->CreateAction(0x1234, "thread_done", ""); thread->SetPending(); @@ -67,10 +66,9 @@ TEST(ThreadsTest, multiple_threads) { Thread* found_thread3 = threads.FindThread(902); ASSERT_EQ(thread3, found_thread3); - AllocEntry thread_done = {.type = THREAD_DONE}; - thread1->SetAllocEntry(&thread_done); - thread2->SetAllocEntry(&thread_done); - thread3->SetAllocEntry(&thread_done); + thread1->CreateAction(0x1234, "thread_done", ""); + thread2->CreateAction(0x1235, "thread_done", ""); + thread3->CreateAction(0x1236, "thread_done", ""); thread1->SetPending(); threads.Finish(thread1); @@ -95,26 +93,17 @@ TEST(ThreadsTest, verify_quiesce) { // If WaitForAllToQuiesce is not correct, then this should provoke an error // since we are overwriting the action data while it's being used. - constexpr size_t kAllocEntries = 512; - std::vector<AllocEntry> mallocs(kAllocEntries); - std::vector<AllocEntry> frees(kAllocEntries); - for (size_t i = 0; i < kAllocEntries; i++) { - mallocs[i].type = MALLOC; - mallocs[i].ptr = 0x1234 + i; - mallocs[i].size = 100; - thread->SetAllocEntry(&mallocs[i]); + for (size_t i = 0; i < 512; i++) { + thread->CreateAction(0x1234 + i, "malloc", "100"); thread->SetPending(); threads.WaitForAllToQuiesce(); - frees[i].type = FREE; - frees[i].ptr = 0x1234 + i; - thread->SetAllocEntry(&frees[i]); + thread->CreateAction(0x1234 + i, "free", ""); thread->SetPending(); threads.WaitForAllToQuiesce(); } - AllocEntry thread_done = {.type = THREAD_DONE}; - thread->SetAllocEntry(&thread_done); + thread->CreateAction(0x1236, "thread_done", ""); thread->SetPending(); threads.Finish(thread); ASSERT_EQ(0U, threads.num_threads()); diff --git a/memory_replay/tests/test.txt b/memory_replay/tests/test.txt deleted file mode 100644 index c7f43f2c..00000000 --- a/memory_replay/tests/test.txt +++ /dev/null @@ -1,2 +0,0 @@ -98765: memalign 0xa000 16 124 -98765: free 0xa000 diff --git a/memory_replay/tests/test.zip b/memory_replay/tests/test.zip Binary files differdeleted file mode 100644 index baef5597..00000000 --- a/memory_replay/tests/test.zip +++ /dev/null |