Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1 | /** |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 2 | * @file test_tree_schema.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from tress_data.c |
| 5 | * |
| 6 | * Copyright (c) 2018-2019 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 | |
| 15 | #include <stdarg.h> |
| 16 | #include <stddef.h> |
| 17 | #include <stdio.h> |
| 18 | #include <setjmp.h> |
| 19 | #include <cmocka.h> |
| 20 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 21 | #include "libyang.h" |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 22 | #include "common.h" |
| 23 | #include "path.h" |
| 24 | #include "xpath.h" |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 25 | |
| 26 | #define BUFSIZE 1024 |
| 27 | char logbuf[BUFSIZE] = {0}; |
| 28 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 29 | |
| 30 | struct ly_ctx *ctx; /* context for tests */ |
| 31 | |
| 32 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 33 | #define ENABLE_LOGGER_CHECKING 1 |
| 34 | |
| 35 | #if ENABLE_LOGGER_CHECKING |
| 36 | static void |
| 37 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 38 | { |
| 39 | (void) level; /* unused */ |
| 40 | if (store) { |
| 41 | if (path && path[0]) { |
| 42 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 43 | } else { |
| 44 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 45 | } |
| 46 | if (store > 0) { |
| 47 | --store; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | #endif |
| 52 | |
| 53 | static int |
| 54 | setup(void **state) |
| 55 | { |
| 56 | (void) state; /* unused */ |
| 57 | |
| 58 | const char *schema_a = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;" |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame^] | 59 | "leaf bar {type string;}" |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 60 | "list l1 { key \"a b\"; leaf a {type string;} leaf b {type string;} leaf c {type string;}}" |
| 61 | "leaf foo { type string;}" |
| 62 | "leaf-list ll { type string;}" |
| 63 | "container c {leaf-list x {type string;}}" |
| 64 | "anydata any {config false;}" |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 65 | "list l2 {config false;" |
| 66 | "container c{leaf x {type string;} leaf-list d {type string;}}" |
| 67 | "}}"; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 68 | |
| 69 | #if ENABLE_LOGGER_CHECKING |
| 70 | ly_set_log_clb(logger, 1); |
| 71 | #endif |
| 72 | |
| 73 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 74 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_a, LYS_IN_YANG, NULL)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int |
| 80 | teardown(void **state) |
| 81 | { |
| 82 | #if ENABLE_LOGGER_CHECKING |
| 83 | if (*state) { |
| 84 | fprintf(stderr, "%s\n", logbuf); |
| 85 | } |
| 86 | #else |
| 87 | (void) state; /* unused */ |
| 88 | #endif |
| 89 | |
| 90 | ly_ctx_destroy(ctx, NULL); |
| 91 | ctx = NULL; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | logbuf_clean(void) |
| 98 | { |
| 99 | logbuf[0] = '\0'; |
| 100 | } |
| 101 | |
| 102 | #if ENABLE_LOGGER_CHECKING |
| 103 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 104 | #else |
| 105 | # define logbuf_assert(str) |
| 106 | #endif |
| 107 | |
| 108 | static void |
| 109 | test_compare(void **state) |
| 110 | { |
| 111 | *state = test_compare; |
| 112 | |
| 113 | struct lyd_node *tree1, *tree2; |
| 114 | |
| 115 | const char *data1 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 116 | const char *data2 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>y</c></l1>"; |
| 117 | |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 118 | assert_int_equal(LY_SUCCESS, lyd_compare_single(NULL, NULL, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 119 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 120 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 121 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 122 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 123 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 124 | assert_int_equal(LY_ENOT, lyd_compare_single(((struct lyd_node_inner*)tree1)->child, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 125 | lyd_free_all(tree1); |
| 126 | lyd_free_all(tree2); |
| 127 | |
| 128 | data1 = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
| 129 | data2 = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 130 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 131 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 132 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0)); |
| 133 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 134 | lyd_free_all(tree1); |
| 135 | lyd_free_all(tree2); |
| 136 | |
| 137 | data1 = "<ll xmlns=\"urn:tests:a\">a</ll><ll xmlns=\"urn:tests:a\">b</ll>"; |
| 138 | data2 = "<ll xmlns=\"urn:tests:a\">b</ll>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 139 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 140 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 141 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0)); |
| 142 | assert_int_equal(LY_ENOT, lyd_compare_single(NULL, tree2, 0)); |
| 143 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, NULL, 0)); |
| 144 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 145 | lyd_free_all(tree1); |
| 146 | lyd_free_all(tree2); |
| 147 | |
| 148 | data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>"; |
| 149 | data2 = "<c xmlns=\"urn:tests:a\"><x>y</x></c>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 150 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 151 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 152 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 153 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 154 | lyd_free_all(tree1); |
| 155 | lyd_free_all(tree2); |
| 156 | |
| 157 | data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>"; |
| 158 | data2 = "<c xmlns=\"urn:tests:a\"><x>x</x><x>y</x></c>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 159 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 160 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 161 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 162 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 163 | lyd_free_all(tree1); |
| 164 | lyd_free_all(tree2); |
| 165 | |
| 166 | data1 = "<any xmlns=\"urn:tests:a\"><x>x</x></any>"; |
| 167 | data2 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 168 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 169 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 170 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 171 | lyd_free_all(tree1); |
| 172 | data1 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 173 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 174 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 175 | lyd_free_all(tree1); |
| 176 | lyd_free_all(tree2); |
| 177 | |
| 178 | *state = NULL; |
| 179 | } |
| 180 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 181 | static void |
| 182 | test_dup(void **state) |
| 183 | { |
| 184 | *state = test_dup; |
| 185 | |
| 186 | struct lyd_node *tree1, *tree2; |
| 187 | const char *result; |
| 188 | const char *data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 189 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 190 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 191 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, LYD_DUP_RECURSIVE, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 192 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 193 | lyd_free_all(tree1); |
| 194 | lyd_free_all(tree2); |
| 195 | |
| 196 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 197 | result = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b></l1>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 198 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 199 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 200 | lyd_free_all(tree1); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 201 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, result, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 202 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 203 | lyd_free_all(tree1); |
| 204 | lyd_free_all(tree2); |
| 205 | |
| 206 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
| 207 | result = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 208 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 209 | assert_int_equal(LY_SUCCESS, lyd_dup_siblings(tree1, NULL, LYD_DUP_RECURSIVE, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 210 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 211 | lyd_free_all(tree2); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 212 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, LYD_DUP_RECURSIVE, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 213 | lyd_free_all(tree1); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 214 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, result, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 215 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 216 | lyd_free_all(tree2); |
| 217 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 218 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 219 | lyd_free_all(tree1); |
| 220 | result = "<l2 xmlns=\"urn:tests:a\"/>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 221 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, result, LYD_XML, LYD_PARSE_ONLY, 0, &tree1)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 222 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 223 | lyd_free_all(tree1); |
| 224 | lyd_free_all(tree2); |
| 225 | |
| 226 | data = "<any xmlns=\"urn:tests:a\"><c><a>a</a></c></any>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 227 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 228 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 229 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 230 | lyd_free_all(tree1); |
| 231 | lyd_free_all(tree2); |
| 232 | |
| 233 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 234 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 235 | assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1)->child)->child, NULL, |
| 236 | LYD_DUP_WITH_PARENTS, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 237 | assert_string_equal("x", tree2->schema->name); |
| 238 | assert_non_null(tree2->parent); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 239 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, (struct lyd_node*)tree2->parent->parent, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 240 | lyd_free_all(tree1); |
| 241 | lyd_free_all(tree2); |
| 242 | |
| 243 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 244 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 245 | assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)tree1)->child->prev, NULL, |
| 246 | LYD_DUP_WITH_PARENTS, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 247 | assert_string_equal("c", tree2->schema->name); |
| 248 | assert_non_null(tree2->parent); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 249 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, (struct lyd_node*)tree2->parent, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 250 | lyd_free_all(tree1); |
| 251 | lyd_free_all(tree2); |
| 252 | |
| 253 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 254 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 255 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2)); |
| 256 | assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1)->child)->child, |
| 257 | (struct lyd_node_inner*)tree2, LYD_DUP_WITH_PARENTS, NULL)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 258 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 259 | lyd_free_all(tree1); |
| 260 | lyd_free_all(tree2); |
| 261 | |
| 262 | /* invalid */ |
| 263 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>c</c></l1><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 264 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 265 | assert_int_equal(LY_EINVAL, lyd_dup_single(((struct lyd_node_inner*)tree1)->child->prev, |
| 266 | (struct lyd_node_inner*)tree1->next, LYD_DUP_WITH_PARENTS, NULL)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 267 | lyd_free_all(tree1); |
| 268 | |
| 269 | *state = NULL; |
| 270 | } |
| 271 | |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 272 | static void |
| 273 | test_target(void **state) |
| 274 | { |
| 275 | *state = test_target; |
| 276 | |
| 277 | const struct lyd_node_term *term; |
| 278 | struct lyd_node *tree; |
| 279 | struct lyxp_expr *exp; |
| 280 | struct ly_path *path; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 281 | const char *path_str = "/a:l2[2]/c/d[3]"; |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 282 | const char *data = |
| 283 | "<l2 xmlns=\"urn:tests:a\"><c>" |
| 284 | "<d>a</d>" |
| 285 | "</c></l2>" |
| 286 | "<l2 xmlns=\"urn:tests:a\"><c>" |
| 287 | "<d>a</d>" |
| 288 | "<d>b</d>" |
| 289 | "<d>b</d>" |
| 290 | "<d>c</d>" |
| 291 | "</c></l2>" |
| 292 | "<l2 xmlns=\"urn:tests:a\"><c>" |
| 293 | "</c></l2>"; |
| 294 | |
| 295 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 296 | assert_int_equal(LY_SUCCESS, ly_path_parse(ctx, NULL, path_str, strlen(path_str), LY_PATH_BEGIN_EITHER, LY_PATH_LREF_FALSE, |
| 297 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp)); |
| 298 | assert_int_equal(LY_SUCCESS, ly_path_compile(ctx, NULL, NULL, exp, LY_PATH_LREF_FALSE, LY_PATH_OPER_INPUT, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 299 | LY_PATH_TARGET_SINGLE, LY_PREF_JSON, NULL, &path)); |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 300 | term = lyd_target(path, tree); |
| 301 | |
| 302 | assert_string_equal(term->schema->name, "d"); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 303 | assert_string_equal(LYD_CANON_VALUE(term), "b"); |
| 304 | assert_string_equal(LYD_CANON_VALUE(term->prev), "b"); |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 305 | |
| 306 | lyd_free_all(tree); |
| 307 | ly_path_free(ctx, path); |
| 308 | lyxp_expr_free(ctx, exp); |
| 309 | |
| 310 | *state = NULL; |
| 311 | } |
| 312 | |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame^] | 313 | static void |
| 314 | test_list_pos(void **state) |
| 315 | { |
| 316 | *state = test_list_pos; |
| 317 | |
| 318 | const char *data; |
| 319 | struct lyd_node *tree; |
| 320 | |
| 321 | data = "<bar xmlns=\"urn:tests:a\">test</bar>" |
| 322 | "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>" |
| 323 | "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>" |
| 324 | "<foo xmlns=\"urn:tests:a\">test</foo>"; |
| 325 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 326 | assert_int_equal(0, lyd_list_pos(tree)); |
| 327 | assert_int_equal(1, lyd_list_pos(tree->next)); |
| 328 | assert_int_equal(2, lyd_list_pos(tree->next->next)); |
| 329 | assert_int_equal(0, lyd_list_pos(tree->next->next->next)); |
| 330 | lyd_free_all(tree); |
| 331 | |
| 332 | data = "<ll xmlns=\"urn:tests:a\">one</ll>" |
| 333 | "<ll xmlns=\"urn:tests:a\">two</ll>" |
| 334 | "<ll xmlns=\"urn:tests:a\">three</ll>"; |
| 335 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 336 | assert_int_equal(1, lyd_list_pos(tree)); |
| 337 | assert_int_equal(2, lyd_list_pos(tree->next)); |
| 338 | assert_int_equal(3, lyd_list_pos(tree->next->next)); |
| 339 | lyd_free_all(tree); |
| 340 | |
| 341 | data = "<ll xmlns=\"urn:tests:a\">one</ll>" |
| 342 | "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b></l1>" |
| 343 | "<ll xmlns=\"urn:tests:a\">two</ll>" |
| 344 | "<l1 xmlns=\"urn:tests:a\"><a>two</a><b>two</b></l1>" |
| 345 | "<ll xmlns=\"urn:tests:a\">three</ll>" |
| 346 | "<l1 xmlns=\"urn:tests:a\"><a>three</a><b>three</b></l1>"; |
| 347 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 348 | assert_string_equal("l1", tree->schema->name); |
| 349 | assert_int_equal(1, lyd_list_pos(tree)); |
| 350 | assert_int_equal(2, lyd_list_pos(tree->next)); |
| 351 | assert_int_equal(3, lyd_list_pos(tree->next->next)); |
| 352 | assert_string_equal("ll", tree->next->next->next->schema->name); |
| 353 | assert_int_equal(1, lyd_list_pos(tree->next->next->next)); |
| 354 | assert_int_equal(2, lyd_list_pos(tree->next->next->next->next)); |
| 355 | assert_int_equal(3, lyd_list_pos(tree->next->next->next->next->next)); |
| 356 | lyd_free_all(tree); |
| 357 | |
| 358 | *state = NULL; |
| 359 | } |
| 360 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 361 | int main(void) |
| 362 | { |
| 363 | const struct CMUnitTest tests[] = { |
| 364 | cmocka_unit_test_setup_teardown(test_compare, setup, teardown), |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 365 | cmocka_unit_test_setup_teardown(test_dup, setup, teardown), |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 366 | cmocka_unit_test_setup_teardown(test_target, setup, teardown), |
Radek Krejci | ca98914 | 2020-11-05 11:32:22 +0100 | [diff] [blame^] | 367 | cmocka_unit_test_setup_teardown(test_list_pos, setup, teardown), |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 371 | } |