blob: b02096c7048d5d370af316bae10e32179cf6221e [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
Radek Krejcid3ca0632019-04-16 16:54:54 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
22
Michal Vasko7c8439f2020-08-05 13:25:19 +020023#include "common.h"
Radek Krejci70593c12020-06-13 20:48:09 +020024#include "context.h"
25#include "printer.h"
26#include "printer_schema.h"
Michal Vasko7c8439f2020-08-05 13:25:19 +020027#include "tree_schema.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020028
29#define BUFSIZE 1024
30char logbuf[BUFSIZE] = {0};
31int store = -1; /* negative for infinite logging, positive for limited logging */
32
33/* set to 0 to printing error messages to stderr instead of checking them in code */
34#define ENABLE_LOGGER_CHECKING 1
35
36#if ENABLE_LOGGER_CHECKING
37static void
38logger(LY_LOG_LEVEL level, const char *msg, const char *path)
39{
40 (void) level; /* unused */
41 if (store) {
42 if (path && path[0]) {
43 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
44 } else {
45 strncpy(logbuf, msg, BUFSIZE - 1);
46 }
47 if (store > 0) {
48 --store;
49 }
50 }
51}
52#endif
53
54static int
55logger_setup(void **state)
56{
57 (void) state; /* unused */
58#if ENABLE_LOGGER_CHECKING
59 ly_set_log_clb(logger, 1);
60#endif
61 return 0;
62}
63
64static int
65logger_teardown(void **state)
66{
67 (void) state; /* unused */
68#if ENABLE_LOGGER_CHECKING
69 if (*state) {
70 fprintf(stderr, "%s\n", logbuf);
71 }
72#endif
73 return 0;
74}
75
76void
77logbuf_clean(void)
78{
79 logbuf[0] = '\0';
80}
81
82#if ENABLE_LOGGER_CHECKING
83# define logbuf_assert(str) assert_string_equal(logbuf, str)
84#else
85# define logbuf_assert(str)
86#endif
87
Radek Krejcid3ca0632019-04-16 16:54:54 +020088static void
89test_module(void **state)
90{
91 *state = test_module;
92
93 struct ly_ctx *ctx = {0};
94 const struct lys_module *mod;
95 const char *orig = "module a {\n"
96 " yang-version 1.1;\n"
97 " namespace \"urn:test:a\";\n"
98 " prefix a;\n\n"
99 " import ietf-yang-types {\n"
100 " prefix yt;\n"
101 " revision-date 2013-07-15;\n"
102 " description\n"
103 " \"YANG types\";\n"
104 " reference\n"
105 " \"RFC reference\";\n"
106 " }\n\n"
107 " organization\n"
108 " \"ORG\";\n"
109 " contact\n"
110 " \"Radek Krejci.\";\n"
111 " description\n"
112 " \"Long multiline\n"
113 " description.\";\n"
114 " reference\n"
115 " \"some reference\";\n"
116 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200117 char *compiled = "module a {\n"
118 " yang-version 1.1;\n"
119 " namespace \"urn:test:a\";\n"
120 " prefix a;\n\n"
Radek Krejciffb410f2019-04-30 09:51:51 +0200121 " organization\n"
122 " \"ORG\";\n"
123 " contact\n"
124 " \"Radek Krejci.\";\n"
125 " description\n"
126 " \"Long multiline\n"
127 " description.\";\n"
128 " reference\n"
129 " \"some reference\";\n"
130 "}\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200131 char *printed;
Radek Krejci241f6b52020-05-21 18:13:49 +0200132 struct ly_out *out;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200133
Radek Krejci84ce7b12020-06-11 17:28:25 +0200134 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200135 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
136
Michal Vasko3a41dff2020-07-15 14:30:28 +0200137 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200138 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200139 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200140 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200141 ly_out_reset(out);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200142 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200143 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200144 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200145 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200146
147 orig = "module b {\n"
148 " yang-version 1.1;\n"
149 " namespace \"urn:test:b\";\n"
150 " prefix b;\n\n"
151 " revision 2019-04-16 {\n"
152 " description\n"
153 " \"text\";\n"
154 " reference\n"
155 " \"text\";\n"
156 " }\n"
157 " revision 2019-04-15 {\n"
158 " description\n"
159 " \"initial revision\";\n"
160 " }\n\n"
161 " feature f1 {\n"
162 " status current;\n"
163 " description\n"
164 " \"text\";\n"
165 " reference\n"
166 " \"text\";\n"
167 " }\n\n"
168 " feature f2 {\n"
169 " if-feature \"not f1\";\n"
170 " }\n"
171 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200172 compiled = "module b {\n"
173 " yang-version 1.1;\n"
174 " namespace \"urn:test:b\";\n"
175 " prefix b;\n\n"
176 " revision 2019-04-16;\n\n"
177 " feature f1 {\n"
178 " status current;\n"
179 " description\n"
180 " \"text\";\n"
181 " reference\n"
182 " \"text\";\n"
183 " }\n\n"
184 " feature f2 {\n"
185 " if-feature \"not f1\";\n"
186 " }\n"
187 "}\n";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200188 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200189 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200190 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200191 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200192 ly_out_reset(out);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200193 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200194 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200195 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200196 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200197
Michal Vasko7c8439f2020-08-05 13:25:19 +0200198 orig = compiled = "module c {\n"
Radek Krejcid3ca0632019-04-16 16:54:54 +0200199 " yang-version 1.1;\n"
200 " namespace \"urn:test:c\";\n"
201 " prefix c;\n\n"
202 " feature f1;\n\n"
203 " identity i1 {\n"
204 " if-feature \"f1\";\n"
205 " description\n"
206 " \"text\";\n"
207 " reference\n"
208 " \"text32\";\n"
209 " }\n\n"
210 " identity i2 {\n"
211 " base i1;\n"
212 " status obsolete;\n"
213 " }\n"
214 "}\n";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200215 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200216 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200217 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200218 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200219 ly_out_reset(out);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200220 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200221 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200222 assert_string_equal(printed, compiled);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200223
224 *state = NULL;
Radek Krejci241f6b52020-05-21 18:13:49 +0200225 ly_out_free(out, NULL, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200226 ly_ctx_destroy(ctx, NULL);
227}
228
Michal Vasko7c8439f2020-08-05 13:25:19 +0200229static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
230 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
231 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
232{
233 *module_data = user_data;
234 *format = LYS_IN_YANG;
235 *free_module_data = NULL;
236 return LY_SUCCESS;
237}
238
239static void
240test_submodule(void **state)
241{
242 *state = test_submodule;
243
244 struct ly_ctx *ctx = {0};
245 const struct lys_module *mod;
246 const char *mod_yang = "module a {\n"
247 " yang-version 1.1;\n"
248 " namespace \"urn:test:a\";\n"
249 " prefix a;\n\n"
250 " include a-sub;\n"
251 "}\n";
252 char *submod_yang = "submodule a-sub {\n"
253 " yang-version 1.1;\n"
254 " belongs-to a {\n"
255 " prefix a;\n"
256 " }\n\n"
257 " import ietf-yang-types {\n"
258 " prefix yt;\n"
259 " revision-date 2013-07-15;\n"
260 " }\n\n"
261 " organization\n"
262 " \"ORG\";\n"
263 " contact\n"
264 " \"Radek Krejci.\";\n"
265 " description\n"
266 " \"Long multiline\n"
267 " description.\";\n"
268 " reference\n"
269 " \"some reference\";\n"
270 "}\n";
271 char *printed;
272 struct ly_out *out;
273
274 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
275 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
276
277 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, submod_yang);
278 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, mod_yang, LYS_IN_YANG, &mod));
279 assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod, mod->parsed->includes[0].submodule, LYS_OUT_YANG, 0, 0));
280 assert_int_equal(strlen(submod_yang), ly_out_printed(out));
281 assert_string_equal(printed, submod_yang);
282
283 *state = NULL;
284 ly_out_free(out, NULL, 1);
285 ly_ctx_destroy(ctx, NULL);
286}
Radek Krejcid3ca0632019-04-16 16:54:54 +0200287
288int main(void)
289{
290 const struct CMUnitTest tests[] = {
291 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Michal Vasko7c8439f2020-08-05 13:25:19 +0200292 cmocka_unit_test_setup_teardown(test_submodule, logger_setup, logger_teardown),
Radek Krejcid3ca0632019-04-16 16:54:54 +0200293 };
294
295 return cmocka_run_group_tests(tests, NULL, NULL);
296}