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