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;}" |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame^] | 64 | "container c {" |
| 65 | "leaf x {type string;}" |
| 66 | "action act { input { leaf al {type string;} } }" |
| 67 | "notification n1 { leaf nl {type string;} }" |
| 68 | "}" |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 69 | "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] | 70 | "anydata any {config false;}" |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 71 | "leaf foo2 { type string; default \"default-val\"; }" |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame^] | 72 | "leaf foo3 { type uint32; }" |
| 73 | "notification n2;}"; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 74 | const struct lys_module *mod; |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 75 | |
| 76 | #if ENABLE_LOGGER_CHECKING |
| 77 | ly_set_log_clb(logger, 1); |
| 78 | #endif |
| 79 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 80 | assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx)); |
| 81 | 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] | 82 | assert_non_null((mod = ly_ctx_load_module(ctx, "ietf-netconf", "2011-06-01"))); |
| 83 | assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "writable-running")); |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 84 | assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG)); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int |
| 90 | teardown(void **state) |
| 91 | { |
| 92 | #if ENABLE_LOGGER_CHECKING |
| 93 | if (*state) { |
| 94 | fprintf(stderr, "%s\n", logbuf); |
| 95 | } |
| 96 | #else |
| 97 | (void) state; /* unused */ |
| 98 | #endif |
| 99 | |
| 100 | ly_ctx_destroy(ctx, NULL); |
| 101 | ctx = NULL; |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | logbuf_clean(void) |
| 108 | { |
| 109 | logbuf[0] = '\0'; |
| 110 | } |
| 111 | |
| 112 | #if ENABLE_LOGGER_CHECKING |
| 113 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 114 | #else |
| 115 | # define logbuf_assert(str) |
| 116 | #endif |
| 117 | |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 118 | static void |
| 119 | test_leaf(void **state) |
| 120 | { |
| 121 | *state = test_leaf; |
| 122 | |
| 123 | const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>"; |
| 124 | struct lyd_node *tree; |
| 125 | struct lyd_node_term *leaf; |
| 126 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 127 | 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] | 128 | assert_non_null(tree); |
| 129 | assert_int_equal(LYS_LEAF, tree->schema->nodetype); |
| 130 | assert_string_equal("foo", tree->schema->name); |
| 131 | leaf = (struct lyd_node_term*)tree; |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 132 | assert_string_equal("foo value", leaf->value.original); |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 133 | |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 134 | assert_int_equal(LYS_LEAF, tree->next->next->schema->nodetype); |
| 135 | assert_string_equal("foo2", tree->next->next->schema->name); |
| 136 | leaf = (struct lyd_node_term*)tree->next->next; |
| 137 | assert_string_equal("default-val", leaf->value.original); |
| 138 | assert_true(leaf->flags & LYD_DEFAULT); |
| 139 | |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 140 | lyd_free_all(tree); |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 141 | |
| 142 | /* make foo2 explicit */ |
| 143 | data = "<foo2 xmlns=\"urn:tests:a\">default-val</foo2>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 144 | 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] | 145 | assert_non_null(tree); |
| 146 | assert_int_equal(LYS_LEAF, tree->schema->nodetype); |
| 147 | assert_string_equal("foo2", tree->schema->name); |
| 148 | leaf = (struct lyd_node_term*)tree; |
| 149 | assert_string_equal("default-val", leaf->value.original); |
| 150 | assert_false(leaf->flags & LYD_DEFAULT); |
| 151 | |
| 152 | lyd_free_all(tree); |
| 153 | |
| 154 | /* parse foo2 but make it implicit */ |
| 155 | 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] | 156 | 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] | 157 | assert_non_null(tree); |
| 158 | assert_int_equal(LYS_LEAF, tree->schema->nodetype); |
| 159 | assert_string_equal("foo2", tree->schema->name); |
| 160 | leaf = (struct lyd_node_term*)tree; |
| 161 | assert_string_equal("default-val", leaf->value.original); |
| 162 | assert_true(leaf->flags & LYD_DEFAULT); |
| 163 | |
| 164 | lyd_free_all(tree); |
| 165 | |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 166 | *state = NULL; |
| 167 | } |
| 168 | |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 169 | static void |
| 170 | test_anydata(void **state) |
| 171 | { |
| 172 | *state = test_anydata; |
| 173 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 174 | const char *data; |
| 175 | char *str; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 176 | struct lyd_node *tree; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 177 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 178 | data = |
| 179 | "<any xmlns=\"urn:tests:a\">" |
| 180 | "<element1>" |
| 181 | "<x:element2 x:attr2=\"test\" xmlns:a=\"urn:tests:a\" xmlns:x=\"urn:x\">a:data</x:element2>" |
| 182 | "</element1>" |
| 183 | "<element1a/>" |
| 184 | "</any>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 185 | 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] | 186 | assert_non_null(tree); |
| 187 | assert_int_equal(LYS_ANYDATA, tree->schema->nodetype); |
| 188 | assert_string_equal("any", tree->schema->name); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 189 | |
| 190 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 191 | assert_string_equal(str, |
| 192 | "<any xmlns=\"urn:tests:a\">" |
| 193 | "<element1>" |
| 194 | "<element2 xmlns=\"urn:x\" xmlns:x=\"urn:x\" x:attr2=\"test\" xmlns:a=\"urn:tests:a\">a:data</element2>" |
| 195 | "</element1>" |
| 196 | "<element1a/>" |
| 197 | "</any>" |
| 198 | ); |
| 199 | free(str); |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 200 | |
| 201 | lyd_free_all(tree); |
| 202 | *state = NULL; |
| 203 | } |
| 204 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 205 | static void |
| 206 | test_list(void **state) |
| 207 | { |
| 208 | *state = test_list; |
| 209 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 210 | 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] | 211 | struct lyd_node *tree, *iter; |
| 212 | struct lyd_node_inner *list; |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 213 | struct lyd_node_term *leaf; |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 214 | |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 215 | /* check hashes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 216 | 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] | 217 | assert_non_null(tree); |
| 218 | assert_int_equal(LYS_LIST, tree->schema->nodetype); |
| 219 | assert_string_equal("l1", tree->schema->name); |
| 220 | list = (struct lyd_node_inner*)tree; |
| 221 | LY_LIST_FOR(list->child, iter) { |
| 222 | assert_int_not_equal(0, iter->hash); |
| 223 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 224 | lyd_free_all(tree); |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 225 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 226 | /* missing keys */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 227 | 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] | 228 | 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] | 229 | 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] | 230 | |
| 231 | data = "<l1 xmlns=\"urn:tests:a\"><a>a</a></l1>"; |
| 232 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
| 233 | logbuf_assert("List instance is missing its key \"b\". /a:l1[a='a']"); |
| 234 | |
| 235 | data = "<l1 xmlns=\"urn:tests:a\"><b>b</b><a>a</a></l1>"; |
| 236 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
| 237 | logbuf_assert("List instance is missing its key \"c\". /a:l1[a='a'][b='b']"); |
| 238 | |
| 239 | /* key duplicate */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 240 | 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] | 241 | 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] | 242 | 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] | 243 | |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 244 | /* keys order */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 245 | 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] | 246 | 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] | 247 | assert_non_null(tree); |
| 248 | assert_int_equal(LYS_LIST, tree->schema->nodetype); |
| 249 | assert_string_equal("l1", tree->schema->name); |
| 250 | list = (struct lyd_node_inner*)tree; |
| 251 | assert_non_null(leaf = (struct lyd_node_term*)list->child); |
| 252 | assert_string_equal("a", leaf->schema->name); |
| 253 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 254 | assert_string_equal("b", leaf->schema->name); |
| 255 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 256 | assert_string_equal("c", leaf->schema->name); |
| 257 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 258 | assert_string_equal("d", leaf->schema->name); |
| 259 | logbuf_assert("Invalid position of the key \"b\" in a list."); |
| 260 | lyd_free_all(tree); |
| 261 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 262 | 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] | 263 | 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] | 264 | assert_non_null(tree); |
| 265 | assert_int_equal(LYS_LIST, tree->schema->nodetype); |
| 266 | assert_string_equal("l1", tree->schema->name); |
| 267 | list = (struct lyd_node_inner*)tree; |
| 268 | assert_non_null(leaf = (struct lyd_node_term*)list->child); |
| 269 | assert_string_equal("a", leaf->schema->name); |
| 270 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 271 | assert_string_equal("b", leaf->schema->name); |
| 272 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 273 | assert_string_equal("c", leaf->schema->name); |
| 274 | logbuf_assert("Invalid position of the key \"a\" in a list."); |
| 275 | logbuf_clean(); |
| 276 | lyd_free_all(tree); |
| 277 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 278 | 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] | 279 | logbuf_assert("Invalid position of the key \"b\" in a list. Line number 1."); |
| 280 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 281 | *state = NULL; |
| 282 | } |
| 283 | |
Radek Krejci | b6f7ae5 | 2019-07-19 10:31:42 +0200 | [diff] [blame] | 284 | static void |
| 285 | test_container(void **state) |
| 286 | { |
| 287 | *state = test_container; |
| 288 | |
| 289 | const char *data = "<c xmlns=\"urn:tests:a\"/>"; |
| 290 | struct lyd_node *tree; |
| 291 | struct lyd_node_inner *cont; |
| 292 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 293 | 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] | 294 | assert_non_null(tree); |
| 295 | assert_int_equal(LYS_CONTAINER, tree->schema->nodetype); |
| 296 | assert_string_equal("c", tree->schema->name); |
| 297 | cont = (struct lyd_node_inner*)tree; |
| 298 | assert_true(cont->flags & LYD_DEFAULT); |
| 299 | lyd_free_all(tree); |
| 300 | |
| 301 | data = "<cp xmlns=\"urn:tests:a\"/>"; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 302 | 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] | 303 | assert_non_null(tree); |
| 304 | assert_int_equal(LYS_CONTAINER, tree->schema->nodetype); |
| 305 | assert_string_equal("cp", tree->schema->name); |
| 306 | cont = (struct lyd_node_inner*)tree; |
| 307 | assert_false(cont->flags & LYD_DEFAULT); |
| 308 | lyd_free_all(tree); |
| 309 | |
| 310 | *state = NULL; |
| 311 | } |
| 312 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 313 | static void |
| 314 | test_opaq(void **state) |
| 315 | { |
| 316 | *state = test_opaq; |
| 317 | |
| 318 | const char *data; |
| 319 | char *str; |
| 320 | struct lyd_node *tree; |
| 321 | |
| 322 | /* invalid value, no flags */ |
| 323 | data = "<foo3 xmlns=\"urn:tests:a\"/>"; |
| 324 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
| 325 | logbuf_assert("Invalid empty uint32 value. /"); |
| 326 | assert_null(tree); |
| 327 | |
| 328 | /* opaq flag */ |
| 329 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree)); |
| 330 | assert_non_null(tree); |
| 331 | assert_null(tree->schema); |
| 332 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "foo3"); |
| 333 | assert_string_equal(((struct lyd_node_opaq *)tree)->value, ""); |
| 334 | |
| 335 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 336 | assert_string_equal(str, "<foo3 xmlns=\"urn:tests:a\"/>"); |
| 337 | free(str); |
| 338 | lyd_free_all(tree); |
| 339 | |
| 340 | /* missing key, no flags */ |
| 341 | data = "<l1 xmlns=\"urn:tests:a\"><a>val_a</a><b>val_b</b><d>val_d</d></l1>"; |
| 342 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
| 343 | logbuf_assert("List instance is missing its key \"c\". /a:l1[a='val_a'][b='val_b']"); |
| 344 | assert_null(tree); |
| 345 | |
| 346 | /* opaq flag */ |
| 347 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree)); |
| 348 | assert_non_null(tree); |
| 349 | assert_null(tree->schema); |
| 350 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1"); |
| 351 | assert_string_equal(((struct lyd_node_opaq *)tree)->value, ""); |
| 352 | |
| 353 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 354 | assert_string_equal(str, data); |
| 355 | free(str); |
| 356 | lyd_free_all(tree); |
| 357 | |
| 358 | /* invalid key, no flags */ |
| 359 | data = "<l1 xmlns=\"urn:tests:a\"><a>val_a</a><b>val_b</b><c>val_c</c></l1>"; |
| 360 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, data, LYD_VALOPT_DATA_ONLY, &tree)); |
| 361 | logbuf_assert("Invalid int16 value \"val_c\". /"); |
| 362 | assert_null(tree); |
| 363 | |
| 364 | /* opaq flag */ |
| 365 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_data(ctx, data, LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree)); |
| 366 | assert_non_null(tree); |
| 367 | assert_null(tree->schema); |
| 368 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1"); |
| 369 | assert_string_equal(((struct lyd_node_opaq *)tree)->value, ""); |
| 370 | |
| 371 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 372 | assert_string_equal(str, data); |
| 373 | free(str); |
| 374 | lyd_free_all(tree); |
| 375 | |
| 376 | *state = NULL; |
| 377 | } |
| 378 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 379 | static void |
| 380 | test_rpc(void **state) |
| 381 | { |
| 382 | *state = test_rpc; |
| 383 | |
| 384 | const char *data; |
| 385 | char *str; |
| 386 | struct lyd_node *tree, *op; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 387 | const struct lyd_node *node; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 388 | |
| 389 | data = |
| 390 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">" |
| 391 | "<edit-config>" |
| 392 | "<target>" |
| 393 | "<running/>" |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 394 | "</target>" |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 395 | "<config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" |
| 396 | "<l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">" |
| 397 | "<a>val_a</a>" |
| 398 | "<b>val_b</b>" |
| 399 | "<c>val_c</c>" |
| 400 | "</l1>" |
| 401 | "<cp xmlns=\"urn:tests:a\">" |
| 402 | "<z nc:operation=\"delete\"/>" |
| 403 | "</cp>" |
| 404 | "</config>" |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 405 | "</edit-config>" |
| 406 | "</rpc>"; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 407 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &tree, &op)); |
| 408 | |
| 409 | assert_non_null(op); |
| 410 | assert_string_equal(op->schema->name, "edit-config"); |
| 411 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 412 | assert_non_null(tree); |
| 413 | assert_null(tree->schema); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 414 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc"); |
| 415 | assert_non_null(((struct lyd_node_opaq *)tree)->attr); |
| 416 | node = lyd_node_children(tree); |
| 417 | assert_string_equal(node->schema->name, "edit-config"); |
| 418 | node = lyd_node_children(node)->next; |
| 419 | assert_string_equal(node->schema->name, "config"); |
| 420 | node = ((struct lyd_node_any *)node)->value.tree; |
| 421 | /* l1 key c has invalid value */ |
| 422 | assert_null(node->schema); |
| 423 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "l1"); |
| 424 | node = node->next; |
| 425 | assert_non_null(node->schema); |
| 426 | assert_string_equal(node->schema->name, "cp"); |
| 427 | node = lyd_node_children(node); |
| 428 | /* z has no value */ |
| 429 | assert_null(node->schema); |
| 430 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "z"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 431 | |
| 432 | lyd_print_mem(&str, tree, LYD_XML, 0); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 433 | assert_string_equal(str, |
| 434 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">" |
| 435 | "<edit-config>" |
| 436 | "<target>" |
| 437 | "<running/>" |
| 438 | "</target>" |
| 439 | "<config>" |
| 440 | "<l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">" |
| 441 | "<a>val_a</a>" |
| 442 | "<b>val_b</b>" |
| 443 | "<c>val_c</c>" |
| 444 | "</l1>" |
| 445 | "<cp xmlns=\"urn:tests:a\">" |
| 446 | "<z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>" |
| 447 | "</cp>" |
| 448 | "</config>" |
| 449 | "</edit-config>" |
| 450 | "</rpc>"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 451 | free(str); |
| 452 | lyd_free_all(tree); |
| 453 | |
| 454 | /* wrong namespace, element name, whatever... */ |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame^] | 455 | /* TODO */ |
| 456 | |
| 457 | *state = NULL; |
| 458 | } |
| 459 | |
| 460 | static void |
| 461 | test_action(void **state) |
| 462 | { |
| 463 | *state = test_action; |
| 464 | |
| 465 | const char *data; |
| 466 | char *str; |
| 467 | struct lyd_node *tree, *op; |
| 468 | const struct lyd_node *node; |
| 469 | |
| 470 | data = |
| 471 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">" |
| 472 | "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">" |
| 473 | "<c xmlns=\"urn:tests:a\">" |
| 474 | "<act>" |
| 475 | "<al>value</al>" |
| 476 | "</act>" |
| 477 | "</c>" |
| 478 | "</action>" |
| 479 | "</rpc>"; |
| 480 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &tree, &op)); |
| 481 | |
| 482 | assert_non_null(op); |
| 483 | assert_string_equal(op->schema->name, "act"); |
| 484 | |
| 485 | assert_non_null(tree); |
| 486 | assert_null(tree->schema); |
| 487 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc"); |
| 488 | assert_non_null(((struct lyd_node_opaq *)tree)->attr); |
| 489 | node = lyd_node_children(tree); |
| 490 | assert_null(node->schema); |
| 491 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "action"); |
| 492 | assert_null(((struct lyd_node_opaq *)node)->attr); |
| 493 | |
| 494 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 495 | assert_string_equal(str, |
| 496 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">" |
| 497 | "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">" |
| 498 | "<c xmlns=\"urn:tests:a\">" |
| 499 | "<act>" |
| 500 | "<al>value</al>" |
| 501 | "</act>" |
| 502 | "</c>" |
| 503 | "</action>" |
| 504 | "</rpc>"); |
| 505 | free(str); |
| 506 | lyd_free_all(tree); |
| 507 | |
| 508 | /* wrong namespace, element name, whatever... */ |
| 509 | /* TODO */ |
| 510 | |
| 511 | *state = NULL; |
| 512 | } |
| 513 | |
| 514 | static void |
| 515 | test_notification(void **state) |
| 516 | { |
| 517 | *state = test_notification; |
| 518 | |
| 519 | const char *data; |
| 520 | char *str; |
| 521 | struct lyd_node *tree, *ntf; |
| 522 | const struct lyd_node *node; |
| 523 | |
| 524 | data = |
| 525 | "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">" |
| 526 | "<eventTime>2037-07-08T00:01:00Z</eventTime>" |
| 527 | "<c xmlns=\"urn:tests:a\">" |
| 528 | "<n1>" |
| 529 | "<nl>value</nl>" |
| 530 | "</n1>" |
| 531 | "</c>" |
| 532 | "</notification>"; |
| 533 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_notif(ctx, data, &tree, &ntf)); |
| 534 | |
| 535 | assert_non_null(ntf); |
| 536 | assert_string_equal(ntf->schema->name, "n1"); |
| 537 | |
| 538 | assert_non_null(tree); |
| 539 | assert_null(tree->schema); |
| 540 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "notification"); |
| 541 | assert_null(((struct lyd_node_opaq *)tree)->attr); |
| 542 | node = lyd_node_children(tree); |
| 543 | assert_null(node->schema); |
| 544 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "eventTime"); |
| 545 | assert_string_equal(((struct lyd_node_opaq *)node)->value, "2037-07-08T00:01:00Z"); |
| 546 | assert_null(((struct lyd_node_opaq *)node)->attr); |
| 547 | node = node->next; |
| 548 | assert_non_null(node->schema); |
| 549 | assert_string_equal(node->schema->name, "c"); |
| 550 | |
| 551 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 552 | assert_string_equal(str, data); |
| 553 | free(str); |
| 554 | lyd_free_all(tree); |
| 555 | |
| 556 | /* top-level notif without envelope */ |
| 557 | data = "<n2 xmlns=\"urn:tests:a\"/>"; |
| 558 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_notif(ctx, data, &tree, &ntf)); |
| 559 | |
| 560 | assert_non_null(ntf); |
| 561 | assert_string_equal(ntf->schema->name, "n2"); |
| 562 | |
| 563 | assert_non_null(tree); |
| 564 | assert_ptr_equal(ntf, tree); |
| 565 | |
| 566 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 567 | assert_string_equal(str, data); |
| 568 | free(str); |
| 569 | lyd_free_all(tree); |
| 570 | |
| 571 | /* wrong namespace, element name, whatever... */ |
| 572 | /* TODO */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 573 | |
| 574 | *state = NULL; |
| 575 | } |
| 576 | |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 577 | int main(void) |
| 578 | { |
| 579 | const struct CMUnitTest tests[] = { |
| 580 | cmocka_unit_test_setup_teardown(test_leaf, setup, teardown), |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 581 | cmocka_unit_test_setup_teardown(test_anydata, setup, teardown), |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 582 | cmocka_unit_test_setup_teardown(test_list, setup, teardown), |
Radek Krejci | b6f7ae5 | 2019-07-19 10:31:42 +0200 | [diff] [blame] | 583 | cmocka_unit_test_setup_teardown(test_container, setup, teardown), |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 584 | cmocka_unit_test_setup_teardown(test_opaq, setup, teardown), |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 585 | cmocka_unit_test_setup_teardown(test_rpc, setup, teardown), |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame^] | 586 | cmocka_unit_test_setup_teardown(test_action, setup, teardown), |
| 587 | cmocka_unit_test_setup_teardown(test_notification, setup, teardown), |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 588 | }; |
| 589 | |
| 590 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 591 | } |