blob: bcee5814d9ac26531382a4b24f122d4718726644 [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
Radek Krejcib9726b22019-02-22 16:35:37 +010015#include <stdarg.h>
16#include <stddef.h>
17#include <stdio.h>
18#include <setjmp.h>
19#include <cmocka.h>
20
Radek Krejci2d7a47b2019-05-16 13:34:10 +020021#include "../../src/common.h"
22#include "../../src/tree_schema.h"
Radek Krejcib9726b22019-02-22 16:35:37 +010023
24#define BUFSIZE 1024
25char logbuf[BUFSIZE] = {0};
26int store = -1; /* negative for infinite logging, positive for limited logging */
27
28/* set to 0 to printing error messages to stderr instead of checking them in code */
29#define ENABLE_LOGGER_CHECKING 1
30
31#if ENABLE_LOGGER_CHECKING
32static void
33logger(LY_LOG_LEVEL level, const char *msg, const char *path)
34{
35 (void) level; /* unused */
36 if (store) {
37 if (path && path[0]) {
38 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
39 } else {
40 strncpy(logbuf, msg, BUFSIZE - 1);
41 }
42 if (store > 0) {
43 --store;
44 }
45 }
46}
47#endif
48
49static int
50logger_setup(void **state)
51{
52 (void) state; /* unused */
53
54 ly_set_log_clb(logger, 0);
55
56 return 0;
57}
58
59static int
60logger_teardown(void **state)
61{
62 (void) state; /* unused */
63#if ENABLE_LOGGER_CHECKING
64 if (*state) {
65 fprintf(stderr, "%s\n", logbuf);
66 }
67#endif
68 return 0;
69}
70
71void
72logbuf_clean(void)
73{
74 logbuf[0] = '\0';
75}
76
77#if ENABLE_LOGGER_CHECKING
78# define logbuf_assert(str) assert_string_equal(logbuf, str)
79#else
80# define logbuf_assert(str)
81#endif
82
83static void
84test_getnext(void **state)
85{
86 *state = test_getnext;
87
88 struct ly_ctx *ctx;
89 struct lys_module *mod;
Radek Krejci05b774b2019-02-25 13:26:18 +010090 const struct lysc_node *node = NULL, *four;
91 const struct lysc_node_container *cont;
92 const struct lysc_action *rpc;
93
Radek Krejcib9726b22019-02-22 16:35:37 +010094 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
95
96 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;"
97 "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}"
98 " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}"
99 " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
100 " notification nine {leaf nine-data {type string;}}}"
101 "leaf b {type string;} leaf-list c {type string;} list d {config false;}"
102 "choice x { leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
103 "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
Radek Krejcib0e49372019-02-25 13:43:21 +0100104 "rpc i;"
105 "notification j {leaf i-data {type string;}}"
106 "notification k;}", LYS_IN_YANG));
Radek Krejci05b774b2019-02-25 13:26:18 +0100107 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
108 assert_string_equal("a", node->name);
109 cont = (const struct lysc_node_container*)node;
110 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
111 assert_string_equal("b", node->name);
112 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
113 assert_string_equal("c", node->name);
114 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
115 assert_string_equal("d", node->name);
116 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
117 assert_string_equal("e", node->name);
118 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
119 assert_string_equal("f", node->name);
120 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
121 assert_string_equal("g", node->name);
122 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
123 assert_string_equal("h", node->name);
124 rpc = (const struct lysc_action*)node;
Radek Krejci05b774b2019-02-25 13:26:18 +0100125 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
126 assert_string_equal("i", node->name);
Radek Krejcib0e49372019-02-25 13:43:21 +0100127 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
128 assert_string_equal("j", node->name);
129 assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0));
130 assert_string_equal("k", node->name);
Radek Krejci05b774b2019-02-25 13:26:18 +0100131 assert_null(node = lys_getnext(node, NULL, mod->compiled, 0));
132 /* Inside container */
133 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
134 assert_string_equal("one", node->name);
135 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
136 assert_string_equal("two", node->name);
137 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
138 assert_string_equal("three", node->name);
139 assert_non_null(node = four = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
140 assert_string_equal("four", node->name);
141 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
142 assert_string_equal("five", node->name);
143 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
144 assert_string_equal("six", node->name);
145 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
146 assert_string_equal("seven", node->name);
147 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
148 assert_string_equal("eight", node->name);
Radek Krejci05b774b2019-02-25 13:26:18 +0100149 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
150 assert_string_equal("nine", node->name);
Radek Krejci05b774b2019-02-25 13:26:18 +0100151 assert_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0));
152 /* Inside RPC */
153 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0));
154 assert_string_equal("h-input", node->name);
155 assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0));
156
157 /* options */
158 assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
159 assert_string_equal("x", node->name);
160 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE));
161 assert_string_equal("seven", node->name);
162
163 assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_NOCHOICE));
164 assert_string_equal("seven", node->name);
165
166 assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
167 assert_string_equal("five", node->name);
168 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
169 assert_string_equal("y", node->name);
170 assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE));
171 assert_string_equal("seven", node->name);
172
173 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT));
174 assert_string_equal("one", node->name);
175
176 assert_non_null(node = lys_getnext(NULL, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
177 assert_string_equal("h-output", node->name);
178 assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT));
Radek Krejcib9726b22019-02-22 16:35:37 +0100179
Radek Krejcib0e49372019-02-25 13:43:21 +0100180 assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; feature f;"
181 "leaf a {type string; if-feature f;}"
182 "leaf b {type string;}}", LYS_IN_YANG));
183 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
184 assert_string_equal("b", node->name);
185 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK));
186 assert_string_equal("a", node->name);
187
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200188 assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; rpc c;}", LYS_IN_YANG));
189 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
190 assert_string_equal("c", node->name);
191 assert_null(node = lys_getnext(node, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK));
192
193 assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; notification d;}", LYS_IN_YANG));
194 assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0));
195 assert_string_equal("d", node->name);
196 assert_null(node = lys_getnext(node, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK));
Radek Krejcib0e49372019-02-25 13:43:21 +0100197
Radek Krejcib9726b22019-02-22 16:35:37 +0100198 *state = NULL;
199 ly_ctx_destroy(ctx, NULL);
200}
201
202int main(void)
203{
204 const struct CMUnitTest tests[] = {
205 cmocka_unit_test_setup_teardown(test_getnext, logger_setup, logger_teardown),
206 };
207
208 return cmocka_run_group_tests(tests, NULL, NULL);
209}