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