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