Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file test_diff.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Cmocka tests for lyd_diff(). |
| 5 | * |
| 6 | * Copyright (c) 2016 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Renato Westphal | 41a9106 | 2018-05-19 22:23:58 -0300 | [diff] [blame] | 15 | #include <stdarg.h> |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <setjmp.h> |
| 19 | #include <cmocka.h> |
| 20 | |
Jan Kundrát | fd7a5c7 | 2017-10-26 17:45:06 +0200 | [diff] [blame] | 21 | #include "tests/config.h" |
| 22 | #include "libyang.h" |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 23 | |
| 24 | struct state { |
| 25 | struct ly_ctx *ctx; |
| 26 | const struct lys_module *mod; |
| 27 | struct lyd_node *first; |
| 28 | struct lyd_node *second; |
| 29 | char *xml; |
| 30 | }; |
| 31 | |
| 32 | static int |
| 33 | setup_f(void **state) |
| 34 | { |
| 35 | struct state *st; |
| 36 | const char *schemafile = TESTS_DIR"/data/files/defaults.yin"; |
| 37 | |
| 38 | (*state) = st = calloc(1, sizeof *st); |
| 39 | if (!st) { |
| 40 | fprintf(stderr, "Memory allocation error"); |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | /* libyang context */ |
Radek Krejci | dd3263a | 2017-07-15 11:50:09 +0200 | [diff] [blame] | 45 | st->ctx = ly_ctx_new(NULL, 0); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 46 | if (!st->ctx) { |
| 47 | fprintf(stderr, "Failed to create context.\n"); |
| 48 | goto error; |
| 49 | } |
| 50 | |
| 51 | /* schemas */ |
| 52 | st->mod = lys_parse_path(st->ctx, schemafile, LYS_IN_YIN); |
| 53 | if (!st->mod) { |
| 54 | fprintf(stderr, "Failed to load data model \"%s\".\n", schemafile); |
| 55 | goto error; |
| 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | |
| 60 | error: |
| 61 | ly_ctx_destroy(st->ctx, NULL); |
| 62 | free(st); |
| 63 | (*state) = NULL; |
| 64 | |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | static int |
| 69 | teardown_f(void **state) |
| 70 | { |
| 71 | struct state *st = (*state); |
| 72 | |
| 73 | lyd_free_withsiblings(st->first); |
| 74 | lyd_free_withsiblings(st->second); |
| 75 | ly_ctx_destroy(st->ctx, NULL); |
| 76 | free(st->xml); |
| 77 | free(st); |
| 78 | (*state) = NULL; |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | test_same(void **state) |
| 85 | { |
| 86 | struct state *st = (*state); |
| 87 | const char *xml = "<nacm xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-acm\">" |
| 88 | "<enable-nacm>true</enable-nacm>" |
| 89 | "<read-default>permit</read-default>" |
| 90 | "<write-default>deny</write-default>" |
| 91 | "<exec-default>permit</exec-default>" |
| 92 | "<enable-external-groups>true</enable-external-groups>" |
| 93 | "</nacm><df xmlns=\"urn:libyang:tests:defaults\">" |
| 94 | "<foo>42</foo><b1_1>42</b1_1>" |
| 95 | "</df><hidden xmlns=\"urn:libyang:tests:defaults\">" |
| 96 | "<foo>42</foo><baz>42</baz></hidden>"; |
| 97 | struct lyd_difflist *diff; |
| 98 | |
| 99 | assert_ptr_not_equal((st->first = lyd_parse_mem(st->ctx, xml, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 100 | assert_ptr_not_equal((st->second = lyd_parse_mem(st->ctx, xml, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 101 | |
| 102 | assert_ptr_not_equal((diff = lyd_diff(st->first, st->second, 0)), NULL); |
| 103 | assert_ptr_not_equal(diff->type, NULL); |
| 104 | assert_int_equal(diff->type[0], LYD_DIFF_END); |
| 105 | |
| 106 | lyd_free_diff(diff); |
| 107 | } |
| 108 | |
| 109 | static void |
Radek Krejci | 4c3bc11 | 2016-05-19 15:09:03 +0200 | [diff] [blame] | 110 | test_empty1(void **state) |
| 111 | { |
| 112 | struct state *st = (*state); |
| 113 | const char *xml = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 114 | "<foo>42</foo><b1_1>42</b1_1>" |
| 115 | "</df><hidden xmlns=\"urn:libyang:tests:defaults\">" |
| 116 | "<foo>42</foo><baz>42</baz></hidden>"; |
| 117 | struct lyd_difflist *diff; |
| 118 | |
| 119 | assert_ptr_not_equal((st->first = lyd_parse_mem(st->ctx, xml, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 120 | |
| 121 | assert_ptr_not_equal((diff = lyd_diff(NULL, st->first, 0)), NULL); |
| 122 | assert_ptr_not_equal(diff->type, NULL); |
| 123 | |
| 124 | assert_int_equal(diff->type[0], LYD_DIFF_CREATED); |
| 125 | assert_ptr_equal(diff->first[0], NULL); |
| 126 | assert_ptr_equal(diff->second[0], st->first); |
| 127 | |
| 128 | assert_int_equal(diff->type[1], LYD_DIFF_CREATED); |
| 129 | assert_ptr_equal(diff->first[1], NULL); |
| 130 | assert_ptr_equal(diff->second[1], st->first->next); |
| 131 | |
| 132 | assert_int_equal(diff->type[2], LYD_DIFF_END); |
| 133 | lyd_free_diff(diff); |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | test_empty2(void **state) |
| 138 | { |
| 139 | struct state *st = (*state); |
| 140 | const char *xml = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 141 | "<foo>42</foo><b1_1>42</b1_1>" |
| 142 | "</df><hidden xmlns=\"urn:libyang:tests:defaults\">" |
| 143 | "<foo>42</foo><baz>42</baz></hidden>"; |
| 144 | struct lyd_difflist *diff; |
| 145 | |
| 146 | assert_ptr_not_equal((st->first = lyd_parse_mem(st->ctx, xml, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 147 | |
| 148 | assert_ptr_not_equal((diff = lyd_diff(st->first, NULL, 0)), NULL); |
| 149 | assert_ptr_not_equal(diff->type, NULL); |
| 150 | |
| 151 | assert_int_equal(diff->type[0], LYD_DIFF_DELETED); |
| 152 | assert_ptr_equal(diff->first[0], st->first); |
| 153 | assert_ptr_equal(diff->second[0], NULL); |
| 154 | |
| 155 | assert_int_equal(diff->type[1], LYD_DIFF_DELETED); |
| 156 | assert_ptr_equal(diff->first[1], st->first->next); |
| 157 | assert_ptr_equal(diff->second[1], NULL); |
| 158 | |
| 159 | assert_int_equal(diff->type[2], LYD_DIFF_END); |
| 160 | lyd_free_diff(diff); |
| 161 | } |
| 162 | |
| 163 | static void |
| 164 | test_empty3(void **state) |
| 165 | { |
| 166 | struct state *st = (*state); |
| 167 | const char *xml = "<df xmlns=\"urn:libyang:tests:defaults\"><foo>42</foo></df>"; |
| 168 | struct lyd_difflist *diff; |
| 169 | |
| 170 | assert_ptr_not_equal((st->first = lyd_parse_mem(st->ctx, xml, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 171 | |
| 172 | assert_ptr_not_equal((diff = lyd_diff(NULL, NULL, 0)), NULL); |
| 173 | assert_ptr_not_equal(diff->type, NULL); |
| 174 | assert_int_equal(diff->type[0], LYD_DIFF_END); |
| 175 | lyd_free_diff(diff); |
| 176 | |
| 177 | assert_ptr_equal((diff = lyd_diff(NULL, st->first->child, 0)), NULL); |
| 178 | assert_int_equal(ly_errno, LY_EINVAL); |
| 179 | |
| 180 | assert_ptr_not_equal((diff = lyd_diff(st->first->child, NULL, 0)), NULL); |
| 181 | assert_ptr_not_equal(diff->type, NULL); |
| 182 | |
| 183 | assert_int_equal(diff->type[0], LYD_DIFF_DELETED); |
| 184 | assert_ptr_equal(diff->first[0], st->first->child); |
| 185 | assert_ptr_equal(diff->second[0], NULL); |
| 186 | |
| 187 | assert_int_equal(diff->type[1], LYD_DIFF_END); |
| 188 | lyd_free_diff(diff); |
| 189 | } |
| 190 | |
| 191 | static void |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 192 | test_diff1(void **state) |
| 193 | { |
| 194 | struct state *st = (*state); |
| 195 | const char *xml1 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 196 | "<foo>42</foo>" |
| 197 | "</df><hidden xmlns=\"urn:libyang:tests:defaults\">" |
| 198 | "<foo>42</foo><baz>42</baz></hidden>"; |
| 199 | const char *xml2 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 200 | "<foo>41</foo><b1_1>42</b1_1>" |
| 201 | "</df>"; |
| 202 | char *str; |
| 203 | struct lyd_difflist *diff; |
| 204 | |
| 205 | assert_ptr_not_equal((st->first = lyd_parse_mem(st->ctx, xml1, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 206 | assert_ptr_not_equal((st->second = lyd_parse_mem(st->ctx, xml2, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 207 | |
| 208 | assert_ptr_not_equal((diff = lyd_diff(st->first, st->second, 0)), NULL); |
| 209 | assert_ptr_not_equal(diff->type, NULL); |
| 210 | |
| 211 | assert_int_equal(diff->type[0], LYD_DIFF_CHANGED); |
| 212 | assert_ptr_not_equal(diff->first[0], NULL); |
| 213 | assert_string_equal((str = lyd_path(diff->first[0])), "/defaults:df/foo"); |
| 214 | free(str); |
| 215 | assert_ptr_not_equal(diff->second[0], NULL); |
| 216 | assert_string_equal((str = lyd_path(diff->second[0])), "/defaults:df/foo"); |
| 217 | free(str); |
| 218 | |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 219 | assert_int_equal(diff->type[1], LYD_DIFF_DELETED); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 220 | assert_ptr_not_equal(diff->first[1], NULL); |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 221 | assert_string_equal((str = lyd_path(diff->first[1])), "/defaults:hidden"); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 222 | free(str); |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 223 | assert_ptr_equal(diff->second[1], NULL); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 224 | |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 225 | assert_int_equal(diff->type[2], LYD_DIFF_CREATED); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 226 | assert_ptr_not_equal(diff->first[2], NULL); |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 227 | assert_string_equal((str = lyd_path(diff->first[2])), "/defaults:df"); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 228 | free(str); |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 229 | assert_ptr_not_equal(diff->second[2], NULL); |
| 230 | assert_string_equal((str = lyd_path(diff->second[2])), "/defaults:df/b1_1"); |
| 231 | free(str); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 232 | |
| 233 | assert_int_equal(diff->type[3], LYD_DIFF_END); |
| 234 | |
| 235 | lyd_free_diff(diff); |
| 236 | } |
| 237 | |
| 238 | static void |
| 239 | test_diff2(void **state) |
| 240 | { |
| 241 | struct state *st = (*state); |
| 242 | const char *xml1 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 243 | "<list><name>a</name><value>1</value></list>" |
| 244 | "<list><name>b</name><value>2</value></list>" |
| 245 | "</df>"; |
| 246 | const char *xml2 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 247 | "<list><name>b</name><value>-2</value></list>" |
| 248 | "<list><name>c</name><value>3</value></list>" |
| 249 | "</df>"; |
| 250 | char *str; |
| 251 | struct lyd_difflist *diff; |
| 252 | |
| 253 | assert_ptr_not_equal((st->first = lyd_parse_mem(st->ctx, xml1, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 254 | assert_ptr_not_equal((st->second = lyd_parse_mem(st->ctx, xml2, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 255 | |
| 256 | assert_ptr_not_equal((diff = lyd_diff(st->first, st->second, 0)), NULL); |
| 257 | assert_ptr_not_equal(diff->type, NULL); |
| 258 | |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 259 | assert_int_equal(diff->type[0], LYD_DIFF_CHANGED); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 260 | assert_ptr_not_equal(diff->first[0], NULL); |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 261 | assert_string_equal((str = lyd_path(diff->first[0])), "/defaults:df/list[name=\'b\']/value"); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 262 | free(str); |
| 263 | assert_ptr_not_equal(diff->second[0], NULL); |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 264 | assert_string_equal((str = lyd_path(diff->second[0])), "/defaults:df/list[name=\'b\']/value"); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 265 | free(str); |
| 266 | |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 267 | assert_int_equal(diff->type[1], LYD_DIFF_DELETED); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 268 | assert_ptr_not_equal(diff->first[1], NULL); |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 269 | assert_string_equal((str = lyd_path(diff->first[1])), "/defaults:df/list[name=\'a\']"); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 270 | free(str); |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 271 | assert_ptr_equal(diff->second[1], NULL); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 272 | |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 273 | assert_int_equal(diff->type[2], LYD_DIFF_CREATED); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 274 | assert_ptr_not_equal(diff->first[2], NULL); |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 275 | assert_string_equal((str = lyd_path(diff->first[2])), "/defaults:df"); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 276 | free(str); |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 277 | assert_ptr_not_equal(diff->second[2], NULL); |
| 278 | assert_string_equal((str = lyd_path(diff->second[2])), "/defaults:df/list[name=\'c\']"); |
| 279 | free(str); |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 280 | |
| 281 | assert_int_equal(diff->type[3], LYD_DIFF_END); |
| 282 | |
| 283 | lyd_free_diff(diff); |
| 284 | } |
| 285 | |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 286 | static void |
| 287 | test_move1(void **state) |
| 288 | { |
| 289 | struct state *st = (*state); |
| 290 | const char *xml1 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 291 | "<llist>1</llist>" |
| 292 | "<llist>2</llist>" |
| 293 | "</df>"; |
| 294 | const char *xml2 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 295 | "<llist>2</llist>" |
| 296 | "<llist>1</llist>" |
| 297 | "</df>"; |
| 298 | char *str; |
| 299 | struct lyd_difflist *diff; |
| 300 | |
| 301 | assert_ptr_not_equal((st->first = lyd_parse_mem(st->ctx, xml1, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 302 | assert_ptr_not_equal((st->second = lyd_parse_mem(st->ctx, xml2, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 303 | |
| 304 | assert_ptr_not_equal((diff = lyd_diff(st->first, st->second, 0)), NULL); |
| 305 | assert_ptr_not_equal(diff->type, NULL); |
| 306 | |
| 307 | assert_int_equal(diff->type[0], LYD_DIFF_MOVEDAFTER1); |
| 308 | assert_ptr_not_equal(diff->first[0], NULL); |
| 309 | assert_string_equal((str = lyd_path(diff->first[0])), "/defaults:df/llist[.='1']"); |
| 310 | free(str); |
| 311 | assert_ptr_not_equal(diff->second[0], NULL); |
| 312 | assert_string_equal((str = lyd_path(diff->second[0])), "/defaults:df/llist[.='2']"); |
| 313 | free(str); |
| 314 | |
| 315 | assert_int_equal(diff->type[1], LYD_DIFF_END); |
| 316 | |
| 317 | lyd_free_diff(diff); |
| 318 | } |
| 319 | |
| 320 | static void |
| 321 | test_move2(void **state) |
| 322 | { |
| 323 | struct state *st = (*state); |
| 324 | const char *xml1 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 325 | "<llist>1</llist>" |
| 326 | "<llist>2</llist>" |
| 327 | "<llist>3</llist>" |
| 328 | "<llist>4</llist>" |
| 329 | "<llist>5</llist>" |
| 330 | "</df>"; |
| 331 | const char *xml2 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 332 | "<llist>1</llist>" |
| 333 | "<llist>4</llist>" |
| 334 | "<llist>3</llist>" |
| 335 | "<llist>2</llist>" |
| 336 | "<llist>5</llist>" |
| 337 | "</df>"; |
| 338 | char *str; |
| 339 | struct lyd_difflist *diff; |
| 340 | |
| 341 | assert_ptr_not_equal((st->first = lyd_parse_mem(st->ctx, xml1, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 342 | assert_ptr_not_equal((st->second = lyd_parse_mem(st->ctx, xml2, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 343 | |
| 344 | assert_ptr_not_equal((diff = lyd_diff(st->first, st->second, 0)), NULL); |
| 345 | assert_ptr_not_equal(diff->type, NULL); |
| 346 | |
| 347 | assert_int_equal(diff->type[0], LYD_DIFF_MOVEDAFTER1); |
| 348 | assert_ptr_not_equal(diff->first[0], NULL); |
| 349 | assert_string_equal((str = lyd_path(diff->first[0])), "/defaults:df/llist[.='2']"); |
| 350 | free(str); |
| 351 | assert_ptr_not_equal(diff->second[0], NULL); |
| 352 | assert_string_equal((str = lyd_path(diff->second[0])), "/defaults:df/llist[.='4']"); |
| 353 | free(str); |
| 354 | |
| 355 | assert_int_equal(diff->type[1], LYD_DIFF_MOVEDAFTER1); |
| 356 | assert_ptr_not_equal(diff->first[1], NULL); |
| 357 | assert_string_equal((str = lyd_path(diff->first[1])), "/defaults:df/llist[.='4']"); |
| 358 | free(str); |
| 359 | assert_ptr_not_equal(diff->second[1], NULL); |
| 360 | assert_string_equal((str = lyd_path(diff->second[1])), "/defaults:df/llist[.='1']"); |
| 361 | free(str); |
| 362 | |
| 363 | assert_int_equal(diff->type[2], LYD_DIFF_END); |
| 364 | |
| 365 | lyd_free_diff(diff); |
| 366 | } |
| 367 | |
| 368 | static void |
Radek Krejci | 450b94a | 2017-01-17 11:01:27 +0100 | [diff] [blame] | 369 | test_move3(void **state) |
| 370 | { |
| 371 | struct state *st = (*state); |
| 372 | const char *xml1 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 373 | "<llist>1</llist>" |
| 374 | "<list><name>a</name><value>1</value></list>" |
| 375 | "<llist>2</llist>" |
| 376 | "<llist>3</llist>" |
| 377 | "<llist>4</llist>" |
| 378 | "</df>"; |
| 379 | const char *xml2 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 380 | "<llist>1</llist>" |
| 381 | "<list><name>a</name><value>1</value></list>" |
| 382 | "<llist>2</llist>" |
| 383 | "<llist>4</llist>" |
| 384 | "<llist>3</llist>" |
| 385 | "</df>"; |
| 386 | char *str; |
| 387 | struct lyd_difflist *diff; |
| 388 | |
| 389 | assert_ptr_not_equal((st->first = lyd_parse_mem(st->ctx, xml1, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 390 | assert_ptr_not_equal((st->second = lyd_parse_mem(st->ctx, xml2, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 391 | |
| 392 | assert_ptr_not_equal((diff = lyd_diff(st->first, st->second, 0)), NULL); |
| 393 | assert_ptr_not_equal(diff->type, NULL); |
| 394 | |
| 395 | assert_int_equal(diff->type[0], LYD_DIFF_MOVEDAFTER1); |
| 396 | assert_ptr_not_equal(diff->first[0], NULL); |
| 397 | assert_string_equal((str = lyd_path(diff->first[0])), "/defaults:df/llist[.='3']"); |
| 398 | free(str); |
| 399 | assert_ptr_not_equal(diff->second[0], NULL); |
| 400 | assert_string_equal((str = lyd_path(diff->second[0])), "/defaults:df/llist[.='4']"); |
| 401 | free(str); |
| 402 | |
| 403 | assert_int_equal(diff->type[1], LYD_DIFF_END); |
| 404 | |
| 405 | lyd_free_diff(diff); |
| 406 | } |
| 407 | |
| 408 | static void |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 409 | test_mix1(void **state) |
| 410 | { |
| 411 | struct state *st = (*state); |
| 412 | const char *xml1 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 413 | "<llist>1</llist>" |
| 414 | "<llist>2</llist>" |
| 415 | "<llist>3</llist>" |
| 416 | "</df>"; |
| 417 | const char *xml2 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 418 | "<llist>3</llist>" |
| 419 | "<llist>1</llist>" |
| 420 | "</df>"; |
| 421 | char *str; |
| 422 | struct lyd_difflist *diff; |
| 423 | |
| 424 | assert_ptr_not_equal((st->first = lyd_parse_mem(st->ctx, xml1, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 425 | assert_ptr_not_equal((st->second = lyd_parse_mem(st->ctx, xml2, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 426 | |
| 427 | assert_ptr_not_equal((diff = lyd_diff(st->first, st->second, 0)), NULL); |
| 428 | assert_ptr_not_equal(diff->type, NULL); |
| 429 | |
| 430 | assert_int_equal(diff->type[0], LYD_DIFF_DELETED); |
| 431 | assert_ptr_not_equal(diff->first[0], NULL); |
| 432 | assert_string_equal((str = lyd_path(diff->first[0])), "/defaults:df/llist[.='2']"); |
| 433 | free(str); |
| 434 | assert_ptr_equal(diff->second[0], NULL); |
| 435 | |
| 436 | assert_int_equal(diff->type[1], LYD_DIFF_MOVEDAFTER1); |
| 437 | assert_ptr_not_equal(diff->first[1], NULL); |
| 438 | assert_string_equal((str = lyd_path(diff->first[1])), "/defaults:df/llist[.='1']"); |
| 439 | free(str); |
| 440 | assert_ptr_not_equal(diff->second[1], NULL); |
| 441 | assert_string_equal((str = lyd_path(diff->second[1])), "/defaults:df/llist[.='3']"); |
| 442 | free(str); |
| 443 | |
| 444 | assert_int_equal(diff->type[2], LYD_DIFF_END); |
| 445 | |
| 446 | lyd_free_diff(diff); |
| 447 | } |
| 448 | |
| 449 | static void |
| 450 | test_mix2(void **state) |
| 451 | { |
| 452 | struct state *st = (*state); |
| 453 | const char *xml1 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 454 | "<llist>1</llist>" |
| 455 | "<llist>2</llist>" |
| 456 | "<llist>3</llist>" |
| 457 | "</df>"; |
| 458 | const char *xml2 = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 459 | "<llist>4</llist>" |
| 460 | "<llist>3</llist>" |
| 461 | "<llist>1</llist>" |
| 462 | "</df>"; |
| 463 | char *str; |
| 464 | struct lyd_difflist *diff; |
| 465 | |
| 466 | assert_ptr_not_equal((st->first = lyd_parse_mem(st->ctx, xml1, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 467 | assert_ptr_not_equal((st->second = lyd_parse_mem(st->ctx, xml2, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 468 | |
| 469 | assert_ptr_not_equal((diff = lyd_diff(st->first, st->second, 0)), NULL); |
| 470 | assert_ptr_not_equal(diff->type, NULL); |
| 471 | |
| 472 | assert_int_equal(diff->type[0], LYD_DIFF_DELETED); |
| 473 | assert_ptr_not_equal(diff->first[0], NULL); |
| 474 | assert_string_equal((str = lyd_path(diff->first[0])), "/defaults:df/llist[.='2']"); |
| 475 | free(str); |
| 476 | assert_ptr_equal(diff->second[0], NULL); |
| 477 | |
| 478 | assert_int_equal(diff->type[1], LYD_DIFF_MOVEDAFTER1); |
| 479 | assert_ptr_not_equal(diff->first[1], NULL); |
| 480 | assert_string_equal((str = lyd_path(diff->first[1])), "/defaults:df/llist[.='1']"); |
| 481 | free(str); |
| 482 | assert_ptr_not_equal(diff->second[1], NULL); |
| 483 | assert_string_equal((str = lyd_path(diff->second[1])), "/defaults:df/llist[.='3']"); |
| 484 | free(str); |
| 485 | |
| 486 | assert_int_equal(diff->type[2], LYD_DIFF_CREATED); |
| 487 | assert_ptr_not_equal(diff->first[2], NULL); |
| 488 | assert_string_equal((str = lyd_path(diff->first[2])), "/defaults:df"); |
| 489 | free(str); |
| 490 | assert_ptr_not_equal(diff->second[2], NULL); |
| 491 | assert_string_equal((str = lyd_path(diff->second[2])), "/defaults:df/llist[.='4']"); |
| 492 | free(str); |
| 493 | |
| 494 | assert_int_equal(diff->type[3], LYD_DIFF_MOVEDAFTER2); |
| 495 | assert_ptr_equal(diff->first[3], NULL); |
| 496 | assert_ptr_not_equal(diff->second[3], NULL); |
| 497 | assert_string_equal((str = lyd_path(diff->second[3])), "/defaults:df/llist[.='4']"); |
| 498 | free(str); |
| 499 | |
| 500 | assert_int_equal(diff->type[4], LYD_DIFF_END); |
| 501 | |
| 502 | lyd_free_diff(diff); |
| 503 | } |
| 504 | |
Radek Krejci | 99d737f | 2016-09-06 11:19:52 +0200 | [diff] [blame] | 505 | static void |
| 506 | test_wd1(void **state) |
| 507 | { |
| 508 | struct state *st = (*state); |
| 509 | const char *xml = "<df xmlns=\"urn:libyang:tests:defaults\">" |
| 510 | "<foo>41</foo><dllist>4</dllist>" |
| 511 | "</df>"; |
| 512 | char *str; |
| 513 | struct lyd_difflist *diff; |
| 514 | |
| 515 | st->first = NULL; |
| 516 | lyd_validate(&st->first, LYD_OPT_CONFIG, st->ctx); |
| 517 | assert_ptr_not_equal(st->first, NULL); |
| 518 | assert_ptr_not_equal((st->second = lyd_parse_mem(st->ctx, xml, LYD_XML, LYD_OPT_CONFIG)), NULL); |
| 519 | |
| 520 | assert_ptr_not_equal((diff = lyd_diff(st->first, st->second, LYD_DIFFOPT_WITHDEFAULTS)), NULL); |
| 521 | assert_ptr_not_equal(diff->type, NULL); |
| 522 | |
| 523 | assert_int_equal(diff->type[0], LYD_DIFF_CHANGED); |
| 524 | assert_ptr_not_equal(diff->first[0], NULL); |
| 525 | assert_string_equal((str = lyd_path(diff->first[0])), "/defaults:df/foo"); |
| 526 | assert_int_equal(((struct lyd_node_leaf_list*)diff->first[0])->value.int32, 42); |
| 527 | free(str); |
| 528 | assert_ptr_not_equal(diff->second[0], NULL); |
| 529 | assert_string_equal((str = lyd_path(diff->second[0])), "/defaults:df/foo"); |
| 530 | assert_int_equal(((struct lyd_node_leaf_list*)diff->second[0])->value.int32, 41); |
| 531 | free(str); |
| 532 | |
| 533 | assert_int_equal(diff->type[1], LYD_DIFF_DELETED); |
| 534 | assert_ptr_not_equal(diff->first[1], NULL); |
| 535 | assert_string_equal((str = lyd_path(diff->first[1])), "/defaults:df/dllist[.='1']"); |
| 536 | free(str); |
| 537 | assert_ptr_equal(diff->second[1], NULL); |
| 538 | |
| 539 | assert_int_equal(diff->type[2], LYD_DIFF_DELETED); |
| 540 | assert_ptr_not_equal(diff->first[2], NULL); |
| 541 | assert_string_equal((str = lyd_path(diff->first[2])), "/defaults:df/dllist[.='2']"); |
| 542 | free(str); |
| 543 | assert_ptr_equal(diff->second[2], NULL); |
| 544 | |
| 545 | assert_int_equal(diff->type[3], LYD_DIFF_DELETED); |
| 546 | assert_ptr_not_equal(diff->first[3], NULL); |
| 547 | assert_string_equal((str = lyd_path(diff->first[3])), "/defaults:df/dllist[.='3']"); |
| 548 | free(str); |
| 549 | assert_ptr_equal(diff->second[3], NULL); |
| 550 | |
| 551 | assert_int_equal(diff->type[4], LYD_DIFF_CREATED); |
| 552 | assert_ptr_not_equal(diff->first[4], NULL); |
| 553 | assert_string_equal((str = lyd_path(diff->first[4])), "/defaults:df"); |
| 554 | free(str); |
| 555 | assert_ptr_not_equal(diff->second[4], NULL); |
| 556 | assert_string_equal((str = lyd_path(diff->second[4])), "/defaults:df/dllist[.='4']"); |
| 557 | free(str); |
| 558 | |
| 559 | assert_int_equal(diff->type[5], LYD_DIFF_END); |
| 560 | |
| 561 | lyd_free_diff(diff); |
| 562 | } |
| 563 | |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 564 | int main(void) |
| 565 | { |
| 566 | const struct CMUnitTest tests[] = { |
| 567 | cmocka_unit_test_setup_teardown(test_same, setup_f, teardown_f), |
Radek Krejci | 4c3bc11 | 2016-05-19 15:09:03 +0200 | [diff] [blame] | 568 | cmocka_unit_test_setup_teardown(test_empty1, setup_f, teardown_f), |
| 569 | cmocka_unit_test_setup_teardown(test_empty2, setup_f, teardown_f), |
| 570 | cmocka_unit_test_setup_teardown(test_empty3, setup_f, teardown_f), |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 571 | cmocka_unit_test_setup_teardown(test_diff1, setup_f, teardown_f), |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 572 | cmocka_unit_test_setup_teardown(test_diff2, setup_f, teardown_f), |
| 573 | cmocka_unit_test_setup_teardown(test_move1, setup_f, teardown_f), |
| 574 | cmocka_unit_test_setup_teardown(test_move2, setup_f, teardown_f), |
Radek Krejci | 450b94a | 2017-01-17 11:01:27 +0100 | [diff] [blame] | 575 | cmocka_unit_test_setup_teardown(test_move3, setup_f, teardown_f), |
Radek Krejci | 5e7c63f | 2016-05-17 16:24:34 +0200 | [diff] [blame] | 576 | cmocka_unit_test_setup_teardown(test_mix1, setup_f, teardown_f), |
Radek Krejci | 99d737f | 2016-09-06 11:19:52 +0200 | [diff] [blame] | 577 | cmocka_unit_test_setup_teardown(test_mix2, setup_f, teardown_f), |
| 578 | cmocka_unit_test_setup_teardown(test_wd1, setup_f, teardown_f), }; |
Radek Krejci | 0d531f9 | 2016-05-05 15:57:49 +0200 | [diff] [blame] | 579 | |
| 580 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 581 | } |