Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file test_validation.c |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 4 | * @brief unit tests for functions from validation.c |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 5 | * |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 6 | * Copyright (c) 2020 CESNET, z.s.p.o. |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 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 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 23 | #include "context.h" |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 24 | #include "parser.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 25 | #include "parser_data.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 26 | #include "printer.h" |
| 27 | #include "printer_data.h" |
| 28 | #include "tests/config.h" |
| 29 | #include "tree_schema.h" |
| 30 | #include "tree_data_internal.h" |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 31 | |
| 32 | #define BUFSIZE 1024 |
| 33 | char logbuf[BUFSIZE] = {0}; |
| 34 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 35 | |
| 36 | struct ly_ctx *ctx; /* context for tests */ |
| 37 | |
| 38 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 39 | #define ENABLE_LOGGER_CHECKING 1 |
| 40 | |
| 41 | #if ENABLE_LOGGER_CHECKING |
| 42 | static void |
| 43 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 44 | { |
| 45 | (void) level; /* unused */ |
| 46 | if (store) { |
| 47 | if (path && path[0]) { |
| 48 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 49 | } else { |
| 50 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 51 | } |
| 52 | if (store > 0) { |
| 53 | --store; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | #endif |
| 58 | |
| 59 | static int |
| 60 | setup(void **state) |
| 61 | { |
| 62 | (void) state; /* unused */ |
| 63 | |
| 64 | const char *schema_a = |
| 65 | "module a {" |
| 66 | "namespace urn:tests:a;" |
| 67 | "prefix a;" |
| 68 | "yang-version 1.1;" |
| 69 | |
| 70 | "container cont {" |
| 71 | "leaf a {" |
| 72 | "when \"../../c = 'val_c'\";" |
| 73 | "type string;" |
| 74 | "}" |
| 75 | "leaf b {" |
| 76 | "type string;" |
| 77 | "}" |
| 78 | "}" |
| 79 | "leaf c {" |
| 80 | "when \"/cont/b = 'val_b'\";" |
| 81 | "type string;" |
| 82 | "}" |
| 83 | "}"; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 84 | const char *schema_b = |
| 85 | "module b {" |
| 86 | "namespace urn:tests:b;" |
| 87 | "prefix b;" |
| 88 | "yang-version 1.1;" |
| 89 | |
| 90 | "choice choic {" |
| 91 | "mandatory true;" |
| 92 | "leaf a {" |
| 93 | "type string;" |
| 94 | "}" |
| 95 | "case b {" |
| 96 | "leaf l {" |
| 97 | "type string;" |
| 98 | "}" |
| 99 | "}" |
| 100 | "}" |
| 101 | "leaf c {" |
| 102 | "mandatory true;" |
| 103 | "type string;" |
| 104 | "}" |
| 105 | "leaf d {" |
| 106 | "type empty;" |
| 107 | "}" |
| 108 | "}"; |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 109 | const char *schema_c = |
| 110 | "module c {" |
| 111 | "namespace urn:tests:c;" |
| 112 | "prefix c;" |
| 113 | "yang-version 1.1;" |
| 114 | |
| 115 | "choice choic {" |
| 116 | "leaf a {" |
| 117 | "type string;" |
| 118 | "}" |
| 119 | "case b {" |
| 120 | "leaf-list l {" |
| 121 | "min-elements 3;" |
| 122 | "type string;" |
| 123 | "}" |
| 124 | "}" |
| 125 | "}" |
| 126 | "list lt {" |
| 127 | "max-elements 4;" |
| 128 | "key \"k\";" |
| 129 | "leaf k {" |
| 130 | "type string;" |
| 131 | "}" |
| 132 | "}" |
| 133 | "leaf d {" |
| 134 | "type empty;" |
| 135 | "}" |
| 136 | "}"; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 137 | const char *schema_d = |
| 138 | "module d {" |
| 139 | "namespace urn:tests:d;" |
| 140 | "prefix d;" |
| 141 | "yang-version 1.1;" |
| 142 | |
| 143 | "list lt {" |
| 144 | "key \"k\";" |
| 145 | "unique \"l1\";" |
| 146 | "leaf k {" |
| 147 | "type string;" |
| 148 | "}" |
| 149 | "leaf l1 {" |
| 150 | "type string;" |
| 151 | "}" |
| 152 | "}" |
| 153 | "list lt2 {" |
| 154 | "key \"k\";" |
| 155 | "unique \"cont/l2 l4\";" |
| 156 | "unique \"l5 l6\";" |
| 157 | "leaf k {" |
| 158 | "type string;" |
| 159 | "}" |
| 160 | "container cont {" |
| 161 | "leaf l2 {" |
| 162 | "type string;" |
| 163 | "}" |
| 164 | "}" |
| 165 | "leaf l4 {" |
| 166 | "type string;" |
| 167 | "}" |
| 168 | "leaf l5 {" |
| 169 | "type string;" |
| 170 | "}" |
| 171 | "leaf l6 {" |
| 172 | "type string;" |
| 173 | "}" |
| 174 | "list lt3 {" |
| 175 | "key \"kk\";" |
| 176 | "unique \"l3\";" |
| 177 | "leaf kk {" |
| 178 | "type string;" |
| 179 | "}" |
| 180 | "leaf l3 {" |
| 181 | "type string;" |
| 182 | "}" |
| 183 | "}" |
| 184 | "}" |
| 185 | "}"; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 186 | const char *schema_e = |
| 187 | "module e {" |
| 188 | "namespace urn:tests:e;" |
| 189 | "prefix e;" |
| 190 | "yang-version 1.1;" |
| 191 | |
| 192 | "choice choic {" |
| 193 | "leaf a {" |
| 194 | "type string;" |
| 195 | "}" |
| 196 | "case b {" |
| 197 | "leaf-list l {" |
| 198 | "type string;" |
| 199 | "}" |
| 200 | "}" |
| 201 | "}" |
| 202 | "list lt {" |
| 203 | "key \"k\";" |
| 204 | "leaf k {" |
| 205 | "type string;" |
| 206 | "}" |
| 207 | "}" |
| 208 | "leaf d {" |
| 209 | "type uint32;" |
| 210 | "}" |
| 211 | "leaf-list ll {" |
| 212 | "type string;" |
| 213 | "}" |
| 214 | "container cont {" |
| 215 | "list lt {" |
| 216 | "key \"k\";" |
| 217 | "leaf k {" |
| 218 | "type string;" |
| 219 | "}" |
| 220 | "}" |
| 221 | "leaf d {" |
| 222 | "type uint32;" |
| 223 | "}" |
| 224 | "leaf-list ll {" |
| 225 | "type string;" |
| 226 | "}" |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 227 | "leaf-list ll2 {" |
| 228 | "type enumeration {" |
| 229 | "enum one;" |
| 230 | "enum two;" |
| 231 | "}" |
| 232 | "}" |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 233 | "}" |
| 234 | "}"; |
| 235 | const char *schema_f = |
| 236 | "module f {" |
| 237 | "namespace urn:tests:f;" |
| 238 | "prefix f;" |
| 239 | "yang-version 1.1;" |
| 240 | |
| 241 | "choice choic {" |
| 242 | "default \"c\";" |
| 243 | "leaf a {" |
| 244 | "type string;" |
| 245 | "}" |
| 246 | "case b {" |
| 247 | "leaf l {" |
| 248 | "type string;" |
| 249 | "}" |
| 250 | "}" |
| 251 | "case c {" |
| 252 | "leaf-list ll1 {" |
| 253 | "type string;" |
| 254 | "default \"def1\";" |
| 255 | "default \"def2\";" |
| 256 | "default \"def3\";" |
| 257 | "}" |
| 258 | "}" |
| 259 | "}" |
| 260 | "leaf d {" |
| 261 | "type uint32;" |
| 262 | "default 15;" |
| 263 | "}" |
| 264 | "leaf-list ll2 {" |
| 265 | "type string;" |
| 266 | "default \"dflt1\";" |
| 267 | "default \"dflt2\";" |
| 268 | "}" |
| 269 | "container cont {" |
| 270 | "choice choic {" |
| 271 | "default \"c\";" |
| 272 | "leaf a {" |
| 273 | "type string;" |
| 274 | "}" |
| 275 | "case b {" |
| 276 | "leaf l {" |
| 277 | "type string;" |
| 278 | "}" |
| 279 | "}" |
| 280 | "case c {" |
| 281 | "leaf-list ll1 {" |
| 282 | "type string;" |
| 283 | "default \"def1\";" |
| 284 | "default \"def2\";" |
| 285 | "default \"def3\";" |
| 286 | "}" |
| 287 | "}" |
| 288 | "}" |
| 289 | "leaf d {" |
| 290 | "type uint32;" |
| 291 | "default 15;" |
| 292 | "}" |
| 293 | "leaf-list ll2 {" |
| 294 | "type string;" |
| 295 | "default \"dflt1\";" |
| 296 | "default \"dflt2\";" |
| 297 | "}" |
| 298 | "}" |
| 299 | "}"; |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 300 | const char *schema_g = |
| 301 | "module g {" |
| 302 | "namespace urn:tests:g;" |
| 303 | "prefix g;" |
| 304 | "yang-version 1.1;" |
| 305 | |
| 306 | "feature f1;" |
| 307 | "feature f2;" |
| 308 | "feature f3;" |
| 309 | |
| 310 | "container cont {" |
| 311 | "if-feature \"f1\";" |
| 312 | "choice choic {" |
| 313 | "if-feature \"f2 or f3\";" |
| 314 | "leaf a {" |
| 315 | "type string;" |
| 316 | "}" |
| 317 | "case b {" |
| 318 | "if-feature \"f2 and f1\";" |
| 319 | "leaf l {" |
| 320 | "type string;" |
| 321 | "}" |
| 322 | "}" |
| 323 | "}" |
| 324 | "leaf d {" |
| 325 | "type uint32;" |
| 326 | "}" |
| 327 | "container cont2 {" |
| 328 | "if-feature \"f2\";" |
| 329 | "leaf e {" |
| 330 | "type string;" |
| 331 | "}" |
| 332 | "}" |
| 333 | "}" |
| 334 | "}"; |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 335 | const char *schema_h = |
| 336 | "module h {" |
| 337 | "namespace urn:tests:h;" |
| 338 | "prefix h;" |
| 339 | "yang-version 1.1;" |
| 340 | |
| 341 | "container cont {" |
| 342 | "container cont2 {" |
| 343 | "config false;" |
| 344 | "leaf l {" |
| 345 | "type string;" |
| 346 | "}" |
| 347 | "}" |
| 348 | "}" |
| 349 | "}"; |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 350 | const char *schema_i = |
| 351 | "module i {" |
| 352 | "namespace urn:tests:i;" |
| 353 | "prefix i;" |
| 354 | "yang-version 1.1;" |
| 355 | |
| 356 | "container cont {" |
| 357 | "leaf l {" |
| 358 | "type string;" |
| 359 | "}" |
| 360 | "leaf l2 {" |
| 361 | "must \"../l = 'right'\";" |
| 362 | "type string;" |
| 363 | "}" |
| 364 | "}" |
| 365 | "}"; |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 366 | const char *schema_j = |
| 367 | "module j {" |
| 368 | "namespace urn:tests:j;" |
| 369 | "prefix j;" |
| 370 | "yang-version 1.1;" |
| 371 | |
| 372 | "feature feat1;" |
| 373 | |
| 374 | "container cont {" |
| 375 | "must \"false()\";" |
| 376 | "list l1 {" |
| 377 | "key \"k\";" |
| 378 | "leaf k {" |
| 379 | "type string;" |
| 380 | "}" |
| 381 | "action act {" |
| 382 | "if-feature feat1;" |
| 383 | "input {" |
| 384 | "must \"../../lf1 = 'true'\";" |
| 385 | "leaf lf2 {" |
| 386 | "type leafref {" |
| 387 | "path /lf3;" |
| 388 | "}" |
| 389 | "}" |
| 390 | "}" |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 391 | "output {" |
| 392 | "must \"../../lf1 = 'true2'\";" |
| 393 | "leaf lf2 {" |
| 394 | "type leafref {" |
| 395 | "path /lf4;" |
| 396 | "}" |
| 397 | "}" |
| 398 | "}" |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 399 | "}" |
| 400 | "}" |
| 401 | |
| 402 | "leaf lf1 {" |
| 403 | "type string;" |
| 404 | "}" |
| 405 | "}" |
| 406 | |
| 407 | "leaf lf3 {" |
| 408 | "type string;" |
| 409 | "}" |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 410 | |
| 411 | "leaf lf4 {" |
| 412 | "type string;" |
| 413 | "}" |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 414 | "}"; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 415 | |
| 416 | #if ENABLE_LOGGER_CHECKING |
| 417 | ly_set_log_clb(logger, 1); |
| 418 | #endif |
| 419 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 420 | assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx)); |
| 421 | assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01")); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 422 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_a, LYS_IN_YANG, NULL)); |
| 423 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_b, LYS_IN_YANG, NULL)); |
| 424 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_c, LYS_IN_YANG, NULL)); |
| 425 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_d, LYS_IN_YANG, NULL)); |
| 426 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_e, LYS_IN_YANG, NULL)); |
| 427 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_f, LYS_IN_YANG, NULL)); |
| 428 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_g, LYS_IN_YANG, NULL)); |
| 429 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_h, LYS_IN_YANG, NULL)); |
| 430 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_i, LYS_IN_YANG, NULL)); |
| 431 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_j, LYS_IN_YANG, NULL)); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static int |
| 437 | teardown(void **state) |
| 438 | { |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 439 | (void)state; |
| 440 | ly_ctx_destroy(ctx, NULL); |
| 441 | ctx = NULL; |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static int |
| 447 | teardown_s(void **state) |
| 448 | { |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 449 | #if ENABLE_LOGGER_CHECKING |
| 450 | if (*state) { |
| 451 | fprintf(stderr, "%s\n", logbuf); |
| 452 | } |
| 453 | #else |
| 454 | (void) state; /* unused */ |
| 455 | #endif |
| 456 | |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | void |
| 461 | logbuf_clean(void) |
| 462 | { |
| 463 | logbuf[0] = '\0'; |
| 464 | } |
| 465 | |
| 466 | #if ENABLE_LOGGER_CHECKING |
| 467 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 468 | #else |
| 469 | # define logbuf_assert(str) |
| 470 | #endif |
| 471 | |
| 472 | static void |
| 473 | test_when(void **state) |
| 474 | { |
| 475 | *state = test_when; |
| 476 | |
| 477 | const char *data; |
| 478 | struct lyd_node *tree; |
| 479 | |
| 480 | data = "<c xmlns=\"urn:tests:a\">hey</c>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 481 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 482 | assert_null(tree); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 483 | logbuf_assert("When condition \"/cont/b = 'val_b'\" not satisfied. /a:c"); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 484 | |
| 485 | data = "<cont xmlns=\"urn:tests:a\"><b>val_b</b></cont><c xmlns=\"urn:tests:a\">hey</c>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 486 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 487 | assert_non_null(tree); |
| 488 | assert_string_equal("c", tree->next->schema->name); |
| 489 | assert_int_equal(LYD_WHEN_TRUE, tree->next->flags); |
| 490 | lyd_free_all(tree); |
| 491 | |
| 492 | data = "<cont xmlns=\"urn:tests:a\"><a>val</a><b>val_b</b></cont><c xmlns=\"urn:tests:a\">val_c</c>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 493 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 494 | assert_non_null(tree); |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 495 | assert_string_equal("a", lyd_node_children(tree, 0)->schema->name); |
| 496 | assert_int_equal(LYD_WHEN_TRUE, lyd_node_children(tree, 0)->flags); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 497 | assert_string_equal("c", tree->next->schema->name); |
| 498 | assert_int_equal(LYD_WHEN_TRUE, tree->next->flags); |
| 499 | lyd_free_all(tree); |
| 500 | |
| 501 | *state = NULL; |
| 502 | } |
| 503 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 504 | static void |
| 505 | test_mandatory(void **state) |
| 506 | { |
| 507 | *state = test_mandatory; |
| 508 | |
| 509 | const char *data; |
| 510 | struct lyd_node *tree; |
| 511 | |
| 512 | data = "<d xmlns=\"urn:tests:b\"/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 513 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 514 | assert_null(tree); |
| 515 | logbuf_assert("Mandatory node \"choic\" instance does not exist. /b:choic"); |
| 516 | |
| 517 | data = "<l xmlns=\"urn:tests:b\">string</l><d xmlns=\"urn:tests:b\"/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 518 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 519 | assert_null(tree); |
| 520 | logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c"); |
| 521 | |
| 522 | data = "<a xmlns=\"urn:tests:b\">string</a>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 523 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 524 | assert_null(tree); |
| 525 | logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c"); |
| 526 | |
| 527 | data = "<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 528 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 529 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 530 | lyd_free_siblings(tree); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 531 | |
| 532 | *state = NULL; |
| 533 | } |
| 534 | |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 535 | static void |
| 536 | test_minmax(void **state) |
| 537 | { |
| 538 | *state = test_minmax; |
| 539 | |
| 540 | const char *data; |
| 541 | struct lyd_node *tree; |
| 542 | |
| 543 | data = "<d xmlns=\"urn:tests:c\"/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 544 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 545 | assert_null(tree); |
| 546 | logbuf_assert("Too few \"l\" instances. /c:choic/b/l"); |
| 547 | |
| 548 | data = |
| 549 | "<l xmlns=\"urn:tests:c\">val1</l>" |
| 550 | "<l xmlns=\"urn:tests:c\">val2</l>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 551 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 552 | assert_null(tree); |
| 553 | logbuf_assert("Too few \"l\" instances. /c:choic/b/l"); |
| 554 | |
| 555 | data = |
| 556 | "<l xmlns=\"urn:tests:c\">val1</l>" |
| 557 | "<l xmlns=\"urn:tests:c\">val2</l>" |
| 558 | "<l xmlns=\"urn:tests:c\">val3</l>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 559 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 560 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 561 | lyd_free_siblings(tree); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 562 | |
| 563 | data = |
| 564 | "<l xmlns=\"urn:tests:c\">val1</l>" |
| 565 | "<l xmlns=\"urn:tests:c\">val2</l>" |
| 566 | "<l xmlns=\"urn:tests:c\">val3</l>" |
| 567 | "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>" |
| 568 | "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>" |
| 569 | "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>" |
| 570 | "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>" |
| 571 | "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 572 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 573 | assert_null(tree); |
| 574 | logbuf_assert("Too many \"lt\" instances. /c:lt"); |
| 575 | |
| 576 | *state = NULL; |
| 577 | } |
| 578 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 579 | static void |
| 580 | test_unique(void **state) |
| 581 | { |
| 582 | *state = test_unique; |
| 583 | |
| 584 | const char *data; |
| 585 | struct lyd_node *tree; |
| 586 | |
| 587 | data = |
| 588 | "<lt xmlns=\"urn:tests:d\">" |
| 589 | "<k>val1</k>" |
| 590 | "<l1>same</l1>" |
| 591 | "</lt>" |
| 592 | "<lt xmlns=\"urn:tests:d\">" |
| 593 | "<k>val2</k>" |
| 594 | "</lt>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 595 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 596 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 597 | lyd_free_siblings(tree); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 598 | |
| 599 | data = |
| 600 | "<lt xmlns=\"urn:tests:d\">" |
| 601 | "<k>val1</k>" |
| 602 | "<l1>same</l1>" |
| 603 | "</lt>" |
| 604 | "<lt xmlns=\"urn:tests:d\">" |
| 605 | "<k>val2</k>" |
| 606 | "<l1>not-same</l1>" |
| 607 | "</lt>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 608 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 609 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 610 | lyd_free_siblings(tree); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 611 | |
| 612 | data = |
| 613 | "<lt xmlns=\"urn:tests:d\">" |
| 614 | "<k>val1</k>" |
| 615 | "<l1>same</l1>" |
| 616 | "</lt>" |
| 617 | "<lt xmlns=\"urn:tests:d\">" |
| 618 | "<k>val2</k>" |
| 619 | "<l1>same</l1>" |
| 620 | "</lt>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 621 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 622 | assert_null(tree); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 623 | logbuf_assert("Unique data leaf(s) \"l1\" not satisfied in \"/d:lt[k='val1']\" and \"/d:lt[k='val2']\". /d:lt[k='val2']"); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 624 | |
| 625 | /* now try with more instances */ |
| 626 | data = |
| 627 | "<lt xmlns=\"urn:tests:d\">" |
| 628 | "<k>val1</k>" |
| 629 | "<l1>1</l1>" |
| 630 | "</lt>" |
| 631 | "<lt xmlns=\"urn:tests:d\">" |
| 632 | "<k>val2</k>" |
| 633 | "<l1>2</l1>" |
| 634 | "</lt>" |
| 635 | "<lt xmlns=\"urn:tests:d\">" |
| 636 | "<k>val3</k>" |
| 637 | "<l1>3</l1>" |
| 638 | "</lt>" |
| 639 | "<lt xmlns=\"urn:tests:d\">" |
| 640 | "<k>val4</k>" |
| 641 | "<l1>4</l1>" |
| 642 | "</lt>" |
| 643 | "<lt xmlns=\"urn:tests:d\">" |
| 644 | "<k>val5</k>" |
| 645 | "<l1>5</l1>" |
| 646 | "</lt>" |
| 647 | "<lt xmlns=\"urn:tests:d\">" |
| 648 | "<k>val6</k>" |
| 649 | "<l1>6</l1>" |
| 650 | "</lt>" |
| 651 | "<lt xmlns=\"urn:tests:d\">" |
| 652 | "<k>val7</k>" |
| 653 | "<l1>7</l1>" |
| 654 | "</lt>" |
| 655 | "<lt xmlns=\"urn:tests:d\">" |
| 656 | "<k>val8</k>" |
| 657 | "<l1>8</l1>" |
| 658 | "</lt>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 659 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 660 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 661 | lyd_free_siblings(tree); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 662 | |
| 663 | data = |
| 664 | "<lt xmlns=\"urn:tests:d\">" |
| 665 | "<k>val1</k>" |
| 666 | "<l1>1</l1>" |
| 667 | "</lt>" |
| 668 | "<lt xmlns=\"urn:tests:d\">" |
| 669 | "<k>val2</k>" |
| 670 | "<l1>2</l1>" |
| 671 | "</lt>" |
| 672 | "<lt xmlns=\"urn:tests:d\">" |
| 673 | "<k>val3</k>" |
| 674 | "<l1>3</l1>" |
| 675 | "</lt>" |
| 676 | "<lt xmlns=\"urn:tests:d\">" |
| 677 | "<k>val4</k>" |
| 678 | "</lt>" |
| 679 | "<lt xmlns=\"urn:tests:d\">" |
| 680 | "<k>val5</k>" |
| 681 | "<l1>5</l1>" |
| 682 | "</lt>" |
| 683 | "<lt xmlns=\"urn:tests:d\">" |
| 684 | "<k>val6</k>" |
| 685 | "<l1>6</l1>" |
| 686 | "</lt>" |
| 687 | "<lt xmlns=\"urn:tests:d\">" |
| 688 | "<k>val7</k>" |
| 689 | "</lt>" |
| 690 | "<lt xmlns=\"urn:tests:d\">" |
| 691 | "<k>val8</k>" |
| 692 | "</lt>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 693 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 694 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 695 | lyd_free_siblings(tree); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 696 | |
| 697 | data = |
| 698 | "<lt xmlns=\"urn:tests:d\">" |
| 699 | "<k>val1</k>" |
| 700 | "<l1>1</l1>" |
| 701 | "</lt>" |
| 702 | "<lt xmlns=\"urn:tests:d\">" |
| 703 | "<k>val2</k>" |
| 704 | "<l1>2</l1>" |
| 705 | "</lt>" |
| 706 | "<lt xmlns=\"urn:tests:d\">" |
| 707 | "<k>val3</k>" |
| 708 | "</lt>" |
| 709 | "<lt xmlns=\"urn:tests:d\">" |
| 710 | "<k>val4</k>" |
| 711 | "<l1>4</l1>" |
| 712 | "</lt>" |
| 713 | "<lt xmlns=\"urn:tests:d\">" |
| 714 | "<k>val5</k>" |
| 715 | "</lt>" |
| 716 | "<lt xmlns=\"urn:tests:d\">" |
| 717 | "<k>val6</k>" |
| 718 | "</lt>" |
| 719 | "<lt xmlns=\"urn:tests:d\">" |
| 720 | "<k>val7</k>" |
| 721 | "<l1>2</l1>" |
| 722 | "</lt>" |
| 723 | "<lt xmlns=\"urn:tests:d\">" |
| 724 | "<k>val8</k>" |
| 725 | "<l1>8</l1>" |
| 726 | "</lt>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 727 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 728 | assert_null(tree); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 729 | logbuf_assert("Unique data leaf(s) \"l1\" not satisfied in \"/d:lt[k='val7']\" and \"/d:lt[k='val2']\". /d:lt[k='val2']"); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 730 | |
| 731 | *state = NULL; |
| 732 | } |
| 733 | |
| 734 | static void |
| 735 | test_unique_nested(void **state) |
| 736 | { |
| 737 | *state = test_unique_nested; |
| 738 | |
| 739 | const char *data; |
| 740 | struct lyd_node *tree; |
| 741 | |
| 742 | /* nested list uniquest are compared only with instances in the same parent list instance */ |
| 743 | data = |
| 744 | "<lt2 xmlns=\"urn:tests:d\">" |
| 745 | "<k>val1</k>" |
| 746 | "<cont>" |
| 747 | "<l2>1</l2>" |
| 748 | "</cont>" |
| 749 | "<l4>1</l4>" |
| 750 | "</lt2>" |
| 751 | "<lt2 xmlns=\"urn:tests:d\">" |
| 752 | "<k>val2</k>" |
| 753 | "<cont>" |
| 754 | "<l2>2</l2>" |
| 755 | "</cont>" |
| 756 | "<l4>2</l4>" |
| 757 | "<lt3>" |
| 758 | "<kk>val1</kk>" |
| 759 | "<l3>1</l3>" |
| 760 | "</lt3>" |
| 761 | "<lt3>" |
| 762 | "<kk>val2</kk>" |
| 763 | "<l3>2</l3>" |
| 764 | "</lt3>" |
| 765 | "</lt2>" |
| 766 | "<lt2 xmlns=\"urn:tests:d\">" |
| 767 | "<k>val3</k>" |
| 768 | "<cont>" |
| 769 | "<l2>3</l2>" |
| 770 | "</cont>" |
| 771 | "<l4>3</l4>" |
| 772 | "<lt3>" |
| 773 | "<kk>val1</kk>" |
| 774 | "<l3>2</l3>" |
| 775 | "</lt3>" |
| 776 | "</lt2>" |
| 777 | "<lt2 xmlns=\"urn:tests:d\">" |
| 778 | "<k>val4</k>" |
| 779 | "<cont>" |
| 780 | "<l2>4</l2>" |
| 781 | "</cont>" |
| 782 | "<l4>4</l4>" |
| 783 | "<lt3>" |
| 784 | "<kk>val1</kk>" |
| 785 | "<l3>3</l3>" |
| 786 | "</lt3>" |
| 787 | "</lt2>" |
| 788 | "<lt2 xmlns=\"urn:tests:d\">" |
| 789 | "<k>val5</k>" |
| 790 | "<cont>" |
| 791 | "<l2>5</l2>" |
| 792 | "</cont>" |
| 793 | "<l4>5</l4>" |
| 794 | "<lt3>" |
| 795 | "<kk>val1</kk>" |
| 796 | "<l3>3</l3>" |
| 797 | "</lt3>" |
| 798 | "</lt2>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 799 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 800 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 801 | lyd_free_siblings(tree); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 802 | |
| 803 | data = |
| 804 | "<lt2 xmlns=\"urn:tests:d\">" |
| 805 | "<k>val1</k>" |
| 806 | "<cont>" |
| 807 | "<l2>1</l2>" |
| 808 | "</cont>" |
| 809 | "<l4>1</l4>" |
| 810 | "</lt2>" |
| 811 | "<lt2 xmlns=\"urn:tests:d\">" |
| 812 | "<k>val2</k>" |
| 813 | "<cont>" |
| 814 | "<l2>2</l2>" |
| 815 | "</cont>" |
| 816 | "<lt3>" |
| 817 | "<kk>val1</kk>" |
| 818 | "<l3>1</l3>" |
| 819 | "</lt3>" |
| 820 | "<lt3>" |
| 821 | "<kk>val2</kk>" |
| 822 | "<l3>2</l3>" |
| 823 | "</lt3>" |
| 824 | "<lt3>" |
| 825 | "<kk>val3</kk>" |
| 826 | "<l3>1</l3>" |
| 827 | "</lt3>" |
| 828 | "</lt2>" |
| 829 | "<lt2 xmlns=\"urn:tests:d\">" |
| 830 | "<k>val3</k>" |
| 831 | "<cont>" |
| 832 | "<l2>3</l2>" |
| 833 | "</cont>" |
| 834 | "<l4>1</l4>" |
| 835 | "<lt3>" |
| 836 | "<kk>val1</kk>" |
| 837 | "<l3>2</l3>" |
| 838 | "</lt3>" |
| 839 | "</lt2>" |
| 840 | "<lt2 xmlns=\"urn:tests:d\">" |
| 841 | "<k>val4</k>" |
| 842 | "<cont>" |
| 843 | "<l2>4</l2>" |
| 844 | "</cont>" |
| 845 | "<lt3>" |
| 846 | "<kk>val1</kk>" |
| 847 | "<l3>3</l3>" |
| 848 | "</lt3>" |
| 849 | "</lt2>" |
| 850 | "<lt2 xmlns=\"urn:tests:d\">" |
| 851 | "<k>val5</k>" |
| 852 | "<cont>" |
| 853 | "<l2>5</l2>" |
| 854 | "</cont>" |
| 855 | "<lt3>" |
| 856 | "<kk>val1</kk>" |
| 857 | "<l3>3</l3>" |
| 858 | "</lt3>" |
| 859 | "</lt2>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 860 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 861 | assert_null(tree); |
| 862 | logbuf_assert("Unique data leaf(s) \"l3\" not satisfied in \"/d:lt2[k='val2']/lt3[kk='val3']\" and" |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 863 | " \"/d:lt2[k='val2']/lt3[kk='val1']\". /d:lt2[k='val2']/lt3[kk='val1']"); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 864 | |
| 865 | data = |
| 866 | "<lt2 xmlns=\"urn:tests:d\">" |
| 867 | "<k>val1</k>" |
| 868 | "<cont>" |
| 869 | "<l2>1</l2>" |
| 870 | "</cont>" |
| 871 | "<l4>1</l4>" |
| 872 | "</lt2>" |
| 873 | "<lt2 xmlns=\"urn:tests:d\">" |
| 874 | "<k>val2</k>" |
| 875 | "<cont>" |
| 876 | "<l2>2</l2>" |
| 877 | "</cont>" |
| 878 | "<l4>2</l4>" |
| 879 | "</lt2>" |
| 880 | "<lt2 xmlns=\"urn:tests:d\">" |
| 881 | "<k>val3</k>" |
| 882 | "<cont>" |
| 883 | "<l2>3</l2>" |
| 884 | "</cont>" |
| 885 | "<l4>3</l4>" |
| 886 | "</lt2>" |
| 887 | "<lt2 xmlns=\"urn:tests:d\">" |
| 888 | "<k>val4</k>" |
| 889 | "<cont>" |
| 890 | "<l2>2</l2>" |
| 891 | "</cont>" |
| 892 | "<l4>2</l4>" |
| 893 | "</lt2>" |
| 894 | "<lt2 xmlns=\"urn:tests:d\">" |
| 895 | "<k>val5</k>" |
| 896 | "<cont>" |
| 897 | "<l2>5</l2>" |
| 898 | "</cont>" |
| 899 | "<l4>5</l4>" |
| 900 | "</lt2>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 901 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 902 | assert_null(tree); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 903 | logbuf_assert("Unique data leaf(s) \"cont/l2 l4\" not satisfied in \"/d:lt2[k='val4']\" and \"/d:lt2[k='val2']\". /d:lt2[k='val2']"); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 904 | |
| 905 | data = |
| 906 | "<lt2 xmlns=\"urn:tests:d\">" |
| 907 | "<k>val1</k>" |
| 908 | "<cont>" |
| 909 | "<l2>1</l2>" |
| 910 | "</cont>" |
| 911 | "<l4>1</l4>" |
| 912 | "<l5>1</l5>" |
| 913 | "<l6>1</l6>" |
| 914 | "</lt2>" |
| 915 | "<lt2 xmlns=\"urn:tests:d\">" |
| 916 | "<k>val2</k>" |
| 917 | "<cont>" |
| 918 | "<l2>2</l2>" |
| 919 | "</cont>" |
| 920 | "<l4>1</l4>" |
| 921 | "<l5>1</l5>" |
| 922 | "</lt2>" |
| 923 | "<lt2 xmlns=\"urn:tests:d\">" |
| 924 | "<k>val3</k>" |
| 925 | "<cont>" |
| 926 | "<l2>3</l2>" |
| 927 | "</cont>" |
| 928 | "<l4>1</l4>" |
| 929 | "<l5>3</l5>" |
| 930 | "<l6>3</l6>" |
| 931 | "</lt2>" |
| 932 | "<lt2 xmlns=\"urn:tests:d\">" |
| 933 | "<k>val4</k>" |
| 934 | "<cont>" |
| 935 | "<l2>4</l2>" |
| 936 | "</cont>" |
| 937 | "<l4>1</l4>" |
| 938 | "<l6>1</l6>" |
| 939 | "</lt2>" |
| 940 | "<lt2 xmlns=\"urn:tests:d\">" |
| 941 | "<k>val5</k>" |
| 942 | "<cont>" |
| 943 | "<l2>5</l2>" |
| 944 | "</cont>" |
| 945 | "<l4>1</l4>" |
| 946 | "<l5>3</l5>" |
| 947 | "<l6>3</l6>" |
| 948 | "</lt2>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 949 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 950 | assert_null(tree); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 951 | logbuf_assert("Unique data leaf(s) \"l5 l6\" not satisfied in \"/d:lt2[k='val5']\" and \"/d:lt2[k='val3']\". /d:lt2[k='val3']"); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 952 | |
| 953 | *state = NULL; |
| 954 | } |
| 955 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 956 | static void |
| 957 | test_dup(void **state) |
| 958 | { |
| 959 | *state = test_dup; |
| 960 | |
| 961 | const char *data; |
| 962 | struct lyd_node *tree; |
| 963 | |
| 964 | data = "<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 965 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 966 | assert_null(tree); |
| 967 | logbuf_assert("Duplicate instance of \"d\". /e:d"); |
| 968 | |
| 969 | data = "<lt xmlns=\"urn:tests:e\"><k>A</k></lt><lt xmlns=\"urn:tests:e\"><k>B</k></lt><lt xmlns=\"urn:tests:e\"><k>A</k></lt>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 970 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 971 | assert_null(tree); |
| 972 | logbuf_assert("Duplicate instance of \"lt\". /e:lt[k='A']"); |
| 973 | |
| 974 | data = "<ll xmlns=\"urn:tests:e\">A</ll><ll xmlns=\"urn:tests:e\">B</ll><ll xmlns=\"urn:tests:e\">B</ll>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 975 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 976 | assert_null(tree); |
| 977 | logbuf_assert("Duplicate instance of \"ll\". /e:ll[.='B']"); |
| 978 | |
| 979 | data = "<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 980 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 981 | assert_null(tree); |
| 982 | logbuf_assert("Duplicate instance of \"cont\". /e:cont"); |
| 983 | |
| 984 | /* same tests again but using hashes */ |
| 985 | data = "<cont xmlns=\"urn:tests:e\"><d>25</d><d>50</d><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll></cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 986 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 987 | assert_null(tree); |
| 988 | logbuf_assert("Duplicate instance of \"d\". /e:cont/d"); |
| 989 | |
| 990 | data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>" |
| 991 | "<lt><k>a</k></lt><lt><k>b</k></lt><lt><k>c</k></lt><lt><k>d</k></lt><lt><k>c</k></lt></cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 992 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 993 | assert_null(tree); |
| 994 | logbuf_assert("Duplicate instance of \"lt\". /e:cont/lt[k='c']"); |
| 995 | |
| 996 | data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>" |
| 997 | "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 998 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 999 | assert_null(tree); |
| 1000 | logbuf_assert("Duplicate instance of \"ll\". /e:cont/ll[.='d']"); |
| 1001 | |
| 1002 | /* cases */ |
| 1003 | data = "<l xmlns=\"urn:tests:e\">a</l><l xmlns=\"urn:tests:e\">b</l><l xmlns=\"urn:tests:e\">c</l><l xmlns=\"urn:tests:e\">b</l>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1004 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1005 | assert_null(tree); |
| 1006 | logbuf_assert("Duplicate instance of \"l\". /e:l[.='b']"); |
| 1007 | |
| 1008 | data = "<l xmlns=\"urn:tests:e\">a</l><l xmlns=\"urn:tests:e\">b</l><l xmlns=\"urn:tests:e\">c</l><a xmlns=\"urn:tests:e\">aa</a>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1009 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1010 | assert_null(tree); |
| 1011 | logbuf_assert("Data for both cases \"a\" and \"b\" exist. /e:choic"); |
| 1012 | |
| 1013 | *state = NULL; |
| 1014 | } |
| 1015 | |
| 1016 | static void |
| 1017 | test_defaults(void **state) |
| 1018 | { |
| 1019 | *state = test_defaults; |
| 1020 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1021 | char *str; |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1022 | struct lyd_node *tree, *node, *diff; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1023 | const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "f"); |
| 1024 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 1025 | struct ly_out *out; |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 1026 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 1027 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1028 | /* get defaults */ |
| 1029 | tree = NULL; |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1030 | assert_int_equal(lyd_validate_module(&tree, mod, 0, &diff), LY_SUCCESS); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1031 | assert_non_null(tree); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1032 | assert_non_null(diff); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1033 | |
| 1034 | /* check all defaults exist */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1035 | lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1036 | assert_string_equal(str, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1037 | "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>" |
| 1038 | "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>" |
| 1039 | "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>" |
| 1040 | "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>" |
| 1041 | "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>" |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1042 | "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>" |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1043 | "<cont xmlns=\"urn:tests:f\">" |
| 1044 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>" |
| 1045 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>" |
| 1046 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>" |
| 1047 | "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>" |
| 1048 | "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>" |
| 1049 | "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>" |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1050 | "</cont>"); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 1051 | ly_out_reset(out); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1052 | |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1053 | /* check diff */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1054 | lyd_print_all(out, diff, LYD_XML, LYD_PRINT_WD_ALL); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1055 | assert_string_equal(str, |
| 1056 | "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def1</ll1>" |
| 1057 | "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def2</ll1>" |
| 1058 | "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">def3</ll1>" |
| 1059 | "<d xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">15</d>" |
| 1060 | "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">dflt1</ll2>" |
| 1061 | "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">dflt2</ll2>" |
| 1062 | "<cont xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
| 1063 | "<ll1 yang:operation=\"create\">def1</ll1>" |
| 1064 | "<ll1 yang:operation=\"create\">def2</ll1>" |
| 1065 | "<ll1 yang:operation=\"create\">def3</ll1>" |
| 1066 | "<d yang:operation=\"create\">15</d>" |
| 1067 | "<ll2 yang:operation=\"create\">dflt1</ll2>" |
| 1068 | "<ll2 yang:operation=\"create\">dflt2</ll2>" |
| 1069 | "</cont>" |
| 1070 | ); |
| 1071 | ly_out_reset(out); |
| 1072 | lyd_free_siblings(diff); |
| 1073 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1074 | /* create another explicit case and validate */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1075 | assert_int_equal(lyd_new_term(NULL, mod, "l", "value", &node), LY_SUCCESS); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1076 | assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1077 | assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1078 | |
| 1079 | /* check data tree */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1080 | lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1081 | assert_string_equal(str, |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1082 | "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>" |
| 1083 | "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>" |
| 1084 | "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>" |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1085 | "<cont xmlns=\"urn:tests:f\">" |
| 1086 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>" |
| 1087 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>" |
| 1088 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>" |
| 1089 | "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>" |
| 1090 | "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>" |
| 1091 | "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>" |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1092 | "</cont>" |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1093 | "<l xmlns=\"urn:tests:f\">value</l>"); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 1094 | ly_out_reset(out); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1095 | |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1096 | /* check diff */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1097 | lyd_print_all(out, diff, LYD_XML, LYD_PRINT_WD_ALL); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1098 | assert_string_equal(str, |
| 1099 | "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def1</ll1>" |
| 1100 | "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def2</ll1>" |
| 1101 | "<ll1 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">def3</ll1>" |
| 1102 | ); |
| 1103 | ly_out_reset(out); |
| 1104 | lyd_free_siblings(diff); |
| 1105 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1106 | /* create explicit leaf-list and leaf and validate */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1107 | assert_int_equal(lyd_new_term(NULL, mod, "d", "15", &node), LY_SUCCESS); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1108 | assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1109 | assert_int_equal(lyd_new_term(NULL, mod, "ll2", "dflt2", &node), LY_SUCCESS); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1110 | assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1111 | assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1112 | |
| 1113 | /* check data tree */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1114 | lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1115 | assert_string_equal(str, |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1116 | "<cont xmlns=\"urn:tests:f\">" |
| 1117 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>" |
| 1118 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>" |
| 1119 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>" |
| 1120 | "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>" |
| 1121 | "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>" |
| 1122 | "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>" |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1123 | "</cont>" |
| 1124 | "<l xmlns=\"urn:tests:f\">value</l>" |
| 1125 | "<d xmlns=\"urn:tests:f\">15</d>" |
| 1126 | "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 1127 | ly_out_reset(out); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1128 | |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1129 | /* check diff */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1130 | lyd_print_all(out, diff, LYD_XML, LYD_PRINT_WD_ALL); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1131 | assert_string_equal(str, |
| 1132 | "<d xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">15</d>" |
| 1133 | "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">dflt1</ll2>" |
| 1134 | "<ll2 xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">dflt2</ll2>" |
| 1135 | ); |
| 1136 | ly_out_reset(out); |
| 1137 | lyd_free_siblings(diff); |
| 1138 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1139 | /* create first explicit container, which should become implicit */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1140 | assert_int_equal(lyd_new_inner(NULL, mod, "cont", &node), LY_SUCCESS); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1141 | assert_int_equal(lyd_insert_before(tree, node), LY_SUCCESS); |
| 1142 | tree = tree->prev; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1143 | assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1144 | |
| 1145 | /* check data tree */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1146 | lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1147 | assert_string_equal(str, |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1148 | "<cont xmlns=\"urn:tests:f\">" |
| 1149 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>" |
| 1150 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>" |
| 1151 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>" |
| 1152 | "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>" |
| 1153 | "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>" |
| 1154 | "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>" |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1155 | "</cont>" |
| 1156 | "<l xmlns=\"urn:tests:f\">value</l>" |
| 1157 | "<d xmlns=\"urn:tests:f\">15</d>" |
| 1158 | "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 1159 | ly_out_reset(out); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1160 | |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1161 | /* check diff */ |
| 1162 | assert_null(diff); |
| 1163 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1164 | /* create second explicit container, which should become implicit, so the first tree node should be removed */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1165 | assert_int_equal(lyd_new_inner(NULL, mod, "cont", &node), LY_SUCCESS); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1166 | assert_int_equal(lyd_insert_after(tree, node), LY_SUCCESS); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1167 | assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1168 | |
| 1169 | /* check data tree */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1170 | lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1171 | assert_string_equal(str, |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1172 | "<cont xmlns=\"urn:tests:f\">" |
| 1173 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>" |
| 1174 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>" |
| 1175 | "<ll1 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>" |
| 1176 | "<d xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>" |
| 1177 | "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>" |
| 1178 | "<ll2 xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>" |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1179 | "</cont>" |
| 1180 | "<l xmlns=\"urn:tests:f\">value</l>" |
| 1181 | "<d xmlns=\"urn:tests:f\">15</d>" |
| 1182 | "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 1183 | ly_out_reset(out); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1184 | |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1185 | /* check diff */ |
| 1186 | assert_null(diff); |
| 1187 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1188 | /* similar changes for nested defaults */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1189 | assert_int_equal(lyd_new_term(tree, NULL, "ll1", "def3", NULL), LY_SUCCESS); |
| 1190 | assert_int_equal(lyd_new_term(tree, NULL, "d", "5", NULL), LY_SUCCESS); |
| 1191 | assert_int_equal(lyd_new_term(tree, NULL, "ll2", "non-dflt", NULL), LY_SUCCESS); |
| 1192 | assert_int_equal(lyd_validate_all(&tree, ctx, LYD_VALIDATE_PRESENT, &diff), LY_SUCCESS); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1193 | |
| 1194 | /* check data tree */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1195 | lyd_print_all(out, tree, LYD_XML, LYD_PRINT_WD_IMPL_TAG); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1196 | assert_string_equal(str, |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 1197 | "<cont xmlns=\"urn:tests:f\">" |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1198 | "<ll1>def3</ll1>" |
| 1199 | "<d>5</d>" |
| 1200 | "<ll2>non-dflt</ll2>" |
| 1201 | "</cont>" |
| 1202 | "<l xmlns=\"urn:tests:f\">value</l>" |
| 1203 | "<d xmlns=\"urn:tests:f\">15</d>" |
| 1204 | "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 1205 | ly_out_reset(out); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1206 | |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1207 | /* check diff */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1208 | lyd_print_all(out, diff, LYD_XML, LYD_PRINT_WD_ALL); |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1209 | assert_string_equal(str, |
| 1210 | "<cont xmlns=\"urn:tests:f\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
| 1211 | "<ll1 yang:operation=\"delete\">def1</ll1>" |
| 1212 | "<ll1 yang:operation=\"delete\">def2</ll1>" |
| 1213 | "<ll1 yang:operation=\"delete\">def3</ll1>" |
| 1214 | "<d yang:operation=\"delete\">15</d>" |
| 1215 | "<ll2 yang:operation=\"delete\">dflt1</ll2>" |
| 1216 | "<ll2 yang:operation=\"delete\">dflt2</ll2>" |
| 1217 | "</cont>" |
| 1218 | ); |
| 1219 | ly_out_reset(out); |
| 1220 | lyd_free_siblings(diff); |
| 1221 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1222 | lyd_free_siblings(tree); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 1223 | ly_out_free(out, NULL, 1); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1224 | |
| 1225 | *state = NULL; |
| 1226 | } |
| 1227 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1228 | static void |
| 1229 | test_iffeature(void **state) |
| 1230 | { |
| 1231 | *state = test_iffeature; |
| 1232 | |
| 1233 | const char *data; |
| 1234 | struct lyd_node *tree; |
| 1235 | const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "g"); |
| 1236 | |
| 1237 | /* get empty data */ |
| 1238 | tree = NULL; |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1239 | assert_int_equal(lyd_validate_module(&tree, mod, 0, NULL), LY_SUCCESS); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1240 | assert_null(tree); |
| 1241 | |
| 1242 | /* disabled by f1 */ |
| 1243 | data = |
| 1244 | "<cont xmlns=\"urn:tests:g\">" |
| 1245 | "<d>51</d>" |
| 1246 | "</cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1247 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1248 | assert_null(tree); |
| 1249 | logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont"); |
| 1250 | |
| 1251 | /* enable f1 */ |
| 1252 | assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS); |
| 1253 | |
| 1254 | /* get data with default container */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1255 | assert_int_equal(lyd_validate_module(&tree, mod, 0, NULL), LY_SUCCESS); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1256 | assert_non_null(tree); |
| 1257 | lyd_free_siblings(tree); |
| 1258 | |
| 1259 | /* disabled by f2 */ |
| 1260 | data = |
| 1261 | "<cont xmlns=\"urn:tests:g\">" |
| 1262 | "<cont2>" |
| 1263 | "<e>val</e>" |
| 1264 | "</cont2>" |
| 1265 | "</cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1266 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1267 | assert_null(tree); |
| 1268 | logbuf_assert("Data are disabled by \"cont2\" schema node if-feature. /g:cont/cont2"); |
| 1269 | |
| 1270 | data = |
| 1271 | "<cont xmlns=\"urn:tests:g\">" |
| 1272 | "<a>val</a>" |
| 1273 | "</cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1274 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1275 | assert_null(tree); |
| 1276 | logbuf_assert("Data are disabled by \"choic\" schema node if-feature. /g:cont/a"); |
| 1277 | |
| 1278 | /* enable f3 */ |
| 1279 | assert_int_equal(lys_feature_enable(mod, "f3"), LY_SUCCESS); |
| 1280 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1281 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1282 | assert_non_null(tree); |
| 1283 | lyd_free_siblings(tree); |
| 1284 | |
| 1285 | /* disabled by f2 */ |
| 1286 | data = |
| 1287 | "<cont xmlns=\"urn:tests:g\">" |
| 1288 | "<l>val</l>" |
| 1289 | "</cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1290 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1291 | assert_null(tree); |
| 1292 | logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l"); |
| 1293 | |
| 1294 | /* enable f2 */ |
| 1295 | assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS); |
| 1296 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1297 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1298 | assert_non_null(tree); |
| 1299 | lyd_free_siblings(tree); |
| 1300 | |
| 1301 | /* try separate validation */ |
| 1302 | assert_int_equal(lys_feature_disable(mod, "f1"), LY_SUCCESS); |
| 1303 | assert_int_equal(lys_feature_disable(mod, "f2"), LY_SUCCESS); |
| 1304 | assert_int_equal(lys_feature_disable(mod, "f3"), LY_SUCCESS); |
| 1305 | |
| 1306 | data = |
| 1307 | "<cont xmlns=\"urn:tests:g\">" |
| 1308 | "<l>val</l>" |
| 1309 | "<d>51</d>" |
| 1310 | "<cont2>" |
| 1311 | "<e>val</e>" |
| 1312 | "</cont2>" |
| 1313 | "</cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1314 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY, 0, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1315 | assert_non_null(tree); |
| 1316 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1317 | assert_int_equal(LY_EVALID, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT, NULL)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1318 | logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont"); |
| 1319 | |
| 1320 | assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS); |
| 1321 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1322 | assert_int_equal(LY_EVALID, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT, NULL)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1323 | logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l"); |
| 1324 | |
| 1325 | assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS); |
| 1326 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1327 | assert_int_equal(LY_SUCCESS, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT, NULL)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1328 | |
| 1329 | lyd_free_siblings(tree); |
| 1330 | |
| 1331 | *state = NULL; |
| 1332 | } |
| 1333 | |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1334 | static void |
| 1335 | test_state(void **state) |
| 1336 | { |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1337 | *state = test_state; |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1338 | |
| 1339 | const char *data; |
| 1340 | struct lyd_node *tree; |
| 1341 | |
| 1342 | data = |
| 1343 | "<cont xmlns=\"urn:tests:h\">" |
| 1344 | "<cont2>" |
| 1345 | "<l>val</l>" |
| 1346 | "</cont2>" |
| 1347 | "</cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1348 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_NO_STATE, 0, &tree)); |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1349 | assert_null(tree); |
| 1350 | logbuf_assert("Invalid state data node \"cont2\" found. Line number 1."); |
| 1351 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1352 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT | LYD_VALIDATE_NO_STATE, &tree)); |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1353 | assert_null(tree); |
| 1354 | logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2"); |
| 1355 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1356 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY, 0, &tree)); |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1357 | assert_non_null(tree); |
| 1358 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame^] | 1359 | assert_int_equal(LY_EVALID, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT | LYD_VALIDATE_NO_STATE, NULL)); |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1360 | logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2"); |
| 1361 | |
| 1362 | lyd_free_siblings(tree); |
| 1363 | |
| 1364 | *state = NULL; |
| 1365 | } |
| 1366 | |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1367 | static void |
| 1368 | test_must(void **state) |
| 1369 | { |
| 1370 | *state = test_must; |
| 1371 | |
| 1372 | const char *data; |
| 1373 | struct lyd_node *tree; |
| 1374 | |
| 1375 | data = |
| 1376 | "<cont xmlns=\"urn:tests:i\">" |
| 1377 | "<l>wrong</l>" |
| 1378 | "<l2>val</l2>" |
| 1379 | "</cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1380 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1381 | assert_null(tree); |
| 1382 | logbuf_assert("Must condition \"../l = 'right'\" not satisfied. /i:cont/l2"); |
| 1383 | |
| 1384 | data = |
| 1385 | "<cont xmlns=\"urn:tests:i\">" |
| 1386 | "<l>right</l>" |
| 1387 | "<l2>val</l2>" |
| 1388 | "</cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1389 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1390 | assert_non_null(tree); |
| 1391 | lyd_free_tree(tree); |
| 1392 | |
| 1393 | *state = NULL; |
| 1394 | } |
| 1395 | |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1396 | static void |
| 1397 | test_action(void **state) |
| 1398 | { |
| 1399 | *state = test_action; |
| 1400 | |
| 1401 | const char *data; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1402 | struct ly_in *in; |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1403 | struct lyd_node *tree, *op_tree; |
| 1404 | const struct lys_module *mod; |
| 1405 | |
| 1406 | data = |
| 1407 | "<cont xmlns=\"urn:tests:j\">" |
| 1408 | "<l1>" |
| 1409 | "<k>val1</k>" |
| 1410 | "<act>" |
| 1411 | "<lf2>target</lf2>" |
| 1412 | "</act>" |
| 1413 | "</l1>" |
| 1414 | "</cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1415 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 1416 | assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_XML, &op_tree, NULL)); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1417 | assert_non_null(op_tree); |
| 1418 | |
| 1419 | /* missing leafref */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1420 | assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALIDATE_OP_RPC, NULL)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1421 | logbuf_assert("Invalid leafref value \"target\" - no target instance \"/lf3\" with the same value." |
| 1422 | " /j:cont/l1[k='val1']/act/lf2"); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1423 | ly_in_free(in, 0); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1424 | |
| 1425 | data = |
| 1426 | "<cont xmlns=\"urn:tests:j\">" |
| 1427 | "<lf1>not true</lf1>" |
| 1428 | "</cont>" |
| 1429 | "<lf3 xmlns=\"urn:tests:j\">target</lf3>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1430 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_TRUSTED, 0, &tree)); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1431 | assert_non_null(tree); |
| 1432 | |
| 1433 | /* disabled if-feature */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1434 | assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_RPC, NULL)); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1435 | logbuf_assert("Data are disabled by \"act\" schema node if-feature. /j:cont/l1[k='val1']/act"); |
| 1436 | |
| 1437 | mod = ly_ctx_get_module_latest(ctx, "j"); |
| 1438 | assert_non_null(mod); |
| 1439 | assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1")); |
| 1440 | |
| 1441 | /* input must false */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1442 | assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_RPC, NULL)); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1443 | logbuf_assert("Must condition \"../../lf1 = 'true'\" not satisfied. /j:cont/l1[k='val1']/act"); |
| 1444 | |
| 1445 | lyd_free_siblings(tree); |
| 1446 | data = |
| 1447 | "<cont xmlns=\"urn:tests:j\">" |
| 1448 | "<lf1>true</lf1>" |
| 1449 | "</cont>" |
| 1450 | "<lf3 xmlns=\"urn:tests:j\">target</lf3>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1451 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_TRUSTED, 0, &tree)); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1452 | assert_non_null(tree); |
| 1453 | |
| 1454 | /* success */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1455 | assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_RPC, NULL)); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1456 | |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1457 | lys_feature_disable(mod, "feat1"); |
| 1458 | lyd_free_tree(op_tree); |
| 1459 | lyd_free_siblings(tree); |
| 1460 | |
| 1461 | *state = NULL; |
| 1462 | } |
| 1463 | |
| 1464 | static void |
| 1465 | test_reply(void **state) |
| 1466 | { |
| 1467 | *state = test_reply; |
| 1468 | |
| 1469 | const char *data; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1470 | struct ly_in *in; |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1471 | struct lyd_node *tree, *op_tree, *request; |
| 1472 | const struct lys_module *mod; |
| 1473 | |
| 1474 | data = |
| 1475 | "<cont xmlns=\"urn:tests:j\">" |
| 1476 | "<l1>" |
| 1477 | "<k>val1</k>" |
| 1478 | "<act>" |
| 1479 | "<lf2>target</lf2>" |
| 1480 | "</act>" |
| 1481 | "</l1>" |
| 1482 | "</cont>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1483 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 1484 | assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_XML, &request, NULL)); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1485 | assert_non_null(request); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1486 | ly_in_free(in, 0); |
| 1487 | |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1488 | data = "<lf2 xmlns=\"urn:tests:j\">target</lf2>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1489 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 1490 | assert_int_equal(LY_SUCCESS, lyd_parse_reply(request, in, LYD_XML, &op_tree, NULL)); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1491 | lyd_free_all(request); |
| 1492 | assert_non_null(op_tree); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1493 | ly_in_free(in, 0); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1494 | |
| 1495 | /* missing leafref */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1496 | assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, NULL, LYD_VALIDATE_OP_REPLY, NULL)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1497 | logbuf_assert("Invalid leafref value \"target\" - no target instance \"/lf4\" with the same value." |
| 1498 | " /j:cont/l1[k='val1']/act/lf2"); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1499 | |
| 1500 | data = |
| 1501 | "<cont xmlns=\"urn:tests:j\">" |
| 1502 | "<lf1>not true</lf1>" |
| 1503 | "</cont>" |
| 1504 | "<lf4 xmlns=\"urn:tests:j\">target</lf4>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1505 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_TRUSTED, 0, &tree)); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1506 | assert_non_null(tree); |
| 1507 | |
| 1508 | /* disabled if-feature */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1509 | assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_REPLY, NULL)); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1510 | logbuf_assert("Data are disabled by \"act\" schema node if-feature. /j:cont/l1[k='val1']/act"); |
| 1511 | |
| 1512 | mod = ly_ctx_get_module_latest(ctx, "j"); |
| 1513 | assert_non_null(mod); |
| 1514 | assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "feat1")); |
| 1515 | |
| 1516 | /* input must false */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1517 | assert_int_equal(LY_EVALID, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_REPLY, NULL)); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1518 | logbuf_assert("Must condition \"../../lf1 = 'true2'\" not satisfied. /j:cont/l1[k='val1']/act"); |
| 1519 | |
| 1520 | lyd_free_siblings(tree); |
| 1521 | data = |
| 1522 | "<cont xmlns=\"urn:tests:j\">" |
| 1523 | "<lf1>true2</lf1>" |
| 1524 | "</cont>" |
| 1525 | "<lf4 xmlns=\"urn:tests:j\">target</lf4>"; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1526 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_TRUSTED, 0, &tree)); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1527 | assert_non_null(tree); |
| 1528 | |
| 1529 | /* success */ |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 1530 | assert_int_equal(LY_SUCCESS, lyd_validate_op(op_tree, tree, LYD_VALIDATE_OP_REPLY, NULL)); |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1531 | |
| 1532 | lys_feature_disable(mod, "feat1"); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1533 | lyd_free_tree(op_tree); |
| 1534 | lyd_free_siblings(tree); |
| 1535 | |
| 1536 | *state = NULL; |
| 1537 | } |
| 1538 | |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1539 | int main(void) |
| 1540 | { |
| 1541 | const struct CMUnitTest tests[] = { |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 1542 | cmocka_unit_test_teardown(test_when, teardown_s), |
| 1543 | cmocka_unit_test_teardown(test_mandatory, teardown_s), |
| 1544 | cmocka_unit_test_teardown(test_minmax, teardown_s), |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1545 | cmocka_unit_test_teardown(test_unique, teardown_s), |
| 1546 | cmocka_unit_test_teardown(test_unique_nested, teardown_s), |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1547 | cmocka_unit_test_teardown(test_dup, teardown_s), |
| 1548 | cmocka_unit_test_teardown(test_defaults, teardown_s), |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1549 | cmocka_unit_test_teardown(test_iffeature, teardown_s), |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1550 | cmocka_unit_test_teardown(test_state, teardown_s), |
Michal Vasko | cc048b2 | 2020-03-27 15:52:38 +0100 | [diff] [blame] | 1551 | cmocka_unit_test_teardown(test_must, teardown_s), |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 1552 | cmocka_unit_test_teardown(test_action, teardown_s), |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 1553 | cmocka_unit_test_teardown(test_reply, teardown_s), |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1554 | }; |
| 1555 | |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 1556 | return cmocka_run_group_tests(tests, setup, teardown); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1557 | } |