blob: 66d3d6ba1b972c39b599a95601723cc1e443324c [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
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "context.h"
24#include "printer.h"
25#include "printer_schema.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020026
27#define BUFSIZE 1024
28char logbuf[BUFSIZE] = {0};
29int store = -1; /* negative for infinite logging, positive for limited logging */
30
31/* set to 0 to printing error messages to stderr instead of checking them in code */
32#define ENABLE_LOGGER_CHECKING 1
33
34#if ENABLE_LOGGER_CHECKING
35static void
36logger(LY_LOG_LEVEL level, const char *msg, const char *path)
37{
38 (void) level; /* unused */
39 if (store) {
40 if (path && path[0]) {
41 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
42 } else {
43 strncpy(logbuf, msg, BUFSIZE - 1);
44 }
45 if (store > 0) {
46 --store;
47 }
48 }
49}
50#endif
51
52static int
53logger_setup(void **state)
54{
55 (void) state; /* unused */
56#if ENABLE_LOGGER_CHECKING
57 ly_set_log_clb(logger, 1);
58#endif
59 return 0;
60}
61
62static int
63logger_teardown(void **state)
64{
65 (void) state; /* unused */
66#if ENABLE_LOGGER_CHECKING
67 if (*state) {
68 fprintf(stderr, "%s\n", logbuf);
69 }
70#endif
71 return 0;
72}
73
74void
75logbuf_clean(void)
76{
77 logbuf[0] = '\0';
78}
79
80#if ENABLE_LOGGER_CHECKING
81# define logbuf_assert(str) assert_string_equal(logbuf, str)
82#else
83# define logbuf_assert(str)
84#endif
85
86
87static void
88test_module(void **state)
89{
90 *state = test_module;
91
92 struct ly_ctx *ctx = {0};
93 const struct lys_module *mod;
94 const char *orig = "module a {\n"
95 " yang-version 1.1;\n"
96 " namespace \"urn:test:a\";\n"
97 " prefix a;\n\n"
98 " import ietf-yang-types {\n"
99 " prefix yt;\n"
100 " revision-date 2013-07-15;\n"
101 " description\n"
102 " \"YANG types\";\n"
103 " reference\n"
104 " \"RFC reference\";\n"
105 " }\n\n"
106 " organization\n"
107 " \"ORG\";\n"
108 " contact\n"
109 " \"Radek Krejci.\";\n"
110 " description\n"
111 " \"Long multiline\n"
112 " description.\";\n"
113 " reference\n"
114 " \"some reference\";\n"
115 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200116 char *compiled = "module a {\n"
117 " yang-version 1.1;\n"
118 " namespace \"urn:test:a\";\n"
119 " prefix a;\n\n"
120 " import ietf-yang-types {\n"
121 " prefix yt;\n"
122 " revision-date 2013-07-15;\n"
123 " }\n\n"
124 " organization\n"
125 " \"ORG\";\n"
126 " contact\n"
127 " \"Radek Krejci.\";\n"
128 " description\n"
129 " \"Long multiline\n"
130 " description.\";\n"
131 " reference\n"
132 " \"some reference\";\n"
133 "}\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200134 char *printed;
Radek Krejci241f6b52020-05-21 18:13:49 +0200135 struct ly_out *out;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200136
Radek Krejci84ce7b12020-06-11 17:28:25 +0200137 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200138 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
139
Michal Vasko3a41dff2020-07-15 14:30:28 +0200140 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko63f3d842020-07-08 10:10:14 +0200141 assert_int_equal(LY_SUCCESS, lys_print(out, mod, LYS_OUT_YANG, 0, 0));
142 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200143 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200144 ly_out_reset(out);
Michal Vasko63f3d842020-07-08 10:10:14 +0200145 assert_int_equal(LY_SUCCESS, lys_print(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
146 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200147 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200148 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200149
150 orig = "module b {\n"
151 " yang-version 1.1;\n"
152 " namespace \"urn:test:b\";\n"
153 " prefix b;\n\n"
154 " revision 2019-04-16 {\n"
155 " description\n"
156 " \"text\";\n"
157 " reference\n"
158 " \"text\";\n"
159 " }\n"
160 " revision 2019-04-15 {\n"
161 " description\n"
162 " \"initial revision\";\n"
163 " }\n\n"
164 " feature f1 {\n"
165 " status current;\n"
166 " description\n"
167 " \"text\";\n"
168 " reference\n"
169 " \"text\";\n"
170 " }\n\n"
171 " feature f2 {\n"
172 " if-feature \"not f1\";\n"
173 " }\n"
174 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200175 compiled = "module b {\n"
176 " yang-version 1.1;\n"
177 " namespace \"urn:test:b\";\n"
178 " prefix b;\n\n"
179 " revision 2019-04-16;\n\n"
180 " feature f1 {\n"
181 " status current;\n"
182 " description\n"
183 " \"text\";\n"
184 " reference\n"
185 " \"text\";\n"
186 " }\n\n"
187 " feature f2 {\n"
188 " if-feature \"not f1\";\n"
189 " }\n"
190 "}\n";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200191 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko63f3d842020-07-08 10:10:14 +0200192 assert_int_equal(LY_SUCCESS, lys_print(out, mod, LYS_OUT_YANG, 0, 0));
193 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200194 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200195 ly_out_reset(out);
Michal Vasko63f3d842020-07-08 10:10:14 +0200196 assert_int_equal(LY_SUCCESS, lys_print(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
197 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200198 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200199 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200200
Radek Krejciffb410f2019-04-30 09:51:51 +0200201 orig = compiled ="module c {\n"
Radek Krejcid3ca0632019-04-16 16:54:54 +0200202 " yang-version 1.1;\n"
203 " namespace \"urn:test:c\";\n"
204 " prefix c;\n\n"
205 " feature f1;\n\n"
206 " identity i1 {\n"
207 " if-feature \"f1\";\n"
208 " description\n"
209 " \"text\";\n"
210 " reference\n"
211 " \"text32\";\n"
212 " }\n\n"
213 " identity i2 {\n"
214 " base i1;\n"
215 " status obsolete;\n"
216 " }\n"
217 "}\n";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200218 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod));
Michal Vasko63f3d842020-07-08 10:10:14 +0200219 assert_int_equal(LY_SUCCESS, lys_print(out, mod, LYS_OUT_YANG, 0, 0));
220 assert_int_equal(strlen(orig), ly_out_printed(out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200221 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200222 ly_out_reset(out);
Michal Vasko63f3d842020-07-08 10:10:14 +0200223 assert_int_equal(LY_SUCCESS, lys_print(out, mod, LYS_OUT_YANG, 0, 0));
224 assert_int_equal(strlen(compiled), ly_out_printed(out));
Radek Krejciffb410f2019-04-30 09:51:51 +0200225 assert_string_equal(printed, compiled);
Radek Krejcia5bba312020-01-09 15:41:20 +0100226 /* missing free(printed); which is done in the following lyp_free() */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200227
228 *state = NULL;
Radek Krejci241f6b52020-05-21 18:13:49 +0200229 ly_out_free(out, NULL, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200230 ly_ctx_destroy(ctx, NULL);
231}
232
233/* TODO: include */
234
235int main(void)
236{
237 const struct CMUnitTest tests[] = {
238 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
239 };
240
241 return cmocka_run_group_tests(tests, NULL, NULL);
242}