blob: ff60fc8bc5c8c890634f1cd297dbdd7249195bcc [file] [log] [blame]
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001/*
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 */
14
15#include <stdarg.h>
16#include <stddef.h>
17#include <stdio.h>
18#include <setjmp.h>
19#include <cmocka.h>
20
21#include "../../src/libyang.h"
22
23#define BUFSIZE 1024
24char logbuf[BUFSIZE] = {0};
25int store = -1; /* negative for infinite logging, positive for limited logging */
26
27struct ly_ctx *ctx; /* context for tests */
28
29/* set to 0 to printing error messages to stderr instead of checking them in code */
30#define ENABLE_LOGGER_CHECKING 1
31
32#if ENABLE_LOGGER_CHECKING
33static void
34logger(LY_LOG_LEVEL level, const char *msg, const char *path)
35{
36 (void) level; /* unused */
37 if (store) {
38 if (path && path[0]) {
39 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
40 } else {
41 strncpy(logbuf, msg, BUFSIZE - 1);
42 }
43 if (store > 0) {
44 --store;
45 }
46 }
47}
48#endif
49
50static int
51setup(void **state)
52{
53 (void) state; /* unused */
54
55 const char *schema_a = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;"
56 "list l1 { key \"a b\"; leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
57 "leaf foo { type string;}"
58 "leaf-list ll { type string;}"
59 "container c {leaf-list x {type string;}}"
60 "anydata any {config false;}"
61 "list l2 {config false; container c{leaf x {type string;}}}}";
62
63#if ENABLE_LOGGER_CHECKING
64 ly_set_log_clb(logger, 1);
65#endif
66
67 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
68 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
69
70 return 0;
71}
72
73static int
74teardown(void **state)
75{
76#if ENABLE_LOGGER_CHECKING
77 if (*state) {
78 fprintf(stderr, "%s\n", logbuf);
79 }
80#else
81 (void) state; /* unused */
82#endif
83
84 ly_ctx_destroy(ctx, NULL);
85 ctx = NULL;
86
87 return 0;
88}
89
90void
91logbuf_clean(void)
92{
93 logbuf[0] = '\0';
94}
95
96#if ENABLE_LOGGER_CHECKING
97# define logbuf_assert(str) assert_string_equal(logbuf, str)
98#else
99# define logbuf_assert(str)
100#endif
101
102static void
103test_compare(void **state)
104{
105 *state = test_compare;
106
107 struct lyd_node *tree1, *tree2;
108
109 const char *data1 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
110 const char *data2 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>y</c></l1>";
111
112 assert_int_equal(LY_SUCCESS, lyd_compare(NULL, NULL, 0));
113
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200114 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA, NULL));
115 assert_non_null(tree2 = lyd_parse_mem(ctx, data2, LYD_XML, LYD_OPT_DATA, NULL));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200116 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, 0));
117 assert_int_equal(LY_ENOT, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
118 assert_int_equal(LY_ENOT, lyd_compare(((struct lyd_node_inner*)tree1)->child, tree2, 0));
119 lyd_free_all(tree1);
120 lyd_free_all(tree2);
121
122 data1 = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
123 data2 = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200124 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA, NULL));
125 assert_non_null(tree2 = lyd_parse_mem(ctx, data2, LYD_XML, LYD_OPT_DATA, NULL));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200126 assert_int_equal(LY_ENOT, lyd_compare(tree1, tree2, 0));
127 assert_int_equal(LY_SUCCESS, lyd_compare(tree1->next, tree2, 0));
128 lyd_free_all(tree1);
129 lyd_free_all(tree2);
130
131 data1 = "<ll xmlns=\"urn:tests:a\">a</ll><ll xmlns=\"urn:tests:a\">b</ll>";
132 data2 = "<ll xmlns=\"urn:tests:a\">b</ll>";
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200133 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA, NULL));
134 assert_non_null(tree2 = lyd_parse_mem(ctx, data2, LYD_XML, LYD_OPT_DATA, NULL));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200135 assert_int_equal(LY_ENOT, lyd_compare(tree1, tree2, 0));
136 assert_int_equal(LY_ENOT, lyd_compare(NULL, tree2, 0));
137 assert_int_equal(LY_ENOT, lyd_compare(tree1, NULL, 0));
138 assert_int_equal(LY_SUCCESS, lyd_compare(tree1->next, tree2, 0));
139 lyd_free_all(tree1);
140 lyd_free_all(tree2);
141
142 data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>";
143 data2 = "<c xmlns=\"urn:tests:a\"><x>y</x></c>";
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200144 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA, NULL));
145 assert_non_null(tree2 = lyd_parse_mem(ctx, data2, LYD_XML, LYD_OPT_DATA, NULL));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200146 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, 0));
147 assert_int_equal(LY_ENOT, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
148 lyd_free_all(tree1);
149 lyd_free_all(tree2);
150
151 data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>";
152 data2 = "<c xmlns=\"urn:tests:a\"><x>x</x><x>y</x></c>";
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200153 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA, NULL));
154 assert_non_null(tree2 = lyd_parse_mem(ctx, data2, LYD_XML, LYD_OPT_DATA, NULL));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200155 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, 0));
156 assert_int_equal(LY_ENOT, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
157 lyd_free_all(tree1);
158 lyd_free_all(tree2);
159
160 data1 = "<any xmlns=\"urn:tests:a\"><x>x</x></any>";
161 data2 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200162 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA, NULL));
163 assert_non_null(tree2 = lyd_parse_mem(ctx, data2, LYD_XML, LYD_OPT_DATA, NULL));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200164 assert_int_equal(LY_ENOT, lyd_compare(tree1, tree2, 0));
165 lyd_free_all(tree1);
166 data1 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200167 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA, NULL));
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200168 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, 0));
169 lyd_free_all(tree1);
170 lyd_free_all(tree2);
171
172 *state = NULL;
173}
174
Radek Krejci22ebdba2019-07-25 13:59:43 +0200175
176static void
177test_dup(void **state)
178{
179 *state = test_dup;
180
181 struct lyd_node *tree1, *tree2;
182 const char *result;
183 const char *data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
184
185 assert_non_null(tree1 = lyd_parse_mem(ctx, data, LYD_XML, LYD_OPT_DATA, NULL));
186 assert_non_null(tree2 = lyd_dup(tree1, NULL, LYD_DUP_RECURSIVE));
187 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
188 lyd_free_all(tree1);
189 lyd_free_all(tree2);
190
191 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>";
192 result = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b></l1>";
193 assert_non_null(tree1 = lyd_parse_mem(ctx, data, LYD_XML, LYD_OPT_DATA, NULL));
194 assert_non_null(tree2 = lyd_dup(tree1, NULL, 0));
195 lyd_free_all(tree1);
196 assert_non_null(tree1 = lyd_parse_mem(ctx, result, LYD_XML, LYD_OPT_DATA, NULL));
197 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
198 lyd_free_all(tree1);
199 lyd_free_all(tree2);
200
201 data = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
202 result = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2>";
203 assert_non_null(tree1 = lyd_parse_mem(ctx, data, LYD_XML, LYD_OPT_DATA, NULL));
204 assert_non_null(tree2 = lyd_dup(tree1, NULL, LYD_DUP_WITH_SIBLINGS | LYD_DUP_RECURSIVE));
205 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
206 lyd_free_all(tree2);
207
208 assert_non_null(tree2 = lyd_dup(tree1, NULL, LYD_DUP_RECURSIVE));
209 lyd_free_all(tree1);
210 assert_non_null(tree1 = lyd_parse_mem(ctx, result, LYD_XML, LYD_OPT_DATA, NULL));
211 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
212 lyd_free_all(tree2);
213
214 assert_non_null(tree2 = lyd_dup(tree1, NULL, 0));
215 lyd_free_all(tree1);
216 result = "<l2 xmlns=\"urn:tests:a\"/>";
217 assert_non_null(tree1 = lyd_parse_mem(ctx, result, LYD_XML, LYD_OPT_DATA, NULL));
218 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
219 lyd_free_all(tree1);
220 lyd_free_all(tree2);
221
222 data = "<any xmlns=\"urn:tests:a\"><c><a>a</a></c></any>";
223 assert_non_null(tree1 = lyd_parse_mem(ctx, data, LYD_XML, LYD_OPT_DATA, NULL));
224 assert_non_null(tree2 = lyd_dup(tree1, NULL, 0));
225 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
226 lyd_free_all(tree1);
227 lyd_free_all(tree2);
228
229 data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
230 assert_non_null(tree1 = lyd_parse_mem(ctx, data, LYD_XML, LYD_OPT_DATA, NULL));
231 assert_non_null(tree2 = lyd_dup(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1)->child)->child, NULL, LYD_DUP_WITH_PARENTS));
232 assert_string_equal("x", tree2->schema->name);
233 assert_non_null(tree2->parent);
234 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, (struct lyd_node*)tree2->parent->parent, LYD_COMPARE_FULL_RECURSION));
235 lyd_free_all(tree1);
236 lyd_free_all(tree2);
237
238 data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1>";
239 assert_non_null(tree1 = lyd_parse_mem(ctx, data, LYD_XML, LYD_OPT_DATA, NULL));
240 assert_non_null(tree2 = lyd_dup(((struct lyd_node_inner*)tree1)->child->prev, NULL, LYD_DUP_WITH_PARENTS));
241 assert_string_equal("c", tree2->schema->name);
242 assert_non_null(tree2->parent);
243 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, (struct lyd_node*)tree2->parent, LYD_COMPARE_FULL_RECURSION));
244 lyd_free_all(tree1);
245 lyd_free_all(tree2);
246
247 data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
248 assert_non_null(tree1 = lyd_parse_mem(ctx, data, LYD_XML, LYD_OPT_DATA, NULL));
249 assert_non_null(tree2 = lyd_dup(tree1, NULL, 0));
250 assert_non_null(lyd_dup(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1)->child)->child,
251 (struct lyd_node_inner*)tree2, LYD_DUP_WITH_PARENTS));
252 assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
253 lyd_free_all(tree1);
254 lyd_free_all(tree2);
255
256 /* invalid */
257 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>";
258 assert_non_null(tree1 = lyd_parse_mem(ctx, data, LYD_XML, LYD_OPT_DATA, NULL));
259 assert_null(lyd_dup(((struct lyd_node_inner*)tree1)->child->prev, (struct lyd_node_inner*)tree1->next, LYD_DUP_WITH_PARENTS));
260 lyd_free_all(tree1);
261
262 *state = NULL;
263}
264
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200265int main(void)
266{
267 const struct CMUnitTest tests[] = {
268 cmocka_unit_test_setup_teardown(test_compare, setup, teardown),
Radek Krejci22ebdba2019-07-25 13:59:43 +0200269 cmocka_unit_test_setup_teardown(test_dup, setup, teardown),
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200270 };
271
272 return cmocka_run_group_tests(tests, NULL, NULL);
273}