blob: 8456473311626755c3a53c71f115abb3ed135f4b [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 Vasko26123192020-11-09 21:02:34 +0100132 assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0));
133 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next->next, tree2->next, 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 Vasko26123192020-11-09 21:02:34 +0100170 assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 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 Vasko26123192020-11-09 21:02:34 +0100210 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2->next, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200211 lyd_free_all(tree2);
Michal Vasko26123192020-11-09 21:02:34 +0100212 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, 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 Vasko26123192020-11-09 21:02:34 +0100215 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200216 lyd_free_all(tree2);
217
Michal Vasko26123192020-11-09 21:02:34 +0100218 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, 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 Vasko26123192020-11-09 21:02:34 +0100235 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1->next)->child)->child, NULL,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200236 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 Vasko26123192020-11-09 21:02:34 +0100239 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, (struct lyd_node*)tree2->parent->parent,
240 LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200241 lyd_free_all(tree1);
242 lyd_free_all(tree2);
243
244 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1>";
Radek Krejci7931b192020-06-25 17:05:03 +0200245 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 +0200246 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)tree1)->child->prev, NULL,
247 LYD_DUP_WITH_PARENTS, &tree2));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200248 assert_string_equal("c", tree2->schema->name);
249 assert_non_null(tree2->parent);
Michal Vasko8f359bf2020-07-28 10:41:15 +0200250 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 +0200251 lyd_free_all(tree1);
252 lyd_free_all(tree2);
253
254 data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Krejci7931b192020-06-25 17:05:03 +0200255 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
Michal Vasko26123192020-11-09 21:02:34 +0100256 assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2));
257 assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1->next)->child)->child,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200258 (struct lyd_node_inner*)tree2, LYD_DUP_WITH_PARENTS, NULL));
Michal Vasko26123192020-11-09 21:02:34 +0100259 assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200260 lyd_free_all(tree1);
261 lyd_free_all(tree2);
262
263 /* invalid */
264 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 +0200265 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 +0200266 assert_int_equal(LY_EINVAL, lyd_dup_single(((struct lyd_node_inner*)tree1)->child->prev,
267 (struct lyd_node_inner*)tree1->next, LYD_DUP_WITH_PARENTS, NULL));
Radek Krejci22ebdba2019-07-25 13:59:43 +0200268 lyd_free_all(tree1);
269
270 *state = NULL;
271}
272
Michal Vasko38c2ba72020-07-27 14:46:59 +0200273static void
274test_target(void **state)
275{
276 *state = test_target;
277
278 const struct lyd_node_term *term;
279 struct lyd_node *tree;
280 struct lyxp_expr *exp;
281 struct ly_path *path;
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200282 const char *path_str = "/a:l2[2]/c/d[3]";
Michal Vasko38c2ba72020-07-27 14:46:59 +0200283 const char *data =
284 "<l2 xmlns=\"urn:tests:a\"><c>"
285 "<d>a</d>"
286 "</c></l2>"
287 "<l2 xmlns=\"urn:tests:a\"><c>"
288 "<d>a</d>"
289 "<d>b</d>"
290 "<d>b</d>"
291 "<d>c</d>"
292 "</c></l2>"
293 "<l2 xmlns=\"urn:tests:a\"><c>"
294 "</c></l2>";
295
296 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
297 assert_int_equal(LY_SUCCESS, ly_path_parse(ctx, NULL, path_str, strlen(path_str), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE,
298 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp));
299 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 +0200300 LY_PATH_TARGET_SINGLE, LY_PREF_JSON, NULL, &path));
Michal Vasko38c2ba72020-07-27 14:46:59 +0200301 term = lyd_target(path, tree);
302
303 assert_string_equal(term->schema->name, "d");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200304 assert_string_equal(LYD_CANON_VALUE(term), "b");
305 assert_string_equal(LYD_CANON_VALUE(term->prev), "b");
Michal Vasko38c2ba72020-07-27 14:46:59 +0200306
307 lyd_free_all(tree);
308 ly_path_free(ctx, path);
309 lyxp_expr_free(ctx, exp);
310
311 *state = NULL;
312}
313
Radek Krejcica989142020-11-05 11:32:22 +0100314static void
315test_list_pos(void **state)
316{
317 *state = test_list_pos;
318
319 const char *data;
320 struct lyd_node *tree;
321
322 data = "<bar xmlns=\"urn:tests:a\">test</bar>"
323 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>"
324 "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>"
325 "<foo xmlns=\"urn:tests:a\">test</foo>";
326 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
327 assert_int_equal(0, lyd_list_pos(tree));
328 assert_int_equal(1, lyd_list_pos(tree->next));
329 assert_int_equal(2, lyd_list_pos(tree->next->next));
330 assert_int_equal(0, lyd_list_pos(tree->next->next->next));
331 lyd_free_all(tree);
332
333 data = "<ll xmlns=\"urn:tests:a\">one</ll>"
334 "<ll xmlns=\"urn:tests:a\">two</ll>"
335 "<ll xmlns=\"urn:tests:a\">three</ll>";
336 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
337 assert_int_equal(1, lyd_list_pos(tree));
338 assert_int_equal(2, lyd_list_pos(tree->next));
339 assert_int_equal(3, lyd_list_pos(tree->next->next));
340 lyd_free_all(tree);
341
342 data = "<ll xmlns=\"urn:tests:a\">one</ll>"
343 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>"
344 "<ll xmlns=\"urn:tests:a\">two</ll>"
345 "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>"
346 "<ll xmlns=\"urn:tests:a\">three</ll>"
347 "<l1 xmlns=\"urn:tests:a\"><a>three</a><b>three</b></l1>";
348 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
349 assert_string_equal("l1", tree->schema->name);
350 assert_int_equal(1, lyd_list_pos(tree));
351 assert_int_equal(2, lyd_list_pos(tree->next));
352 assert_int_equal(3, lyd_list_pos(tree->next->next));
353 assert_string_equal("ll", tree->next->next->next->schema->name);
354 assert_int_equal(1, lyd_list_pos(tree->next->next->next));
355 assert_int_equal(2, lyd_list_pos(tree->next->next->next->next));
356 assert_int_equal(3, lyd_list_pos(tree->next->next->next->next->next));
357 lyd_free_all(tree);
358
359 *state = NULL;
360}
361
Radek Krejci4233f9b2020-11-05 12:38:35 +0100362static void
363test_first_sibling(void **state)
364{
365 *state = test_first_sibling;
366
367 const char *data;
368 struct lyd_node *tree;
369 struct lyd_node_inner *parent;
370
371 data = "<bar xmlns=\"urn:tests:a\">test</bar>"
372 "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>one</c></l1>"
373 "<foo xmlns=\"urn:tests:a\">test</foo>";
374 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
375 assert_ptr_equal(tree, lyd_first_sibling(tree->next));
376 assert_ptr_equal(tree, lyd_first_sibling(tree));
377 assert_ptr_equal(tree, lyd_first_sibling(tree->prev));
378 parent = (struct lyd_node_inner*)tree->next;
379 assert_int_equal(LYS_LIST, parent->schema->nodetype);
380 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->next));
381 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child));
382 assert_ptr_equal(parent->child, lyd_first_sibling(parent->child->prev));
383 lyd_free_all(tree);
384
385 *state = NULL;
386}
387
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200388int main(void)
389{
390 const struct CMUnitTest tests[] = {
391 cmocka_unit_test_setup_teardown(test_compare, setup, teardown),
Radek Krejci22ebdba2019-07-25 13:59:43 +0200392 cmocka_unit_test_setup_teardown(test_dup, setup, teardown),
Michal Vasko38c2ba72020-07-27 14:46:59 +0200393 cmocka_unit_test_setup_teardown(test_target, setup, teardown),
Radek Krejcica989142020-11-05 11:32:22 +0100394 cmocka_unit_test_setup_teardown(test_list_pos, setup, teardown),
Radek Krejci4233f9b2020-11-05 12:38:35 +0100395 cmocka_unit_test_setup_teardown(test_first_sibling, setup, teardown),
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200396 };
397
398 return cmocka_run_group_tests(tests, NULL, NULL);
399}