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