blob: dbc26ea7b9f85c1d02c0d4dbbfaa4af227d476e8 [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
114 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA));
115 assert_non_null(tree2 = lyd_parse_mem(ctx, data2, LYD_XML, LYD_OPT_DATA));
116 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>";
124 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA));
125 assert_non_null(tree2 = lyd_parse_mem(ctx, data2, LYD_XML, LYD_OPT_DATA));
126 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>";
133 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA));
134 assert_non_null(tree2 = lyd_parse_mem(ctx, data2, LYD_XML, LYD_OPT_DATA));
135 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>";
144 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA));
145 assert_non_null(tree2 = lyd_parse_mem(ctx, data2, LYD_XML, LYD_OPT_DATA));
146 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>";
153 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA));
154 assert_non_null(tree2 = lyd_parse_mem(ctx, data2, LYD_XML, LYD_OPT_DATA));
155 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>";
162 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA));
163 assert_non_null(tree2 = lyd_parse_mem(ctx, data2, LYD_XML, LYD_OPT_DATA));
164 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>";
167 assert_non_null(tree1 = lyd_parse_mem(ctx, data1, LYD_XML, LYD_OPT_DATA));
168 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
175int main(void)
176{
177 const struct CMUnitTest tests[] = {
178 cmocka_unit_test_setup_teardown(test_compare, setup, teardown),
179 };
180
181 return cmocka_run_group_tests(tests, NULL, NULL);
182}