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" |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 22 | |
| 23 | #define BUFSIZE 1024 |
| 24 | char logbuf[BUFSIZE] = {0}; |
| 25 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 26 | |
| 27 | struct ly_ctx *ctx; /* context for tests */ |
| 28 | |
| 29 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 30 | #define ENABLE_LOGGER_CHECKING 1 |
| 31 | |
| 32 | #if ENABLE_LOGGER_CHECKING |
| 33 | static void |
| 34 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 35 | { |
| 36 | (void) level; /* unused */ |
| 37 | if (store) { |
| 38 | if (path && path[0]) { |
| 39 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 40 | } else { |
| 41 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 42 | } |
| 43 | if (store > 0) { |
| 44 | --store; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | static int |
| 51 | setup(void **state) |
| 52 | { |
| 53 | (void) state; /* unused */ |
| 54 | |
| 55 | const char *schema_a = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;" |
| 56 | "list l1 { key \"a b\"; leaf a {type string;} leaf b {type string;} leaf c {type string;}}" |
| 57 | "leaf foo { type string;}" |
| 58 | "leaf-list ll { type string;}" |
| 59 | "container c {leaf-list x {type string;}}" |
| 60 | "anydata any {config false;}" |
| 61 | "list l2 {config false; container c{leaf x {type string;}}}}"; |
| 62 | |
| 63 | #if ENABLE_LOGGER_CHECKING |
| 64 | ly_set_log_clb(logger, 1); |
| 65 | #endif |
| 66 | |
| 67 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 68 | assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG)); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int |
| 74 | teardown(void **state) |
| 75 | { |
| 76 | #if ENABLE_LOGGER_CHECKING |
| 77 | if (*state) { |
| 78 | fprintf(stderr, "%s\n", logbuf); |
| 79 | } |
| 80 | #else |
| 81 | (void) state; /* unused */ |
| 82 | #endif |
| 83 | |
| 84 | ly_ctx_destroy(ctx, NULL); |
| 85 | ctx = NULL; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | logbuf_clean(void) |
| 92 | { |
| 93 | logbuf[0] = '\0'; |
| 94 | } |
| 95 | |
| 96 | #if ENABLE_LOGGER_CHECKING |
| 97 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 98 | #else |
| 99 | # define logbuf_assert(str) |
| 100 | #endif |
| 101 | |
| 102 | static void |
| 103 | test_compare(void **state) |
| 104 | { |
| 105 | *state = test_compare; |
| 106 | |
| 107 | struct lyd_node *tree1, *tree2; |
| 108 | |
| 109 | const char *data1 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 110 | const char *data2 = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>y</c></l1>"; |
| 111 | |
| 112 | assert_int_equal(LY_SUCCESS, lyd_compare(NULL, NULL, 0)); |
| 113 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 114 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 115 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 116 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, 0)); |
| 117 | assert_int_equal(LY_ENOT, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 118 | assert_int_equal(LY_ENOT, lyd_compare(((struct lyd_node_inner*)tree1)->child, tree2, 0)); |
| 119 | lyd_free_all(tree1); |
| 120 | lyd_free_all(tree2); |
| 121 | |
| 122 | data1 = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
| 123 | data2 = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 124 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 125 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 126 | assert_int_equal(LY_ENOT, lyd_compare(tree1, tree2, 0)); |
| 127 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1->next, tree2, 0)); |
| 128 | lyd_free_all(tree1); |
| 129 | lyd_free_all(tree2); |
| 130 | |
| 131 | data1 = "<ll xmlns=\"urn:tests:a\">a</ll><ll xmlns=\"urn:tests:a\">b</ll>"; |
| 132 | data2 = "<ll xmlns=\"urn:tests:a\">b</ll>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 133 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 134 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 135 | assert_int_equal(LY_ENOT, lyd_compare(tree1, tree2, 0)); |
| 136 | assert_int_equal(LY_ENOT, lyd_compare(NULL, tree2, 0)); |
| 137 | assert_int_equal(LY_ENOT, lyd_compare(tree1, NULL, 0)); |
| 138 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1->next, tree2, 0)); |
| 139 | lyd_free_all(tree1); |
| 140 | lyd_free_all(tree2); |
| 141 | |
| 142 | data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>"; |
| 143 | data2 = "<c xmlns=\"urn:tests:a\"><x>y</x></c>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 144 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 145 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 146 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, 0)); |
| 147 | assert_int_equal(LY_ENOT, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 148 | lyd_free_all(tree1); |
| 149 | lyd_free_all(tree2); |
| 150 | |
| 151 | data1 = "<c xmlns=\"urn:tests:a\"><x>x</x></c>"; |
| 152 | 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] | 153 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 154 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 155 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, 0)); |
| 156 | assert_int_equal(LY_ENOT, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 157 | lyd_free_all(tree1); |
| 158 | lyd_free_all(tree2); |
| 159 | |
| 160 | data1 = "<any xmlns=\"urn:tests:a\"><x>x</x></any>"; |
| 161 | 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] | 162 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
| 163 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 164 | assert_int_equal(LY_ENOT, lyd_compare(tree1, tree2, 0)); |
| 165 | lyd_free_all(tree1); |
| 166 | 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] | 167 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 168 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, 0)); |
| 169 | lyd_free_all(tree1); |
| 170 | lyd_free_all(tree2); |
| 171 | |
| 172 | *state = NULL; |
| 173 | } |
| 174 | |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 175 | static void |
| 176 | test_dup(void **state) |
| 177 | { |
| 178 | *state = test_dup; |
| 179 | |
| 180 | struct lyd_node *tree1, *tree2; |
| 181 | const char *result; |
| 182 | const char *data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 183 | |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 184 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 185 | assert_non_null(tree2 = lyd_dup(tree1, NULL, LYD_DUP_RECURSIVE)); |
| 186 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 187 | lyd_free_all(tree1); |
| 188 | lyd_free_all(tree2); |
| 189 | |
| 190 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a><b>b</b><c>x</c></l1>"; |
| 191 | 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] | 192 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 193 | assert_non_null(tree2 = lyd_dup(tree1, NULL, 0)); |
| 194 | lyd_free_all(tree1); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 195 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, result, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 196 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 197 | lyd_free_all(tree1); |
| 198 | lyd_free_all(tree2); |
| 199 | |
| 200 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2><l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
| 201 | result = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 202 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 203 | assert_non_null(tree2 = lyd_dup(tree1, NULL, LYD_DUP_WITH_SIBLINGS | LYD_DUP_RECURSIVE)); |
| 204 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 205 | lyd_free_all(tree2); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 206 | assert_non_null(tree2 = lyd_dup(tree1, NULL, LYD_DUP_RECURSIVE)); |
| 207 | lyd_free_all(tree1); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 208 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, result, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 209 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 210 | lyd_free_all(tree2); |
| 211 | |
| 212 | assert_non_null(tree2 = lyd_dup(tree1, NULL, 0)); |
| 213 | lyd_free_all(tree1); |
| 214 | result = "<l2 xmlns=\"urn:tests:a\"/>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 215 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, result, LYD_XML, LYD_PARSE_ONLY, 0, &tree1)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 216 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 217 | lyd_free_all(tree1); |
| 218 | lyd_free_all(tree2); |
| 219 | |
| 220 | data = "<any xmlns=\"urn:tests:a\"><c><a>a</a></c></any>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 221 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 222 | assert_non_null(tree2 = lyd_dup(tree1, NULL, 0)); |
| 223 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 224 | lyd_free_all(tree1); |
| 225 | lyd_free_all(tree2); |
| 226 | |
| 227 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 228 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 229 | assert_non_null(tree2 = lyd_dup(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1)->child)->child, NULL, LYD_DUP_WITH_PARENTS)); |
| 230 | assert_string_equal("x", tree2->schema->name); |
| 231 | assert_non_null(tree2->parent); |
| 232 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, (struct lyd_node*)tree2->parent->parent, LYD_COMPARE_FULL_RECURSION)); |
| 233 | lyd_free_all(tree1); |
| 234 | lyd_free_all(tree2); |
| 235 | |
| 236 | 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] | 237 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 238 | assert_non_null(tree2 = lyd_dup(((struct lyd_node_inner*)tree1)->child->prev, NULL, LYD_DUP_WITH_PARENTS)); |
| 239 | assert_string_equal("c", tree2->schema->name); |
| 240 | assert_non_null(tree2->parent); |
| 241 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, (struct lyd_node*)tree2->parent, LYD_COMPARE_FULL_RECURSION)); |
| 242 | lyd_free_all(tree1); |
| 243 | lyd_free_all(tree2); |
| 244 | |
| 245 | data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>"; |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 246 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 247 | assert_non_null(tree2 = lyd_dup(tree1, NULL, 0)); |
| 248 | assert_non_null(lyd_dup(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1)->child)->child, |
| 249 | (struct lyd_node_inner*)tree2, LYD_DUP_WITH_PARENTS)); |
| 250 | assert_int_equal(LY_SUCCESS, lyd_compare(tree1, tree2, LYD_COMPARE_FULL_RECURSION)); |
| 251 | lyd_free_all(tree1); |
| 252 | lyd_free_all(tree2); |
| 253 | |
| 254 | /* invalid */ |
| 255 | 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] | 256 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1)); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 257 | assert_null(lyd_dup(((struct lyd_node_inner*)tree1)->child->prev, (struct lyd_node_inner*)tree1->next, LYD_DUP_WITH_PARENTS)); |
| 258 | lyd_free_all(tree1); |
| 259 | |
| 260 | *state = NULL; |
| 261 | } |
| 262 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 263 | int main(void) |
| 264 | { |
| 265 | const struct CMUnitTest tests[] = { |
| 266 | cmocka_unit_test_setup_teardown(test_compare, setup, teardown), |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 267 | cmocka_unit_test_setup_teardown(test_dup, setup, teardown), |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 271 | } |