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