From bbd75fda4618dcbef272c8e5432153992c392588 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Thu, 23 Oct 2014 05:46:44 +0000 Subject: tests: Improve test coverage git-svn-id: https://pugixml.googlecode.com/svn/trunk@1074 99668b35-9821-0410-8761-19e4c4f06640 --- tests/test_document.cpp | 34 ++++++++++++++++++++++++++++++++++ tests/test_dom_modify.cpp | 35 +++++++++++++++++++++++++++++++++++ tests/test_parse.cpp | 31 ++++++++++++++++++++++++++++++- tests/test_xpath_paths.cpp | 3 +++ 4 files changed, 102 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index c75ed8f..c76f671 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -1235,3 +1235,37 @@ TEST(document_alignment) doc->~xml_document(); } } + +TEST(document_convert_out_of_memory) +{ + file_data_t files[] = + { + {"tests/data/utftest_utf16_be_clean.xml", encoding_utf16_be, 0, 0}, + {"tests/data/utftest_utf16_le_clean.xml", encoding_utf16_le, 0, 0}, + {"tests/data/utftest_utf32_be_clean.xml", encoding_utf32_be, 0, 0}, + {"tests/data/utftest_utf32_le_clean.xml", encoding_utf32_le, 0, 0}, + {"tests/data/utftest_utf8_clean.xml", encoding_utf8, 0, 0}, + {"tests/data/latintest_latin1.xml", encoding_latin1, 0, 0} + }; + + // load files in memory + for (unsigned int i = 0; i < sizeof(files) / sizeof(files[0]); ++i) + { + CHECK(load_file_in_memory(files[i].path, files[i].data, files[i].size)); + } + + // disallow allocations + test_runner::_memory_fail_threshold = 1; + + 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); + } + + // cleanup + for (unsigned int j = 0; j < sizeof(files) / sizeof(files[0]); ++j) + { + delete[] files[j].data; + } +} diff --git a/tests/test_dom_modify.cpp b/tests/test_dom_modify.cpp index 612b017..5f4e26c 100644 --- a/tests/test_dom_modify.cpp +++ b/tests/test_dom_modify.cpp @@ -1354,3 +1354,38 @@ TEST_XML(dom_node_copy_out_of_memory, "text") +{ + xml_node node = doc.child(STR("node")); + + xml_attribute attr = node.attribute(STR("attr")); + attr.set_name(STR("longattr")); + attr.set_value(STR("longvalue")); + + node.set_name(STR("longnode")); + node.text().set(STR("longtext")); + + node.remove_attribute(attr); + doc.remove_child(node); + + CHECK_NODE(doc, STR("")); +} + +TEST_XML(dom_node_set_deallocate, "text") +{ + xml_node node = doc.child(STR("node")); + + xml_attribute attr = node.attribute(STR("attr")); + + attr.set_name(STR("longattr")); + attr.set_value(STR("longvalue")); + node.set_name(STR("longnode")); + + attr.set_name(STR("")); + attr.set_value(STR("")); + node.set_name(STR("")); + node.text().set(STR("")); + + CHECK_NODE(doc, STR("<:anonymous :anonymous=\"\">")); +} diff --git a/tests/test_parse.cpp b/tests/test_parse.cpp index 2094ef9..444a0fb 100644 --- a/tests/test_parse.cpp +++ b/tests/test_parse.cpp @@ -876,7 +876,7 @@ TEST(parse_out_of_memory) CHECK(!doc.first_child()); } -TEST(parse_out_of_memory_halfway) +TEST(parse_out_of_memory_halfway_node) { const unsigned int count = 10000; static char_t text[count * 4]; @@ -896,6 +896,35 @@ TEST(parse_out_of_memory_halfway) CHECK_NODE(doc.first_child(), STR("")); } +TEST(parse_out_of_memory_halfway_attr) +{ + const unsigned int count = 10000; + static char_t text[count * 5 + 4]; + + text[0] = '<'; + text[1] = 'n'; + + for (unsigned int i = 0; i < count; ++i) + { + text[5*i + 2] = ' '; + text[5*i + 3] = 'a'; + text[5*i + 4] = '='; + text[5*i + 5] = '"'; + text[5*i + 6] = '"'; + } + + text[5 * count + 2] = '/'; + text[5 * count + 3] = '>'; + + test_runner::_memory_fail_threshold = 65536; + + xml_document doc; + CHECK(doc.load_buffer_inplace(text, count * 5 + 4).status == status_out_of_memory); + CHECK_STRING(doc.first_child().name(), STR("n")); + CHECK_STRING(doc.first_child().first_attribute().name(), STR("a")); + CHECK_STRING(doc.first_child().last_attribute().name(), STR("a")); +} + static bool test_offset(const char_t* contents, unsigned int options, pugi::xml_parse_status status, ptrdiff_t offset) { xml_document doc; diff --git a/tests/test_xpath_paths.cpp b/tests/test_xpath_paths.cpp index da8811d..ee2401a 100644 --- a/tests/test_xpath_paths.cpp +++ b/tests/test_xpath_paths.cpp @@ -615,6 +615,9 @@ TEST_XML(xpath_paths_optimize_step_once, "

") -- cgit v1.2.3