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;" |
| 59 | "list l1 { key \"a b\"; leaf a {type string;} leaf b {type string;} leaf c {type string;}}" |
| 60 | "leaf foo { type string;}" |
| 61 | "leaf-list ll { type string;}" |
| 62 | "container c {leaf-list x {type string;}}" |
| 63 | "anydata any {config false;}" |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 64 | "list l2 {config false;" |
| 65 | "container c{leaf x {type string;} leaf-list d {type string;}}" |
| 66 | "}}"; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 67 | |
| 68 | #if ENABLE_LOGGER_CHECKING |
| 69 | ly_set_log_clb(logger, 1); |
| 70 | #endif |
| 71 | |
| 72 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 73 | 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] | 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int |
| 79 | teardown(void **state) |
| 80 | { |
| 81 | #if ENABLE_LOGGER_CHECKING |
| 82 | if (*state) { |
| 83 | fprintf(stderr, "%s\n", logbuf); |
| 84 | } |
| 85 | #else |
| 86 | (void) state; /* unused */ |
| 87 | #endif |
| 88 | |
| 89 | ly_ctx_destroy(ctx, NULL); |
| 90 | ctx = NULL; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | logbuf_clean(void) |
| 97 | { |
| 98 | logbuf[0] = '\0'; |
| 99 | } |
| 100 | |
| 101 | #if ENABLE_LOGGER_CHECKING |
| 102 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 103 | #else |
| 104 | # define logbuf_assert(str) |
| 105 | #endif |
| 106 | |
| 107 | static void |
| 108 | test_compare(void **state) |
| 109 | { |
| 110 | *state = test_compare; |
| 111 | |
| 112 | struct lyd_node *tree1, *tree2; |
| 113 | |
| 114 | const char *data1 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 115 | const char *data2 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>y</c></l1>"; |
| 116 | |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 117 | assert_int_equal(LY_SUCCESS, lyd_compare_single(NULL, NULL, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 118 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 119 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 120 | 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] | 121 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 122 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 123 | 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] | 124 | lyd_free_all(tree1); |
| 125 | lyd_free_all(tree2); |
| 126 | |
| 127 | data1 = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
| 128 | data2 = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 129 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 130 | 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] | 131 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0)); |
| 132 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 133 | lyd_free_all(tree1); |
| 134 | lyd_free_all(tree2); |
| 135 | |
| 136 | data1 = "<ll xmlns=\"urn:tests:a\">a</ll><ll xmlns=\"urn:tests:a\">b</ll>"; |
| 137 | data2 = "<ll xmlns=\"urn:tests:a\">b</ll>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 138 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 139 | 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] | 140 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0)); |
| 141 | assert_int_equal(LY_ENOT, lyd_compare_single(NULL, tree2, 0)); |
| 142 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, NULL, 0)); |
| 143 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 144 | lyd_free_all(tree1); |
| 145 | lyd_free_all(tree2); |
| 146 | |
| 147 | data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>"; |
| 148 | data2 = "<c xmlns=\"urn:tests:a\"><x>y</x></c>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 149 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 150 | 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] | 151 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 152 | 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] | 153 | lyd_free_all(tree1); |
| 154 | lyd_free_all(tree2); |
| 155 | |
| 156 | data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>"; |
| 157 | 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] | 158 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 159 | 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] | 160 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
| 161 | 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] | 162 | lyd_free_all(tree1); |
| 163 | lyd_free_all(tree2); |
| 164 | |
| 165 | data1 = "<any xmlns=\"urn:tests:a\"><x>x</x></any>"; |
| 166 | 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] | 167 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 168 | 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] | 169 | assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 170 | lyd_free_all(tree1); |
| 171 | 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] | 172 | 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] | 173 | assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, 0)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 174 | lyd_free_all(tree1); |
| 175 | lyd_free_all(tree2); |
| 176 | |
| 177 | *state = NULL; |
| 178 | } |
| 179 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 180 | static void |
| 181 | test_dup(void **state) |
| 182 | { |
| 183 | *state = test_dup; |
| 184 | |
| 185 | struct lyd_node *tree1, *tree2; |
| 186 | const char *result; |
| 187 | const char *data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 188 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 189 | 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] | 190 | 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] | 191 | 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] | 192 | lyd_free_all(tree1); |
| 193 | lyd_free_all(tree2); |
| 194 | |
| 195 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 196 | 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] | 197 | 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] | 198 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 199 | lyd_free_all(tree1); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 200 | 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] | 201 | 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] | 202 | lyd_free_all(tree1); |
| 203 | lyd_free_all(tree2); |
| 204 | |
| 205 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
| 206 | result = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 207 | 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] | 208 | 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] | 209 | 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] | 210 | lyd_free_all(tree2); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 211 | 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] | 212 | lyd_free_all(tree1); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 213 | 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] | 214 | 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] | 215 | lyd_free_all(tree2); |
| 216 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 217 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 218 | lyd_free_all(tree1); |
| 219 | result = "<l2 xmlns=\"urn:tests:a\"/>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 220 | 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] | 221 | 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] | 222 | lyd_free_all(tree1); |
| 223 | lyd_free_all(tree2); |
| 224 | |
| 225 | data = "<any xmlns=\"urn:tests:a\"><c><a>a</a></c></any>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 226 | 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] | 227 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 228 | 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] | 229 | lyd_free_all(tree1); |
| 230 | lyd_free_all(tree2); |
| 231 | |
| 232 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 233 | 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] | 234 | assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1)->child)->child, NULL, |
| 235 | LYD_DUP_WITH_PARENTS, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 236 | assert_string_equal("x", tree2->schema->name); |
| 237 | assert_non_null(tree2->parent); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 238 | 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] | 239 | lyd_free_all(tree1); |
| 240 | lyd_free_all(tree2); |
| 241 | |
| 242 | 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] | 243 | 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] | 244 | assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)tree1)->child->prev, NULL, |
| 245 | LYD_DUP_WITH_PARENTS, &tree2)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 246 | assert_string_equal("c", tree2->schema->name); |
| 247 | assert_non_null(tree2->parent); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 248 | 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] | 249 | lyd_free_all(tree1); |
| 250 | lyd_free_all(tree2); |
| 251 | |
| 252 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 253 | 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] | 254 | assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2)); |
| 255 | assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1)->child)->child, |
| 256 | (struct lyd_node_inner*)tree2, LYD_DUP_WITH_PARENTS, NULL)); |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 257 | 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] | 258 | lyd_free_all(tree1); |
| 259 | lyd_free_all(tree2); |
| 260 | |
| 261 | /* invalid */ |
| 262 | 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] | 263 | 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] | 264 | assert_int_equal(LY_EINVAL, lyd_dup_single(((struct lyd_node_inner*)tree1)->child->prev, |
| 265 | (struct lyd_node_inner*)tree1->next, LYD_DUP_WITH_PARENTS, NULL)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 266 | lyd_free_all(tree1); |
| 267 | |
| 268 | *state = NULL; |
| 269 | } |
| 270 | |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 271 | static void |
| 272 | test_target(void **state) |
| 273 | { |
| 274 | *state = test_target; |
| 275 | |
| 276 | const struct lyd_node_term *term; |
| 277 | struct lyd_node *tree; |
| 278 | struct lyxp_expr *exp; |
| 279 | struct ly_path *path; |
| 280 | const char *path_str = "/a:l2[2]/c/d[3]", *val; |
| 281 | int dynamic; |
| 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"); |
| 303 | val = lyd_value2str(term, &dynamic); |
| 304 | assert_int_equal(dynamic, 0); |
| 305 | assert_string_equal(val, "b"); |
| 306 | val = lyd_value2str((struct lyd_node_term *)term->prev, &dynamic); |
| 307 | assert_int_equal(dynamic, 0); |
| 308 | assert_string_equal(val, "b"); |
| 309 | |
| 310 | lyd_free_all(tree); |
| 311 | ly_path_free(ctx, path); |
| 312 | lyxp_expr_free(ctx, exp); |
| 313 | |
| 314 | *state = NULL; |
| 315 | } |
| 316 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 317 | int main(void) |
| 318 | { |
| 319 | const struct CMUnitTest tests[] = { |
| 320 | cmocka_unit_test_setup_teardown(test_compare, setup, teardown), |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 321 | cmocka_unit_test_setup_teardown(test_dup, setup, teardown), |
Michal Vasko | 38c2ba7 | 2020-07-27 14:46:59 +0200 | [diff] [blame] | 322 | cmocka_unit_test_setup_teardown(test_target, setup, teardown), |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 326 | } |