blob: 303b8554ce80f52743df69765fdb56271d64b73e [file] [log] [blame]
Radek Iša59bf3272021-03-10 11:22:52 +01001/**
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_range.c
Radek Iša59bf3272021-03-10 11:22:52 +01003 * @author Radek Iša <isa@cesnet.cz>
4 * @brief test for int8 values
5 *
6 * Copyright (c) 2021 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 UTEST HEADER */
16#define _UTEST_MAIN_
17#include "../utests.h"
18
19/* GLOBAL INCLUDE HEADERS */
20#include <ctype.h>
21
22/* LOCAL INCLUDE HEADERS */
23#include "libyang.h"
24#include "path.h"
25
26#define MODULE_CREATE_YIN(MOD_NAME, NODES) \
27 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" \
28 "<module name=\"" MOD_NAME "\"\n" \
29 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" \
30 " xmlns:pref=\"urn:tests:" MOD_NAME "\">\n" \
31 " <yang-version value=\"1.1\"/>\n" \
32 " <namespace uri=\"urn:tests:" MOD_NAME "\"/>\n" \
33 " <prefix value=\"pref\"/>\n" \
34 NODES \
35 "</module>\n"
36
37#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
38 "module " MOD_NAME " {\n" \
39 " yang-version 1.1;\n" \
40 " namespace \"urn:tests:" MOD_NAME "\";\n" \
41 " prefix pref;\n" \
42 NODES \
43 "}\n"
44
45#define TEST_SUCCESS_XML(MOD_NAME, DATA, TYPE, ...) \
46 { \
47 struct lyd_node *tree; \
48 const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
49 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
50 CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \
51 CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
52 lyd_free_all(tree); \
53 }
54
55#define TEST_SUCCESS_JSON(MOD_NAME, DATA, TYPE, ...) \
56 { \
57 struct lyd_node *tree; \
58 const char *data = "{\"" MOD_NAME ":port\":" DATA "}"; \
59 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
60 CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \
61 CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
62 lyd_free_all(tree); \
63 }
64
65#define TEST_ERROR_XML(MOD_NAME, DATA) \
66 {\
67 struct lyd_node *tree; \
68 const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
69 CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
70 assert_null(tree); \
71 }
72
73#define TEST_ERROR_JSON(MOD_NAME, DATA) \
74 { \
75 struct lyd_node *tree; \
76 const char *data = "{\"" MOD_NAME ":port\":" DATA "}"; \
77 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
78 assert_null(tree); \
79 }
80
81static void
82test_schema_yang(void **state)
83{
84 const char *schema;
Michal Vasko4de7d072021-07-09 09:13:18 +020085 struct lys_module *mod;
Radek Iša59bf3272021-03-10 11:22:52 +010086 struct lysc_node_leaf *lysc_leaf;
87 struct lysp_node_leaf *lysp_leaf;
88 struct lysc_range *range;
89
90 schema = MODULE_CREATE_YANG("T0", "leaf port {type int8 {"
91 "range \"0 .. 50 | 127\"{"
92 "description \"description test\";"
93 "error-app-tag \"err-apt-tag\";"
94 "error-message \"error message\";}}}");
95 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
96 assert_non_null(mod);
97 lysc_leaf = (void *)mod->compiled->data;
98 CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
99 CHECK_LYSC_TYPE_NUM((struct lysc_type_num *)lysc_leaf->type, LY_TYPE_INT8, 0, 1);
100 range = ((struct lysc_type_num *)lysc_leaf->type)->range;
101 CHECK_LYSC_RANGE(range, "description test", "err-apt-tag", "error message", 0, 2, NULL);
102 lysp_leaf = (void *)mod->parsed->data;
103 CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
104 CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x80, 0, 0, "int8", 0, 0, 1, 1, 0, 0);
105 CHECK_LYSP_RESTR(lysp_leaf->type.range, "0 .. 50 | 127", "description test", "err-apt-tag", "error message", 0, NULL);
106
107 /* heredity */
108 schema = MODULE_CREATE_YANG("T1", "typedef my_type {type uint16 {"
109 "range \"0 .. 100\"{"
110 "description \"percentage\";"
111 "error-app-tag \"err-apt-tag\";"
112 "error-message \"error message\";}}}"
113 "leaf port {type my_type;}");
114 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
115 assert_non_null(mod);
116 lysc_leaf = (void *)mod->compiled->data;
117 CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
118 CHECK_LYSC_TYPE_NUM((struct lysc_type_num *)lysc_leaf->type, LY_TYPE_UINT16, 0, 1);
119 range = ((struct lysc_type_num *)lysc_leaf->type)->range;
120 CHECK_LYSC_RANGE(range, "percentage", "err-apt-tag", "error message", 0, 1, NULL);
121 lysp_leaf = (void *)mod->parsed->data;
122 CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
123 CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x0, 0, 0, "my_type", 0, 0, 1, 0, 0, 0);
124
125 /* heredity new range */
126 schema = MODULE_CREATE_YANG("T2", "typedef my_type {type uint16 {"
127 "range \"0 .. 100\"{"
128 "description \"percentage\";"
129 "error-app-tag \"err-apt-tag\";"
130 "error-message \"error message\";}}}"
131 "leaf port {type my_type{range \"0 .. 20\";}}");
132 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
133 assert_non_null(mod);
134 lysc_leaf = (void *)mod->compiled->data;
135 CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
136 CHECK_LYSC_TYPE_NUM((struct lysc_type_num *)lysc_leaf->type, LY_TYPE_UINT16, 0, 1);
137 range = ((struct lysc_type_num *)lysc_leaf->type)->range;
138 CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 1, NULL);
139 lysp_leaf = (void *)mod->parsed->data;
140 CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
141 CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x80, 0, 0, "my_type", 0, 0, 1, 1, 0, 0);
142
143 /* change */
144 schema = MODULE_CREATE_YANG("T3", "typedef my_type {type uint16 {"
145 "range \"0 .. 100\"{"
146 "description \"percentage\";"
147 "error-app-tag \"err-apt-tag\";"
148 "error-message \"error message\";}}}"
149 "leaf port {type my_type{"
150 " range \"0 .. 50\"{"
151 " description \"description 0-50\";"
152 " error-app-tag \"err-apt-tag 0-50\";"
153 " error-message \"error message 0-50\";}}"
154 "}");
155 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
156 assert_non_null(mod);
157 lysc_leaf = (void *)mod->compiled->data;
158 CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
159 CHECK_LYSC_TYPE_NUM((struct lysc_type_num *)lysc_leaf->type, LY_TYPE_UINT16, 0, 1);
160 range = ((struct lysc_type_num *)lysc_leaf->type)->range;
161 CHECK_LYSC_RANGE(range, "description 0-50", "err-apt-tag 0-50", "error message 0-50", 0, 1, NULL);
162 lysp_leaf = (void *)mod->parsed->data;
163 CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
164 CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x80, 0, 0, "my_type", 0, 0, 1, 1, 0, 0);
165 CHECK_LYSP_RESTR(lysp_leaf->type.range, "0 .. 50", "description 0-50", "err-apt-tag 0-50", "error message 0-50", 0, NULL);
166
167}
168
169static void
170test_schema_yin(void **state)
171{
172 const char *schema;
Michal Vasko4de7d072021-07-09 09:13:18 +0200173 struct lys_module *mod;
Radek Iša59bf3272021-03-10 11:22:52 +0100174 struct lysc_node_leaf *lysc_leaf;
175 struct lysp_node_leaf *lysp_leaf;
176 struct lysc_range *range;
177
178 schema = MODULE_CREATE_YIN("T0", "<leaf name=\"port\">"
179 "<type name=\"int64\">"
180 "<range value = \"0 .. 50 | 256\">"
181 " <description>"
182 " <text>desc</text>\n"
183 " </description>\n"
184 "<error-app-tag value=\"text &lt; tag\"/>"
185 " <error-message>"
186 " <value>yin error message &lt;</value>\n"
187 " </error-message>\n"
188 "</range>"
189 "</type>"
190 "</leaf>");
191 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
192 assert_non_null(mod);
193 lysc_leaf = (void *)mod->compiled->data;
194 CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
195 CHECK_LYSC_TYPE_NUM((struct lysc_type_num *)lysc_leaf->type, LY_TYPE_INT64, 0, 1);
196 range = ((struct lysc_type_num *)lysc_leaf->type)->range;
197 CHECK_LYSC_RANGE(range, "desc", "text < tag", "yin error message <", 0, 2, NULL);
198 lysp_leaf = (void *)mod->parsed->data;
199 CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
200 CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x80, 0, 0, "int64", 0, 0, 1, 1, 0, 0);
201 CHECK_LYSP_RESTR(lysp_leaf->type.range, "0 .. 50 | 256", "desc", "text < tag", "yin error message <", 0, NULL);
202
203 /* heredity */
204 schema = MODULE_CREATE_YIN("T1", "<typedef name=\"my_type\">"
205 "<type name=\"int16\">"
206 "<range value = \"0 .. 50\">"
207 " <description>"
208 " <text>percentage</text>\n"
209 " </description>\n"
210 "<error-app-tag value=\"text &lt; tag\"/>"
211 " <error-message>"
212 " <value>yin error message &lt;</value>\n"
213 " </error-message>\n"
214 "</range>"
215 "</type>"
216 "</typedef>"
217 "<leaf name=\"port\"> <type name=\"my_type\"/> </leaf>");
218
219 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
220 assert_non_null(mod);
221 lysc_leaf = (void *)mod->compiled->data;
222 CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
223 CHECK_LYSC_TYPE_NUM((struct lysc_type_num *)lysc_leaf->type, LY_TYPE_INT16, 0, 1);
224 range = ((struct lysc_type_num *)lysc_leaf->type)->range;
225 CHECK_LYSC_RANGE(range, "percentage", "text < tag", "yin error message <", 0, 1, NULL);
226 lysp_leaf = (void *)mod->parsed->data;
227 CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
228 CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x0, 0, 0, "my_type", 0, 0, 1, 0, 0, 0);
229
230 /* heredity new range */
231 schema = MODULE_CREATE_YIN("T2", "<typedef name=\"my_type\">"
232 "<type name=\"int32\">"
233 "<range value = \"0 .. 100\">"
234 " <description>"
235 " <text>percentage</text>\n"
236 " </description>\n"
237 " <error-app-tag value=\"text &lt; tag\"/>"
238 " <error-message>"
239 " <value>yin error message &lt;</value>\n"
240 " </error-message>\n"
241 " </range>"
242 " </type>"
243 "</typedef>"
244 "<leaf name=\"port\"> <type name=\"my_type\">"
245 " <range value = \"0 .. 50\"/>"
246 "</type></leaf>");
247
248 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
249 assert_non_null(mod);
250 lysc_leaf = (void *)mod->compiled->data;
251 CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
252 CHECK_LYSC_TYPE_NUM((struct lysc_type_num *)lysc_leaf->type, LY_TYPE_INT32, 0, 1);
253 range = ((struct lysc_type_num *)lysc_leaf->type)->range;
254 CHECK_LYSC_RANGE(range, NULL, NULL, NULL, 0, 1, NULL);
255 lysp_leaf = (void *)mod->parsed->data;
256 CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
257 CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x80, 0, 0, "my_type", 0, 0, 1, 1, 0, 0);
258
259 /* change */
260 schema = MODULE_CREATE_YIN("T3", "<typedef name=\"my_type\">"
261 "<type name=\"int32\">"
262 "<range value = \"0 .. 100\">"
263 " <description>"
264 " <text>percentage</text>\n"
265 " </description>\n"
266 " <error-app-tag value=\"text &lt; tag\"/>"
267 " <error-message>"
268 " <value>yin error message &lt;</value>\n"
269 " </error-message>\n"
270 " </range>"
271 " </type>"
272 "</typedef>"
273 "<leaf name=\"port\"> <type name=\"my_type\">"
274 " <range value = \"0 .. 50\">"
275 " <description>"
276 " <text>percentage 0-50</text>\n"
277 " </description>\n"
278 " <error-app-tag value=\"text tag 0-50\"/>"
279 " <error-message>"
280 " <value>yin error message 0-50</value>\n"
281 " </error-message>\n"
282 " </range>"
283 "</type></leaf>");
284
285 UTEST_ADD_MODULE(schema, LYS_IN_YIN, NULL, &mod);
286 assert_non_null(mod);
287 lysc_leaf = (void *)mod->compiled->data;
288 CHECK_LYSC_NODE_LEAF(lysc_leaf, NULL, 0, 0x5, 1, "port", 0, 0, 0, NULL, 0, 0, NULL, NULL);
289 CHECK_LYSC_TYPE_NUM((struct lysc_type_num *)lysc_leaf->type, LY_TYPE_INT32, 0, 1);
290 range = ((struct lysc_type_num *)lysc_leaf->type)->range;
291 CHECK_LYSC_RANGE(range, "percentage 0-50", "text tag 0-50", "yin error message 0-50", 0, 1, NULL);
292 lysp_leaf = (void *)mod->parsed->data;
293 CHECK_LYSP_NODE_LEAF(lysp_leaf, NULL, 0, 0x0, 0, "port", 0, 0, NULL, 0, 0, NULL, NULL);
294 CHECK_LYSP_TYPE(&(lysp_leaf->type), 0, 0, 0, 0, 0, 0x80, 0, 0, "my_type", 0, 0, 1, 1, 0, 0);
295 CHECK_LYSP_RESTR(lysp_leaf->type.range, "0 .. 50", "percentage 0-50", "text tag 0-50", "yin error message 0-50", 0, NULL);
296
297}
298
299static void
300test_schema_print(void **state)
301{
302 const char *schema_yang, *schema_yin;
303 char *printed;
Michal Vasko4de7d072021-07-09 09:13:18 +0200304 struct lys_module *mod;
Radek Iša59bf3272021-03-10 11:22:52 +0100305
306 /* test print yang to yin */
307 schema_yang = MODULE_CREATE_YANG("PRINT0", "leaf port {type int32 {"
308 "range \"0 .. 50\"{"
309 "description \"desc < \";"
310 "error-app-tag \"err-apt-tag <\";"
311 "error-message \"error message <\";}}}");
312
313 schema_yin = MODULE_CREATE_YIN("PRINT0",
314 " <leaf name=\"port\">\n"
315 " <type name=\"int32\">\n"
316 " <range value=\"0 .. 50\">\n"
317 " <error-message>\n"
318 " <value>error message &lt;</value>\n"
319 " </error-message>\n"
320 " <error-app-tag value=\"err-apt-tag &lt;\"/>\n"
321 " <description>\n"
322 " <text>desc &lt; </text>\n"
323 " </description>\n"
324 " </range>\n"
325 " </type>\n"
326 " </leaf>\n");
327
328 UTEST_ADD_MODULE(schema_yang, LYS_IN_YANG, NULL, &mod);
329 assert_non_null(mod);
330 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YIN, 0));
331 assert_string_equal(printed, schema_yin);
332 free(printed);
333
334 /* test print yin to yang */
335 schema_yang = MODULE_CREATE_YANG("PRINT1",
Radek Krejciaa14a0c2020-09-04 11:16:47 +0200336 "\n"
Radek Iša59bf3272021-03-10 11:22:52 +0100337 " leaf port {\n"
338 " type int32 {\n"
339 " range \"0 .. 50\" {\n"
340 " error-message\n"
341 " \"error message <\";\n"
342 " error-app-tag \"err-apt-tag <\";\n"
343 " description\n"
344 " \"desc < \";\n"
345 " }\n"
346 " }\n"
347 " }\n");
348
349 schema_yin = MODULE_CREATE_YIN("PRINT1",
350 " <leaf name=\"port\">\n"
351 " <type name=\"int32\">\n"
352 " <range value=\"0 .. 50\">\n"
353 " <error-message>\n"
354 " <value>error message &lt;</value>\n"
355 " </error-message>\n"
356 " <error-app-tag value=\"err-apt-tag &lt;\"/>\n"
357 " <description>\n"
358 " <text>desc &lt; </text>\n"
359 " </description>\n"
360 " </range>\n"
361 " </type>\n"
362 " </leaf>\n");
363
364 UTEST_ADD_MODULE(schema_yin, LYS_IN_YIN, NULL, &mod);
365 assert_non_null(mod);
366 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG, 0));
367 assert_string_equal(printed, schema_yang);
368 free(printed);
369}
370
371static void
372test_data_xml(void **state)
373{
374 const char *schema;
375
376 /* xml test */
377 schema = MODULE_CREATE_YANG("TRANGE_0", "leaf port {type int8 {"
378 "range \"0 .. 50 | 126\"{"
379 "description \"description test\";"
380 "error-app-tag \"err-apt-tag\";"
381 "error-message \"error message\";}}}");
382 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
383
384 /* test success */
385 TEST_SUCCESS_XML("TRANGE_0", "126", INT8, "126", 126);
386 /* test print error */
387 TEST_ERROR_XML("TRANGE_0", "-1");
388 CHECK_LOG_CTX("error message",
389 "Schema location /TRANGE_0:port, line number 1.");
390 TEST_ERROR_XML("TRANGE_0", "51");
391 CHECK_LOG_CTX("error message",
392 "Schema location /TRANGE_0:port, line number 1.");
393 TEST_ERROR_XML("TRANGE_0", "127");
394 CHECK_LOG_CTX("error message",
395 "Schema location /TRANGE_0:port, line number 1.");
396
397 /* xml test */
398 schema = MODULE_CREATE_YANG("TRANGE_1", "leaf port {type uint8 {"
399 "range \"30 .. 50 | 126\"{"
400 "description \"description test\";"
401 "error-app-tag \"err-apt-tag\";"
402 "error-message \"error message\";}}}");
403 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
404
405 /* test success */
406 TEST_SUCCESS_XML("TRANGE_1", "126", UINT8, "126", 126);
407 /* test print error */
408 TEST_ERROR_XML("TRANGE_1", "0");
409 CHECK_LOG_CTX("error message",
410 "Schema location /TRANGE_1:port, line number 1.");
411 TEST_ERROR_XML("TRANGE_1", "51");
412 CHECK_LOG_CTX("error message",
413 "Schema location /TRANGE_1:port, line number 1.");
414 TEST_ERROR_XML("TRANGE_1", "127");
415 CHECK_LOG_CTX("error message",
416 "Schema location /TRANGE_1:port, line number 1.");
417
418}
419
420int
421main(void)
422{
423 const struct CMUnitTest tests[] = {
424 UTEST(test_schema_yang),
425 UTEST(test_schema_yin),
426 UTEST(test_schema_print),
427 UTEST(test_data_xml),
428 };
429
430 return cmocka_run_group_tests(tests, NULL, NULL);
431}