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 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 23 | #include "tests/config.h" |
| 24 | |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 25 | #include "../../src/context.h" |
| 26 | #include "../../src/tree_data_internal.h" |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 27 | #include "../../src/printer_data.h" |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 28 | |
| 29 | #define BUFSIZE 1024 |
| 30 | char logbuf[BUFSIZE] = {0}; |
| 31 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 32 | |
| 33 | struct ly_ctx *ctx; /* context for tests */ |
| 34 | |
| 35 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 36 | #define ENABLE_LOGGER_CHECKING 1 |
| 37 | |
| 38 | #if ENABLE_LOGGER_CHECKING |
| 39 | static void |
| 40 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 41 | { |
| 42 | (void) level; /* unused */ |
| 43 | if (store) { |
| 44 | if (path && path[0]) { |
| 45 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 46 | } else { |
| 47 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 48 | } |
| 49 | if (store > 0) { |
| 50 | --store; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | static int |
| 57 | setup(void **state) |
| 58 | { |
| 59 | (void) state; /* unused */ |
| 60 | |
| 61 | const char *schema_a = |
| 62 | "module a {" |
| 63 | "namespace urn:tests:a;" |
| 64 | "prefix a;" |
| 65 | "yang-version 1.1;" |
| 66 | |
| 67 | "container cont {" |
| 68 | "leaf a {" |
| 69 | "when \"../../c = 'val_c'\";" |
| 70 | "type string;" |
| 71 | "}" |
| 72 | "leaf b {" |
| 73 | "type string;" |
| 74 | "}" |
| 75 | "}" |
| 76 | "leaf c {" |
| 77 | "when \"/cont/b = 'val_b'\";" |
| 78 | "type string;" |
| 79 | "}" |
| 80 | "}"; |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 81 | const char *schema_b = |
| 82 | "module b {" |
| 83 | "namespace urn:tests:b;" |
| 84 | "prefix b;" |
| 85 | "yang-version 1.1;" |
| 86 | |
| 87 | "choice choic {" |
| 88 | "mandatory true;" |
| 89 | "leaf a {" |
| 90 | "type string;" |
| 91 | "}" |
| 92 | "case b {" |
| 93 | "leaf l {" |
| 94 | "type string;" |
| 95 | "}" |
| 96 | "}" |
| 97 | "}" |
| 98 | "leaf c {" |
| 99 | "mandatory true;" |
| 100 | "type string;" |
| 101 | "}" |
| 102 | "leaf d {" |
| 103 | "type empty;" |
| 104 | "}" |
| 105 | "}"; |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 106 | const char *schema_c = |
| 107 | "module c {" |
| 108 | "namespace urn:tests:c;" |
| 109 | "prefix c;" |
| 110 | "yang-version 1.1;" |
| 111 | |
| 112 | "choice choic {" |
| 113 | "leaf a {" |
| 114 | "type string;" |
| 115 | "}" |
| 116 | "case b {" |
| 117 | "leaf-list l {" |
| 118 | "min-elements 3;" |
| 119 | "type string;" |
| 120 | "}" |
| 121 | "}" |
| 122 | "}" |
| 123 | "list lt {" |
| 124 | "max-elements 4;" |
| 125 | "key \"k\";" |
| 126 | "leaf k {" |
| 127 | "type string;" |
| 128 | "}" |
| 129 | "}" |
| 130 | "leaf d {" |
| 131 | "type empty;" |
| 132 | "}" |
| 133 | "}"; |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 134 | const char *schema_d = |
| 135 | "module d {" |
| 136 | "namespace urn:tests:d;" |
| 137 | "prefix d;" |
| 138 | "yang-version 1.1;" |
| 139 | |
| 140 | "list lt {" |
| 141 | "key \"k\";" |
| 142 | "unique \"l1\";" |
| 143 | "leaf k {" |
| 144 | "type string;" |
| 145 | "}" |
| 146 | "leaf l1 {" |
| 147 | "type string;" |
| 148 | "}" |
| 149 | "}" |
| 150 | "list lt2 {" |
| 151 | "key \"k\";" |
| 152 | "unique \"cont/l2 l4\";" |
| 153 | "unique \"l5 l6\";" |
| 154 | "leaf k {" |
| 155 | "type string;" |
| 156 | "}" |
| 157 | "container cont {" |
| 158 | "leaf l2 {" |
| 159 | "type string;" |
| 160 | "}" |
| 161 | "}" |
| 162 | "leaf l4 {" |
| 163 | "type string;" |
| 164 | "}" |
| 165 | "leaf l5 {" |
| 166 | "type string;" |
| 167 | "}" |
| 168 | "leaf l6 {" |
| 169 | "type string;" |
| 170 | "}" |
| 171 | "list lt3 {" |
| 172 | "key \"kk\";" |
| 173 | "unique \"l3\";" |
| 174 | "leaf kk {" |
| 175 | "type string;" |
| 176 | "}" |
| 177 | "leaf l3 {" |
| 178 | "type string;" |
| 179 | "}" |
| 180 | "}" |
| 181 | "}" |
| 182 | "}"; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 183 | const char *schema_e = |
| 184 | "module e {" |
| 185 | "namespace urn:tests:e;" |
| 186 | "prefix e;" |
| 187 | "yang-version 1.1;" |
| 188 | |
| 189 | "choice choic {" |
| 190 | "leaf a {" |
| 191 | "type string;" |
| 192 | "}" |
| 193 | "case b {" |
| 194 | "leaf-list l {" |
| 195 | "type string;" |
| 196 | "}" |
| 197 | "}" |
| 198 | "}" |
| 199 | "list lt {" |
| 200 | "key \"k\";" |
| 201 | "leaf k {" |
| 202 | "type string;" |
| 203 | "}" |
| 204 | "}" |
| 205 | "leaf d {" |
| 206 | "type uint32;" |
| 207 | "}" |
| 208 | "leaf-list ll {" |
| 209 | "type string;" |
| 210 | "}" |
| 211 | "container cont {" |
| 212 | "list lt {" |
| 213 | "key \"k\";" |
| 214 | "leaf k {" |
| 215 | "type string;" |
| 216 | "}" |
| 217 | "}" |
| 218 | "leaf d {" |
| 219 | "type uint32;" |
| 220 | "}" |
| 221 | "leaf-list ll {" |
| 222 | "type string;" |
| 223 | "}" |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 224 | "leaf-list ll2 {" |
| 225 | "type enumeration {" |
| 226 | "enum one;" |
| 227 | "enum two;" |
| 228 | "}" |
| 229 | "}" |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 230 | "}" |
| 231 | "}"; |
| 232 | const char *schema_f = |
| 233 | "module f {" |
| 234 | "namespace urn:tests:f;" |
| 235 | "prefix f;" |
| 236 | "yang-version 1.1;" |
| 237 | |
| 238 | "choice choic {" |
| 239 | "default \"c\";" |
| 240 | "leaf a {" |
| 241 | "type string;" |
| 242 | "}" |
| 243 | "case b {" |
| 244 | "leaf l {" |
| 245 | "type string;" |
| 246 | "}" |
| 247 | "}" |
| 248 | "case c {" |
| 249 | "leaf-list ll1 {" |
| 250 | "type string;" |
| 251 | "default \"def1\";" |
| 252 | "default \"def2\";" |
| 253 | "default \"def3\";" |
| 254 | "}" |
| 255 | "}" |
| 256 | "}" |
| 257 | "leaf d {" |
| 258 | "type uint32;" |
| 259 | "default 15;" |
| 260 | "}" |
| 261 | "leaf-list ll2 {" |
| 262 | "type string;" |
| 263 | "default \"dflt1\";" |
| 264 | "default \"dflt2\";" |
| 265 | "}" |
| 266 | "container cont {" |
| 267 | "choice choic {" |
| 268 | "default \"c\";" |
| 269 | "leaf a {" |
| 270 | "type string;" |
| 271 | "}" |
| 272 | "case b {" |
| 273 | "leaf l {" |
| 274 | "type string;" |
| 275 | "}" |
| 276 | "}" |
| 277 | "case c {" |
| 278 | "leaf-list ll1 {" |
| 279 | "type string;" |
| 280 | "default \"def1\";" |
| 281 | "default \"def2\";" |
| 282 | "default \"def3\";" |
| 283 | "}" |
| 284 | "}" |
| 285 | "}" |
| 286 | "leaf d {" |
| 287 | "type uint32;" |
| 288 | "default 15;" |
| 289 | "}" |
| 290 | "leaf-list ll2 {" |
| 291 | "type string;" |
| 292 | "default \"dflt1\";" |
| 293 | "default \"dflt2\";" |
| 294 | "}" |
| 295 | "}" |
| 296 | "}"; |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 297 | const char *schema_g = |
| 298 | "module g {" |
| 299 | "namespace urn:tests:g;" |
| 300 | "prefix g;" |
| 301 | "yang-version 1.1;" |
| 302 | |
| 303 | "feature f1;" |
| 304 | "feature f2;" |
| 305 | "feature f3;" |
| 306 | |
| 307 | "container cont {" |
| 308 | "if-feature \"f1\";" |
| 309 | "choice choic {" |
| 310 | "if-feature \"f2 or f3\";" |
| 311 | "leaf a {" |
| 312 | "type string;" |
| 313 | "}" |
| 314 | "case b {" |
| 315 | "if-feature \"f2 and f1\";" |
| 316 | "leaf l {" |
| 317 | "type string;" |
| 318 | "}" |
| 319 | "}" |
| 320 | "}" |
| 321 | "leaf d {" |
| 322 | "type uint32;" |
| 323 | "}" |
| 324 | "container cont2 {" |
| 325 | "if-feature \"f2\";" |
| 326 | "leaf e {" |
| 327 | "type string;" |
| 328 | "}" |
| 329 | "}" |
| 330 | "}" |
| 331 | "}"; |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 332 | const char *schema_h = |
| 333 | "module h {" |
| 334 | "namespace urn:tests:h;" |
| 335 | "prefix h;" |
| 336 | "yang-version 1.1;" |
| 337 | |
| 338 | "container cont {" |
| 339 | "container cont2 {" |
| 340 | "config false;" |
| 341 | "leaf l {" |
| 342 | "type string;" |
| 343 | "}" |
| 344 | "}" |
| 345 | "}" |
| 346 | "}"; |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 347 | |
| 348 | #if ENABLE_LOGGER_CHECKING |
| 349 | ly_set_log_clb(logger, 1); |
| 350 | #endif |
| 351 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 352 | assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx)); |
| 353 | assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01")); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 354 | assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 355 | assert_non_null(lys_parse_mem(ctx, schema_b, LYS_IN_YANG)); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 356 | assert_non_null(lys_parse_mem(ctx, schema_c, LYS_IN_YANG)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 357 | assert_non_null(lys_parse_mem(ctx, schema_d, LYS_IN_YANG)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 358 | assert_non_null(lys_parse_mem(ctx, schema_e, LYS_IN_YANG)); |
| 359 | assert_non_null(lys_parse_mem(ctx, schema_f, LYS_IN_YANG)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 360 | assert_non_null(lys_parse_mem(ctx, schema_g, LYS_IN_YANG)); |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 361 | assert_non_null(lys_parse_mem(ctx, schema_h, LYS_IN_YANG)); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int |
| 367 | teardown(void **state) |
| 368 | { |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 369 | (void)state; |
| 370 | ly_ctx_destroy(ctx, NULL); |
| 371 | ctx = NULL; |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static int |
| 377 | teardown_s(void **state) |
| 378 | { |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 379 | #if ENABLE_LOGGER_CHECKING |
| 380 | if (*state) { |
| 381 | fprintf(stderr, "%s\n", logbuf); |
| 382 | } |
| 383 | #else |
| 384 | (void) state; /* unused */ |
| 385 | #endif |
| 386 | |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | void |
| 391 | logbuf_clean(void) |
| 392 | { |
| 393 | logbuf[0] = '\0'; |
| 394 | } |
| 395 | |
| 396 | #if ENABLE_LOGGER_CHECKING |
| 397 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 398 | #else |
| 399 | # define logbuf_assert(str) |
| 400 | #endif |
| 401 | |
| 402 | static void |
| 403 | test_when(void **state) |
| 404 | { |
| 405 | *state = test_when; |
| 406 | |
| 407 | const char *data; |
| 408 | struct lyd_node *tree; |
| 409 | |
| 410 | data = "<c xmlns=\"urn:tests:a\">hey</c>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 411 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 412 | assert_null(tree); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 413 | logbuf_assert("When condition \"/cont/b = 'val_b'\" not satisfied. /a:c"); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 414 | |
| 415 | data = "<cont xmlns=\"urn:tests:a\"><b>val_b</b></cont><c xmlns=\"urn:tests:a\">hey</c>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 416 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 417 | assert_non_null(tree); |
| 418 | assert_string_equal("c", tree->next->schema->name); |
| 419 | assert_int_equal(LYD_WHEN_TRUE, tree->next->flags); |
| 420 | lyd_free_all(tree); |
| 421 | |
| 422 | data = "<cont xmlns=\"urn:tests:a\"><a>val</a><b>val_b</b></cont><c xmlns=\"urn:tests:a\">val_c</c>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 423 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 424 | assert_non_null(tree); |
| 425 | assert_string_equal("a", lyd_node_children(tree)->schema->name); |
| 426 | assert_int_equal(LYD_WHEN_TRUE, lyd_node_children(tree)->flags); |
| 427 | assert_string_equal("c", tree->next->schema->name); |
| 428 | assert_int_equal(LYD_WHEN_TRUE, tree->next->flags); |
| 429 | lyd_free_all(tree); |
| 430 | |
| 431 | *state = NULL; |
| 432 | } |
| 433 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 434 | static void |
| 435 | test_mandatory(void **state) |
| 436 | { |
| 437 | *state = test_mandatory; |
| 438 | |
| 439 | const char *data; |
| 440 | struct lyd_node *tree; |
| 441 | |
| 442 | data = "<d xmlns=\"urn:tests:b\"/>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 443 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 444 | assert_null(tree); |
| 445 | logbuf_assert("Mandatory node \"choic\" instance does not exist. /b:choic"); |
| 446 | |
| 447 | data = "<l xmlns=\"urn:tests:b\">string</l><d xmlns=\"urn:tests:b\"/>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 448 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 449 | assert_null(tree); |
| 450 | logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c"); |
| 451 | |
| 452 | data = "<a xmlns=\"urn:tests:b\">string</a>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 453 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 454 | assert_null(tree); |
| 455 | logbuf_assert("Mandatory node \"c\" instance does not exist. /b:c"); |
| 456 | |
| 457 | data = "<a xmlns=\"urn:tests:b\">string</a><c xmlns=\"urn:tests:b\">string2</c>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 458 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 459 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 460 | lyd_free_siblings(tree); |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 461 | |
| 462 | *state = NULL; |
| 463 | } |
| 464 | |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 465 | static void |
| 466 | test_minmax(void **state) |
| 467 | { |
| 468 | *state = test_minmax; |
| 469 | |
| 470 | const char *data; |
| 471 | struct lyd_node *tree; |
| 472 | |
| 473 | data = "<d xmlns=\"urn:tests:c\"/>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 474 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 475 | assert_null(tree); |
| 476 | logbuf_assert("Too few \"l\" instances. /c:choic/b/l"); |
| 477 | |
| 478 | data = |
| 479 | "<l xmlns=\"urn:tests:c\">val1</l>" |
| 480 | "<l xmlns=\"urn:tests:c\">val2</l>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 481 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 482 | assert_null(tree); |
| 483 | logbuf_assert("Too few \"l\" instances. /c:choic/b/l"); |
| 484 | |
| 485 | data = |
| 486 | "<l xmlns=\"urn:tests:c\">val1</l>" |
| 487 | "<l xmlns=\"urn:tests:c\">val2</l>" |
| 488 | "<l xmlns=\"urn:tests:c\">val3</l>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 489 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 490 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 491 | lyd_free_siblings(tree); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 492 | |
| 493 | data = |
| 494 | "<l xmlns=\"urn:tests:c\">val1</l>" |
| 495 | "<l xmlns=\"urn:tests:c\">val2</l>" |
| 496 | "<l xmlns=\"urn:tests:c\">val3</l>" |
| 497 | "<lt xmlns=\"urn:tests:c\"><k>val1</k></lt>" |
| 498 | "<lt xmlns=\"urn:tests:c\"><k>val2</k></lt>" |
| 499 | "<lt xmlns=\"urn:tests:c\"><k>val3</k></lt>" |
| 500 | "<lt xmlns=\"urn:tests:c\"><k>val4</k></lt>" |
| 501 | "<lt xmlns=\"urn:tests:c\"><k>val5</k></lt>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 502 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 503 | assert_null(tree); |
| 504 | logbuf_assert("Too many \"lt\" instances. /c:lt"); |
| 505 | |
| 506 | *state = NULL; |
| 507 | } |
| 508 | |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 509 | static void |
| 510 | test_unique(void **state) |
| 511 | { |
| 512 | *state = test_unique; |
| 513 | |
| 514 | const char *data; |
| 515 | struct lyd_node *tree; |
| 516 | |
| 517 | data = |
| 518 | "<lt xmlns=\"urn:tests:d\">" |
| 519 | "<k>val1</k>" |
| 520 | "<l1>same</l1>" |
| 521 | "</lt>" |
| 522 | "<lt xmlns=\"urn:tests:d\">" |
| 523 | "<k>val2</k>" |
| 524 | "</lt>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 525 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 526 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 527 | lyd_free_siblings(tree); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 528 | |
| 529 | data = |
| 530 | "<lt xmlns=\"urn:tests:d\">" |
| 531 | "<k>val1</k>" |
| 532 | "<l1>same</l1>" |
| 533 | "</lt>" |
| 534 | "<lt xmlns=\"urn:tests:d\">" |
| 535 | "<k>val2</k>" |
| 536 | "<l1>not-same</l1>" |
| 537 | "</lt>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 538 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 539 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 540 | lyd_free_siblings(tree); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 541 | |
| 542 | data = |
| 543 | "<lt xmlns=\"urn:tests:d\">" |
| 544 | "<k>val1</k>" |
| 545 | "<l1>same</l1>" |
| 546 | "</lt>" |
| 547 | "<lt xmlns=\"urn:tests:d\">" |
| 548 | "<k>val2</k>" |
| 549 | "<l1>same</l1>" |
| 550 | "</lt>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 551 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 552 | assert_null(tree); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 553 | 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] | 554 | |
| 555 | /* now try with more instances */ |
| 556 | data = |
| 557 | "<lt xmlns=\"urn:tests:d\">" |
| 558 | "<k>val1</k>" |
| 559 | "<l1>1</l1>" |
| 560 | "</lt>" |
| 561 | "<lt xmlns=\"urn:tests:d\">" |
| 562 | "<k>val2</k>" |
| 563 | "<l1>2</l1>" |
| 564 | "</lt>" |
| 565 | "<lt xmlns=\"urn:tests:d\">" |
| 566 | "<k>val3</k>" |
| 567 | "<l1>3</l1>" |
| 568 | "</lt>" |
| 569 | "<lt xmlns=\"urn:tests:d\">" |
| 570 | "<k>val4</k>" |
| 571 | "<l1>4</l1>" |
| 572 | "</lt>" |
| 573 | "<lt xmlns=\"urn:tests:d\">" |
| 574 | "<k>val5</k>" |
| 575 | "<l1>5</l1>" |
| 576 | "</lt>" |
| 577 | "<lt xmlns=\"urn:tests:d\">" |
| 578 | "<k>val6</k>" |
| 579 | "<l1>6</l1>" |
| 580 | "</lt>" |
| 581 | "<lt xmlns=\"urn:tests:d\">" |
| 582 | "<k>val7</k>" |
| 583 | "<l1>7</l1>" |
| 584 | "</lt>" |
| 585 | "<lt xmlns=\"urn:tests:d\">" |
| 586 | "<k>val8</k>" |
| 587 | "<l1>8</l1>" |
| 588 | "</lt>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 589 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 590 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 591 | lyd_free_siblings(tree); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 592 | |
| 593 | data = |
| 594 | "<lt xmlns=\"urn:tests:d\">" |
| 595 | "<k>val1</k>" |
| 596 | "<l1>1</l1>" |
| 597 | "</lt>" |
| 598 | "<lt xmlns=\"urn:tests:d\">" |
| 599 | "<k>val2</k>" |
| 600 | "<l1>2</l1>" |
| 601 | "</lt>" |
| 602 | "<lt xmlns=\"urn:tests:d\">" |
| 603 | "<k>val3</k>" |
| 604 | "<l1>3</l1>" |
| 605 | "</lt>" |
| 606 | "<lt xmlns=\"urn:tests:d\">" |
| 607 | "<k>val4</k>" |
| 608 | "</lt>" |
| 609 | "<lt xmlns=\"urn:tests:d\">" |
| 610 | "<k>val5</k>" |
| 611 | "<l1>5</l1>" |
| 612 | "</lt>" |
| 613 | "<lt xmlns=\"urn:tests:d\">" |
| 614 | "<k>val6</k>" |
| 615 | "<l1>6</l1>" |
| 616 | "</lt>" |
| 617 | "<lt xmlns=\"urn:tests:d\">" |
| 618 | "<k>val7</k>" |
| 619 | "</lt>" |
| 620 | "<lt xmlns=\"urn:tests:d\">" |
| 621 | "<k>val8</k>" |
| 622 | "</lt>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 623 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 624 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 625 | lyd_free_siblings(tree); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 626 | |
| 627 | data = |
| 628 | "<lt xmlns=\"urn:tests:d\">" |
| 629 | "<k>val1</k>" |
| 630 | "<l1>1</l1>" |
| 631 | "</lt>" |
| 632 | "<lt xmlns=\"urn:tests:d\">" |
| 633 | "<k>val2</k>" |
| 634 | "<l1>2</l1>" |
| 635 | "</lt>" |
| 636 | "<lt xmlns=\"urn:tests:d\">" |
| 637 | "<k>val3</k>" |
| 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 | "</lt>" |
| 646 | "<lt xmlns=\"urn:tests:d\">" |
| 647 | "<k>val6</k>" |
| 648 | "</lt>" |
| 649 | "<lt xmlns=\"urn:tests:d\">" |
| 650 | "<k>val7</k>" |
| 651 | "<l1>2</l1>" |
| 652 | "</lt>" |
| 653 | "<lt xmlns=\"urn:tests:d\">" |
| 654 | "<k>val8</k>" |
| 655 | "<l1>8</l1>" |
| 656 | "</lt>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 657 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 658 | assert_null(tree); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 659 | 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] | 660 | |
| 661 | *state = NULL; |
| 662 | } |
| 663 | |
| 664 | static void |
| 665 | test_unique_nested(void **state) |
| 666 | { |
| 667 | *state = test_unique_nested; |
| 668 | |
| 669 | const char *data; |
| 670 | struct lyd_node *tree; |
| 671 | |
| 672 | /* nested list uniquest are compared only with instances in the same parent list instance */ |
| 673 | data = |
| 674 | "<lt2 xmlns=\"urn:tests:d\">" |
| 675 | "<k>val1</k>" |
| 676 | "<cont>" |
| 677 | "<l2>1</l2>" |
| 678 | "</cont>" |
| 679 | "<l4>1</l4>" |
| 680 | "</lt2>" |
| 681 | "<lt2 xmlns=\"urn:tests:d\">" |
| 682 | "<k>val2</k>" |
| 683 | "<cont>" |
| 684 | "<l2>2</l2>" |
| 685 | "</cont>" |
| 686 | "<l4>2</l4>" |
| 687 | "<lt3>" |
| 688 | "<kk>val1</kk>" |
| 689 | "<l3>1</l3>" |
| 690 | "</lt3>" |
| 691 | "<lt3>" |
| 692 | "<kk>val2</kk>" |
| 693 | "<l3>2</l3>" |
| 694 | "</lt3>" |
| 695 | "</lt2>" |
| 696 | "<lt2 xmlns=\"urn:tests:d\">" |
| 697 | "<k>val3</k>" |
| 698 | "<cont>" |
| 699 | "<l2>3</l2>" |
| 700 | "</cont>" |
| 701 | "<l4>3</l4>" |
| 702 | "<lt3>" |
| 703 | "<kk>val1</kk>" |
| 704 | "<l3>2</l3>" |
| 705 | "</lt3>" |
| 706 | "</lt2>" |
| 707 | "<lt2 xmlns=\"urn:tests:d\">" |
| 708 | "<k>val4</k>" |
| 709 | "<cont>" |
| 710 | "<l2>4</l2>" |
| 711 | "</cont>" |
| 712 | "<l4>4</l4>" |
| 713 | "<lt3>" |
| 714 | "<kk>val1</kk>" |
| 715 | "<l3>3</l3>" |
| 716 | "</lt3>" |
| 717 | "</lt2>" |
| 718 | "<lt2 xmlns=\"urn:tests:d\">" |
| 719 | "<k>val5</k>" |
| 720 | "<cont>" |
| 721 | "<l2>5</l2>" |
| 722 | "</cont>" |
| 723 | "<l4>5</l4>" |
| 724 | "<lt3>" |
| 725 | "<kk>val1</kk>" |
| 726 | "<l3>3</l3>" |
| 727 | "</lt3>" |
| 728 | "</lt2>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 729 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY | LYD_OPT_STRICT, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 730 | assert_non_null(tree); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 731 | lyd_free_siblings(tree); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 732 | |
| 733 | data = |
| 734 | "<lt2 xmlns=\"urn:tests:d\">" |
| 735 | "<k>val1</k>" |
| 736 | "<cont>" |
| 737 | "<l2>1</l2>" |
| 738 | "</cont>" |
| 739 | "<l4>1</l4>" |
| 740 | "</lt2>" |
| 741 | "<lt2 xmlns=\"urn:tests:d\">" |
| 742 | "<k>val2</k>" |
| 743 | "<cont>" |
| 744 | "<l2>2</l2>" |
| 745 | "</cont>" |
| 746 | "<lt3>" |
| 747 | "<kk>val1</kk>" |
| 748 | "<l3>1</l3>" |
| 749 | "</lt3>" |
| 750 | "<lt3>" |
| 751 | "<kk>val2</kk>" |
| 752 | "<l3>2</l3>" |
| 753 | "</lt3>" |
| 754 | "<lt3>" |
| 755 | "<kk>val3</kk>" |
| 756 | "<l3>1</l3>" |
| 757 | "</lt3>" |
| 758 | "</lt2>" |
| 759 | "<lt2 xmlns=\"urn:tests:d\">" |
| 760 | "<k>val3</k>" |
| 761 | "<cont>" |
| 762 | "<l2>3</l2>" |
| 763 | "</cont>" |
| 764 | "<l4>1</l4>" |
| 765 | "<lt3>" |
| 766 | "<kk>val1</kk>" |
| 767 | "<l3>2</l3>" |
| 768 | "</lt3>" |
| 769 | "</lt2>" |
| 770 | "<lt2 xmlns=\"urn:tests:d\">" |
| 771 | "<k>val4</k>" |
| 772 | "<cont>" |
| 773 | "<l2>4</l2>" |
| 774 | "</cont>" |
| 775 | "<lt3>" |
| 776 | "<kk>val1</kk>" |
| 777 | "<l3>3</l3>" |
| 778 | "</lt3>" |
| 779 | "</lt2>" |
| 780 | "<lt2 xmlns=\"urn:tests:d\">" |
| 781 | "<k>val5</k>" |
| 782 | "<cont>" |
| 783 | "<l2>5</l2>" |
| 784 | "</cont>" |
| 785 | "<lt3>" |
| 786 | "<kk>val1</kk>" |
| 787 | "<l3>3</l3>" |
| 788 | "</lt3>" |
| 789 | "</lt2>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 790 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 791 | assert_null(tree); |
| 792 | 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] | 793 | " \"/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] | 794 | |
| 795 | data = |
| 796 | "<lt2 xmlns=\"urn:tests:d\">" |
| 797 | "<k>val1</k>" |
| 798 | "<cont>" |
| 799 | "<l2>1</l2>" |
| 800 | "</cont>" |
| 801 | "<l4>1</l4>" |
| 802 | "</lt2>" |
| 803 | "<lt2 xmlns=\"urn:tests:d\">" |
| 804 | "<k>val2</k>" |
| 805 | "<cont>" |
| 806 | "<l2>2</l2>" |
| 807 | "</cont>" |
| 808 | "<l4>2</l4>" |
| 809 | "</lt2>" |
| 810 | "<lt2 xmlns=\"urn:tests:d\">" |
| 811 | "<k>val3</k>" |
| 812 | "<cont>" |
| 813 | "<l2>3</l2>" |
| 814 | "</cont>" |
| 815 | "<l4>3</l4>" |
| 816 | "</lt2>" |
| 817 | "<lt2 xmlns=\"urn:tests:d\">" |
| 818 | "<k>val4</k>" |
| 819 | "<cont>" |
| 820 | "<l2>2</l2>" |
| 821 | "</cont>" |
| 822 | "<l4>2</l4>" |
| 823 | "</lt2>" |
| 824 | "<lt2 xmlns=\"urn:tests:d\">" |
| 825 | "<k>val5</k>" |
| 826 | "<cont>" |
| 827 | "<l2>5</l2>" |
| 828 | "</cont>" |
| 829 | "<l4>5</l4>" |
| 830 | "</lt2>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 831 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 832 | assert_null(tree); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 833 | 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] | 834 | |
| 835 | data = |
| 836 | "<lt2 xmlns=\"urn:tests:d\">" |
| 837 | "<k>val1</k>" |
| 838 | "<cont>" |
| 839 | "<l2>1</l2>" |
| 840 | "</cont>" |
| 841 | "<l4>1</l4>" |
| 842 | "<l5>1</l5>" |
| 843 | "<l6>1</l6>" |
| 844 | "</lt2>" |
| 845 | "<lt2 xmlns=\"urn:tests:d\">" |
| 846 | "<k>val2</k>" |
| 847 | "<cont>" |
| 848 | "<l2>2</l2>" |
| 849 | "</cont>" |
| 850 | "<l4>1</l4>" |
| 851 | "<l5>1</l5>" |
| 852 | "</lt2>" |
| 853 | "<lt2 xmlns=\"urn:tests:d\">" |
| 854 | "<k>val3</k>" |
| 855 | "<cont>" |
| 856 | "<l2>3</l2>" |
| 857 | "</cont>" |
| 858 | "<l4>1</l4>" |
| 859 | "<l5>3</l5>" |
| 860 | "<l6>3</l6>" |
| 861 | "</lt2>" |
| 862 | "<lt2 xmlns=\"urn:tests:d\">" |
| 863 | "<k>val4</k>" |
| 864 | "<cont>" |
| 865 | "<l2>4</l2>" |
| 866 | "</cont>" |
| 867 | "<l4>1</l4>" |
| 868 | "<l6>1</l6>" |
| 869 | "</lt2>" |
| 870 | "<lt2 xmlns=\"urn:tests:d\">" |
| 871 | "<k>val5</k>" |
| 872 | "<cont>" |
| 873 | "<l2>5</l2>" |
| 874 | "</cont>" |
| 875 | "<l4>1</l4>" |
| 876 | "<l5>3</l5>" |
| 877 | "<l6>3</l6>" |
| 878 | "</lt2>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 879 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 880 | assert_null(tree); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 881 | 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] | 882 | |
| 883 | *state = NULL; |
| 884 | } |
| 885 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 886 | static void |
| 887 | test_dup(void **state) |
| 888 | { |
| 889 | *state = test_dup; |
| 890 | |
| 891 | const char *data; |
| 892 | struct lyd_node *tree; |
| 893 | |
| 894 | data = "<d xmlns=\"urn:tests:e\">25</d><d xmlns=\"urn:tests:e\">50</d>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 895 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 896 | assert_null(tree); |
| 897 | logbuf_assert("Duplicate instance of \"d\". /e:d"); |
| 898 | |
| 899 | 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 | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 900 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 901 | assert_null(tree); |
| 902 | logbuf_assert("Duplicate instance of \"lt\". /e:lt[k='A']"); |
| 903 | |
| 904 | data = "<ll xmlns=\"urn:tests:e\">A</ll><ll xmlns=\"urn:tests:e\">B</ll><ll xmlns=\"urn:tests:e\">B</ll>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 905 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 906 | assert_null(tree); |
| 907 | logbuf_assert("Duplicate instance of \"ll\". /e:ll[.='B']"); |
| 908 | |
| 909 | data = "<cont xmlns=\"urn:tests:e\"></cont><cont xmlns=\"urn:tests:e\"/>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 910 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 911 | assert_null(tree); |
| 912 | logbuf_assert("Duplicate instance of \"cont\". /e:cont"); |
| 913 | |
| 914 | /* same tests again but using hashes */ |
| 915 | 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 | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 916 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 917 | assert_null(tree); |
| 918 | logbuf_assert("Duplicate instance of \"d\". /e:cont/d"); |
| 919 | |
| 920 | data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>" |
| 921 | "<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 | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 922 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 923 | assert_null(tree); |
| 924 | logbuf_assert("Duplicate instance of \"lt\". /e:cont/lt[k='c']"); |
| 925 | |
| 926 | data = "<cont xmlns=\"urn:tests:e\"><ll>1</ll><ll>2</ll><ll>3</ll><ll>4</ll>" |
| 927 | "<ll>a</ll><ll>b</ll><ll>c</ll><ll>d</ll><ll>d</ll></cont>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 928 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 929 | assert_null(tree); |
| 930 | logbuf_assert("Duplicate instance of \"ll\". /e:cont/ll[.='d']"); |
| 931 | |
| 932 | /* cases */ |
| 933 | 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 | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 934 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 935 | assert_null(tree); |
| 936 | logbuf_assert("Duplicate instance of \"l\". /e:l[.='b']"); |
| 937 | |
| 938 | 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 | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 939 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 940 | assert_null(tree); |
| 941 | logbuf_assert("Data for both cases \"a\" and \"b\" exist. /e:choic"); |
| 942 | |
| 943 | *state = NULL; |
| 944 | } |
| 945 | |
| 946 | static void |
| 947 | test_defaults(void **state) |
| 948 | { |
| 949 | *state = test_defaults; |
| 950 | |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 951 | char *str; |
| 952 | struct lyd_node *tree, *node; |
| 953 | const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "f"); |
| 954 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 955 | /* get defaults */ |
| 956 | tree = NULL; |
| 957 | assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 958 | assert_non_null(tree); |
| 959 | |
| 960 | /* check all defaults exist */ |
| 961 | lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG); |
| 962 | assert_string_equal(str, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 963 | "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def1</ll1>" |
| 964 | "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def2</ll1>" |
| 965 | "<ll1 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">def3</ll1>" |
| 966 | "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>" |
| 967 | "<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] | 968 | "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt2</ll2>" |
| 969 | "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">" |
| 970 | "<ll1 ncwd:default=\"true\">def1</ll1>" |
| 971 | "<ll1 ncwd:default=\"true\">def2</ll1>" |
| 972 | "<ll1 ncwd:default=\"true\">def3</ll1>" |
| 973 | "<d ncwd:default=\"true\">15</d>" |
| 974 | "<ll2 ncwd:default=\"true\">dflt1</ll2>" |
| 975 | "<ll2 ncwd:default=\"true\">dflt2</ll2>" |
| 976 | "</cont>"); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 977 | free(str); |
| 978 | |
| 979 | /* create another explicit case and validate */ |
| 980 | node = lyd_new_term(NULL, mod, "l", "value"); |
| 981 | assert_non_null(node); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 982 | assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS); |
| 983 | assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 984 | |
| 985 | /* check data tree */ |
| 986 | lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG); |
| 987 | assert_string_equal(str, |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 988 | "<d xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">15</d>" |
| 989 | "<ll2 xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" ncwd:default=\"true\">dflt1</ll2>" |
| 990 | "<ll2 xmlns=\"urn:tests:f\" 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] | 991 | "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">" |
| 992 | "<ll1 ncwd:default=\"true\">def1</ll1>" |
| 993 | "<ll1 ncwd:default=\"true\">def2</ll1>" |
| 994 | "<ll1 ncwd:default=\"true\">def3</ll1>" |
| 995 | "<d ncwd:default=\"true\">15</d>" |
| 996 | "<ll2 ncwd:default=\"true\">dflt1</ll2>" |
| 997 | "<ll2 ncwd:default=\"true\">dflt2</ll2>" |
| 998 | "</cont>" |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 999 | "<l xmlns=\"urn:tests:f\">value</l>"); |
| 1000 | free(str); |
| 1001 | |
| 1002 | /* create explicit leaf-list and leaf and validate */ |
| 1003 | node = lyd_new_term(NULL, mod, "d", "15"); |
| 1004 | assert_non_null(node); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1005 | assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1006 | node = lyd_new_term(NULL, mod, "ll2", "dflt2"); |
| 1007 | assert_non_null(node); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1008 | assert_int_equal(lyd_insert_sibling(tree, node), LY_SUCCESS); |
| 1009 | assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1010 | |
| 1011 | /* check data tree */ |
| 1012 | lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG); |
| 1013 | assert_string_equal(str, |
| 1014 | "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">" |
| 1015 | "<ll1 ncwd:default=\"true\">def1</ll1>" |
| 1016 | "<ll1 ncwd:default=\"true\">def2</ll1>" |
| 1017 | "<ll1 ncwd:default=\"true\">def3</ll1>" |
| 1018 | "<d ncwd:default=\"true\">15</d>" |
| 1019 | "<ll2 ncwd:default=\"true\">dflt1</ll2>" |
| 1020 | "<ll2 ncwd:default=\"true\">dflt2</ll2>" |
| 1021 | "</cont>" |
| 1022 | "<l xmlns=\"urn:tests:f\">value</l>" |
| 1023 | "<d xmlns=\"urn:tests:f\">15</d>" |
| 1024 | "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"); |
| 1025 | free(str); |
| 1026 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1027 | /* create first explicit container, which should become implicit */ |
| 1028 | node = lyd_new_inner(NULL, mod, "cont"); |
| 1029 | assert_non_null(node); |
| 1030 | assert_int_equal(lyd_insert_before(tree, node), LY_SUCCESS); |
| 1031 | tree = tree->prev; |
| 1032 | assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS); |
| 1033 | |
| 1034 | /* check data tree */ |
| 1035 | lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG); |
| 1036 | assert_string_equal(str, |
| 1037 | "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">" |
| 1038 | "<ll1 ncwd:default=\"true\">def1</ll1>" |
| 1039 | "<ll1 ncwd:default=\"true\">def2</ll1>" |
| 1040 | "<ll1 ncwd:default=\"true\">def3</ll1>" |
| 1041 | "<d ncwd:default=\"true\">15</d>" |
| 1042 | "<ll2 ncwd:default=\"true\">dflt1</ll2>" |
| 1043 | "<ll2 ncwd:default=\"true\">dflt2</ll2>" |
| 1044 | "</cont>" |
| 1045 | "<l xmlns=\"urn:tests:f\">value</l>" |
| 1046 | "<d xmlns=\"urn:tests:f\">15</d>" |
| 1047 | "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"); |
| 1048 | free(str); |
| 1049 | |
| 1050 | /* create second explicit container, which should become implicit, so the first tree node should be removed */ |
| 1051 | node = lyd_new_inner(NULL, mod, "cont"); |
| 1052 | assert_non_null(node); |
| 1053 | assert_int_equal(lyd_insert_after(tree, node), LY_SUCCESS); |
| 1054 | assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS); |
| 1055 | |
| 1056 | /* check data tree */ |
| 1057 | lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG); |
| 1058 | assert_string_equal(str, |
| 1059 | "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">" |
| 1060 | "<ll1 ncwd:default=\"true\">def1</ll1>" |
| 1061 | "<ll1 ncwd:default=\"true\">def2</ll1>" |
| 1062 | "<ll1 ncwd:default=\"true\">def3</ll1>" |
| 1063 | "<d ncwd:default=\"true\">15</d>" |
| 1064 | "<ll2 ncwd:default=\"true\">dflt1</ll2>" |
| 1065 | "<ll2 ncwd:default=\"true\">dflt2</ll2>" |
| 1066 | "</cont>" |
| 1067 | "<l xmlns=\"urn:tests:f\">value</l>" |
| 1068 | "<d xmlns=\"urn:tests:f\">15</d>" |
| 1069 | "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"); |
| 1070 | free(str); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1071 | |
| 1072 | /* similar changes for nested defaults */ |
| 1073 | assert_non_null(lyd_new_term(tree, NULL, "ll1", "def3")); |
| 1074 | assert_non_null(lyd_new_term(tree, NULL, "d", "5")); |
| 1075 | assert_non_null(lyd_new_term(tree, NULL, "ll2", "non-dflt")); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 1076 | assert_int_equal(lyd_validate(&tree, ctx, LYD_VALOPT_DATA_ONLY), LY_SUCCESS); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1077 | |
| 1078 | /* check data tree */ |
| 1079 | lyd_print_mem(&str, tree, LYD_XML, LYDP_WITHSIBLINGS | LYDP_WD_IMPL_TAG); |
| 1080 | assert_string_equal(str, |
| 1081 | "<cont xmlns=\"urn:tests:f\" xmlns:ncwd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\">" |
| 1082 | "<ll1>def3</ll1>" |
| 1083 | "<d>5</d>" |
| 1084 | "<ll2>non-dflt</ll2>" |
| 1085 | "</cont>" |
| 1086 | "<l xmlns=\"urn:tests:f\">value</l>" |
| 1087 | "<d xmlns=\"urn:tests:f\">15</d>" |
| 1088 | "<ll2 xmlns=\"urn:tests:f\">dflt2</ll2>"); |
| 1089 | free(str); |
| 1090 | |
| 1091 | lyd_free_siblings(tree); |
| 1092 | |
| 1093 | *state = NULL; |
| 1094 | } |
| 1095 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1096 | static void |
| 1097 | test_iffeature(void **state) |
| 1098 | { |
| 1099 | *state = test_iffeature; |
| 1100 | |
| 1101 | const char *data; |
| 1102 | struct lyd_node *tree; |
| 1103 | const struct lys_module *mod = ly_ctx_get_module_latest(ctx, "g"); |
| 1104 | |
| 1105 | /* get empty data */ |
| 1106 | tree = NULL; |
| 1107 | assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS); |
| 1108 | assert_null(tree); |
| 1109 | |
| 1110 | /* disabled by f1 */ |
| 1111 | data = |
| 1112 | "<cont xmlns=\"urn:tests:g\">" |
| 1113 | "<d>51</d>" |
| 1114 | "</cont>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 1115 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1116 | assert_null(tree); |
| 1117 | logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont"); |
| 1118 | |
| 1119 | /* enable f1 */ |
| 1120 | assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS); |
| 1121 | |
| 1122 | /* get data with default container */ |
| 1123 | assert_int_equal(lyd_validate_modules(&tree, &mod, 1, 0), LY_SUCCESS); |
| 1124 | assert_non_null(tree); |
| 1125 | lyd_free_siblings(tree); |
| 1126 | |
| 1127 | /* disabled by f2 */ |
| 1128 | data = |
| 1129 | "<cont xmlns=\"urn:tests:g\">" |
| 1130 | "<cont2>" |
| 1131 | "<e>val</e>" |
| 1132 | "</cont2>" |
| 1133 | "</cont>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 1134 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1135 | assert_null(tree); |
| 1136 | logbuf_assert("Data are disabled by \"cont2\" schema node if-feature. /g:cont/cont2"); |
| 1137 | |
| 1138 | data = |
| 1139 | "<cont xmlns=\"urn:tests:g\">" |
| 1140 | "<a>val</a>" |
| 1141 | "</cont>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 1142 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1143 | assert_null(tree); |
| 1144 | logbuf_assert("Data are disabled by \"choic\" schema node if-feature. /g:cont/a"); |
| 1145 | |
| 1146 | /* enable f3 */ |
| 1147 | assert_int_equal(lys_feature_enable(mod, "f3"), LY_SUCCESS); |
| 1148 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 1149 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1150 | assert_non_null(tree); |
| 1151 | lyd_free_siblings(tree); |
| 1152 | |
| 1153 | /* disabled by f2 */ |
| 1154 | data = |
| 1155 | "<cont xmlns=\"urn:tests:g\">" |
| 1156 | "<l>val</l>" |
| 1157 | "</cont>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 1158 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1159 | assert_null(tree); |
| 1160 | logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l"); |
| 1161 | |
| 1162 | /* enable f2 */ |
| 1163 | assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS); |
| 1164 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 1165 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1166 | assert_non_null(tree); |
| 1167 | lyd_free_siblings(tree); |
| 1168 | |
| 1169 | /* try separate validation */ |
| 1170 | assert_int_equal(lys_feature_disable(mod, "f1"), LY_SUCCESS); |
| 1171 | assert_int_equal(lys_feature_disable(mod, "f2"), LY_SUCCESS); |
| 1172 | assert_int_equal(lys_feature_disable(mod, "f3"), LY_SUCCESS); |
| 1173 | |
| 1174 | data = |
| 1175 | "<cont xmlns=\"urn:tests:g\">" |
| 1176 | "<l>val</l>" |
| 1177 | "<d>51</d>" |
| 1178 | "<cont2>" |
| 1179 | "<e>val</e>" |
| 1180 | "</cont2>" |
| 1181 | "</cont>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 1182 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY, &tree)); |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1183 | assert_non_null(tree); |
| 1184 | |
| 1185 | assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY)); |
| 1186 | logbuf_assert("Data are disabled by \"cont\" schema node if-feature. /g:cont"); |
| 1187 | |
| 1188 | assert_int_equal(lys_feature_enable(mod, "f1"), LY_SUCCESS); |
| 1189 | |
| 1190 | assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY)); |
| 1191 | logbuf_assert("Data are disabled by \"b\" schema node if-feature. /g:cont/l"); |
| 1192 | |
| 1193 | assert_int_equal(lys_feature_enable(mod, "f2"), LY_SUCCESS); |
| 1194 | |
| 1195 | assert_int_equal(LY_SUCCESS, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY)); |
| 1196 | |
| 1197 | lyd_free_siblings(tree); |
| 1198 | |
| 1199 | *state = NULL; |
| 1200 | } |
| 1201 | |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1202 | static void |
| 1203 | test_state(void **state) |
| 1204 | { |
| 1205 | *state = test_iffeature; |
| 1206 | |
| 1207 | const char *data; |
| 1208 | struct lyd_node *tree; |
| 1209 | |
| 1210 | data = |
| 1211 | "<cont xmlns=\"urn:tests:h\">" |
| 1212 | "<cont2>" |
| 1213 | "<l>val</l>" |
| 1214 | "</cont2>" |
| 1215 | "</cont>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 1216 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY | LYD_OPT_NO_STATE, &tree)); |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1217 | assert_null(tree); |
| 1218 | logbuf_assert("Invalid state data node \"cont2\" found. Line number 1."); |
| 1219 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 1220 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY | LYD_VALOPT_NO_STATE, &tree)); |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1221 | assert_null(tree); |
| 1222 | logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2"); |
| 1223 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 1224 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_PARSE_ONLY, &tree)); |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1225 | assert_non_null(tree); |
| 1226 | |
| 1227 | assert_int_equal(LY_EVALID, lyd_validate(&tree, NULL, LYD_VALOPT_DATA_ONLY | LYD_VALOPT_NO_STATE)); |
| 1228 | logbuf_assert("Invalid state data node \"cont2\" found. /h:cont/cont2"); |
| 1229 | |
| 1230 | lyd_free_siblings(tree); |
| 1231 | |
| 1232 | *state = NULL; |
| 1233 | } |
| 1234 | |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1235 | int main(void) |
| 1236 | { |
| 1237 | const struct CMUnitTest tests[] = { |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 1238 | cmocka_unit_test_teardown(test_when, teardown_s), |
| 1239 | cmocka_unit_test_teardown(test_mandatory, teardown_s), |
| 1240 | cmocka_unit_test_teardown(test_minmax, teardown_s), |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1241 | cmocka_unit_test_teardown(test_unique, teardown_s), |
| 1242 | cmocka_unit_test_teardown(test_unique_nested, teardown_s), |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1243 | cmocka_unit_test_teardown(test_dup, teardown_s), |
| 1244 | cmocka_unit_test_teardown(test_defaults, teardown_s), |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 1245 | cmocka_unit_test_teardown(test_iffeature, teardown_s), |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 1246 | cmocka_unit_test_teardown(test_state, teardown_s), |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame^] | 1247 | //cmocka_unit_test_teardown(test_edit, teardown_s), |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1248 | }; |
| 1249 | |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 1250 | return cmocka_run_group_tests(tests, setup, teardown); |
Michal Vasko | cde73ac | 2019-11-14 16:10:27 +0100 | [diff] [blame] | 1251 | } |