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;}" |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 66 | "action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }" |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 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)); |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 325 | logbuf_assert("Invalid empty uint32 value. /a:foo3"); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 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)); |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 361 | logbuf_assert("Invalid int16 value \"val_c\". /a:c"); |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 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 | |
Michal Vasko | 413c7f2 | 2020-05-05 12:34:06 +0200 | [diff] [blame] | 376 | /* opaq flag and fail */ |
| 377 | assert_int_equal(LY_EVALID, lyd_parse_xml_data(ctx, "<a xmlns=\"ns\"><b>x</b><c xml:id=\"D\">1</c></a>", |
| 378 | LYD_OPT_OPAQ | LYD_VALOPT_DATA_ONLY, &tree)); |
| 379 | assert_null(tree); |
| 380 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 381 | *state = NULL; |
| 382 | } |
| 383 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 384 | static void |
| 385 | test_rpc(void **state) |
| 386 | { |
| 387 | *state = test_rpc; |
| 388 | |
| 389 | const char *data; |
| 390 | char *str; |
| 391 | struct lyd_node *tree, *op; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 392 | const struct lyd_node *node; |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 393 | |
| 394 | data = |
| 395 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">" |
| 396 | "<edit-config>" |
| 397 | "<target>" |
| 398 | "<running/>" |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 399 | "</target>" |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 400 | "<config xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" |
| 401 | "<l1 xmlns=\"urn:tests:a\" nc:operation=\"replace\">" |
| 402 | "<a>val_a</a>" |
| 403 | "<b>val_b</b>" |
| 404 | "<c>val_c</c>" |
| 405 | "</l1>" |
| 406 | "<cp xmlns=\"urn:tests:a\">" |
| 407 | "<z nc:operation=\"delete\"/>" |
| 408 | "</cp>" |
| 409 | "</config>" |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 410 | "</edit-config>" |
| 411 | "</rpc>"; |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 412 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &tree, &op)); |
| 413 | |
| 414 | assert_non_null(op); |
| 415 | assert_string_equal(op->schema->name, "edit-config"); |
| 416 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 417 | assert_non_null(tree); |
| 418 | assert_null(tree->schema); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 419 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc"); |
| 420 | assert_non_null(((struct lyd_node_opaq *)tree)->attr); |
| 421 | node = lyd_node_children(tree); |
| 422 | assert_string_equal(node->schema->name, "edit-config"); |
| 423 | node = lyd_node_children(node)->next; |
| 424 | assert_string_equal(node->schema->name, "config"); |
| 425 | node = ((struct lyd_node_any *)node)->value.tree; |
| 426 | /* l1 key c has invalid value */ |
| 427 | assert_null(node->schema); |
| 428 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "l1"); |
| 429 | node = node->next; |
| 430 | assert_non_null(node->schema); |
| 431 | assert_string_equal(node->schema->name, "cp"); |
| 432 | node = lyd_node_children(node); |
| 433 | /* z has no value */ |
| 434 | assert_null(node->schema); |
| 435 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "z"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 436 | |
| 437 | lyd_print_mem(&str, tree, LYD_XML, 0); |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 438 | assert_string_equal(str, |
| 439 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">" |
| 440 | "<edit-config>" |
| 441 | "<target>" |
| 442 | "<running/>" |
| 443 | "</target>" |
| 444 | "<config>" |
| 445 | "<l1 xmlns=\"urn:tests:a\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"replace\">" |
| 446 | "<a>val_a</a>" |
| 447 | "<b>val_b</b>" |
| 448 | "<c>val_c</c>" |
| 449 | "</l1>" |
| 450 | "<cp xmlns=\"urn:tests:a\">" |
| 451 | "<z xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"delete\"/>" |
| 452 | "</cp>" |
| 453 | "</config>" |
| 454 | "</edit-config>" |
| 455 | "</rpc>"); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 456 | free(str); |
| 457 | lyd_free_all(tree); |
| 458 | |
| 459 | /* wrong namespace, element name, whatever... */ |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 460 | /* TODO */ |
| 461 | |
| 462 | *state = NULL; |
| 463 | } |
| 464 | |
| 465 | static void |
| 466 | test_action(void **state) |
| 467 | { |
| 468 | *state = test_action; |
| 469 | |
| 470 | const char *data; |
| 471 | char *str; |
| 472 | struct lyd_node *tree, *op; |
| 473 | const struct lyd_node *node; |
| 474 | |
| 475 | data = |
| 476 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">" |
| 477 | "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">" |
| 478 | "<c xmlns=\"urn:tests:a\">" |
| 479 | "<act>" |
| 480 | "<al>value</al>" |
| 481 | "</act>" |
| 482 | "</c>" |
| 483 | "</action>" |
| 484 | "</rpc>"; |
| 485 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &tree, &op)); |
| 486 | |
| 487 | assert_non_null(op); |
| 488 | assert_string_equal(op->schema->name, "act"); |
| 489 | |
| 490 | assert_non_null(tree); |
| 491 | assert_null(tree->schema); |
| 492 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc"); |
| 493 | assert_non_null(((struct lyd_node_opaq *)tree)->attr); |
| 494 | node = lyd_node_children(tree); |
| 495 | assert_null(node->schema); |
| 496 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "action"); |
| 497 | assert_null(((struct lyd_node_opaq *)node)->attr); |
| 498 | |
| 499 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 500 | assert_string_equal(str, |
| 501 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\" custom-attr=\"val\">" |
| 502 | "<action xmlns=\"urn:ietf:params:xml:ns:yang:1\">" |
| 503 | "<c xmlns=\"urn:tests:a\">" |
| 504 | "<act>" |
| 505 | "<al>value</al>" |
| 506 | "</act>" |
| 507 | "</c>" |
| 508 | "</action>" |
| 509 | "</rpc>"); |
| 510 | free(str); |
| 511 | lyd_free_all(tree); |
| 512 | |
| 513 | /* wrong namespace, element name, whatever... */ |
| 514 | /* TODO */ |
| 515 | |
| 516 | *state = NULL; |
| 517 | } |
| 518 | |
| 519 | static void |
| 520 | test_notification(void **state) |
| 521 | { |
| 522 | *state = test_notification; |
| 523 | |
| 524 | const char *data; |
| 525 | char *str; |
| 526 | struct lyd_node *tree, *ntf; |
| 527 | const struct lyd_node *node; |
| 528 | |
| 529 | data = |
| 530 | "<notification xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">" |
| 531 | "<eventTime>2037-07-08T00:01:00Z</eventTime>" |
| 532 | "<c xmlns=\"urn:tests:a\">" |
| 533 | "<n1>" |
| 534 | "<nl>value</nl>" |
| 535 | "</n1>" |
| 536 | "</c>" |
| 537 | "</notification>"; |
| 538 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_notif(ctx, data, &tree, &ntf)); |
| 539 | |
| 540 | assert_non_null(ntf); |
| 541 | assert_string_equal(ntf->schema->name, "n1"); |
| 542 | |
| 543 | assert_non_null(tree); |
| 544 | assert_null(tree->schema); |
| 545 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "notification"); |
| 546 | assert_null(((struct lyd_node_opaq *)tree)->attr); |
| 547 | node = lyd_node_children(tree); |
| 548 | assert_null(node->schema); |
| 549 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "eventTime"); |
| 550 | assert_string_equal(((struct lyd_node_opaq *)node)->value, "2037-07-08T00:01:00Z"); |
| 551 | assert_null(((struct lyd_node_opaq *)node)->attr); |
| 552 | node = node->next; |
| 553 | assert_non_null(node->schema); |
| 554 | assert_string_equal(node->schema->name, "c"); |
| 555 | |
| 556 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 557 | assert_string_equal(str, data); |
| 558 | free(str); |
| 559 | lyd_free_all(tree); |
| 560 | |
| 561 | /* top-level notif without envelope */ |
| 562 | data = "<n2 xmlns=\"urn:tests:a\"/>"; |
| 563 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_notif(ctx, data, &tree, &ntf)); |
| 564 | |
| 565 | assert_non_null(ntf); |
| 566 | assert_string_equal(ntf->schema->name, "n2"); |
| 567 | |
| 568 | assert_non_null(tree); |
| 569 | assert_ptr_equal(ntf, tree); |
| 570 | |
| 571 | lyd_print_mem(&str, tree, LYD_XML, 0); |
| 572 | assert_string_equal(str, data); |
| 573 | free(str); |
| 574 | lyd_free_all(tree); |
| 575 | |
| 576 | /* wrong namespace, element name, whatever... */ |
| 577 | /* TODO */ |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 578 | |
| 579 | *state = NULL; |
| 580 | } |
| 581 | |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 582 | static void |
| 583 | test_reply(void **state) |
| 584 | { |
| 585 | *state = test_reply; |
| 586 | |
| 587 | const char *data; |
| 588 | char *str; |
| 589 | struct lyd_node *request, *tree, *op; |
| 590 | const struct lyd_node *node; |
| 591 | |
| 592 | data = |
| 593 | "<c xmlns=\"urn:tests:a\">" |
| 594 | "<act>" |
| 595 | "<al>value</al>" |
| 596 | "</act>" |
| 597 | "</c>"; |
| 598 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_rpc(ctx, data, &request, NULL)); |
| 599 | data = |
| 600 | "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" msgid=\"25\">" |
| 601 | "<al xmlns=\"urn:tests:a\">25</al>" |
| 602 | "</rpc-reply>"; |
| 603 | assert_int_equal(LY_SUCCESS, lyd_parse_xml_reply(request, data, &tree, &op)); |
| 604 | lyd_free_all(request); |
| 605 | |
| 606 | assert_non_null(op); |
| 607 | assert_string_equal(op->schema->name, "act"); |
| 608 | node = lyd_node_children(op); |
| 609 | assert_non_null(node->schema); |
| 610 | assert_string_equal(node->schema->name, "al"); |
| 611 | assert_true(node->schema->flags & LYS_CONFIG_R); |
| 612 | |
| 613 | assert_non_null(tree); |
| 614 | assert_null(tree->schema); |
| 615 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc-reply"); |
| 616 | assert_non_null(((struct lyd_node_opaq *)tree)->attr); |
| 617 | node = lyd_node_children(tree); |
| 618 | assert_non_null(node->schema); |
| 619 | assert_string_equal(node->schema->name, "c"); |
| 620 | |
| 621 | /* TODO print only rpc-reply node and then output subtree */ |
| 622 | lyd_print_mem(&str, lyd_node_children(op), LYD_XML, 0); |
| 623 | assert_string_equal(str, |
| 624 | "<al xmlns=\"urn:tests:a\">25</al>"); |
| 625 | free(str); |
| 626 | lyd_free_all(tree); |
| 627 | |
| 628 | /* wrong namespace, element name, whatever... */ |
| 629 | /* TODO */ |
| 630 | |
| 631 | *state = NULL; |
| 632 | } |
| 633 | |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 634 | int main(void) |
| 635 | { |
| 636 | const struct CMUnitTest tests[] = { |
| 637 | cmocka_unit_test_setup_teardown(test_leaf, setup, teardown), |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 638 | cmocka_unit_test_setup_teardown(test_anydata, setup, teardown), |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 639 | cmocka_unit_test_setup_teardown(test_list, setup, teardown), |
Radek Krejci | b6f7ae5 | 2019-07-19 10:31:42 +0200 | [diff] [blame] | 640 | cmocka_unit_test_setup_teardown(test_container, setup, teardown), |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 641 | cmocka_unit_test_setup_teardown(test_opaq, setup, teardown), |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 642 | cmocka_unit_test_setup_teardown(test_rpc, setup, teardown), |
Michal Vasko | a8edff0 | 2020-03-27 14:47:01 +0100 | [diff] [blame] | 643 | cmocka_unit_test_setup_teardown(test_action, setup, teardown), |
| 644 | cmocka_unit_test_setup_teardown(test_notification, setup, teardown), |
Michal Vasko | 1ce933a | 2020-03-30 12:38:22 +0200 | [diff] [blame] | 645 | cmocka_unit_test_setup_teardown(test_reply, setup, teardown), |
Radek Krejci | 509e259 | 2019-05-15 16:30:48 +0200 | [diff] [blame] | 646 | }; |
| 647 | |
| 648 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 649 | } |