blob: c84ce86d8ea4ed446fe571816ee3821d927f0006 [file] [log] [blame]
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001/**
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
25struct state {
26 struct ly_ctx *ctx;
27 struct lyd_node *first;
28 struct lyd_node *second;
Michal Vaskoe6323f62020-07-09 15:49:02 +020029 struct lyd_node *third;
30 struct lyd_node *diff1;
31 struct lyd_node *diff2;
Michal Vaskoe893ddd2020-06-23 13:35:20 +020032 char *xml;
33 char *xml1;
34 char *xml2;
35};
36
37static int
38setup_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));
230 assert_non_null(ly_ctx_load_module(st->ctx, "ietf-netconf-acm", "2018-02-14"));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200231 assert_int_equal(LY_SUCCESS, lys_parse_mem(st->ctx, schema, LYS_IN_YANG, NULL));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200232
233 return 0;
234}
235
236static int
237teardown_f(void **state)
238{
239 struct state *st = (*state);
240
241 lyd_free_siblings(st->first);
242 lyd_free_siblings(st->second);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200243 lyd_free_siblings(st->third);
244 lyd_free_siblings(st->diff1);
245 lyd_free_siblings(st->diff2);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200246 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
256static void
257test_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 Krejci7931b192020-06-25 17:05:03 +0200262 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->first));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200263 assert_non_null(st->first);
264 st->second = NULL;
265
Michal Vasko3a41dff2020-07-15 14:30:28 +0200266 assert_int_equal(lyd_diff_siblings(st->first, lyd_node_children(st->first, 0), 0, &st->diff1), LY_EINVAL);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200267
Michal Vasko3a41dff2020-07-15 14:30:28 +0200268 assert_int_equal(lyd_diff_siblings(NULL, NULL, 0, NULL), LY_EINVAL);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200269}
270
271static void
272test_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 Krejci7931b192020-06-25 17:05:03 +0200286 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->first));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200287 assert_non_null(st->first);
Radek Krejci7931b192020-06-25 17:05:03 +0200288 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->second));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200289 assert_non_null(st->second);
290
Michal Vasko3a41dff2020-07-15 14:30:28 +0200291 assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200292 assert_null(st->diff1);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200293
Michal Vasko3a41dff2020-07-15 14:30:28 +0200294 assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS);
Radek Krejci7931b192020-06-25 17:05:03 +0200295 lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS);
296 lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200297 assert_string_equal(st->xml1, st->xml2);
298}
299
300static void
301test_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 Krejci7931b192020-06-25 17:05:03 +0200313 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->second));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200314 assert_non_null(st->second);
315
Michal Vasko3a41dff2020-07-15 14:30:28 +0200316 assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200317
Michal Vaskoe6323f62020-07-09 15:49:02 +0200318 assert_non_null(st->diff1);
319 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200320 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 Vasko3a41dff2020-07-15 14:30:28 +0200329 assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS);
Radek Krejci7931b192020-06-25 17:05:03 +0200330 lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS);
331 lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200332 assert_string_equal(st->xml1, st->xml2);
333}
334
335static void
336test_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 Krejci7931b192020-06-25 17:05:03 +0200344 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->first));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200345 assert_non_null(st->first);
346 st->second = NULL;
347
Michal Vasko3a41dff2020-07-15 14:30:28 +0200348 assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200349
Michal Vaskoe6323f62020-07-09 15:49:02 +0200350 assert_non_null(st->diff1);
351 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200352 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 Vasko3a41dff2020-07-15 14:30:28 +0200361 assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200362 assert_ptr_equal(st->first, st->second);
363}
364
365static void
366test_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 Krejci7931b192020-06-25 17:05:03 +0200371 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml, LYD_XML, LYD_PARSE_ONLY, 0, &st->first));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200372 assert_non_null(st->first);
373 st->second = NULL;
374
Michal Vasko3a41dff2020-07-15 14:30:28 +0200375 assert_int_equal(lyd_diff_siblings(NULL, NULL, 0, &st->diff1), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200376 assert_null(st->diff1);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200377
Michal Vasko3a41dff2020-07-15 14:30:28 +0200378 assert_int_equal(lyd_diff_siblings(NULL, lyd_node_children(st->first, 0), 0, &st->diff1), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200379
Michal Vaskoe6323f62020-07-09 15:49:02 +0200380 assert_non_null(st->diff1);
381 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200382 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);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200389 assert_int_equal(lyd_diff_siblings(lyd_node_children(st->first, 0), NULL, 0, &st->diff2), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200390
Michal Vaskoe6323f62020-07-09 15:49:02 +0200391 assert_non_null(st->diff2);
392 lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200393 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
400static void
401test_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 Vaskoe6323f62020-07-09 15:49:02 +0200415 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 Vaskoe893ddd2020-06-23 13:35:20 +0200422
Radek Krejci7931b192020-06-25 17:05:03 +0200423 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml1, LYD_XML, LYD_PARSE_ONLY, 0, &st->first));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200424 assert_non_null(st->first);
Radek Krejci7931b192020-06-25 17:05:03 +0200425 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml2, LYD_XML, LYD_PARSE_ONLY, 0, &st->second));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200426 assert_non_null(st->second);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200427 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 Vaskoe893ddd2020-06-23 13:35:20 +0200429
Michal Vaskoe6323f62020-07-09 15:49:02 +0200430 /* diff1 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200431 assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200432
Michal Vaskoe6323f62020-07-09 15:49:02 +0200433 assert_non_null(st->diff1);
434 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200435 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 Vaskoe6323f62020-07-09 15:49:02 +0200437 "<foo yang:operation=\"replace\" yang:orig-value=\"42\">41</foo>"
438 "<b1_1 yang:operation=\"create\">42</b1_1>"
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200439 "</df>"
440 "<hidden xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"delete\">"
Michal Vaskoe6323f62020-07-09 15:49:02 +0200441 "<foo>42</foo>"
442 "<baz>42</baz>"
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200443 "</hidden>"
444 );
445
Michal Vasko3a41dff2020-07-15 14:30:28 +0200446 assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS);
Radek Krejci7931b192020-06-25 17:05:03 +0200447 lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS);
448 lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200449 assert_string_equal(st->xml1, st->xml2);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200450
451 /* diff2 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200452 assert_int_equal(lyd_diff_siblings(st->second, st->third, 0, &st->diff2), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200453
454 assert_non_null(st->diff2);
455 free(st->xml);
456 lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS);
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\">"
459 "<foo yang:operation=\"replace\" yang:orig-value=\"41\">40</foo>"
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 Vasko3a41dff2020-07-15 14:30:28 +0200467 assert_int_equal(lyd_diff_apply_all(&st->second, st->diff2), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200468 free(st->xml1);
469 lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
470 free(st->xml2);
471 lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS);
472 assert_string_equal(st->xml1, st->xml2);
473
474 /* merge */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200475 assert_int_equal(lyd_diff_merge_all(st->diff2, &st->diff1), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200476
477 free(st->xml);
478 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
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\">"
481 "<foo yang:operation=\"replace\" yang:orig-value=\"42\">40</foo>"
482 "</df>"
483 "<hidden xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">"
484 "<foo yang:operation=\"replace\" yang:orig-value=\"42\">40</foo>"
485 "<baz yang:operation=\"delete\">42</baz>"
486 "</hidden>"
487 );
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200488}
489
490static void
491test_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 Vaskoe6323f62020-07-09 15:49:02 +0200502 const char *xml3 = "<df xmlns=\"urn:libyang:tests:defaults\">"
Michal Vaskoe6323f62020-07-09 15:49:02 +0200503 "<list><name>b</name><value>-2</value></list>"
Michal Vaskob104f112020-07-17 09:54:54 +0200504 "<list><name>a</name><value>2</value></list>"
Michal Vaskoe6323f62020-07-09 15:49:02 +0200505 "</df>";
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200506
Radek Krejci7931b192020-06-25 17:05:03 +0200507 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml1, LYD_XML, LYD_PARSE_ONLY, 0, &st->first));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200508 assert_non_null(st->first);
Radek Krejci7931b192020-06-25 17:05:03 +0200509 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml2, LYD_XML, LYD_PARSE_ONLY, 0, &st->second));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200510 assert_non_null(st->second);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200511 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 Vaskoe893ddd2020-06-23 13:35:20 +0200513
Michal Vaskoe6323f62020-07-09 15:49:02 +0200514 /* diff1 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200515 assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200516
Michal Vaskoe6323f62020-07-09 15:49:02 +0200517 assert_non_null(st->diff1);
518 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200519 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 Vaskoe6323f62020-07-09 15:49:02 +0200521 "<list yang:operation=\"delete\">"
522 "<name>a</name>"
523 "<value>1</value>"
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200524 "</list>"
Michal Vaskoe6323f62020-07-09 15:49:02 +0200525 "<list yang:operation=\"none\">"
526 "<name>b</name>"
527 "<value yang:operation=\"replace\" yang:orig-value=\"2\">-2</value>"
528 "</list>"
529 "<list yang:operation=\"create\">"
530 "<name>c</name>"
531 "<value>3</value>"
532 "</list>"
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200533 "</df>"
534 );
535
Michal Vasko3a41dff2020-07-15 14:30:28 +0200536 assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS);
Radek Krejci7931b192020-06-25 17:05:03 +0200537 lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS);
538 lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200539 assert_string_equal(st->xml1, st->xml2);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200540
541 /* diff2 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200542 assert_int_equal(lyd_diff_siblings(st->second, st->third, 0, &st->diff2), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200543
544 assert_non_null(st->diff2);
545 free(st->xml);
546 lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS);
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 Vasko3a41dff2020-07-15 14:30:28 +0200560 assert_int_equal(lyd_diff_apply_all(&st->second, st->diff2), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200561 free(st->xml1);
562 lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
563 free(st->xml2);
564 lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskob104f112020-07-17 09:54:54 +0200565 assert_string_equal(st->xml1, st->xml2);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200566
567 /* merge */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200568 assert_int_equal(lyd_diff_merge_all(st->diff2, &st->diff1), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200569
570 free(st->xml);
571 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
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>"
576 "<value yang:operation=\"replace\" yang:orig-value=\"1\">2</value>"
577 "</list>"
578 "<list yang:operation=\"none\">"
579 "<name>b</name>"
580 "<value yang:operation=\"replace\" yang:orig-value=\"2\">-2</value>"
581 "</list>"
582 "</df>"
583 );
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200584}
585
586static void
587test_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 Vaskoe6323f62020-07-09 15:49:02 +0200604 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 Vaskoe893ddd2020-06-23 13:35:20 +0200610
Radek Krejci7931b192020-06-25 17:05:03 +0200611 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml1, LYD_XML, LYD_PARSE_ONLY, 0, &st->first));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200612 assert_non_null(st->first);
Radek Krejci7931b192020-06-25 17:05:03 +0200613 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml2, LYD_XML, LYD_PARSE_ONLY, 0, &st->second));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200614 assert_non_null(st->second);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200615 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 Vaskoe893ddd2020-06-23 13:35:20 +0200617
Michal Vaskoe6323f62020-07-09 15:49:02 +0200618 /* diff1 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200619 assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200620
Michal Vaskoe6323f62020-07-09 15:49:02 +0200621 assert_non_null(st->diff1);
622 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200623 assert_string_equal(st->xml,
624 "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">"
625 "<llist yang:operation=\"replace\" yang:orig-value=\"3\" yang:value=\"1\">4</llist>"
626 "<llist yang:operation=\"replace\" yang:orig-value=\"2\" yang:value=\"4\">3</llist>"
627 "</df>"
628 );
629
Michal Vasko3a41dff2020-07-15 14:30:28 +0200630 assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS);
Radek Krejci7931b192020-06-25 17:05:03 +0200631 lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS);
632 lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200633 assert_string_equal(st->xml1, st->xml2);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200634
635 /* diff2 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200636 assert_int_equal(lyd_diff_siblings(st->second, st->third, 0, &st->diff2), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200637
638 assert_non_null(st->diff2);
639 free(st->xml);
640 lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS);
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>"
644 "<llist yang:operation=\"replace\" yang:orig-value=\"2\" yang:value=\"\">5</llist>"
645 "</df>"
646 );
647
Michal Vasko3a41dff2020-07-15 14:30:28 +0200648 assert_int_equal(lyd_diff_apply_all(&st->second, st->diff2), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200649 free(st->xml1);
650 lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
651 free(st->xml2);
652 lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS);
653 assert_string_equal(st->xml1, st->xml2);
654
655 /* merge */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200656 assert_int_equal(lyd_diff_merge_all(st->diff2, &st->diff1), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200657
658 free(st->xml);
659 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
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\">"
662 "<llist yang:operation=\"replace\" yang:orig-value=\"3\" yang:value=\"1\">4</llist>"
663 "<llist yang:operation=\"replace\" yang:orig-value=\"2\" yang:value=\"4\">3</llist>"
664 "<llist yang:orig-value=\"\" yang:operation=\"delete\">1</llist>"
665 "<llist yang:orig-value=\"2\" yang:value=\"\" yang:operation=\"replace\">5</llist>"
666 "</df>"
667 );
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200668}
669
670static void
671test_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 Vaskoe6323f62020-07-09 15:49:02 +0200688 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 Vaskoe893ddd2020-06-23 13:35:20 +0200694
Radek Krejci7931b192020-06-25 17:05:03 +0200695 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml1, LYD_XML, LYD_PARSE_ONLY, 0, &st->first));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200696 assert_non_null(st->first);
Radek Krejci7931b192020-06-25 17:05:03 +0200697 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml2, LYD_XML, LYD_PARSE_ONLY, 0, &st->second));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200698 assert_non_null(st->second);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200699 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 Vaskoe893ddd2020-06-23 13:35:20 +0200701
Michal Vaskoe6323f62020-07-09 15:49:02 +0200702 /* diff1 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200703 assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200704
Michal Vaskoe6323f62020-07-09 15:49:02 +0200705 assert_non_null(st->diff1);
706 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200707 assert_string_equal(st->xml,
708 "<df xmlns=\"urn:libyang:tests:defaults\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">"
709 "<llist yang:operation=\"replace\" yang:orig-value=\"3\" yang:value=\"2\">4</llist>"
710 "</df>"
711 );
712
Michal Vasko3a41dff2020-07-15 14:30:28 +0200713 assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS);
Radek Krejci7931b192020-06-25 17:05:03 +0200714 lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS);
715 lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200716 assert_string_equal(st->xml1, st->xml2);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200717
718 /* diff2 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200719 assert_int_equal(lyd_diff_siblings(st->second, st->third, 0, &st->diff2), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200720
721 assert_non_null(st->diff2);
722 free(st->xml);
723 lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS);
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>"
727 "<llist yang:operation=\"replace\" yang:orig-value=\"1\" yang:value=\"\">4</llist>"
728 "</df>"
729 );
730
Michal Vasko3a41dff2020-07-15 14:30:28 +0200731 assert_int_equal(lyd_diff_apply_all(&st->second, st->diff2), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200732 free(st->xml1);
733 lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
734 free(st->xml2);
735 lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS);
736 assert_string_equal(st->xml1, st->xml2);
737
738 /* merge */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200739 assert_int_equal(lyd_diff_merge_all(st->diff2, &st->diff1), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200740
741 free(st->xml);
742 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
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\">"
745 "<llist yang:operation=\"replace\" yang:orig-value=\"3\" yang:value=\"\">4</llist>"
746 "<llist yang:orig-value=\"1\" yang:operation=\"delete\">2</llist>"
747 "</df>"
748 );
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200749}
750
751static void
752test_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 Vaskoe6323f62020-07-09 15:49:02 +0200764 const char *xml3 = "<df xmlns=\"urn:libyang:tests:defaults\">"
765 "<llist>1</llist>"
766 "<llist>4</llist>"
767 "<llist>3</llist>"
768 "</df>";
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200769
Radek Krejci7931b192020-06-25 17:05:03 +0200770 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml1, LYD_XML, LYD_PARSE_ONLY, 0, &st->first));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200771 assert_non_null(st->first);
Radek Krejci7931b192020-06-25 17:05:03 +0200772 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml2, LYD_XML, LYD_PARSE_ONLY, 0, &st->second));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200773 assert_non_null(st->second);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200774 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 Vaskoe893ddd2020-06-23 13:35:20 +0200776
Michal Vaskoe6323f62020-07-09 15:49:02 +0200777 /* diff1 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200778 assert_int_equal(lyd_diff_siblings(st->first, st->second, 0, &st->diff1), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200779
Michal Vaskoe6323f62020-07-09 15:49:02 +0200780 assert_non_null(st->diff1);
781 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200782 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>"
785 "<llist yang:operation=\"replace\" yang:orig-value=\"1\" yang:value=\"\">3</llist>"
786 "</df>"
787 );
788
Michal Vasko3a41dff2020-07-15 14:30:28 +0200789 assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS);
Radek Krejci7931b192020-06-25 17:05:03 +0200790 lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS);
791 lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200792 assert_string_equal(st->xml1, st->xml2);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200793
Michal Vaskoe6323f62020-07-09 15:49:02 +0200794 /* diff2 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200795 assert_int_equal(lyd_diff_siblings(st->second, st->third, 0, &st->diff2), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200796
Michal Vaskoe6323f62020-07-09 15:49:02 +0200797 assert_non_null(st->diff2);
798 free(st->xml);
799 lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200800 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 Vaskoe6323f62020-07-09 15:49:02 +0200802 "<llist yang:operation=\"replace\" yang:orig-value=\"3\" yang:value=\"\">1</llist>"
803 "<llist yang:operation=\"create\" yang:value=\"1\">4</llist>"
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200804 "</df>"
805 );
806
Michal Vasko3a41dff2020-07-15 14:30:28 +0200807 assert_int_equal(lyd_diff_apply_all(&st->second, st->diff2), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200808 free(st->xml1);
809 lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
810 free(st->xml2);
811 lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200812 assert_string_equal(st->xml1, st->xml2);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200813
814 /* merge */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200815 assert_int_equal(lyd_diff_merge_all(st->diff2, &st->diff1), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200816
817 free(st->xml);
818 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS);
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>"
822 "<llist yang:operation=\"replace\" yang:orig-value=\"1\" yang:value=\"\">3</llist>"
823 "<llist yang:orig-value=\"3\" yang:value=\"\" yang:operation=\"replace\">1</llist>"
824 "<llist yang:value=\"1\" yang:operation=\"create\">4</llist>"
825 "</df>"
826 );
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200827}
828
829static void
830test_wd(void **state)
831{
832 struct state *st = (*state);
833 const struct lys_module *mod;
Michal Vaskoe6323f62020-07-09 15:49:02 +0200834 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 Vaskob104f112020-07-17 09:54:54 +0200838 "<foo>42</foo><dllist>4</dllist><dllist>1</dllist>"
Michal Vaskoe6323f62020-07-09 15:49:02 +0200839 "</df>";
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200840
841 mod = ly_ctx_get_module_implemented(st->ctx, "defaults");
842 assert_non_null(mod);
843
844 st->first = NULL;
Michal Vasko8104fd42020-07-13 11:09:51 +0200845 assert_int_equal(lyd_validate_module(&st->first, mod, 0, NULL), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200846 assert_ptr_not_equal(st->first, NULL);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200847 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(st->ctx, xml2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &st->second));
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200848 assert_non_null(st->second);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200849 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 Vaskoe893ddd2020-06-23 13:35:20 +0200851
Michal Vaskoe6323f62020-07-09 15:49:02 +0200852 /* diff1 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200853 assert_int_equal(lyd_diff_siblings(st->first, st->second, LYD_DIFF_DEFAULTS, &st->diff1), LY_SUCCESS);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200854
Michal Vaskoe6323f62020-07-09 15:49:02 +0200855 assert_non_null(st->diff1);
856 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL);
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200857 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 Vasko3a41dff2020-07-15 14:30:28 +0200867 assert_int_equal(lyd_diff_apply_all(&st->first, st->diff1), LY_SUCCESS);
Radek Krejci7931b192020-06-25 17:05:03 +0200868 lyd_print_mem(&st->xml1, st->first, LYD_XML, LYD_PRINT_WITHSIBLINGS);
869 lyd_print_mem(&st->xml2, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskob104f112020-07-17 09:54:54 +0200870 assert_string_equal(st->xml1, st->xml2);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200871
872 /* diff2 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200873 assert_int_equal(lyd_diff_siblings(st->second, st->third, LYD_DIFF_DEFAULTS, &st->diff2), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200874
875 assert_non_null(st->diff2);
876 free(st->xml);
877 lyd_print_mem(&st->xml, st->diff2, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL);
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\">"
880 "<foo yang:operation=\"replace\" yang:orig-value=\"41\">42</foo>"
881 "<dllist yang:operation=\"create\">1</dllist>"
882 "</df>"
883 );
884
Michal Vasko3a41dff2020-07-15 14:30:28 +0200885 assert_int_equal(lyd_diff_apply_all(&st->second, st->diff2), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200886 free(st->xml1);
887 lyd_print_mem(&st->xml1, st->second, LYD_XML, LYD_PRINT_WITHSIBLINGS);
888 free(st->xml2);
889 lyd_print_mem(&st->xml2, st->third, LYD_XML, LYD_PRINT_WITHSIBLINGS);
Michal Vaskob104f112020-07-17 09:54:54 +0200890 assert_string_equal(st->xml1, st->xml2);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200891
892 /* merge */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200893 assert_int_equal(lyd_diff_merge_all(st->diff2, &st->diff1), LY_SUCCESS);
Michal Vaskoe6323f62020-07-09 15:49:02 +0200894
895 free(st->xml);
896 lyd_print_mem(&st->xml, st->diff1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL);
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 Vaskoe893ddd2020-06-23 13:35:20 +0200906}
907
908int 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 Vaskoe893ddd2020-06-23 13:35:20 +0200921 cmocka_unit_test_setup_teardown(test_wd, setup_f, teardown_f),
922 };
923
924 return cmocka_run_group_tests(tests, NULL, NULL);
925}