From 5b86a8f6124749bdb618ebd459e6642ac24efe14 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sun, 20 Sep 2015 10:48:30 -0700 Subject: tests: Add tests for integer overflow during conversion These tests are only testing attribute as_int in hopes that xml_text uses the same underlying implementation (which it does). --- tests/test_dom_traverse.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/tests/test_dom_traverse.cpp b/tests/test_dom_traverse.cpp index 7215c3e..1fbc317 100644 --- a/tests/test_dom_traverse.cpp +++ b/tests/test_dom_traverse.cpp @@ -1151,3 +1151,77 @@ TEST_XML(dom_node_attribute_hinted, "") CHECK(!node.attribute(STR("attr"), hint) && hint == attr2); } + +TEST_XML(dom_as_int_overflow, "") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.attribute(STR("attr1")).as_int() == -2147483647 - 1); + CHECK(node.attribute(STR("attr2")).as_int() == 2147483647); + CHECK(node.attribute(STR("attr3")).as_int() == -2147483647 - 1); +} + +TEST_XML(dom_as_uint_overflow, "") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.attribute(STR("attr1")).as_uint() == 0); + CHECK(node.attribute(STR("attr2")).as_uint() == 4294967295); + CHECK(node.attribute(STR("attr3")).as_uint() == 4294967295); + CHECK(node.attribute(STR("attr4")).as_uint() == 4294967295); +} + +TEST_XML(dom_as_int_hex_overflow, "") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.attribute(STR("attr1")).as_int() == -2147483647 - 1); + CHECK(node.attribute(STR("attr2")).as_int() == 2147483647); +} + +TEST_XML(dom_as_uint_hex_overflow, "") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.attribute(STR("attr1")).as_uint() == 0); + CHECK(node.attribute(STR("attr2")).as_uint() == 4294967295); + CHECK(node.attribute(STR("attr3")).as_uint() == 4294967295); +} + +#ifdef PUGIXML_HAS_LONG_LONG +TEST_XML(dom_as_llong_overflow, "") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.attribute(STR("attr1")).as_llong() == -9223372036854775807ll - 1); + CHECK(node.attribute(STR("attr2")).as_llong() == 9223372036854775807ll); + CHECK(node.attribute(STR("attr3")).as_llong() == -9223372036854775807ll - 1); +} + +TEST_XML(dom_as_ullong_overflow, "") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.attribute(STR("attr1")).as_ullong() == 0); + CHECK(node.attribute(STR("attr2")).as_ullong() == 18446744073709551615ull); + CHECK(node.attribute(STR("attr3")).as_ullong() == 18446744073709551615ull); + CHECK(node.attribute(STR("attr4")).as_ullong() == 18446744073709551615ull); +} + +TEST_XML(dom_as_llong_hex_overflow, "") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.attribute(STR("attr1")).as_llong() == -9223372036854775807ll - 1); + CHECK(node.attribute(STR("attr2")).as_llong() == 9223372036854775807ll); +} + +TEST_XML(dom_as_ullong_hex_overflow, "") +{ + xml_node node = doc.child(STR("node")); + + CHECK(node.attribute(STR("attr1")).as_ullong() == 0); + CHECK(node.attribute(STR("attr2")).as_ullong() == 18446744073709551615ull); + CHECK(node.attribute(STR("attr3")).as_ullong() == 18446744073709551615ull); +} +#endif \ No newline at end of file -- cgit v1.2.3