blob: e5513dafe970290fb229b925e5247aeaec8925c7 [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; }"
Radek Krejci1798aae2020-07-14 13:26:06 +020049 "notification n2;}";
Radek Krejci1798aae2020-07-14 13:26:06 +020050
Radek Iša56ca9e42020-09-08 18:42:00 +020051 UTEST_SETUP;
Radek Krejci1798aae2020-07-14 13:26:06 +020052
Radek Iša56ca9e42020-09-08 18:42:00 +020053 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
54 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Radek Krejci1798aae2020-07-14 13:26:06 +020055
56 return 0;
57}
58
Radek Iša56ca9e42020-09-08 18:42:00 +020059#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
60 CHECK_PARSE_LYD_PARAM(INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci1798aae2020-07-14 13:26:06 +020061
Radek Iša56ca9e42020-09-08 18:42:00 +020062#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH) \
63 assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
64 CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH);\
65 assert_null(MODEL)
Radek Krejci1798aae2020-07-14 13:26:06 +020066
Radek Iša56ca9e42020-09-08 18:42:00 +020067#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
68 CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_JSON, PRINT_OPTION)
Radek Krejci1798aae2020-07-14 13:26:06 +020069
70static void
71test_leaf(void **state)
72{
Radek Krejci1798aae2020-07-14 13:26:06 +020073 struct lyd_node *tree;
74 struct lyd_node_term *leaf;
Radek Iša56ca9e42020-09-08 18:42:00 +020075 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +020076
Radek Iša56ca9e42020-09-08 18:42:00 +020077 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010078
Radek Iša56ca9e42020-09-08 18:42:00 +020079 data = "{\"a:foo\":\"foo value\"}";
80 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
81 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010082 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +020083 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
Radek Krejci1798aae2020-07-14 13:26:06 +020084
Radek Iša56ca9e42020-09-08 18:42:00 +020085 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
86 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010087 leaf = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020088
89 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +020090 assert_true(leaf->flags & LYD_DEFAULT);
91
Radek Iša56ca9e42020-09-08 18:42:00 +020092 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +020093 lyd_free_all(tree);
94
95 /* make foo2 explicit */
96 data = "{\"a:foo2\":\"default-val\"}";
Radek Iša56ca9e42020-09-08 18:42:00 +020097 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +020098 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +010099 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200100 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
101 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100102 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200103 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +0200104 assert_false(leaf->flags & LYD_DEFAULT);
105
Radek Iša56ca9e42020-09-08 18:42:00 +0200106 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200107 lyd_free_all(tree);
108
109 /* parse foo2 but make it implicit */
Radek Krejci5536d282020-08-04 23:27:44 +0200110 data = "{\"a:foo2\":\"default-val\",\"@a:foo2\":{\"ietf-netconf-with-defaults:default\":true}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200111 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200112 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100113 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200114 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
115 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100116 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200117 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +0200118 assert_true(leaf->flags & LYD_DEFAULT);
119
Michal Vaskob68c3b42022-05-20 10:36:40 +0200120 /* print default values */
121 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL_TAG, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200122 lyd_free_all(tree);
123
Michal Vaskoa4af6252022-05-20 10:38:54 +0200124 /* skip leaf */
125 data = "{\"a:cp\":{\"x\":\"val\",\"y\":\"valy\",\"z\":5}}";
126 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
127 assert_non_null(tree);
128
129 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:cp\":{\"y\":\"valy\",\"z\":5}}");
130 lyd_free_all(tree);
131
Radek Krejci1798aae2020-07-14 13:26:06 +0200132 /* multiple meatadata hint and unknown metadata xxx supposed to be skipped since it is from missing schema */
Radek Krejci5536d282020-08-04 23:27:44 +0200133 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 +0200134 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
135 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
136 CHECK_LYD_META(tree->meta, 1, "hint", 1, 1, INT8, "1", 1);
137 CHECK_LYD_META(tree->meta->next, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200138 assert_null(tree->meta->next->next);
139
Radek Iša56ca9e42020-09-08 18:42:00 +0200140 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
141 "{\"a:foo\":\"xxx\",\"@a:foo\":{\"a:hint\":1,\"a:hint\":2}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200142 lyd_free_all(tree);
143
Radek Iša56ca9e42020-09-08 18:42:00 +0200144 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200145 "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 +0200146
147 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200148 PARSER_CHECK_ERROR("{\"@a:foo\" : { \"a:hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200149 "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 +0200150
151 /* missing namespace for meatadata*/
Radek Iša56ca9e42020-09-08 18:42:00 +0200152 PARSER_CHECK_ERROR("{\"a:foo\" : \"value\", \"@a:foo\" : { \"hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200153 "Metadata in JSON must be namespace-qualified, missing prefix for \"hint\".",
154 "Schema location \"/a:foo\", line number 1.");
aPiecek49e2a3a2021-05-13 10:36:46 +0200155
Michal Vaskodbf3e652022-10-21 08:46:25 +0200156 /* invalid JSON type */
157 data = "{\"a:l1\" : [{ \"a\" : \"val-a\", \"b\" : \"val-b\", \"c\" : 1, \"cont\" : { \"e\" : \"0\" } }]}";
158 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
159 "Invalid non-boolean-encoded boolean value \"0\".",
160 "Data location \"/a:l1[a='val-a'][b='val-b'][c='1']/cont/e\", line number 1.");
161
aPiecek49e2a3a2021-05-13 10:36:46 +0200162 /* reverse solidus in JSON object member name */
163 data = "{\"@a:foo\":{\"a:hi\\nt\":1},\"a:foo\":\"xxx\"}";
164 assert_int_equal(LY_EINVAL, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko62af3692023-02-09 14:00:09 +0100165 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 +0200166}
167
168static void
169test_leaflist(void **state)
170{
Radek Iša56ca9e42020-09-08 18:42:00 +0200171 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200172 struct lyd_node *tree;
173 struct lyd_node_term *ll;
174
Radek Iša56ca9e42020-09-08 18:42:00 +0200175 data = "{\"a:ll1\":[10,11]}";
176 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200177 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100178 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200179 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
180 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100181 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200182 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200183
184 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200185 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
186 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100187 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200188 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
Radek Krejci1798aae2020-07-14 13:26:06 +0200189
Radek Iša56ca9e42020-09-08 18:42:00 +0200190 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200191 lyd_free_all(tree);
192
Michal Vasko4e26adc2022-03-30 11:01:16 +0200193 /* accept empty */
194 data = "{\"a:ll1\":[]}";
195 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
196 assert_null(tree);
197
Radek Krejci1798aae2020-07-14 13:26:06 +0200198 /* simple metadata */
Radek Krejci5536d282020-08-04 23:27:44 +0200199 data = "{\"a:ll1\":[10,11],\"@a:ll1\":[null,{\"a:hint\":2}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200200 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200201 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100202 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200203 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
204 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100205 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200206 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200207 assert_null(ll->meta);
208
209 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200210 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
211 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100212 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200213 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
214 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200215 assert_null(ll->meta->next);
216
Radek Iša56ca9e42020-09-08 18:42:00 +0200217 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200218 lyd_free_all(tree);
219
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100220 /* 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 +0200221 data = "{\"@a:ll1\" : [{\"a:hint\" : 1, \"x:xxx\" : { \"value\" : \"/x:no/x:yes\" }, "
222 "\"a:hint\" : 10},null,{\"a:hint\" : 3}], \"a:ll1\" : [1,2,3]}";
223 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200224 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100225 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200226 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
227 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100228 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200229 CHECK_LYD_VALUE(ll->value, UINT8, "1", 1);
230 CHECK_LYD_META(ll->meta, 1, "hint", 1, 1, INT8, "1", 1);
231 CHECK_LYD_META(ll->meta->next, 1, "hint", 0, 1, INT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200232
233 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200234 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
235 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100236 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200237 CHECK_LYD_VALUE(ll->value, UINT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200238 assert_null(ll->meta);
239
240 assert_non_null(tree->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200241 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
242 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100243 ll = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200244 CHECK_LYD_VALUE(ll->value, UINT8, "3", 3);
245 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "3", 3);
Radek Krejci1798aae2020-07-14 13:26:06 +0200246 assert_null(ll->meta->next);
247
Radek Iša56ca9e42020-09-08 18:42:00 +0200248 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
249 "{\"a:ll1\":[1,2,3],\"@a:ll1\":[{\"a:hint\":1,\"a:hint\":10},null,{\"a:hint\":3}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200250 lyd_free_all(tree);
251
252 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200253 PARSER_CHECK_ERROR("{\"@a:ll1\":[{\"a:hint\":1}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200254 "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 +0200255
Radek Iša56ca9e42020-09-08 18:42:00 +0200256 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 +0200257 "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 +0200258
Radek Iša56ca9e42020-09-08 18:42:00 +0200259 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 +0200260 tree, LY_EVALID, "Missing JSON data instance #3 to be coupled with @a:ll1 metadata.",
261 "Data location \"/@a:ll1\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200262}
263
264static void
265test_anydata(void **state)
266{
Radek Krejci1798aae2020-07-14 13:26:06 +0200267 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200268 struct lyd_node *tree;
269
Radek Krejci1798aae2020-07-14 13:26:06 +0200270 data = "{\"a:any\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200271 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200272 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100273 tree = tree->next;
Michal Vasko76096ec2022-02-24 16:06:16 +0100274 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 +0200275 1, LYS_ANYDATA, 0, 0, NULL, 0);
276 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200277 lyd_free_all(tree);
Michal Vaskofbc4a252022-02-03 13:15:58 +0100278
279 data = "{\"a:any\":{}}";
280 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
281 assert_non_null(tree);
282 tree = tree->next;
Michal Vasko76096ec2022-02-24 16:06:16 +0100283 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 +0100284 1, LYS_ANYDATA, 0, 0, NULL, 0);
285 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
286 lyd_free_all(tree);
Michal Vasko76096ec2022-02-24 16:06:16 +0100287
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100288 data = "{\"a:any\":{\"node\":20}}";
Michal Vasko76096ec2022-02-24 16:06:16 +0100289 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
290 assert_non_null(tree);
291 tree = tree->next;
292 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any",
293 1, LYS_ANYDATA, 0, 0, NULL, 0);
294 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
295 lyd_free_all(tree);
296}
297
298static void
299test_anyxml(void **state)
300{
301 const char *data;
302 struct lyd_node *tree;
303
304 data = "{\"a:axml\":\"some-value in string\"}";
305 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
306 assert_non_null(tree);
307 tree = tree->next;
308 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
309 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
310 lyd_free_all(tree);
311
312 data = "{\"a:axml\":\"\"}";
313 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
314 assert_non_null(tree);
315 tree = tree->next;
316 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
317 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
318 lyd_free_all(tree);
319
320 data = "{\"a:axml\":55}";
321 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
322 assert_non_null(tree);
323 tree = tree->next;
324 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
325 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
326 lyd_free_all(tree);
327
328 data = "{\"a:axml\":false}";
329 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
330 assert_non_null(tree);
331 tree = tree->next;
332 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
333 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
334 lyd_free_all(tree);
335
336 data = "{\"a:axml\":null}";
337 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
338 assert_non_null(tree);
339 tree = tree->next;
340 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
341 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
342 lyd_free_all(tree);
343
344 data = "{\"a:axml\":[null,true,false]}";
345 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
346 assert_non_null(tree);
347 tree = tree->next;
348 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
349 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
350 lyd_free_all(tree);
351
352 data = "{\"a:axml\":[null,true,{\"name\":[25,40, false]}]}";
353 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
354 assert_non_null(tree);
355 tree = tree->next;
356 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
357 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
358 lyd_free_all(tree);
359
360 /* same as anydata tests */
361 data = "{\"a:axml\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
362 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
363 assert_non_null(tree);
364 tree = tree->next;
365 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
366 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
367 lyd_free_all(tree);
368
369 data = "{\"a:axml\":{}}";
370 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
371 assert_non_null(tree);
372 tree = tree->next;
373 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
374 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
375 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200376}
377
378static void
379test_list(void **state)
380{
Radek Iša56ca9e42020-09-08 18:42:00 +0200381 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200382 struct lyd_node *tree, *iter;
383 struct lyd_node_inner *list;
384 struct lyd_node_term *leaf;
385
386 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200387 data = "{\"a:l1\":[{\"a\":\"one\",\"b\":\"one\",\"c\":1}]}";
388 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
389 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
390 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100391 list = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200392 LY_LIST_FOR(list->child, iter) {
393 assert_int_not_equal(0, iter->hash);
394 }
Radek Iša56ca9e42020-09-08 18:42:00 +0200395 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200396 lyd_free_all(tree);
397
Michal Vasko4e26adc2022-03-30 11:01:16 +0200398 /* accept empty */
399 data = "{\"a:l1\":[]}";
400 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
401 assert_null(tree);
402
Radek Krejci1798aae2020-07-14 13:26:06 +0200403 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200404 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200405 "List instance is missing its key \"a\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200406 "Data location \"/a:l1[b='b'][c='1']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200407
Radek Iša56ca9e42020-09-08 18:42:00 +0200408 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200409 "List instance is missing its key \"b\".", "Data location \"/a:l1[a='a']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200410
Radek Iša56ca9e42020-09-08 18:42:00 +0200411 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"b\" : \"b\", \"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200412 "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 +0200413
414 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200415 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\", \"a\" : \"a\", \"c\" : 1}]}", 0, LYD_VALIDATE_PRESENT,
Michal Vasko959f8d82022-06-16 07:51:50 +0200416 tree, LY_EVALID, "Duplicate instance of \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200417 "Data location \"/a:l1[a='a'][b='b'][c='1'][c='1']/c\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200418
419 /* keys order, in contrast to XML, JSON accepts keys in any order even in strict mode */
Radek Iša56ca9e42020-09-08 18:42:00 +0200420 CHECK_PARSE_LYD("{ \"a:l1\": [ {\"d\" : \"d\", \"a\" : \"a\", \"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree);
421 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
422 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100423 list = (struct lyd_node_inner *)tree;
424 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200425 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 +0100426 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200427 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 +0100428 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200429 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 +0100430 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200431 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 +0200432 CHECK_LOG_CTX(NULL, NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200433 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
434 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1,\"d\":\"d\"}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200435 lyd_free_all(tree);
436
Radek Iša56ca9e42020-09-08 18:42:00 +0200437 CHECK_PARSE_LYD("{\"a:l1\":[{\"c\":1,\"b\":\"b\",\"a\":\"a\"}]}", LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree);
438 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
439 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100440 list = (struct lyd_node_inner *)tree;
441 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200442 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a",
443 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100444 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200445 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b",
446 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100447 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200448 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c",
449 1, LYS_LEAF, 1, 0, NULL, 0);
450 CHECK_LOG_CTX(NULL, NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200451 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
452 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200453 lyd_free_all(tree);
454
Michal Vasko87c5d6b2023-02-14 15:27:41 +0100455 /* skip unknown nested nodes */
456 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":3,\"counters\":{\"count1\":\"c1\",\"count2\":\"c2\"}}]}";
457 CHECK_PARSE_LYD(data, LYD_PARSE_ONLY, 0, tree);
458 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":3}]}");
459 lyd_free_all(tree);
460
Radek Krejci5536d282020-08-04 23:27:44 +0200461 data = "{\"a:cp\":{\"@\":{\"a:hint\":1}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200462 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200463 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100464 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100465 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 +0200466 1, LYS_CONTAINER, 0, 0, NULL, 0);
467 CHECK_LYD_META(tree->meta, 1, "hint", 0, 1, INT8, "1", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200468 assert_null(tree->meta->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200469 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200470 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200471}
472
473static void
474test_container(void **state)
475{
Radek Iša56ca9e42020-09-08 18:42:00 +0200476 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200477 struct lyd_node *tree;
478 struct lyd_node_inner *cont;
479
Radek Iša56ca9e42020-09-08 18:42:00 +0200480 CHECK_PARSE_LYD("{\"a:c\":{}}", 0, LYD_VALIDATE_PRESENT, tree);
481 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c",
482 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100483 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200484 assert_true(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200485
Radek Iša56ca9e42020-09-08 18:42:00 +0200486 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200487 lyd_free_all(tree);
488
Radek Krejci5536d282020-08-04 23:27:44 +0200489 data = "{\"a:cp\":{}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200490 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200491 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100492 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100493 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 +0200494 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100495 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200496 assert_false(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200497
Radek Iša56ca9e42020-09-08 18:42:00 +0200498 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200499 lyd_free_all(tree);
Michal Vaskoa4af6252022-05-20 10:38:54 +0200500
501 /* skip container */
502 CHECK_PARSE_LYD("{\"a:unknown\":{\"a\":\"val\",\"b\":5}}", 0, LYD_VALIDATE_PRESENT, tree);
503 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
504 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200505}
506
507static void
508test_opaq(void **state)
509{
Radek Krejci1798aae2020-07-14 13:26:06 +0200510 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200511 struct lyd_node *tree;
512
Radek Krejci1798aae2020-07-14 13:26:06 +0200513 /* invalid value, no flags */
514 data = "{\"a:foo3\":[null]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200515 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200516 "Invalid non-number-encoded uint32 value \"\".", "Schema location \"/a:foo3\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200517
518 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200519 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200520 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 +0200521 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200522 lyd_free_all(tree);
523
Michal Vasko5e2742d2023-05-25 10:01:42 +0200524 /* special chars */
525 data = "{\"a:foo3\":\"ab\\\"\\\\\\r\\t\"}";
526 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
527 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
528 lyd_free_all(tree);
529
Michal Vasko583b4642023-05-25 10:39:34 +0200530 /* wrong encoding */
531 data = "{\"a:foo3\":\"25\"}";
532 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
533 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo3", 0, 0, NULL, 0, "25");
534 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
535 lyd_free_all(tree);
536
537 data = "{\"a:foo4\":25}";
538 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
539 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo4", 0, 0, NULL, 0, "25");
540 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
541 lyd_free_all(tree);
542
Radek Krejci1798aae2020-07-14 13:26:06 +0200543 /* missing key, no flags */
544 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"d\":\"val_d\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200545 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200546 "List instance is missing its key \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200547 "Data location \"/a:l1[a='val_a'][b='val_b']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200548
549 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200550 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200551 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 +0200552 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200553 lyd_free_all(tree);
554
555 /* invalid key, no flags */
556 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200557 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200558 "Invalid non-number-encoded int16 value \"val_c\".",
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200559 "Data location \"/a:l1[a='val_a'][b='val_b']/c\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200560
561 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200562 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200563 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 +0200564 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200565 lyd_free_all(tree);
566
567 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":{\"val\":\"val_c\"}}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200568 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200569 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 +0200570 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200571 lyd_free_all(tree);
572
573 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\"}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200574 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200575 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 +0200576 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200577 lyd_free_all(tree);
aPiecekb7b29e62021-05-11 10:02:43 +0200578
Michal Vasko6a6e3082022-05-10 10:32:38 +0200579 /* invalid metadata */
aPiecekc5f36a72021-05-18 14:12:31 +0200580 data = "{\"@a:foo\":\"str\",\"@a:foo3\":1,\"a:foo3\":2}";
Michal Vasko6a6e3082022-05-10 10:32:38 +0200581 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko6727c682023-02-17 10:40:26 +0100582 "Unknown module of node \"@a:foo\".", "Path \"/\".");
Michal Vasko62af3692023-02-09 14:00:09 +0100583 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 +0200584
aPiecekb7b29e62021-05-11 10:02:43 +0200585 /* empty name */
586 PARSER_CHECK_ERROR("{\"@a:foo\":{\"\":0}}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskobfebf662023-02-17 09:07:22 +0100587 "JSON object member name cannot be a zero-length string.", "Line number 1.");
Michal Vasko1a85d332021-08-27 10:35:28 +0200588
589 /* opaque data tree format print */
590 data =
591 "{\n"
592 " \"ietf-netconf-nmda:get-data\": {\n"
593 " \"data\": {\n"
594 " \"ietf-keystore:keystore\": {\n"
595 " \"asymmetric-keys\": {\n"
596 " \"asymmetric-key\": [\n"
597 " {\n"
598 " \"name\": \"genkey\",\n"
599 " \"algorithm\": \"rsa2048\"\n"
600 " }\n"
601 " ]\n"
602 " }\n"
603 " },\n"
604 " \"ietf-netconf-server:netconf-server\": {\n"
605 " \"listen\": {\n"
606 " \"idle-timeout\": 3600,\n"
607 " \"endpoint\": [\n"
608 " {\n"
609 " \"name\": \"default-ssh\",\n"
610 " \"ssh\": {\n"
611 " \"tcp-server-parameters\": {\n"
612 " \"local-address\": \"0.0.0.0\",\n"
613 " \"local-port\": 830\n"
614 " },\n"
615 " \"ssh-server-parameters\": {\n"
616 " \"server-identity\": {\n"
617 " \"host-key\": [\n"
618 " {\n"
619 " \"name\": \"default-key\",\n"
620 " \"public-key\": {\n"
621 " \"keystore-reference\": \"genkey\"\n"
622 " }\n"
623 " }\n"
624 " ]\n"
625 " },\n"
626 " \"client-authentication\": {\n"
627 " \"supported-authentication-methods\": {\n"
628 " \"publickey\": [null],\n"
629 " \"passsword\": [null],\n"
630 " \"other\": [\n"
631 " \"interactive\",\n"
632 " \"gssapi\"\n"
633 " ]\n"
634 " }\n"
635 " }\n"
636 " }\n"
637 " }\n"
638 " }\n"
639 " ]\n"
640 " }\n"
641 " }\n"
642 " }\n"
643 " }\n"
644 "}\n";
645 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
646 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
647 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200648}
649
650static void
651test_rpc(void **state)
652{
Radek Krejci1798aae2020-07-14 13:26:06 +0200653 const char *data;
654 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200655 struct lyd_node *tree, *op;
656 const struct lyd_node *node;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100657 const char *dsc = "Edit data in an NMDA datastore.\n"
658 "\n"
659 "If an error condition occurs such that an error severity\n"
660 "<rpc-error> element is generated, the server will stop\n"
661 "processing the <edit-data> operation and restore the\n"
662 "specified configuration to its complete state at\n"
663 "the start of this <edit-data> operation.";
Radek Krejci1798aae2020-07-14 13:26:06 +0200664
Michal Vaskobbdadda2022-01-06 11:40:10 +0100665 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
Radek Krejci1798aae2020-07-14 13:26:06 +0200666
Michal Vaskobbdadda2022-01-06 11:40:10 +0100667 data = "{\"ietf-netconf-nmda:edit-data\":{"
668 "\"datastore\":\"ietf-datastores:running\","
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100669 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
670 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
Michal Vasko2552ea32020-12-08 15:32:34 +0100671 "}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200672 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100673 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 +0200674 ly_in_free(in, 0);
675
676 assert_non_null(op);
Radek Krejci1798aae2020-07-14 13:26:06 +0200677
Radek Krejci2a9fc652021-01-22 17:44:34 +0100678 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100679 1, 0, 0, 1, "edit-data", LYS_RPC,
680 0, 0, 0, 0, 0, NULL, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200681
Michal Vasko2552ea32020-12-08 15:32:34 +0100682 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100683 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100684 1, 0, 0, 1, "edit-data", LYS_RPC,
685 0, 0, 0, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200686 node = lyd_child(node)->next;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100687 CHECK_LYSC_NODE(node->schema, "Inline config content.", 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config",
688 0, LYS_ANYDATA, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200689
690 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100691 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 +0200692 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200693 node = lyd_child(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200694 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200695 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 +0200696 node = node->parent->next;
697 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200698 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 +0200699
Radek Iša56ca9e42020-09-08 18:42:00 +0200700 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200701 lyd_free_all(tree);
702
703 /* wrong namespace, element name, whatever... */
704 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200705}
706
707static void
708test_action(void **state)
709{
Radek Krejci1798aae2020-07-14 13:26:06 +0200710 const char *data;
711 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200712 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200713
Michal Vasko2552ea32020-12-08 15:32:34 +0100714 data = "{\"a:c\":{\"act\":{\"al\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200715 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100716 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 +0200717 ly_in_free(in, 0);
718
719 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100720 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200721 1, 0, 0, 1, "act", LYS_ACTION,
722 1, 0, 0, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200723
Radek Iša56ca9e42020-09-08 18:42:00 +0200724 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200725 lyd_free_all(tree);
726
727 /* wrong namespace, element name, whatever... */
728 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200729}
730
731static void
732test_notification(void **state)
733{
Radek Krejci1798aae2020-07-14 13:26:06 +0200734 const char *data;
735 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200736 struct lyd_node *tree, *ntf;
Radek Krejci1798aae2020-07-14 13:26:06 +0200737
Michal Vasko2552ea32020-12-08 15:32:34 +0100738 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200739 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100740 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 +0200741 ly_in_free(in, 0);
742
743 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100744 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 +0200745
Michal Vasko2552ea32020-12-08 15:32:34 +0100746 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 +0200747
Radek Iša56ca9e42020-09-08 18:42:00 +0200748 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200749 lyd_free_all(tree);
750
Radek Krejci1798aae2020-07-14 13:26:06 +0200751 data = "{\"a:n2\":{}}";
752 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100753 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 +0200754 ly_in_free(in, 0);
755
756 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100757 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 +0200758
759 assert_non_null(tree);
760 assert_ptr_equal(ntf, tree);
761
Radek Iša56ca9e42020-09-08 18:42:00 +0200762 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200763 lyd_free_all(tree);
764
765 /* wrong namespace, element name, whatever... */
766 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200767}
768
769static void
770test_reply(void **state)
771{
Radek Krejci1798aae2020-07-14 13:26:06 +0200772 const char *data;
773 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100774 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200775 const struct lyd_node *node;
776
Michal Vasko2552ea32020-12-08 15:32:34 +0100777 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200778 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100779 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 +0200780 ly_in_free(in, 0);
781
Radek Krejci1798aae2020-07-14 13:26:06 +0200782 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100783 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200784 1, 0, 0, 1, "act", LYS_ACTION,
785 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200786 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100787 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 +0200788
Michal Vasko2552ea32020-12-08 15:32:34 +0100789 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 +0200790
791 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200792 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:al\":25}");
Michal Vasko2552ea32020-12-08 15:32:34 +0100793 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:c\":{\"act\":{\"al\":25}}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200794 lyd_free_all(tree);
795
796 /* wrong namespace, element name, whatever... */
797 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200798}
799
Michal Vasko820efe82023-05-12 15:47:43 +0200800static void
801test_restconf_rpc(void **state)
802{
803 const char *data;
804 struct ly_in *in;
805 struct lyd_node *tree, *envp;
806
807 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
808
809 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/ietf-netconf-nmda:edit-data", NULL, 0, &tree));
810
811 data = "{\"ietf-netconf-nmda:input\":{"
812 "\"datastore\":\"ietf-datastores:running\","
813 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
814 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
815 "}}";
816 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
817 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, tree, in, LYD_JSON, LYD_TYPE_RPC_RESTCONF, &envp, NULL));
818 ly_in_free(in, 0);
819
820 /* the same just connected to the edit-data RPC */
821 data = "{\"ietf-netconf-nmda:edit-data\":{"
822 "\"datastore\":\"ietf-datastores:running\","
823 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
824 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
825 "}}";
826 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
827 lyd_free_all(tree);
828 lyd_free_all(envp);
829}
830
831static void
832test_restconf_notification(void **state)
833{
834 const char *data;
835 struct ly_in *in;
836 struct lyd_node *tree, *ntf;
837
838 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\",\"a:c\":{\"n1\":{\"nl\":\"value\"}}}}";
839 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
840 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
841 ly_in_free(in, 0);
842
843 /* envelopes separately */
844 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\"}}";
845 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
846
847 /* notification with the parent node */
848 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
849 CHECK_LYD_STRING(lyd_parent(ntf), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
850
851 lyd_free_all(tree);
852 lyd_free_all(ntf);
853}
854
855static void
856test_restconf_reply(void **state)
857{
858 const char *data;
859 struct ly_in *in;
860 struct lyd_node *tree, *envp;
861
862 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:c/act", NULL, 0, &tree));
863
864 data = "{\"a:output\":{\"al\":25}}";
865 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
866 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, lyd_child(tree), in, LYD_JSON, LYD_TYPE_REPLY_RESTCONF, &envp, NULL));
867 ly_in_free(in, 0);
868
869 /* connected to the RPC with the parent */
870 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
871 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
872 lyd_free_all(tree);
873 lyd_free_all(envp);
874}
875
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100876int
877main(void)
Radek Krejci1798aae2020-07-14 13:26:06 +0200878{
879 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200880 UTEST(test_leaf, setup),
881 UTEST(test_leaflist, setup),
882 UTEST(test_anydata, setup),
Michal Vasko76096ec2022-02-24 16:06:16 +0100883 UTEST(test_anyxml, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200884 UTEST(test_list, setup),
885 UTEST(test_container, setup),
886 UTEST(test_opaq, setup),
887 UTEST(test_rpc, setup),
888 UTEST(test_action, setup),
889 UTEST(test_notification, setup),
890 UTEST(test_reply, setup),
Michal Vasko820efe82023-05-12 15:47:43 +0200891 UTEST(test_restconf_rpc, setup),
892 UTEST(test_restconf_notification, setup),
893 UTEST(test_restconf_reply, setup),
Radek Krejci1798aae2020-07-14 13:26:06 +0200894 };
895
896 return cmocka_run_group_tests(tests, NULL, NULL);
897}