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