blob: 0cf085117c9ef84358c362dc85290fca83fde106 [file] [log] [blame]
Michal Vasko004d3152020-06-11 19:59:22 +02001/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +02002 * @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 */
14
15#include <stdarg.h>
16#include <stddef.h>
17#include <stdio.h>
18#include <setjmp.h>
19#include <cmocka.h>
20
Radek Krejci70593c12020-06-13 20:48:09 +020021#include "libyang.h"
Michal Vasko38c2ba72020-07-27 14:46:59 +020022#include "common.h"
23#include "path.h"
24#include "xpath.h"
Radek Krejci1f05b6a2019-07-18 16:15:06 +020025
26#define BUFSIZE 1024
27char logbuf[BUFSIZE] = {0};
28int store = -1; /* negative for infinite logging, positive for limited logging */
29
30struct ly_ctx *ctx; /* context for tests */
31
32/* set to 0 to printing error messages to stderr instead of checking them in code */
33#define ENABLE_LOGGER_CHECKING 1
34
35#if ENABLE_LOGGER_CHECKING
36static void
37logger(LY_LOG_LEVEL level, const char *msg, const char *path)
38{
39 (void) level; /* unused */
40 if (store) {
41 if (path && path[0]) {
42 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
43 } else {
44 strncpy(logbuf, msg, BUFSIZE - 1);
45 }
46 if (store > 0) {
47 --store;
48 }
49 }
50}
51#endif
52
53static int
54setup(void **state)
55{
56 (void) state; /* unused */
57
58 const char *schema_a = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;"
Radek Krejcica989142020-11-05 11:32:22 +010059 "leaf bar {type string;}"
Radek Krejci1f05b6a2019-07-18 16:15:06 +020060 "list l1 { key \"a b\"; leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
61 "leaf foo { type string;}"
62 "leaf-list ll { type string;}"
63 "container c {leaf-list x {type string;}}"
64 "anydata any {config false;}"
Michal Vasko38c2ba72020-07-27 14:46:59 +020065 "list l2 {config false;"
66 "container c{leaf x {type string;} leaf-list d {type string;}}"
67 "}}";
Radek Krejci1f05b6a2019-07-18 16:15:06 +020068
69#if ENABLE_LOGGER_CHECKING
70 ly_set_log_clb(logger, 1);
71#endif
72
73 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
Michal Vasko3a41dff2020-07-15 14:30:28 +020074 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_a, LYS_IN_YANG, NULL));
Radek Krejci1f05b6a2019-07-18 16:15:06 +020075
76 return 0;
77}
78
79static int
80teardown(void **state)
81{
82#if ENABLE_LOGGER_CHECKING
83 if (*state) {
84 fprintf(stderr, "%s\n", logbuf);
85 }
86#else
87 (void) state; /* unused */
88#endif
89
90 ly_ctx_destroy(ctx, NULL);
91 ctx = NULL;
92
93 return 0;
94}
95
96void
97logbuf_clean(void)
98{
99 logbuf[0] = '\0';
100}
101
102#if ENABLE_LOGGER_CHECKING
103# define logbuf_assert(str) assert_string_equal(logbuf, str)
104#else
105# define logbuf_assert(str)
106#endif
107
108static void
109test_compare(void **state)
110{
111 *state = test_compare;
112
113 struct lyd_node *tree1, *tree2;
114
115 const char *data1 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
116 const char *data2 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>y</c></l1>";
117
Michal Vasko8f359bf2020-07-28 10:41:15 +0200118 assert_int_equal(LY_SUCCESS, lyd_compare_single(NULL, NULL, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200119
Radek Krejci7931b192020-06-25 17:05:03 +0200120 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
121 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200122 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
123 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
124 assert_int_equal(LY_ENOT, lyd_compare_single(((struct lyd_node_inner*)tree1)->child, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200125 lyd_free_all(tree1);
126 lyd_free_all(tree2);
127
128 data1 = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
129 data2 = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Krejci7931b192020-06-25 17:05:03 +0200130 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
131 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200132 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
133 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200134 lyd_free_all(tree1);
135 lyd_free_all(tree2);
136
137 data1 = "<ll xmlns=\"urn:tests:a\">a</ll><ll xmlns=\"urn:tests:a\">b</ll>";
138 data2 = "<ll xmlns=\"urn:tests:a\">b</ll>";
Radek Krejci7931b192020-06-25 17:05:03 +0200139 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
140 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200141 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
142 assert_int_equal(LY_ENOT, lyd_compare_single(NULL, tree2, 0));
143 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, NULL, 0));
144 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200145 lyd_free_all(tree1);
146 lyd_free_all(tree2);
147
148 data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>";
149 data2 = "<c xmlns=\"urn:tests:a\"><x>y</x></c>";
Radek Krejci7931b192020-06-25 17:05:03 +0200150 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
151 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200152 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
153 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200154 lyd_free_all(tree1);
155 lyd_free_all(tree2);
156
157 data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>";
158 data2 = "<c xmlns=\"urn:tests:a\"><x>x</x><x>y</x></c>";
Radek Krejci7931b192020-06-25 17:05:03 +0200159 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
160 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200161 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
162 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200163 lyd_free_all(tree1);
164 lyd_free_all(tree2);
165
166 data1 = "<any xmlns=\"urn:tests:a\"><x>x</x></any>";
167 data2 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
Radek Krejci7931b192020-06-25 17:05:03 +0200168 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
169 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200170 assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200171 lyd_free_all(tree1);
172 data1 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
Radek Krejci7931b192020-06-25 17:05:03 +0200173 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200174 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200175 lyd_free_all(tree1);
176 lyd_free_all(tree2);
177
178 *state = NULL;
179}
180
Radek Krejci22ebdba2019-07-25 13:59:43 +0200181static void
182test_dup(void **state)
183{
184 *state = test_dup;
185
186 struct lyd_node *tree1, *tree2;
187 const char *result;
188 const char *data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
189
Radek Krejci7931b192020-06-25 17:05:03 +0200190 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200191 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200192 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200193 lyd_free_all(tree1);
194 lyd_free_all(tree2);
195
196 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
197 result = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b></l1>";
Radek Krejci7931b192020-06-25 17:05:03 +0200198 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200199 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200200 lyd_free_all(tree1);
Radek Krejci7931b192020-06-25 17:05:03 +0200201 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, result, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200202 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200203 lyd_free_all(tree1);
204 lyd_free_all(tree2);
205
206 data = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
207 result = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2>";
Radek Krejci7931b192020-06-25 17:05:03 +0200208 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200209 assert_int_equal(LY_SUCCESS, lyd_dup_siblings(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200210 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200211 lyd_free_all(tree2);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200212 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200213 lyd_free_all(tree1);
Radek Krejci7931b192020-06-25 17:05:03 +0200214 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, result, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200215 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200216 lyd_free_all(tree2);
217
Michal Vasko3a41dff2020-07-15 14:30:28 +0200218 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200219 lyd_free_all(tree1);
220 result = "<l2 xmlns=\"urn:tests:a\"/>";
Radek Krejci7931b192020-06-25 17:05:03 +0200221 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, result, LYD_XML, LYD_PARSE_ONLY, 0, &tree1));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200222 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200223 lyd_free_all(tree1);
224 lyd_free_all(tree2);
225
226 data = "<any xmlns=\"urn:tests:a\"><c><a>a</a></c></any>";
Radek Krejci7931b192020-06-25 17:05:03 +0200227 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200228 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200229 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200230 lyd_free_all(tree1);
231 lyd_free_all(tree2);
232
233 data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Krejci7931b192020-06-25 17:05:03 +0200234 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200235 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1)->child)->child, NULL,
236 LYD_DUP_WITH_PARENTS, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200237 assert_string_equal("x", tree2->schema->name);
238 assert_non_null(tree2->parent);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200239 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, (struct lyd_node*)tree2->parent->parent, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200240 lyd_free_all(tree1);
241 lyd_free_all(tree2);
242
243 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1>";
Radek Krejci7931b192020-06-25 17:05:03 +0200244 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200245 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)tree1)->child->prev, NULL,
246 LYD_DUP_WITH_PARENTS, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200247 assert_string_equal("c", tree2->schema->name);
248 assert_non_null(tree2->parent);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200249 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, (struct lyd_node*)tree2->parent, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200250 lyd_free_all(tree1);
251 lyd_free_all(tree2);
252
253 data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Krejci7931b192020-06-25 17:05:03 +0200254 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200255 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
256 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1)->child)->child,
257 (struct lyd_node_inner*)tree2, LYD_DUP_WITH_PARENTS, NULL));
Michal Vasko8f359bf2020-07-28 10:41:15 +0200258 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200259 lyd_free_all(tree1);
260 lyd_free_all(tree2);
261
262 /* invalid */
263 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 Krejci7931b192020-06-25 17:05:03 +0200264 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200265 assert_int_equal(LY_EINVAL, lyd_dup_single(((struct lyd_node_inner*)tree1)->child->prev,
266 (struct lyd_node_inner*)tree1->next, LYD_DUP_WITH_PARENTS, NULL));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200267 lyd_free_all(tree1);
268
269 *state = NULL;
270}
271
Michal Vasko38c2ba72020-07-27 14:46:59 +0200272static void
273test_target(void **state)
274{
275 *state = test_target;
276
277 const struct lyd_node_term *term;
278 struct lyd_node *tree;
279 struct lyxp_expr *exp;
280 struct ly_path *path;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200281 const char *path_str = "/a:l2[2]/c/d[3]";
Michal Vasko38c2ba72020-07-27 14:46:59 +0200282 const char *data =
283 "<l2 xmlns=\"urn:tests:a\"><c>"
284 "<d>a</d>"
285 "</c></l2>"
286 "<l2 xmlns=\"urn:tests:a\"><c>"
287 "<d>a</d>"
288 "<d>b</d>"
289 "<d>b</d>"
290 "<d>c</d>"
291 "</c></l2>"
292 "<l2 xmlns=\"urn:tests:a\"><c>"
293 "</c></l2>";
294
295 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
296 assert_int_equal(LY_SUCCESS, ly_path_parse(ctx, NULL, path_str, strlen(path_str), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
297 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp));
298 assert_int_equal(LY_SUCCESS, ly_path_compile(ctx, NULL, NULL, exp, LY_PATH_LREF_FALSE, LY_PATH_OPER_INPUT,
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200299 LY_PATH_TARGET_SINGLE, LY_PREF_JSON, NULL, &path));
Michal Vasko38c2ba72020-07-27 14:46:59 +0200300 term = lyd_target(path, tree);
301
302 assert_string_equal(term->schema->name, "d");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200303 assert_string_equal(LYD_CANON_VALUE(term), "b");
304 assert_string_equal(LYD_CANON_VALUE(term->prev), "b");
Michal Vasko38c2ba72020-07-27 14:46:59 +0200305
306 lyd_free_all(tree);
307 ly_path_free(ctx, path);
308 lyxp_expr_free(ctx, exp);
309
310 *state = NULL;
311}
312
Radek Krejcica989142020-11-05 11:32:22 +0100313static void
314test_list_pos(void **state)
315{
316 *state = test_list_pos;
317
318 const char *data;
319 struct lyd_node *tree;
320
321 data = "<bar xmlns=\"urn:tests:a\">test</bar>"
322 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>"
323 "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>"
324 "<foo xmlns=\"urn:tests:a\">test</foo>";
325 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
326 assert_int_equal(0, lyd_list_pos(tree));
327 assert_int_equal(1, lyd_list_pos(tree->next));
328 assert_int_equal(2, lyd_list_pos(tree->next->next));
329 assert_int_equal(0, lyd_list_pos(tree->next->next->next));
330 lyd_free_all(tree);
331
332 data = "<ll xmlns=\"urn:tests:a\">one</ll>"
333 "<ll xmlns=\"urn:tests:a\">two</ll>"
334 "<ll xmlns=\"urn:tests:a\">three</ll>";
335 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
336 assert_int_equal(1, lyd_list_pos(tree));
337 assert_int_equal(2, lyd_list_pos(tree->next));
338 assert_int_equal(3, lyd_list_pos(tree->next->next));
339 lyd_free_all(tree);
340
341 data = "<ll xmlns=\"urn:tests:a\">one</ll>"
342 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>"
343 "<ll xmlns=\"urn:tests:a\">two</ll>"
344 "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>"
345 "<ll xmlns=\"urn:tests:a\">three</ll>"
346 "<l1 xmlns=\"urn:tests:a\"><a>three</a><b>three</b></l1>";
347 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
348 assert_string_equal("l1", tree->schema->name);
349 assert_int_equal(1, lyd_list_pos(tree));
350 assert_int_equal(2, lyd_list_pos(tree->next));
351 assert_int_equal(3, lyd_list_pos(tree->next->next));
352 assert_string_equal("ll", tree->next->next->next->schema->name);
353 assert_int_equal(1, lyd_list_pos(tree->next->next->next));
354 assert_int_equal(2, lyd_list_pos(tree->next->next->next->next));
355 assert_int_equal(3, lyd_list_pos(tree->next->next->next->next->next));
356 lyd_free_all(tree);
357
358 *state = NULL;
359}
360
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200361int main(void)
362{
363 const struct CMUnitTest tests[] = {
364 cmocka_unit_test_setup_teardown(test_compare, setup, teardown),
Radek Krejci22ebdba2019-07-25 13:59:43 +0200365 cmocka_unit_test_setup_teardown(test_dup, setup, teardown),
Michal Vasko38c2ba72020-07-27 14:46:59 +0200366 cmocka_unit_test_setup_teardown(test_target, setup, teardown),
Radek Krejcica989142020-11-05 11:32:22 +0100367 cmocka_unit_test_setup_teardown(test_list_pos, setup, teardown),
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200368 };
369
370 return cmocka_run_group_tests(tests, NULL, NULL);
371}