xml BUGFIX handling namespaces in parent/child elements of the same name
correctly remove active namespaces (via lyxml_ns_rm()) in case the
parent element holding the namespace definition has the same name
(stored as the same memory chunk) as its child.
diff --git a/tests/src/test_xml.c b/tests/src/test_xml.c
index 587fe67..0aacf29 100644
--- a/tests/src/test_xml.c
+++ b/tests/src/test_xml.c
@@ -405,11 +405,17 @@
e1 = "element1";
e2 = "element2";
+ /* simulate adding open element1 into context */
+ ctx.elements.count++;
+ /* processing namespace definitions */
assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e1, NULL, 0, strdup("urn:default")));
assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e1, "nc", 2, strdup("urn:nc1")));
+ /* simulate adding open element2 into context */
+ ctx.elements.count++;
+ /* processing namespace definitions */
assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e2, "nc", 2, strdup("urn:nc2")));
- assert_int_equal(3, (&ctx)->ns.count);
- assert_int_not_equal(0, (&ctx)->ns.size);
+ assert_int_equal(3, ctx.ns.count);
+ assert_int_not_equal(0, ctx.ns.size);
ns = lyxml_ns_get(&ctx, NULL, 0);
assert_non_null(ns);
@@ -421,21 +427,57 @@
assert_string_equal("nc", ns->prefix);
assert_string_equal("urn:nc2", ns->uri);
+ /* simulate closing element2 */
+ ctx.elements.count--;
assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e2));
- assert_int_equal(2, (&ctx)->ns.count);
+ assert_int_equal(2, ctx.ns.count);
ns = lyxml_ns_get(&ctx, "nc", 2);
assert_non_null(ns);
assert_string_equal("nc", ns->prefix);
assert_string_equal("urn:nc1", ns->uri);
+ /* simulate closing element1 */
+ ctx.elements.count--;
assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e1));
- assert_int_equal(0, (&ctx)->ns.count);
+ assert_int_equal(0, ctx.ns.count);
assert_null(lyxml_ns_get(&ctx, "nc", 2));
assert_null(lyxml_ns_get(&ctx, NULL, 0));
}
+static void
+test_ns2(void **state)
+{
+ (void) state; /* unused */
+
+ const char *e1, *e2;
+
+ struct lyxml_context ctx;
+ memset(&ctx, 0, sizeof ctx);
+ ctx.line = 1;
+
+ /* parent element has the same name as its child (and for some reason both names are stored at the same place) */
+ e1 = e2 = "element1";
+ /* simulate adding open element1 into context */
+ ctx.elements.count++;
+ /* default namespace defined in parent element1 */
+ assert_int_equal(LY_SUCCESS, lyxml_ns_add(&ctx, e1, NULL, 0, strdup("urn:default")));
+ assert_int_equal(1, ctx.ns.count);
+ /* going into child element1 */
+ e1 = e2;
+ /* simulate adding open element1 into context */
+ ctx.elements.count++;
+ /* no namespace defined, going out (first, simulate closing of so far open element) */
+ ctx.elements.count--;
+ assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e2));
+ assert_int_equal(1, ctx.ns.count);
+ /* nothing else, going out of the parent element1 (first, simulate closing of so far open element) */
+ ctx.elements.count--;
+ assert_int_equal(LY_SUCCESS, lyxml_ns_rm(&ctx, e1));
+ assert_int_equal(0, ctx.ns.count);
+}
+
int main(void)
{
const struct CMUnitTest tests[] = {
@@ -443,6 +485,7 @@
cmocka_unit_test_setup(test_attribute, logger_setup),
cmocka_unit_test_setup(test_text, logger_setup),
cmocka_unit_test_setup(test_ns, logger_setup),
+ cmocka_unit_test_setup(test_ns2, logger_setup),
};
return cmocka_run_group_tests(tests, NULL, NULL);