Radek Krejci | 1798aae | 2020-07-14 13:26:06 +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 | |
| 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 | |
| 23 | #include "context.h" |
| 24 | #include "parser.h" |
| 25 | #include "parser_data.h" |
| 26 | #include "printer.h" |
| 27 | #include "printer_data.h" |
| 28 | #include "tests/config.h" |
| 29 | #include "tree_data_internal.h" |
| 30 | #include "tree_schema.h" |
| 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; import ietf-yang-metadata {prefix md;}" |
| 65 | "md:annotation hint { type int8;}" |
| 66 | "list l1 { key \"a b c\"; leaf a {type string;} leaf b {type string;} leaf c {type int16;} leaf d {type string;}}" |
| 67 | "leaf foo { type string;}" |
| 68 | "container c {" |
| 69 | "leaf x {type string;}" |
| 70 | "action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }" |
| 71 | "notification n1 { leaf nl {type string;} }" |
| 72 | "}" |
| 73 | "container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}" |
| 74 | "anydata any {config false;}" |
| 75 | "leaf-list ll1 { type uint8; }" |
| 76 | "leaf foo2 { type string; default \"default-val\"; }" |
| 77 | "leaf foo3 { type uint32; }" |
| 78 | "notification n2;}"; |
| 79 | const struct lys_module *mod; |
| 80 | |
| 81 | #if ENABLE_LOGGER_CHECKING |
| 82 | ly_set_log_clb(logger, 1); |
| 83 | #endif |
| 84 | |
| 85 | assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &ctx)); |
| 86 | assert_non_null(ly_ctx_load_module(ctx, "ietf-netconf-with-defaults", "2011-06-01")); |
| 87 | assert_non_null((mod = ly_ctx_load_module(ctx, "ietf-netconf", "2011-06-01"))); |
| 88 | assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "writable-running")); |
| 89 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_a, LYS_IN_YANG, NULL)); |
| 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 | |
| 123 | static void |
| 124 | test_leaf(void **state) |
| 125 | { |
| 126 | *state = test_leaf; |
| 127 | |
| 128 | const char *data = "{\"a:foo\":\"foo value\"}"; |
| 129 | struct lyd_node *tree; |
| 130 | struct lyd_node_term *leaf; |
| 131 | |
| 132 | char *printed; |
| 133 | struct ly_out *out; |
| 134 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
| 135 | |
| 136 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 137 | assert_non_null(tree); |
| 138 | assert_int_equal(LYS_LEAF, tree->schema->nodetype); |
| 139 | assert_string_equal("foo", tree->schema->name); |
| 140 | leaf = (struct lyd_node_term*)tree; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 141 | assert_string_equal("foo value", leaf->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 142 | |
| 143 | assert_int_equal(LYS_LEAF, tree->next->next->schema->nodetype); |
| 144 | assert_string_equal("foo2", tree->next->next->schema->name); |
| 145 | leaf = (struct lyd_node_term*)tree->next->next; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 146 | assert_string_equal("default-val", leaf->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 147 | assert_true(leaf->flags & LYD_DEFAULT); |
| 148 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 149 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 150 | assert_string_equal(printed, data); |
| 151 | ly_out_reset(out); |
| 152 | lyd_free_all(tree); |
| 153 | |
| 154 | /* make foo2 explicit */ |
| 155 | data = "{\"a:foo2\":\"default-val\"}"; |
| 156 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 157 | assert_non_null(tree); |
| 158 | assert_int_equal(LYS_LEAF, tree->schema->nodetype); |
| 159 | assert_string_equal("foo2", tree->schema->name); |
| 160 | leaf = (struct lyd_node_term*)tree; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 161 | assert_string_equal("default-val", leaf->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 162 | assert_false(leaf->flags & LYD_DEFAULT); |
| 163 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 164 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 165 | assert_string_equal(printed, data); |
| 166 | ly_out_reset(out); |
| 167 | lyd_free_all(tree); |
| 168 | |
| 169 | /* parse foo2 but make it implicit */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 170 | data = "{\"a:foo2\":\"default-val\",\"@a:foo2\":{\"ietf-netconf-with-defaults:default\":true}}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 171 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 172 | assert_non_null(tree); |
| 173 | assert_int_equal(LYS_LEAF, tree->schema->nodetype); |
| 174 | assert_string_equal("foo2", tree->schema->name); |
| 175 | leaf = (struct lyd_node_term*)tree; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 176 | assert_string_equal("default-val", leaf->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 177 | assert_true(leaf->flags & LYD_DEFAULT); |
| 178 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 179 | /* TODO default values |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 180 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 181 | assert_string_equal(printed, data); |
| 182 | ly_out_reset(out); |
| 183 | */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 184 | lyd_free_all(tree); |
| 185 | |
| 186 | /* multiple meatadata hint and unknown metadata xxx supposed to be skipped since it is from missing schema */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 187 | data = "{\"@a:foo\":{\"a:hint\":1,\"a:hint\":2,\"x:xxx\":{\"value\":\"/x:no/x:yes\"}},\"a:foo\":\"xxx\"}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 188 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 189 | assert_non_null(tree); |
| 190 | assert_int_equal(LYS_LEAF, tree->schema->nodetype); |
| 191 | assert_string_equal("foo", tree->schema->name); |
| 192 | assert_non_null(tree->meta); |
| 193 | assert_string_equal("hint", tree->meta->name); |
| 194 | assert_int_equal(LY_TYPE_INT8, tree->meta->value.realtype->basetype); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 195 | assert_string_equal("1", tree->meta->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 196 | assert_int_equal(1, tree->meta->value.int8); |
| 197 | assert_ptr_equal(tree, tree->meta->parent); |
| 198 | assert_non_null(tree->meta->next); |
| 199 | assert_string_equal("hint", tree->meta->next->name); |
| 200 | assert_int_equal(LY_TYPE_INT8, tree->meta->next->value.realtype->basetype); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 201 | assert_string_equal("2", tree->meta->next->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 202 | assert_int_equal(2, tree->meta->next->value.int8); |
| 203 | assert_ptr_equal(tree, tree->meta->next->parent); |
| 204 | assert_null(tree->meta->next->next); |
| 205 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 206 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 207 | assert_string_equal(printed, "{\"a:foo\":\"xxx\",\"@a:foo\":{\"a:hint\":1,\"a:hint\":2}}"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 208 | ly_out_free(out, NULL, 1); |
| 209 | lyd_free_all(tree); |
| 210 | |
| 211 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
| 212 | logbuf_assert("Unknown (or not implemented) YANG module \"x\" for metadata \"x:xxx\". /a:foo"); |
| 213 | |
| 214 | /* missing referenced metadata node */ |
| 215 | data = "{\"@a:foo\" : { \"a:hint\" : 1 }}"; |
| 216 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 217 | logbuf_assert("Missing JSON data instance to be coupled with @a:foo metadata. /"); |
| 218 | |
| 219 | /* missing namespace for meatadata*/ |
| 220 | data = "{\"a:foo\" : \"value\", \"@a:foo\" : { \"hint\" : 1 }}"; |
| 221 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 222 | logbuf_assert("Metadata in JSON must be namespace-qualified, missing prefix for \"hint\". /a:foo"); |
| 223 | |
| 224 | *state = NULL; |
| 225 | } |
| 226 | |
| 227 | static void |
| 228 | test_leaflist(void **state) |
| 229 | { |
| 230 | *state = test_leaflist; |
| 231 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 232 | const char *data = "{\"a:ll1\":[10,11]}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 233 | struct lyd_node *tree; |
| 234 | struct lyd_node_term *ll; |
| 235 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 236 | char *printed; |
| 237 | struct ly_out *out; |
| 238 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
| 239 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 240 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 241 | assert_non_null(tree); |
| 242 | assert_int_equal(LYS_LEAFLIST, tree->schema->nodetype); |
| 243 | assert_string_equal("ll1", tree->schema->name); |
| 244 | ll = (struct lyd_node_term*)tree; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 245 | assert_string_equal("10", ll->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 246 | |
| 247 | assert_non_null(tree->next); |
| 248 | assert_int_equal(LYS_LEAFLIST, tree->next->schema->nodetype); |
| 249 | assert_string_equal("ll1", tree->next->schema->name); |
| 250 | ll = (struct lyd_node_term*)tree->next; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 251 | assert_string_equal("11", ll->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 252 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 253 | lyd_print_all(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 254 | assert_string_equal(printed, data); |
| 255 | ly_out_reset(out); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 256 | lyd_free_all(tree); |
| 257 | |
| 258 | /* simple metadata */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 259 | data = "{\"a:ll1\":[10,11],\"@a:ll1\":[null,{\"a:hint\":2}]}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 260 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 261 | assert_non_null(tree); |
| 262 | assert_int_equal(LYS_LEAFLIST, tree->schema->nodetype); |
| 263 | assert_string_equal("ll1", tree->schema->name); |
| 264 | ll = (struct lyd_node_term*)tree; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 265 | assert_string_equal("10", ll->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 266 | assert_null(ll->meta); |
| 267 | |
| 268 | assert_non_null(tree->next); |
| 269 | assert_int_equal(LYS_LEAFLIST, tree->next->schema->nodetype); |
| 270 | assert_string_equal("ll1", tree->next->schema->name); |
| 271 | ll = (struct lyd_node_term*)tree->next; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 272 | assert_string_equal("11", ll->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 273 | assert_non_null(ll->meta); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 274 | assert_string_equal("2", ll->meta->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 275 | assert_null(ll->meta->next); |
| 276 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 277 | lyd_print_all(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 278 | assert_string_equal(printed, data); |
| 279 | ly_out_reset(out); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 280 | lyd_free_all(tree); |
| 281 | |
| 282 | /* multiple meatadata hint and unknown metadata xxx supposed to be skipped since it is from missing schema */ |
| 283 | data = "{\"@a:ll1\" : [{\"a:hint\" : 1, \"x:xxx\" : { \"value\" : \"/x:no/x:yes\" }, \"a:hint\" : 10},null,{\"a:hint\" : 3}], \"a:ll1\" : [1,2,3]}"; |
| 284 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 285 | assert_non_null(tree); |
| 286 | assert_int_equal(LYS_LEAFLIST, tree->schema->nodetype); |
| 287 | assert_string_equal("ll1", tree->schema->name); |
| 288 | ll = (struct lyd_node_term*)tree; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 289 | assert_string_equal("1", ll->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 290 | assert_non_null(ll->meta); |
| 291 | assert_string_equal("hint", ll->meta->name); |
| 292 | assert_int_equal(LY_TYPE_INT8, ll->meta->value.realtype->basetype); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 293 | assert_string_equal("1", ll->meta->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 294 | assert_int_equal(1, ll->meta->value.int8); |
| 295 | assert_ptr_equal(ll, ll->meta->parent); |
| 296 | assert_non_null(ll->meta->next); |
| 297 | assert_string_equal("hint", ll->meta->next->name); |
| 298 | assert_int_equal(LY_TYPE_INT8, ll->meta->next->value.realtype->basetype); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 299 | assert_string_equal("10", ll->meta->next->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 300 | assert_int_equal(10, ll->meta->next->value.int8); |
| 301 | assert_ptr_equal(ll, ll->meta->next->parent); |
| 302 | assert_null(ll->meta->next->next); |
| 303 | |
| 304 | assert_non_null(tree->next); |
| 305 | assert_int_equal(LYS_LEAFLIST, tree->next->schema->nodetype); |
| 306 | assert_string_equal("ll1", tree->next->schema->name); |
| 307 | ll = (struct lyd_node_term*)tree->next; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 308 | assert_string_equal("2", ll->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 309 | assert_null(ll->meta); |
| 310 | |
| 311 | assert_non_null(tree->next->next); |
| 312 | assert_int_equal(LYS_LEAFLIST, tree->next->next->schema->nodetype); |
| 313 | assert_string_equal("ll1", tree->next->next->schema->name); |
| 314 | ll = (struct lyd_node_term*)tree->next->next; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 315 | assert_string_equal("3", ll->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 316 | assert_non_null(ll->meta); |
| 317 | assert_string_equal("hint", ll->meta->name); |
| 318 | assert_int_equal(LY_TYPE_INT8, ll->meta->value.realtype->basetype); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 319 | assert_string_equal("3", ll->meta->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 320 | assert_int_equal(3, ll->meta->value.int8); |
| 321 | assert_ptr_equal(ll, ll->meta->parent); |
| 322 | assert_null(ll->meta->next); |
| 323 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 324 | lyd_print_all(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 325 | assert_string_equal(printed, "{\"a:ll1\":[1,2,3],\"@a:ll1\":[{\"a:hint\":1,\"a:hint\":10},null,{\"a:hint\":3}]}"); |
| 326 | ly_out_free(out, NULL, 1); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 327 | lyd_free_all(tree); |
| 328 | |
| 329 | /* missing referenced metadata node */ |
| 330 | data = "{\"@a:ll1\":[{\"a:hint\":1}]}"; |
| 331 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 332 | logbuf_assert("Missing JSON data instance to be coupled with @a:ll1 metadata. /"); |
| 333 | |
| 334 | data = "{\"a:ll1\":[1],\"@a:ll1\":[{\"a:hint\":1},{\"a:hint\":2}]}"; |
| 335 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 336 | logbuf_assert("Missing JSON data instance no. 2 of a:ll1 to be coupled with metadata. /"); |
| 337 | |
| 338 | data = "{\"@a:ll1\":[{\"a:hint\":1},{\"a:hint\":2},{\"a:hint\":3}],\"a:ll1\" : [1, 2]}"; |
| 339 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 340 | logbuf_assert("Missing 3rd JSON data instance to be coupled with @a:ll1 metadata. /"); |
| 341 | |
| 342 | *state = NULL; |
| 343 | } |
| 344 | |
| 345 | static void |
| 346 | test_anydata(void **state) |
| 347 | { |
| 348 | *state = test_anydata; |
| 349 | |
| 350 | const char *data; |
| 351 | char *str; |
| 352 | struct lyd_node *tree; |
| 353 | |
| 354 | struct ly_out *out; |
| 355 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out)); |
| 356 | |
| 357 | data = "{\"a:any\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}"; |
| 358 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 359 | assert_non_null(tree); |
| 360 | assert_int_equal(LYS_ANYDATA, tree->schema->nodetype); |
| 361 | assert_string_equal("any", tree->schema->name); |
| 362 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 363 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 364 | assert_string_equal(str, data); |
| 365 | ly_out_reset(out); |
| 366 | |
| 367 | lyd_free_all(tree); |
| 368 | ly_out_free(out, NULL, 1); |
| 369 | |
| 370 | *state = NULL; |
| 371 | } |
| 372 | |
| 373 | static void |
| 374 | test_list(void **state) |
| 375 | { |
| 376 | *state = test_list; |
| 377 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 378 | const char *data = "{\"a:l1\":[{\"a\":\"one\",\"b\":\"one\",\"c\":1}]}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 379 | struct lyd_node *tree, *iter; |
| 380 | struct lyd_node_inner *list; |
| 381 | struct lyd_node_term *leaf; |
| 382 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 383 | char *printed; |
| 384 | struct ly_out *out; |
| 385 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
| 386 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 387 | /* check hashes */ |
| 388 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 389 | assert_non_null(tree); |
| 390 | assert_int_equal(LYS_LIST, tree->schema->nodetype); |
| 391 | assert_string_equal("l1", tree->schema->name); |
| 392 | list = (struct lyd_node_inner*)tree; |
| 393 | LY_LIST_FOR(list->child, iter) { |
| 394 | assert_int_not_equal(0, iter->hash); |
| 395 | } |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 396 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 397 | lyd_print_all(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 398 | assert_string_equal(printed, data); |
| 399 | ly_out_reset(out); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 400 | lyd_free_all(tree); |
| 401 | |
| 402 | /* missing keys */ |
| 403 | data = "{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\"}]}"; |
| 404 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 405 | logbuf_assert("List instance is missing its key \"a\". /a:l1[b='b'][c='1']"); |
| 406 | |
| 407 | data = "{ \"a:l1\": [ {\"a\" : \"a\"}]}"; |
| 408 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 409 | logbuf_assert("List instance is missing its key \"b\". /a:l1[a='a']"); |
| 410 | |
| 411 | data = "{ \"a:l1\": [ {\"b\" : \"b\", \"a\" : \"a\"}]}"; |
| 412 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 413 | logbuf_assert("List instance is missing its key \"c\". /a:l1[a='a'][b='b']"); |
| 414 | |
| 415 | /* key duplicate */ |
| 416 | data = "{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\", \"a\" : \"a\", \"c\" : 1}]}"; |
| 417 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 418 | logbuf_assert("Duplicate instance of \"c\". /a:l1[a='a'][b='b'][c='1'][c='1']/c"); |
| 419 | |
| 420 | /* keys order, in contrast to XML, JSON accepts keys in any order even in strict mode */ |
| 421 | logbuf_clean(); |
| 422 | data = "{ \"a:l1\": [ {\"d\" : \"d\", \"a\" : \"a\", \"c\" : 1, \"b\" : \"b\"}]}"; |
| 423 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 424 | assert_non_null(tree); |
| 425 | assert_int_equal(LYS_LIST, tree->schema->nodetype); |
| 426 | assert_string_equal("l1", tree->schema->name); |
| 427 | list = (struct lyd_node_inner*)tree; |
| 428 | assert_non_null(leaf = (struct lyd_node_term*)list->child); |
| 429 | assert_string_equal("a", leaf->schema->name); |
| 430 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 431 | assert_string_equal("b", leaf->schema->name); |
| 432 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 433 | assert_string_equal("c", leaf->schema->name); |
| 434 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 435 | assert_string_equal("d", leaf->schema->name); |
| 436 | logbuf_assert(""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 437 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 438 | lyd_print_all(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 439 | assert_string_equal(printed, "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1,\"d\":\"d\"}]}"); |
| 440 | ly_out_reset(out); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 441 | lyd_free_all(tree); |
| 442 | |
| 443 | /* */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 444 | data = "{\"a:l1\":[{\"c\":1,\"b\":\"b\",\"a\":\"a\"}]}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 445 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree)); |
| 446 | assert_non_null(tree); |
| 447 | assert_int_equal(LYS_LIST, tree->schema->nodetype); |
| 448 | assert_string_equal("l1", tree->schema->name); |
| 449 | list = (struct lyd_node_inner*)tree; |
| 450 | assert_non_null(leaf = (struct lyd_node_term*)list->child); |
| 451 | assert_string_equal("a", leaf->schema->name); |
| 452 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 453 | assert_string_equal("b", leaf->schema->name); |
| 454 | assert_non_null(leaf = (struct lyd_node_term*)leaf->next); |
| 455 | assert_string_equal("c", leaf->schema->name); |
| 456 | logbuf_assert(""); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 457 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 458 | lyd_print_all(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 459 | assert_string_equal(printed, "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1}]}"); |
| 460 | ly_out_reset(out); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 461 | lyd_free_all(tree); |
| 462 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 463 | data = "{\"a:cp\":{\"@\":{\"a:hint\":1}}}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 464 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 465 | assert_non_null(tree); |
| 466 | assert_int_equal(LYS_CONTAINER, tree->schema->nodetype); |
| 467 | assert_string_equal("cp", tree->schema->name); |
| 468 | assert_non_null(tree->meta); |
| 469 | assert_string_equal("hint", tree->meta->name); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 470 | assert_string_equal("1", tree->meta->value.canonical); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 471 | assert_ptr_equal(tree, tree->meta->parent); |
| 472 | assert_null(tree->meta->next); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 473 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 474 | lyd_print_all(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 475 | assert_string_equal(printed, data); |
| 476 | ly_out_free(out, NULL, 1); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 477 | lyd_free_all(tree); |
| 478 | |
| 479 | *state = NULL; |
| 480 | } |
| 481 | |
| 482 | static void |
| 483 | test_container(void **state) |
| 484 | { |
| 485 | *state = test_container; |
| 486 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 487 | const char *data = "{\"a:c\":{}}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 488 | struct lyd_node *tree; |
| 489 | struct lyd_node_inner *cont; |
| 490 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 491 | char *printed; |
| 492 | struct ly_out *out; |
| 493 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
| 494 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 495 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 496 | assert_non_null(tree); |
| 497 | assert_int_equal(LYS_CONTAINER, tree->schema->nodetype); |
| 498 | assert_string_equal("c", tree->schema->name); |
| 499 | cont = (struct lyd_node_inner*)tree; |
| 500 | assert_true(cont->flags & LYD_DEFAULT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 501 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 502 | lyd_print_all(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 503 | assert_string_equal(printed, "{}"); |
| 504 | ly_out_reset(out); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 505 | lyd_free_all(tree); |
| 506 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 507 | data = "{\"a:cp\":{}}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 508 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 509 | assert_non_null(tree); |
| 510 | assert_int_equal(LYS_CONTAINER, tree->schema->nodetype); |
| 511 | assert_string_equal("cp", tree->schema->name); |
| 512 | cont = (struct lyd_node_inner*)tree; |
| 513 | assert_false(cont->flags & LYD_DEFAULT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 514 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 515 | lyd_print_all(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 516 | assert_string_equal(printed, data); |
| 517 | ly_out_free(out, NULL, 1); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 518 | lyd_free_all(tree); |
| 519 | |
| 520 | *state = NULL; |
| 521 | } |
| 522 | |
| 523 | static void |
| 524 | test_opaq(void **state) |
| 525 | { |
| 526 | *state = test_opaq; |
| 527 | |
| 528 | const char *data; |
| 529 | char *str; |
| 530 | struct lyd_node *tree; |
| 531 | |
| 532 | struct ly_out *out; |
| 533 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out)); |
| 534 | |
| 535 | /* invalid value, no flags */ |
| 536 | data = "{\"a:foo3\":[null]}"; |
| 537 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 538 | logbuf_assert("Invalid empty uint32 value. /a:foo3"); |
| 539 | assert_null(tree); |
| 540 | |
| 541 | /* opaq flag */ |
| 542 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree)); |
| 543 | assert_non_null(tree); |
| 544 | assert_null(tree->schema); |
| 545 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "foo3"); |
| 546 | assert_string_equal(((struct lyd_node_opaq *)tree)->value, ""); |
| 547 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 548 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 549 | assert_string_equal(str, data); |
| 550 | ly_out_reset(out); |
| 551 | lyd_free_all(tree); |
| 552 | |
| 553 | /* missing key, no flags */ |
| 554 | data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"d\":\"val_d\"}]}"; |
| 555 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 556 | logbuf_assert("List instance is missing its key \"c\". /a:l1[a='val_a'][b='val_b']"); |
| 557 | assert_null(tree); |
| 558 | |
| 559 | /* opaq flag */ |
| 560 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree)); |
| 561 | assert_non_null(tree); |
| 562 | assert_null(tree->schema); |
| 563 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1"); |
| 564 | assert_string_equal(((struct lyd_node_opaq *)tree)->value, ""); |
| 565 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 566 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 567 | assert_string_equal(str, data); |
| 568 | ly_out_reset(out); |
| 569 | lyd_free_all(tree); |
| 570 | |
| 571 | /* invalid key, no flags */ |
| 572 | data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"; |
| 573 | assert_int_equal(LY_EVALID, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
| 574 | logbuf_assert("Invalid int16 value \"val_c\". /a:l1/c"); |
| 575 | assert_null(tree); |
| 576 | |
| 577 | /* opaq flag */ |
| 578 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree)); |
| 579 | assert_non_null(tree); |
| 580 | assert_null(tree->schema); |
| 581 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1"); |
| 582 | assert_string_equal(((struct lyd_node_opaq *)tree)->value, ""); |
| 583 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 584 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 585 | assert_string_equal(str, data); |
| 586 | ly_out_reset(out); |
| 587 | lyd_free_all(tree); |
| 588 | |
| 589 | data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":{\"val\":\"val_c\"}}]}"; |
| 590 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree)); |
| 591 | assert_non_null(tree); |
| 592 | assert_null(tree->schema); |
| 593 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1"); |
| 594 | assert_string_equal(((struct lyd_node_opaq *)tree)->value, ""); |
| 595 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 596 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 597 | assert_string_equal(str, data); |
| 598 | ly_out_reset(out); |
| 599 | lyd_free_all(tree); |
| 600 | |
| 601 | data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\"}]}"; |
| 602 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &tree)); |
| 603 | assert_non_null(tree); |
| 604 | assert_null(tree->schema); |
| 605 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "l1"); |
| 606 | assert_string_equal(((struct lyd_node_opaq *)tree)->value, ""); |
| 607 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 608 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 609 | assert_string_equal(str, data); |
| 610 | ly_out_reset(out); |
| 611 | lyd_free_all(tree); |
| 612 | |
| 613 | ly_out_free(out, NULL, 1); |
| 614 | |
| 615 | *state = NULL; |
| 616 | } |
| 617 | |
| 618 | static void |
| 619 | test_rpc(void **state) |
| 620 | { |
| 621 | *state = test_rpc; |
| 622 | |
| 623 | const char *data; |
| 624 | struct ly_in *in; |
| 625 | char *str; |
| 626 | struct lyd_node *tree, *op; |
| 627 | const struct lyd_node *node; |
| 628 | |
| 629 | struct ly_out *out; |
| 630 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out)); |
| 631 | |
| 632 | data = "{\"ietf-netconf:rpc\":{\"edit-config\":{" |
| 633 | "\"target\":{\"running\":[null]}," |
| 634 | "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}}," |
| 635 | "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}" |
| 636 | "}}}"; |
| 637 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 638 | assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_JSON, &tree, &op)); |
| 639 | ly_in_free(in, 0); |
| 640 | |
| 641 | assert_non_null(op); |
| 642 | assert_string_equal(op->schema->name, "edit-config"); |
| 643 | |
| 644 | assert_non_null(tree); |
| 645 | assert_null(tree->schema); |
| 646 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc"); |
| 647 | /* TODO support generic attributes in JSON ? |
| 648 | assert_non_null(((struct lyd_node_opaq *)tree)->attr); |
| 649 | */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame^] | 650 | node = lyd_child(tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 651 | assert_string_equal(node->schema->name, "edit-config"); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame^] | 652 | node = lyd_child(node)->next; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 653 | assert_string_equal(node->schema->name, "config"); |
| 654 | |
| 655 | node = ((struct lyd_node_any *)node)->value.tree; |
| 656 | assert_non_null(node->schema); |
| 657 | assert_string_equal(node->schema->name, "cp"); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame^] | 658 | node = lyd_child(node); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 659 | /* z has no value */ |
| 660 | assert_null(node->schema); |
| 661 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "z"); |
| 662 | node = node->parent->next; |
| 663 | /* l1 key c has invalid value so it is at the end */ |
| 664 | assert_null(node->schema); |
| 665 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "l1"); |
| 666 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 667 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 668 | assert_string_equal(str, data); |
| 669 | ly_out_reset(out); |
| 670 | lyd_free_all(tree); |
| 671 | |
| 672 | /* wrong namespace, element name, whatever... */ |
| 673 | /* TODO */ |
| 674 | |
| 675 | ly_out_free(out, NULL, 1); |
| 676 | |
| 677 | *state = NULL; |
| 678 | } |
| 679 | |
| 680 | static void |
| 681 | test_action(void **state) |
| 682 | { |
| 683 | *state = test_action; |
| 684 | |
| 685 | const char *data; |
| 686 | struct ly_in *in; |
| 687 | char *str; |
| 688 | struct lyd_node *tree, *op; |
| 689 | const struct lyd_node *node; |
| 690 | |
| 691 | struct ly_out *out; |
| 692 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out)); |
| 693 | |
| 694 | data = "{\"ietf-netconf:rpc\":{\"yang:action\":{\"a:c\":{\"act\":{\"al\":\"value\"}}}}}"; |
| 695 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 696 | assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_JSON, &tree, &op)); |
| 697 | ly_in_free(in, 0); |
| 698 | |
| 699 | assert_non_null(op); |
| 700 | assert_string_equal(op->schema->name, "act"); |
| 701 | |
| 702 | assert_non_null(tree); |
| 703 | assert_null(tree->schema); |
| 704 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc"); |
| 705 | assert_null(((struct lyd_node_opaq *)tree)->attr); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame^] | 706 | node = lyd_child(tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 707 | assert_null(node->schema); |
| 708 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "action"); |
| 709 | assert_null(((struct lyd_node_opaq *)node)->attr); |
| 710 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 711 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 712 | assert_string_equal(str, data); |
| 713 | ly_out_reset(out); |
| 714 | lyd_free_all(tree); |
| 715 | |
| 716 | /* wrong namespace, element name, whatever... */ |
| 717 | /* TODO */ |
| 718 | |
| 719 | ly_out_free(out, NULL, 1); |
| 720 | |
| 721 | *state = NULL; |
| 722 | } |
| 723 | |
| 724 | static void |
| 725 | test_notification(void **state) |
| 726 | { |
| 727 | *state = test_notification; |
| 728 | |
| 729 | const char *data; |
| 730 | struct ly_in *in; |
| 731 | char *str; |
| 732 | struct lyd_node *tree, *ntf; |
| 733 | const struct lyd_node *node; |
| 734 | |
| 735 | struct ly_out *out; |
| 736 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out)); |
| 737 | |
| 738 | data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2037-07-08T00:01:00Z\",\"a:c\":{\"n1\":{\"nl\":\"value\"}}}}"; |
| 739 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 740 | assert_int_equal(LY_SUCCESS, lyd_parse_notif(ctx, in, LYD_JSON, &tree, &ntf)); |
| 741 | ly_in_free(in, 0); |
| 742 | |
| 743 | assert_non_null(ntf); |
| 744 | assert_string_equal(ntf->schema->name, "n1"); |
| 745 | |
| 746 | assert_non_null(tree); |
| 747 | assert_null(tree->schema); |
| 748 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "notification"); |
| 749 | assert_null(((struct lyd_node_opaq *)tree)->attr); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame^] | 750 | node = lyd_child(tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 751 | assert_null(node->schema); |
| 752 | assert_string_equal(((struct lyd_node_opaq *)node)->name, "eventTime"); |
| 753 | assert_string_equal(((struct lyd_node_opaq *)node)->value, "2037-07-08T00:01:00Z"); |
| 754 | assert_null(((struct lyd_node_opaq *)node)->attr); |
| 755 | node = node->next; |
| 756 | assert_non_null(node->schema); |
| 757 | assert_string_equal(node->schema->name, "c"); |
| 758 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 759 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 760 | assert_string_equal(str, data); |
| 761 | ly_out_reset(out); |
| 762 | lyd_free_all(tree); |
| 763 | |
| 764 | /* top-level notif without envelope */ |
| 765 | data = "{\"a:n2\":{}}"; |
| 766 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 767 | assert_int_equal(LY_SUCCESS, lyd_parse_notif(ctx, in, LYD_JSON, &tree, &ntf)); |
| 768 | ly_in_free(in, 0); |
| 769 | |
| 770 | assert_non_null(ntf); |
| 771 | assert_string_equal(ntf->schema->name, "n2"); |
| 772 | |
| 773 | assert_non_null(tree); |
| 774 | assert_ptr_equal(ntf, tree); |
| 775 | |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 776 | lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 777 | assert_string_equal(str, data); |
| 778 | ly_out_reset(out); |
| 779 | lyd_free_all(tree); |
| 780 | |
| 781 | /* wrong namespace, element name, whatever... */ |
| 782 | /* TODO */ |
| 783 | |
| 784 | ly_out_free(out, NULL, 1); |
| 785 | |
| 786 | *state = NULL; |
| 787 | } |
| 788 | |
| 789 | static void |
| 790 | test_reply(void **state) |
| 791 | { |
| 792 | *state = test_reply; |
| 793 | |
| 794 | const char *data; |
| 795 | struct ly_in *in; |
| 796 | char *str; |
| 797 | struct lyd_node *request, *tree, *op; |
| 798 | const struct lyd_node *node; |
| 799 | |
| 800 | struct ly_out *out; |
| 801 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&str, 0, &out)); |
| 802 | |
| 803 | data = "{\"a:c\":{\"act\":{\"al\":\"value\"}}}"; |
| 804 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 805 | assert_int_equal(LY_SUCCESS, lyd_parse_rpc(ctx, in, LYD_JSON, &request, NULL)); |
| 806 | ly_in_free(in, 0); |
| 807 | |
| 808 | data = "{\"ietf-netconf:rpc-reply\":{\"a:al\":25}}"; |
| 809 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 810 | assert_int_equal(LY_SUCCESS, lyd_parse_reply(request, in, LYD_JSON, &tree, &op)); |
| 811 | ly_in_free(in, 0); |
| 812 | lyd_free_all(request); |
| 813 | |
| 814 | assert_non_null(op); |
| 815 | assert_string_equal(op->schema->name, "act"); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame^] | 816 | node = lyd_child(op); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 817 | assert_non_null(node->schema); |
| 818 | assert_string_equal(node->schema->name, "al"); |
| 819 | assert_true(node->schema->flags & LYS_CONFIG_R); |
| 820 | |
| 821 | assert_non_null(tree); |
| 822 | assert_null(tree->schema); |
| 823 | assert_string_equal(((struct lyd_node_opaq *)tree)->name, "rpc-reply"); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame^] | 824 | node = lyd_child(tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 825 | assert_non_null(node->schema); |
| 826 | assert_string_equal(node->schema->name, "c"); |
| 827 | |
| 828 | /* TODO print only rpc-reply node and then output subtree */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame^] | 829 | lyd_print_tree(out, lyd_child(op), LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 830 | assert_string_equal(str, "{\"a:al\":25}"); |
| 831 | ly_out_reset(out); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame^] | 832 | lyd_print_tree(out, lyd_child(tree), LYD_JSON, LYD_PRINT_SHRINK); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 833 | assert_string_equal(str, "{\"a:c\":{\"act\":{\"al\":25}}}"); |
| 834 | ly_out_reset(out); |
| 835 | lyd_free_all(tree); |
| 836 | |
| 837 | /* wrong namespace, element name, whatever... */ |
| 838 | /* TODO */ |
| 839 | |
| 840 | ly_out_free(out, NULL, 1); |
| 841 | |
| 842 | *state = NULL; |
| 843 | } |
| 844 | |
| 845 | int main(void) |
| 846 | { |
| 847 | const struct CMUnitTest tests[] = { |
| 848 | cmocka_unit_test_setup_teardown(test_leaf, setup, teardown), |
| 849 | cmocka_unit_test_setup_teardown(test_leaflist, setup, teardown), |
| 850 | cmocka_unit_test_setup_teardown(test_anydata, setup, teardown), |
| 851 | cmocka_unit_test_setup_teardown(test_list, setup, teardown), |
| 852 | cmocka_unit_test_setup_teardown(test_container, setup, teardown), |
| 853 | cmocka_unit_test_setup_teardown(test_opaq, setup, teardown), |
| 854 | cmocka_unit_test_setup_teardown(test_rpc, setup, teardown), |
| 855 | cmocka_unit_test_setup_teardown(test_action, setup, teardown), |
| 856 | cmocka_unit_test_setup_teardown(test_notification, setup, teardown), |
| 857 | cmocka_unit_test_setup_teardown(test_reply, setup, teardown), |
| 858 | }; |
| 859 | |
| 860 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 861 | } |