From 29fef9aca2f5832eb721f9d097d2a6f6ebdb0179 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 11 Apr 2015 00:16:39 -0700 Subject: [PATCH] tests: Add more out of memory tests This provides more coverage for #17. --- tests/test_dom_modify.cpp | 36 ++++++++++++++++++++++++++++++++++++ tests/test_parse.cpp | 9 +++++++++ 2 files changed, 45 insertions(+) diff --git a/tests/test_dom_modify.cpp b/tests/test_dom_modify.cpp index fa50112..8610a74 100644 --- a/tests/test_dom_modify.cpp +++ b/tests/test_dom_modify.cpp @@ -1100,6 +1100,42 @@ TEST(dom_node_append_buffer_out_of_memory_buffer) CHECK(!doc.first_child()); } +TEST(dom_node_append_buffer_out_of_memory_nodes) +{ + unsigned int count = 4000; + std::basic_string data; + + for (unsigned int i = 0; i < count; ++i) + data += STR(""); + + test_runner::_memory_fail_threshold = 32768 + 128 + data.length() * sizeof(wchar_t) + 32; + + xml_document doc; + CHECK(doc.append_buffer(data.c_str(), data.length() * sizeof(wchar_t), parse_fragment).status == status_out_of_memory); + + unsigned int valid = 0; + + for (xml_node n = doc.first_child(); n; n = n.next_sibling()) + { + CHECK_STRING(n.name(), STR("a")); + valid++; + } + + CHECK(valid > 0 && valid < count); +} + +TEST(dom_node_append_buffer_out_of_memory_name) +{ + test_runner::_memory_fail_threshold = 32768 + 128; + + char data[128] = {0}; + + xml_document doc; + CHECK(doc.append_child(STR("root"))); + CHECK(doc.first_child().append_buffer(data, sizeof(data)).status == status_out_of_memory); + CHECK_STRING(doc.first_child().name(), STR("root")); +} + TEST_XML(dom_node_append_buffer_fragment, "") { xml_node node = doc.child(STR("node")); diff --git a/tests/test_parse.cpp b/tests/test_parse.cpp index 7bb2663..3ae96d1 100644 --- a/tests/test_parse.cpp +++ b/tests/test_parse.cpp @@ -926,6 +926,15 @@ TEST(parse_out_of_memory_halfway_attr) CHECK_STRING(doc.first_child().last_attribute().name(), STR("a")); } +TEST(parse_out_of_memory_conversion) +{ + test_runner::_memory_fail_threshold = 256; + + xml_document doc; + CHECK(doc.load_buffer("", 7, parse_default, encoding_latin1).status == status_out_of_memory); + CHECK(!doc.first_child()); +} + static bool test_offset(const char_t* contents, unsigned int options, pugi::xml_parse_status status, ptrdiff_t offset) { xml_document doc;