blob: c7f3023a2a1cb0eaa746c1492a5deecaa008dc64 [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"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "../../src/plugins_types.c"
Radek Krejcib9726b22019-02-22 16:35:37 +010024#include "../../src/hash_table.c"
25#include "../../src/xpath.c"
26#include "../../src/context.c"
27
28#include <stdarg.h>
29#include <stddef.h>
30#include <stdio.h>
31#include <setjmp.h>
32#include <cmocka.h>
33
34#include "libyang.h"
35
36#define BUFSIZE 1024
37char logbuf[BUFSIZE] = {0};
38int store = -1; /* negative for infinite logging, positive for limited logging */
39
40/* set to 0 to printing error messages to stderr instead of checking them in code */
41#define ENABLE_LOGGER_CHECKING 1
42
43#if ENABLE_LOGGER_CHECKING
44static void
45logger(LY_LOG_LEVEL level, const char *msg, const char *path)
46{
47 (void) level; /* unused */
48 if (store) {
49 if (path && path[0]) {
50 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
51 } else {
52 strncpy(logbuf, msg, BUFSIZE - 1);
53 }
54 if (store > 0) {
55 --store;
56 }
57 }
58}
59#endif
60
61static int
62logger_setup(void **state)
63{
64 (void) state; /* unused */
65
66 ly_set_log_clb(logger, 0);
67
68 return 0;
69}
70
71static int
72logger_teardown(void **state)
73{
74 (void) state; /* unused */
75#if ENABLE_LOGGER_CHECKING
76 if (*state) {
77 fprintf(stderr, "%s\n", logbuf);
78 }
79#endif
80 return 0;
81}
82
83void
84logbuf_clean(void)
85{
86 logbuf[0] = '\0';
87}
88
89#if ENABLE_LOGGER_CHECKING
90# define logbuf_assert(str) assert_string_equal(logbuf, str)
91#else
92# define logbuf_assert(str)
93#endif
94
95static void
96test_getnext(void **state)
97{
98 *state = test_getnext;
99
100 struct ly_ctx *ctx;
101 struct lys_module *mod;
Radek Krejci05b774b2019-02-25 13:26:18 +0100102 const struct lysc_node *node = NULL, *four;
103 const struct lysc_node_container *cont;
104 const struct lysc_action *rpc;
105
Radek Krejcib9726b22019-02-22 16:35:37 +0100106 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
107
108 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;"
109 "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}"
110 " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}"
111 " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
112 " notification nine {leaf nine-data {type string;}}}"
113 "leaf b {type string;} leaf-list c {type string;} list d {config false;}"
114 "choice x { leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
115 "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
Radek Krejcib0e49372019-02-25 13:43:21 +0100116 "rpc i;"
117 "notification j {leaf i-data {type string;}}"
118 "notification k;}", LYS_IN_YANG));
Radek Krejci05b774b2019-02-25 13:26:18 +0100119 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
120 assert_string_equal("a", node->name);
121 cont = (const struct lysc_node_container*)node;
122 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
123 assert_string_equal("b", node->name);
124 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
125 assert_string_equal("c", node->name);
126 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
127 assert_string_equal("d", node->name);
128 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
129 assert_string_equal("e", node->name);
130 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
131 assert_string_equal("f", node->name);
132 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
133 assert_string_equal("g", node->name);
134 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
135 assert_string_equal("h", node->name);
136 rpc = (const struct lysc_action*)node;
Radek Krejci05b774b2019-02-25 13:26:18 +0100137 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
138 assert_string_equal("i", node->name);
Radek Krejcib0e49372019-02-25 13:43:21 +0100139 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 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
144 /* Inside container */
145 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
146 assert_string_equal("one", node->name);
147 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
148 assert_string_equal("two", node->name);
149 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
150 assert_string_equal("three", node->name);
151 assert_non_null(node = four = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
152 assert_string_equal("four", node->name);
153 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
154 assert_string_equal("five", node->name);
155 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
156 assert_string_equal("six", node->name);
157 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
158 assert_string_equal("seven", node->name);
159 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
160 assert_string_equal("eight", node->name);
Radek Krejci05b774b2019-02-25 13:26:18 +0100161 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
162 assert_string_equal("nine", node->name);
Radek Krejci05b774b2019-02-25 13:26:18 +0100163 assert_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
164 /* Inside RPC */
165 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0));
166 assert_string_equal("h-input", node->name);
167 assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0));
168
169 /* options */
170 assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
171 assert_string_equal("x", node->name);
172 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
173 assert_string_equal("seven", node->name);
174
175 assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_NOCHOICE));
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_WITHCASE));
179 assert_string_equal("five", node->name);
180 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
181 assert_string_equal("y", node->name);
182 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
183 assert_string_equal("seven", node->name);
184
185 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
186 assert_string_equal("one", node->name);
187
188 assert_non_null(node = lys_getnext(NULL, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
189 assert_string_equal("h-output", node->name);
190 assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
Radek Krejcib9726b22019-02-22 16:35:37 +0100191
Radek Krejcib0e49372019-02-25 13:43:21 +0100192 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; feature f;"
193 "leaf a {type string; if-feature f;}"
194 "leaf b {type string;}}", LYS_IN_YANG));
195 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
196 assert_string_equal("b", node->name);
197 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK));
198 assert_string_equal("a", node->name);
199
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200200 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; rpc c;}", LYS_IN_YANG));
201 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
202 assert_string_equal("c", node->name);
203 assert_null(node = lys_getnext(node, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK));
204
205 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; notification d;}", LYS_IN_YANG));
206 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
207 assert_string_equal("d", node->name);
208 assert_null(node = lys_getnext(node, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK));
Radek Krejcib0e49372019-02-25 13:43:21 +0100209
Radek Krejcib9726b22019-02-22 16:35:37 +0100210 *state = NULL;
211 ly_ctx_destroy(ctx, NULL);
212}
213
214int main(void)
215{
216 const struct CMUnitTest tests[] = {
217 cmocka_unit_test_setup_teardown(test_getnext, logger_setup, logger_teardown),
218 };
219
220 return cmocka_run_group_tests(tests, NULL, NULL);
221}