From c40fd364ce856405f86fcc67250d36480e3712b8 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Thu, 15 Jun 2017 07:17:26 -0700 Subject: tests: Add tests for loading special files New tests try to load a folder as an XML document, and a device. Both are intended to exercise some otherwise non-hittable error paths in load_file implementation. --- tests/main.cpp | 2 +- tests/test_document.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/main.cpp b/tests/main.cpp index 712edda..352b58b 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -41,7 +41,7 @@ static void* custom_allocate(size_t size) else { void* ptr = memory_allocate(size); - assert(ptr); + if (!ptr) return 0; g_memory_total_size += memory_size(ptr); g_memory_total_count++; diff --git a/tests/test_document.cpp b/tests/test_document.cpp index 9860737..c7ceb8f 100644 --- a/tests/test_document.cpp +++ b/tests/test_document.cpp @@ -383,6 +383,23 @@ TEST(document_load_file_wide_out_of_memory) CHECK(result.status == status_out_of_memory || result.status == status_file_not_found); } +#if defined(__linux__) || defined(__APPLE__) +TEST(document_load_file_special_folder) +{ + xml_document doc; + xml_parse_result result = doc.load_file("."); + // status_out_of_memory is somewhat counter-intuitive but on Linux ftell returns LONG_MAX for directories + CHECK(result.status == status_file_not_found || result.status == status_io_error || result.status == status_out_of_memory); +} + +TEST(document_load_file_special_device) +{ + xml_document doc; + xml_parse_result result = doc.load_file("/dev/tty"); + CHECK(result.status == status_file_not_found || result.status == status_io_error); +} +#endif + TEST_XML(document_save, "") { xml_writer_string writer; -- cgit v1.2.3 From b3b44841f02ea800f7f237f87c33081bb7032b09 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Thu, 15 Jun 2017 09:26:23 -0700 Subject: Mark all assert(false) statements as unreachable Now we can exclude these from code coverage since it's logically impossible to hit them in tests. --- src/pugixml.cpp | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 56d7c75..9264309 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -353,7 +353,7 @@ PUGI__NS_BEGIN bucket = (bucket + probe + 1) & hashmod; } - assert(false && "Hash table is full"); + assert(false && "Hash table is full"); // unreachable return 0; } @@ -2144,7 +2144,7 @@ PUGI__NS_BEGIN if (encoding == encoding_latin1) return convert_buffer_generic(out_buffer, out_length, contents, size, latin1_decoder()); - assert(false && "Invalid encoding"); + assert(false && "Invalid encoding"); // unreachable return false; } #else @@ -2249,7 +2249,7 @@ PUGI__NS_BEGIN if (encoding == encoding_latin1) return convert_buffer_latin1(out_buffer, out_length, contents, size, is_mutable); - assert(false && "Invalid encoding"); + assert(false && "Invalid encoding"); // unreachable return false; } #endif @@ -2696,7 +2696,7 @@ PUGI__NS_BEGIN case 5: return strconv_pcdata_impl::parse; case 6: return strconv_pcdata_impl::parse; case 7: return strconv_pcdata_impl::parse; - default: assert(false); return 0; // should not get here + default: assert(false); return 0; // unreachable } } @@ -2873,7 +2873,7 @@ PUGI__NS_BEGIN case 13: return strconv_attribute_impl::parse_wnorm; case 14: return strconv_attribute_impl::parse_wnorm; case 15: return strconv_attribute_impl::parse_wnorm; - default: assert(false); return 0; // should not get here + default: assert(false); return 0; // unreachable } } @@ -3622,7 +3622,7 @@ PUGI__NS_BEGIN if (encoding == encoding_latin1) return convert_buffer_output_generic(r_u8, data, length, wchar_decoder(), latin1_writer()); - assert(false && "Invalid encoding"); + assert(false && "Invalid encoding"); // unreachable return 0; } #else @@ -3661,7 +3661,7 @@ PUGI__NS_BEGIN if (encoding == encoding_latin1) return convert_buffer_output_generic(r_u8, data, length, utf8_decoder(), latin1_writer()); - assert(false && "Invalid encoding"); + assert(false && "Invalid encoding"); // unreachable return 0; } #endif @@ -4188,7 +4188,7 @@ PUGI__NS_BEGIN break; default: - assert(false && "Invalid node type"); + assert(false && "Invalid node type"); // unreachable } } @@ -6290,7 +6290,7 @@ namespace pugi return _root->value && (_root->header & impl::xml_memory_page_value_allocated_or_shared_mask) == 0 ? _root->value - doc.buffer : -1; default: - assert(false && "Invalid node type"); + assert(false && "Invalid node type"); // unreachable return -1; } } @@ -8498,7 +8498,7 @@ PUGI__NS_BEGIN break; default: - assert(false && "Invalid variable type"); + assert(false && "Invalid variable type"); // unreachable } } @@ -8519,7 +8519,7 @@ PUGI__NS_BEGIN return lhs->set(static_cast(rhs)->value); default: - assert(false && "Invalid variable type"); + assert(false && "Invalid variable type"); // unreachable return false; } } @@ -8606,7 +8606,7 @@ PUGI__NS_BEGIN return *min_element(begin, end, document_order_comparator()); default: - assert(false && "Invalid node set type"); + assert(false && "Invalid node set type"); // unreachable return xpath_node(); } } @@ -9336,7 +9336,7 @@ PUGI__NS_BEGIN } } - assert(false && "Wrong types"); + assert(false && "Wrong types"); // unreachable return false; } @@ -9411,7 +9411,7 @@ PUGI__NS_BEGIN } else { - assert(false && "Wrong types"); + assert(false && "Wrong types"); // unreachable return false; } } @@ -9629,7 +9629,7 @@ PUGI__NS_BEGIN break; default: - assert(false && "Unknown axis"); + assert(false && "Unknown axis"); // unreachable } return false; @@ -9824,7 +9824,7 @@ PUGI__NS_BEGIN } default: - assert(false && "Unimplemented axis"); + assert(false && "Unimplemented axis"); // unreachable } } @@ -9905,7 +9905,7 @@ PUGI__NS_BEGIN } default: - assert(false && "Unimplemented axis"); + assert(false && "Unimplemented axis"); // unreachable } } @@ -10146,7 +10146,7 @@ PUGI__NS_BEGIN } default: - assert(false && "Wrong expression for return type boolean"); + assert(false && "Wrong expression for return type boolean"); // unreachable return false; } } @@ -10281,7 +10281,7 @@ PUGI__NS_BEGIN } default: - assert(false && "Wrong expression for return type number"); + assert(false && "Wrong expression for return type number"); // unreachable return 0; } @@ -10571,7 +10571,7 @@ PUGI__NS_BEGIN } default: - assert(false && "Wrong expression for return type string"); + assert(false && "Wrong expression for return type string"); // unreachable return xpath_string(); } } @@ -10662,7 +10662,7 @@ PUGI__NS_BEGIN return step_do(c, stack, eval, axis_to_type()); default: - assert(false && "Unknown axis"); + assert(false && "Unknown axis"); // unreachable return xpath_node_set_raw(); } } @@ -10700,7 +10700,7 @@ PUGI__NS_BEGIN // fallthrough default: - assert(false && "Wrong expression for return type node set"); + assert(false && "Wrong expression for return type node set"); // unreachable return xpath_node_set_raw(); } } @@ -12042,7 +12042,7 @@ namespace pugi return static_cast(this)->name; default: - assert(false && "Invalid variable type"); + assert(false && "Invalid variable type"); // unreachable return 0; } } -- cgit v1.2.3 From 927d321d90a971caae629210e53858f6057903e4 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Thu, 15 Jun 2017 09:28:31 -0700 Subject: Exclude unreachable lines from code coverage codecov.io does not seem to support lcov regex customization; additionally, we can't just replace unreachable with LCOV_LINE_EXCL in gcov file - so we have to patch the ##### indicator (which suggests the line hasn't been hit) with 1. See also https://github.com/codecov/support/issues/144 --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index df5569c..f35124d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,4 +14,6 @@ script: - make test cxxstd=c++11 defines=$DEFINES config=release -j2 - make test cxxstd=c++98 defines=$DEFINES config=debug -j2 -after_success: bash <(curl -s https://codecov.io/bash) -f pugixml.cpp.gcov +after_success: + - sed -e "s/#####\(.*\)\(\/\/ unreachable.*\)/ 1\1\2/" -i pugixml.cpp.gcov + - bash <(curl -s https://codecov.io/bash) -f pugixml.cpp.gcov -- cgit v1.2.3