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