blob: eb35cff6193fb05b22c49e6a1de9eb3772325cbe [file] [log] [blame]
Michal Vasko57c10cd2020-05-27 15:57:11 +02001/**
2 * @file test_yanglib.c
3 * @author: Michal Vasko <mvasko@cesnet.cz>
4 * @brief unit tests for ietf-yang-library data
5 *
6 * Copyright (c) 2020 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 "tests/config.h"
16
17#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21
22#include <stdio.h>
23#include <string.h>
24
25#include "../../src/context.h"
26#include "../../src/tree_schema.h"
27
28#define BUFSIZE 1024
29char logbuf[BUFSIZE] = {0};
30int store = -1; /* negative for infinite logging, positive for limited logging */
31
32struct ly_ctx *ctx; /* context for tests */
33
34/* set to 0 to printing error messages to stderr instead of checking them in code */
35#define ENABLE_LOGGER_CHECKING 1
36
37#if ENABLE_LOGGER_CHECKING
38static void
39logger(LY_LOG_LEVEL level, const char *msg, const char *path)
40{
41 (void) level; /* unused */
42 if (store) {
43 if (path && path[0]) {
44 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
45 } else {
46 strncpy(logbuf, msg, BUFSIZE - 1);
47 }
48 if (store > 0) {
49 --store;
50 }
51 }
52}
53#endif
54
55static LY_ERR
56test_imp_clb(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev, void *user_data,
57 LYS_INFORMAT *format, const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
58{
59 const char *schema_a_sub =
60 "submodule a_sub {"
61 "belongs-to a {"
62 "prefix a;"
63 "}"
64 "yang-version 1.1;"
65
66 "feature feat1;"
67
68 "list l3 {"
69 "key \"a\";"
70 "leaf a {"
71 "type uint16;"
72 "}"
73 "leaf b {"
74 "type uint16;"
75 "}"
76 "}"
77 "}";
78
79 assert_string_equal(mod_name, "a");
80 assert_null(mod_rev);
81 if (!submod_name) {
82 return LY_ENOTFOUND;
83 }
84 assert_string_equal(submod_name, "a_sub");
85 assert_null(sub_rev);
86 assert_null(user_data);
87
88 *format = LYS_IN_YANG;
89 *module_data = schema_a_sub;
90 *free_module_data = NULL;
91 return LY_SUCCESS;
92}
93
94static int
95setup(void **state)
96{
97 (void) state; /* unused */
98
99 const char *schema_a =
100 "module a {"
101 "namespace urn:tests:a;"
102 "prefix a;"
103 "yang-version 1.1;"
104
105 "include a_sub;"
106
107 "list l2 {"
108 "key \"a\";"
109 "leaf a {"
110 "type uint16;"
111 "}"
112 "leaf b {"
113 "type uint16;"
114 "}"
115 "}"
116 "}";
117 const char *schema_b =
118 "module b {"
119 "namespace urn:tests:b;"
120 "prefix b;"
121 "yang-version 1.1;"
122
123 "import a {"
124 "prefix a;"
125 "}"
126
127 "deviation /a:l2 {"
128 "deviate add {"
129 "max-elements 40;"
130 "}"
131 "}"
132
133 "leaf foo {"
134 "type string;"
135 "}"
136 "}";
137 const struct lys_module *mod;
138
139#if ENABLE_LOGGER_CHECKING
140 ly_set_log_clb(logger, 1);
141#endif
142
143 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx));
144 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, NULL);
145
146 mod = lys_parse_mem(ctx, schema_a, LYS_IN_YANG);
147 assert_non_null(mod);
148 assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1"));
149 assert_non_null(lys_parse_mem(ctx, schema_b, LYS_IN_YANG));
150
151 return 0;
152}
153
154static int
155teardown(void **state)
156{
157#if ENABLE_LOGGER_CHECKING
158 if (*state) {
159 fprintf(stderr, "%s\n", logbuf);
160 }
161#else
162 (void) state; /* unused */
163#endif
164
165 ly_ctx_destroy(ctx, NULL);
166 ctx = NULL;
167
168 return 0;
169}
170
171void
172logbuf_clean(void)
173{
174 logbuf[0] = '\0';
175}
176
177#if ENABLE_LOGGER_CHECKING
178# define logbuf_assert(str) assert_string_equal(logbuf, str)
179#else
180# define logbuf_assert(str)
181#endif
182
183static void
184test_yanglib(void **state)
185{
186 *state = test_yanglib;
187
188 struct lyd_node *tree;
189 struct ly_set *set;
190 LY_ERR ret;
191
192 tree = ly_ctx_get_yanglib_data(ctx);
193 assert_non_null(tree);
194
195 /* make sure there is "a" with a submodule and deviation */
196 ret = lyd_find_xpath(tree, "/ietf-yang-library:yang-library/module-set/module[name='a'][submodule/name='a_sub']"
197 "[feature='feat1'][deviation='b']", &set);
198 assert_int_equal(ret, LY_SUCCESS);
199
200 assert_int_equal(set->count, 1);
201 ly_set_free(set, NULL);
202
203 lyd_free_all(tree);
204 *state = NULL;
205}
206
207int main(void)
208{
209 const struct CMUnitTest tests[] = {
210 cmocka_unit_test_setup_teardown(test_yanglib, setup, teardown),
211 };
212
213 return cmocka_run_group_tests(tests, NULL, NULL);
214}