blob: 3a2cabd0ca9cb5690c98fa35113525d880634394 [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{
Radek Krejcid3ca0632019-04-16 16:54:54 +020026 const struct lys_module *mod;
27 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"
137 "}\n";
Radek Iša56ca9e42020-09-08 18:42:00 +0200138 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200139 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200140 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200141 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200142 ly_out_reset(out);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100143 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Michal Vasko63f3d842020-07-08 10:10:14 +0200144 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200145 assert_string_equal(printed, compiled);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200146
Radek Krejci241f6b52020-05-21 18:13:49 +0200147 ly_out_free(out, NULL, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200148}
149
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100150static LY_ERR
151test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
152 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
153 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
Michal Vasko7c8439f2020-08-05 13:25:19 +0200154{
155 *module_data = user_data;
156 *format = LYS_IN_YANG;
157 *free_module_data = NULL;
158 return LY_SUCCESS;
159}
160
161static void
162test_submodule(void **state)
163{
Michal Vasko7c8439f2020-08-05 13:25:19 +0200164 const struct lys_module *mod;
165 const char *mod_yang = "module a {\n"
166 " yang-version 1.1;\n"
167 " namespace \"urn:test:a\";\n"
168 " prefix a;\n\n"
169 " include a-sub;\n"
170 "}\n";
171 char *submod_yang = "submodule a-sub {\n"
172 " yang-version 1.1;\n"
173 " belongs-to a {\n"
174 " prefix a;\n"
175 " }\n\n"
176 " import ietf-yang-types {\n"
177 " prefix yt;\n"
178 " revision-date 2013-07-15;\n"
179 " }\n\n"
180 " organization\n"
181 " \"ORG\";\n"
182 " contact\n"
183 " \"Radek Krejci.\";\n"
184 " description\n"
185 " \"Long multiline\n"
186 " description.\";\n"
187 " reference\n"
188 " \"some reference\";\n"
189 "}\n";
190 char *printed;
191 struct ly_out *out;
192
193 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200194
Radek Iša56ca9e42020-09-08 18:42:00 +0200195 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod_yang);
196 UTEST_ADD_MODULE(mod_yang, LYS_IN_YANG, NULL, &mod);
Michal Vasko7997d5a2021-02-22 10:55:56 +0100197 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 +0200198 assert_int_equal(strlen(submod_yang), ly_out_printed(out));
199 assert_string_equal(printed, submod_yang);
200
201 *state = NULL;
202 ly_out_free(out, NULL, 1);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200203}
Radek Krejcid3ca0632019-04-16 16:54:54 +0200204
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100205int
206main(void)
Radek Krejcid3ca0632019-04-16 16:54:54 +0200207{
208 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200209 UTEST(test_module),
210 UTEST(test_submodule),
Radek Krejcid3ca0632019-04-16 16:54:54 +0200211 };
212
213 return cmocka_run_group_tests(tests, NULL, NULL);
214}