blob: 922b17088a11c17d0efc230dc46d3f6366486637 [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";
128 char *printed;
129
130 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
131
132 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
133 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0, 0));
134 assert_string_equal(printed, orig);
135 free(printed);
136
137 orig = "module b {\n"
138 " yang-version 1.1;\n"
139 " namespace \"urn:test:b\";\n"
140 " prefix b;\n\n"
141 " revision 2019-04-16 {\n"
142 " description\n"
143 " \"text\";\n"
144 " reference\n"
145 " \"text\";\n"
146 " }\n"
147 " revision 2019-04-15 {\n"
148 " description\n"
149 " \"initial revision\";\n"
150 " }\n\n"
151 " feature f1 {\n"
152 " status current;\n"
153 " description\n"
154 " \"text\";\n"
155 " reference\n"
156 " \"text\";\n"
157 " }\n\n"
158 " feature f2 {\n"
159 " if-feature \"not f1\";\n"
160 " }\n"
161 "}\n";
162 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
163 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0, 0));
164 assert_string_equal(printed, orig);
165 free(printed);
166
167 orig = "module c {\n"
168 " yang-version 1.1;\n"
169 " namespace \"urn:test:c\";\n"
170 " prefix c;\n\n"
171 " feature f1;\n\n"
172 " identity i1 {\n"
173 " if-feature \"f1\";\n"
174 " description\n"
175 " \"text\";\n"
176 " reference\n"
177 " \"text32\";\n"
178 " }\n\n"
179 " identity i2 {\n"
180 " base i1;\n"
181 " status obsolete;\n"
182 " }\n"
183 "}\n";
184 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
185 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0, 0));
186 assert_string_equal(printed, orig);
187 free(printed);
188
189 *state = NULL;
190 ly_ctx_destroy(ctx, NULL);
191}
192
193/* TODO: include */
194
195int main(void)
196{
197 const struct CMUnitTest tests[] = {
198 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
199 };
200
201 return cmocka_run_group_tests(tests, NULL, NULL);
202}