blob: 4296c0594ae30b847de2de325f00823103b409db [file] [log] [blame]
Radek Krejcid3ca0632019-04-16 16:54:54 +02001/*
2 * @file test_printer_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from printer_yang.c
5 *
6 * Copyright (c) 2019 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
Michal Vasko7c8439f2020-08-05 13:25:19 +020015#include "common.h"
Radek Krejci70593c12020-06-13 20:48:09 +020016#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020017#include "out.h"
Radek Krejci70593c12020-06-13 20:48:09 +020018#include "printer_schema.h"
Michal Vasko7c8439f2020-08-05 13:25:19 +020019#include "tree_schema.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010020#include "utests.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020021
22#define BUFSIZE 1024
23char logbuf[BUFSIZE] = {0};
24int store = -1; /* negative for infinite logging, positive for limited logging */
25
26/* set to 0 to printing error messages to stderr instead of checking them in code */
27#define ENABLE_LOGGER_CHECKING 1
28
29#if ENABLE_LOGGER_CHECKING
30static void
31logger(LY_LOG_LEVEL level, const char *msg, const char *path)
32{
33 (void) level; /* unused */
34 if (store) {
35 if (path && path[0]) {
36 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
37 } else {
38 strncpy(logbuf, msg, BUFSIZE - 1);
39 }
40 if (store > 0) {
41 --store;
42 }
43 }
44}
Radek Krejcib4ac5a92020-11-23 17:54:33 +010045
Radek Krejcid3ca0632019-04-16 16:54:54 +020046#endif
47
48static int
49logger_setup(void **state)
50{
51 (void) state; /* unused */
52#if ENABLE_LOGGER_CHECKING
53 ly_set_log_clb(logger, 1);
54#endif
55 return 0;
56}
57
58static int
59logger_teardown(void **state)
60{
61 (void) state; /* unused */
62#if ENABLE_LOGGER_CHECKING
63 if (*state) {
64 fprintf(stderr, "%s\n", logbuf);
65 }
66#endif
67 return 0;
68}
69
70void
71logbuf_clean(void)
72{
73 logbuf[0] = '\0';
74}
75
76#if ENABLE_LOGGER_CHECKING
77# define logbuf_assert(str) assert_string_equal(logbuf, str)
78#else
79# define logbuf_assert(str)
80#endif
81
Radek Krejcid3ca0632019-04-16 16:54:54 +020082static void
83test_module(void **state)
84{
85 *state = test_module;
86
87 struct ly_ctx *ctx = {0};
88 const struct lys_module *mod;
89 const char *orig = "module a {\n"
90 " yang-version 1.1;\n"
91 " namespace \"urn:test:a\";\n"
92 " prefix a;\n\n"
93 " import ietf-yang-types {\n"
94 " prefix yt;\n"
95 " revision-date 2013-07-15;\n"
96 " description\n"
97 " \"YANG types\";\n"
98 " reference\n"
99 " \"RFC reference\";\n"
100 " }\n\n"
101 " organization\n"
102 " \"ORG\";\n"
103 " contact\n"
104 " \"Radek Krejci.\";\n"
105 " description\n"
106 " \"Long multiline\n"
107 " description.\";\n"
108 " reference\n"
109 " \"some reference\";\n"
110 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200111 char *compiled = "module a {\n"
Radek Krejciffb410f2019-04-30 09:51:51 +0200112 " namespace \"urn:test:a\";\n"
113 " prefix a;\n\n"
Radek Krejciffb410f2019-04-30 09:51:51 +0200114 " organization\n"
115 " \"ORG\";\n"
116 " contact\n"
117 " \"Radek Krejci.\";\n"
118 " description\n"
119 " \"Long multiline\n"
120 " description.\";\n"
121 " reference\n"
122 " \"some reference\";\n"
123 "}\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200124 char *printed;
Radek Krejci241f6b52020-05-21 18:13:49 +0200125 struct ly_out *out;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200126
Radek Krejci84ce7b12020-06-11 17:28:25 +0200127 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200128 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
129
Michal Vasko3a41dff2020-07-15 14:30:28 +0200130 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200131 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200132 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200133 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200134 ly_out_reset(out);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200135 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200136 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200137 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200138 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200139
140 orig = "module b {\n"
141 " yang-version 1.1;\n"
142 " namespace \"urn:test:b\";\n"
143 " prefix b;\n\n"
144 " revision 2019-04-16 {\n"
145 " description\n"
146 " \"text\";\n"
147 " reference\n"
148 " \"text\";\n"
149 " }\n"
150 " revision 2019-04-15 {\n"
151 " description\n"
152 " \"initial revision\";\n"
153 " }\n\n"
154 " feature f1 {\n"
155 " status current;\n"
156 " description\n"
157 " \"text\";\n"
158 " reference\n"
159 " \"text\";\n"
160 " }\n\n"
161 " feature f2 {\n"
162 " if-feature \"not f1\";\n"
163 " }\n"
164 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200165 compiled = "module b {\n"
Radek Krejciffb410f2019-04-30 09:51:51 +0200166 " namespace \"urn:test:b\";\n"
167 " prefix b;\n\n"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100168 " revision 2019-04-16;\n"
Radek Krejciffb410f2019-04-30 09:51:51 +0200169 "}\n";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200170 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200171 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200172 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200173 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200174 ly_out_reset(out);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200175 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200176 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200177 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200178 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200179
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100180 orig = "module c {\n"
Radek Krejcid3ca0632019-04-16 16:54:54 +0200181 " yang-version 1.1;\n"
182 " namespace \"urn:test:c\";\n"
183 " prefix c;\n\n"
184 " feature f1;\n\n"
185 " identity i1 {\n"
186 " if-feature \"f1\";\n"
187 " description\n"
188 " \"text\";\n"
189 " reference\n"
190 " \"text32\";\n"
191 " }\n\n"
192 " identity i2 {\n"
193 " base i1;\n"
194 " status obsolete;\n"
195 " }\n"
196 "}\n";
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100197 compiled = "module c {\n"
198 " namespace \"urn:test:c\";\n"
199 " prefix c;\n"
200 "}\n";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200201 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200202 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200203 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200204 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200205 ly_out_reset(out);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100206 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200207 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200208 assert_string_equal(printed, compiled);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200209
210 *state = NULL;
Radek Krejci241f6b52020-05-21 18:13:49 +0200211 ly_out_free(out, NULL, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200212 ly_ctx_destroy(ctx, NULL);
213}
214
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100215static LY_ERR
216test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
217 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
218 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
Michal Vasko7c8439f2020-08-05 13:25:19 +0200219{
220 *module_data = user_data;
221 *format = LYS_IN_YANG;
222 *free_module_data = NULL;
223 return LY_SUCCESS;
224}
225
226static void
227test_submodule(void **state)
228{
229 *state = test_submodule;
230
231 struct ly_ctx *ctx = {0};
232 const struct lys_module *mod;
233 const char *mod_yang = "module a {\n"
234 " yang-version 1.1;\n"
235 " namespace \"urn:test:a\";\n"
236 " prefix a;\n\n"
237 " include a-sub;\n"
238 "}\n";
239 char *submod_yang = "submodule a-sub {\n"
240 " yang-version 1.1;\n"
241 " belongs-to a {\n"
242 " prefix a;\n"
243 " }\n\n"
244 " import ietf-yang-types {\n"
245 " prefix yt;\n"
246 " revision-date 2013-07-15;\n"
247 " }\n\n"
248 " organization\n"
249 " \"ORG\";\n"
250 " contact\n"
251 " \"Radek Krejci.\";\n"
252 " description\n"
253 " \"Long multiline\n"
254 " description.\";\n"
255 " reference\n"
256 " \"some reference\";\n"
257 "}\n";
258 char *printed;
259 struct ly_out *out;
260
261 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
262 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
263
264 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, submod_yang);
265 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, mod_yang, LYS_IN_YANG, &mod));
266 assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod, mod->parsed->includes[0].submodule, LYS_OUT_YANG, 0, 0));
267 assert_int_equal(strlen(submod_yang), ly_out_printed(out));
268 assert_string_equal(printed, submod_yang);
269
270 *state = NULL;
271 ly_out_free(out, NULL, 1);
272 ly_ctx_destroy(ctx, NULL);
273}
Radek Krejcid3ca0632019-04-16 16:54:54 +0200274
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100275int
276main(void)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200277{
278 const struct CMUnitTest tests[] = {
279 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Michal Vasko7c8439f2020-08-05 13:25:19 +0200280 cmocka_unit_test_setup_teardown(test_submodule, logger_setup, logger_teardown),
Radek Krejcid3ca0632019-04-16 16:54:54 +0200281 };
282
283 return cmocka_run_group_tests(tests, NULL, NULL);
284}