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