blob: feea18ef5238fad33935e7e5f3b42e093f435d7b [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 Krejci2d7a47b2019-05-16 13:34:10 +020023#include "../../src/context.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "../../src/printer.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020025#include "../../src/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 Krejcia5bba312020-01-09 15:41:20 +0100136 size_t size = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200137
Radek Krejci84ce7b12020-06-11 17:28:25 +0200138 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200139 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
140
141 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
Radek Krejcia5bba312020-01-09 15:41:20 +0100142 assert_int_equal(strlen(orig), size = lys_print(out, mod, LYS_OUT_YANG, 0, 0));
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);
Radek Krejcia5bba312020-01-09 15:41:20 +0100145 assert_int_equal(strlen(compiled), lys_print(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Radek Krejciffb410f2019-04-30 09:51:51 +0200146 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200147 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200148
149 orig = "module b {\n"
150 " yang-version 1.1;\n"
151 " namespace \"urn:test:b\";\n"
152 " prefix b;\n\n"
153 " revision 2019-04-16 {\n"
154 " description\n"
155 " \"text\";\n"
156 " reference\n"
157 " \"text\";\n"
158 " }\n"
159 " revision 2019-04-15 {\n"
160 " description\n"
161 " \"initial revision\";\n"
162 " }\n\n"
163 " feature f1 {\n"
164 " status current;\n"
165 " description\n"
166 " \"text\";\n"
167 " reference\n"
168 " \"text\";\n"
169 " }\n\n"
170 " feature f2 {\n"
171 " if-feature \"not f1\";\n"
172 " }\n"
173 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200174 compiled = "module b {\n"
175 " yang-version 1.1;\n"
176 " namespace \"urn:test:b\";\n"
177 " prefix b;\n\n"
178 " revision 2019-04-16;\n\n"
179 " feature f1 {\n"
180 " status current;\n"
181 " description\n"
182 " \"text\";\n"
183 " reference\n"
184 " \"text\";\n"
185 " }\n\n"
186 " feature f2 {\n"
187 " if-feature \"not f1\";\n"
188 " }\n"
189 "}\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200190 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
Radek Krejcia5bba312020-01-09 15:41:20 +0100191 assert_int_equal(strlen(orig), lys_print(out, mod, LYS_OUT_YANG, 0, 0));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200192 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200193 ly_out_reset(out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100194 assert_int_equal(strlen(compiled), lys_print(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Radek Krejciffb410f2019-04-30 09:51:51 +0200195 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200196 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200197
Radek Krejciffb410f2019-04-30 09:51:51 +0200198 orig = compiled ="module c {\n"
Radek Krejcid3ca0632019-04-16 16:54:54 +0200199 " yang-version 1.1;\n"
200 " namespace \"urn:test:c\";\n"
201 " prefix c;\n\n"
202 " feature f1;\n\n"
203 " identity i1 {\n"
204 " if-feature \"f1\";\n"
205 " description\n"
206 " \"text\";\n"
207 " reference\n"
208 " \"text32\";\n"
209 " }\n\n"
210 " identity i2 {\n"
211 " base i1;\n"
212 " status obsolete;\n"
213 " }\n"
214 "}\n";
215 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
Radek Krejcia5bba312020-01-09 15:41:20 +0100216 assert_int_equal(strlen(orig), lys_print(out, mod, LYS_OUT_YANG, 0, 0));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200217 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200218 ly_out_reset(out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100219 assert_int_equal(strlen(compiled), lys_print(out, mod, LYS_OUT_YANG, 0, 0));
Radek Krejciffb410f2019-04-30 09:51:51 +0200220 assert_string_equal(printed, compiled);
Radek Krejcia5bba312020-01-09 15:41:20 +0100221 /* missing free(printed); which is done in the following lyp_free() */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200222
223 *state = NULL;
Radek Krejci241f6b52020-05-21 18:13:49 +0200224 ly_out_free(out, NULL, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200225 ly_ctx_destroy(ctx, NULL);
226}
227
228/* TODO: include */
229
230int main(void)
231{
232 const struct CMUnitTest tests[] = {
233 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
234 };
235
236 return cmocka_run_group_tests(tests, NULL, NULL);
237}