Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1 | /** |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file test_tree_data.c |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 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 | { |
aPiecek | 2425220 | 2021-03-30 12:23:32 +0200 | [diff] [blame] | 25 | const char *schema1 = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;" |
| 26 | "revision 2014-05-08;" |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 27 | "leaf bar {type string;}" |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 28 | "list l1 { key \"a b\"; leaf a {type string;} leaf b {type string;} leaf c {type string;}}" |
| 29 | "leaf foo { type string;}" |
| 30 | "leaf-list ll { type string;}" |
| 31 | "container c {leaf-list x {type string;}}" |
| 32 | "anydata any {config false;}" |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 33 | "list l2 {config false;" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 34 | " container c{leaf x {type string;} leaf-list d {type string;}}" |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 35 | "}}"; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 36 | |
aPiecek | 2425220 | 2021-03-30 12:23:32 +0200 | [diff] [blame] | 37 | const char *schema2 = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;" |
| 38 | "revision 2014-05-08;" |
| 39 | "list l2 {config false;" |
| 40 | " container c{leaf x {type string;}}}" |
| 41 | "anydata any {config false;}" |
| 42 | "}"; |
| 43 | |
Michal Vasko | 6b669ff | 2021-04-30 16:25:36 +0200 | [diff] [blame] | 44 | const char *schema3 = "module c {yang-version 1.1; namespace \"http://example.com/main\";prefix m;" |
| 45 | "import \"ietf-inet-types\" {prefix inet;}" |
| 46 | "typedef optional-ip-address {type union {" |
| 47 | " type inet:ip-address;" |
Michal Vasko | 4fd9192 | 2021-09-15 08:51:06 +0200 | [diff] [blame] | 48 | " type string;" |
Michal Vasko | 6b669ff | 2021-04-30 16:25:36 +0200 | [diff] [blame] | 49 | "}}" |
| 50 | "container cont {" |
| 51 | " list nexthop {min-elements 1; key \"gateway\";" |
| 52 | " leaf gateway {type optional-ip-address;}" |
| 53 | " }" |
Michal Vasko | 15dc9fa | 2021-05-03 14:33:05 +0200 | [diff] [blame] | 54 | " leaf-list pref {type inet:ipv6-prefix;}" |
Michal Vasko | 6b669ff | 2021-04-30 16:25:36 +0200 | [diff] [blame] | 55 | "}}"; |
| 56 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 57 | UTEST_SETUP; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 58 | |
aPiecek | 2425220 | 2021-03-30 12:23:32 +0200 | [diff] [blame] | 59 | UTEST_ADD_MODULE(schema1, LYS_IN_YANG, NULL, NULL); |
| 60 | UTEST_ADD_MODULE(schema2, LYS_IN_YANG, NULL, NULL); |
Michal Vasko | 6b669ff | 2021-04-30 16:25:36 +0200 | [diff] [blame] | 61 | UTEST_ADD_MODULE(schema3, LYS_IN_YANG, NULL, NULL); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 66 | #define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \ |
| 67 | 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] | 68 | |
aPiecek | 2425220 | 2021-03-30 12:23:32 +0200 | [diff] [blame] | 69 | #define CHECK_PARSE_LYD_PARAM_CTX(CTX, INPUT, PARSE_OPTION, OUT_NODE) \ |
| 70 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(CTX, INPUT, LYD_XML, PARSE_OPTION, LYD_VALIDATE_PRESENT, &OUT_NODE)); \ |
| 71 | assert_non_null(OUT_NODE); |
| 72 | |
| 73 | #define RECREATE_CTX_WITH_MODULE(CTX, MODULE) \ |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 74 | ly_ctx_destroy(CTX); \ |
aPiecek | 2425220 | 2021-03-30 12:23:32 +0200 | [diff] [blame] | 75 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &CTX)); \ |
| 76 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(MODULE, &_UC->in)); \ |
| 77 | assert_int_equal(LY_SUCCESS, lys_parse(CTX, _UC->in, LYS_IN_YANG, NULL, NULL)); \ |
| 78 | ly_in_free(_UC->in, 0); |
| 79 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 80 | static void |
| 81 | test_compare(void **state) |
| 82 | { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 83 | struct lyd_node *tree1, *tree2; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 84 | const char *data1; |
| 85 | const char *data2; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 86 | |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 87 | assert_int_equal(LY_SUCCESS, lyd_compare_single(NULL, NULL, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 88 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 89 | data1 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 90 | data2 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>y</c></l1>"; |
| 91 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
| 92 | CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 93 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 94 | 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] | 95 | 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] | 96 | lyd_free_all(tree1); |
| 97 | lyd_free_all(tree2); |
| 98 | |
| 99 | data1 = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
| 100 | 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] | 101 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
| 102 | CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 103 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0)); |
| 104 | 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] | 105 | lyd_free_all(tree1); |
| 106 | lyd_free_all(tree2); |
| 107 | |
| 108 | data1 = "<ll xmlns=\"urn:tests:a\">a</ll><ll xmlns=\"urn:tests:a\">b</ll>"; |
| 109 | data2 = "<ll xmlns=\"urn:tests:a\">b</ll>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 110 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
| 111 | CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 112 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0)); |
| 113 | assert_int_equal(LY_ENOT, lyd_compare_single(NULL, tree2, 0)); |
| 114 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, NULL, 0)); |
| 115 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 116 | lyd_free_all(tree1); |
| 117 | lyd_free_all(tree2); |
| 118 | |
| 119 | data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>"; |
| 120 | data2 = "<c xmlns=\"urn:tests:a\"><x>y</x></c>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 121 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
| 122 | CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 123 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 124 | 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] | 125 | lyd_free_all(tree1); |
| 126 | lyd_free_all(tree2); |
| 127 | |
| 128 | data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>"; |
| 129 | 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] | 130 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
| 131 | CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 132 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 133 | 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] | 134 | lyd_free_all(tree1); |
| 135 | lyd_free_all(tree2); |
| 136 | |
| 137 | data1 = "<any xmlns=\"urn:tests:a\"><x>x</x></any>"; |
| 138 | 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] | 139 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
| 140 | CHECK_PARSE_LYD(data2, 0, LYD_VALIDATE_PRESENT, tree2); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 141 | 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] | 142 | lyd_free_all(tree1); |
| 143 | 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] | 144 | CHECK_PARSE_LYD(data1, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 145 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 146 | lyd_free_all(tree1); |
| 147 | lyd_free_all(tree2); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 150 | static void |
aPiecek | 2425220 | 2021-03-30 12:23:32 +0200 | [diff] [blame] | 151 | test_compare_diff_ctx(void **state) |
| 152 | { |
| 153 | struct lyd_node *tree1, *tree2; |
| 154 | const char *data1, *data2; |
| 155 | struct ly_ctx *ctx2 = NULL; |
| 156 | const char *module; |
| 157 | |
| 158 | /* create second context with the same schema */ |
| 159 | module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;" |
| 160 | "revision 2014-05-08;" |
| 161 | "list l2 {config false;" |
| 162 | " container c{leaf x {type string;}}" |
| 163 | "}}"; |
| 164 | RECREATE_CTX_WITH_MODULE(ctx2, module); |
| 165 | data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>"; |
| 166 | data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>"; |
| 167 | CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1); |
| 168 | CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2); |
| 169 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 170 | lyd_free_all(tree1); |
| 171 | lyd_free_all(tree2); |
| 172 | |
| 173 | /* recreate second context with schema that has a different name */ |
| 174 | module = "module c {namespace urn:tests:c;prefix c;yang-version 1.1;" |
| 175 | "revision 2014-05-08;" |
| 176 | "list l2 {config false;" |
| 177 | " container c{leaf x {type string;}}" |
| 178 | "}}"; |
| 179 | RECREATE_CTX_WITH_MODULE(ctx2, module); |
| 180 | data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>"; |
| 181 | data2 = "<l2 xmlns=\"urn:tests:c\"><c><x>b</x></c></l2>"; |
| 182 | CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1); |
| 183 | CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2); |
| 184 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0)); |
| 185 | lyd_free_all(tree1); |
| 186 | lyd_free_all(tree2); |
| 187 | |
| 188 | /* recreate second context with schema that has a different revision */ |
| 189 | module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;" |
| 190 | "revision 2015-05-08;" |
| 191 | "list l2 {config false;" |
| 192 | " container c{leaf x {type string;}}" |
| 193 | "}}"; |
| 194 | RECREATE_CTX_WITH_MODULE(ctx2, module); |
| 195 | data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>"; |
| 196 | data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>"; |
| 197 | CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1); |
| 198 | CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2); |
Michal Vasko | 5d49f94 | 2023-02-14 10:02:36 +0100 | [diff] [blame] | 199 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
aPiecek | 2425220 | 2021-03-30 12:23:32 +0200 | [diff] [blame] | 200 | lyd_free_all(tree1); |
| 201 | lyd_free_all(tree2); |
| 202 | |
| 203 | /* recreate second context with schema that has no revision */ |
| 204 | module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;" |
| 205 | "list l2 {config false;" |
| 206 | " container c{leaf x {type string;}}" |
| 207 | "}}"; |
| 208 | RECREATE_CTX_WITH_MODULE(ctx2, module); |
| 209 | data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>"; |
| 210 | data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>"; |
| 211 | CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1); |
| 212 | CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2); |
Michal Vasko | 5d49f94 | 2023-02-14 10:02:36 +0100 | [diff] [blame] | 213 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
aPiecek | 2425220 | 2021-03-30 12:23:32 +0200 | [diff] [blame] | 214 | lyd_free_all(tree1); |
| 215 | lyd_free_all(tree2); |
| 216 | |
| 217 | /* recreate second context with schema that has a different parent nodetype */ |
| 218 | module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;" |
| 219 | "revision 2014-05-08;" |
| 220 | "container l2 {config false;" |
| 221 | " container c{leaf x {type string;}}" |
| 222 | "}}"; |
| 223 | RECREATE_CTX_WITH_MODULE(ctx2, module); |
| 224 | data1 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>"; |
| 225 | data2 = "<l2 xmlns=\"urn:tests:b\"><c><x>b</x></c></l2>"; |
| 226 | CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, 0, tree1); |
| 227 | CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, 0, tree2); |
| 228 | assert_int_equal(LY_ENOT, lyd_compare_single(lyd_child(lyd_child(tree1)), lyd_child(lyd_child(tree2)), 0)); |
| 229 | lyd_free_all(tree1); |
| 230 | lyd_free_all(tree2); |
| 231 | |
| 232 | /* recreate second context with the same opaq data nodes */ |
| 233 | module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;" |
| 234 | "revision 2014-05-08;" |
| 235 | "anydata any {config false;}" |
| 236 | "}"; |
| 237 | RECREATE_CTX_WITH_MODULE(ctx2, module); |
| 238 | data1 = "<any xmlns=\"urn:tests:b\" xmlns:aa=\"urn:tests:b\"><x>aa:x</x></any>"; |
| 239 | data2 = "<any xmlns=\"urn:tests:b\" xmlns:bb=\"urn:tests:b\"><x>bb:x</x></any>"; |
| 240 | CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, LYD_PARSE_ONLY, tree1); |
| 241 | CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, LYD_PARSE_ONLY, tree2); |
| 242 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 243 | lyd_free_all(tree1); |
| 244 | lyd_free_all(tree2); |
| 245 | |
| 246 | /* recreate second context with the different opaq data node value */ |
| 247 | module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;" |
| 248 | "revision 2014-05-08;" |
| 249 | "anydata any {config false;}" |
| 250 | "}"; |
| 251 | RECREATE_CTX_WITH_MODULE(ctx2, module); |
| 252 | data1 = "<any xmlns=\"urn:tests:b\" xmlns:aa=\"urn:tests:b\"><x>aa:x</x></any>"; |
| 253 | data2 = "<any xmlns=\"urn:tests:b\" xmlns:bb=\"urn:tests:b\"><x>bb:y</x></any>"; |
| 254 | CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, LYD_PARSE_ONLY, tree1); |
| 255 | CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, LYD_PARSE_ONLY, tree2); |
| 256 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0)); |
| 257 | lyd_free_all(tree1); |
| 258 | lyd_free_all(tree2); |
| 259 | |
| 260 | /* recreate second context with the wrong prefix in opaq data node value */ |
| 261 | module = "module b {namespace urn:tests:b;prefix b;yang-version 1.1;" |
| 262 | "revision 2014-05-08;" |
| 263 | "anydata any {config false;}" |
| 264 | "}"; |
| 265 | RECREATE_CTX_WITH_MODULE(ctx2, module); |
| 266 | data1 = "<any xmlns=\"urn:tests:b\" xmlns:aa=\"urn:tests:b\"><x>aa:x</x></any>"; |
| 267 | data2 = "<any xmlns=\"urn:tests:b\" xmlns:bb=\"urn:tests:b\"><x>cc:x</x></any>"; |
| 268 | CHECK_PARSE_LYD_PARAM_CTX(UTEST_LYCTX, data1, LYD_PARSE_ONLY, tree1); |
| 269 | CHECK_PARSE_LYD_PARAM_CTX(ctx2, data2, LYD_PARSE_ONLY, tree2); |
| 270 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0)); |
| 271 | lyd_free_all(tree1); |
| 272 | lyd_free_all(tree2); |
| 273 | |
| 274 | /* clean up */ |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 275 | ly_ctx_destroy(ctx2); |
aPiecek | 2425220 | 2021-03-30 12:23:32 +0200 | [diff] [blame] | 276 | _UC->in = NULL; |
| 277 | } |
| 278 | |
| 279 | static void |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 280 | test_dup(void **state) |
| 281 | { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 282 | struct lyd_node *tree1, *tree2; |
| 283 | const char *result; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 284 | const char *data; |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 285 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 286 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 287 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 288 | 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] | 289 | 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] | 290 | lyd_free_all(tree1); |
| 291 | lyd_free_all(tree2); |
| 292 | |
| 293 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 294 | 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] | 295 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 296 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 297 | lyd_free_all(tree1); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 298 | CHECK_PARSE_LYD(result, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 299 | 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] | 300 | lyd_free_all(tree1); |
| 301 | lyd_free_all(tree2); |
| 302 | |
| 303 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
| 304 | 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] | 305 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 306 | 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] | 307 | 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] | 308 | lyd_free_all(tree2); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 309 | 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] | 310 | lyd_free_all(tree1); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 311 | CHECK_PARSE_LYD(result, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 312 | 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] | 313 | lyd_free_all(tree2); |
| 314 | |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 315 | 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] | 316 | lyd_free_all(tree1); |
| 317 | result = "<l2 xmlns=\"urn:tests:a\"/>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 318 | 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] | 319 | 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] | 320 | lyd_free_all(tree1); |
| 321 | lyd_free_all(tree2); |
| 322 | |
| 323 | 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] | 324 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 325 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 326 | 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] | 327 | lyd_free_all(tree1); |
| 328 | lyd_free_all(tree2); |
| 329 | |
| 330 | 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] | 331 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 332 | assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child, NULL, |
| 333 | LYD_DUP_WITH_PARENTS, &tree2)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 334 | int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM; |
| 335 | |
| 336 | 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] | 337 | 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] | 338 | LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 339 | lyd_free_all(tree1); |
| 340 | lyd_free_all(tree2); |
| 341 | |
| 342 | 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] | 343 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 344 | assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)tree1)->child->prev, NULL, |
| 345 | LYD_DUP_WITH_PARENTS, &tree2)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 346 | flag = LYS_CONFIG_W | LYS_SET_ENUM; |
| 347 | 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] | 348 | 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] | 349 | lyd_free_all(tree1); |
| 350 | lyd_free_all(tree2); |
| 351 | |
| 352 | 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] | 353 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 354 | 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] | 355 | assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child, |
| 356 | (struct lyd_node_inner *)tree2, LYD_DUP_WITH_PARENTS, NULL)); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 357 | 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] | 358 | lyd_free_all(tree1); |
| 359 | lyd_free_all(tree2); |
| 360 | |
| 361 | /* invalid */ |
| 362 | 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] | 363 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 364 | assert_int_equal(LY_EINVAL, lyd_dup_single(((struct lyd_node_inner *)tree1)->child->prev, |
| 365 | (struct lyd_node_inner *)tree1->next, LYD_DUP_WITH_PARENTS, NULL)); |
Michal Vasko | 62af369 | 2023-02-09 14:00:09 +0100 | [diff] [blame] | 366 | CHECK_LOG_CTX("Invalid argument parent (lyd_dup_get_local_parent()) - does not interconnect with the created node's parents chain.", |
| 367 | NULL); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 368 | lyd_free_all(tree1); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 371 | static void |
| 372 | test_target(void **state) |
| 373 | { |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 374 | const struct lyd_node_term *term; |
| 375 | struct lyd_node *tree; |
| 376 | struct lyxp_expr *exp; |
| 377 | struct ly_path *path; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 378 | const char *path_str = "/a:l2[2]/c/d[3]"; |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 379 | const char *data = |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 380 | "<l2 xmlns=\"urn:tests:a\"><c>" |
| 381 | " <d>a</d>" |
| 382 | " </c></l2>" |
| 383 | "<l2 xmlns=\"urn:tests:a\"><c>" |
| 384 | " <d>a</d>" |
| 385 | " <d>b</d>" |
| 386 | " <d>b</d>" |
| 387 | " <d>c</d>" |
| 388 | "</c></l2>" |
| 389 | "<l2 xmlns=\"urn:tests:a\"><c>" |
| 390 | "</c></l2>"; |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 391 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 392 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 393 | assert_int_equal(LY_SUCCESS, ly_path_parse(UTEST_LYCTX, NULL, path_str, strlen(path_str), 0, LY_PATH_BEGIN_EITHER, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 394 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp)); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 395 | assert_int_equal(LY_SUCCESS, ly_path_compile(UTEST_LYCTX, NULL, NULL, NULL, exp, LY_PATH_OPER_INPUT, |
Michal Vasko | 0884d21 | 2021-10-14 09:21:46 +0200 | [diff] [blame] | 396 | LY_PATH_TARGET_SINGLE, 1, LY_VALUE_JSON, NULL, &path)); |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 397 | term = lyd_target(path, tree); |
| 398 | |
Michal Vasko | e78faec | 2021-04-08 17:24:43 +0200 | [diff] [blame] | 399 | const int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM | LYS_ORDBY_USER; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 400 | |
| 401 | CHECK_LYSC_NODE(term->schema, NULL, 0, flag, 1, "d", 0, LYS_LEAFLIST, 1, 0, NULL, 0); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 402 | assert_string_equal(lyd_get_value(&term->node), "b"); |
| 403 | assert_string_equal(lyd_get_value(term->prev), "b"); |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 404 | |
| 405 | lyd_free_all(tree); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 406 | ly_path_free(UTEST_LYCTX, path); |
| 407 | lyxp_expr_free(UTEST_LYCTX, exp); |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 408 | } |
| 409 | |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 410 | static void |
| 411 | test_list_pos(void **state) |
| 412 | { |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 413 | const char *data; |
| 414 | struct lyd_node *tree; |
| 415 | |
| 416 | data = "<bar xmlns=\"urn:tests:a\">test</bar>" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 417 | "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>" |
| 418 | "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>" |
| 419 | "<foo xmlns=\"urn:tests:a\">test</foo>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 420 | 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] | 421 | assert_int_equal(0, lyd_list_pos(tree)); |
| 422 | assert_int_equal(1, lyd_list_pos(tree->next)); |
| 423 | assert_int_equal(2, lyd_list_pos(tree->next->next)); |
| 424 | assert_int_equal(0, lyd_list_pos(tree->next->next->next)); |
| 425 | lyd_free_all(tree); |
| 426 | |
| 427 | data = "<ll xmlns=\"urn:tests:a\">one</ll>" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 428 | "<ll xmlns=\"urn:tests:a\">two</ll>" |
| 429 | "<ll xmlns=\"urn:tests:a\">three</ll>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 430 | 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] | 431 | assert_int_equal(1, lyd_list_pos(tree)); |
| 432 | assert_int_equal(2, lyd_list_pos(tree->next)); |
| 433 | assert_int_equal(3, lyd_list_pos(tree->next->next)); |
| 434 | lyd_free_all(tree); |
| 435 | |
| 436 | data = "<ll xmlns=\"urn:tests:a\">one</ll>" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 437 | "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>" |
| 438 | "<ll xmlns=\"urn:tests:a\">two</ll>" |
| 439 | "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>" |
| 440 | "<ll xmlns=\"urn:tests:a\">three</ll>" |
| 441 | "<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] | 442 | 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] | 443 | assert_string_equal("l1", tree->schema->name); |
| 444 | assert_int_equal(1, lyd_list_pos(tree)); |
| 445 | assert_int_equal(2, lyd_list_pos(tree->next)); |
| 446 | assert_int_equal(3, lyd_list_pos(tree->next->next)); |
| 447 | assert_string_equal("ll", tree->next->next->next->schema->name); |
| 448 | assert_int_equal(1, lyd_list_pos(tree->next->next->next)); |
| 449 | assert_int_equal(2, lyd_list_pos(tree->next->next->next->next)); |
| 450 | assert_int_equal(3, lyd_list_pos(tree->next->next->next->next->next)); |
| 451 | lyd_free_all(tree); |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 454 | static void |
| 455 | test_first_sibling(void **state) |
| 456 | { |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 457 | const char *data; |
| 458 | struct lyd_node *tree; |
| 459 | struct lyd_node_inner *parent; |
| 460 | |
| 461 | data = "<bar xmlns=\"urn:tests:a\">test</bar>" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 462 | "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>one</c></l1>" |
| 463 | "<foo xmlns=\"urn:tests:a\">test</foo>"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 464 | 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] | 465 | assert_ptr_equal(tree, lyd_first_sibling(tree->next)); |
| 466 | assert_ptr_equal(tree, lyd_first_sibling(tree)); |
| 467 | assert_ptr_equal(tree, lyd_first_sibling(tree->prev)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 468 | parent = (struct lyd_node_inner *)tree->next; |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 469 | assert_int_equal(LYS_LIST, parent->schema->nodetype); |
| 470 | assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->next)); |
| 471 | assert_ptr_equal(parent->child, lyd_first_sibling(parent->child)); |
| 472 | assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->prev)); |
| 473 | lyd_free_all(tree); |
Radek Krejci | 4233f9b | 2020-11-05 12:38:35 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Michal Vasko | 6b669ff | 2021-04-30 16:25:36 +0200 | [diff] [blame] | 476 | static void |
| 477 | test_find_path(void **state) |
| 478 | { |
| 479 | struct lyd_node *root; |
| 480 | const struct lys_module *mod; |
| 481 | |
| 482 | mod = ly_ctx_get_module_implemented(UTEST_LYCTX, "c"); |
| 483 | assert_non_null(mod); |
| 484 | |
| 485 | assert_int_equal(LY_SUCCESS, lyd_new_inner(NULL, mod, "cont", 0, &root)); |
| 486 | assert_int_equal(LY_SUCCESS, lyd_new_path(root, NULL, "/c:cont/nexthop[gateway='10.0.0.1']", NULL, LYD_NEW_PATH_UPDATE, NULL)); |
| 487 | assert_int_equal(LY_SUCCESS, lyd_new_path(root, NULL, "/c:cont/nexthop[gateway='2100::1']", NULL, LYD_NEW_PATH_UPDATE, NULL)); |
Michal Vasko | 15dc9fa | 2021-05-03 14:33:05 +0200 | [diff] [blame] | 488 | assert_int_equal(LY_SUCCESS, lyd_new_path(root, NULL, "/c:cont/pref[.='fc00::/64']", NULL, 0, NULL)); |
Michal Vasko | 6b669ff | 2021-04-30 16:25:36 +0200 | [diff] [blame] | 489 | |
| 490 | assert_int_equal(LY_SUCCESS, lyd_find_path(root, "/c:cont/nexthop[gateway='10.0.0.1']", 0, NULL)); |
| 491 | assert_int_equal(LY_SUCCESS, lyd_find_path(root, "/c:cont/nexthop[gateway='2100::1']", 0, NULL)); |
Michal Vasko | 15dc9fa | 2021-05-03 14:33:05 +0200 | [diff] [blame] | 492 | assert_int_equal(LY_SUCCESS, lyd_find_path(root, "/c:cont/pref[.='fc00::/64']", 0, NULL)); |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 493 | |
| 494 | assert_int_equal(LY_EVALID, lyd_find_path(root, "/cont", 0, NULL)); |
| 495 | CHECK_LOG_CTX("Prefix missing for \"cont\" in path.", "Schema location \"/c:cont\"."); |
| 496 | assert_int_equal(LY_SUCCESS, lyd_find_path(root, "nexthop[gateway='2100::1']", 0, NULL)); |
| 497 | |
Michal Vasko | 6b669ff | 2021-04-30 16:25:36 +0200 | [diff] [blame] | 498 | lyd_free_all(root); |
| 499 | } |
| 500 | |
aPiecek | 4f07c3e | 2021-06-11 10:53:07 +0200 | [diff] [blame] | 501 | static void |
| 502 | test_data_hash(void **state) |
| 503 | { |
| 504 | struct lyd_node *tree; |
| 505 | const char *schema, *data; |
| 506 | |
| 507 | schema = |
| 508 | "module test-data-hash {" |
| 509 | " yang-version 1.1;" |
| 510 | " namespace \"urn:tests:tdh\";" |
| 511 | " prefix t;" |
| 512 | " container c {" |
| 513 | " leaf-list ll {" |
| 514 | " type string;" |
| 515 | " }" |
| 516 | " }" |
| 517 | "}"; |
| 518 | |
| 519 | UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL); |
| 520 | |
| 521 | /* The number of <ll/> must be greater or equal to LYD_HT_MIN_ITEMS |
| 522 | * for the correct test run. It should guarantee the creation of a hash table. |
| 523 | */ |
| 524 | assert_true(LYD_HT_MIN_ITEMS <= 4); |
| 525 | data = |
| 526 | "<c xmlns='urn:tests:tdh'>" |
| 527 | " <ll/>" |
| 528 | " <ll/>" |
| 529 | " <ll/>" |
| 530 | " <ll/>" |
| 531 | "</c>"; |
| 532 | |
| 533 | /* The run must not crash due to the assert that checks the hash. */ |
| 534 | CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); |
Michal Vasko | 62af369 | 2023-02-09 14:00:09 +0100 | [diff] [blame] | 535 | CHECK_LOG_CTX("Duplicate instance of \"ll\".", "Data location \"/test-data-hash:c/ll[.='']\", line number 1."); |
aPiecek | 4f07c3e | 2021-06-11 10:53:07 +0200 | [diff] [blame] | 536 | lyd_free_all(tree); |
| 537 | } |
| 538 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 539 | static void |
| 540 | test_lyxp_vars(void **UNUSED(state)) |
| 541 | { |
| 542 | struct lyxp_var *vars; |
| 543 | |
| 544 | /* Test free. */ |
| 545 | vars = NULL; |
| 546 | lyxp_vars_free(vars); |
| 547 | |
| 548 | /* Bad arguments for lyxp_vars_add(). */ |
| 549 | assert_int_equal(LY_EINVAL, lyxp_vars_set(NULL, "var1", "val1")); |
| 550 | assert_int_equal(LY_EINVAL, lyxp_vars_set(&vars, NULL, "val1")); |
| 551 | assert_int_equal(LY_EINVAL, lyxp_vars_set(&vars, "var1", NULL)); |
| 552 | lyxp_vars_free(vars); |
| 553 | vars = NULL; |
| 554 | |
| 555 | /* Add one item. */ |
| 556 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "val1")); |
| 557 | assert_int_equal(LY_ARRAY_COUNT(vars), 1); |
| 558 | assert_string_equal(vars[0].name, "var1"); |
| 559 | assert_string_equal(vars[0].value, "val1"); |
| 560 | lyxp_vars_free(vars); |
| 561 | vars = NULL; |
| 562 | |
| 563 | /* Add three items. */ |
| 564 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "val1")); |
| 565 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var2", "val2")); |
| 566 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var3", "val3")); |
| 567 | assert_int_equal(LY_ARRAY_COUNT(vars), 3); |
| 568 | assert_string_equal(vars[0].name, "var1"); |
| 569 | assert_string_equal(vars[0].value, "val1"); |
| 570 | assert_string_equal(vars[1].name, "var2"); |
| 571 | assert_string_equal(vars[1].value, "val2"); |
| 572 | assert_string_equal(vars[2].name, "var3"); |
| 573 | assert_string_equal(vars[2].value, "val3"); |
| 574 | lyxp_vars_free(vars); |
| 575 | vars = NULL; |
| 576 | |
| 577 | /* Change value of a variable. */ |
| 578 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "val1")); |
| 579 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var2", "val2")); |
| 580 | assert_int_equal(LY_SUCCESS, lyxp_vars_set(&vars, "var1", "new_value")); |
| 581 | assert_string_equal(vars[0].name, "var1"); |
| 582 | assert_string_equal(vars[0].value, "new_value"); |
| 583 | assert_string_equal(vars[1].name, "var2"); |
| 584 | assert_string_equal(vars[1].value, "val2"); |
| 585 | lyxp_vars_free(vars); |
| 586 | vars = NULL; |
| 587 | } |
| 588 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 589 | int |
| 590 | main(void) |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 591 | { |
| 592 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 593 | UTEST(test_compare, setup), |
aPiecek | 2425220 | 2021-03-30 12:23:32 +0200 | [diff] [blame] | 594 | UTEST(test_compare_diff_ctx, setup), |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 595 | UTEST(test_dup, setup), |
| 596 | UTEST(test_target, setup), |
| 597 | UTEST(test_list_pos, setup), |
| 598 | UTEST(test_first_sibling, setup), |
Michal Vasko | 6b669ff | 2021-04-30 16:25:36 +0200 | [diff] [blame] | 599 | UTEST(test_find_path, setup), |
aPiecek | 4f07c3e | 2021-06-11 10:53:07 +0200 | [diff] [blame] | 600 | UTEST(test_data_hash, setup), |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 601 | UTEST(test_lyxp_vars), |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 602 | }; |
| 603 | |
| 604 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 605 | } |