blob: 7d70e186d18123858b6c6d891d96197e210eb3c7 [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"
Radek Krejciffb410f2019-04-30 09:51:51 +0200118 " namespace \"urn:test:a\";\n"
119 " prefix a;\n\n"
Radek Krejciffb410f2019-04-30 09:51:51 +0200120 " organization\n"
121 " \"ORG\";\n"
122 " contact\n"
123 " \"Radek Krejci.\";\n"
124 " description\n"
125 " \"Long multiline\n"
126 " description.\";\n"
127 " reference\n"
128 " \"some reference\";\n"
129 "}\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200130 char *printed;
Radek Krejci241f6b52020-05-21 18:13:49 +0200131 struct ly_out *out;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200132
Radek Krejci84ce7b12020-06-11 17:28:25 +0200133 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200134 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
135
Michal Vasko3a41dff2020-07-15 14:30:28 +0200136 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200137 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200138 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200139 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200140 ly_out_reset(out);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200141 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200142 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200143 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200144 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200145
146 orig = "module b {\n"
147 " yang-version 1.1;\n"
148 " namespace \"urn:test:b\";\n"
149 " prefix b;\n\n"
150 " revision 2019-04-16 {\n"
151 " description\n"
152 " \"text\";\n"
153 " reference\n"
154 " \"text\";\n"
155 " }\n"
156 " revision 2019-04-15 {\n"
157 " description\n"
158 " \"initial revision\";\n"
159 " }\n\n"
160 " feature f1 {\n"
161 " status current;\n"
162 " description\n"
163 " \"text\";\n"
164 " reference\n"
165 " \"text\";\n"
166 " }\n\n"
167 " feature f2 {\n"
168 " if-feature \"not f1\";\n"
169 " }\n"
170 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200171 compiled = "module b {\n"
Radek Krejciffb410f2019-04-30 09:51:51 +0200172 " namespace \"urn:test:b\";\n"
173 " prefix b;\n\n"
174 " revision 2019-04-16;\n\n"
175 " feature f1 {\n"
176 " status current;\n"
177 " description\n"
178 " \"text\";\n"
179 " reference\n"
180 " \"text\";\n"
181 " }\n\n"
182 " feature f2 {\n"
183 " if-feature \"not f1\";\n"
184 " }\n"
185 "}\n";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200186 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200187 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200188 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200189 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200190 ly_out_reset(out);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200191 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200192 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200193 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200194 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200195
Michal Vasko7c8439f2020-08-05 13:25:19 +0200196 orig = compiled = "module c {\n"
Radek Krejcid3ca0632019-04-16 16:54:54 +0200197 " yang-version 1.1;\n"
198 " namespace \"urn:test:c\";\n"
199 " prefix c;\n\n"
200 " feature f1;\n\n"
201 " identity i1 {\n"
202 " if-feature \"f1\";\n"
203 " description\n"
204 " \"text\";\n"
205 " reference\n"
206 " \"text32\";\n"
207 " }\n\n"
208 " identity i2 {\n"
209 " base i1;\n"
210 " status obsolete;\n"
211 " }\n"
212 "}\n";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200213 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200214 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200215 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200216 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200217 ly_out_reset(out);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200218 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200219 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200220 assert_string_equal(printed, compiled);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200221
222 *state = NULL;
Radek Krejci241f6b52020-05-21 18:13:49 +0200223 ly_out_free(out, NULL, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200224 ly_ctx_destroy(ctx, NULL);
225}
226
Michal Vasko7c8439f2020-08-05 13:25:19 +0200227static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
228 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
229 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
230{
231 *module_data = user_data;
232 *format = LYS_IN_YANG;
233 *free_module_data = NULL;
234 return LY_SUCCESS;
235}
236
237static void
238test_submodule(void **state)
239{
240 *state = test_submodule;
241
242 struct ly_ctx *ctx = {0};
243 const struct lys_module *mod;
244 const char *mod_yang = "module a {\n"
245 " yang-version 1.1;\n"
246 " namespace \"urn:test:a\";\n"
247 " prefix a;\n\n"
248 " include a-sub;\n"
249 "}\n";
250 char *submod_yang = "submodule a-sub {\n"
251 " yang-version 1.1;\n"
252 " belongs-to a {\n"
253 " prefix a;\n"
254 " }\n\n"
255 " import ietf-yang-types {\n"
256 " prefix yt;\n"
257 " revision-date 2013-07-15;\n"
258 " }\n\n"
259 " organization\n"
260 " \"ORG\";\n"
261 " contact\n"
262 " \"Radek Krejci.\";\n"
263 " description\n"
264 " \"Long multiline\n"
265 " description.\";\n"
266 " reference\n"
267 " \"some reference\";\n"
268 "}\n";
269 char *printed;
270 struct ly_out *out;
271
272 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
273 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
274
275 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, submod_yang);
276 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, mod_yang, LYS_IN_YANG, &mod));
277 assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod, mod->parsed->includes[0].submodule, LYS_OUT_YANG, 0, 0));
278 assert_int_equal(strlen(submod_yang), ly_out_printed(out));
279 assert_string_equal(printed, submod_yang);
280
281 *state = NULL;
282 ly_out_free(out, NULL, 1);
283 ly_ctx_destroy(ctx, NULL);
284}
Radek Krejcid3ca0632019-04-16 16:54:54 +0200285
286int main(void)
287{
288 const struct CMUnitTest tests[] = {
289 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Michal Vasko7c8439f2020-08-05 13:25:19 +0200290 cmocka_unit_test_setup_teardown(test_submodule, logger_setup, logger_teardown),
Radek Krejcid3ca0632019-04-16 16:54:54 +0200291 };
292
293 return cmocka_run_group_tests(tests, NULL, NULL);
294}