Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file test_parser_xml.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from parser_xml.c |
| 5 | * |
| 6 | * Copyright (c) 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 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 15 | #include "tests/config.h" |
| 16 | |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 17 | #include <stdarg.h> |
| 18 | #include <stddef.h> |
| 19 | #include <setjmp.h> |
| 20 | #include <cmocka.h> |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 25 | #include "../../src/context.h" |
| 26 | #include "../../src/tree_data_internal.h" |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 27 | #include "../../src/printer_data.h" |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 28 | |
| 29 | #define BUFSIZE 1024 |
| 30 | char logbuf[BUFSIZE] = {0}; |
| 31 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 32 | |
| 33 | struct ly_ctx *ctx; /* context for tests */ |
| 34 | |
| 35 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 36 | #define ENABLE_LOGGER_CHECKING 1 |
| 37 | |
| 38 | #if ENABLE_LOGGER_CHECKING |
| 39 | static void |
| 40 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 41 | { |
| 42 | (void) level; /* unused */ |
| 43 | if (store) { |
| 44 | if (path && path[0]) { |
| 45 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 46 | } else { |
| 47 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 48 | } |
| 49 | if (store > 0) { |
| 50 | --store; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | static int |
| 57 | setup(void **state) |
| 58 | { |
| 59 | (void) state; /* unused */ |
| 60 | |
| 61 | const char *schema_a = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;" |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 62 | "list l1 { key \"a b c\"; leaf a {type string;} leaf b {type string;} leaf c {type int16;} leaf d {type string;}}" |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 63 | "leaf foo { type string;}" |
Radek Krejci | b6f7ae5 | 2019-07-19 10:31:42 +0200 | [diff] [blame] | 64 | "container c { leaf x {type string;}}" |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 65 | "container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}" |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 66 | "anydata any {config false;}" |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 67 | "leaf foo2 { type string; default \"default-val\"; }" |
| 68 | "leaf foo3 { type uint32; }}"; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 69 | const struct lys_module *mod; |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 70 | |
| 71 | #if ENABLE_LOGGER_CHECKING |
| 72 | ly_set_log_clb(logger, 1); |
| 73 | #endif |
| 74 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 75 | assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx)); |
| 76 | assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01")); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 77 | assert_non_null((mod = ly_ctx_load_module(ctx, "ietf-netconf", "2011-06-01"))); |
| 78 | assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "writable-running")); |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 79 | assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG)); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int |
| 85 | teardown(void **state) |
| 86 | { |
| 87 | #if ENABLE_LOGGER_CHECKING |
| 88 | if (*state) { |
| 89 | fprintf(stderr, "%s\n", logbuf); |
| 90 | } |
| 91 | #else |
| 92 | (void) state; /* unused */ |
| 93 | #endif |
| 94 | |
| 95 | ly_ctx_destroy(ctx, NULL); |
| 96 | ctx = NULL; |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | logbuf_clean(void) |
| 103 | { |
| 104 | logbuf[0] = '\0'; |
| 105 | } |
| 106 | |
| 107 | #if ENABLE_LOGGER_CHECKING |
| 108 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 109 | #else |
| 110 | # define logbuf_assert(str) |
| 111 | #endif |
| 112 | |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 113 | static void |
| 114 | test_leaf(void **state) |
| 115 | { |
| 116 | *state = test_leaf; |
| 117 | |
| 118 | const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>"; |
| 119 | struct lyd_node *tree; |
| 120 | struct lyd_node_term *leaf; |
| 121 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 122 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 123 | assert_non_null(tree); |
| 124 | assert_int_equal(LYS_LEAF, tree->schema->nodetype); |
| 125 | assert_string_equal("foo", tree->schema->name); |
| 126 | leaf = (struct lyd_node_term*)tree; |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 127 | assert_string_equal("foo value", leaf->value.original); |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 128 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 129 | assert_int_equal(LYS_LEAF, tree->next->next->schema->nodetype); |
| 130 | assert_string_equal("foo2", tree->next->next->schema->name); |
| 131 | leaf = (struct lyd_node_term*)tree->next->next; |
| 132 | assert_string_equal("default-val", leaf->value.original); |
| 133 | assert_true(leaf->flags & LYD_DEFAULT); |
| 134 | |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 135 | lyd_free_all(tree); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 136 | |
| 137 | /* make foo2 explicit */ |
| 138 | data = "<foo2 xmlns=\"urn:tests:a\">default-val</foo2>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 139 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 140 | assert_non_null(tree); |
| 141 | assert_int_equal(LYS_LEAF, tree->schema->nodetype); |
| 142 | assert_string_equal("foo2", tree->schema->name); |
| 143 | leaf = (struct lyd_node_term*)tree; |
| 144 | assert_string_equal("default-val", leaf->value.original); |
| 145 | assert_false(leaf->flags & LYD_DEFAULT); |
| 146 | |
| 147 | lyd_free_all(tree); |
| 148 | |
| 149 | /* parse foo2 but make it implicit */ |
| 150 | data = "<foo2 xmlns=\"urn:tests:a\" xmlns:wd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" wd:default=\"true\">default-val</foo2>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 151 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 152 | assert_non_null(tree); |
| 153 | assert_int_equal(LYS_LEAF, tree->schema->nodetype); |
| 154 | assert_string_equal("foo2", tree->schema->name); |
| 155 | leaf = (struct lyd_node_term*)tree; |
| 156 | assert_string_equal("default-val", leaf->value.original); |
| 157 | assert_true(leaf->flags & LYD_DEFAULT); |
| 158 | |
| 159 | lyd_free_all(tree); |
| 160 | |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 161 | *state = NULL; |
| 162 | } |
| 163 | |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 164 | static void |
| 165 | test_anydata(void **state) |
| 166 | { |
| 167 | *state = test_anydata; |
| 168 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 169 | const char *data; |
| 170 | char *str; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 171 | struct lyd_node *tree; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 172 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 173 | data = |
| 174 | "<any xmlns=\"urn:tests:a\">" |
| 175 | "<element1>" |
| 176 | "<x:element2 x:attr2=\"test\" xmlns:a=\"urn:tests:a\" xmlns:x=\"urn:x\">a:data</x:element2>" |
| 177 | "</element1>" |
| 178 | "<element1a/>" |
| 179 | "</any>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 180 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 181 | assert_non_null(tree); |
| 182 | assert_int_equal(LYS_ANYDATA, tree->schema->nodetype); |
| 183 | assert_string_equal("any", tree->schema->name); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 184 | |
| 185 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 186 | assert_string_equal(str, |
| 187 | "<any xmlns=\"urn:tests:a\">" |
| 188 | "<element1>" |
| 189 | "<element2 xmlns=\"urn:x\" xmlns:x=\"urn:x\" x:attr2=\"test\" xmlns:a=\"urn:tests:a\">a:data</element2>" |
| 190 | "</element1>" |
| 191 | "<element1a/>" |
| 192 | "</any>" |
| 193 | ); |
| 194 | free(str); |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 195 | |
| 196 | lyd_free_all(tree); |
| 197 | *state = NULL; |
| 198 | } |
| 199 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 200 | static void |
| 201 | test_list(void **state) |
| 202 | { |
| 203 | *state = test_list; |
| 204 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 205 | const char *data = "<l1 xmlns=\"urn:tests:a\"><a>one</a><b>one</b><c>1</c></l1>"; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 206 | struct lyd_node *tree, *iter; |
| 207 | struct lyd_node_inner *list; |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 208 | struct lyd_node_term *leaf; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 209 | |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 210 | /* check hashes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 211 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 212 | assert_non_null(tree); |
| 213 | assert_int_equal(LYS_LIST, tree->schema->nodetype); |
| 214 | assert_string_equal("l1", tree->schema->name); |
| 215 | list = (struct lyd_node_inner*)tree; |
| 216 | LY_LIST_FOR(list->child, iter) { |
| 217 | assert_int_not_equal(0, iter->hash); |
| 218 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 219 | lyd_free_all(tree); |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 220 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 221 | /* missing keys */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 222 | data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b></l1>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 223 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 224 | logbuf_assert("List instance is missing its key \"a\". /a:l1[b='b'][c='1']"); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 225 | |
| 226 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a></l1>"; |
| 227 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
| 228 | logbuf_assert("List instance is missing its key \"b\". /a:l1[a='a']"); |
| 229 | |
| 230 | data = "<l1 xmlns=\"urn:tests:a\"><b>b</b><a>a</a></l1>"; |
| 231 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
| 232 | logbuf_assert("List instance is missing its key \"c\". /a:l1[a='a'][b='b']"); |
| 233 | |
| 234 | /* key duplicate */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 235 | data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a><c>1</c></l1>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 236 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 237 | logbuf_assert("Duplicate instance of \"c\". /a:l1[a='a'][b='b'][c='1'][c='1']/c"); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 238 | |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 239 | /* keys order */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 240 | data = "<l1 xmlns=\"urn:tests:a\"><d>d</d><a>a</a><c>1</c><b>b</b></l1>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 241 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 242 | assert_non_null(tree); |
| 243 | assert_int_equal(LYS_LIST, tree->schema->nodetype); |
| 244 | assert_string_equal("l1", tree->schema->name); |
| 245 | list = (struct lyd_node_inner*)tree; |
| 246 | assert_non_null(leaf = (struct lyd_node_term*)list->child); |
| 247 | assert_string_equal("a", leaf->schema->name); |
| 248 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 249 | assert_string_equal("b", leaf->schema->name); |
| 250 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 251 | assert_string_equal("c", leaf->schema->name); |
| 252 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 253 | assert_string_equal("d", leaf->schema->name); |
| 254 | logbuf_assert("Invalid position of the key \"b\" in a list."); |
| 255 | lyd_free_all(tree); |
| 256 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 257 | data = "<l1 xmlns=\"urn:tests:a\"><c>1</c><b>b</b><a>a</a></l1>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 258 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 259 | assert_non_null(tree); |
| 260 | assert_int_equal(LYS_LIST, tree->schema->nodetype); |
| 261 | assert_string_equal("l1", tree->schema->name); |
| 262 | list = (struct lyd_node_inner*)tree; |
| 263 | assert_non_null(leaf = (struct lyd_node_term*)list->child); |
| 264 | assert_string_equal("a", leaf->schema->name); |
| 265 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 266 | assert_string_equal("b", leaf->schema->name); |
| 267 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 268 | assert_string_equal("c", leaf->schema->name); |
| 269 | logbuf_assert("Invalid position of the key \"a\" in a list."); |
| 270 | logbuf_clean(); |
| 271 | lyd_free_all(tree); |
| 272 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 273 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_OPT_STRICT, &tree)); |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 274 | logbuf_assert("Invalid position of the key \"b\" in a list. Line number 1."); |
| 275 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 276 | *state = NULL; |
| 277 | } |
| 278 | |
Radek Krejci | b6f7ae5 | 2019-07-19 10:31:42 +0200 | [diff] [blame] | 279 | static void |
| 280 | test_container(void **state) |
| 281 | { |
| 282 | *state = test_container; |
| 283 | |
| 284 | const char *data = "<c xmlns=\"urn:tests:a\"/>"; |
| 285 | struct lyd_node *tree; |
| 286 | struct lyd_node_inner *cont; |
| 287 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 288 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Radek Krejci | b6f7ae5 | 2019-07-19 10:31:42 +0200 | [diff] [blame] | 289 | assert_non_null(tree); |
| 290 | assert_int_equal(LYS_CONTAINER, tree->schema->nodetype); |
| 291 | assert_string_equal("c", tree->schema->name); |
| 292 | cont = (struct lyd_node_inner*)tree; |
| 293 | assert_true(cont->flags & LYD_DEFAULT); |
| 294 | lyd_free_all(tree); |
| 295 | |
| 296 | data = "<cp xmlns=\"urn:tests:a\"/>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 297 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
Radek Krejci | b6f7ae5 | 2019-07-19 10:31:42 +0200 | [diff] [blame] | 298 | assert_non_null(tree); |
| 299 | assert_int_equal(LYS_CONTAINER, tree->schema->nodetype); |
| 300 | assert_string_equal("cp", tree->schema->name); |
| 301 | cont = (struct lyd_node_inner*)tree; |
| 302 | assert_false(cont->flags & LYD_DEFAULT); |
| 303 | lyd_free_all(tree); |
| 304 | |
| 305 | *state = NULL; |
| 306 | } |
| 307 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 308 | static void |
| 309 | test_opaq(void **state) |
| 310 | { |
| 311 | *state = test_opaq; |
| 312 | |
| 313 | const char *data; |
| 314 | char *str; |
| 315 | struct lyd_node *tree; |
| 316 | |
| 317 | /* invalid value, no flags */ |
| 318 | data = "<foo3 xmlns=\"urn:tests:a\"/>"; |
| 319 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
| 320 | logbuf_assert("Invalid empty uint32 value. /"); |
| 321 | assert_null(tree); |
| 322 | |
| 323 | /* opaq flag */ |
| 324 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree)); |
| 325 | assert_non_null(tree); |
| 326 | assert_null(tree->schema); |
| 327 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "foo3"); |
| 328 | assert_string_equal(((struct lyd_node_opaq *)tree)->value, ""); |
| 329 | |
| 330 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 331 | assert_string_equal(str, "<foo3 xmlns=\"urn:tests:a\"/>"); |
| 332 | free(str); |
| 333 | lyd_free_all(tree); |
| 334 | |
| 335 | /* missing key, no flags */ |
| 336 | data = "<l1 xmlns=\"urn:tests:a\"><a>val_a</a><b>val_b</b><d>val_d</d></l1>"; |
| 337 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
| 338 | logbuf_assert("List instance is missing its key \"c\". /a:l1[a='val_a'][b='val_b']"); |
| 339 | assert_null(tree); |
| 340 | |
| 341 | /* opaq flag */ |
| 342 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree)); |
| 343 | assert_non_null(tree); |
| 344 | assert_null(tree->schema); |
| 345 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1"); |
| 346 | assert_string_equal(((struct lyd_node_opaq *)tree)->value, ""); |
| 347 | |
| 348 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 349 | assert_string_equal(str, data); |
| 350 | free(str); |
| 351 | lyd_free_all(tree); |
| 352 | |
| 353 | /* invalid key, no flags */ |
| 354 | data = "<l1 xmlns=\"urn:tests:a\"><a>val_a</a><b>val_b</b><c>val_c</c></l1>"; |
| 355 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
| 356 | logbuf_assert("Invalid int16 value \"val_c\". /"); |
| 357 | assert_null(tree); |
| 358 | |
| 359 | /* opaq flag */ |
| 360 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree)); |
| 361 | assert_non_null(tree); |
| 362 | assert_null(tree->schema); |
| 363 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1"); |
| 364 | assert_string_equal(((struct lyd_node_opaq *)tree)->value, ""); |
| 365 | |
| 366 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 367 | assert_string_equal(str, data); |
| 368 | free(str); |
| 369 | lyd_free_all(tree); |
| 370 | |
| 371 | *state = NULL; |
| 372 | } |
| 373 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 374 | static void |
| 375 | test_rpc(void **state) |
| 376 | { |
| 377 | *state = test_rpc; |
| 378 | |
| 379 | const char *data; |
| 380 | char *str; |
| 381 | struct lyd_node *tree, *op; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame^] | 382 | const struct lyd_node *node; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 383 | |
| 384 | data = |
| 385 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">" |
| 386 | "<edit-config>" |
| 387 | "<target>" |
| 388 | "<running/>" |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 389 | "</target>" |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame^] | 390 | "<config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" |
| 391 | "<l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">" |
| 392 | "<a>val_a</a>" |
| 393 | "<b>val_b</b>" |
| 394 | "<c>val_c</c>" |
| 395 | "</l1>" |
| 396 | "<cp xmlns=\"urn:tests:a\">" |
| 397 | "<z nc:operation=\"delete\"/>" |
| 398 | "</cp>" |
| 399 | "</config>" |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 400 | "</edit-config>" |
| 401 | "</rpc>"; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame^] | 402 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &tree, &op)); |
| 403 | |
| 404 | assert_non_null(op); |
| 405 | assert_string_equal(op->schema->name, "edit-config"); |
| 406 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 407 | assert_non_null(tree); |
| 408 | assert_null(tree->schema); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame^] | 409 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc"); |
| 410 | assert_non_null(((struct lyd_node_opaq *)tree)->attr); |
| 411 | node = lyd_node_children(tree); |
| 412 | assert_string_equal(node->schema->name, "edit-config"); |
| 413 | node = lyd_node_children(node)->next; |
| 414 | assert_string_equal(node->schema->name, "config"); |
| 415 | node = ((struct lyd_node_any *)node)->value.tree; |
| 416 | /* l1 key c has invalid value */ |
| 417 | assert_null(node->schema); |
| 418 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "l1"); |
| 419 | node = node->next; |
| 420 | assert_non_null(node->schema); |
| 421 | assert_string_equal(node->schema->name, "cp"); |
| 422 | node = lyd_node_children(node); |
| 423 | /* z has no value */ |
| 424 | assert_null(node->schema); |
| 425 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "z"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 426 | |
| 427 | lyd_print_mem(&str, tree, LYD_XML, 0); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame^] | 428 | assert_string_equal(str, |
| 429 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">" |
| 430 | "<edit-config>" |
| 431 | "<target>" |
| 432 | "<running/>" |
| 433 | "</target>" |
| 434 | "<config>" |
| 435 | "<l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">" |
| 436 | "<a>val_a</a>" |
| 437 | "<b>val_b</b>" |
| 438 | "<c>val_c</c>" |
| 439 | "</l1>" |
| 440 | "<cp xmlns=\"urn:tests:a\">" |
| 441 | "<z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>" |
| 442 | "</cp>" |
| 443 | "</config>" |
| 444 | "</edit-config>" |
| 445 | "</rpc>"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 446 | free(str); |
| 447 | lyd_free_all(tree); |
| 448 | |
| 449 | /* wrong namespace, element name, whatever... */ |
| 450 | |
| 451 | *state = NULL; |
| 452 | } |
| 453 | |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 454 | int main(void) |
| 455 | { |
| 456 | const struct CMUnitTest tests[] = { |
| 457 | cmocka_unit_test_setup_teardown(test_leaf, setup, teardown), |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 458 | cmocka_unit_test_setup_teardown(test_anydata, setup, teardown), |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 459 | cmocka_unit_test_setup_teardown(test_list, setup, teardown), |
Radek Krejci | b6f7ae5 | 2019-07-19 10:31:42 +0200 | [diff] [blame] | 460 | cmocka_unit_test_setup_teardown(test_container, setup, teardown), |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 461 | cmocka_unit_test_setup_teardown(test_opaq, setup, teardown), |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame^] | 462 | cmocka_unit_test_setup_teardown(test_rpc, setup, teardown), |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 463 | }; |
| 464 | |
| 465 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 466 | } |