From 4e004176bacb0160007102742ec62e79a9d958bb Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 11 Apr 2015 22:46:08 -0700 Subject: tests: Improve out-of-memory tests Previously there was no guarantee that the tests that check for out of memory handling behavior are actually correct - e.g. that they correctly simulate out of memory conditions. Now every simulated out of memory condition has to be "guarded" using CHECK_ALLOC_FAIL. It makes sure that every piece of code that is supposed to cause out-of-memory does so, and that no other code runs out of memory unnoticed. --- tests/test_document.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'tests/test_document.cpp') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index 09d89d7..2c52030 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -110,7 +110,7 @@ TEST(document_load_stream_error) std::istringstream iss(""); test_runner::_memory_fail_threshold = 1; - CHECK(doc.load(iss).status == status_out_of_memory); + CHECK_ALLOC_FAIL(CHECK(doc.load(iss).status == status_out_of_memory)); } TEST(document_load_stream_empty) @@ -237,7 +237,7 @@ TEST(document_load_stream_nonseekable_out_of_memory) test_runner::_memory_fail_threshold = 1; pugi::xml_document doc; - CHECK(doc.load(in).status == status_out_of_memory); + CHECK_ALLOC_FAIL(CHECK(doc.load(in).status == status_out_of_memory)); } TEST(document_load_stream_nonseekable_out_of_memory_large) @@ -253,7 +253,7 @@ TEST(document_load_stream_nonseekable_out_of_memory_large) test_runner::_memory_fail_threshold = 10000 * 8 * 3 / 2; pugi::xml_document doc; - CHECK(doc.load(in).status == status_out_of_memory); + CHECK_ALLOC_FAIL(CHECK(doc.load(in).status == status_out_of_memory)); } #endif @@ -302,7 +302,7 @@ TEST(document_load_file_error) CHECK(doc.load_file("filedoesnotexist").status == status_file_not_found); test_runner::_memory_fail_threshold = 1; - CHECK(doc.load_file("tests/data/small.xml").status == status_out_of_memory); + CHECK_ALLOC_FAIL(CHECK(doc.load_file("tests/data/small.xml").status == status_out_of_memory)); } TEST(document_load_file_error_previous) @@ -339,7 +339,9 @@ TEST(document_load_file_wide_out_of_memory) pugi::xml_document doc; - pugi::xml_parse_result result = doc.load_file(L"tests/data/small.xml"); + pugi::xml_parse_result result; + result.status = status_out_of_memory; + CHECK_ALLOC_FAIL(result = doc.load_file(L"tests/data/small.xml")); CHECK(result.status == status_out_of_memory || result.status == status_file_not_found); } @@ -1320,7 +1322,7 @@ TEST(document_convert_out_of_memory) for (unsigned int src = 0; src < sizeof(files) / sizeof(files[0]); ++src) { xml_document doc; - CHECK(doc.load_buffer(files[src].data, files[src].size, parse_default, files[src].encoding).status == status_out_of_memory); + CHECK_ALLOC_FAIL(CHECK(doc.load_buffer(files[src].data, files[src].size, parse_default, files[src].encoding).status == status_out_of_memory)); } // cleanup -- cgit v1.2.3 From 2537cccad34b64086ad2ff47db07c3674b9be07a Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sun, 12 Apr 2015 02:17:20 -0700 Subject: tests: Fix some Coverity issues --- tests/test_document.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'tests/test_document.cpp') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index 2c52030..d81458a 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -32,9 +32,11 @@ static bool load_file_in_memory(const char* path, char*& data, size_t& size) if (!file) return false; fseek(file, 0, SEEK_END); - size = static_cast(ftell(file)); + long length = ftell(file); fseek(file, 0, SEEK_SET); + CHECK(length >= 0); + size = static_cast(length); data = new char[size]; CHECK(fread(data, 1, size, file) == size); -- cgit v1.2.3 From c5d07e2c2825129a37e8d3cac4c19ff3692c11f6 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sun, 12 Apr 2015 02:34:48 -0700 Subject: tests: Add a test that verifies absence of file leaks If an out of memory error happens in load_file there's a danger of leaking the FILE object. Since there is a limited supply of the objects we can easily test that the leak does not happen. --- tests/test_document.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'tests/test_document.cpp') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index d81458a..1545e19 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -302,11 +302,33 @@ TEST(document_load_file_error) pugi::xml_document doc; CHECK(doc.load_file("filedoesnotexist").status == status_file_not_found); +} +TEST(document_load_file_out_of_memory) +{ test_runner::_memory_fail_threshold = 1; + + pugi::xml_document doc; CHECK_ALLOC_FAIL(CHECK(doc.load_file("tests/data/small.xml").status == status_out_of_memory)); } +TEST(document_load_file_out_of_memory_file_leak) +{ + test_runner::_memory_fail_threshold = 1; + + pugi::xml_document doc; + + for (int i = 0; i < 256; ++i) + { + CHECK_ALLOC_FAIL(CHECK(doc.load_file("tests/data/small.xml").status == status_out_of_memory)); + } + + test_runner::_memory_fail_threshold = 0; + + CHECK(doc.load_file("tests/data/small.xml")); + CHECK_NODE(doc, STR("")); +} + TEST(document_load_file_error_previous) { pugi::xml_document doc; -- cgit v1.2.3