blob: 9dbec761b48bc020c52371eee10acf0ebecc0d03 [file] [log] [blame]
FredGand944bdc2019-11-05 21:57:07 +08001/*
2 * @file test_printer_yin.c
3 * @author: Fred Gan <ganshaolong@vip.qq.com>
4 * @brief unit tests for functions from printer_yin.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 <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
22
23#include "../../src/context.h"
24#include "../../src/printer_schema.h"
25
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
94 const char * orig =
95 "module all {\n"
96 " yang-version 1.1;\n"
97 " namespace \"urn:all\";\n"
98 " prefix all_mod;\n\n"
99 " import ietf-yang-types {\n"
100 " prefix yt;\n"
101 " revision-date 2013-07-15;\n"
102 " description\n"
103 " \"YANG types\";\n"
104 " reference\n"
105 " \"RFC reference\";\n"
106 " }\n\n"
107 " feature feat1 {\n"
108 " if-feature \"feat2\";\n"
109 " status obsolete;\n"
110 " }\n\n"
111 " feature feat2;\n"
112 " feature feat3;\n\n"
113 " identity ident2 {\n"
114 " base ident1;\n"
115 " }\n\n"
116 " identity ident1;\n\n"
117 " typedef tdef1 {\n"
118 " type tdef2 {\n"
119 " length \"3..9 | 30..40\";\n"
120 " pattern \"[ac]*\";\n"
121 " }\n"
122 " units \"none\";\n"
123 " default \"aaa\";\n"
124 " }\n\n"
125 " typedef tdef2 {\n"
126 " type string {\n"
127 " length \"2..10 | 20..50\";\n"
128 " pattern \"[ab]*\";\n"
129 " }\n"
130 " }\n\n"
131 " grouping group1 {\n"
132 " leaf leaf1 {\n"
133 " type int8;\n"
134 " }\n"
135 " }\n\n"
136 " container cont1 {\n"
137 " leaf leaf2 {\n"
138 " if-feature \"feat1\";\n"
139 " type int16;\n"
140 " status obsolete;\n"
141 " }\n\n"
142 " uses group1 {\n"
143 " if-feature \"feat2\";\n"
144 " refine \"leaf1\" {\n"
145 " if-feature \"feat3\";\n"
146 " must \"24 - 4 = number('20')\";\n"
147 " default \"25\";\n"
148 " config true;\n"
149 " mandatory false;\n"
150 " description\n"
151 " \"dsc\";\n"
152 " reference\n"
153 " \"none\";\n"
154 " }\n"
155 " }\n\n"
156 " leaf leaf3 {\n"
157 " type int32;\n"
158 " }\n\n"
159 " leaf leaf4 {\n"
160 " type int64 {\n"
161 " range \"1000 .. 50000\" {\n"
162 " error-message\n"
163 " \"Special error message.\";\n"
164 " error-app-tag \"special-tag\";\n"
165 " }\n"
166 " }\n"
167 " }\n\n"
168 " leaf leaf5 {\n"
169 " type uint8;\n"
170 " }\n\n"
171 " leaf leaf6 {\n"
172 " type uint16;\n"
173 " }\n\n"
174 " leaf leaf7 {\n"
175 " type uint32;\n"
176 " }\n\n"
177 " leaf leaf8 {\n"
178 " type uint64;\n"
179 " }\n\n"
180 " choice choic1 {\n"
181 " default \"leaf9b\";\n"
182 " leaf leaf9a {\n"
183 " type decimal64 {\n"
184 " fraction-digits 9;\n"
185 " }\n"
186 " }\n\n"
187 " leaf leaf9b {\n"
188 " type boolean;\n"
189 " default \"false\";\n"
190 " }\n"
191 " }\n\n"
192 " leaf leaf10 {\n"
193 " type boolean;\n"
194 " }\n\n"
195 " leaf leaf11 {\n"
196 " type enumeration {\n"
197 " enum \"one\";\n"
198 " enum \"two\";\n"
199 " enum \"five\" {\n"
200 " value 5;\n"
201 " }\n"
202 " }\n"
203 " }\n\n"
204 " leaf leaf12 {\n"
205 " type bits {\n"
206 " bit flag0 {\n"
207 " position 0;\n"
208 " }\n"
209 " bit flag1;\n"
210 " bit flag2 {\n"
211 " position 2;\n"
212 " }\n"
213 " bit flag3 {\n"
214 " position 3;\n"
215 " }\n"
216 " }\n"
217 " default \"flag0 flag3\";\n"
218 " }\n\n"
219 " leaf leaf13 {\n"
220 " type binary;\n"
221 " }\n\n"
222 " leaf leaf14 {\n"
223 " type leafref {\n"
224 " path \"/cont1/leaf17\";\n"
225 " }\n"
226 " }\n\n"
227 " leaf leaf15 {\n"
228 " type empty;\n"
229 " }\n\n"
230 " leaf leaf16 {\n"
231 " type union {\n"
232 " type instance-identifier {\n"
233 " require-instance true;\n"
234 " }\n"
235 " type int8;\n"
236 " }\n"
237 " }\n\n"
238 " list list1 {\n"
239 " key \"leaf18\";\n"
240 " unique \"leaf19\";\n"
241 " min-elements 1;\n"
242 " max-elements 20;\n"
243 " leaf leaf18 {\n"
244 " type string;\n"
245 " }\n\n"
246 " leaf leaf19 {\n"
247 " type uint32;\n"
248 " }\n\n"
249 " anyxml axml1;\n"
250 " anydata adata1;\n\n"
251 " action act1 {\n"
252 " input {\n"
253 " leaf leaf24 {\n"
254 " type string;\n"
255 " }\n"
256 " }\n\n"
257 " output {\n"
258 " leaf leaf25 {\n"
259 " type string;\n"
260 " }\n"
261 " }\n"
262 " }\n\n"
263 " notification notif1 {\n"
264 " leaf leaf26 {\n"
265 " type string;\n"
266 " }\n"
267 " }\n"
268 " }\n\n"
269 " leaf-list llist1 {\n"
270 " type tdef1;\n"
271 " ordered-by user;\n"
272 " }\n\n"
273 " list list2 {\n"
274 " key \"leaf27 leaf28\";\n"
275 " leaf leaf27 {\n"
276 " type uint8;\n"
277 " }\n\n"
278 " leaf leaf28 {\n"
279 " type uint8;\n"
280 " }\n"
281 " }\n\n"
282 " leaf leaf29 {\n"
283 " type instance-identifier;\n"
284 " }\n\n"
285 " container must-deviations-container {\n"
286 " presence \"Allows deviations on the leaf\";\n"
287 " leaf leaf30 {\n"
288 " type string;\n"
289 " }\n"
290 " }\n\n"
291 " leaf leaf23 {\n"
292 " type empty;\n"
293 " }\n"
294 " }\n\n"
295 " augment \"/cont1\" {\n"
296 " leaf leaf17 {\n"
297 " type string;\n"
298 " }\n"
299 " }\n\n"
300 " rpc rpc1 {\n"
301 " input {\n"
302 " leaf leaf20 {\n"
303 " type tdef1;\n"
304 " }\n"
305 " }\n\n"
306 " output {\n"
307 " container cont2 {\n"
308 " leaf leaf21 {\n"
309 " type empty;\n"
310 " }\n"
311 " }\n"
312 " }\n"
313 " }\n\n"
314 " container test-when {\n"
315 " leaf when-check {\n"
316 " type boolean;\n"
317 " }\n\n"
318 " leaf gated-data {\n"
319 " when \"../when-check = 'true'\";\n"
320 " type uint16;\n"
321 " }\n"
322 " }\n\n"
323 " extension c-define {\n"
324 " description\n"
325 " \"Takes as an argument a name string.\n"
326 " Makes the code generator use the given name\n"
327 " in the #define.\";\n"
328 " argument \"name\";\n"
329 " }\n"
330 "}\n";
331
332
333 const char * ori_res =
334 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
335 "<module name=\"all\"\n"
336 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
337 " xmlns:all_mod=\"urn:all\"\n"
338 " xmlns:yt=\"urn:ietf:params:xml:ns:yang:ietf-yang-types\">\n"
339 " <yang-version value=\"1.1\"/>\n"
340 " <namespace uri=\"urn:all\"/>\n"
341 " <prefix value=\"all_mod\"/>\n"
342 " <import module=\"ietf-yang-types\">\n"
343 " <prefix value=\"yt\"/>\n"
344 " <revision-date date=\"2013-07-15\"/>\n"
345 " <description>\n"
346 " <text>YANG types</text>\n"
347 " </description>\n"
348 " <reference>\n"
349 " <text>RFC reference</text>\n"
350 " </reference>\n"
351 " </import>\n\n"
352 " <extension name=\"c-define\">\n"
353 " <argument name=\"name\"/>\n"
354 " <description>\n"
355 " <text>Takes as an argument a name string.\n"
356 "Makes the code generator use the given name\n"
357 "in the #define.</text>\n"
358 " </description>\n"
359 " </extension>\n"
360 " <feature name=\"feat1\">\n"
361 " <if-feature name=\"feat2\"/>\n"
362 " <status value=\"obsolete\"/>\n"
363 " </feature>\n"
364 " <feature name=\"feat2\"/>\n"
365 " <feature name=\"feat3\"/>\n"
366 " <identity name=\"ident2\">\n"
367 " <base name=\"ident1\"/>\n"
368 " </identity>\n"
369 " <identity name=\"ident1\"/>\n"
370 " <typedef name=\"tdef1\">\n"
371 " <type name=\"tdef2\">\n"
372 " <length value=\"3..9 | 30..40\"/>\n"
373 " <pattern value=\"[ac]*\"/>\n"
374 " </type>\n"
375 " <units name=\"none\"/>\n"
376 " <default value=\"aaa\"/>\n"
377 " </typedef>\n"
378 " <typedef name=\"tdef2\">\n"
379 " <type name=\"string\">\n"
380 " <length value=\"2..10 | 20..50\"/>\n"
381 " <pattern value=\"[ab]*\"/>\n"
382 " </type>\n"
383 " </typedef>\n"
384 " <grouping name=\"group1\">\n"
385 " <leaf name=\"leaf1\">\n"
386 " <type name=\"int8\"/>\n"
387 " </leaf>\n"
388 " </grouping>\n"
389 " <container name=\"cont1\">\n"
390 " <leaf name=\"leaf2\">\n"
391 " <if-feature name=\"feat1\"/>\n"
392 " <type name=\"int16\"/>\n"
393 " <status value=\"obsolete\"/>\n"
394 " </leaf>\n"
395 " <uses name=\"group1\">\n"
396 " <if-feature name=\"feat2\"/>\n"
397 " <refine target-node=\"leaf1\">\n"
398 " <if-feature name=\"feat3\"/>\n"
399 " <must condition=\"24 - 4 = number('20')\"/>\n"
400 " <default value=\"25\"/>\n"
401 " <config value=\"true\"/>\n"
402 " <mandatory value=\"false\"/>\n"
403 " <description>\n"
404 " <text>dsc</text>\n"
405 " </description>\n"
406 " <reference>\n"
407 " <text>none</text>\n"
408 " </reference>\n"
409 " </refine>\n"
410 " </uses>\n"
411 " <leaf name=\"leaf3\">\n"
412 " <type name=\"int32\"/>\n"
413 " </leaf>\n"
414 " <leaf name=\"leaf4\">\n"
415 " <type name=\"int64\">\n"
416 " <range value=\"1000 .. 50000\">\n"
417 " <error-message>\n"
418 " <value>Special error message.</value>\n"
419 " </error-message>\n"
420 " <error-app-tag value=\"special-tag\"/>\n"
421 " </range>\n"
422 " </type>\n"
423 " </leaf>\n"
424 " <leaf name=\"leaf5\">\n"
425 " <type name=\"uint8\"/>\n"
426 " </leaf>\n"
427 " <leaf name=\"leaf6\">\n"
428 " <type name=\"uint16\"/>\n"
429 " </leaf>\n"
430 " <leaf name=\"leaf7\">\n"
431 " <type name=\"uint32\"/>\n"
432 " </leaf>\n"
433 " <leaf name=\"leaf8\">\n"
434 " <type name=\"uint64\"/>\n"
435 " </leaf>\n"
436 " <choice name=\"choic1\">\n"
437 " <default value=\"leaf9b\"/>\n"
438 " <leaf name=\"leaf9a\">\n"
439 " <type name=\"decimal64\">\n"
440 " <fraction-digits value=\"9\"/>\n"
441 " </type>\n"
442 " </leaf>\n"
443 " <leaf name=\"leaf9b\">\n"
444 " <type name=\"boolean\"/>\n"
445 " <default value=\"false\"/>\n"
446 " </leaf>\n"
447 " </choice>\n"
448 " <leaf name=\"leaf10\">\n"
449 " <type name=\"boolean\"/>\n"
450 " </leaf>\n"
451 " <leaf name=\"leaf11\">\n"
452 " <type name=\"enumeration\">\n"
453 " <enum name=\"one\"/>\n"
454 " <enum name=\"two\"/>\n"
455 " <enum name=\"five\">\n"
456 " <value value=\"5\"/>\n"
457 " </enum>\n"
458 " </type>\n"
459 " </leaf>\n"
460 " <leaf name=\"leaf12\">\n"
461 " <type name=\"bits\">\n"
462 " <bit name=\"flag0\">\n"
463 " <position value=\"0\"/>\n"
464 " </bit>\n"
465 " <bit name=\"flag1\"/>\n"
466 " <bit name=\"flag2\">\n"
467 " <position value=\"2\"/>\n"
468 " </bit>\n"
469 " <bit name=\"flag3\">\n"
470 " <position value=\"3\"/>\n"
471 " </bit>\n"
472 " </type>\n"
473 " <default value=\"flag0 flag3\"/>\n"
474 " </leaf>\n"
475 " <leaf name=\"leaf13\">\n"
476 " <type name=\"binary\"/>\n"
477 " </leaf>\n"
478 " <leaf name=\"leaf14\">\n"
479 " <type name=\"leafref\">\n"
480 " <path value=\"/cont1/leaf17\"/>\n"
481 " </type>\n"
482 " </leaf>\n"
483 " <leaf name=\"leaf15\">\n"
484 " <type name=\"empty\"/>\n"
485 " </leaf>\n"
486 " <leaf name=\"leaf16\">\n"
487 " <type name=\"union\">\n"
488 " <type name=\"instance-identifier\">\n"
489 " <require-instance value=\"true\"/>\n"
490 " </type>\n"
491 " <type name=\"int8\"/>\n"
492 " </type>\n"
493 " </leaf>\n"
494 " <list name=\"list1\">\n"
495 " <key value=\"leaf18\"/>\n"
496 " <unique tag=\"leaf19\"/>\n"
497 " <min-elements value=\"1\"/>\n"
498 " <max-elements value=\"20\"/>\n"
499 " <leaf name=\"leaf18\">\n"
500 " <type name=\"string\"/>\n"
501 " </leaf>\n"
502 " <leaf name=\"leaf19\">\n"
503 " <type name=\"uint32\"/>\n"
504 " </leaf>\n"
505 " <anyxml name=\"axml1\"/>\n"
506 " <anydata name=\"adata1\"/>\n"
507 " <action name=\"act1\">\n"
508 " <input>\n"
509 " <leaf name=\"leaf24\">\n"
510 " <type name=\"string\"/>\n"
511 " </leaf>\n"
512 " </input>\n"
513 " <output>\n"
514 " <leaf name=\"leaf25\">\n"
515 " <type name=\"string\"/>\n"
516 " </leaf>\n"
517 " </output>\n"
518 " </action>\n"
519 " <notification name=\"notif1\">\n"
520 " <leaf name=\"leaf26\">\n"
521 " <type name=\"string\"/>\n"
522 " </leaf>\n"
523 " </notification>\n"
524 " </list>\n"
525 " <leaf-list name=\"llist1\">\n"
526 " <type name=\"tdef1\"/>\n"
527 " <ordered-by value=\"user\"/>\n"
528 " </leaf-list>\n"
529 " <list name=\"list2\">\n"
530 " <key value=\"leaf27 leaf28\"/>\n"
531 " <leaf name=\"leaf27\">\n"
532 " <type name=\"uint8\"/>\n"
533 " </leaf>\n"
534 " <leaf name=\"leaf28\">\n"
535 " <type name=\"uint8\"/>\n"
536 " </leaf>\n"
537 " </list>\n"
538 " <leaf name=\"leaf29\">\n"
539 " <type name=\"instance-identifier\"/>\n"
540 " </leaf>\n"
541 " <container name=\"must-deviations-container\">\n"
542 " <presence value=\"Allows deviations on the leaf\"/>\n"
543 " <leaf name=\"leaf30\">\n"
544 " <type name=\"string\"/>\n"
545 " </leaf>\n"
546 " </container>\n"
547 " <leaf name=\"leaf23\">\n"
548 " <type name=\"empty\"/>\n"
549 " </leaf>\n"
550 " </container>\n"
551 " <container name=\"test-when\">\n"
552 " <leaf name=\"when-check\">\n"
553 " <type name=\"boolean\"/>\n"
554 " </leaf>\n"
555 " <leaf name=\"gated-data\">\n"
556 " <when condition=\"../when-check = 'true'\"/>\n"
557 " <type name=\"uint16\"/>\n"
558 " </leaf>\n"
559 " </container>\n"
560 " <augment target-node=\"/cont1\">\n"
561 " <leaf name=\"leaf17\">\n"
562 " <type name=\"string\"/>\n"
563 " </leaf>\n"
564 " </augment>\n"
565 " <rpc name=\"rpc1\">\n"
566 " <input>\n"
567 " <leaf name=\"leaf20\">\n"
568 " <type name=\"tdef1\"/>\n"
569 " </leaf>\n"
570 " </input>\n"
571 " <output>\n"
572 " <container name=\"cont2\">\n"
573 " <leaf name=\"leaf21\">\n"
574 " <type name=\"empty\"/>\n"
575 " </leaf>\n"
576 " </container>\n"
577 " </output>\n"
578 " </rpc>\n"
579 "</module>\n";
580
Radek Krejcia5bba312020-01-09 15:41:20 +0100581 char *printed;
Radek Krejci241f6b52020-05-21 18:13:49 +0200582 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100583
Radek Krejci241f6b52020-05-21 18:13:49 +0200584 assert_non_null(out = ly_out_new_memory(&printed, 0));
FredGand944bdc2019-11-05 21:57:07 +0800585 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
586
587 assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG));
Radek Krejcia5bba312020-01-09 15:41:20 +0100588 assert_int_equal(strlen(ori_res), lys_print(out, mod, LYS_OUT_YIN, 0, 0));
FredGand944bdc2019-11-05 21:57:07 +0800589 assert_string_equal(printed, ori_res);
Radek Krejcia5bba312020-01-09 15:41:20 +0100590
FredGand944bdc2019-11-05 21:57:07 +0800591 /*
Radek Krejcia5bba312020-01-09 15:41:20 +0100592 lyp_memory_clean(out);
FredGand944bdc2019-11-05 21:57:07 +0800593 assert_int_equal(strlen(compiled), lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0, 0));
594 assert_string_equal(printed, compiled);
FredGand944bdc2019-11-05 21:57:07 +0800595 */
596
Radek Krejcia5bba312020-01-09 15:41:20 +0100597 /* note that the printed is freed here, so it must not be freed via lyp_free()! */
598 free(printed);
599
FredGand944bdc2019-11-05 21:57:07 +0800600 *state = NULL;
Radek Krejci241f6b52020-05-21 18:13:49 +0200601 ly_out_free(out, NULL, 0);
FredGand944bdc2019-11-05 21:57:07 +0800602 ly_ctx_destroy(ctx, NULL);
603}
604
605int main(void)
606{
607 const struct CMUnitTest tests[] = {
608 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
609 };
610
611 return cmocka_run_group_tests(tests, NULL, NULL);
612}
613