blob: 0a91f03715837a7bc89b376293ceaade6db904f2 [file] [log] [blame]
Radek Krejcib9726b22019-02-22 16:35:37 +01001/*
Radek Krejcid0676f12019-02-25 13:34:44 +01002 * @file test_tree_schema.c
Radek Krejcib9726b22019-02-22 16:35:37 +01003 * @author: Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcid0676f12019-02-25 13:34:44 +01004 * @brief unit tests for functions from tress_schema.c
Radek Krejcib9726b22019-02-22 16:35:37 +01005 *
Radek Krejcid0676f12019-02-25 13:34:44 +01006 * Copyright (c) 2018-2019 CESNET, z.s.p.o.
Radek Krejcib9726b22019-02-22 16:35:37 +01007 *
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 "../../src/common.c"
16#include "../../src/log.c"
17#include "../../src/set.c"
18#include "../../src/parser_yang.c"
19#include "../../src/tree_schema.c"
20#include "../../src/tree_schema_compile.c"
21#include "../../src/tree_schema_free.c"
22#include "../../src/tree_schema_helpers.c"
23#include "../../src/hash_table.c"
24#include "../../src/xpath.c"
25#include "../../src/context.c"
26
27#include <stdarg.h>
28#include <stddef.h>
29#include <stdio.h>
30#include <setjmp.h>
31#include <cmocka.h>
32
33#include "libyang.h"
34
35#define BUFSIZE 1024
36char logbuf[BUFSIZE] = {0};
37int store = -1; /* negative for infinite logging, positive for limited logging */
38
39/* set to 0 to printing error messages to stderr instead of checking them in code */
40#define ENABLE_LOGGER_CHECKING 1
41
42#if ENABLE_LOGGER_CHECKING
43static void
44logger(LY_LOG_LEVEL level, const char *msg, const char *path)
45{
46 (void) level; /* unused */
47 if (store) {
48 if (path && path[0]) {
49 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
50 } else {
51 strncpy(logbuf, msg, BUFSIZE - 1);
52 }
53 if (store > 0) {
54 --store;
55 }
56 }
57}
58#endif
59
60static int
61logger_setup(void **state)
62{
63 (void) state; /* unused */
64
65 ly_set_log_clb(logger, 0);
66
67 return 0;
68}
69
70static int
71logger_teardown(void **state)
72{
73 (void) state; /* unused */
74#if ENABLE_LOGGER_CHECKING
75 if (*state) {
76 fprintf(stderr, "%s\n", logbuf);
77 }
78#endif
79 return 0;
80}
81
82void
83logbuf_clean(void)
84{
85 logbuf[0] = '\0';
86}
87
88#if ENABLE_LOGGER_CHECKING
89# define logbuf_assert(str) assert_string_equal(logbuf, str)
90#else
91# define logbuf_assert(str)
92#endif
93
94static void
95test_getnext(void **state)
96{
97 *state = test_getnext;
98
99 struct ly_ctx *ctx;
100 struct lys_module *mod;
Radek Krejci05b774b2019-02-25 13:26:18 +0100101 const struct lysc_node *node = NULL, *four;
102 const struct lysc_node_container *cont;
103 const struct lysc_action *rpc;
104
Radek Krejcib9726b22019-02-22 16:35:37 +0100105 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
106
107 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;"
108 "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}"
109 " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}"
110 " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
111 " notification nine {leaf nine-data {type string;}}}"
112 "leaf b {type string;} leaf-list c {type string;} list d {config false;}"
113 "choice x { leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
114 "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
Radek Krejcib0e49372019-02-25 13:43:21 +0100115 "rpc i;"
116 "notification j {leaf i-data {type string;}}"
117 "notification k;}", LYS_IN_YANG));
Radek Krejci05b774b2019-02-25 13:26:18 +0100118 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
119 assert_string_equal("a", node->name);
120 cont = (const struct lysc_node_container*)node;
121 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
122 assert_string_equal("b", node->name);
123 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
124 assert_string_equal("c", node->name);
125 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
126 assert_string_equal("d", node->name);
127 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
128 assert_string_equal("e", node->name);
129 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
130 assert_string_equal("f", node->name);
131 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
132 assert_string_equal("g", node->name);
133 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
134 assert_string_equal("h", node->name);
135 rpc = (const struct lysc_action*)node;
Radek Krejci05b774b2019-02-25 13:26:18 +0100136 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
137 assert_string_equal("i", node->name);
Radek Krejcib0e49372019-02-25 13:43:21 +0100138 /* TODO Notifications
139 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
140 assert_string_equal("j", node->name);
141 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
142 assert_string_equal("k", node->name);
Radek Krejci05b774b2019-02-25 13:26:18 +0100143 */
144 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
145 /* Inside container */
146 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
147 assert_string_equal("one", node->name);
148 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
149 assert_string_equal("two", node->name);
150 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
151 assert_string_equal("three", node->name);
152 assert_non_null(node = four = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
153 assert_string_equal("four", node->name);
154 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
155 assert_string_equal("five", node->name);
156 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
157 assert_string_equal("six", node->name);
158 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
159 assert_string_equal("seven", node->name);
160 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
161 assert_string_equal("eight", node->name);
162 /* TODO Notifications
163 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
164 assert_string_equal("nine", node->name);
165 */
166 assert_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
167 /* Inside RPC */
168 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0));
169 assert_string_equal("h-input", node->name);
170 assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0));
171
172 /* options */
173 assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
174 assert_string_equal("x", node->name);
175 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
176 assert_string_equal("seven", node->name);
177
178 assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_NOCHOICE));
179 assert_string_equal("seven", node->name);
180
181 assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
182 assert_string_equal("five", node->name);
183 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
184 assert_string_equal("y", node->name);
185 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
186 assert_string_equal("seven", node->name);
187
188 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
189 assert_string_equal("one", node->name);
190
191 assert_non_null(node = lys_getnext(NULL, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
192 assert_string_equal("h-output", node->name);
193 assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
Radek Krejcib9726b22019-02-22 16:35:37 +0100194
Radek Krejcib0e49372019-02-25 13:43:21 +0100195 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; feature f;"
196 "leaf a {type string; if-feature f;}"
197 "leaf b {type string;}}", LYS_IN_YANG));
198 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
199 assert_string_equal("b", node->name);
200 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK));
201 assert_string_equal("a", node->name);
202
203
Radek Krejcib9726b22019-02-22 16:35:37 +0100204 *state = NULL;
205 ly_ctx_destroy(ctx, NULL);
206}
207
208int main(void)
209{
210 const struct CMUnitTest tests[] = {
211 cmocka_unit_test_setup_teardown(test_getnext, logger_setup, logger_teardown),
212 };
213
214 return cmocka_run_group_tests(tests, NULL, NULL);
215}