blob: 4fdc1bf62084e924a030140d2ff07da6c40d9b27 [file] [log] [blame]
Radek Krejci1798aae2020-07-14 13:26:06 +02001/*
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_parser_json.c
Radek Krejci1798aae2020-07-14 13:26:06 +02003 * @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 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020016
Radek Krejci1798aae2020-07-14 13:26:06 +020017#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020018#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020019#include "out.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010020#include "parser_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020021#include "printer_data.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020022#include "tests_config.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020023#include "tree_data_internal.h"
24#include "tree_schema.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020025
26static int
27setup(void **state)
28{
Radek Iša56ca9e42020-09-08 18:42:00 +020029 const char *schema = "module a {namespace urn:tests:a;prefix a;yang-version 1.1; import ietf-yang-metadata {prefix md;}"
Radek Krejci1798aae2020-07-14 13:26:06 +020030 "md:annotation hint { type int8;}"
31 "list l1 { key \"a b c\"; leaf a {type string;} leaf b {type string;} leaf c {type int16;} leaf d {type string;}}"
32 "leaf foo { type string;}"
33 "container c {"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010034 " leaf x {type string;}"
35 " action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }"
36 " notification n1 { leaf nl {type string;} }"
Radek Krejci1798aae2020-07-14 13:26:06 +020037 "}"
38 "container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}"
39 "anydata any {config false;}"
Michal Vasko76096ec2022-02-24 16:06:16 +010040 "anyxml axml;"
Radek Krejci1798aae2020-07-14 13:26:06 +020041 "leaf-list ll1 { type uint8; }"
42 "leaf foo2 { type string; default \"default-val\"; }"
43 "leaf foo3 { type uint32; }"
44 "notification n2;}";
Radek Krejci1798aae2020-07-14 13:26:06 +020045
Radek Iša56ca9e42020-09-08 18:42:00 +020046 UTEST_SETUP;
Radek Krejci1798aae2020-07-14 13:26:06 +020047
Radek Iša56ca9e42020-09-08 18:42:00 +020048 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
49 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Radek Krejci1798aae2020-07-14 13:26:06 +020050
51 return 0;
52}
53
Radek Iša56ca9e42020-09-08 18:42:00 +020054#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
55 CHECK_PARSE_LYD_PARAM(INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci1798aae2020-07-14 13:26:06 +020056
Radek Iša56ca9e42020-09-08 18:42:00 +020057#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH) \
58 assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
59 CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH);\
60 assert_null(MODEL)
Radek Krejci1798aae2020-07-14 13:26:06 +020061
Radek Iša56ca9e42020-09-08 18:42:00 +020062#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
63 CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_JSON, PRINT_OPTION)
Radek Krejci1798aae2020-07-14 13:26:06 +020064
65static void
66test_leaf(void **state)
67{
Radek Krejci1798aae2020-07-14 13:26:06 +020068 struct lyd_node *tree;
69 struct lyd_node_term *leaf;
Radek Iša56ca9e42020-09-08 18:42:00 +020070 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +020071
Radek Iša56ca9e42020-09-08 18:42:00 +020072 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010073
Radek Iša56ca9e42020-09-08 18:42:00 +020074 data = "{\"a:foo\":\"foo value\"}";
75 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
76 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010077 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +020078 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
Radek Krejci1798aae2020-07-14 13:26:06 +020079
Radek Iša56ca9e42020-09-08 18:42:00 +020080 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
81 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010082 leaf = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020083
84 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +020085 assert_true(leaf->flags & LYD_DEFAULT);
86
Radek Iša56ca9e42020-09-08 18:42:00 +020087 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +020088 lyd_free_all(tree);
89
90 /* make foo2 explicit */
91 data = "{\"a:foo2\":\"default-val\"}";
Radek Iša56ca9e42020-09-08 18:42:00 +020092 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +020093 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +010094 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020095 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
96 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010097 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +020098 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +020099 assert_false(leaf->flags & LYD_DEFAULT);
100
Radek Iša56ca9e42020-09-08 18:42:00 +0200101 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200102 lyd_free_all(tree);
103
104 /* parse foo2 but make it implicit */
Radek Krejci5536d282020-08-04 23:27:44 +0200105 data = "{\"a:foo2\":\"default-val\",\"@a:foo2\":{\"ietf-netconf-with-defaults:default\":true}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200106 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200107 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100108 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200109 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
110 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100111 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200112 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +0200113 assert_true(leaf->flags & LYD_DEFAULT);
114
Michal Vaskob68c3b42022-05-20 10:36:40 +0200115 /* print default values */
116 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL_TAG, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200117 lyd_free_all(tree);
118
Michal Vaskoa4af6252022-05-20 10:38:54 +0200119 /* skip leaf */
120 data = "{\"a:cp\":{\"x\":\"val\",\"y\":\"valy\",\"z\":5}}";
121 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
122 assert_non_null(tree);
123
124 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:cp\":{\"y\":\"valy\",\"z\":5}}");
125 lyd_free_all(tree);
126
Radek Krejci1798aae2020-07-14 13:26:06 +0200127 /* multiple meatadata hint and unknown metadata xxx supposed to be skipped since it is from missing schema */
Radek Krejci5536d282020-08-04 23:27:44 +0200128 data = "{\"@a:foo\":{\"a:hint\":1,\"a:hint\":2,\"x:xxx\":{\"value\":\"/x:no/x:yes\"}},\"a:foo\":\"xxx\"}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200129 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
130 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
131 CHECK_LYD_META(tree->meta, 1, "hint", 1, 1, INT8, "1", 1);
132 CHECK_LYD_META(tree->meta->next, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200133 assert_null(tree->meta->next->next);
134
Radek Iša56ca9e42020-09-08 18:42:00 +0200135 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
136 "{\"a:foo\":\"xxx\",\"@a:foo\":{\"a:hint\":1,\"a:hint\":2}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200137 lyd_free_all(tree);
138
Radek Iša56ca9e42020-09-08 18:42:00 +0200139 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200140 "Unknown (or not implemented) YANG module \"x\" of metadata \"x:xxx\".", "Data location \"/@a:foo\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200141
142 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200143 PARSER_CHECK_ERROR("{\"@a:foo\" : { \"a:hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200144 "Missing JSON data instance to be coupled with @a:foo metadata.", "Data location \"/@a:foo\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200145
146 /* missing namespace for meatadata*/
Radek Iša56ca9e42020-09-08 18:42:00 +0200147 PARSER_CHECK_ERROR("{\"a:foo\" : \"value\", \"@a:foo\" : { \"hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200148 "Metadata in JSON must be namespace-qualified, missing prefix for \"hint\".",
149 "Schema location \"/a:foo\", line number 1.");
aPiecek49e2a3a2021-05-13 10:36:46 +0200150
151 /* reverse solidus in JSON object member name */
152 data = "{\"@a:foo\":{\"a:hi\\nt\":1},\"a:foo\":\"xxx\"}";
153 assert_int_equal(LY_EINVAL, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejci1798aae2020-07-14 13:26:06 +0200154}
155
156static void
157test_leaflist(void **state)
158{
Radek Iša56ca9e42020-09-08 18:42:00 +0200159 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200160 struct lyd_node *tree;
161 struct lyd_node_term *ll;
162
Radek Iša56ca9e42020-09-08 18:42:00 +0200163 data = "{\"a:ll1\":[10,11]}";
164 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200165 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100166 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200167 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
168 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100169 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200170 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200171
172 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200173 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
174 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100175 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200176 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
Radek Krejci1798aae2020-07-14 13:26:06 +0200177
Radek Iša56ca9e42020-09-08 18:42:00 +0200178 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200179 lyd_free_all(tree);
180
Michal Vasko4e26adc2022-03-30 11:01:16 +0200181 /* accept empty */
182 data = "{\"a:ll1\":[]}";
183 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
184 assert_null(tree);
185
Radek Krejci1798aae2020-07-14 13:26:06 +0200186 /* simple metadata */
Radek Krejci5536d282020-08-04 23:27:44 +0200187 data = "{\"a:ll1\":[10,11],\"@a:ll1\":[null,{\"a:hint\":2}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200188 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200189 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100190 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200191 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
192 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100193 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200194 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200195 assert_null(ll->meta);
196
197 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200198 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
199 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100200 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200201 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
202 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200203 assert_null(ll->meta->next);
204
Radek Iša56ca9e42020-09-08 18:42:00 +0200205 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200206 lyd_free_all(tree);
207
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100208 /* multiple meatadata hint and unknown metadata xxx supposed to be skipped since it is from missing schema */
Radek Iša56ca9e42020-09-08 18:42:00 +0200209 data = "{\"@a:ll1\" : [{\"a:hint\" : 1, \"x:xxx\" : { \"value\" : \"/x:no/x:yes\" }, "
210 "\"a:hint\" : 10},null,{\"a:hint\" : 3}], \"a:ll1\" : [1,2,3]}";
211 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200212 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100213 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200214 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
215 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100216 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200217 CHECK_LYD_VALUE(ll->value, UINT8, "1", 1);
218 CHECK_LYD_META(ll->meta, 1, "hint", 1, 1, INT8, "1", 1);
219 CHECK_LYD_META(ll->meta->next, 1, "hint", 0, 1, INT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200220
221 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200222 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
223 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100224 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200225 CHECK_LYD_VALUE(ll->value, UINT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200226 assert_null(ll->meta);
227
228 assert_non_null(tree->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200229 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
230 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100231 ll = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200232 CHECK_LYD_VALUE(ll->value, UINT8, "3", 3);
233 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "3", 3);
Radek Krejci1798aae2020-07-14 13:26:06 +0200234 assert_null(ll->meta->next);
235
Radek Iša56ca9e42020-09-08 18:42:00 +0200236 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
237 "{\"a:ll1\":[1,2,3],\"@a:ll1\":[{\"a:hint\":1,\"a:hint\":10},null,{\"a:hint\":3}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200238 lyd_free_all(tree);
239
240 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200241 PARSER_CHECK_ERROR("{\"@a:ll1\":[{\"a:hint\":1}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200242 "Missing JSON data instance to be coupled with @a:ll1 metadata.", "Data location \"/@a:ll1\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200243
Radek Iša56ca9e42020-09-08 18:42:00 +0200244 PARSER_CHECK_ERROR("{\"a:ll1\":[1],\"@a:ll1\":[{\"a:hint\":1},{\"a:hint\":2}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200245 "Missing JSON data instance #2 of a:ll1 to be coupled with metadata.", "Schema location \"/a:ll1\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200246
Radek Iša56ca9e42020-09-08 18:42:00 +0200247 PARSER_CHECK_ERROR("{\"@a:ll1\":[{\"a:hint\":1},{\"a:hint\":2},{\"a:hint\":3}],\"a:ll1\" : [1, 2]}", 0, LYD_VALIDATE_PRESENT,
Michal Vasko959f8d82022-06-16 07:51:50 +0200248 tree, LY_EVALID, "Missing JSON data instance #3 to be coupled with @a:ll1 metadata.",
249 "Data location \"/@a:ll1\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200250}
251
252static void
253test_anydata(void **state)
254{
Radek Krejci1798aae2020-07-14 13:26:06 +0200255 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200256 struct lyd_node *tree;
257
Radek Krejci1798aae2020-07-14 13:26:06 +0200258 data = "{\"a:any\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200259 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200260 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100261 tree = tree->next;
Michal Vasko76096ec2022-02-24 16:06:16 +0100262 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any",
Radek Iša56ca9e42020-09-08 18:42:00 +0200263 1, LYS_ANYDATA, 0, 0, NULL, 0);
264 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200265 lyd_free_all(tree);
Michal Vaskofbc4a252022-02-03 13:15:58 +0100266
267 data = "{\"a:any\":{}}";
268 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
269 assert_non_null(tree);
270 tree = tree->next;
Michal Vasko76096ec2022-02-24 16:06:16 +0100271 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any",
Michal Vaskofbc4a252022-02-03 13:15:58 +0100272 1, LYS_ANYDATA, 0, 0, NULL, 0);
273 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
274 lyd_free_all(tree);
Michal Vasko76096ec2022-02-24 16:06:16 +0100275
276 data = "{\"a:any\":[null]}";
277 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
278 assert_non_null(tree);
279 tree = tree->next;
280 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any",
281 1, LYS_ANYDATA, 0, 0, NULL, 0);
282 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
283 lyd_free_all(tree);
284}
285
286static void
287test_anyxml(void **state)
288{
289 const char *data;
290 struct lyd_node *tree;
291
292 data = "{\"a:axml\":\"some-value in string\"}";
293 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
294 assert_non_null(tree);
295 tree = tree->next;
296 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
297 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
298 lyd_free_all(tree);
299
300 data = "{\"a:axml\":\"\"}";
301 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
302 assert_non_null(tree);
303 tree = tree->next;
304 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
305 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
306 lyd_free_all(tree);
307
308 data = "{\"a:axml\":55}";
309 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
310 assert_non_null(tree);
311 tree = tree->next;
312 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
313 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
314 lyd_free_all(tree);
315
316 data = "{\"a:axml\":false}";
317 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
318 assert_non_null(tree);
319 tree = tree->next;
320 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
321 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
322 lyd_free_all(tree);
323
324 data = "{\"a:axml\":null}";
325 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
326 assert_non_null(tree);
327 tree = tree->next;
328 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
329 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
330 lyd_free_all(tree);
331
332 data = "{\"a:axml\":[null,true,false]}";
333 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
334 assert_non_null(tree);
335 tree = tree->next;
336 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
337 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
338 lyd_free_all(tree);
339
340 data = "{\"a:axml\":[null,true,{\"name\":[25,40, false]}]}";
341 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
342 assert_non_null(tree);
343 tree = tree->next;
344 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
345 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
346 lyd_free_all(tree);
347
348 /* same as anydata tests */
349 data = "{\"a:axml\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
350 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
351 assert_non_null(tree);
352 tree = tree->next;
353 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
354 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
355 lyd_free_all(tree);
356
357 data = "{\"a:axml\":{}}";
358 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
359 assert_non_null(tree);
360 tree = tree->next;
361 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
362 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
363 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200364}
365
366static void
367test_list(void **state)
368{
Radek Iša56ca9e42020-09-08 18:42:00 +0200369 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200370 struct lyd_node *tree, *iter;
371 struct lyd_node_inner *list;
372 struct lyd_node_term *leaf;
373
374 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200375 data = "{\"a:l1\":[{\"a\":\"one\",\"b\":\"one\",\"c\":1}]}";
376 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
377 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
378 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100379 list = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200380 LY_LIST_FOR(list->child, iter) {
381 assert_int_not_equal(0, iter->hash);
382 }
Radek Iša56ca9e42020-09-08 18:42:00 +0200383 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200384 lyd_free_all(tree);
385
Michal Vasko4e26adc2022-03-30 11:01:16 +0200386 /* accept empty */
387 data = "{\"a:l1\":[]}";
388 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
389 assert_null(tree);
390
Radek Krejci1798aae2020-07-14 13:26:06 +0200391 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200392 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200393 "List instance is missing its key \"a\".",
394 "Schema location \"/a:l1\", data location \"/a:l1[b='b'][c='1']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200395
Radek Iša56ca9e42020-09-08 18:42:00 +0200396 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200397 "List instance is missing its key \"b\".", "Schema location \"/a:l1\", data location \"/a:l1[a='a']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200398
Radek Iša56ca9e42020-09-08 18:42:00 +0200399 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"b\" : \"b\", \"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200400 "List instance is missing its key \"c\".", "Schema location \"/a:l1\", data location \"/a:l1[a='a'][b='b']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200401
402 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200403 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\", \"a\" : \"a\", \"c\" : 1}]}", 0, LYD_VALIDATE_PRESENT,
Michal Vasko959f8d82022-06-16 07:51:50 +0200404 tree, LY_EVALID, "Duplicate instance of \"c\".",
405 "Schema location \"/a:l1/c\", data location \"/a:l1[a='a'][b='b'][c='1'][c='1']/c\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200406
407 /* keys order, in contrast to XML, JSON accepts keys in any order even in strict mode */
Radek Iša56ca9e42020-09-08 18:42:00 +0200408 CHECK_PARSE_LYD("{ \"a:l1\": [ {\"d\" : \"d\", \"a\" : \"a\", \"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree);
409 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
410 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100411 list = (struct lyd_node_inner *)tree;
412 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200413 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 Krejcib4ac5a92020-11-23 17:54:33 +0100414 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200415 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 Krejcib4ac5a92020-11-23 17:54:33 +0100416 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200417 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 Krejcib4ac5a92020-11-23 17:54:33 +0100418 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200419 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "d", 0, LYS_LEAF, 1, 0, NULL, 0);
420 CHECK_LOG_CTX(NULL, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200421
Radek Iša56ca9e42020-09-08 18:42:00 +0200422 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
423 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1,\"d\":\"d\"}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200424 lyd_free_all(tree);
425
426 /* */
Radek Iša56ca9e42020-09-08 18:42:00 +0200427 CHECK_PARSE_LYD("{\"a:l1\":[{\"c\":1,\"b\":\"b\",\"a\":\"a\"}]}", LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree);
428 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
429 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100430 list = (struct lyd_node_inner *)tree;
431 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200432 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a",
433 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100434 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200435 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b",
436 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100437 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200438 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c",
439 1, LYS_LEAF, 1, 0, NULL, 0);
440 CHECK_LOG_CTX(NULL, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200441
Radek Iša56ca9e42020-09-08 18:42:00 +0200442 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
443 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200444 lyd_free_all(tree);
445
Radek Krejci5536d282020-08-04 23:27:44 +0200446 data = "{\"a:cp\":{\"@\":{\"a:hint\":1}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200447 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200448 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100449 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100450 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp",
Radek Iša56ca9e42020-09-08 18:42:00 +0200451 1, LYS_CONTAINER, 0, 0, NULL, 0);
452 CHECK_LYD_META(tree->meta, 1, "hint", 0, 1, INT8, "1", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200453 assert_null(tree->meta->next);
Radek Krejci5536d282020-08-04 23:27:44 +0200454
Radek Iša56ca9e42020-09-08 18:42:00 +0200455 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200456 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200457}
458
459static void
460test_container(void **state)
461{
Radek Iša56ca9e42020-09-08 18:42:00 +0200462 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200463 struct lyd_node *tree;
464 struct lyd_node_inner *cont;
465
Radek Iša56ca9e42020-09-08 18:42:00 +0200466 CHECK_PARSE_LYD("{\"a:c\":{}}", 0, LYD_VALIDATE_PRESENT, tree);
467 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c",
468 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100469 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200470 assert_true(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200471
Radek Iša56ca9e42020-09-08 18:42:00 +0200472 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200473 lyd_free_all(tree);
474
Radek Krejci5536d282020-08-04 23:27:44 +0200475 data = "{\"a:cp\":{}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200476 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200477 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100478 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100479 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp",
Radek Iša56ca9e42020-09-08 18:42:00 +0200480 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100481 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200482 assert_false(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200483
Radek Iša56ca9e42020-09-08 18:42:00 +0200484 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200485 lyd_free_all(tree);
Michal Vaskoa4af6252022-05-20 10:38:54 +0200486
487 /* skip container */
488 CHECK_PARSE_LYD("{\"a:unknown\":{\"a\":\"val\",\"b\":5}}", 0, LYD_VALIDATE_PRESENT, tree);
489 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
490 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200491}
492
493static void
494test_opaq(void **state)
495{
Radek Krejci1798aae2020-07-14 13:26:06 +0200496 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200497 struct lyd_node *tree;
498
Radek Krejci1798aae2020-07-14 13:26:06 +0200499 /* invalid value, no flags */
500 data = "{\"a:foo3\":[null]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200501 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200502 "Invalid non-number-encoded uint32 value \"\".", "Schema location \"/a:foo3\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200503
504 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200505 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200506 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo3", 0, 0, NULL, 0, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200507 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200508 lyd_free_all(tree);
509
510 /* missing key, no flags */
511 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"d\":\"val_d\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200512 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200513 "List instance is missing its key \"c\".",
514 "Schema location \"/a:l1\", data location \"/a:l1[a='val_a'][b='val_b']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200515
516 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200517 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200518 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_JSON, "l1", 0, 0, NULL, 0, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200519 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200520 lyd_free_all(tree);
521
522 /* invalid key, no flags */
523 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200524 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200525 "Invalid non-number-encoded int16 value \"val_c\".",
526 "Schema location \"/a:l1/c\", data location \"/a:l1[a='val_a'][b='val_b']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200527
528 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200529 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200530 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_JSON, "l1", 0, 0, NULL, 0, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200531 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200532 lyd_free_all(tree);
533
534 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":{\"val\":\"val_c\"}}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200535 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200536 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_JSON, "l1", 0, 0, NULL, 0, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200537 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200538 lyd_free_all(tree);
539
540 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\"}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200541 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200542 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0x1, LY_VALUE_JSON, "l1", 0, 0, NULL, 0, "");
Radek Iša56ca9e42020-09-08 18:42:00 +0200543 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200544 lyd_free_all(tree);
aPiecekb7b29e62021-05-11 10:02:43 +0200545
Michal Vasko6a6e3082022-05-10 10:32:38 +0200546 /* invalid metadata */
aPiecekc5f36a72021-05-18 14:12:31 +0200547 data = "{\"@a:foo\":\"str\",\"@a:foo3\":1,\"a:foo3\":2}";
Michal Vasko6a6e3082022-05-10 10:32:38 +0200548 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200549 "Unknown module of node \"@a:foo\".", "Data location \"/@a:foo\", line number 1.");
aPiecekc5f36a72021-05-18 14:12:31 +0200550
aPiecekb7b29e62021-05-11 10:02:43 +0200551 /* empty name */
552 PARSER_CHECK_ERROR("{\"@a:foo\":{\"\":0}}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
553 "A JSON object member name cannot be a zero-length string.", "Line number 1.");
Michal Vasko1a85d332021-08-27 10:35:28 +0200554
555 /* opaque data tree format print */
556 data =
557 "{\n"
558 " \"ietf-netconf-nmda:get-data\": {\n"
559 " \"data\": {\n"
560 " \"ietf-keystore:keystore\": {\n"
561 " \"asymmetric-keys\": {\n"
562 " \"asymmetric-key\": [\n"
563 " {\n"
564 " \"name\": \"genkey\",\n"
565 " \"algorithm\": \"rsa2048\"\n"
566 " }\n"
567 " ]\n"
568 " }\n"
569 " },\n"
570 " \"ietf-netconf-server:netconf-server\": {\n"
571 " \"listen\": {\n"
572 " \"idle-timeout\": 3600,\n"
573 " \"endpoint\": [\n"
574 " {\n"
575 " \"name\": \"default-ssh\",\n"
576 " \"ssh\": {\n"
577 " \"tcp-server-parameters\": {\n"
578 " \"local-address\": \"0.0.0.0\",\n"
579 " \"local-port\": 830\n"
580 " },\n"
581 " \"ssh-server-parameters\": {\n"
582 " \"server-identity\": {\n"
583 " \"host-key\": [\n"
584 " {\n"
585 " \"name\": \"default-key\",\n"
586 " \"public-key\": {\n"
587 " \"keystore-reference\": \"genkey\"\n"
588 " }\n"
589 " }\n"
590 " ]\n"
591 " },\n"
592 " \"client-authentication\": {\n"
593 " \"supported-authentication-methods\": {\n"
594 " \"publickey\": [null],\n"
595 " \"passsword\": [null],\n"
596 " \"other\": [\n"
597 " \"interactive\",\n"
598 " \"gssapi\"\n"
599 " ]\n"
600 " }\n"
601 " }\n"
602 " }\n"
603 " }\n"
604 " }\n"
605 " ]\n"
606 " }\n"
607 " }\n"
608 " }\n"
609 " }\n"
610 "}\n";
611 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
612 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
613 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200614}
615
616static void
617test_rpc(void **state)
618{
Radek Krejci1798aae2020-07-14 13:26:06 +0200619 const char *data;
620 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200621 struct lyd_node *tree, *op;
622 const struct lyd_node *node;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100623 const char *dsc = "Edit data in an NMDA datastore.\n"
624 "\n"
625 "If an error condition occurs such that an error severity\n"
626 "<rpc-error> element is generated, the server will stop\n"
627 "processing the <edit-data> operation and restore the\n"
628 "specified configuration to its complete state at\n"
629 "the start of this <edit-data> operation.";
Radek Krejci1798aae2020-07-14 13:26:06 +0200630
Michal Vaskobbdadda2022-01-06 11:40:10 +0100631 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
Radek Krejci1798aae2020-07-14 13:26:06 +0200632
Michal Vaskobbdadda2022-01-06 11:40:10 +0100633 data = "{\"ietf-netconf-nmda:edit-data\":{"
634 "\"datastore\":\"ietf-datastores:running\","
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100635 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
636 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
Michal Vasko2552ea32020-12-08 15:32:34 +0100637 "}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200638 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100639 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_RPC_YANG, &tree, &op));
Radek Krejci1798aae2020-07-14 13:26:06 +0200640 ly_in_free(in, 0);
641
642 assert_non_null(op);
Radek Krejci1798aae2020-07-14 13:26:06 +0200643
Radek Krejci2a9fc652021-01-22 17:44:34 +0100644 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100645 1, 0, 0, 1, "edit-data", LYS_RPC,
646 0, 0, 0, 0, 0, NULL, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200647
Michal Vasko2552ea32020-12-08 15:32:34 +0100648 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100649 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100650 1, 0, 0, 1, "edit-data", LYS_RPC,
651 0, 0, 0, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200652 node = lyd_child(node)->next;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100653 CHECK_LYSC_NODE(node->schema, "Inline config content.", 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config",
654 0, LYS_ANYDATA, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200655
656 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100657 CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_PRESENCE, 1, "cp",
Radek Iša56ca9e42020-09-08 18:42:00 +0200658 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200659 node = lyd_child(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200660 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200661 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0, LY_VALUE_JSON, "z", 0, 0, NULL, 0, "");
Radek Krejci1798aae2020-07-14 13:26:06 +0200662 node = node->parent->next;
663 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200664 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)node, 0x1, 0x1, LY_VALUE_JSON, "l1", 0, 0, NULL, 0, "");
Radek Krejci1798aae2020-07-14 13:26:06 +0200665
Radek Iša56ca9e42020-09-08 18:42:00 +0200666 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200667 lyd_free_all(tree);
668
669 /* wrong namespace, element name, whatever... */
670 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200671}
672
673static void
674test_action(void **state)
675{
Radek Krejci1798aae2020-07-14 13:26:06 +0200676 const char *data;
677 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200678 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200679
Michal Vasko2552ea32020-12-08 15:32:34 +0100680 data = "{\"a:c\":{\"act\":{\"al\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200681 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100682 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_RPC_YANG, &tree, &op));
Radek Krejci1798aae2020-07-14 13:26:06 +0200683 ly_in_free(in, 0);
684
685 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100686 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200687 1, 0, 0, 1, "act", LYS_ACTION,
688 1, 0, 0, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200689
Radek Iša56ca9e42020-09-08 18:42:00 +0200690 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200691 lyd_free_all(tree);
692
693 /* wrong namespace, element name, whatever... */
694 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200695}
696
697static void
698test_notification(void **state)
699{
Radek Krejci1798aae2020-07-14 13:26:06 +0200700 const char *data;
701 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200702 struct lyd_node *tree, *ntf;
Radek Krejci1798aae2020-07-14 13:26:06 +0200703
Michal Vasko2552ea32020-12-08 15:32:34 +0100704 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200705 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100706 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_YANG, &tree, &ntf));
Radek Krejci1798aae2020-07-14 13:26:06 +0200707 ly_in_free(in, 0);
708
709 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100710 CHECK_LYSC_NOTIF((struct lysc_node_notif *)ntf->schema, 1, NULL, 0, 0x4, 1, 0, "n1", 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200711
Michal Vasko2552ea32020-12-08 15:32:34 +0100712 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200713
Radek Iša56ca9e42020-09-08 18:42:00 +0200714 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200715 lyd_free_all(tree);
716
Radek Krejci1798aae2020-07-14 13:26:06 +0200717 data = "{\"a:n2\":{}}";
718 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100719 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_YANG, &tree, &ntf));
Radek Krejci1798aae2020-07-14 13:26:06 +0200720 ly_in_free(in, 0);
721
722 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100723 CHECK_LYSC_NOTIF((struct lysc_node_notif *)ntf->schema, 0, NULL, 0, 0x4, 1, 0, "n2", 0, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200724
725 assert_non_null(tree);
726 assert_ptr_equal(ntf, tree);
727
Radek Iša56ca9e42020-09-08 18:42:00 +0200728 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200729 lyd_free_all(tree);
730
731 /* wrong namespace, element name, whatever... */
732 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200733}
734
735static void
736test_reply(void **state)
737{
Radek Krejci1798aae2020-07-14 13:26:06 +0200738 const char *data;
739 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100740 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200741 const struct lyd_node *node;
742
Michal Vasko2552ea32020-12-08 15:32:34 +0100743 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200744 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100745 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_REPLY_YANG, &tree, &op));
Radek Krejci1798aae2020-07-14 13:26:06 +0200746 ly_in_free(in, 0);
747
Radek Krejci1798aae2020-07-14 13:26:06 +0200748 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100749 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200750 1, 0, 0, 1, "act", LYS_ACTION,
751 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200752 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100753 CHECK_LYSC_NODE(node->schema, NULL, 0, LYS_STATUS_CURR | LYS_IS_OUTPUT, 1, "al", 0, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200754
Michal Vasko2552ea32020-12-08 15:32:34 +0100755 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c", 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200756
757 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200758 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:al\":25}");
Michal Vasko2552ea32020-12-08 15:32:34 +0100759 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:c\":{\"act\":{\"al\":25}}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200760 lyd_free_all(tree);
761
762 /* wrong namespace, element name, whatever... */
763 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200764}
765
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100766int
767main(void)
Radek Krejci1798aae2020-07-14 13:26:06 +0200768{
769 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200770 UTEST(test_leaf, setup),
771 UTEST(test_leaflist, setup),
772 UTEST(test_anydata, setup),
Michal Vasko76096ec2022-02-24 16:06:16 +0100773 UTEST(test_anyxml, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200774 UTEST(test_list, setup),
775 UTEST(test_container, setup),
776 UTEST(test_opaq, setup),
777 UTEST(test_rpc, setup),
778 UTEST(test_action, setup),
779 UTEST(test_notification, setup),
780 UTEST(test_reply, setup),
Radek Krejci1798aae2020-07-14 13:26:06 +0200781 };
782
783 return cmocka_run_group_tests(tests, NULL, NULL);
784}