diff options
| -rw-r--r-- | tests/test_document.cpp | 39 | ||||
| -rw-r--r-- | tests/test_dom_modify.cpp | 17 | ||||
| -rw-r--r-- | tests/test_parse.cpp | 22 | ||||
| -rw-r--r-- | tests/test_parse_doctype.cpp | 9 | ||||
| -rw-r--r-- | tests/test_xpath.cpp | 23 | ||||
| -rw-r--r-- | tests/test_xpath_api.cpp | 5 | ||||
| -rw-r--r-- | tests/test_xpath_functions.cpp | 15 | ||||
| -rw-r--r-- | tests/test_xpath_variables.cpp | 3 | 
8 files changed, 131 insertions, 2 deletions
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[] = "<node />"; +    char_array_buffer<char> 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<pugi::char_t> str; +	str += STR("<node>"); +	for (int i = 0; i < 10000; ++i) str += STR("<node />"); +	str += STR("</node>"); + +    char_array_buffer<pugi::char_t> buffer(&str[0], &str[0] + str.length()); +    std::basic_istream<pugi::char_t> 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("<node />"));  } +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, "<node/>")  {  	xml_writer_string writer; diff --git a/tests/test_dom_modify.cpp b/tests/test_dom_modify.cpp index 5f4e26c..01b562a 100644 --- a/tests/test_dom_modify.cpp +++ b/tests/test_dom_modify.cpp @@ -27,6 +27,16 @@ TEST_XML(dom_attr_assign, "<node/>")  	CHECK_NODE(node, STR("<node attr1=\"v1\" attr2=\"-2147483647\" attr3=\"-2147483648\" attr4=\"4294967295\" attr5=\"4294967294\" attr6=\"0.5\" attr7=\"true\" />"));  } +TEST_XML(dom_attr_set_name, "<node attr='value' />") +{ +	xml_attribute attr = doc.child(STR("node")).attribute(STR("attr")); + +	CHECK(attr.set_name(STR("n"))); +	CHECK(!xml_attribute().set_name(STR("n"))); + +	CHECK_NODE(doc, STR("<node n=\"value\" />")); +} +  TEST_XML(dom_attr_set_value, "<node/>")  {  	xml_node node = doc.child(STR("node")); @@ -758,8 +768,9 @@ TEST(dom_node_declaration_name)  	doc.insert_child_after(node_declaration, doc.first_child());  	doc.insert_child_before(node_declaration, doc.first_child()); +	doc.prepend_child(node_declaration); -	CHECK_NODE(doc, STR("<?xml?><?xml?><?xml?>")); +	CHECK_NODE(doc, STR("<?xml?><?xml?><?xml?><?xml?>"));  }  TEST(dom_node_declaration_attributes) @@ -867,17 +878,21 @@ TEST(dom_node_out_of_memory)  	// verify all node modification operations  	CHECK(!n.append_child()); +	CHECK(!n.prepend_child());  	CHECK(!n.insert_child_after(node_element, n.first_child()));  	CHECK(!n.insert_child_before(node_element, n.first_child()));  	CHECK(!n.append_attribute(STR(""))); +	CHECK(!n.prepend_attribute(STR("")));  	CHECK(!n.insert_attribute_after(STR(""), a));  	CHECK(!n.insert_attribute_before(STR(""), a));  	// verify node copy operations  	CHECK(!n.append_copy(n.first_child())); +	CHECK(!n.prepend_copy(n.first_child()));  	CHECK(!n.insert_copy_after(n.first_child(), n.first_child()));  	CHECK(!n.insert_copy_before(n.first_child(), n.first_child()));  	CHECK(!n.append_copy(a)); +	CHECK(!n.prepend_copy(a));  	CHECK(!n.insert_copy_after(a, a));  	CHECK(!n.insert_copy_before(a, a));  } diff --git a/tests/test_parse.cpp b/tests/test_parse.cpp index 444a0fb..c45b783 100644 --- a/tests/test_parse.cpp +++ b/tests/test_parse.cpp @@ -1068,3 +1068,25 @@ TEST(parse_pcdata_gap_fragment)  	CHECK(doc.load(STR("a&b"), parse_fragment | parse_escapes));  	CHECK_STRING(doc.text().get(), STR("a&b"));  } + +TEST(parse_name_end_eof) +{ +	char_t test[] = STR("<node>"); + +	xml_document doc; +	CHECK(doc.load_buffer_inplace(test, 6 * sizeof(char_t)).status == status_end_element_mismatch); +	CHECK_STRING(doc.first_child().name(), STR("node")); +} + +TEST(parse_close_tag_eof) +{ +	char_t test1[] = STR("<node></node"); +	char_t test2[] = STR("<node></nodx"); + +	xml_document doc; +	CHECK(doc.load_buffer_inplace(test1, 12 * sizeof(char_t)).status == status_bad_end_element); +	CHECK_STRING(doc.first_child().name(), STR("node")); + +	CHECK(doc.load_buffer_inplace(test2, 12 * sizeof(char_t)).status == status_end_element_mismatch); +	CHECK_STRING(doc.first_child().name(), STR("node")); +} diff --git a/tests/test_parse_doctype.cpp b/tests/test_parse_doctype.cpp index 8976890..f8619fd 100644 --- a/tests/test_parse_doctype.cpp +++ b/tests/test_parse_doctype.cpp @@ -313,3 +313,12 @@ TEST(parse_doctype_error_toplevel)      CHECK(doc.load(STR("<node><!DOCTYPE></node>")).status == status_bad_doctype);      CHECK(doc.load(STR("<node><!DOCTYPE></node>"), parse_doctype).status == status_bad_doctype);  } + +TEST(parse_doctype_error_ignore) +{ +    xml_document doc; +	CHECK(doc.load(STR("<!DOCTYPE root [ <![IGNORE[ ")).status == status_bad_doctype); +	CHECK(doc.load(STR("<!DOCTYPE root [ <![IGNORE[ "), parse_doctype).status == status_bad_doctype); +	CHECK(doc.load(STR("<!DOCTYPE root [ <![IGNORE[ <![INCLUDE[")).status == status_bad_doctype); +	CHECK(doc.load(STR("<!DOCTYPE root [ <![IGNORE[ <![INCLUDE["), parse_doctype).status == status_bad_doctype); +} diff --git a/tests/test_xpath.cpp b/tests/test_xpath.cpp index 2143376..80115b8 100644 --- a/tests/test_xpath.cpp +++ b/tests/test_xpath.cpp @@ -606,4 +606,27 @@ TEST(xpath_sort_crossdoc_different_depth)  	CHECK(ns.size() == 2);  	CHECK((ns[0] == ns1[0] && ns[1] == ns2[0]) || (ns[0] == ns2[0] && ns[1] == ns1[0]));  } + +TEST(xpath_allocate_string_out_of_memory) +{ +	std::basic_string<char_t> query; + +	for (int i = 0; i < 1024; ++i) query += STR("abcdefgh"); + +	test_runner::_memory_fail_threshold = 8*1024; + +#ifdef PUGIXML_NO_EXCEPTIONS +	CHECK(!xpath_query(query.c_str())); +#else +	try +	{ +		xpath_query q(query.c_str()); + +		CHECK_FORCE_FAIL("Expected out of memory exception"); +	} +	catch (const std::bad_alloc&) +	{ +	} +#endif +}  #endif diff --git a/tests/test_xpath_api.cpp b/tests/test_xpath_api.cpp index 270f6aa..deb3beb 100644 --- a/tests/test_xpath_api.cpp +++ b/tests/test_xpath_api.cpp @@ -128,6 +128,11 @@ TEST_XML(xpath_api_nodeset_copy, "<node><foo/><foo/></node>")  	copy4 = copy1;  	CHECK(copy4.size() == 2);  	CHECK_STRING(copy4[0].node().name(), STR("foo")); + +	xpath_node_set copy5; +	copy5 = set; +	copy5 = xpath_node_set(); +	CHECK(copy5.size() == 0);  }  TEST(xpath_api_nodeset_copy_empty) diff --git a/tests/test_xpath_functions.cpp b/tests/test_xpath_functions.cpp index da820ef..678bc2e 100644 --- a/tests/test_xpath_functions.cpp +++ b/tests/test_xpath_functions.cpp @@ -216,7 +216,7 @@ TEST(xpath_boolean_false)  	CHECK_XPATH_FAIL(STR("false(1)"));  } -TEST_XML(xpath_boolean_lang, "<node xml:lang='en'><child xml:lang='zh-UK'><subchild/></child></node><foo><bar/></foo>") +TEST_XML(xpath_boolean_lang, "<node xml:lang='en'><child xml:lang='zh-UK'><subchild attr=''/></child></node><foo><bar/></foo>")  {  	xml_node c; @@ -244,6 +244,9 @@ TEST_XML(xpath_boolean_lang, "<node xml:lang='en'><child xml:lang='zh-UK'><subch  	CHECK_XPATH_BOOLEAN(doc.child(STR("node")).child(STR("child")), STR("lang('r')"), false);  	CHECK_XPATH_BOOLEAN(doc.child(STR("node")).child(STR("child")).child(STR("subchild")), STR("lang('en')"), false); +	// lang with 1 attribute argument +	CHECK_XPATH_NODESET(doc, STR("//@*[lang('en')]")); +  	// lang with 2 arguments  	CHECK_XPATH_FAIL(STR("lang(1, 2)"));  } @@ -773,6 +776,16 @@ TEST_XML_FLAGS(xpath_string_value, "<node><c1>pcdata</c1><c2><child/></c2><c3 at  	CHECK_XPATH_STRING(n, STR("string(c6/node())"), STR("cdata"));  } +TEST(xpath_string_value_empty) +{ +	xml_document doc; +	doc.append_child(node_pcdata).set_value(STR("head")); +	doc.append_child(node_pcdata); +	doc.append_child(node_pcdata).set_value(STR("tail")); + +	CHECK_XPATH_STRING(doc, STR("string()"), STR("headtail")); +} +  TEST_XML(xpath_string_concat_translate, "<node>foobar</node>")  {  	CHECK_XPATH_STRING(doc, STR("concat('a', 'b', 'c', translate(node, 'o', 'a'), 'd')"), STR("abcfaabard")); diff --git a/tests/test_xpath_variables.cpp b/tests/test_xpath_variables.cpp index 70bb4ea..39a4f05 100644 --- a/tests/test_xpath_variables.cpp +++ b/tests/test_xpath_variables.cpp @@ -88,6 +88,9 @@ TEST(xpath_variables_type_string)  	CHECK_DOUBLE_NAN(var->get_number());
  	CHECK_STRING(var->get_string(), STR("abc"));
  	CHECK(var->get_node_set().empty());
 +
 +	CHECK(var->set(STR("abcdef")));
 +	CHECK_STRING(var->get_string(), STR("abcdef"));
  }
  TEST_XML(xpath_variables_type_node_set, "<node/>")
  | 
