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