Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1 | /** |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2 | * @file test_tree_schema.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from tress_data.c |
| 5 | * |
| 6 | * Copyright (c) 2018-2019 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
| 15 | #include "utests.h" |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 16 | |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 17 | #include "common.h" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 18 | #include "libyang.h" |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 19 | #include "path.h" |
| 20 | #include "xpath.h" |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 21 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 22 | static int |
| 23 | setup(void **state) |
| 24 | { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 25 | const char *schema = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;" |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 26 | "leaf bar {type string;}" |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 27 | "list l1 { key \"a b\"; leaf a {type string;} leaf b {type string;} leaf c {type string;}}" |
| 28 | "leaf foo { type string;}" |
| 29 | "leaf-list ll { type string;}" |
| 30 | "container c {leaf-list x {type string;}}" |
| 31 | "anydata any {config false;}" |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 32 | "list l2 {config false;" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 33 | " container c{leaf x {type string;} leaf-list d {type string;}}" |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 34 | "}}"; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 35 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 36 | UTEST_SETUP; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 37 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 38 | UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 43 | #define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \ |
| 44 | CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE) |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 45 | |
| 46 | static void |
| 47 | test_compare(void **state) |
| 48 | { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 49 | struct lyd_node *tree1, *tree2; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 50 | const char *data1; |
| 51 | const char *data2; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 52 | |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 53 | assert_int_equal(LY_SUCCESS, lyd_compare_single(NULL, NULL, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 54 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 55 | data1 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 56 | data2 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>y</c></l1>"; |
| 57 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
| 58 | CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 59 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 60 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 61 | assert_int_equal(LY_ENOT, lyd_compare_single(((struct lyd_node_inner *)tree1)->child, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 62 | lyd_free_all(tree1); |
| 63 | lyd_free_all(tree2); |
| 64 | |
| 65 | data1 = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
| 66 | data2 = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 67 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
| 68 | CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 69 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0)); |
| 70 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next->next, tree2->next, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 71 | lyd_free_all(tree1); |
| 72 | lyd_free_all(tree2); |
| 73 | |
| 74 | data1 = "<ll xmlns=\"urn:tests:a\">a</ll><ll xmlns=\"urn:tests:a\">b</ll>"; |
| 75 | data2 = "<ll xmlns=\"urn:tests:a\">b</ll>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 76 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
| 77 | CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 78 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0)); |
| 79 | assert_int_equal(LY_ENOT, lyd_compare_single(NULL, tree2, 0)); |
| 80 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, NULL, 0)); |
| 81 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 82 | lyd_free_all(tree1); |
| 83 | lyd_free_all(tree2); |
| 84 | |
| 85 | data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>"; |
| 86 | data2 = "<c xmlns=\"urn:tests:a\"><x>y</x></c>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 87 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
| 88 | CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 89 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 90 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 91 | lyd_free_all(tree1); |
| 92 | lyd_free_all(tree2); |
| 93 | |
| 94 | data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>"; |
| 95 | data2 = "<c xmlns=\"urn:tests:a\"><x>x</x><x>y</x></c>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 96 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
| 97 | CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 98 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 99 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 100 | lyd_free_all(tree1); |
| 101 | lyd_free_all(tree2); |
| 102 | |
| 103 | data1 = "<any xmlns=\"urn:tests:a\"><x>x</x></any>"; |
| 104 | data2 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 105 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
| 106 | CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 107 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 108 | lyd_free_all(tree1); |
| 109 | data1 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 110 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 111 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 112 | lyd_free_all(tree1); |
| 113 | lyd_free_all(tree2); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 116 | static void |
| 117 | test_dup(void **state) |
| 118 | { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 119 | struct lyd_node *tree1, *tree2; |
| 120 | const char *result; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 121 | const char *data; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 122 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 123 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 124 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 125 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, LYD_DUP_RECURSIVE, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 126 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 127 | lyd_free_all(tree1); |
| 128 | lyd_free_all(tree2); |
| 129 | |
| 130 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 131 | result = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b></l1>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 132 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 133 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 134 | lyd_free_all(tree1); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 135 | CHECK_PARSE_LYD(result, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 136 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 137 | lyd_free_all(tree1); |
| 138 | lyd_free_all(tree2); |
| 139 | |
| 140 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
| 141 | result = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 142 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 143 | assert_int_equal(LY_SUCCESS, lyd_dup_siblings(tree1, NULL, LYD_DUP_RECURSIVE, &tree2)); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 144 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2->next, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 145 | lyd_free_all(tree2); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 146 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, LYD_DUP_RECURSIVE, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 147 | lyd_free_all(tree1); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 148 | CHECK_PARSE_LYD(result, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 149 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 150 | lyd_free_all(tree2); |
| 151 | |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 152 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 153 | lyd_free_all(tree1); |
| 154 | result = "<l2 xmlns=\"urn:tests:a\"/>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 155 | CHECK_PARSE_LYD_PARAM(result, LYD_XML, LYD_PARSE_ONLY, 0, LY_SUCCESS, tree1); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 156 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 157 | lyd_free_all(tree1); |
| 158 | lyd_free_all(tree2); |
| 159 | |
| 160 | data = "<any xmlns=\"urn:tests:a\"><c><a>a</a></c></any>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 161 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 162 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 163 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 164 | lyd_free_all(tree1); |
| 165 | lyd_free_all(tree2); |
| 166 | |
| 167 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 168 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 169 | assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child, NULL, |
| 170 | LYD_DUP_WITH_PARENTS, &tree2)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 171 | int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM; |
| 172 | |
| 173 | CHECK_LYSC_NODE(tree2->schema, NULL, 0, flag, 1, "x", 1, LYS_LEAF, 1, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 174 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, (struct lyd_node *)tree2->parent->parent, |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 175 | LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 176 | lyd_free_all(tree1); |
| 177 | lyd_free_all(tree2); |
| 178 | |
| 179 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 180 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 181 | assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)tree1)->child->prev, NULL, |
| 182 | LYD_DUP_WITH_PARENTS, &tree2)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 183 | flag = LYS_CONFIG_W | LYS_SET_ENUM; |
| 184 | CHECK_LYSC_NODE(tree2->schema, NULL, 0, flag, 1, "c", 0, LYS_LEAF, 1, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 185 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, (struct lyd_node *)tree2->parent, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 186 | lyd_free_all(tree1); |
| 187 | lyd_free_all(tree2); |
| 188 | |
| 189 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 190 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 191 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 192 | assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child, |
| 193 | (struct lyd_node_inner *)tree2, LYD_DUP_WITH_PARENTS, NULL)); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 194 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 195 | lyd_free_all(tree1); |
| 196 | lyd_free_all(tree2); |
| 197 | |
| 198 | /* invalid */ |
| 199 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 200 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 201 | assert_int_equal(LY_EINVAL, lyd_dup_single(((struct lyd_node_inner *)tree1)->child->prev, |
| 202 | (struct lyd_node_inner *)tree1->next, LYD_DUP_WITH_PARENTS, NULL)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 203 | lyd_free_all(tree1); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 206 | static void |
| 207 | test_target(void **state) |
| 208 | { |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 209 | const struct lyd_node_term *term; |
| 210 | struct lyd_node *tree; |
| 211 | struct lyxp_expr *exp; |
| 212 | struct ly_path *path; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 213 | const char *path_str = "/a:l2[2]/c/d[3]"; |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 214 | const char *data = |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 215 | "<l2 xmlns=\"urn:tests:a\"><c>" |
| 216 | " <d>a</d>" |
| 217 | " </c></l2>" |
| 218 | "<l2 xmlns=\"urn:tests:a\"><c>" |
| 219 | " <d>a</d>" |
| 220 | " <d>b</d>" |
| 221 | " <d>b</d>" |
| 222 | " <d>c</d>" |
| 223 | "</c></l2>" |
| 224 | "<l2 xmlns=\"urn:tests:a\"><c>" |
| 225 | "</c></l2>"; |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 226 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 227 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 228 | assert_int_equal(LY_SUCCESS, ly_path_parse(UTEST_LYCTX, NULL, path_str, strlen(path_str), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 229 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 230 | assert_int_equal(LY_SUCCESS, ly_path_compile(UTEST_LYCTX, NULL, NULL, exp, LY_PATH_LREF_FALSE, LY_PATH_OPER_INPUT, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 231 | LY_PATH_TARGET_SINGLE, LY_PREF_JSON, NULL, NULL, &path)); |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 232 | term = lyd_target(path, tree); |
| 233 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 234 | const int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM | LYS_ORDBY_SYSTEM; |
| 235 | |
| 236 | CHECK_LYSC_NODE(term->schema, NULL, 0, flag, 1, "d", 0, LYS_LEAFLIST, 1, 0, NULL, 0); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 237 | assert_string_equal(LYD_CANON_VALUE(term), "b"); |
| 238 | assert_string_equal(LYD_CANON_VALUE(term->prev), "b"); |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 239 | |
| 240 | lyd_free_all(tree); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 241 | ly_path_free(UTEST_LYCTX, path); |
| 242 | lyxp_expr_free(UTEST_LYCTX, exp); |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 245 | static void |
| 246 | test_list_pos(void **state) |
| 247 | { |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 248 | const char *data; |
| 249 | struct lyd_node *tree; |
| 250 | |
| 251 | data = "<bar xmlns=\"urn:tests:a\">test</bar>" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 252 | "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>" |
| 253 | "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>" |
| 254 | "<foo xmlns=\"urn:tests:a\">test</foo>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 255 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 256 | assert_int_equal(0, lyd_list_pos(tree)); |
| 257 | assert_int_equal(1, lyd_list_pos(tree->next)); |
| 258 | assert_int_equal(2, lyd_list_pos(tree->next->next)); |
| 259 | assert_int_equal(0, lyd_list_pos(tree->next->next->next)); |
| 260 | lyd_free_all(tree); |
| 261 | |
| 262 | data = "<ll xmlns=\"urn:tests:a\">one</ll>" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 263 | "<ll xmlns=\"urn:tests:a\">two</ll>" |
| 264 | "<ll xmlns=\"urn:tests:a\">three</ll>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 265 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 266 | assert_int_equal(1, lyd_list_pos(tree)); |
| 267 | assert_int_equal(2, lyd_list_pos(tree->next)); |
| 268 | assert_int_equal(3, lyd_list_pos(tree->next->next)); |
| 269 | lyd_free_all(tree); |
| 270 | |
| 271 | data = "<ll xmlns=\"urn:tests:a\">one</ll>" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 272 | "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>" |
| 273 | "<ll xmlns=\"urn:tests:a\">two</ll>" |
| 274 | "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>" |
| 275 | "<ll xmlns=\"urn:tests:a\">three</ll>" |
| 276 | "<l1 xmlns=\"urn:tests:a\"><a>three</a><b>three</b></l1>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 277 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 278 | assert_string_equal("l1", tree->schema->name); |
| 279 | assert_int_equal(1, lyd_list_pos(tree)); |
| 280 | assert_int_equal(2, lyd_list_pos(tree->next)); |
| 281 | assert_int_equal(3, lyd_list_pos(tree->next->next)); |
| 282 | assert_string_equal("ll", tree->next->next->next->schema->name); |
| 283 | assert_int_equal(1, lyd_list_pos(tree->next->next->next)); |
| 284 | assert_int_equal(2, lyd_list_pos(tree->next->next->next->next)); |
| 285 | assert_int_equal(3, lyd_list_pos(tree->next->next->next->next->next)); |
| 286 | lyd_free_all(tree); |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 289 | static void |
| 290 | test_first_sibling(void **state) |
| 291 | { |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 292 | const char *data; |
| 293 | struct lyd_node *tree; |
| 294 | struct lyd_node_inner *parent; |
| 295 | |
| 296 | data = "<bar xmlns=\"urn:tests:a\">test</bar>" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 297 | "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>one</c></l1>" |
| 298 | "<foo xmlns=\"urn:tests:a\">test</foo>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 299 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 300 | assert_ptr_equal(tree, lyd_first_sibling(tree->next)); |
| 301 | assert_ptr_equal(tree, lyd_first_sibling(tree)); |
| 302 | assert_ptr_equal(tree, lyd_first_sibling(tree->prev)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 303 | parent = (struct lyd_node_inner *)tree->next; |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 304 | assert_int_equal(LYS_LIST, parent->schema->nodetype); |
| 305 | assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->next)); |
| 306 | assert_ptr_equal(parent->child, lyd_first_sibling(parent->child)); |
| 307 | assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->prev)); |
| 308 | lyd_free_all(tree); |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 311 | int |
| 312 | main(void) |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 313 | { |
| 314 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 315 | UTEST(test_compare, setup), |
| 316 | UTEST(test_dup, setup), |
| 317 | UTEST(test_target, setup), |
| 318 | UTEST(test_list_pos, setup), |
| 319 | UTEST(test_first_sibling, setup), |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 323 | } |