blob: 37d9d43b17f49585d5fe8590cb92958fd1d4043e [file] [log] [blame]
Michal Vasko5b414dd2023-04-13 10:29:58 +02001/**
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_parser_json.c
Michal Vasko5b414dd2023-04-13 10:29:58 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief unit tests for JSON parser
Radek Krejci1798aae2020-07-14 13:26:06 +02006 *
Michal Vasko5b414dd2023-04-13 10:29:58 +02007 * Copyright (c) 2019 - 2023 CESNET, z.s.p.o.
Radek Krejci1798aae2020-07-14 13:26:06 +02008 *
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ša56ca9e42020-09-08 18:42:00 +020015#define _UTEST_MAIN_
16#include "utests.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020017
Radek Krejci1798aae2020-07-14 13:26:06 +020018#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020019#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020020#include "out.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010021#include "parser_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020022#include "printer_data.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020023#include "tests_config.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020024#include "tree_data_internal.h"
25#include "tree_schema.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020026
27static int
28setup(void **state)
29{
Radek Iša56ca9e42020-09-08 18:42:00 +020030 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 +020031 "md:annotation hint { type int8;}"
Michal Vaskodbf3e652022-10-21 08:46:25 +020032 "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 Krejci1798aae2020-07-14 13:26:06 +020036 "leaf foo { type string;}"
37 "container c {"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010038 " 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 Krejci1798aae2020-07-14 13:26:06 +020041 "}"
42 "container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}"
43 "anydata any {config false;}"
Michal Vasko76096ec2022-02-24 16:06:16 +010044 "anyxml axml;"
Radek Krejci1798aae2020-07-14 13:26:06 +020045 "leaf-list ll1 { type uint8; }"
46 "leaf foo2 { type string; default \"default-val\"; }"
47 "leaf foo3 { type uint32; }"
Michal Vasko583b4642023-05-25 10:39:34 +020048 "leaf foo4 { type uint64; }"
Michal Vasko5df28522023-08-25 08:04:37 +020049 "rpc r1 {input {leaf l1 {type string;} leaf l2 {type string;}}}"
Radek Krejci1798aae2020-07-14 13:26:06 +020050 "notification n2;}";
Radek Krejci1798aae2020-07-14 13:26:06 +020051
Radek Iša56ca9e42020-09-08 18:42:00 +020052 UTEST_SETUP;
Radek Krejci1798aae2020-07-14 13:26:06 +020053
Radek Iša56ca9e42020-09-08 18:42:00 +020054 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
55 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Radek Krejci1798aae2020-07-14 13:26:06 +020056
57 return 0;
58}
59
Radek Iša56ca9e42020-09-08 18:42:00 +020060#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
61 CHECK_PARSE_LYD_PARAM(INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci1798aae2020-07-14 13:26:06 +020062
Radek Iša56ca9e42020-09-08 18:42:00 +020063#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH) \
64 assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
65 CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH);\
66 assert_null(MODEL)
Radek Krejci1798aae2020-07-14 13:26:06 +020067
Radek Iša56ca9e42020-09-08 18:42:00 +020068#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
69 CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_JSON, PRINT_OPTION)
Radek Krejci1798aae2020-07-14 13:26:06 +020070
71static void
72test_leaf(void **state)
73{
Radek Krejci1798aae2020-07-14 13:26:06 +020074 struct lyd_node *tree;
75 struct lyd_node_term *leaf;
Radek Iša56ca9e42020-09-08 18:42:00 +020076 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +020077
Radek Iša56ca9e42020-09-08 18:42:00 +020078 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010079
Radek Iša56ca9e42020-09-08 18:42:00 +020080 data = "{\"a:foo\":\"foo value\"}";
81 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
82 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 +010083 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +020084 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
Radek Krejci1798aae2020-07-14 13:26:06 +020085
Radek Iša56ca9e42020-09-08 18:42:00 +020086 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
87 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010088 leaf = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020089
90 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +020091 assert_true(leaf->flags & LYD_DEFAULT);
92
Radek Iša56ca9e42020-09-08 18:42:00 +020093 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +020094 lyd_free_all(tree);
95
96 /* make foo2 explicit */
97 data = "{\"a:foo2\":\"default-val\"}";
Radek Iša56ca9e42020-09-08 18:42:00 +020098 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +020099 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100100 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200101 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
102 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100103 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200104 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +0200105 assert_false(leaf->flags & LYD_DEFAULT);
106
Radek Iša56ca9e42020-09-08 18:42:00 +0200107 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200108 lyd_free_all(tree);
109
110 /* parse foo2 but make it implicit */
Radek Krejci5536d282020-08-04 23:27:44 +0200111 data = "{\"a:foo2\":\"default-val\",\"@a:foo2\":{\"ietf-netconf-with-defaults:default\":true}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200112 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200113 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100114 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200115 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
116 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100117 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200118 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +0200119 assert_true(leaf->flags & LYD_DEFAULT);
120
Michal Vaskob68c3b42022-05-20 10:36:40 +0200121 /* print default values */
122 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL_TAG, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200123 lyd_free_all(tree);
124
Michal Vaskoa4af6252022-05-20 10:38:54 +0200125 /* skip leaf */
126 data = "{\"a:cp\":{\"x\":\"val\",\"y\":\"valy\",\"z\":5}}";
127 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
128 assert_non_null(tree);
129
130 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:cp\":{\"y\":\"valy\",\"z\":5}}");
131 lyd_free_all(tree);
132
Radek Krejci1798aae2020-07-14 13:26:06 +0200133 /* multiple meatadata hint and unknown metadata xxx supposed to be skipped since it is from missing schema */
Radek Krejci5536d282020-08-04 23:27:44 +0200134 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 +0200135 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
136 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
137 CHECK_LYD_META(tree->meta, 1, "hint", 1, 1, INT8, "1", 1);
138 CHECK_LYD_META(tree->meta->next, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200139 assert_null(tree->meta->next->next);
140
Radek Iša56ca9e42020-09-08 18:42:00 +0200141 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
142 "{\"a:foo\":\"xxx\",\"@a:foo\":{\"a:hint\":1,\"a:hint\":2}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200143 lyd_free_all(tree);
144
Radek Iša56ca9e42020-09-08 18:42:00 +0200145 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200146 "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 +0200147
148 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200149 PARSER_CHECK_ERROR("{\"@a:foo\" : { \"a:hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200150 "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 +0200151
152 /* missing namespace for meatadata*/
Radek Iša56ca9e42020-09-08 18:42:00 +0200153 PARSER_CHECK_ERROR("{\"a:foo\" : \"value\", \"@a:foo\" : { \"hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200154 "Metadata in JSON must be namespace-qualified, missing prefix for \"hint\".",
155 "Schema location \"/a:foo\", line number 1.");
aPiecek49e2a3a2021-05-13 10:36:46 +0200156
Michal Vaskodbf3e652022-10-21 08:46:25 +0200157 /* invalid JSON type */
158 data = "{\"a:l1\" : [{ \"a\" : \"val-a\", \"b\" : \"val-b\", \"c\" : 1, \"cont\" : { \"e\" : \"0\" } }]}";
159 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
160 "Invalid non-boolean-encoded boolean value \"0\".",
161 "Data location \"/a:l1[a='val-a'][b='val-b'][c='1']/cont/e\", line number 1.");
162
aPiecek49e2a3a2021-05-13 10:36:46 +0200163 /* reverse solidus in JSON object member name */
164 data = "{\"@a:foo\":{\"a:hi\\nt\":1},\"a:foo\":\"xxx\"}";
165 assert_int_equal(LY_EINVAL, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko62af3692023-02-09 14:00:09 +0100166 CHECK_LOG_CTX("Annotation definition for attribute \"a:hi\nt\" not found.", "Data location \"/@a:foo\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200167}
168
169static void
170test_leaflist(void **state)
171{
Radek Iša56ca9e42020-09-08 18:42:00 +0200172 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200173 struct lyd_node *tree;
174 struct lyd_node_term *ll;
175
Radek Iša56ca9e42020-09-08 18:42:00 +0200176 data = "{\"a:ll1\":[10,11]}";
177 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200178 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100179 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200180 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
181 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100182 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200183 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200184
185 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200186 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
187 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100188 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200189 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
Radek Krejci1798aae2020-07-14 13:26:06 +0200190
Radek Iša56ca9e42020-09-08 18:42:00 +0200191 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200192 lyd_free_all(tree);
193
Michal Vasko4e26adc2022-03-30 11:01:16 +0200194 /* accept empty */
195 data = "{\"a:ll1\":[]}";
196 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
197 assert_null(tree);
198
Radek Krejci1798aae2020-07-14 13:26:06 +0200199 /* simple metadata */
Radek Krejci5536d282020-08-04 23:27:44 +0200200 data = "{\"a:ll1\":[10,11],\"@a:ll1\":[null,{\"a:hint\":2}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200201 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200202 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100203 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200204 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
205 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100206 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200207 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200208 assert_null(ll->meta);
209
210 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200211 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
212 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100213 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200214 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
215 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200216 assert_null(ll->meta->next);
217
Radek Iša56ca9e42020-09-08 18:42:00 +0200218 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200219 lyd_free_all(tree);
220
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100221 /* 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 +0200222 data = "{\"@a:ll1\" : [{\"a:hint\" : 1, \"x:xxx\" : { \"value\" : \"/x:no/x:yes\" }, "
223 "\"a:hint\" : 10},null,{\"a:hint\" : 3}], \"a:ll1\" : [1,2,3]}";
224 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200225 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100226 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200227 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
228 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100229 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200230 CHECK_LYD_VALUE(ll->value, UINT8, "1", 1);
231 CHECK_LYD_META(ll->meta, 1, "hint", 1, 1, INT8, "1", 1);
232 CHECK_LYD_META(ll->meta->next, 1, "hint", 0, 1, INT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200233
234 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200235 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
236 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100237 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200238 CHECK_LYD_VALUE(ll->value, UINT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200239 assert_null(ll->meta);
240
241 assert_non_null(tree->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200242 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
243 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100244 ll = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200245 CHECK_LYD_VALUE(ll->value, UINT8, "3", 3);
246 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "3", 3);
Radek Krejci1798aae2020-07-14 13:26:06 +0200247 assert_null(ll->meta->next);
248
Radek Iša56ca9e42020-09-08 18:42:00 +0200249 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
250 "{\"a:ll1\":[1,2,3],\"@a:ll1\":[{\"a:hint\":1,\"a:hint\":10},null,{\"a:hint\":3}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200251 lyd_free_all(tree);
252
253 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200254 PARSER_CHECK_ERROR("{\"@a:ll1\":[{\"a:hint\":1}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200255 "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 +0200256
Radek Iša56ca9e42020-09-08 18:42:00 +0200257 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 +0200258 "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 +0200259
Radek Iša56ca9e42020-09-08 18:42:00 +0200260 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 +0200261 tree, LY_EVALID, "Missing JSON data instance #3 to be coupled with @a:ll1 metadata.",
262 "Data location \"/@a:ll1\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200263}
264
265static void
266test_anydata(void **state)
267{
Radek Krejci1798aae2020-07-14 13:26:06 +0200268 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200269 struct lyd_node *tree;
270
Radek Krejci1798aae2020-07-14 13:26:06 +0200271 data = "{\"a:any\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200272 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200273 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100274 tree = tree->next;
Michal Vasko76096ec2022-02-24 16:06:16 +0100275 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 +0200276 1, LYS_ANYDATA, 0, 0, NULL, 0);
277 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200278 lyd_free_all(tree);
Michal Vaskofbc4a252022-02-03 13:15:58 +0100279
280 data = "{\"a:any\":{}}";
281 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
282 assert_non_null(tree);
283 tree = tree->next;
Michal Vasko76096ec2022-02-24 16:06:16 +0100284 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 +0100285 1, LYS_ANYDATA, 0, 0, NULL, 0);
286 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
287 lyd_free_all(tree);
Michal Vasko76096ec2022-02-24 16:06:16 +0100288
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100289 data = "{\"a:any\":{\"node\":20}}";
Michal Vasko76096ec2022-02-24 16:06:16 +0100290 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
291 assert_non_null(tree);
292 tree = tree->next;
293 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any",
294 1, LYS_ANYDATA, 0, 0, NULL, 0);
295 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
296 lyd_free_all(tree);
297}
298
299static void
300test_anyxml(void **state)
301{
302 const char *data;
303 struct lyd_node *tree;
304
305 data = "{\"a:axml\":\"some-value in string\"}";
306 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
307 assert_non_null(tree);
308 tree = tree->next;
309 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
310 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
311 lyd_free_all(tree);
312
313 data = "{\"a:axml\":\"\"}";
314 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
315 assert_non_null(tree);
316 tree = tree->next;
317 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
318 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
319 lyd_free_all(tree);
320
321 data = "{\"a:axml\":55}";
322 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
323 assert_non_null(tree);
324 tree = tree->next;
325 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
326 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
327 lyd_free_all(tree);
328
329 data = "{\"a:axml\":false}";
330 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
331 assert_non_null(tree);
332 tree = tree->next;
333 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
334 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
335 lyd_free_all(tree);
336
337 data = "{\"a:axml\":null}";
338 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
339 assert_non_null(tree);
340 tree = tree->next;
341 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
342 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
343 lyd_free_all(tree);
344
345 data = "{\"a:axml\":[null,true,false]}";
346 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
347 assert_non_null(tree);
348 tree = tree->next;
349 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
350 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
351 lyd_free_all(tree);
352
353 data = "{\"a:axml\":[null,true,{\"name\":[25,40, false]}]}";
354 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
355 assert_non_null(tree);
356 tree = tree->next;
357 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
358 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
359 lyd_free_all(tree);
360
361 /* same as anydata tests */
362 data = "{\"a:axml\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
363 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
364 assert_non_null(tree);
365 tree = tree->next;
366 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
367 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
368 lyd_free_all(tree);
369
370 data = "{\"a:axml\":{}}";
371 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
372 assert_non_null(tree);
373 tree = tree->next;
374 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
375 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
376 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200377}
378
379static void
380test_list(void **state)
381{
Radek Iša56ca9e42020-09-08 18:42:00 +0200382 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200383 struct lyd_node *tree, *iter;
384 struct lyd_node_inner *list;
385 struct lyd_node_term *leaf;
386
387 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200388 data = "{\"a:l1\":[{\"a\":\"one\",\"b\":\"one\",\"c\":1}]}";
389 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
390 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
391 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100392 list = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200393 LY_LIST_FOR(list->child, iter) {
394 assert_int_not_equal(0, iter->hash);
395 }
Radek Iša56ca9e42020-09-08 18:42:00 +0200396 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200397 lyd_free_all(tree);
398
Michal Vasko4e26adc2022-03-30 11:01:16 +0200399 /* accept empty */
400 data = "{\"a:l1\":[]}";
401 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
402 assert_null(tree);
403
Radek Krejci1798aae2020-07-14 13:26:06 +0200404 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200405 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200406 "List instance is missing its key \"a\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200407 "Data location \"/a:l1[b='b'][c='1']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200408
Radek Iša56ca9e42020-09-08 18:42:00 +0200409 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200410 "List instance is missing its key \"b\".", "Data location \"/a:l1[a='a']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200411
Radek Iša56ca9e42020-09-08 18:42:00 +0200412 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"b\" : \"b\", \"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200413 "List instance is missing its key \"c\".", "Data location \"/a:l1[a='a'][b='b']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200414
415 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200416 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\", \"a\" : \"a\", \"c\" : 1}]}", 0, LYD_VALIDATE_PRESENT,
Michal Vasko959f8d82022-06-16 07:51:50 +0200417 tree, LY_EVALID, "Duplicate instance of \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200418 "Data location \"/a:l1[a='a'][b='b'][c='1'][c='1']/c\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200419
420 /* keys order, in contrast to XML, JSON accepts keys in any order even in strict mode */
Radek Iša56ca9e42020-09-08 18:42:00 +0200421 CHECK_PARSE_LYD("{ \"a:l1\": [ {\"d\" : \"d\", \"a\" : \"a\", \"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree);
422 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
423 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100424 list = (struct lyd_node_inner *)tree;
425 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200426 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 +0100427 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200428 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 +0100429 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200430 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 +0100431 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200432 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "d", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200433 CHECK_LOG_CTX(NULL, NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200434 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
435 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1,\"d\":\"d\"}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200436 lyd_free_all(tree);
437
Radek Iša56ca9e42020-09-08 18:42:00 +0200438 CHECK_PARSE_LYD("{\"a:l1\":[{\"c\":1,\"b\":\"b\",\"a\":\"a\"}]}", LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree);
439 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
440 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100441 list = (struct lyd_node_inner *)tree;
442 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200443 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a",
444 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100445 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200446 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b",
447 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100448 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200449 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c",
450 1, LYS_LEAF, 1, 0, NULL, 0);
451 CHECK_LOG_CTX(NULL, NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200452 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
453 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200454 lyd_free_all(tree);
455
Michal Vasko87c5d6b2023-02-14 15:27:41 +0100456 /* skip unknown nested nodes */
457 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":3,\"counters\":{\"count1\":\"c1\",\"count2\":\"c2\"}}]}";
458 CHECK_PARSE_LYD(data, LYD_PARSE_ONLY, 0, tree);
459 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":3}]}");
460 lyd_free_all(tree);
461
Radek Krejci5536d282020-08-04 23:27:44 +0200462 data = "{\"a:cp\":{\"@\":{\"a:hint\":1}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200463 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200464 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100465 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100466 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 +0200467 1, LYS_CONTAINER, 0, 0, NULL, 0);
468 CHECK_LYD_META(tree->meta, 1, "hint", 0, 1, INT8, "1", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200469 assert_null(tree->meta->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200470 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200471 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200472}
473
474static void
475test_container(void **state)
476{
Radek Iša56ca9e42020-09-08 18:42:00 +0200477 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200478 struct lyd_node *tree;
479 struct lyd_node_inner *cont;
480
Radek Iša56ca9e42020-09-08 18:42:00 +0200481 CHECK_PARSE_LYD("{\"a:c\":{}}", 0, LYD_VALIDATE_PRESENT, tree);
482 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c",
483 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100484 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200485 assert_true(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200486
Radek Iša56ca9e42020-09-08 18:42:00 +0200487 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200488 lyd_free_all(tree);
489
Radek Krejci5536d282020-08-04 23:27:44 +0200490 data = "{\"a:cp\":{}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200491 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200492 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100493 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100494 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 +0200495 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100496 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200497 assert_false(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200498
Radek Iša56ca9e42020-09-08 18:42:00 +0200499 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200500 lyd_free_all(tree);
Michal Vaskoa4af6252022-05-20 10:38:54 +0200501
502 /* skip container */
503 CHECK_PARSE_LYD("{\"a:unknown\":{\"a\":\"val\",\"b\":5}}", 0, LYD_VALIDATE_PRESENT, tree);
504 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
505 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200506}
507
508static void
509test_opaq(void **state)
510{
Radek Krejci1798aae2020-07-14 13:26:06 +0200511 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200512 struct lyd_node *tree;
513
Radek Krejci1798aae2020-07-14 13:26:06 +0200514 /* invalid value, no flags */
515 data = "{\"a:foo3\":[null]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200516 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200517 "Invalid non-number-encoded uint32 value \"\".", "Schema location \"/a:foo3\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200518
519 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200520 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200521 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 +0200522 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200523 lyd_free_all(tree);
524
Michal Vasko5e2742d2023-05-25 10:01:42 +0200525 /* special chars */
526 data = "{\"a:foo3\":\"ab\\\"\\\\\\r\\t\"}";
527 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
528 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
529 lyd_free_all(tree);
530
Michal Vasko583b4642023-05-25 10:39:34 +0200531 /* wrong encoding */
532 data = "{\"a:foo3\":\"25\"}";
533 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
534 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo3", 0, 0, NULL, 0, "25");
535 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
536 lyd_free_all(tree);
537
538 data = "{\"a:foo4\":25}";
539 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
540 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo4", 0, 0, NULL, 0, "25");
541 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
542 lyd_free_all(tree);
543
Radek Krejci1798aae2020-07-14 13:26:06 +0200544 /* missing key, no flags */
545 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"d\":\"val_d\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200546 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200547 "List instance is missing its key \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200548 "Data location \"/a:l1[a='val_a'][b='val_b']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200549
550 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200551 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200552 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 +0200553 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200554 lyd_free_all(tree);
555
556 /* invalid key, no flags */
557 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200558 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200559 "Invalid non-number-encoded int16 value \"val_c\".",
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200560 "Data location \"/a:l1[a='val_a'][b='val_b']/c\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200561
562 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200563 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200564 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 +0200565 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200566 lyd_free_all(tree);
567
568 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":{\"val\":\"val_c\"}}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200569 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200570 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 +0200571 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200572 lyd_free_all(tree);
573
574 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\"}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200575 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200576 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 +0200577 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200578 lyd_free_all(tree);
aPiecekb7b29e62021-05-11 10:02:43 +0200579
Michal Vasko6a6e3082022-05-10 10:32:38 +0200580 /* invalid metadata */
aPiecekc5f36a72021-05-18 14:12:31 +0200581 data = "{\"@a:foo\":\"str\",\"@a:foo3\":1,\"a:foo3\":2}";
Michal Vasko6a6e3082022-05-10 10:32:38 +0200582 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko6727c682023-02-17 10:40:26 +0100583 "Unknown module of node \"@a:foo\".", "Path \"/\".");
Michal Vasko62af3692023-02-09 14:00:09 +0100584 CHECK_LOG_CTX("Missing JSON data instance to be coupled with @a:foo metadata.", "Data location \"/@a:foo\", line number 1.");
aPiecekc5f36a72021-05-18 14:12:31 +0200585
aPiecekb7b29e62021-05-11 10:02:43 +0200586 /* empty name */
587 PARSER_CHECK_ERROR("{\"@a:foo\":{\"\":0}}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskoa878a892023-08-18 12:22:07 +0200588 "JSON object member name cannot be a zero-length string.", "Data location \"/@a:foo\", line number 1.");
Michal Vasko1a85d332021-08-27 10:35:28 +0200589
590 /* opaque data tree format print */
591 data =
592 "{\n"
593 " \"ietf-netconf-nmda:get-data\": {\n"
594 " \"data\": {\n"
595 " \"ietf-keystore:keystore\": {\n"
596 " \"asymmetric-keys\": {\n"
597 " \"asymmetric-key\": [\n"
598 " {\n"
599 " \"name\": \"genkey\",\n"
600 " \"algorithm\": \"rsa2048\"\n"
601 " }\n"
602 " ]\n"
603 " }\n"
604 " },\n"
605 " \"ietf-netconf-server:netconf-server\": {\n"
606 " \"listen\": {\n"
607 " \"idle-timeout\": 3600,\n"
608 " \"endpoint\": [\n"
609 " {\n"
610 " \"name\": \"default-ssh\",\n"
611 " \"ssh\": {\n"
612 " \"tcp-server-parameters\": {\n"
613 " \"local-address\": \"0.0.0.0\",\n"
614 " \"local-port\": 830\n"
615 " },\n"
616 " \"ssh-server-parameters\": {\n"
617 " \"server-identity\": {\n"
618 " \"host-key\": [\n"
619 " {\n"
620 " \"name\": \"default-key\",\n"
621 " \"public-key\": {\n"
622 " \"keystore-reference\": \"genkey\"\n"
623 " }\n"
624 " }\n"
625 " ]\n"
626 " },\n"
627 " \"client-authentication\": {\n"
628 " \"supported-authentication-methods\": {\n"
629 " \"publickey\": [null],\n"
630 " \"passsword\": [null],\n"
631 " \"other\": [\n"
632 " \"interactive\",\n"
633 " \"gssapi\"\n"
634 " ]\n"
635 " }\n"
636 " }\n"
637 " }\n"
638 " }\n"
639 " }\n"
640 " ]\n"
641 " }\n"
642 " }\n"
643 " }\n"
644 " }\n"
645 "}\n";
646 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
647 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
648 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200649}
650
651static void
652test_rpc(void **state)
653{
Radek Krejci1798aae2020-07-14 13:26:06 +0200654 const char *data;
Michal Vasko5df28522023-08-25 08:04:37 +0200655 char *str;
Radek Krejci1798aae2020-07-14 13:26:06 +0200656 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200657 struct lyd_node *tree, *op;
658 const struct lyd_node *node;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100659 const char *dsc = "Edit data in an NMDA datastore.\n"
660 "\n"
661 "If an error condition occurs such that an error severity\n"
662 "<rpc-error> element is generated, the server will stop\n"
663 "processing the <edit-data> operation and restore the\n"
664 "specified configuration to its complete state at\n"
665 "the start of this <edit-data> operation.";
Radek Krejci1798aae2020-07-14 13:26:06 +0200666
Michal Vaskobbdadda2022-01-06 11:40:10 +0100667 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
Radek Krejci1798aae2020-07-14 13:26:06 +0200668
Michal Vaskobbdadda2022-01-06 11:40:10 +0100669 data = "{\"ietf-netconf-nmda:edit-data\":{"
670 "\"datastore\":\"ietf-datastores:running\","
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100671 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
672 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
Michal Vasko2552ea32020-12-08 15:32:34 +0100673 "}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200674 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100675 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 +0200676 ly_in_free(in, 0);
677
678 assert_non_null(op);
Radek Krejci1798aae2020-07-14 13:26:06 +0200679
Radek Krejci2a9fc652021-01-22 17:44:34 +0100680 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100681 1, 0, 0, 1, "edit-data", LYS_RPC,
682 0, 0, 0, 0, 0, NULL, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200683
Michal Vasko2552ea32020-12-08 15:32:34 +0100684 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100685 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100686 1, 0, 0, 1, "edit-data", LYS_RPC,
687 0, 0, 0, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200688 node = lyd_child(node)->next;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100689 CHECK_LYSC_NODE(node->schema, "Inline config content.", 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config",
690 0, LYS_ANYDATA, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200691
692 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100693 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 +0200694 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200695 node = lyd_child(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200696 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200697 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 +0200698 node = node->parent->next;
699 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200700 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 +0200701
Radek Iša56ca9e42020-09-08 18:42:00 +0200702 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200703 lyd_free_all(tree);
704
Michal Vasko5df28522023-08-25 08:04:37 +0200705 /* append to parent */
706 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:r1", NULL, 0, &op));
707 assert_int_equal(LY_SUCCESS, ly_in_new_memory("{\"l1\": \"some str\", \"l2\": \"some other str\"}", &in));
708 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, op, in, LYD_JSON, LYD_TYPE_RPC_YANG, &tree, NULL));
709 ly_in_free(in, 0);
710
711 assert_int_equal(LY_SUCCESS, lyd_print_mem(&str, op, LYD_JSON, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK));
712 lyd_free_tree(op);
713 assert_string_equal(str, "{\"a:r1\":{\"l1\":\"some str\",\"l2\":\"some other str\"}}");
714 free(str);
Radek Krejci1798aae2020-07-14 13:26:06 +0200715}
716
717static void
718test_action(void **state)
719{
Radek Krejci1798aae2020-07-14 13:26:06 +0200720 const char *data;
721 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200722 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200723
Michal Vasko2552ea32020-12-08 15:32:34 +0100724 data = "{\"a:c\":{\"act\":{\"al\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200725 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100726 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 +0200727 ly_in_free(in, 0);
728
729 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100730 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200731 1, 0, 0, 1, "act", LYS_ACTION,
732 1, 0, 0, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200733
Radek Iša56ca9e42020-09-08 18:42:00 +0200734 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200735 lyd_free_all(tree);
736
737 /* wrong namespace, element name, whatever... */
738 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200739}
740
741static void
742test_notification(void **state)
743{
Radek Krejci1798aae2020-07-14 13:26:06 +0200744 const char *data;
745 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200746 struct lyd_node *tree, *ntf;
Radek Krejci1798aae2020-07-14 13:26:06 +0200747
Michal Vasko2552ea32020-12-08 15:32:34 +0100748 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200749 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100750 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 +0200751 ly_in_free(in, 0);
752
753 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100754 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 +0200755
Michal Vasko2552ea32020-12-08 15:32:34 +0100756 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 +0200757
Radek Iša56ca9e42020-09-08 18:42:00 +0200758 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200759 lyd_free_all(tree);
760
Radek Krejci1798aae2020-07-14 13:26:06 +0200761 data = "{\"a:n2\":{}}";
762 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100763 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 +0200764 ly_in_free(in, 0);
765
766 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100767 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 +0200768
769 assert_non_null(tree);
770 assert_ptr_equal(ntf, tree);
771
Radek Iša56ca9e42020-09-08 18:42:00 +0200772 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200773 lyd_free_all(tree);
774
775 /* wrong namespace, element name, whatever... */
776 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200777}
778
779static void
780test_reply(void **state)
781{
Radek Krejci1798aae2020-07-14 13:26:06 +0200782 const char *data;
783 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100784 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200785 const struct lyd_node *node;
786
Michal Vasko2552ea32020-12-08 15:32:34 +0100787 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200788 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100789 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 +0200790 ly_in_free(in, 0);
791
Radek Krejci1798aae2020-07-14 13:26:06 +0200792 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100793 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200794 1, 0, 0, 1, "act", LYS_ACTION,
795 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200796 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100797 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 +0200798
Michal Vasko2552ea32020-12-08 15:32:34 +0100799 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 +0200800
801 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200802 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:al\":25}");
Michal Vasko2552ea32020-12-08 15:32:34 +0100803 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:c\":{\"act\":{\"al\":25}}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200804 lyd_free_all(tree);
805
806 /* wrong namespace, element name, whatever... */
807 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200808}
809
Michal Vasko820efe82023-05-12 15:47:43 +0200810static void
811test_restconf_rpc(void **state)
812{
813 const char *data;
814 struct ly_in *in;
815 struct lyd_node *tree, *envp;
816
817 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
818
819 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/ietf-netconf-nmda:edit-data", NULL, 0, &tree));
820
821 data = "{\"ietf-netconf-nmda:input\":{"
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 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
827 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, tree, in, LYD_JSON, LYD_TYPE_RPC_RESTCONF, &envp, NULL));
828 ly_in_free(in, 0);
829
830 /* the same just connected to the edit-data RPC */
831 data = "{\"ietf-netconf-nmda:edit-data\":{"
832 "\"datastore\":\"ietf-datastores:running\","
833 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
834 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
835 "}}";
836 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
837 lyd_free_all(tree);
838 lyd_free_all(envp);
839}
840
841static void
842test_restconf_notification(void **state)
843{
844 const char *data;
845 struct ly_in *in;
846 struct lyd_node *tree, *ntf;
847
848 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\",\"a:c\":{\"n1\":{\"nl\":\"value\"}}}}";
849 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
850 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
851 ly_in_free(in, 0);
852
853 /* envelopes separately */
854 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\"}}";
855 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
856
857 /* notification with the parent node */
858 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
859 CHECK_LYD_STRING(lyd_parent(ntf), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
860
861 lyd_free_all(tree);
862 lyd_free_all(ntf);
Michal Vasko4a1e3e82023-09-05 08:45:01 +0200863
864 /* wrong order */
865 data = "{\"ietf-restconf:notification\":{\"a:n2\":{},\"eventTime\":\"2013-12-21T00:01:00Z\"}}";
866 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
867 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
868 ly_in_free(in, 0);
869
870 lyd_free_all(tree);
871 lyd_free_all(ntf);
Michal Vasko32d3d972023-09-05 13:37:03 +0200872
873 /* unknown notification */
874 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\",\"invalid:n2\":{}}}";
875 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
876 assert_int_equal(LY_EVALID, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
877 UTEST_LOG_CTX_CLEAN;
878 ly_in_free(in, 0);
879 lyd_free_all(tree);
Michal Vasko820efe82023-05-12 15:47:43 +0200880}
881
882static void
883test_restconf_reply(void **state)
884{
885 const char *data;
886 struct ly_in *in;
887 struct lyd_node *tree, *envp;
888
889 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:c/act", NULL, 0, &tree));
890
891 data = "{\"a:output\":{\"al\":25}}";
892 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
893 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, lyd_child(tree), in, LYD_JSON, LYD_TYPE_REPLY_RESTCONF, &envp, NULL));
894 ly_in_free(in, 0);
895
896 /* connected to the RPC with the parent */
897 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
898 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
899 lyd_free_all(tree);
900 lyd_free_all(envp);
901}
902
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100903int
904main(void)
Radek Krejci1798aae2020-07-14 13:26:06 +0200905{
906 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200907 UTEST(test_leaf, setup),
908 UTEST(test_leaflist, setup),
909 UTEST(test_anydata, setup),
Michal Vasko76096ec2022-02-24 16:06:16 +0100910 UTEST(test_anyxml, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200911 UTEST(test_list, setup),
912 UTEST(test_container, setup),
913 UTEST(test_opaq, setup),
914 UTEST(test_rpc, setup),
915 UTEST(test_action, setup),
916 UTEST(test_notification, setup),
917 UTEST(test_reply, setup),
Michal Vasko820efe82023-05-12 15:47:43 +0200918 UTEST(test_restconf_rpc, setup),
919 UTEST(test_restconf_notification, setup),
920 UTEST(test_restconf_reply, setup),
Radek Krejci1798aae2020-07-14 13:26:06 +0200921 };
922
923 return cmocka_run_group_tests(tests, NULL, NULL);
924}