blob: b59e6b2710ea7fc2850e91ecec955947baba301c [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"
Michal Vaskoafac7822020-10-20 14:22:26 +020025#include "out.h"
Radek Krejci70593c12020-06-13 20:48:09 +020026#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"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100174 " revision 2019-04-16;\n"
Radek Krejciffb410f2019-04-30 09:51:51 +0200175 "}\n";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200176 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200177 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200178 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200179 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200180 ly_out_reset(out);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200181 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200182 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200183 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200184 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200185
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100186 orig = "module c {\n"
Radek Krejcid3ca0632019-04-16 16:54:54 +0200187 " yang-version 1.1;\n"
188 " namespace \"urn:test:c\";\n"
189 " prefix c;\n\n"
190 " feature f1;\n\n"
191 " identity i1 {\n"
192 " if-feature \"f1\";\n"
193 " description\n"
194 " \"text\";\n"
195 " reference\n"
196 " \"text32\";\n"
197 " }\n\n"
198 " identity i2 {\n"
199 " base i1;\n"
200 " status obsolete;\n"
201 " }\n"
202 "}\n";
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100203 compiled = "module c {\n"
204 " namespace \"urn:test:c\";\n"
205 " prefix c;\n"
206 "}\n";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200207 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200208 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200209 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200210 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200211 ly_out_reset(out);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100212 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200213 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200214 assert_string_equal(printed, compiled);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200215
216 *state = NULL;
Radek Krejci241f6b52020-05-21 18:13:49 +0200217 ly_out_free(out, NULL, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200218 ly_ctx_destroy(ctx, NULL);
219}
220
Michal Vasko7c8439f2020-08-05 13:25:19 +0200221static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
222 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
223 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
224{
225 *module_data = user_data;
226 *format = LYS_IN_YANG;
227 *free_module_data = NULL;
228 return LY_SUCCESS;
229}
230
231static void
232test_submodule(void **state)
233{
234 *state = test_submodule;
235
236 struct ly_ctx *ctx = {0};
237 const struct lys_module *mod;
238 const char *mod_yang = "module a {\n"
239 " yang-version 1.1;\n"
240 " namespace \"urn:test:a\";\n"
241 " prefix a;\n\n"
242 " include a-sub;\n"
243 "}\n";
244 char *submod_yang = "submodule a-sub {\n"
245 " yang-version 1.1;\n"
246 " belongs-to a {\n"
247 " prefix a;\n"
248 " }\n\n"
249 " import ietf-yang-types {\n"
250 " prefix yt;\n"
251 " revision-date 2013-07-15;\n"
252 " }\n\n"
253 " organization\n"
254 " \"ORG\";\n"
255 " contact\n"
256 " \"Radek Krejci.\";\n"
257 " description\n"
258 " \"Long multiline\n"
259 " description.\";\n"
260 " reference\n"
261 " \"some reference\";\n"
262 "}\n";
263 char *printed;
264 struct ly_out *out;
265
266 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
267 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
268
269 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, submod_yang);
270 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, mod_yang, LYS_IN_YANG, &mod));
271 assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod, mod->parsed->includes[0].submodule, LYS_OUT_YANG, 0, 0));
272 assert_int_equal(strlen(submod_yang), ly_out_printed(out));
273 assert_string_equal(printed, submod_yang);
274
275 *state = NULL;
276 ly_out_free(out, NULL, 1);
277 ly_ctx_destroy(ctx, NULL);
278}
Radek Krejcid3ca0632019-04-16 16:54:54 +0200279
280int main(void)
281{
282 const struct CMUnitTest tests[] = {
283 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Michal Vasko7c8439f2020-08-05 13:25:19 +0200284 cmocka_unit_test_setup_teardown(test_submodule, logger_setup, logger_teardown),
Radek Krejcid3ca0632019-04-16 16:54:54 +0200285 };
286
287 return cmocka_run_group_tests(tests, NULL, NULL);
288}