Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 1 | /** |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file test_parser_json.c |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 5 | * @brief unit tests for JSON parser |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 6 | * |
Michal Vasko | 5b414dd | 2023-04-13 10:29:58 +0200 | [diff] [blame] | 7 | * Copyright (c) 2019 - 2023 CESNET, z.s.p.o. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 15 | #define _UTEST_MAIN_ |
| 16 | #include "utests.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 17 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 18 | #include "context.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 19 | #include "in.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 20 | #include "out.h" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 21 | #include "parser_data.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 22 | #include "printer_data.h" |
Radek Krejci | ef5f767 | 2021-04-01 17:04:12 +0200 | [diff] [blame] | 23 | #include "tests_config.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 24 | #include "tree_data_internal.h" |
| 25 | #include "tree_schema.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 26 | |
| 27 | static int |
| 28 | setup(void **state) |
| 29 | { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 30 | const char *schema = "module a {namespace urn:tests:a;prefix a;yang-version 1.1; import ietf-yang-metadata {prefix md;}" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 31 | "md:annotation hint { type int8;}" |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 32 | "list l1 { key \"a b c\"; leaf a {type string;} leaf b {type string;} leaf c {type int16;}" |
| 33 | " leaf d {type string;}" |
| 34 | " container cont {leaf e {type boolean;}}" |
| 35 | "}" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 36 | "leaf foo { type string;}" |
| 37 | "container c {" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 38 | " leaf x {type string;}" |
| 39 | " action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }" |
| 40 | " notification n1 { leaf nl {type string;} }" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 41 | "}" |
| 42 | "container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}" |
| 43 | "anydata any {config false;}" |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 44 | "anyxml axml;" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 45 | "leaf-list ll1 { type uint8; }" |
| 46 | "leaf foo2 { type string; default \"default-val\"; }" |
| 47 | "leaf foo3 { type uint32; }" |
Michal Vasko | 583b464 | 2023-05-25 10:39:34 +0200 | [diff] [blame] | 48 | "leaf foo4 { type uint64; }" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 49 | "notification n2;}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 50 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 51 | UTEST_SETUP; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 52 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 53 | UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL); |
| 54 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 59 | #define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \ |
| 60 | CHECK_PARSE_LYD_PARAM(INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 61 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 62 | #define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH) \ |
| 63 | assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\ |
| 64 | CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH);\ |
| 65 | assert_null(MODEL) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 66 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 67 | #define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \ |
| 68 | CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_JSON, PRINT_OPTION) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 69 | |
| 70 | static void |
| 71 | test_leaf(void **state) |
| 72 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 73 | struct lyd_node *tree; |
| 74 | struct lyd_node_term *leaf; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 75 | const char *data; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 76 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 77 | assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 78 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 79 | data = "{\"a:foo\":\"foo value\"}"; |
| 80 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 81 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 82 | leaf = (struct lyd_node_term *)tree; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 83 | CHECK_LYD_VALUE(leaf->value, STRING, "foo value"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 84 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 85 | CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2", |
| 86 | 1, LYS_LEAF, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 87 | leaf = (struct lyd_node_term *)tree->next->next; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 88 | |
| 89 | CHECK_LYD_VALUE(leaf->value, STRING, "default-val"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 90 | assert_true(leaf->flags & LYD_DEFAULT); |
| 91 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 92 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 93 | lyd_free_all(tree); |
| 94 | |
| 95 | /* make foo2 explicit */ |
| 96 | data = "{\"a:foo2\":\"default-val\"}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 97 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 98 | assert_non_null(tree); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 99 | tree = tree->next; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 100 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2", |
| 101 | 1, LYS_LEAF, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 102 | leaf = (struct lyd_node_term *)tree; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 103 | CHECK_LYD_VALUE(leaf->value, STRING, "default-val"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 104 | assert_false(leaf->flags & LYD_DEFAULT); |
| 105 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 106 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 107 | lyd_free_all(tree); |
| 108 | |
| 109 | /* parse foo2 but make it implicit */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 110 | data = "{\"a:foo2\":\"default-val\",\"@a:foo2\":{\"ietf-netconf-with-defaults:default\":true}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 111 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 112 | assert_non_null(tree); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 113 | tree = tree->next; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 114 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2", |
| 115 | 1, LYS_LEAF, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 116 | leaf = (struct lyd_node_term *)tree; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 117 | CHECK_LYD_VALUE(leaf->value, STRING, "default-val"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 118 | assert_true(leaf->flags & LYD_DEFAULT); |
| 119 | |
Michal Vasko | b68c3b4 | 2022-05-20 10:36:40 +0200 | [diff] [blame] | 120 | /* print default values */ |
| 121 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL_TAG, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 122 | lyd_free_all(tree); |
| 123 | |
Michal Vasko | a4af625 | 2022-05-20 10:38:54 +0200 | [diff] [blame] | 124 | /* skip leaf */ |
| 125 | data = "{\"a:cp\":{\"x\":\"val\",\"y\":\"valy\",\"z\":5}}"; |
| 126 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 127 | assert_non_null(tree); |
| 128 | |
| 129 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:cp\":{\"y\":\"valy\",\"z\":5}}"); |
| 130 | lyd_free_all(tree); |
| 131 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 132 | /* 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] | 133 | data = "{\"@a:foo\":{\"a:hint\":1,\"a:hint\":2,\"x:xxx\":{\"value\":\"/x:no/x:yes\"}},\"a:foo\":\"xxx\"}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 134 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 135 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0); |
| 136 | CHECK_LYD_META(tree->meta, 1, "hint", 1, 1, INT8, "1", 1); |
| 137 | CHECK_LYD_META(tree->meta->next, 1, "hint", 0, 1, INT8, "2", 2); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 138 | assert_null(tree->meta->next->next); |
| 139 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 140 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, |
| 141 | "{\"a:foo\":\"xxx\",\"@a:foo\":{\"a:hint\":1,\"a:hint\":2}}"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 142 | lyd_free_all(tree); |
| 143 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 144 | PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 145 | "Unknown (or not implemented) YANG module \"x\" of metadata \"x:xxx\".", "Data location \"/@a:foo\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 146 | |
| 147 | /* missing referenced metadata node */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 148 | PARSER_CHECK_ERROR("{\"@a:foo\" : { \"a:hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 149 | "Missing JSON data instance to be coupled with @a:foo metadata.", "Data location \"/@a:foo\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 150 | |
| 151 | /* missing namespace for meatadata*/ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 152 | PARSER_CHECK_ERROR("{\"a:foo\" : \"value\", \"@a:foo\" : { \"hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 153 | "Metadata in JSON must be namespace-qualified, missing prefix for \"hint\".", |
| 154 | "Schema location \"/a:foo\", line number 1."); |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame] | 155 | |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 156 | /* invalid JSON type */ |
| 157 | data = "{\"a:l1\" : [{ \"a\" : \"val-a\", \"b\" : \"val-b\", \"c\" : 1, \"cont\" : { \"e\" : \"0\" } }]}"; |
| 158 | PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
| 159 | "Invalid non-boolean-encoded boolean value \"0\".", |
| 160 | "Data location \"/a:l1[a='val-a'][b='val-b'][c='1']/cont/e\", line number 1."); |
| 161 | |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame] | 162 | /* reverse solidus in JSON object member name */ |
| 163 | data = "{\"@a:foo\":{\"a:hi\\nt\":1},\"a:foo\":\"xxx\"}"; |
| 164 | assert_int_equal(LY_EINVAL, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); |
Michal Vasko | 62af369 | 2023-02-09 14:00:09 +0100 | [diff] [blame] | 165 | CHECK_LOG_CTX("Annotation definition for attribute \"a:hi\nt\" not found.", "Data location \"/@a:foo\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static void |
| 169 | test_leaflist(void **state) |
| 170 | { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 171 | const char *data; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 172 | struct lyd_node *tree; |
| 173 | struct lyd_node_term *ll; |
| 174 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 175 | data = "{\"a:ll1\":[10,11]}"; |
| 176 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 177 | assert_non_null(tree); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 178 | tree = tree->next; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 179 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1", |
| 180 | 1, LYS_LEAFLIST, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 181 | ll = (struct lyd_node_term *)tree; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 182 | CHECK_LYD_VALUE(ll->value, UINT8, "10", 10); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 183 | |
| 184 | assert_non_null(tree->next); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 185 | CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1", |
| 186 | 1, LYS_LEAFLIST, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 187 | ll = (struct lyd_node_term *)tree->next; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 188 | CHECK_LYD_VALUE(ll->value, UINT8, "11", 11); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 189 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 190 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 191 | lyd_free_all(tree); |
| 192 | |
Michal Vasko | 4e26adc | 2022-03-30 11:01:16 +0200 | [diff] [blame] | 193 | /* accept empty */ |
| 194 | data = "{\"a:ll1\":[]}"; |
| 195 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 196 | assert_null(tree); |
| 197 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 198 | /* simple metadata */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 199 | data = "{\"a:ll1\":[10,11],\"@a:ll1\":[null,{\"a:hint\":2}]}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 200 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 201 | assert_non_null(tree); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 202 | tree = tree->next; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 203 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1", |
| 204 | 1, LYS_LEAFLIST, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 205 | ll = (struct lyd_node_term *)tree; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 206 | CHECK_LYD_VALUE(ll->value, UINT8, "10", 10); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 207 | assert_null(ll->meta); |
| 208 | |
| 209 | assert_non_null(tree->next); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 210 | CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1", |
| 211 | 1, LYS_LEAFLIST, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 212 | ll = (struct lyd_node_term *)tree->next; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 213 | CHECK_LYD_VALUE(ll->value, UINT8, "11", 11); |
| 214 | CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "2", 2); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 215 | assert_null(ll->meta->next); |
| 216 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 217 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 218 | lyd_free_all(tree); |
| 219 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 220 | /* multiple meatadata hint and unknown metadata xxx supposed to be skipped since it is from missing schema */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 221 | data = "{\"@a:ll1\" : [{\"a:hint\" : 1, \"x:xxx\" : { \"value\" : \"/x:no/x:yes\" }, " |
| 222 | "\"a:hint\" : 10},null,{\"a:hint\" : 3}], \"a:ll1\" : [1,2,3]}"; |
| 223 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 224 | assert_non_null(tree); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 225 | tree = tree->next; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 226 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1", |
| 227 | 1, LYS_LEAFLIST, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 228 | ll = (struct lyd_node_term *)tree; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 229 | CHECK_LYD_VALUE(ll->value, UINT8, "1", 1); |
| 230 | CHECK_LYD_META(ll->meta, 1, "hint", 1, 1, INT8, "1", 1); |
| 231 | CHECK_LYD_META(ll->meta->next, 1, "hint", 0, 1, INT8, "10", 10); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 232 | |
| 233 | assert_non_null(tree->next); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 234 | CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1", |
| 235 | 1, LYS_LEAFLIST, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 236 | ll = (struct lyd_node_term *)tree->next; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 237 | CHECK_LYD_VALUE(ll->value, UINT8, "2", 2); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 238 | assert_null(ll->meta); |
| 239 | |
| 240 | assert_non_null(tree->next->next); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 241 | CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1", |
| 242 | 1, LYS_LEAFLIST, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 243 | ll = (struct lyd_node_term *)tree->next->next; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 244 | CHECK_LYD_VALUE(ll->value, UINT8, "3", 3); |
| 245 | CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "3", 3); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 246 | assert_null(ll->meta->next); |
| 247 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 248 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, |
| 249 | "{\"a:ll1\":[1,2,3],\"@a:ll1\":[{\"a:hint\":1,\"a:hint\":10},null,{\"a:hint\":3}]}"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 250 | lyd_free_all(tree); |
| 251 | |
| 252 | /* missing referenced metadata node */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 253 | PARSER_CHECK_ERROR("{\"@a:ll1\":[{\"a:hint\":1}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 254 | "Missing JSON data instance to be coupled with @a:ll1 metadata.", "Data location \"/@a:ll1\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 255 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 256 | PARSER_CHECK_ERROR("{\"a:ll1\":[1],\"@a:ll1\":[{\"a:hint\":1},{\"a:hint\":2}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 257 | "Missing JSON data instance #2 of a:ll1 to be coupled with metadata.", "Schema location \"/a:ll1\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 258 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 259 | PARSER_CHECK_ERROR("{\"@a:ll1\":[{\"a:hint\":1},{\"a:hint\":2},{\"a:hint\":3}],\"a:ll1\" : [1, 2]}", 0, LYD_VALIDATE_PRESENT, |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 260 | tree, LY_EVALID, "Missing JSON data instance #3 to be coupled with @a:ll1 metadata.", |
| 261 | "Data location \"/@a:ll1\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static void |
| 265 | test_anydata(void **state) |
| 266 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 267 | const char *data; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 268 | struct lyd_node *tree; |
| 269 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 270 | data = "{\"a:any\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 271 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 272 | assert_non_null(tree); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 273 | tree = tree->next; |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 274 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any", |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 275 | 1, LYS_ANYDATA, 0, 0, NULL, 0); |
| 276 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 277 | lyd_free_all(tree); |
Michal Vasko | fbc4a25 | 2022-02-03 13:15:58 +0100 | [diff] [blame] | 278 | |
| 279 | data = "{\"a:any\":{}}"; |
| 280 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 281 | assert_non_null(tree); |
| 282 | tree = tree->next; |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 283 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any", |
Michal Vasko | fbc4a25 | 2022-02-03 13:15:58 +0100 | [diff] [blame] | 284 | 1, LYS_ANYDATA, 0, 0, NULL, 0); |
| 285 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 286 | lyd_free_all(tree); |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 287 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 288 | data = "{\"a:any\":{\"node\":20}}"; |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 289 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 290 | assert_non_null(tree); |
| 291 | tree = tree->next; |
| 292 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any", |
| 293 | 1, LYS_ANYDATA, 0, 0, NULL, 0); |
| 294 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 295 | lyd_free_all(tree); |
| 296 | } |
| 297 | |
| 298 | static void |
| 299 | test_anyxml(void **state) |
| 300 | { |
| 301 | const char *data; |
| 302 | struct lyd_node *tree; |
| 303 | |
| 304 | data = "{\"a:axml\":\"some-value in string\"}"; |
| 305 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 306 | assert_non_null(tree); |
| 307 | tree = tree->next; |
| 308 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0); |
| 309 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 310 | lyd_free_all(tree); |
| 311 | |
| 312 | data = "{\"a:axml\":\"\"}"; |
| 313 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 314 | assert_non_null(tree); |
| 315 | tree = tree->next; |
| 316 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0); |
| 317 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 318 | lyd_free_all(tree); |
| 319 | |
| 320 | data = "{\"a:axml\":55}"; |
| 321 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 322 | assert_non_null(tree); |
| 323 | tree = tree->next; |
| 324 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0); |
| 325 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 326 | lyd_free_all(tree); |
| 327 | |
| 328 | data = "{\"a:axml\":false}"; |
| 329 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 330 | assert_non_null(tree); |
| 331 | tree = tree->next; |
| 332 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0); |
| 333 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 334 | lyd_free_all(tree); |
| 335 | |
| 336 | data = "{\"a:axml\":null}"; |
| 337 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 338 | assert_non_null(tree); |
| 339 | tree = tree->next; |
| 340 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0); |
| 341 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 342 | lyd_free_all(tree); |
| 343 | |
| 344 | data = "{\"a:axml\":[null,true,false]}"; |
| 345 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 346 | assert_non_null(tree); |
| 347 | tree = tree->next; |
| 348 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0); |
| 349 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 350 | lyd_free_all(tree); |
| 351 | |
| 352 | data = "{\"a:axml\":[null,true,{\"name\":[25,40, false]}]}"; |
| 353 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 354 | assert_non_null(tree); |
| 355 | tree = tree->next; |
| 356 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0); |
| 357 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 358 | lyd_free_all(tree); |
| 359 | |
| 360 | /* same as anydata tests */ |
| 361 | data = "{\"a:axml\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}"; |
| 362 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 363 | assert_non_null(tree); |
| 364 | tree = tree->next; |
| 365 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0); |
| 366 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 367 | lyd_free_all(tree); |
| 368 | |
| 369 | data = "{\"a:axml\":{}}"; |
| 370 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 371 | assert_non_null(tree); |
| 372 | tree = tree->next; |
| 373 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0); |
| 374 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 375 | lyd_free_all(tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static void |
| 379 | test_list(void **state) |
| 380 | { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 381 | const char *data; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 382 | struct lyd_node *tree, *iter; |
| 383 | struct lyd_node_inner *list; |
| 384 | struct lyd_node_term *leaf; |
| 385 | |
| 386 | /* check hashes */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 387 | data = "{\"a:l1\":[{\"a\":\"one\",\"b\":\"one\",\"c\":1}]}"; |
| 388 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 389 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1", |
| 390 | 1, LYS_LIST, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 391 | list = (struct lyd_node_inner *)tree; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 392 | LY_LIST_FOR(list->child, iter) { |
| 393 | assert_int_not_equal(0, iter->hash); |
| 394 | } |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 395 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 396 | lyd_free_all(tree); |
| 397 | |
Michal Vasko | 4e26adc | 2022-03-30 11:01:16 +0200 | [diff] [blame] | 398 | /* accept empty */ |
| 399 | data = "{\"a:l1\":[]}"; |
| 400 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
| 401 | assert_null(tree); |
| 402 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 403 | /* missing keys */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 404 | PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 405 | "List instance is missing its key \"a\".", |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 406 | "Data location \"/a:l1[b='b'][c='1']\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 407 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 408 | PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 409 | "List instance is missing its key \"b\".", "Data location \"/a:l1[a='a']\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 410 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 411 | PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"b\" : \"b\", \"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 412 | "List instance is missing its key \"c\".", "Data location \"/a:l1[a='a'][b='b']\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 413 | |
| 414 | /* key duplicate */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 415 | PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\", \"a\" : \"a\", \"c\" : 1}]}", 0, LYD_VALIDATE_PRESENT, |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 416 | tree, LY_EVALID, "Duplicate instance of \"c\".", |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 417 | "Data location \"/a:l1[a='a'][b='b'][c='1'][c='1']/c\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 418 | |
| 419 | /* keys order, in contrast to XML, JSON accepts keys in any order even in strict mode */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 420 | CHECK_PARSE_LYD("{ \"a:l1\": [ {\"d\" : \"d\", \"a\" : \"a\", \"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree); |
| 421 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1", |
| 422 | 1, LYS_LIST, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 423 | list = (struct lyd_node_inner *)tree; |
| 424 | assert_non_null(leaf = (struct lyd_node_term *)list->child); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 425 | CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a", 1, LYS_LEAF, 1, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 426 | assert_non_null(leaf = (struct lyd_node_term *)leaf->next); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 427 | CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b", 1, LYS_LEAF, 1, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 428 | assert_non_null(leaf = (struct lyd_node_term *)leaf->next); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 429 | CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c", 1, LYS_LEAF, 1, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 430 | assert_non_null(leaf = (struct lyd_node_term *)leaf->next); |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 431 | CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "d", 1, LYS_LEAF, 1, 0, NULL, 0); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 432 | CHECK_LOG_CTX(NULL, NULL); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 433 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, |
| 434 | "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1,\"d\":\"d\"}]}"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 435 | lyd_free_all(tree); |
| 436 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 437 | CHECK_PARSE_LYD("{\"a:l1\":[{\"c\":1,\"b\":\"b\",\"a\":\"a\"}]}", LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree); |
| 438 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1", |
| 439 | 1, LYS_LIST, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 440 | list = (struct lyd_node_inner *)tree; |
| 441 | assert_non_null(leaf = (struct lyd_node_term *)list->child); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 442 | CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a", |
| 443 | 1, LYS_LEAF, 1, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 444 | assert_non_null(leaf = (struct lyd_node_term *)leaf->next); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 445 | CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b", |
| 446 | 1, LYS_LEAF, 1, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 447 | assert_non_null(leaf = (struct lyd_node_term *)leaf->next); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 448 | CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c", |
| 449 | 1, LYS_LEAF, 1, 0, NULL, 0); |
| 450 | CHECK_LOG_CTX(NULL, NULL); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 451 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, |
| 452 | "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1}]}"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 453 | lyd_free_all(tree); |
| 454 | |
Michal Vasko | 87c5d6b | 2023-02-14 15:27:41 +0100 | [diff] [blame] | 455 | /* skip unknown nested nodes */ |
| 456 | data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":3,\"counters\":{\"count1\":\"c1\",\"count2\":\"c2\"}}]}"; |
| 457 | CHECK_PARSE_LYD(data, LYD_PARSE_ONLY, 0, tree); |
| 458 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":3}]}"); |
| 459 | lyd_free_all(tree); |
| 460 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 461 | data = "{\"a:cp\":{\"@\":{\"a:hint\":1}}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 462 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 463 | assert_non_null(tree); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 464 | tree = tree->next; |
Michal Vasko | e16c7b7 | 2021-02-26 10:39:06 +0100 | [diff] [blame] | 465 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp", |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 466 | 1, LYS_CONTAINER, 0, 0, NULL, 0); |
| 467 | CHECK_LYD_META(tree->meta, 1, "hint", 0, 1, INT8, "1", 1); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 468 | assert_null(tree->meta->next); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 469 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 470 | lyd_free_all(tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static void |
| 474 | test_container(void **state) |
| 475 | { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 476 | const char *data; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 477 | struct lyd_node *tree; |
| 478 | struct lyd_node_inner *cont; |
| 479 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 480 | CHECK_PARSE_LYD("{\"a:c\":{}}", 0, LYD_VALIDATE_PRESENT, tree); |
| 481 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", |
| 482 | 1, LYS_CONTAINER, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 483 | cont = (struct lyd_node_inner *)tree; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 484 | assert_true(cont->flags & LYD_DEFAULT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 485 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 486 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 487 | lyd_free_all(tree); |
| 488 | |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 489 | data = "{\"a:cp\":{}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 490 | CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 491 | assert_non_null(tree); |
Michal Vasko | 2612319 | 2020-11-09 21:02:34 +0100 | [diff] [blame] | 492 | tree = tree->next; |
Michal Vasko | e16c7b7 | 2021-02-26 10:39:06 +0100 | [diff] [blame] | 493 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp", |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 494 | 1, LYS_CONTAINER, 0, 0, NULL, 0); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 495 | cont = (struct lyd_node_inner *)tree; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 496 | assert_false(cont->flags & LYD_DEFAULT); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 497 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 498 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 499 | lyd_free_all(tree); |
Michal Vasko | a4af625 | 2022-05-20 10:38:54 +0200 | [diff] [blame] | 500 | |
| 501 | /* skip container */ |
| 502 | CHECK_PARSE_LYD("{\"a:unknown\":{\"a\":\"val\",\"b\":5}}", 0, LYD_VALIDATE_PRESENT, tree); |
| 503 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}"); |
| 504 | lyd_free_all(tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static void |
| 508 | test_opaq(void **state) |
| 509 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 510 | const char *data; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 511 | struct lyd_node *tree; |
| 512 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 513 | /* invalid value, no flags */ |
| 514 | data = "{\"a:foo3\":[null]}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 515 | PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 516 | "Invalid non-number-encoded uint32 value \"\".", "Schema location \"/a:foo3\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 517 | |
| 518 | /* opaq flag */ |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 519 | CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 520 | CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo3", 0, 0, NULL, 0, ""); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 521 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 522 | lyd_free_all(tree); |
| 523 | |
Michal Vasko | 5e2742d | 2023-05-25 10:01:42 +0200 | [diff] [blame] | 524 | /* special chars */ |
| 525 | data = "{\"a:foo3\":\"ab\\\"\\\\\\r\\t\"}"; |
| 526 | CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree); |
| 527 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 528 | lyd_free_all(tree); |
| 529 | |
Michal Vasko | 583b464 | 2023-05-25 10:39:34 +0200 | [diff] [blame] | 530 | /* wrong encoding */ |
| 531 | data = "{\"a:foo3\":\"25\"}"; |
| 532 | CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree); |
| 533 | CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo3", 0, 0, NULL, 0, "25"); |
| 534 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 535 | lyd_free_all(tree); |
| 536 | |
| 537 | data = "{\"a:foo4\":25}"; |
| 538 | CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree); |
| 539 | CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo4", 0, 0, NULL, 0, "25"); |
| 540 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 541 | lyd_free_all(tree); |
| 542 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 543 | /* missing key, no flags */ |
| 544 | data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"d\":\"val_d\"}]}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 545 | PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 546 | "List instance is missing its key \"c\".", |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 547 | "Data location \"/a:l1[a='val_a'][b='val_b']\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 548 | |
| 549 | /* opaq flag */ |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 550 | CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 551 | CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_JSON, "l1", 0, 0, NULL, 0, ""); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 552 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 553 | lyd_free_all(tree); |
| 554 | |
| 555 | /* invalid key, no flags */ |
| 556 | data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 557 | PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 558 | "Invalid non-number-encoded int16 value \"val_c\".", |
Michal Vasko | a4dfb3c | 2022-10-25 14:59:31 +0200 | [diff] [blame] | 559 | "Data location \"/a:l1[a='val_a'][b='val_b']/c\", line number 1."); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 560 | |
| 561 | /* opaq flag */ |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 562 | CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 563 | CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_JSON, "l1", 0, 0, NULL, 0, ""); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 564 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 565 | lyd_free_all(tree); |
| 566 | |
| 567 | data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":{\"val\":\"val_c\"}}]}"; |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 568 | CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 569 | CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_JSON, "l1", 0, 0, NULL, 0, ""); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 570 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 571 | lyd_free_all(tree); |
| 572 | |
| 573 | data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\"}]}"; |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 574 | CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 575 | CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_JSON, "l1", 0, 0, NULL, 0, ""); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 576 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 577 | lyd_free_all(tree); |
aPiecek | b7b29e6 | 2021-05-11 10:02:43 +0200 | [diff] [blame] | 578 | |
Michal Vasko | 6a6e308 | 2022-05-10 10:32:38 +0200 | [diff] [blame] | 579 | /* invalid metadata */ |
aPiecek | c5f36a7 | 2021-05-18 14:12:31 +0200 | [diff] [blame] | 580 | data = "{\"@a:foo\":\"str\",\"@a:foo3\":1,\"a:foo3\":2}"; |
Michal Vasko | 6a6e308 | 2022-05-10 10:32:38 +0200 | [diff] [blame] | 581 | PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 582 | "Unknown module of node \"@a:foo\".", "Path \"/\"."); |
Michal Vasko | 62af369 | 2023-02-09 14:00:09 +0100 | [diff] [blame] | 583 | CHECK_LOG_CTX("Missing JSON data instance to be coupled with @a:foo metadata.", "Data location \"/@a:foo\", line number 1."); |
aPiecek | c5f36a7 | 2021-05-18 14:12:31 +0200 | [diff] [blame] | 584 | |
aPiecek | b7b29e6 | 2021-05-11 10:02:43 +0200 | [diff] [blame] | 585 | /* empty name */ |
| 586 | PARSER_CHECK_ERROR("{\"@a:foo\":{\"\":0}}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, |
Michal Vasko | bfebf66 | 2023-02-17 09:07:22 +0100 | [diff] [blame] | 587 | "JSON object member name cannot be a zero-length string.", "Line number 1."); |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 588 | |
| 589 | /* opaque data tree format print */ |
| 590 | data = |
| 591 | "{\n" |
| 592 | " \"ietf-netconf-nmda:get-data\": {\n" |
| 593 | " \"data\": {\n" |
| 594 | " \"ietf-keystore:keystore\": {\n" |
| 595 | " \"asymmetric-keys\": {\n" |
| 596 | " \"asymmetric-key\": [\n" |
| 597 | " {\n" |
| 598 | " \"name\": \"genkey\",\n" |
| 599 | " \"algorithm\": \"rsa2048\"\n" |
| 600 | " }\n" |
| 601 | " ]\n" |
| 602 | " }\n" |
| 603 | " },\n" |
| 604 | " \"ietf-netconf-server:netconf-server\": {\n" |
| 605 | " \"listen\": {\n" |
| 606 | " \"idle-timeout\": 3600,\n" |
| 607 | " \"endpoint\": [\n" |
| 608 | " {\n" |
| 609 | " \"name\": \"default-ssh\",\n" |
| 610 | " \"ssh\": {\n" |
| 611 | " \"tcp-server-parameters\": {\n" |
| 612 | " \"local-address\": \"0.0.0.0\",\n" |
| 613 | " \"local-port\": 830\n" |
| 614 | " },\n" |
| 615 | " \"ssh-server-parameters\": {\n" |
| 616 | " \"server-identity\": {\n" |
| 617 | " \"host-key\": [\n" |
| 618 | " {\n" |
| 619 | " \"name\": \"default-key\",\n" |
| 620 | " \"public-key\": {\n" |
| 621 | " \"keystore-reference\": \"genkey\"\n" |
| 622 | " }\n" |
| 623 | " }\n" |
| 624 | " ]\n" |
| 625 | " },\n" |
| 626 | " \"client-authentication\": {\n" |
| 627 | " \"supported-authentication-methods\": {\n" |
| 628 | " \"publickey\": [null],\n" |
| 629 | " \"passsword\": [null],\n" |
| 630 | " \"other\": [\n" |
| 631 | " \"interactive\",\n" |
| 632 | " \"gssapi\"\n" |
| 633 | " ]\n" |
| 634 | " }\n" |
| 635 | " }\n" |
| 636 | " }\n" |
| 637 | " }\n" |
| 638 | " }\n" |
| 639 | " ]\n" |
| 640 | " }\n" |
| 641 | " }\n" |
| 642 | " }\n" |
| 643 | " }\n" |
| 644 | "}\n"; |
| 645 | CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree); |
| 646 | CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data); |
| 647 | lyd_free_all(tree); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static void |
| 651 | test_rpc(void **state) |
| 652 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 653 | const char *data; |
| 654 | struct ly_in *in; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 655 | struct lyd_node *tree, *op; |
| 656 | const struct lyd_node *node; |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 657 | const char *dsc = "Edit data in an NMDA datastore.\n" |
| 658 | "\n" |
| 659 | "If an error condition occurs such that an error severity\n" |
| 660 | "<rpc-error> element is generated, the server will stop\n" |
| 661 | "processing the <edit-data> operation and restore the\n" |
| 662 | "specified configuration to its complete state at\n" |
| 663 | "the start of this <edit-data> operation."; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 664 | |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 665 | assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL))); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 666 | |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 667 | data = "{\"ietf-netconf-nmda:edit-data\":{" |
| 668 | "\"datastore\":\"ietf-datastores:running\"," |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 669 | "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}}," |
| 670 | "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}" |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 671 | "}}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 672 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 673 | assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_RPC_YANG, &tree, &op)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 674 | ly_in_free(in, 0); |
| 675 | |
| 676 | assert_non_null(op); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 677 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 678 | CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR, |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 679 | 1, 0, 0, 1, "edit-data", LYS_RPC, |
| 680 | 0, 0, 0, 0, 0, NULL, 0); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 681 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 682 | node = tree; |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 683 | CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR, |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 684 | 1, 0, 0, 1, "edit-data", LYS_RPC, |
| 685 | 0, 0, 0, 0, 0, NULL, 0); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 686 | node = lyd_child(node)->next; |
Michal Vasko | bbdadda | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 687 | CHECK_LYSC_NODE(node->schema, "Inline config content.", 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config", |
| 688 | 0, LYS_ANYDATA, 1, 0, NULL, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 689 | |
| 690 | node = ((struct lyd_node_any *)node)->value.tree; |
Michal Vasko | e16c7b7 | 2021-02-26 10:39:06 +0100 | [diff] [blame] | 691 | CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp", |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 692 | 1, LYS_CONTAINER, 0, 0, NULL, 0); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 693 | node = lyd_child(node); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 694 | /* z has no value */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 695 | CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0, LY_VALUE_JSON, "z", 0, 0, NULL, 0, ""); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 696 | node = node->parent->next; |
| 697 | /* l1 key c has invalid value so it is at the end */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 698 | CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0x1, LY_VALUE_JSON, "l1", 0, 0, NULL, 0, ""); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 699 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 700 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 701 | lyd_free_all(tree); |
| 702 | |
| 703 | /* wrong namespace, element name, whatever... */ |
| 704 | /* TODO */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static void |
| 708 | test_action(void **state) |
| 709 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 710 | const char *data; |
| 711 | struct ly_in *in; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 712 | struct lyd_node *tree, *op; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 713 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 714 | data = "{\"a:c\":{\"act\":{\"al\":\"value\"}}}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 715 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 716 | assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_RPC_YANG, &tree, &op)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 717 | ly_in_free(in, 0); |
| 718 | |
| 719 | assert_non_null(op); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 720 | CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR, |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 721 | 1, 0, 0, 1, "act", LYS_ACTION, |
| 722 | 1, 0, 0, 1, 0, NULL, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 723 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 724 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 725 | lyd_free_all(tree); |
| 726 | |
| 727 | /* wrong namespace, element name, whatever... */ |
| 728 | /* TODO */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | static void |
| 732 | test_notification(void **state) |
| 733 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 734 | const char *data; |
| 735 | struct ly_in *in; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 736 | struct lyd_node *tree, *ntf; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 737 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 738 | data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 739 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 740 | assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_YANG, &tree, &ntf)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 741 | ly_in_free(in, 0); |
| 742 | |
| 743 | assert_non_null(ntf); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 744 | CHECK_LYSC_NOTIF((struct lysc_node_notif *)ntf->schema, 1, NULL, 0, 0x4, 1, 0, "n1", 1, 0, NULL, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 745 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 746 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", 1, LYS_CONTAINER, 0, 0, NULL, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 747 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 748 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 749 | lyd_free_all(tree); |
| 750 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 751 | data = "{\"a:n2\":{}}"; |
| 752 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 753 | assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_YANG, &tree, &ntf)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 754 | ly_in_free(in, 0); |
| 755 | |
| 756 | assert_non_null(ntf); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 757 | CHECK_LYSC_NOTIF((struct lysc_node_notif *)ntf->schema, 0, NULL, 0, 0x4, 1, 0, "n2", 0, 0, NULL, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 758 | |
| 759 | assert_non_null(tree); |
| 760 | assert_ptr_equal(ntf, tree); |
| 761 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 762 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 763 | lyd_free_all(tree); |
| 764 | |
| 765 | /* wrong namespace, element name, whatever... */ |
| 766 | /* TODO */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | static void |
| 770 | test_reply(void **state) |
| 771 | { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 772 | const char *data; |
| 773 | struct ly_in *in; |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 774 | struct lyd_node *tree, *op; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 775 | const struct lyd_node *node; |
| 776 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 777 | data = "{\"a:c\":{\"act\":{\"al\":25}}}"; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 778 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 779 | assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_REPLY_YANG, &tree, &op)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 780 | ly_in_free(in, 0); |
| 781 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 782 | assert_non_null(op); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 783 | CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR, |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 784 | 1, 0, 0, 1, "act", LYS_ACTION, |
| 785 | 1, 0, 0, 1, 0, NULL, 0); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 786 | node = lyd_child(op); |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 787 | CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_STATUS_CURR | LYS_IS_OUTPUT, 1, "al", 0, LYS_LEAF, 1, 0, NULL, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 788 | |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 789 | CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", 1, LYS_CONTAINER, 0, 0, NULL, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 790 | |
| 791 | /* TODO print only rpc-reply node and then output subtree */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 792 | CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:al\":25}"); |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 793 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:c\":{\"act\":{\"al\":25}}}"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 794 | lyd_free_all(tree); |
| 795 | |
| 796 | /* wrong namespace, element name, whatever... */ |
| 797 | /* TODO */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 798 | } |
| 799 | |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 800 | static void |
| 801 | test_restconf_rpc(void **state) |
| 802 | { |
| 803 | const char *data; |
| 804 | struct ly_in *in; |
| 805 | struct lyd_node *tree, *envp; |
| 806 | |
| 807 | assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL))); |
| 808 | |
| 809 | assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/ietf-netconf-nmda:edit-data", NULL, 0, &tree)); |
| 810 | |
| 811 | data = "{\"ietf-netconf-nmda:input\":{" |
| 812 | "\"datastore\":\"ietf-datastores:running\"," |
| 813 | "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}}," |
| 814 | "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}" |
| 815 | "}}"; |
| 816 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 817 | assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, tree, in, LYD_JSON, LYD_TYPE_RPC_RESTCONF, &envp, NULL)); |
| 818 | ly_in_free(in, 0); |
| 819 | |
| 820 | /* the same just connected to the edit-data RPC */ |
| 821 | data = "{\"ietf-netconf-nmda:edit-data\":{" |
| 822 | "\"datastore\":\"ietf-datastores:running\"," |
| 823 | "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}}," |
| 824 | "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}" |
| 825 | "}}"; |
| 826 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 827 | lyd_free_all(tree); |
| 828 | lyd_free_all(envp); |
| 829 | } |
| 830 | |
| 831 | static void |
| 832 | test_restconf_notification(void **state) |
| 833 | { |
| 834 | const char *data; |
| 835 | struct ly_in *in; |
| 836 | struct lyd_node *tree, *ntf; |
| 837 | |
| 838 | data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\",\"a:c\":{\"n1\":{\"nl\":\"value\"}}}}"; |
| 839 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 840 | assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf)); |
| 841 | ly_in_free(in, 0); |
| 842 | |
| 843 | /* envelopes separately */ |
| 844 | data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\"}}"; |
| 845 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 846 | |
| 847 | /* notification with the parent node */ |
| 848 | data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}"; |
| 849 | CHECK_LYD_STRING(lyd_parent(ntf), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 850 | |
| 851 | lyd_free_all(tree); |
| 852 | lyd_free_all(ntf); |
| 853 | } |
| 854 | |
| 855 | static void |
| 856 | test_restconf_reply(void **state) |
| 857 | { |
| 858 | const char *data; |
| 859 | struct ly_in *in; |
| 860 | struct lyd_node *tree, *envp; |
| 861 | |
| 862 | assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:c/act", NULL, 0, &tree)); |
| 863 | |
| 864 | data = "{\"a:output\":{\"al\":25}}"; |
| 865 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 866 | assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, lyd_child(tree), in, LYD_JSON, LYD_TYPE_REPLY_RESTCONF, &envp, NULL)); |
| 867 | ly_in_free(in, 0); |
| 868 | |
| 869 | /* connected to the RPC with the parent */ |
| 870 | data = "{\"a:c\":{\"act\":{\"al\":25}}}"; |
| 871 | CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data); |
| 872 | lyd_free_all(tree); |
| 873 | lyd_free_all(envp); |
| 874 | } |
| 875 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 876 | int |
| 877 | main(void) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 878 | { |
| 879 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 880 | UTEST(test_leaf, setup), |
| 881 | UTEST(test_leaflist, setup), |
| 882 | UTEST(test_anydata, setup), |
Michal Vasko | 76096ec | 2022-02-24 16:06:16 +0100 | [diff] [blame] | 883 | UTEST(test_anyxml, setup), |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 884 | UTEST(test_list, setup), |
| 885 | UTEST(test_container, setup), |
| 886 | UTEST(test_opaq, setup), |
| 887 | UTEST(test_rpc, setup), |
| 888 | UTEST(test_action, setup), |
| 889 | UTEST(test_notification, setup), |
| 890 | UTEST(test_reply, setup), |
Michal Vasko | 820efe8 | 2023-05-12 15:47:43 +0200 | [diff] [blame] | 891 | UTEST(test_restconf_rpc, setup), |
| 892 | UTEST(test_restconf_notification, setup), |
| 893 | UTEST(test_restconf_reply, setup), |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 894 | }; |
| 895 | |
| 896 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 897 | } |