FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 1 | /* |
| 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 Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 21 | #include <stdlib.h> |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 22 | #include <string.h> |
| 23 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 24 | #include "common.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 25 | #include "context.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 26 | #include "out.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 27 | #include "printer_schema.h" |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 28 | #include "tree_schema.h" |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 29 | |
| 30 | #define BUFSIZE 1024 |
| 31 | char logbuf[BUFSIZE] = {0}; |
| 32 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 33 | |
| 34 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 35 | #define ENABLE_LOGGER_CHECKING 1 |
| 36 | |
| 37 | #if ENABLE_LOGGER_CHECKING |
| 38 | static void |
| 39 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 40 | { |
| 41 | (void) level; /* unused */ |
| 42 | if (store) { |
| 43 | if (path && path[0]) { |
| 44 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 45 | } else { |
| 46 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 47 | } |
| 48 | if (store > 0) { |
| 49 | --store; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | #endif |
| 54 | |
| 55 | static int |
| 56 | logger_setup(void **state) |
| 57 | { |
| 58 | (void) state; /* unused */ |
| 59 | #if ENABLE_LOGGER_CHECKING |
| 60 | ly_set_log_clb(logger, 1); |
| 61 | #endif |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int |
| 66 | logger_teardown(void **state) |
| 67 | { |
| 68 | (void) state; /* unused */ |
| 69 | #if ENABLE_LOGGER_CHECKING |
| 70 | if (*state) { |
| 71 | fprintf(stderr, "%s\n", logbuf); |
| 72 | } |
| 73 | #endif |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | logbuf_clean(void) |
| 79 | { |
| 80 | logbuf[0] = '\0'; |
| 81 | } |
| 82 | |
| 83 | #if ENABLE_LOGGER_CHECKING |
| 84 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 85 | #else |
| 86 | # define logbuf_assert(str) |
| 87 | #endif |
| 88 | |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 89 | static void |
| 90 | test_module(void **state) |
| 91 | { |
| 92 | *state = test_module; |
| 93 | |
| 94 | struct ly_ctx *ctx = {0}; |
| 95 | const struct lys_module *mod; |
| 96 | |
| 97 | const char * orig = |
| 98 | "module all {\n" |
| 99 | " yang-version 1.1;\n" |
| 100 | " namespace \"urn:all\";\n" |
| 101 | " prefix all_mod;\n\n" |
| 102 | " import ietf-yang-types {\n" |
| 103 | " prefix yt;\n" |
| 104 | " revision-date 2013-07-15;\n" |
| 105 | " description\n" |
| 106 | " \"YANG types\";\n" |
| 107 | " reference\n" |
| 108 | " \"RFC reference\";\n" |
| 109 | " }\n\n" |
| 110 | " feature feat1 {\n" |
| 111 | " if-feature \"feat2\";\n" |
| 112 | " status obsolete;\n" |
| 113 | " }\n\n" |
| 114 | " feature feat2;\n" |
| 115 | " feature feat3;\n\n" |
| 116 | " identity ident2 {\n" |
| 117 | " base ident1;\n" |
| 118 | " }\n\n" |
| 119 | " identity ident1;\n\n" |
| 120 | " typedef tdef1 {\n" |
| 121 | " type tdef2 {\n" |
| 122 | " length \"3..9 | 30..40\";\n" |
| 123 | " pattern \"[ac]*\";\n" |
| 124 | " }\n" |
| 125 | " units \"none\";\n" |
| 126 | " default \"aaa\";\n" |
| 127 | " }\n\n" |
| 128 | " typedef tdef2 {\n" |
| 129 | " type string {\n" |
| 130 | " length \"2..10 | 20..50\";\n" |
| 131 | " pattern \"[ab]*\";\n" |
| 132 | " }\n" |
| 133 | " }\n\n" |
| 134 | " grouping group1 {\n" |
| 135 | " leaf leaf1 {\n" |
| 136 | " type int8;\n" |
| 137 | " }\n" |
| 138 | " }\n\n" |
| 139 | " container cont1 {\n" |
| 140 | " leaf leaf2 {\n" |
| 141 | " if-feature \"feat1\";\n" |
| 142 | " type int16;\n" |
| 143 | " status obsolete;\n" |
| 144 | " }\n\n" |
| 145 | " uses group1 {\n" |
| 146 | " if-feature \"feat2\";\n" |
| 147 | " refine \"leaf1\" {\n" |
| 148 | " if-feature \"feat3\";\n" |
| 149 | " must \"24 - 4 = number('20')\";\n" |
| 150 | " default \"25\";\n" |
| 151 | " config true;\n" |
| 152 | " mandatory false;\n" |
| 153 | " description\n" |
| 154 | " \"dsc\";\n" |
| 155 | " reference\n" |
| 156 | " \"none\";\n" |
| 157 | " }\n" |
| 158 | " }\n\n" |
| 159 | " leaf leaf3 {\n" |
| 160 | " type int32;\n" |
| 161 | " }\n\n" |
| 162 | " leaf leaf4 {\n" |
| 163 | " type int64 {\n" |
| 164 | " range \"1000 .. 50000\" {\n" |
| 165 | " error-message\n" |
| 166 | " \"Special error message.\";\n" |
| 167 | " error-app-tag \"special-tag\";\n" |
| 168 | " }\n" |
| 169 | " }\n" |
| 170 | " }\n\n" |
| 171 | " leaf leaf5 {\n" |
| 172 | " type uint8;\n" |
| 173 | " }\n\n" |
| 174 | " leaf leaf6 {\n" |
| 175 | " type uint16;\n" |
| 176 | " }\n\n" |
| 177 | " leaf leaf7 {\n" |
| 178 | " type uint32;\n" |
| 179 | " }\n\n" |
| 180 | " leaf leaf8 {\n" |
| 181 | " type uint64;\n" |
| 182 | " }\n\n" |
| 183 | " choice choic1 {\n" |
| 184 | " default \"leaf9b\";\n" |
| 185 | " leaf leaf9a {\n" |
| 186 | " type decimal64 {\n" |
| 187 | " fraction-digits 9;\n" |
| 188 | " }\n" |
| 189 | " }\n\n" |
| 190 | " leaf leaf9b {\n" |
| 191 | " type boolean;\n" |
| 192 | " default \"false\";\n" |
| 193 | " }\n" |
| 194 | " }\n\n" |
| 195 | " leaf leaf10 {\n" |
| 196 | " type boolean;\n" |
| 197 | " }\n\n" |
| 198 | " leaf leaf11 {\n" |
| 199 | " type enumeration {\n" |
| 200 | " enum \"one\";\n" |
| 201 | " enum \"two\";\n" |
| 202 | " enum \"five\" {\n" |
| 203 | " value 5;\n" |
| 204 | " }\n" |
| 205 | " }\n" |
| 206 | " }\n\n" |
| 207 | " leaf leaf12 {\n" |
| 208 | " type bits {\n" |
| 209 | " bit flag0 {\n" |
| 210 | " position 0;\n" |
| 211 | " }\n" |
| 212 | " bit flag1;\n" |
| 213 | " bit flag2 {\n" |
| 214 | " position 2;\n" |
| 215 | " }\n" |
| 216 | " bit flag3 {\n" |
| 217 | " position 3;\n" |
| 218 | " }\n" |
| 219 | " }\n" |
| 220 | " default \"flag0 flag3\";\n" |
| 221 | " }\n\n" |
| 222 | " leaf leaf13 {\n" |
| 223 | " type binary;\n" |
| 224 | " }\n\n" |
| 225 | " leaf leaf14 {\n" |
| 226 | " type leafref {\n" |
| 227 | " path \"/cont1/leaf17\";\n" |
| 228 | " }\n" |
| 229 | " }\n\n" |
| 230 | " leaf leaf15 {\n" |
| 231 | " type empty;\n" |
| 232 | " }\n\n" |
| 233 | " leaf leaf16 {\n" |
| 234 | " type union {\n" |
| 235 | " type instance-identifier {\n" |
| 236 | " require-instance true;\n" |
| 237 | " }\n" |
| 238 | " type int8;\n" |
| 239 | " }\n" |
| 240 | " }\n\n" |
| 241 | " list list1 {\n" |
| 242 | " key \"leaf18\";\n" |
| 243 | " unique \"leaf19\";\n" |
| 244 | " min-elements 1;\n" |
| 245 | " max-elements 20;\n" |
| 246 | " leaf leaf18 {\n" |
| 247 | " type string;\n" |
| 248 | " }\n\n" |
| 249 | " leaf leaf19 {\n" |
| 250 | " type uint32;\n" |
| 251 | " }\n\n" |
| 252 | " anyxml axml1;\n" |
| 253 | " anydata adata1;\n\n" |
| 254 | " action act1 {\n" |
| 255 | " input {\n" |
| 256 | " leaf leaf24 {\n" |
| 257 | " type string;\n" |
| 258 | " }\n" |
| 259 | " }\n\n" |
| 260 | " output {\n" |
| 261 | " leaf leaf25 {\n" |
| 262 | " type string;\n" |
| 263 | " }\n" |
| 264 | " }\n" |
| 265 | " }\n\n" |
| 266 | " notification notif1 {\n" |
| 267 | " leaf leaf26 {\n" |
| 268 | " type string;\n" |
| 269 | " }\n" |
| 270 | " }\n" |
| 271 | " }\n\n" |
| 272 | " leaf-list llist1 {\n" |
| 273 | " type tdef1;\n" |
| 274 | " ordered-by user;\n" |
| 275 | " }\n\n" |
| 276 | " list list2 {\n" |
| 277 | " key \"leaf27 leaf28\";\n" |
| 278 | " leaf leaf27 {\n" |
| 279 | " type uint8;\n" |
| 280 | " }\n\n" |
| 281 | " leaf leaf28 {\n" |
| 282 | " type uint8;\n" |
| 283 | " }\n" |
| 284 | " }\n\n" |
| 285 | " leaf leaf29 {\n" |
| 286 | " type instance-identifier;\n" |
| 287 | " }\n\n" |
| 288 | " container must-deviations-container {\n" |
| 289 | " presence \"Allows deviations on the leaf\";\n" |
| 290 | " leaf leaf30 {\n" |
| 291 | " type string;\n" |
| 292 | " }\n" |
| 293 | " }\n\n" |
| 294 | " leaf leaf23 {\n" |
| 295 | " type empty;\n" |
| 296 | " }\n" |
| 297 | " }\n\n" |
| 298 | " augment \"/cont1\" {\n" |
| 299 | " leaf leaf17 {\n" |
| 300 | " type string;\n" |
| 301 | " }\n" |
| 302 | " }\n\n" |
| 303 | " rpc rpc1 {\n" |
| 304 | " input {\n" |
| 305 | " leaf leaf20 {\n" |
| 306 | " type tdef1;\n" |
| 307 | " }\n" |
| 308 | " }\n\n" |
| 309 | " output {\n" |
| 310 | " container cont2 {\n" |
| 311 | " leaf leaf21 {\n" |
| 312 | " type empty;\n" |
| 313 | " }\n" |
| 314 | " }\n" |
| 315 | " }\n" |
| 316 | " }\n\n" |
| 317 | " container test-when {\n" |
| 318 | " leaf when-check {\n" |
| 319 | " type boolean;\n" |
| 320 | " }\n\n" |
| 321 | " leaf gated-data {\n" |
| 322 | " when \"../when-check = 'true'\";\n" |
| 323 | " type uint16;\n" |
| 324 | " }\n" |
| 325 | " }\n\n" |
| 326 | " extension c-define {\n" |
| 327 | " description\n" |
| 328 | " \"Takes as an argument a name string.\n" |
| 329 | " Makes the code generator use the given name\n" |
| 330 | " in the #define.\";\n" |
| 331 | " argument \"name\";\n" |
| 332 | " }\n" |
| 333 | "}\n"; |
| 334 | |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 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 Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 583 | char *printed; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 584 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 585 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 586 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 587 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 588 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 589 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 590 | assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YIN, 0, 0)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 591 | assert_int_equal(strlen(ori_res), ly_out_printed(out)); |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 592 | assert_string_equal(printed, ori_res); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 593 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 594 | *state = NULL; |
| 595 | ly_out_free(out, NULL, 1); |
| 596 | ly_ctx_destroy(ctx, NULL); |
| 597 | } |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 598 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 599 | static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 600 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 601 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 602 | { |
| 603 | *module_data = user_data; |
| 604 | *format = LYS_IN_YIN; |
| 605 | *free_module_data = NULL; |
| 606 | return LY_SUCCESS; |
| 607 | } |
| 608 | |
| 609 | static void |
| 610 | test_submodule(void **state) |
| 611 | { |
| 612 | *state = test_module; |
| 613 | |
| 614 | struct ly_ctx *ctx = {0}; |
| 615 | const struct lys_module *mod; |
| 616 | |
| 617 | const char *mod_yin = |
| 618 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 619 | "<module name=\"a\"\n" |
| 620 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 621 | " xmlns:a_mod=\"urn:a\">\n" |
| 622 | " <yang-version value=\"1.1\"/>\n" |
| 623 | " <namespace uri=\"urn:a\"/>\n" |
| 624 | " <prefix value=\"a_mod\"/>\n" |
| 625 | " <include module=\"a-sub\"/>\n" |
| 626 | "</module>\n"; |
| 627 | |
| 628 | char *submod_yin = |
| 629 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 630 | "<submodule name=\"a-sub\"\n" |
| 631 | " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n" |
| 632 | " xmlns:a_mod=\"urn:a\"\n" |
| 633 | " xmlns:yt=\"urn:ietf:params:xml:ns:yang:ietf-yang-types\">\n" |
| 634 | " <yang-version value=\"1.1\"/>\n" |
| 635 | " <belongs-to module=\"a\">\n" |
| 636 | " <prefix value=\"a_mod\"/>\n" |
| 637 | " </belongs-to>\n" |
| 638 | " <import module=\"ietf-yang-types\">\n" |
| 639 | " <prefix value=\"yt\"/>\n" |
| 640 | " <revision-date date=\"2013-07-15\"/>\n" |
| 641 | " </import>\n\n" |
| 642 | " <description>\n" |
| 643 | " <text>YANG types</text>\n" |
| 644 | " </description>\n" |
| 645 | " <reference>\n" |
| 646 | " <text>RFC reference</text>\n" |
| 647 | " </reference>\n" |
| 648 | "</submodule>\n"; |
| 649 | |
| 650 | char *printed; |
| 651 | struct ly_out *out; |
| 652 | |
| 653 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
| 654 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 655 | |
| 656 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, submod_yin); |
| 657 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, mod_yin, LYS_IN_YIN, &mod)); |
| 658 | assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod, mod->parsed->includes[0].submodule, LYS_OUT_YIN, 0, 0)); |
| 659 | assert_int_equal(strlen(submod_yin), ly_out_printed(out)); |
| 660 | assert_string_equal(printed, submod_yin); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 661 | |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 662 | *state = NULL; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 663 | ly_out_free(out, NULL, 1); |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 664 | ly_ctx_destroy(ctx, NULL); |
| 665 | } |
| 666 | |
| 667 | int main(void) |
| 668 | { |
| 669 | const struct CMUnitTest tests[] = { |
| 670 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 671 | cmocka_unit_test_setup_teardown(test_submodule, logger_setup, logger_teardown), |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 672 | }; |
| 673 | |
| 674 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 675 | } |
| 676 | |