blob: d40df7fd6480a2b55d566b2dc34ce79ab80d70d2 [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 *
Radek Iša56ca9e42020-09-08 18:42:00 +02006 * Copyright (c) 2019-2020 CESNET, z.s.p.o.
Radek Krejcid3ca0632019-04-16 16:54:54 +02007 *
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 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020016
Michal Vasko7c8439f2020-08-05 13:25:19 +020017#include "common.h"
Radek Krejci70593c12020-06-13 20:48:09 +020018#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020019#include "out.h"
Radek Krejci70593c12020-06-13 20:48:09 +020020#include "printer_schema.h"
Michal Vasko7c8439f2020-08-05 13:25:19 +020021#include "tree_schema.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020022
Radek Krejcid3ca0632019-04-16 16:54:54 +020023static void
24test_module(void **state)
25{
Michal Vasko4de7d072021-07-09 09:13:18 +020026 struct lys_module *mod;
Radek Krejcid3ca0632019-04-16 16:54:54 +020027 const char *orig = "module a {\n"
28 " yang-version 1.1;\n"
29 " namespace \"urn:test:a\";\n"
30 " prefix a;\n\n"
31 " import ietf-yang-types {\n"
32 " prefix yt;\n"
33 " revision-date 2013-07-15;\n"
34 " description\n"
35 " \"YANG types\";\n"
36 " reference\n"
37 " \"RFC reference\";\n"
38 " }\n\n"
39 " organization\n"
40 " \"ORG\";\n"
41 " contact\n"
42 " \"Radek Krejci.\";\n"
43 " description\n"
44 " \"Long multiline\n"
45 " description.\";\n"
46 " reference\n"
47 " \"some reference\";\n"
48 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +020049 char *compiled = "module a {\n"
Radek Krejciffb410f2019-04-30 09:51:51 +020050 " namespace \"urn:test:a\";\n"
51 " prefix a;\n\n"
Radek Krejciffb410f2019-04-30 09:51:51 +020052 " organization\n"
53 " \"ORG\";\n"
54 " contact\n"
55 " \"Radek Krejci.\";\n"
56 " description\n"
57 " \"Long multiline\n"
58 " description.\";\n"
59 " reference\n"
60 " \"some reference\";\n"
61 "}\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +020062 char *printed;
Radek Krejci241f6b52020-05-21 18:13:49 +020063 struct ly_out *out;
Radek Krejcid3ca0632019-04-16 16:54:54 +020064
Radek Krejci84ce7b12020-06-11 17:28:25 +020065 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Radek Krejcid3ca0632019-04-16 16:54:54 +020066
Radek Iša56ca9e42020-09-08 18:42:00 +020067 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
Michal Vasko7c8439f2020-08-05 13:25:19 +020068 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +020069 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +020070 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +020071 ly_out_reset(out);
Michal Vasko7c8439f2020-08-05 13:25:19 +020072 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +020073 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +020074 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +020075 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +020076
77 orig = "module b {\n"
78 " yang-version 1.1;\n"
79 " namespace \"urn:test:b\";\n"
80 " prefix b;\n\n"
81 " revision 2019-04-16 {\n"
82 " description\n"
83 " \"text\";\n"
84 " reference\n"
85 " \"text\";\n"
86 " }\n"
87 " revision 2019-04-15 {\n"
88 " description\n"
89 " \"initial revision\";\n"
90 " }\n\n"
91 " feature f1 {\n"
92 " status current;\n"
93 " description\n"
94 " \"text\";\n"
95 " reference\n"
96 " \"text\";\n"
97 " }\n\n"
98 " feature f2 {\n"
99 " if-feature \"not f1\";\n"
100 " }\n"
101 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200102 compiled = "module b {\n"
Radek Krejciffb410f2019-04-30 09:51:51 +0200103 " namespace \"urn:test:b\";\n"
104 " prefix b;\n\n"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100105 " revision 2019-04-16;\n"
Radek Krejciffb410f2019-04-30 09:51:51 +0200106 "}\n";
Radek Iša56ca9e42020-09-08 18:42:00 +0200107 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200108 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200109 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200110 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200111 ly_out_reset(out);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200112 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200113 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200114 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200115 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200116
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100117 orig = "module c {\n"
Radek Krejcid3ca0632019-04-16 16:54:54 +0200118 " yang-version 1.1;\n"
119 " namespace \"urn:test:c\";\n"
120 " prefix c;\n\n"
121 " feature f1;\n\n"
122 " identity i1 {\n"
123 " if-feature \"f1\";\n"
124 " description\n"
125 " \"text\";\n"
126 " reference\n"
127 " \"text32\";\n"
128 " }\n\n"
129 " identity i2 {\n"
130 " base i1;\n"
131 " status obsolete;\n"
132 " }\n"
133 "}\n";
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100134 compiled = "module c {\n"
135 " namespace \"urn:test:c\";\n"
136 " prefix c;\n"
aPiecekf4a0a192021-08-03 15:14:17 +0200137 "\n"
138 " identity i1 {\n"
139 " /* identity \"i1\" is disabled by if-feature(s) */\n"
140 " derived i2;\n"
141 " description\n"
142 " \"text\";\n"
143 " reference\n"
144 " \"text32\";\n"
145 " }\n"
146 "\n"
147 " identity i2 {\n"
148 " status obsolete;\n"
149 " }\n"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100150 "}\n";
Radek Iša56ca9e42020-09-08 18:42:00 +0200151 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200152 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200153 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200154 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200155 ly_out_reset(out);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100156 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200157 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200158 assert_string_equal(printed, compiled);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200159
Radek Krejci241f6b52020-05-21 18:13:49 +0200160 ly_out_free(out, NULL, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200161}
162
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100163static LY_ERR
164test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
165 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
166 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
Michal Vasko7c8439f2020-08-05 13:25:19 +0200167{
168 *module_data = user_data;
169 *format = LYS_IN_YANG;
170 *free_module_data = NULL;
171 return LY_SUCCESS;
172}
173
174static void
175test_submodule(void **state)
176{
Michal Vasko4de7d072021-07-09 09:13:18 +0200177 struct lys_module *mod;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200178 const char *mod_yang = "module a {\n"
179 " yang-version 1.1;\n"
180 " namespace \"urn:test:a\";\n"
181 " prefix a;\n\n"
182 " include a-sub;\n"
183 "}\n";
184 char *submod_yang = "submodule a-sub {\n"
185 " yang-version 1.1;\n"
186 " belongs-to a {\n"
187 " prefix a;\n"
188 " }\n\n"
189 " import ietf-yang-types {\n"
190 " prefix yt;\n"
191 " revision-date 2013-07-15;\n"
192 " }\n\n"
193 " organization\n"
194 " \"ORG\";\n"
195 " contact\n"
196 " \"Radek Krejci.\";\n"
197 " description\n"
198 " \"Long multiline\n"
199 " description.\";\n"
200 " reference\n"
201 " \"some reference\";\n"
202 "}\n";
203 char *printed;
204 struct ly_out *out;
205
206 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200207
Radek Iša56ca9e42020-09-08 18:42:00 +0200208 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod_yang);
209 UTEST_ADD_MODULE(mod_yang, LYS_IN_YANG, NULL, &mod);
Michal Vasko7997d5a2021-02-22 10:55:56 +0100210 assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod->parsed->includes[0].submodule, LYS_OUT_YANG, 0, 0));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200211 assert_int_equal(strlen(submod_yang), ly_out_printed(out));
212 assert_string_equal(printed, submod_yang);
213
214 *state = NULL;
215 ly_out_free(out, NULL, 1);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200216}
Radek Krejcid3ca0632019-04-16 16:54:54 +0200217
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100218int
219main(void)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200220{
221 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200222 UTEST(test_module),
223 UTEST(test_submodule),
Radek Krejcid3ca0632019-04-16 16:54:54 +0200224 };
225
226 return cmocka_run_group_tests(tests, NULL, NULL);
227}