blob: a8d524102fbaca893da7395db0785e01bd629889 [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"
24#include "../../src/printer_schema.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020025
26#define BUFSIZE 1024
27char logbuf[BUFSIZE] = {0};
28int store = -1; /* negative for infinite logging, positive for limited logging */
29
30/* set to 0 to printing error messages to stderr instead of checking them in code */
31#define ENABLE_LOGGER_CHECKING 1
32
33#if ENABLE_LOGGER_CHECKING
34static void
35logger(LY_LOG_LEVEL level, const char *msg, const char *path)
36{
37 (void) level; /* unused */
38 if (store) {
39 if (path && path[0]) {
40 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
41 } else {
42 strncpy(logbuf, msg, BUFSIZE - 1);
43 }
44 if (store > 0) {
45 --store;
46 }
47 }
48}
49#endif
50
51static int
52logger_setup(void **state)
53{
54 (void) state; /* unused */
55#if ENABLE_LOGGER_CHECKING
56 ly_set_log_clb(logger, 1);
57#endif
58 return 0;
59}
60
61static int
62logger_teardown(void **state)
63{
64 (void) state; /* unused */
65#if ENABLE_LOGGER_CHECKING
66 if (*state) {
67 fprintf(stderr, "%s\n", logbuf);
68 }
69#endif
70 return 0;
71}
72
73void
74logbuf_clean(void)
75{
76 logbuf[0] = '\0';
77}
78
79#if ENABLE_LOGGER_CHECKING
80# define logbuf_assert(str) assert_string_equal(logbuf, str)
81#else
82# define logbuf_assert(str)
83#endif
84
85
86static void
87test_module(void **state)
88{
89 *state = test_module;
90
91 struct ly_ctx *ctx = {0};
92 const struct lys_module *mod;
93 const char *orig = "module a {\n"
94 " yang-version 1.1;\n"
95 " namespace \"urn:test:a\";\n"
96 " prefix a;\n\n"
97 " import ietf-yang-types {\n"
98 " prefix yt;\n"
99 " revision-date 2013-07-15;\n"
100 " description\n"
101 " \"YANG types\";\n"
102 " reference\n"
103 " \"RFC reference\";\n"
104 " }\n\n"
105 " organization\n"
106 " \"ORG\";\n"
107 " contact\n"
108 " \"Radek Krejci.\";\n"
109 " description\n"
110 " \"Long multiline\n"
111 " description.\";\n"
112 " reference\n"
113 " \"some reference\";\n"
114 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200115 char *compiled = "module a {\n"
116 " yang-version 1.1;\n"
117 " namespace \"urn:test:a\";\n"
118 " prefix a;\n\n"
119 " import ietf-yang-types {\n"
120 " prefix yt;\n"
121 " revision-date 2013-07-15;\n"
122 " }\n\n"
123 " organization\n"
124 " \"ORG\";\n"
125 " contact\n"
126 " \"Radek Krejci.\";\n"
127 " description\n"
128 " \"Long multiline\n"
129 " description.\";\n"
130 " reference\n"
131 " \"some reference\";\n"
132 "}\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200133 char *printed;
Radek Krejci241f6b52020-05-21 18:13:49 +0200134 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100135 size_t size = 0;
Radek Krejcid3ca0632019-04-16 16:54:54 +0200136
Radek Krejci241f6b52020-05-21 18:13:49 +0200137 assert_non_null(out = ly_out_new_memory(&printed, 0));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200138 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
139
140 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
Radek Krejcia5bba312020-01-09 15:41:20 +0100141 assert_int_equal(strlen(orig), size = lys_print(out, mod, LYS_OUT_YANG, 0, 0));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200142 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200143 ly_out_reset(out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100144 assert_int_equal(strlen(compiled), lys_print(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Radek Krejciffb410f2019-04-30 09:51:51 +0200145 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200146 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200147
148 orig = "module b {\n"
149 " yang-version 1.1;\n"
150 " namespace \"urn:test:b\";\n"
151 " prefix b;\n\n"
152 " revision 2019-04-16 {\n"
153 " description\n"
154 " \"text\";\n"
155 " reference\n"
156 " \"text\";\n"
157 " }\n"
158 " revision 2019-04-15 {\n"
159 " description\n"
160 " \"initial revision\";\n"
161 " }\n\n"
162 " feature f1 {\n"
163 " status current;\n"
164 " description\n"
165 " \"text\";\n"
166 " reference\n"
167 " \"text\";\n"
168 " }\n\n"
169 " feature f2 {\n"
170 " if-feature \"not f1\";\n"
171 " }\n"
172 "}\n";
Radek Krejciffb410f2019-04-30 09:51:51 +0200173 compiled = "module b {\n"
174 " yang-version 1.1;\n"
175 " namespace \"urn:test:b\";\n"
176 " prefix b;\n\n"
177 " revision 2019-04-16;\n\n"
178 " feature f1 {\n"
179 " status current;\n"
180 " description\n"
181 " \"text\";\n"
182 " reference\n"
183 " \"text\";\n"
184 " }\n\n"
185 " feature f2 {\n"
186 " if-feature \"not f1\";\n"
187 " }\n"
188 "}\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200189 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
Radek Krejcia5bba312020-01-09 15:41:20 +0100190 assert_int_equal(strlen(orig), lys_print(out, mod, LYS_OUT_YANG, 0, 0));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200191 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200192 ly_out_reset(out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100193 assert_int_equal(strlen(compiled), lys_print(out, mod, LYS_OUT_YANG_COMPILED, 0, 0));
Radek Krejciffb410f2019-04-30 09:51:51 +0200194 assert_string_equal(printed, compiled);
Radek Krejci241f6b52020-05-21 18:13:49 +0200195 ly_out_reset(out);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200196
Radek Krejciffb410f2019-04-30 09:51:51 +0200197 orig = compiled ="module c {\n"
Radek Krejcid3ca0632019-04-16 16:54:54 +0200198 " yang-version 1.1;\n"
199 " namespace \"urn:test:c\";\n"
200 " prefix c;\n\n"
201 " feature f1;\n\n"
202 " identity i1 {\n"
203 " if-feature \"f1\";\n"
204 " description\n"
205 " \"text\";\n"
206 " reference\n"
207 " \"text32\";\n"
208 " }\n\n"
209 " identity i2 {\n"
210 " base i1;\n"
211 " status obsolete;\n"
212 " }\n"
213 "}\n";
214 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
Radek Krejcia5bba312020-01-09 15:41:20 +0100215 assert_int_equal(strlen(orig), lys_print(out, mod, LYS_OUT_YANG, 0, 0));
Radek Krejcid3ca0632019-04-16 16:54:54 +0200216 assert_string_equal(printed, orig);
Radek Krejci241f6b52020-05-21 18:13:49 +0200217 ly_out_reset(out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100218 assert_int_equal(strlen(compiled), lys_print(out, mod, LYS_OUT_YANG, 0, 0));
Radek Krejciffb410f2019-04-30 09:51:51 +0200219 assert_string_equal(printed, compiled);
Radek Krejcia5bba312020-01-09 15:41:20 +0100220 /* missing free(printed); which is done in the following lyp_free() */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200221
222 *state = NULL;
Radek Krejci241f6b52020-05-21 18:13:49 +0200223 ly_out_free(out, NULL, 1);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200224 ly_ctx_destroy(ctx, NULL);
225}
226
227/* TODO: include */
228
229int main(void)
230{
231 const struct CMUnitTest tests[] = {
232 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
233 };
234
235 return cmocka_run_group_tests(tests, NULL, NULL);
236}