blob: 88b33bfee839aee6f0d72bd401ba6145c9061ed8 [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
15#include "../../src/common.c"
16#include "../../src/set.c"
17#include "../../src/log.c"
18#include "../../src/hash_table.c"
19#include "../../src/xpath.c"
20#include "../../src/parser_yang.c"
21#include "../../src/context.c"
22#include "../../src/tree_schema_helpers.c"
23#include "../../src/tree_schema_free.c"
24#include "../../src/tree_schema_compile.c"
25#include "../../src/tree_schema.c"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "../../src/plugins_types.c"
Radek Krejcid3ca0632019-04-16 16:54:54 +020027#include "../../src/printer_yang.c"
Radek Krejcid3ca0632019-04-16 16:54:54 +020028#include <stdarg.h>
29#include <stddef.h>
30#include <setjmp.h>
31#include <cmocka.h>
32
33#include <stdio.h>
34#include <string.h>
35
Radek Krejcie7b95092019-05-15 11:03:07 +020036#include "../../src/printer.c"
37#include "../../src/printer_schema.c"
Radek Krejcid3ca0632019-04-16 16:54:54 +020038#include "libyang.h"
39
40#define BUFSIZE 1024
41char logbuf[BUFSIZE] = {0};
42int store = -1; /* negative for infinite logging, positive for limited logging */
43
44/* set to 0 to printing error messages to stderr instead of checking them in code */
45#define ENABLE_LOGGER_CHECKING 1
46
47#if ENABLE_LOGGER_CHECKING
48static void
49logger(LY_LOG_LEVEL level, const char *msg, const char *path)
50{
51 (void) level; /* unused */
52 if (store) {
53 if (path && path[0]) {
54 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
55 } else {
56 strncpy(logbuf, msg, BUFSIZE - 1);
57 }
58 if (store > 0) {
59 --store;
60 }
61 }
62}
63#endif
64
65static int
66logger_setup(void **state)
67{
68 (void) state; /* unused */
69#if ENABLE_LOGGER_CHECKING
70 ly_set_log_clb(logger, 1);
71#endif
72 return 0;
73}
74
75static int
76logger_teardown(void **state)
77{
78 (void) state; /* unused */
79#if ENABLE_LOGGER_CHECKING
80 if (*state) {
81 fprintf(stderr, "%s\n", logbuf);
82 }
83#endif
84 return 0;
85}
86
87void
88logbuf_clean(void)
89{
90 logbuf[0] = '\0';
91}
92
93#if ENABLE_LOGGER_CHECKING
94# define logbuf_assert(str) assert_string_equal(logbuf, str)
95#else
96# define logbuf_assert(str)
97#endif
98
99
100static void
101test_module(void **state)
102{
103 *state = test_module;
104
105 struct ly_ctx *ctx = {0};
106 const struct lys_module *mod;
107 const char *orig = "module a {\n"
108 " yang-version 1.1;\n"
109 " namespace \"urn:test:a\";\n"
110 " prefix a;\n\n"
111 " import ietf-yang-types {\n"
112 " prefix yt;\n"
113 " revision-date 2013-07-15;\n"
114 " description\n"
115 " \"YANG types\";\n"
116 " reference\n"
117 " \"RFC reference\";\n"
118 " }\n\n"
119 " organization\n"
120 " \"ORG\";\n"
121 " contact\n"
122 " \"Radek Krejci.\";\n"
123 " description\n"
124 " \"Long multiline\n"
125 " description.\";\n"
126 " reference\n"
127 " \"some reference\";\n"
128 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200129 char *compiled = "module a {\n"
130 " yang-version 1.1;\n"
131 " namespace \"urn:test:a\";\n"
132 " prefix a;\n\n"
133 " import ietf-yang-types {\n"
134 " prefix yt;\n"
135 " revision-date 2013-07-15;\n"
136 " }\n\n"
137 " organization\n"
138 " \"ORG\";\n"
139 " contact\n"
140 " \"Radek Krejci.\";\n"
141 " description\n"
142 " \"Long multiline\n"
143 " description.\";\n"
144 " reference\n"
145 " \"some reference\";\n"
146 "}\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200147 char *printed;
148
149 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
150
151 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
Radek Krejci897ad2e2019-04-29 16:43:07 +0200152 assert_int_equal(strlen(orig), lys_print_mem(&printed, mod, LYS_OUT_YANG, 0, 0));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200153 assert_string_equal(printed, orig);
154 free(printed);
Radek Krejciffb410f2019-04-30 09:51:51 +0200155 assert_int_equal(strlen(compiled), lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0, 0));
156 assert_string_equal(printed, compiled);
157 free(printed);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200158
159 orig = "module b {\n"
160 " yang-version 1.1;\n"
161 " namespace \"urn:test:b\";\n"
162 " prefix b;\n\n"
163 " revision 2019-04-16 {\n"
164 " description\n"
165 " \"text\";\n"
166 " reference\n"
167 " \"text\";\n"
168 " }\n"
169 " revision 2019-04-15 {\n"
170 " description\n"
171 " \"initial revision\";\n"
172 " }\n\n"
173 " feature f1 {\n"
174 " status current;\n"
175 " description\n"
176 " \"text\";\n"
177 " reference\n"
178 " \"text\";\n"
179 " }\n\n"
180 " feature f2 {\n"
181 " if-feature \"not f1\";\n"
182 " }\n"
183 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200184 compiled = "module b {\n"
185 " yang-version 1.1;\n"
186 " namespace \"urn:test:b\";\n"
187 " prefix b;\n\n"
188 " revision 2019-04-16;\n\n"
189 " feature f1 {\n"
190 " status current;\n"
191 " description\n"
192 " \"text\";\n"
193 " reference\n"
194 " \"text\";\n"
195 " }\n\n"
196 " feature f2 {\n"
197 " if-feature \"not f1\";\n"
198 " }\n"
199 "}\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200200 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
Radek Krejci897ad2e2019-04-29 16:43:07 +0200201 assert_int_equal(strlen(orig), lys_print_mem(&printed, mod, LYS_OUT_YANG, 0, 0));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200202 assert_string_equal(printed, orig);
203 free(printed);
Radek Krejciffb410f2019-04-30 09:51:51 +0200204 assert_int_equal(strlen(compiled), lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0, 0));
205 assert_string_equal(printed, compiled);
206 free(printed);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200207
Radek Krejciffb410f2019-04-30 09:51:51 +0200208 orig = compiled ="module c {\n"
Radek Krejcid3ca0632019-04-16 16:54:54 +0200209 " yang-version 1.1;\n"
210 " namespace \"urn:test:c\";\n"
211 " prefix c;\n\n"
212 " feature f1;\n\n"
213 " identity i1 {\n"
214 " if-feature \"f1\";\n"
215 " description\n"
216 " \"text\";\n"
217 " reference\n"
218 " \"text32\";\n"
219 " }\n\n"
220 " identity i2 {\n"
221 " base i1;\n"
222 " status obsolete;\n"
223 " }\n"
224 "}\n";
225 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
Radek Krejci897ad2e2019-04-29 16:43:07 +0200226 assert_int_equal(strlen(orig), lys_print_mem(&printed, mod, LYS_OUT_YANG, 0, 0));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200227 assert_string_equal(printed, orig);
228 free(printed);
Radek Krejciffb410f2019-04-30 09:51:51 +0200229 assert_int_equal(strlen(compiled), lys_print_mem(&printed, mod, LYS_OUT_YANG, 0, 0));
230 assert_string_equal(printed, compiled);
231 free(printed);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200232
233 *state = NULL;
234 ly_ctx_destroy(ctx, NULL);
235}
236
237/* TODO: include */
238
239int main(void)
240{
241 const struct CMUnitTest tests[] = {
242 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
243 };
244
245 return cmocka_run_group_tests(tests, NULL, NULL);
246}