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