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 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'tests/test_document.cpp') 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; + } +} -- cgit v1.2.3 From 903db8682a5f14b52adec996584c70ea072619ea Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Thu, 23 Oct 2014 07:41:07 +0000 Subject: tests: Add more tests for better coverage More tests for out-of-memory and other edge conditions git-svn-id: https://pugixml.googlecode.com/svn/trunk@1075 99668b35-9821-0410-8761-19e4c4f06640 --- tests/test_document.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'tests/test_document.cpp') diff --git a/tests/test_document.cpp b/tests/test_document.cpp index c76f671..2cc39a6 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -227,6 +227,34 @@ TEST(document_load_stream_nonseekable_large) CHECK(doc.load(in)); CHECK_NODE(doc, str.c_str()); } + +TEST(document_load_stream_nonseekable_out_of_memory) +{ + char contents[] = ""; + char_array_buffer buffer(contents, contents + sizeof(contents) / sizeof(contents[0])); + std::istream in(&buffer); + + test_runner::_memory_fail_threshold = 1; + + pugi::xml_document doc; + CHECK(doc.load(in).status == status_out_of_memory); +} + +TEST(document_load_stream_nonseekable_out_of_memory_large) +{ + std::basic_string str; + str += STR(""); + for (int i = 0; i < 10000; ++i) str += STR(""); + str += STR(""); + + char_array_buffer buffer(&str[0], &str[0] + str.length()); + std::basic_istream in(&buffer); + + test_runner::_memory_fail_threshold = 10000 * 8 * 3 / 2; + + pugi::xml_document doc; + CHECK(doc.load(in).status == status_out_of_memory); +} #endif TEST(document_load_string) @@ -295,6 +323,17 @@ TEST(document_load_file_wide_ascii) CHECK_NODE(doc, STR("")); } +TEST(document_load_file_wide_out_of_memory) +{ + test_runner::_memory_fail_threshold = 1; + + pugi::xml_document doc; + + pugi::xml_parse_result result = doc.load_file(L"tests/data/small.xml"); + + CHECK(result.status == status_out_of_memory || result.status == status_file_not_found); +} + TEST_XML(document_save, "") { xml_writer_string writer; -- cgit v1.2.3