blob: 8feed9c0975cb2157fb9c9d4321dc30242106e1d [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;}}}"
Michal Vaskodd03ff12023-09-11 10:30:48 +020050 "notification n2;"
51 "}";
Radek Krejci1798aae2020-07-14 13:26:06 +020052
Radek Iša56ca9e42020-09-08 18:42:00 +020053 UTEST_SETUP;
Radek Krejci1798aae2020-07-14 13:26:06 +020054
Radek Iša56ca9e42020-09-08 18:42:00 +020055 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
56 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Radek Krejci1798aae2020-07-14 13:26:06 +020057
58 return 0;
59}
60
Radek Iša56ca9e42020-09-08 18:42:00 +020061#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
62 CHECK_PARSE_LYD_PARAM(INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci1798aae2020-07-14 13:26:06 +020063
Radek Iša56ca9e42020-09-08 18:42:00 +020064#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH) \
65 assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
66 CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH);\
67 assert_null(MODEL)
Radek Krejci1798aae2020-07-14 13:26:06 +020068
Radek Iša56ca9e42020-09-08 18:42:00 +020069#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
70 CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_JSON, PRINT_OPTION)
Radek Krejci1798aae2020-07-14 13:26:06 +020071
72static void
73test_leaf(void **state)
74{
Radek Krejci1798aae2020-07-14 13:26:06 +020075 struct lyd_node *tree;
76 struct lyd_node_term *leaf;
Radek Iša56ca9e42020-09-08 18:42:00 +020077 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +020078
Radek Iša56ca9e42020-09-08 18:42:00 +020079 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010080
Radek Iša56ca9e42020-09-08 18:42:00 +020081 data = "{\"a:foo\":\"foo value\"}";
82 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
83 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 +010084 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +020085 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
Radek Krejci1798aae2020-07-14 13:26:06 +020086
Radek Iša56ca9e42020-09-08 18:42:00 +020087 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
88 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010089 leaf = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020090
91 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +020092 assert_true(leaf->flags & LYD_DEFAULT);
93
Radek Iša56ca9e42020-09-08 18:42:00 +020094 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +020095 lyd_free_all(tree);
96
97 /* make foo2 explicit */
98 data = "{\"a:foo2\":\"default-val\"}";
Radek Iša56ca9e42020-09-08 18:42:00 +020099 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200100 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100101 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200102 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
103 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100104 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200105 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +0200106 assert_false(leaf->flags & LYD_DEFAULT);
107
Radek Iša56ca9e42020-09-08 18:42:00 +0200108 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200109 lyd_free_all(tree);
110
111 /* parse foo2 but make it implicit */
Radek Krejci5536d282020-08-04 23:27:44 +0200112 data = "{\"a:foo2\":\"default-val\",\"@a:foo2\":{\"ietf-netconf-with-defaults:default\":true}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200113 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200114 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100115 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200116 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
117 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100118 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200119 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +0200120 assert_true(leaf->flags & LYD_DEFAULT);
121
Michal Vaskob68c3b42022-05-20 10:36:40 +0200122 /* print default values */
123 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL_TAG, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200124 lyd_free_all(tree);
125
Michal Vaskoa4af6252022-05-20 10:38:54 +0200126 /* skip leaf */
127 data = "{\"a:cp\":{\"x\":\"val\",\"y\":\"valy\",\"z\":5}}";
128 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
129 assert_non_null(tree);
130
131 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:cp\":{\"y\":\"valy\",\"z\":5}}");
132 lyd_free_all(tree);
133
Radek Krejci1798aae2020-07-14 13:26:06 +0200134 /* multiple meatadata hint and unknown metadata xxx supposed to be skipped since it is from missing schema */
Radek Krejci5536d282020-08-04 23:27:44 +0200135 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 +0200136 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
137 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
138 CHECK_LYD_META(tree->meta, 1, "hint", 1, 1, INT8, "1", 1);
139 CHECK_LYD_META(tree->meta->next, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200140 assert_null(tree->meta->next->next);
141
Radek Iša56ca9e42020-09-08 18:42:00 +0200142 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
143 "{\"a:foo\":\"xxx\",\"@a:foo\":{\"a:hint\":1,\"a:hint\":2}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200144 lyd_free_all(tree);
145
Radek Iša56ca9e42020-09-08 18:42:00 +0200146 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200147 "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 +0200148
149 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200150 PARSER_CHECK_ERROR("{\"@a:foo\" : { \"a:hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200151 "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 +0200152
153 /* missing namespace for meatadata*/
Radek Iša56ca9e42020-09-08 18:42:00 +0200154 PARSER_CHECK_ERROR("{\"a:foo\" : \"value\", \"@a:foo\" : { \"hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200155 "Metadata in JSON must be namespace-qualified, missing prefix for \"hint\".",
156 "Schema location \"/a:foo\", line number 1.");
aPiecek49e2a3a2021-05-13 10:36:46 +0200157
Michal Vaskodbf3e652022-10-21 08:46:25 +0200158 /* invalid JSON type */
159 data = "{\"a:l1\" : [{ \"a\" : \"val-a\", \"b\" : \"val-b\", \"c\" : 1, \"cont\" : { \"e\" : \"0\" } }]}";
160 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
161 "Invalid non-boolean-encoded boolean value \"0\".",
162 "Data location \"/a:l1[a='val-a'][b='val-b'][c='1']/cont/e\", line number 1.");
163
aPiecek49e2a3a2021-05-13 10:36:46 +0200164 /* reverse solidus in JSON object member name */
165 data = "{\"@a:foo\":{\"a:hi\\nt\":1},\"a:foo\":\"xxx\"}";
166 assert_int_equal(LY_EINVAL, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vaskodd03ff12023-09-11 10:30:48 +0200167 CHECK_LOG_CTX("Annotation definition for attribute \"a:hi\nt\" not found.", "Path \"/@a:foo/@a:hi\nt\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200168}
169
170static void
171test_leaflist(void **state)
172{
Radek Iša56ca9e42020-09-08 18:42:00 +0200173 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200174 struct lyd_node *tree;
175 struct lyd_node_term *ll;
176
Radek Iša56ca9e42020-09-08 18:42:00 +0200177 data = "{\"a:ll1\":[10,11]}";
178 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200179 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100180 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200181 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
182 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100183 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200184 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200185
186 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200187 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
188 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100189 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200190 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
Radek Krejci1798aae2020-07-14 13:26:06 +0200191
Radek Iša56ca9e42020-09-08 18:42:00 +0200192 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200193 lyd_free_all(tree);
194
Michal Vasko4e26adc2022-03-30 11:01:16 +0200195 /* accept empty */
196 data = "{\"a:ll1\":[]}";
197 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
198 assert_null(tree);
199
Radek Krejci1798aae2020-07-14 13:26:06 +0200200 /* simple metadata */
Radek Krejci5536d282020-08-04 23:27:44 +0200201 data = "{\"a:ll1\":[10,11],\"@a:ll1\":[null,{\"a:hint\":2}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200202 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200203 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100204 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200205 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
206 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100207 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200208 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200209 assert_null(ll->meta);
210
211 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200212 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
213 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100214 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200215 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
216 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200217 assert_null(ll->meta->next);
218
Radek Iša56ca9e42020-09-08 18:42:00 +0200219 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200220 lyd_free_all(tree);
221
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100222 /* 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 +0200223 data = "{\"@a:ll1\" : [{\"a:hint\" : 1, \"x:xxx\" : { \"value\" : \"/x:no/x:yes\" }, "
224 "\"a:hint\" : 10},null,{\"a:hint\" : 3}], \"a:ll1\" : [1,2,3]}";
225 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200226 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100227 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200228 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
229 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100230 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200231 CHECK_LYD_VALUE(ll->value, UINT8, "1", 1);
232 CHECK_LYD_META(ll->meta, 1, "hint", 1, 1, INT8, "1", 1);
233 CHECK_LYD_META(ll->meta->next, 1, "hint", 0, 1, INT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200234
235 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200236 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
237 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100238 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200239 CHECK_LYD_VALUE(ll->value, UINT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200240 assert_null(ll->meta);
241
242 assert_non_null(tree->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200243 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
244 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100245 ll = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200246 CHECK_LYD_VALUE(ll->value, UINT8, "3", 3);
247 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "3", 3);
Radek Krejci1798aae2020-07-14 13:26:06 +0200248 assert_null(ll->meta->next);
249
Radek Iša56ca9e42020-09-08 18:42:00 +0200250 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
251 "{\"a:ll1\":[1,2,3],\"@a:ll1\":[{\"a:hint\":1,\"a:hint\":10},null,{\"a:hint\":3}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200252 lyd_free_all(tree);
253
254 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200255 PARSER_CHECK_ERROR("{\"@a:ll1\":[{\"a:hint\":1}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200256 "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 +0200257
Radek Iša56ca9e42020-09-08 18:42:00 +0200258 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 +0200259 "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 +0200260
Radek Iša56ca9e42020-09-08 18:42:00 +0200261 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 +0200262 tree, LY_EVALID, "Missing JSON data instance #3 to be coupled with @a:ll1 metadata.",
263 "Data location \"/@a:ll1\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200264}
265
266static void
267test_anydata(void **state)
268{
Radek Krejci1798aae2020-07-14 13:26:06 +0200269 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200270 struct lyd_node *tree;
271
Radek Krejci1798aae2020-07-14 13:26:06 +0200272 data = "{\"a:any\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200273 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200274 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100275 tree = tree->next;
Michal Vasko76096ec2022-02-24 16:06:16 +0100276 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 +0200277 1, LYS_ANYDATA, 0, 0, NULL, 0);
278 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200279 lyd_free_all(tree);
Michal Vaskofbc4a252022-02-03 13:15:58 +0100280
281 data = "{\"a:any\":{}}";
282 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
283 assert_non_null(tree);
284 tree = tree->next;
Michal Vasko76096ec2022-02-24 16:06:16 +0100285 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 +0100286 1, LYS_ANYDATA, 0, 0, NULL, 0);
287 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
288 lyd_free_all(tree);
Michal Vasko76096ec2022-02-24 16:06:16 +0100289
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100290 data = "{\"a:any\":{\"node\":20}}";
Michal Vasko76096ec2022-02-24 16:06:16 +0100291 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
292 assert_non_null(tree);
293 tree = tree->next;
294 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any",
295 1, LYS_ANYDATA, 0, 0, NULL, 0);
296 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
297 lyd_free_all(tree);
298}
299
300static void
301test_anyxml(void **state)
302{
303 const char *data;
304 struct lyd_node *tree;
305
306 data = "{\"a:axml\":\"some-value in string\"}";
307 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
308 assert_non_null(tree);
309 tree = tree->next;
310 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
311 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
312 lyd_free_all(tree);
313
314 data = "{\"a:axml\":\"\"}";
315 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
316 assert_non_null(tree);
317 tree = tree->next;
318 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
319 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
320 lyd_free_all(tree);
321
322 data = "{\"a:axml\":55}";
323 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
324 assert_non_null(tree);
325 tree = tree->next;
326 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
327 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
328 lyd_free_all(tree);
329
330 data = "{\"a:axml\":false}";
331 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
332 assert_non_null(tree);
333 tree = tree->next;
334 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
335 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
336 lyd_free_all(tree);
337
338 data = "{\"a:axml\":null}";
339 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
340 assert_non_null(tree);
341 tree = tree->next;
342 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
343 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
344 lyd_free_all(tree);
345
346 data = "{\"a:axml\":[null,true,false]}";
347 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
348 assert_non_null(tree);
349 tree = tree->next;
350 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
351 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
352 lyd_free_all(tree);
353
354 data = "{\"a:axml\":[null,true,{\"name\":[25,40, false]}]}";
355 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
356 assert_non_null(tree);
357 tree = tree->next;
358 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
359 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
360 lyd_free_all(tree);
361
362 /* same as anydata tests */
363 data = "{\"a:axml\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
364 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
365 assert_non_null(tree);
366 tree = tree->next;
367 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
368 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
369 lyd_free_all(tree);
370
371 data = "{\"a:axml\":{}}";
372 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
373 assert_non_null(tree);
374 tree = tree->next;
375 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
376 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
377 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200378}
379
380static void
381test_list(void **state)
382{
Radek Iša56ca9e42020-09-08 18:42:00 +0200383 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200384 struct lyd_node *tree, *iter;
385 struct lyd_node_inner *list;
386 struct lyd_node_term *leaf;
387
388 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200389 data = "{\"a:l1\":[{\"a\":\"one\",\"b\":\"one\",\"c\":1}]}";
390 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
391 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
392 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100393 list = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200394 LY_LIST_FOR(list->child, iter) {
395 assert_int_not_equal(0, iter->hash);
396 }
Radek Iša56ca9e42020-09-08 18:42:00 +0200397 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200398 lyd_free_all(tree);
399
Michal Vasko4e26adc2022-03-30 11:01:16 +0200400 /* accept empty */
401 data = "{\"a:l1\":[]}";
402 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
403 assert_null(tree);
404
Radek Krejci1798aae2020-07-14 13:26:06 +0200405 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200406 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200407 "List instance is missing its key \"a\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200408 "Data location \"/a:l1[b='b'][c='1']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200409
Radek Iša56ca9e42020-09-08 18:42:00 +0200410 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200411 "List instance is missing its key \"b\".", "Data location \"/a:l1[a='a']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200412
Radek Iša56ca9e42020-09-08 18:42:00 +0200413 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"b\" : \"b\", \"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200414 "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 +0200415
416 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200417 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\", \"a\" : \"a\", \"c\" : 1}]}", 0, LYD_VALIDATE_PRESENT,
Michal Vasko959f8d82022-06-16 07:51:50 +0200418 tree, LY_EVALID, "Duplicate instance of \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200419 "Data location \"/a:l1[a='a'][b='b'][c='1'][c='1']/c\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200420
421 /* keys order, in contrast to XML, JSON accepts keys in any order even in strict mode */
Radek Iša56ca9e42020-09-08 18:42:00 +0200422 CHECK_PARSE_LYD("{ \"a:l1\": [ {\"d\" : \"d\", \"a\" : \"a\", \"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree);
423 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
424 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100425 list = (struct lyd_node_inner *)tree;
426 assert_non_null(leaf = (struct lyd_node_term *)list->child);
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, "a", 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, "b", 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);
Radek Iša56ca9e42020-09-08 18:42:00 +0200431 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 +0100432 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200433 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 +0200434 CHECK_LOG_CTX(NULL, NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200435 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
436 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1,\"d\":\"d\"}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200437 lyd_free_all(tree);
438
Radek Iša56ca9e42020-09-08 18:42:00 +0200439 CHECK_PARSE_LYD("{\"a:l1\":[{\"c\":1,\"b\":\"b\",\"a\":\"a\"}]}", LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree);
440 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
441 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100442 list = (struct lyd_node_inner *)tree;
443 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200444 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a",
445 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100446 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200447 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b",
448 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100449 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200450 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c",
451 1, LYS_LEAF, 1, 0, NULL, 0);
452 CHECK_LOG_CTX(NULL, NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200453 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
454 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200455 lyd_free_all(tree);
456
Michal Vasko87c5d6b2023-02-14 15:27:41 +0100457 /* skip unknown nested nodes */
458 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":3,\"counters\":{\"count1\":\"c1\",\"count2\":\"c2\"}}]}";
459 CHECK_PARSE_LYD(data, LYD_PARSE_ONLY, 0, tree);
460 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":3}]}");
461 lyd_free_all(tree);
462
Radek Krejci5536d282020-08-04 23:27:44 +0200463 data = "{\"a:cp\":{\"@\":{\"a:hint\":1}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200464 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200465 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100466 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100467 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 +0200468 1, LYS_CONTAINER, 0, 0, NULL, 0);
469 CHECK_LYD_META(tree->meta, 1, "hint", 0, 1, INT8, "1", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200470 assert_null(tree->meta->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200471 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200472 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200473}
474
475static void
476test_container(void **state)
477{
Radek Iša56ca9e42020-09-08 18:42:00 +0200478 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200479 struct lyd_node *tree;
480 struct lyd_node_inner *cont;
481
Radek Iša56ca9e42020-09-08 18:42:00 +0200482 CHECK_PARSE_LYD("{\"a:c\":{}}", 0, LYD_VALIDATE_PRESENT, tree);
483 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c",
484 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100485 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200486 assert_true(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200487
Radek Iša56ca9e42020-09-08 18:42:00 +0200488 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200489 lyd_free_all(tree);
490
Radek Krejci5536d282020-08-04 23:27:44 +0200491 data = "{\"a:cp\":{}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200492 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200493 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100494 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100495 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 +0200496 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100497 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200498 assert_false(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200499
Radek Iša56ca9e42020-09-08 18:42:00 +0200500 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200501 lyd_free_all(tree);
Michal Vaskoa4af6252022-05-20 10:38:54 +0200502
503 /* skip container */
504 CHECK_PARSE_LYD("{\"a:unknown\":{\"a\":\"val\",\"b\":5}}", 0, LYD_VALIDATE_PRESENT, tree);
505 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
506 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200507}
508
509static void
510test_opaq(void **state)
511{
Radek Krejci1798aae2020-07-14 13:26:06 +0200512 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200513 struct lyd_node *tree;
514
Radek Krejci1798aae2020-07-14 13:26:06 +0200515 /* invalid value, no flags */
516 data = "{\"a:foo3\":[null]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200517 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200518 "Invalid non-number-encoded uint32 value \"\".", "Schema location \"/a:foo3\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200519
520 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200521 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200522 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 +0200523 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200524 lyd_free_all(tree);
525
Michal Vasko5e2742d2023-05-25 10:01:42 +0200526 /* special chars */
527 data = "{\"a:foo3\":\"ab\\\"\\\\\\r\\t\"}";
528 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
529 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
530 lyd_free_all(tree);
531
Michal Vasko583b4642023-05-25 10:39:34 +0200532 /* wrong encoding */
533 data = "{\"a:foo3\":\"25\"}";
534 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
535 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo3", 0, 0, NULL, 0, "25");
536 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
537 lyd_free_all(tree);
538
539 data = "{\"a:foo4\":25}";
540 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
541 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo4", 0, 0, NULL, 0, "25");
542 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
543 lyd_free_all(tree);
544
Radek Krejci1798aae2020-07-14 13:26:06 +0200545 /* missing key, no flags */
546 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"d\":\"val_d\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200547 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200548 "List instance is missing its key \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200549 "Data location \"/a:l1[a='val_a'][b='val_b']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200550
551 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200552 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200553 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 +0200554 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200555 lyd_free_all(tree);
556
557 /* invalid key, no flags */
558 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200559 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200560 "Invalid non-number-encoded int16 value \"val_c\".",
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200561 "Data location \"/a:l1[a='val_a'][b='val_b']/c\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200562
563 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200564 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200565 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 +0200566 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200567 lyd_free_all(tree);
568
569 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":{\"val\":\"val_c\"}}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200570 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200571 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 +0200572 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200573 lyd_free_all(tree);
574
575 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\"}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200576 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200577 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 +0200578 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200579 lyd_free_all(tree);
aPiecekb7b29e62021-05-11 10:02:43 +0200580
Michal Vasko6a6e3082022-05-10 10:32:38 +0200581 /* invalid metadata */
aPiecekc5f36a72021-05-18 14:12:31 +0200582 data = "{\"@a:foo\":\"str\",\"@a:foo3\":1,\"a:foo3\":2}";
Michal Vasko6a6e3082022-05-10 10:32:38 +0200583 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko6727c682023-02-17 10:40:26 +0100584 "Unknown module of node \"@a:foo\".", "Path \"/\".");
Michal Vasko62af3692023-02-09 14:00:09 +0100585 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 +0200586
aPiecekb7b29e62021-05-11 10:02:43 +0200587 /* empty name */
588 PARSER_CHECK_ERROR("{\"@a:foo\":{\"\":0}}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskoa878a892023-08-18 12:22:07 +0200589 "JSON object member name cannot be a zero-length string.", "Data location \"/@a:foo\", line number 1.");
Michal Vasko1a85d332021-08-27 10:35:28 +0200590
591 /* opaque data tree format print */
592 data =
593 "{\n"
594 " \"ietf-netconf-nmda:get-data\": {\n"
595 " \"data\": {\n"
596 " \"ietf-keystore:keystore\": {\n"
597 " \"asymmetric-keys\": {\n"
598 " \"asymmetric-key\": [\n"
599 " {\n"
600 " \"name\": \"genkey\",\n"
601 " \"algorithm\": \"rsa2048\"\n"
602 " }\n"
603 " ]\n"
604 " }\n"
605 " },\n"
606 " \"ietf-netconf-server:netconf-server\": {\n"
607 " \"listen\": {\n"
608 " \"idle-timeout\": 3600,\n"
609 " \"endpoint\": [\n"
610 " {\n"
611 " \"name\": \"default-ssh\",\n"
612 " \"ssh\": {\n"
613 " \"tcp-server-parameters\": {\n"
614 " \"local-address\": \"0.0.0.0\",\n"
615 " \"local-port\": 830\n"
616 " },\n"
617 " \"ssh-server-parameters\": {\n"
618 " \"server-identity\": {\n"
619 " \"host-key\": [\n"
620 " {\n"
621 " \"name\": \"default-key\",\n"
622 " \"public-key\": {\n"
623 " \"keystore-reference\": \"genkey\"\n"
624 " }\n"
625 " }\n"
626 " ]\n"
627 " },\n"
628 " \"client-authentication\": {\n"
629 " \"supported-authentication-methods\": {\n"
630 " \"publickey\": [null],\n"
631 " \"passsword\": [null],\n"
632 " \"other\": [\n"
633 " \"interactive\",\n"
634 " \"gssapi\"\n"
635 " ]\n"
636 " }\n"
637 " }\n"
638 " }\n"
639 " }\n"
640 " }\n"
641 " ]\n"
642 " }\n"
643 " }\n"
644 " }\n"
645 " }\n"
646 "}\n";
647 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
648 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
649 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200650}
651
652static void
653test_rpc(void **state)
654{
Radek Krejci1798aae2020-07-14 13:26:06 +0200655 const char *data;
Michal Vasko5df28522023-08-25 08:04:37 +0200656 char *str;
Radek Krejci1798aae2020-07-14 13:26:06 +0200657 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200658 struct lyd_node *tree, *op;
659 const struct lyd_node *node;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100660 const char *dsc = "Edit data in an NMDA datastore.\n"
661 "\n"
662 "If an error condition occurs such that an error severity\n"
663 "<rpc-error> element is generated, the server will stop\n"
664 "processing the <edit-data> operation and restore the\n"
665 "specified configuration to its complete state at\n"
666 "the start of this <edit-data> operation.";
Radek Krejci1798aae2020-07-14 13:26:06 +0200667
Michal Vaskobbdadda2022-01-06 11:40:10 +0100668 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
Radek Krejci1798aae2020-07-14 13:26:06 +0200669
Michal Vaskobbdadda2022-01-06 11:40:10 +0100670 data = "{\"ietf-netconf-nmda:edit-data\":{"
671 "\"datastore\":\"ietf-datastores:running\","
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100672 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
673 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
Michal Vasko2552ea32020-12-08 15:32:34 +0100674 "}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200675 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100676 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 +0200677 ly_in_free(in, 0);
678
679 assert_non_null(op);
Radek Krejci1798aae2020-07-14 13:26:06 +0200680
Radek Krejci2a9fc652021-01-22 17:44:34 +0100681 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100682 1, 0, 0, 1, "edit-data", LYS_RPC,
683 0, 0, 0, 0, 0, NULL, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200684
Michal Vasko2552ea32020-12-08 15:32:34 +0100685 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100686 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100687 1, 0, 0, 1, "edit-data", LYS_RPC,
688 0, 0, 0, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200689 node = lyd_child(node)->next;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100690 CHECK_LYSC_NODE(node->schema, "Inline config content.", 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config",
691 0, LYS_ANYDATA, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200692
693 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100694 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 +0200695 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200696 node = lyd_child(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200697 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200698 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 +0200699 node = node->parent->next;
700 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200701 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 +0200702
Radek Iša56ca9e42020-09-08 18:42:00 +0200703 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200704 lyd_free_all(tree);
705
Michal Vasko5df28522023-08-25 08:04:37 +0200706 /* append to parent */
707 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:r1", NULL, 0, &op));
708 assert_int_equal(LY_SUCCESS, ly_in_new_memory("{\"l1\": \"some str\", \"l2\": \"some other str\"}", &in));
709 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, op, in, LYD_JSON, LYD_TYPE_RPC_YANG, &tree, NULL));
710 ly_in_free(in, 0);
711
712 assert_int_equal(LY_SUCCESS, lyd_print_mem(&str, op, LYD_JSON, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK));
713 lyd_free_tree(op);
714 assert_string_equal(str, "{\"a:r1\":{\"l1\":\"some str\",\"l2\":\"some other str\"}}");
715 free(str);
Radek Krejci1798aae2020-07-14 13:26:06 +0200716}
717
718static void
719test_action(void **state)
720{
Radek Krejci1798aae2020-07-14 13:26:06 +0200721 const char *data;
722 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200723 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200724
Michal Vasko2552ea32020-12-08 15:32:34 +0100725 data = "{\"a:c\":{\"act\":{\"al\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200726 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100727 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 +0200728 ly_in_free(in, 0);
729
730 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100731 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200732 1, 0, 0, 1, "act", LYS_ACTION,
733 1, 0, 0, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200734
Radek Iša56ca9e42020-09-08 18:42:00 +0200735 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200736 lyd_free_all(tree);
737
738 /* wrong namespace, element name, whatever... */
739 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200740}
741
742static void
743test_notification(void **state)
744{
Radek Krejci1798aae2020-07-14 13:26:06 +0200745 const char *data;
746 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200747 struct lyd_node *tree, *ntf;
Radek Krejci1798aae2020-07-14 13:26:06 +0200748
Michal Vasko2552ea32020-12-08 15:32:34 +0100749 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200750 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100751 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 +0200752 ly_in_free(in, 0);
753
754 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100755 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 +0200756
Michal Vasko2552ea32020-12-08 15:32:34 +0100757 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 +0200758
Radek Iša56ca9e42020-09-08 18:42:00 +0200759 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200760 lyd_free_all(tree);
761
Radek Krejci1798aae2020-07-14 13:26:06 +0200762 data = "{\"a:n2\":{}}";
763 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100764 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 +0200765 ly_in_free(in, 0);
766
767 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100768 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 +0200769
770 assert_non_null(tree);
771 assert_ptr_equal(ntf, tree);
772
Radek Iša56ca9e42020-09-08 18:42:00 +0200773 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200774 lyd_free_all(tree);
775
776 /* wrong namespace, element name, whatever... */
777 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200778}
779
780static void
781test_reply(void **state)
782{
Radek Krejci1798aae2020-07-14 13:26:06 +0200783 const char *data;
784 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100785 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200786 const struct lyd_node *node;
787
Michal Vasko2552ea32020-12-08 15:32:34 +0100788 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200789 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100790 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 +0200791 ly_in_free(in, 0);
792
Radek Krejci1798aae2020-07-14 13:26:06 +0200793 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100794 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200795 1, 0, 0, 1, "act", LYS_ACTION,
796 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200797 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100798 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 +0200799
Michal Vasko2552ea32020-12-08 15:32:34 +0100800 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 +0200801
802 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200803 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:al\":25}");
Michal Vasko2552ea32020-12-08 15:32:34 +0100804 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:c\":{\"act\":{\"al\":25}}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200805 lyd_free_all(tree);
806
807 /* wrong namespace, element name, whatever... */
808 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200809}
810
Michal Vasko820efe82023-05-12 15:47:43 +0200811static void
812test_restconf_rpc(void **state)
813{
814 const char *data;
815 struct ly_in *in;
816 struct lyd_node *tree, *envp;
817
818 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
819
820 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/ietf-netconf-nmda:edit-data", NULL, 0, &tree));
821
822 data = "{\"ietf-netconf-nmda:input\":{"
823 "\"datastore\":\"ietf-datastores:running\","
824 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
825 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
826 "}}";
827 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
828 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, tree, in, LYD_JSON, LYD_TYPE_RPC_RESTCONF, &envp, NULL));
829 ly_in_free(in, 0);
830
831 /* the same just connected to the edit-data RPC */
832 data = "{\"ietf-netconf-nmda:edit-data\":{"
833 "\"datastore\":\"ietf-datastores:running\","
834 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
835 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
836 "}}";
837 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
838 lyd_free_all(tree);
839 lyd_free_all(envp);
840}
841
842static void
843test_restconf_notification(void **state)
844{
845 const char *data;
846 struct ly_in *in;
847 struct lyd_node *tree, *ntf;
848
849 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\",\"a:c\":{\"n1\":{\"nl\":\"value\"}}}}";
850 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
851 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
852 ly_in_free(in, 0);
853
854 /* envelopes separately */
855 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\"}}";
856 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
857
858 /* notification with the parent node */
859 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
860 CHECK_LYD_STRING(lyd_parent(ntf), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
861
862 lyd_free_all(tree);
863 lyd_free_all(ntf);
Michal Vasko4a1e3e82023-09-05 08:45:01 +0200864
865 /* wrong order */
866 data = "{\"ietf-restconf:notification\":{\"a:n2\":{},\"eventTime\":\"2013-12-21T00:01:00Z\"}}";
867 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
868 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
869 ly_in_free(in, 0);
870
871 lyd_free_all(tree);
872 lyd_free_all(ntf);
Michal Vasko32d3d972023-09-05 13:37:03 +0200873
874 /* unknown notification */
875 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\",\"invalid:n2\":{}}}";
876 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
877 assert_int_equal(LY_EVALID, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
878 UTEST_LOG_CTX_CLEAN;
879 ly_in_free(in, 0);
880 lyd_free_all(tree);
Michal Vasko820efe82023-05-12 15:47:43 +0200881}
882
883static void
884test_restconf_reply(void **state)
885{
886 const char *data;
887 struct ly_in *in;
888 struct lyd_node *tree, *envp;
889
890 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:c/act", NULL, 0, &tree));
891
892 data = "{\"a:output\":{\"al\":25}}";
893 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
894 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, lyd_child(tree), in, LYD_JSON, LYD_TYPE_REPLY_RESTCONF, &envp, NULL));
895 ly_in_free(in, 0);
896
897 /* connected to the RPC with the parent */
898 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
899 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
900 lyd_free_all(tree);
901 lyd_free_all(envp);
902}
903
Michal Vaskodd03ff12023-09-11 10:30:48 +0200904static void
905test_metadata(void **state)
906{
907 const char *data;
908 struct lyd_node *tree;
909
910 /* invalid metadata value */
911 data = "{\"a:c\":{\"x\":\"xval\",\"@x\":{\"a:hint\":\"value\"}}}";
912 assert_int_equal(LY_EVALID, lyd_parse_data_mem(_UC->ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
913 assert_null(tree);
914 CHECK_LOG_CTX("Invalid non-number-encoded int8 value \"value\".", "Path \"/a:c/x/@a:hint\", line number 1.");
915}
916
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100917int
918main(void)
Radek Krejci1798aae2020-07-14 13:26:06 +0200919{
920 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200921 UTEST(test_leaf, setup),
922 UTEST(test_leaflist, setup),
923 UTEST(test_anydata, setup),
Michal Vasko76096ec2022-02-24 16:06:16 +0100924 UTEST(test_anyxml, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200925 UTEST(test_list, setup),
926 UTEST(test_container, setup),
927 UTEST(test_opaq, setup),
928 UTEST(test_rpc, setup),
929 UTEST(test_action, setup),
930 UTEST(test_notification, setup),
931 UTEST(test_reply, setup),
Michal Vasko820efe82023-05-12 15:47:43 +0200932 UTEST(test_restconf_rpc, setup),
933 UTEST(test_restconf_notification, setup),
934 UTEST(test_restconf_reply, setup),
Michal Vaskodd03ff12023-09-11 10:30:48 +0200935 UTEST(test_metadata, setup),
Radek Krejci1798aae2020-07-14 13:26:06 +0200936 };
937
938 return cmocka_run_group_tests(tests, NULL, NULL);
939}