From 3aa2b40354208fd4171f5e4b2ddddaeb6ec881fb Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Fri, 16 Jun 2017 16:41:08 -0700 Subject: tests: Add more coverage tests for stream loading Cover more failure cases and simplify the streambuf implementation a bit. --- tests/test_document.cpp | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/tests/test_document.cpp b/tests/test_document.cpp index c7ceb8f..81c9014 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -186,11 +186,6 @@ public: { this->setg(begin, begin, end); } - - typename std::basic_streambuf::int_type underflow() PUGIXML_OVERRIDE - { - return this->gptr() == this->egptr() ? std::basic_streambuf::traits_type::eof() : std::basic_streambuf::traits_type::to_int_type(*this->gptr()); - } }; TEST(document_load_stream_nonseekable) @@ -257,6 +252,46 @@ TEST(document_load_stream_nonseekable_out_of_memory_large) pugi::xml_document doc; CHECK_ALLOC_FAIL(CHECK(doc.load(in).status == status_out_of_memory)); } + +TEST(document_load_stream_wide_nonseekable_out_of_memory) +{ + wchar_t contents[] = L""; + char_array_buffer buffer(contents, contents + sizeof(contents) / sizeof(contents[0])); + std::basic_istream in(&buffer); + + test_runner::_memory_fail_threshold = 1; + + pugi::xml_document doc; + CHECK_ALLOC_FAIL(CHECK(doc.load(in).status == status_out_of_memory)); +} + +template class seek_fail_buffer: public std::basic_streambuf +{ +public: + typename std::basic_streambuf::pos_type seekoff(typename std::basic_streambuf::off_type, std::ios_base::seekdir, std::ios_base::openmode) PUGIXML_OVERRIDE + { + // pretend that our buffer is seekable (this is called by tellg); actual seeks will fail + return 0; + } +}; + +TEST(document_load_stream_seekable_fail_seek) +{ + seek_fail_buffer buffer; + std::basic_istream in(&buffer); + + pugi::xml_document doc; + CHECK(doc.load(in).status == status_io_error); +} + +TEST(document_load_stream_wide_seekable_fail_seek) +{ + seek_fail_buffer buffer; + std::basic_istream in(&buffer); + + pugi::xml_document doc; + CHECK(doc.load(in).status == status_io_error); +} #endif TEST(document_load_string) -- cgit v1.2.3