Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file test_diff.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 5 | * @brief tests for lyd_diff() |
| 6 | * |
| 7 | * Copyright (c) 2020 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #include <stdarg.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <setjmp.h> |
| 20 | #include <cmocka.h> |
| 21 | |
| 22 | #include "libyang.h" |
| 23 | #include "tests/config.h" |
| 24 | |
| 25 | struct state { |
| 26 | struct ly_ctx *ctx; |
| 27 | struct lyd_node *first; |
| 28 | struct lyd_node *second; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 29 | struct lyd_node *third; |
| 30 | struct lyd_node *diff1; |
| 31 | struct lyd_node *diff2; |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 32 | char *xml; |
| 33 | char *xml1; |
| 34 | char *xml2; |
| 35 | }; |
| 36 | |
| 37 | static int |
| 38 | setup_f(void **state) |
| 39 | { |
| 40 | struct state *st; |
| 41 | const char *schema = |
| 42 | "module defaults {" |
| 43 | "yang-version 1.1;" |
| 44 | "namespace \"urn:libyang:tests:defaults\";" |
| 45 | "prefix df;" |
| 46 | |
| 47 | "feature unhide;" |
| 48 | |
| 49 | "typedef defint32 {" |
| 50 | "type int32;" |
| 51 | "default \"42\";" |
| 52 | "}" |
| 53 | |
| 54 | "leaf hiddenleaf {" |
| 55 | "if-feature \"unhide\";" |
| 56 | "type int32;" |
| 57 | "default \"42\";" |
| 58 | "}" |
| 59 | |
| 60 | "container df {" |
| 61 | "leaf foo {" |
| 62 | "type defint32;" |
| 63 | "}" |
| 64 | |
| 65 | "leaf hiddenleaf {" |
| 66 | "if-feature \"unhide\";" |
| 67 | "type int32;" |
| 68 | "default \"42\";" |
| 69 | "}" |
| 70 | |
| 71 | "container bar {" |
| 72 | "presence \"\";" |
| 73 | "leaf hi {" |
| 74 | "type int32;" |
| 75 | "default \"42\";" |
| 76 | "}" |
| 77 | |
| 78 | "leaf ho {" |
| 79 | "type int32;" |
| 80 | "mandatory true;" |
| 81 | "}" |
| 82 | "}" |
| 83 | |
| 84 | "leaf-list llist {" |
| 85 | "type defint32;" |
| 86 | "ordered-by user;" |
| 87 | "}" |
| 88 | |
| 89 | "leaf-list dllist {" |
| 90 | "type uint8;" |
| 91 | "default \"1\";" |
| 92 | "default \"2\";" |
| 93 | "default \"3\";" |
| 94 | "}" |
| 95 | |
| 96 | "list list {" |
| 97 | "key \"name\";" |
| 98 | "leaf name {" |
| 99 | "type string;" |
| 100 | "}" |
| 101 | |
| 102 | "leaf value {" |
| 103 | "type int32;" |
| 104 | "default \"42\";" |
| 105 | "}" |
| 106 | "}" |
| 107 | |
| 108 | "choice select {" |
| 109 | "default \"a\";" |
| 110 | "case a {" |
| 111 | "choice a {" |
| 112 | "leaf a1 {" |
| 113 | "type int32;" |
| 114 | "default \"42\";" |
| 115 | "}" |
| 116 | |
| 117 | "leaf a2 {" |
| 118 | "type int32;" |
| 119 | "default \"24\";" |
| 120 | "}" |
| 121 | "}" |
| 122 | "}" |
| 123 | |
| 124 | "leaf b {" |
| 125 | "type string;" |
| 126 | "}" |
| 127 | |
| 128 | "container c {" |
| 129 | "presence \"\";" |
| 130 | "leaf x {" |
| 131 | "type int32;" |
| 132 | "default \"42\";" |
| 133 | "}" |
| 134 | "}" |
| 135 | "}" |
| 136 | |
| 137 | "choice select2 {" |
| 138 | "default \"s2b\";" |
| 139 | "leaf s2a {" |
| 140 | "type int32;" |
| 141 | "default \"42\";" |
| 142 | "}" |
| 143 | |
| 144 | "case s2b {" |
| 145 | "choice s2b {" |
| 146 | "default \"b1\";" |
| 147 | "case b1 {" |
| 148 | "leaf b1_1 {" |
| 149 | "type int32;" |
| 150 | "default \"42\";" |
| 151 | "}" |
| 152 | |
| 153 | "leaf b1_2 {" |
| 154 | "type string;" |
| 155 | "}" |
| 156 | |
| 157 | "leaf b1_status {" |
| 158 | "type int32;" |
| 159 | "default \"42\";" |
| 160 | "config false;" |
| 161 | "}" |
| 162 | "}" |
| 163 | |
| 164 | "leaf b2 {" |
| 165 | "type int32;" |
| 166 | "default \"42\";" |
| 167 | "}" |
| 168 | "}" |
| 169 | "}" |
| 170 | "}" |
| 171 | "}" |
| 172 | |
| 173 | "container hidden {" |
| 174 | "leaf foo {" |
| 175 | "type int32;" |
| 176 | "default \"42\";" |
| 177 | "}" |
| 178 | |
| 179 | "leaf baz {" |
| 180 | "type int32;" |
| 181 | "default \"42\";" |
| 182 | "}" |
| 183 | |
| 184 | "leaf papa {" |
| 185 | "type int32;" |
| 186 | "default \"42\";" |
| 187 | "config false;" |
| 188 | "}" |
| 189 | "}" |
| 190 | |
| 191 | "rpc rpc1 {" |
| 192 | "input {" |
| 193 | "leaf inleaf1 {" |
| 194 | "type string;" |
| 195 | "}" |
| 196 | |
| 197 | "leaf inleaf2 {" |
| 198 | "type string;" |
| 199 | "default \"def1\";" |
| 200 | "}" |
| 201 | "}" |
| 202 | |
| 203 | "output {" |
| 204 | "leaf outleaf1 {" |
| 205 | "type string;" |
| 206 | "default \"def2\";" |
| 207 | "}" |
| 208 | |
| 209 | "leaf outleaf2 {" |
| 210 | "type string;" |
| 211 | "}" |
| 212 | "}" |
| 213 | "}" |
| 214 | |
| 215 | "notification notif {" |
| 216 | "leaf ntfleaf1 {" |
| 217 | "type string;" |
| 218 | "default \"def3\";" |
| 219 | "}" |
| 220 | |
| 221 | "leaf ntfleaf2 {" |
| 222 | "type string;" |
| 223 | "}" |
| 224 | "}" |
| 225 | "}"; |
| 226 | |
| 227 | (*state) = st = calloc(1, sizeof *st); |
| 228 | assert_non_null(st); |
| 229 | assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &st->ctx)); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 230 | assert_non_null(ly_ctx_load_module(st->ctx, "ietf-netconf-acm", "2018-02-14", NULL)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 231 | assert_int_equal(LY_SUCCESS, lys_parse_mem(st->ctx, schema, LYS_IN_YANG, NULL)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static int |
| 237 | teardown_f(void **state) |
| 238 | { |
| 239 | struct state *st = (*state); |
| 240 | |
| 241 | lyd_free_siblings(st->first); |
| 242 | lyd_free_siblings(st->second); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 243 | lyd_free_siblings(st->third); |
| 244 | lyd_free_siblings(st->diff1); |
| 245 | lyd_free_siblings(st->diff2); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 246 | ly_ctx_destroy(st->ctx, NULL); |
| 247 | free(st->xml); |
| 248 | free(st->xml1); |
| 249 | free(st->xml2); |
| 250 | free(st); |
| 251 | (*state) = NULL; |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | test_invalid(void **state) |
| 258 | { |
| 259 | struct state *st = (*state); |
| 260 | const char *xml = "<df xmlns=\"urn:libyang:tests:defaults\"><foo>42</foo></df>"; |
| 261 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 262 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->first)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 263 | assert_non_null(st->first); |
| 264 | st->second = NULL; |
| 265 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 266 | assert_int_equal(lyd_diff_siblings(st->first, lyd_child(st->first), 0, &st->diff1), LY_EINVAL); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 267 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 268 | assert_int_equal(lyd_diff_siblings(NULL, NULL, 0, NULL), LY_EINVAL); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static void |
| 272 | test_same(void **state) |
| 273 | { |
| 274 | struct state *st = (*state); |
| 275 | const char *xml = "<nacm xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-acm\">" |
| 276 | "<enable-nacm>true</enable-nacm>" |
| 277 | "<read-default>permit</read-default>" |
| 278 | "<write-default>deny</write-default>" |
| 279 | "<exec-default>permit</exec-default>" |
| 280 | "<enable-external-groups>true</enable-external-groups>" |
| 281 | "</nacm><df xmlns=\"urn:libyang:tests:defaults\">" |
| 282 | "<foo>42</foo><b1_1>42</b1_1>" |
| 283 | "</df><hidden xmlns=\"urn:libyang:tests:defaults\">" |
| 284 | "<foo>42</foo><baz>42</baz></hidden>"; |
| 285 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 286 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->first)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 287 | assert_non_null(st->first); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 288 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->second)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 289 | assert_non_null(st->second); |
| 290 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 291 | assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 292 | assert_null(st->diff1); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 293 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 294 | assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 295 | lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
| 296 | lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 297 | assert_string_equal(st->xml1, st->xml2); |
| 298 | } |
| 299 | |
| 300 | static void |
| 301 | test_empty1(void **state) |
| 302 | { |
| 303 | struct state *st = (*state); |
| 304 | const char *xml = |
| 305 | "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 306 | "<foo>42</foo><b1_1>42</b1_1>" |
| 307 | "</df>" |
| 308 | "<hidden xmlns=\"urn:libyang:tests:defaults\">" |
| 309 | "<foo>42</foo><baz>42</baz>" |
| 310 | "</hidden>"; |
| 311 | |
| 312 | st->first = NULL; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 313 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->second)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 314 | assert_non_null(st->second); |
| 315 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 316 | assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 317 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 318 | assert_non_null(st->diff1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 319 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 320 | assert_string_equal(st->xml, |
| 321 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">" |
| 322 | "<foo>42</foo><b1_1>42</b1_1>" |
| 323 | "</df>" |
| 324 | "<hidden xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">" |
| 325 | "<foo>42</foo><baz>42</baz>" |
| 326 | "</hidden>" |
| 327 | ); |
| 328 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 329 | assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 330 | lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
| 331 | lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 332 | assert_string_equal(st->xml1, st->xml2); |
| 333 | } |
| 334 | |
| 335 | static void |
| 336 | test_empty2(void **state) |
| 337 | { |
| 338 | struct state *st = (*state); |
| 339 | const char *xml = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 340 | "<foo>42</foo><b1_1>42</b1_1>" |
| 341 | "</df><hidden xmlns=\"urn:libyang:tests:defaults\">" |
| 342 | "<foo>42</foo><baz>42</baz></hidden>"; |
| 343 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 344 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->first)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 345 | assert_non_null(st->first); |
| 346 | st->second = NULL; |
| 347 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 348 | assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 349 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 350 | assert_non_null(st->diff1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 351 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 352 | assert_string_equal(st->xml, |
| 353 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">" |
| 354 | "<foo>42</foo><b1_1>42</b1_1>" |
| 355 | "</df>" |
| 356 | "<hidden xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">" |
| 357 | "<foo>42</foo><baz>42</baz>" |
| 358 | "</hidden>" |
| 359 | ); |
| 360 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 361 | assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 362 | assert_ptr_equal(st->first, st->second); |
| 363 | } |
| 364 | |
| 365 | static void |
| 366 | test_empty_nested(void **state) |
| 367 | { |
| 368 | struct state *st = (*state); |
| 369 | const char *xml = "<df xmlns=\"urn:libyang:tests:defaults\"><foo>42</foo></df>"; |
| 370 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 371 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->first)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 372 | assert_non_null(st->first); |
| 373 | st->second = NULL; |
| 374 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 375 | assert_int_equal(lyd_diff_siblings(NULL, NULL, 0, &st->diff1), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 376 | assert_null(st->diff1); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 377 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 378 | assert_int_equal(lyd_diff_siblings(NULL, lyd_child(st->first), 0, &st->diff1), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 379 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 380 | assert_non_null(st->diff1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 381 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 382 | assert_string_equal(st->xml, |
| 383 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
| 384 | "<foo yang:operation=\"create\">42</foo>" |
| 385 | "</df>" |
| 386 | ); |
| 387 | |
| 388 | free(st->xml); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 389 | assert_int_equal(lyd_diff_siblings(lyd_child(st->first), NULL, 0, &st->diff2), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 390 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 391 | assert_non_null(st->diff2); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 392 | lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 393 | assert_string_equal(st->xml, |
| 394 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
| 395 | "<foo yang:operation=\"delete\">42</foo>" |
| 396 | "</df>" |
| 397 | ); |
| 398 | } |
| 399 | |
| 400 | static void |
| 401 | test_leaf(void **state) |
| 402 | { |
| 403 | struct state *st = (*state); |
| 404 | const char *xml1 = |
| 405 | "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 406 | "<foo>42</foo>" |
| 407 | "</df>" |
| 408 | "<hidden xmlns=\"urn:libyang:tests:defaults\">" |
| 409 | "<foo>42</foo><baz>42</baz>" |
| 410 | "</hidden>"; |
| 411 | const char *xml2 = |
| 412 | "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 413 | "<foo>41</foo><b1_1>42</b1_1>" |
| 414 | "</df>"; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 415 | const char *xml3 = |
| 416 | "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 417 | "<foo>40</foo>" |
| 418 | "</df>" |
| 419 | "<hidden xmlns=\"urn:libyang:tests:defaults\">" |
| 420 | "<foo>40</foo>" |
| 421 | "</hidden>"; |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 422 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 423 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml1, LYD_XML, LYD_PARSE_ONLY, 0, &st->first)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 424 | assert_non_null(st->first); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 425 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml2, LYD_XML, LYD_PARSE_ONLY, 0, &st->second)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 426 | assert_non_null(st->second); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 427 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml3, LYD_XML, LYD_PARSE_ONLY, 0, &st->third)); |
| 428 | assert_non_null(st->third); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 429 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 430 | /* diff1 */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 431 | assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 432 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 433 | assert_non_null(st->diff1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 434 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 435 | assert_string_equal(st->xml, |
| 436 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 437 | "<foo yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"42\">41</foo>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 438 | "<b1_1 yang:operation=\"create\">42</b1_1>" |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 439 | "</df>" |
| 440 | "<hidden xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 441 | "<foo>42</foo>" |
| 442 | "<baz>42</baz>" |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 443 | "</hidden>" |
| 444 | ); |
| 445 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 446 | assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 447 | lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
| 448 | lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 449 | assert_string_equal(st->xml1, st->xml2); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 450 | |
| 451 | /* diff2 */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 452 | assert_int_equal(lyd_diff_siblings(st->second, st->third, 0, &st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 453 | |
| 454 | assert_non_null(st->diff2); |
| 455 | free(st->xml); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 456 | lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 457 | assert_string_equal(st->xml, |
| 458 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 459 | "<foo yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"41\">40</foo>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 460 | "<b1_1 yang:operation=\"delete\">42</b1_1>" |
| 461 | "</df>" |
| 462 | "<hidden xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"create\">" |
| 463 | "<foo>40</foo>" |
| 464 | "</hidden>" |
| 465 | ); |
| 466 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 467 | assert_int_equal(lyd_diff_apply_all(&st->second, st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 468 | free(st->xml1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 469 | lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 470 | free(st->xml2); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 471 | lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 472 | assert_string_equal(st->xml1, st->xml2); |
| 473 | |
| 474 | /* merge */ |
Michal Vasko | fb737aa | 2020-08-06 13:53:53 +0200 | [diff] [blame] | 475 | assert_int_equal(lyd_diff_merge_all(&st->diff1, st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 476 | |
| 477 | free(st->xml); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 478 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 479 | assert_string_equal(st->xml, |
| 480 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 481 | "<foo yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"42\">40</foo>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 482 | "</df>" |
| 483 | "<hidden xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 484 | "<foo yang:operation=\"replace\" yang:orig-value=\"42\" yang:orig-default=\"false\">40</foo>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 485 | "<baz yang:operation=\"delete\">42</baz>" |
| 486 | "</hidden>" |
| 487 | ); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static void |
| 491 | test_list(void **state) |
| 492 | { |
| 493 | struct state *st = (*state); |
| 494 | const char *xml1 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 495 | "<list><name>a</name><value>1</value></list>" |
| 496 | "<list><name>b</name><value>2</value></list>" |
| 497 | "</df>"; |
| 498 | const char *xml2 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 499 | "<list><name>b</name><value>-2</value></list>" |
| 500 | "<list><name>c</name><value>3</value></list>" |
| 501 | "</df>"; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 502 | const char *xml3 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 503 | "<list><name>b</name><value>-2</value></list>" |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 504 | "<list><name>a</name><value>2</value></list>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 505 | "</df>"; |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 506 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 507 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml1, LYD_XML, LYD_PARSE_ONLY, 0, &st->first)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 508 | assert_non_null(st->first); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 509 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml2, LYD_XML, LYD_PARSE_ONLY, 0, &st->second)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 510 | assert_non_null(st->second); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 511 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml3, LYD_XML, LYD_PARSE_ONLY, 0, &st->third)); |
| 512 | assert_non_null(st->third); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 513 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 514 | /* diff1 */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 515 | assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 516 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 517 | assert_non_null(st->diff1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 518 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 519 | assert_string_equal(st->xml, |
| 520 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 521 | "<list yang:operation=\"delete\">" |
| 522 | "<name>a</name>" |
| 523 | "<value>1</value>" |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 524 | "</list>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 525 | "<list yang:operation=\"none\">" |
| 526 | "<name>b</name>" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 527 | "<value yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"2\">-2</value>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 528 | "</list>" |
| 529 | "<list yang:operation=\"create\">" |
| 530 | "<name>c</name>" |
| 531 | "<value>3</value>" |
| 532 | "</list>" |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 533 | "</df>" |
| 534 | ); |
| 535 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 536 | assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 537 | lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
| 538 | lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 539 | assert_string_equal(st->xml1, st->xml2); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 540 | |
| 541 | /* diff2 */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 542 | assert_int_equal(lyd_diff_siblings(st->second, st->third, 0, &st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 543 | |
| 544 | assert_non_null(st->diff2); |
| 545 | free(st->xml); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 546 | lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 547 | assert_string_equal(st->xml, |
| 548 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
| 549 | "<list yang:operation=\"delete\">" |
| 550 | "<name>c</name>" |
| 551 | "<value>3</value>" |
| 552 | "</list>" |
| 553 | "<list yang:operation=\"create\">" |
| 554 | "<name>a</name>" |
| 555 | "<value>2</value>" |
| 556 | "</list>" |
| 557 | "</df>" |
| 558 | ); |
| 559 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 560 | assert_int_equal(lyd_diff_apply_all(&st->second, st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 561 | free(st->xml1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 562 | lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 563 | free(st->xml2); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 564 | lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 565 | assert_string_equal(st->xml1, st->xml2); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 566 | |
| 567 | /* merge */ |
Michal Vasko | fb737aa | 2020-08-06 13:53:53 +0200 | [diff] [blame] | 568 | assert_int_equal(lyd_diff_merge_all(&st->diff1, st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 569 | |
| 570 | free(st->xml); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 571 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 572 | assert_string_equal(st->xml, |
| 573 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
| 574 | "<list yang:operation=\"none\">" |
| 575 | "<name>a</name>" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 576 | "<value yang:operation=\"replace\" yang:orig-value=\"1\" yang:orig-default=\"false\">2</value>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 577 | "</list>" |
| 578 | "<list yang:operation=\"none\">" |
| 579 | "<name>b</name>" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 580 | "<value yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"2\">-2</value>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 581 | "</list>" |
| 582 | "</df>" |
| 583 | ); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static void |
| 587 | test_userord_llist(void **state) |
| 588 | { |
| 589 | struct state *st = (*state); |
| 590 | const char *xml1 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 591 | "<llist>1</llist>" |
| 592 | "<llist>2</llist>" |
| 593 | "<llist>3</llist>" |
| 594 | "<llist>4</llist>" |
| 595 | "<llist>5</llist>" |
| 596 | "</df>"; |
| 597 | const char *xml2 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 598 | "<llist>1</llist>" |
| 599 | "<llist>4</llist>" |
| 600 | "<llist>3</llist>" |
| 601 | "<llist>2</llist>" |
| 602 | "<llist>5</llist>" |
| 603 | "</df>"; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 604 | const char *xml3 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 605 | "<llist>5</llist>" |
| 606 | "<llist>4</llist>" |
| 607 | "<llist>3</llist>" |
| 608 | "<llist>2</llist>" |
| 609 | "</df>"; |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 610 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 611 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml1, LYD_XML, LYD_PARSE_ONLY, 0, &st->first)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 612 | assert_non_null(st->first); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 613 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml2, LYD_XML, LYD_PARSE_ONLY, 0, &st->second)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 614 | assert_non_null(st->second); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 615 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml3, LYD_XML, LYD_PARSE_ONLY, 0, &st->third)); |
| 616 | assert_non_null(st->third); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 617 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 618 | /* diff1 */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 619 | assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 620 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 621 | assert_non_null(st->diff1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 622 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 623 | assert_string_equal(st->xml, |
| 624 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 625 | "<llist yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"3\" yang:value=\"1\">4</llist>" |
| 626 | "<llist yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"2\" yang:value=\"4\">3</llist>" |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 627 | "</df>" |
| 628 | ); |
| 629 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 630 | assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 631 | lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
| 632 | lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 633 | assert_string_equal(st->xml1, st->xml2); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 634 | |
| 635 | /* diff2 */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 636 | assert_int_equal(lyd_diff_siblings(st->second, st->third, 0, &st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 637 | |
| 638 | assert_non_null(st->diff2); |
| 639 | free(st->xml); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 640 | lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 641 | assert_string_equal(st->xml, |
| 642 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
| 643 | "<llist yang:operation=\"delete\" yang:orig-value=\"\">1</llist>" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 644 | "<llist yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"2\" yang:value=\"\">5</llist>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 645 | "</df>" |
| 646 | ); |
| 647 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 648 | assert_int_equal(lyd_diff_apply_all(&st->second, st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 649 | free(st->xml1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 650 | lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 651 | free(st->xml2); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 652 | lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 653 | assert_string_equal(st->xml1, st->xml2); |
| 654 | |
| 655 | /* merge */ |
Michal Vasko | fb737aa | 2020-08-06 13:53:53 +0200 | [diff] [blame] | 656 | assert_int_equal(lyd_diff_merge_all(&st->diff1, st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 657 | |
| 658 | free(st->xml); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 659 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 660 | assert_string_equal(st->xml, |
| 661 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 662 | "<llist yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"3\" yang:value=\"1\">4</llist>" |
| 663 | "<llist yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"2\" yang:value=\"4\">3</llist>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 664 | "<llist yang:orig-value=\"\" yang:operation=\"delete\">1</llist>" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 665 | "<llist yang:orig-default=\"false\" yang:orig-value=\"2\" yang:value=\"\" yang:operation=\"replace\">5</llist>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 666 | "</df>" |
| 667 | ); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static void |
| 671 | test_userord_llist2(void **state) |
| 672 | { |
| 673 | struct state *st = (*state); |
| 674 | const char *xml1 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 675 | "<llist>1</llist>" |
| 676 | "<list><name>a</name><value>1</value></list>" |
| 677 | "<llist>2</llist>" |
| 678 | "<llist>3</llist>" |
| 679 | "<llist>4</llist>" |
| 680 | "</df>"; |
| 681 | const char *xml2 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 682 | "<llist>1</llist>" |
| 683 | "<list><name>a</name><value>1</value></list>" |
| 684 | "<llist>2</llist>" |
| 685 | "<llist>4</llist>" |
| 686 | "<llist>3</llist>" |
| 687 | "</df>"; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 688 | const char *xml3 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 689 | "<llist>4</llist>" |
| 690 | "<llist>1</llist>" |
| 691 | "<list><name>a</name><value>1</value></list>" |
| 692 | "<llist>3</llist>" |
| 693 | "</df>"; |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 694 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 695 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml1, LYD_XML, LYD_PARSE_ONLY, 0, &st->first)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 696 | assert_non_null(st->first); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 697 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml2, LYD_XML, LYD_PARSE_ONLY, 0, &st->second)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 698 | assert_non_null(st->second); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 699 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml3, LYD_XML, LYD_PARSE_ONLY, 0, &st->third)); |
| 700 | assert_non_null(st->third); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 701 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 702 | /* diff1 */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 703 | assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 704 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 705 | assert_non_null(st->diff1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 706 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 707 | assert_string_equal(st->xml, |
| 708 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 709 | "<llist yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"3\" yang:value=\"2\">4</llist>" |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 710 | "</df>" |
| 711 | ); |
| 712 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 713 | assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 714 | lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
| 715 | lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 716 | assert_string_equal(st->xml1, st->xml2); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 717 | |
| 718 | /* diff2 */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 719 | assert_int_equal(lyd_diff_siblings(st->second, st->third, 0, &st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 720 | |
| 721 | assert_non_null(st->diff2); |
| 722 | free(st->xml); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 723 | lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 724 | assert_string_equal(st->xml, |
| 725 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
| 726 | "<llist yang:operation=\"delete\" yang:orig-value=\"1\">2</llist>" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 727 | "<llist yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"1\" yang:value=\"\">4</llist>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 728 | "</df>" |
| 729 | ); |
| 730 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 731 | assert_int_equal(lyd_diff_apply_all(&st->second, st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 732 | free(st->xml1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 733 | lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 734 | free(st->xml2); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 735 | lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 736 | assert_string_equal(st->xml1, st->xml2); |
| 737 | |
| 738 | /* merge */ |
Michal Vasko | fb737aa | 2020-08-06 13:53:53 +0200 | [diff] [blame] | 739 | assert_int_equal(lyd_diff_merge_all(&st->diff1, st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 740 | |
| 741 | free(st->xml); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 742 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 743 | assert_string_equal(st->xml, |
| 744 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 745 | "<llist yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"3\" yang:value=\"\">4</llist>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 746 | "<llist yang:orig-value=\"1\" yang:operation=\"delete\">2</llist>" |
| 747 | "</df>" |
| 748 | ); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | static void |
| 752 | test_userord_mix(void **state) |
| 753 | { |
| 754 | struct state *st = (*state); |
| 755 | const char *xml1 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 756 | "<llist>1</llist>" |
| 757 | "<llist>2</llist>" |
| 758 | "<llist>3</llist>" |
| 759 | "</df>"; |
| 760 | const char *xml2 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 761 | "<llist>3</llist>" |
| 762 | "<llist>1</llist>" |
| 763 | "</df>"; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 764 | const char *xml3 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 765 | "<llist>1</llist>" |
| 766 | "<llist>4</llist>" |
| 767 | "<llist>3</llist>" |
| 768 | "</df>"; |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 769 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 770 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml1, LYD_XML, LYD_PARSE_ONLY, 0, &st->first)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 771 | assert_non_null(st->first); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 772 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml2, LYD_XML, LYD_PARSE_ONLY, 0, &st->second)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 773 | assert_non_null(st->second); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 774 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml3, LYD_XML, LYD_PARSE_ONLY, 0, &st->third)); |
| 775 | assert_non_null(st->third); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 776 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 777 | /* diff1 */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 778 | assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 779 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 780 | assert_non_null(st->diff1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 781 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 782 | assert_string_equal(st->xml, |
| 783 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
| 784 | "<llist yang:operation=\"delete\" yang:orig-value=\"1\">2</llist>" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 785 | "<llist yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"1\" yang:value=\"\">3</llist>" |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 786 | "</df>" |
| 787 | ); |
| 788 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 789 | assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 790 | lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
| 791 | lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 792 | assert_string_equal(st->xml1, st->xml2); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 793 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 794 | /* diff2 */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 795 | assert_int_equal(lyd_diff_siblings(st->second, st->third, 0, &st->diff2), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 796 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 797 | assert_non_null(st->diff2); |
| 798 | free(st->xml); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 799 | lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 800 | assert_string_equal(st->xml, |
| 801 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 802 | "<llist yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"3\" yang:value=\"\">1</llist>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 803 | "<llist yang:operation=\"create\" yang:value=\"1\">4</llist>" |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 804 | "</df>" |
| 805 | ); |
| 806 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 807 | assert_int_equal(lyd_diff_apply_all(&st->second, st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 808 | free(st->xml1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 809 | lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 810 | free(st->xml2); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 811 | lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 812 | assert_string_equal(st->xml1, st->xml2); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 813 | |
| 814 | /* merge */ |
Michal Vasko | fb737aa | 2020-08-06 13:53:53 +0200 | [diff] [blame] | 815 | assert_int_equal(lyd_diff_merge_all(&st->diff1, st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 816 | |
| 817 | free(st->xml); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 818 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 819 | assert_string_equal(st->xml, |
| 820 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
| 821 | "<llist yang:operation=\"delete\" yang:orig-value=\"1\">2</llist>" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 822 | "<llist yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"1\" yang:value=\"\">3</llist>" |
| 823 | "<llist yang:orig-default=\"false\" yang:orig-value=\"3\" yang:value=\"\" yang:operation=\"replace\">1</llist>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 824 | "<llist yang:value=\"1\" yang:operation=\"create\">4</llist>" |
| 825 | "</df>" |
| 826 | ); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | static void |
| 830 | test_wd(void **state) |
| 831 | { |
| 832 | struct state *st = (*state); |
| 833 | const struct lys_module *mod; |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 834 | const char *xml2 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 835 | "<foo>41</foo><dllist>4</dllist>" |
| 836 | "</df>"; |
| 837 | const char *xml3 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 838 | "<foo>42</foo><dllist>4</dllist><dllist>1</dllist>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 839 | "</df>"; |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 840 | |
| 841 | mod = ly_ctx_get_module_implemented(st->ctx, "defaults"); |
| 842 | assert_non_null(mod); |
| 843 | |
| 844 | st->first = NULL; |
Michal Vasko | 8104fd4 | 2020-07-13 11:09:51 +0200 | [diff] [blame] | 845 | assert_int_equal(lyd_validate_module(&st->first, mod, 0, NULL), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 846 | assert_ptr_not_equal(st->first, NULL); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 847 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &st->second)); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 848 | assert_non_null(st->second); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 849 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml3, LYD_XML, 0, LYD_VALIDATE_PRESENT, &st->third)); |
| 850 | assert_non_null(st->third); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 851 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 852 | /* diff1 */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 853 | assert_int_equal(lyd_diff_siblings(st->first, st->second, LYD_DIFF_DEFAULTS, &st->diff1), LY_SUCCESS); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 854 | |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 855 | assert_non_null(st->diff1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 856 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 857 | assert_string_equal(st->xml, |
| 858 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
| 859 | "<foo yang:operation=\"replace\" yang:orig-default=\"true\" yang:orig-value=\"42\">41</foo>" |
| 860 | "<dllist yang:operation=\"delete\">1</dllist>" |
| 861 | "<dllist yang:operation=\"delete\">2</dllist>" |
| 862 | "<dllist yang:operation=\"delete\">3</dllist>" |
| 863 | "<dllist yang:operation=\"create\">4</dllist>" |
| 864 | "</df>" |
| 865 | ); |
| 866 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 867 | assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 868 | lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
| 869 | lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 870 | assert_string_equal(st->xml1, st->xml2); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 871 | |
| 872 | /* diff2 */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 873 | assert_int_equal(lyd_diff_siblings(st->second, st->third, LYD_DIFF_DEFAULTS, &st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 874 | |
| 875 | assert_non_null(st->diff2); |
| 876 | free(st->xml); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 877 | lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 878 | assert_string_equal(st->xml, |
| 879 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
Michal Vasko | 4b715ca | 2020-11-11 18:39:57 +0100 | [diff] [blame^] | 880 | "<foo yang:operation=\"replace\" yang:orig-default=\"false\" yang:orig-value=\"41\">42</foo>" |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 881 | "<dllist yang:operation=\"create\">1</dllist>" |
| 882 | "</df>" |
| 883 | ); |
| 884 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 885 | assert_int_equal(lyd_diff_apply_all(&st->second, st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 886 | free(st->xml1); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 887 | lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 888 | free(st->xml2); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 889 | lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 890 | assert_string_equal(st->xml1, st->xml2); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 891 | |
| 892 | /* merge */ |
Michal Vasko | fb737aa | 2020-08-06 13:53:53 +0200 | [diff] [blame] | 893 | assert_int_equal(lyd_diff_merge_all(&st->diff1, st->diff2), LY_SUCCESS); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 894 | |
| 895 | free(st->xml); |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 896 | lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL | LYD_PRINT_SHRINK); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 897 | assert_string_equal(st->xml, |
| 898 | "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">" |
| 899 | "<foo yang:orig-default=\"true\" yang:operation=\"none\">42</foo>" |
| 900 | "<dllist yang:operation=\"none\" yang:orig-default=\"true\">1</dllist>" |
| 901 | "<dllist yang:operation=\"delete\">2</dllist>" |
| 902 | "<dllist yang:operation=\"delete\">3</dllist>" |
| 903 | "<dllist yang:operation=\"create\">4</dllist>" |
| 904 | "</df>" |
| 905 | ); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | int main(void) |
| 909 | { |
| 910 | const struct CMUnitTest tests[] = { |
| 911 | cmocka_unit_test_setup_teardown(test_invalid, setup_f, teardown_f), |
| 912 | cmocka_unit_test_setup_teardown(test_same, setup_f, teardown_f), |
| 913 | cmocka_unit_test_setup_teardown(test_empty1, setup_f, teardown_f), |
| 914 | cmocka_unit_test_setup_teardown(test_empty2, setup_f, teardown_f), |
| 915 | cmocka_unit_test_setup_teardown(test_empty_nested, setup_f, teardown_f), |
| 916 | cmocka_unit_test_setup_teardown(test_leaf, setup_f, teardown_f), |
| 917 | cmocka_unit_test_setup_teardown(test_list, setup_f, teardown_f), |
| 918 | cmocka_unit_test_setup_teardown(test_userord_llist, setup_f, teardown_f), |
| 919 | cmocka_unit_test_setup_teardown(test_userord_llist2, setup_f, teardown_f), |
| 920 | cmocka_unit_test_setup_teardown(test_userord_mix, setup_f, teardown_f), |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 921 | cmocka_unit_test_setup_teardown(test_wd, setup_f, teardown_f), |
| 922 | }; |
| 923 | |
| 924 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 925 | } |