blob: a44e6b600cc60e501c469e912b320ba07d45b93d [file] [log] [blame]
Radek Krejci1798aae2020-07-14 13:26:06 +02001/*
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_parser_json.c
Radek Krejci1798aae2020-07-14 13:26:06 +02003 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_xml.c
5 *
6 * Copyright (c) 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020016
Radek Krejci1798aae2020-07-14 13:26:06 +020017#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020018#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020019#include "out.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010020#include "parser_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020021#include "printer_data.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020022#include "tests_config.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020023#include "tree_data_internal.h"
24#include "tree_schema.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020025
26static int
27setup(void **state)
28{
Radek Iša56ca9e42020-09-08 18:42:00 +020029 const char *schema = "module a {namespace urn:tests:a;prefix a;yang-version 1.1; import ietf-yang-metadata {prefix md;}"
Radek Krejci1798aae2020-07-14 13:26:06 +020030 "md:annotation hint { type int8;}"
Michal Vaskodbf3e652022-10-21 08:46:25 +020031 "list l1 { key \"a b c\"; leaf a {type string;} leaf b {type string;} leaf c {type int16;}"
32 " leaf d {type string;}"
33 " container cont {leaf e {type boolean;}}"
34 "}"
Radek Krejci1798aae2020-07-14 13:26:06 +020035 "leaf foo { type string;}"
36 "container c {"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010037 " leaf x {type string;}"
38 " action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }"
39 " notification n1 { leaf nl {type string;} }"
Radek Krejci1798aae2020-07-14 13:26:06 +020040 "}"
41 "container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}"
42 "anydata any {config false;}"
Michal Vasko76096ec2022-02-24 16:06:16 +010043 "anyxml axml;"
Radek Krejci1798aae2020-07-14 13:26:06 +020044 "leaf-list ll1 { type uint8; }"
45 "leaf foo2 { type string; default \"default-val\"; }"
46 "leaf foo3 { type uint32; }"
47 "notification n2;}";
Radek Krejci1798aae2020-07-14 13:26:06 +020048
Radek Iša56ca9e42020-09-08 18:42:00 +020049 UTEST_SETUP;
Radek Krejci1798aae2020-07-14 13:26:06 +020050
Radek Iša56ca9e42020-09-08 18:42:00 +020051 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
52 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Radek Krejci1798aae2020-07-14 13:26:06 +020053
54 return 0;
55}
56
Radek Iša56ca9e42020-09-08 18:42:00 +020057#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
58 CHECK_PARSE_LYD_PARAM(INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci1798aae2020-07-14 13:26:06 +020059
Radek Iša56ca9e42020-09-08 18:42:00 +020060#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH) \
61 assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
62 CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH);\
63 assert_null(MODEL)
Radek Krejci1798aae2020-07-14 13:26:06 +020064
Radek Iša56ca9e42020-09-08 18:42:00 +020065#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
66 CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_JSON, PRINT_OPTION)
Radek Krejci1798aae2020-07-14 13:26:06 +020067
68static void
69test_leaf(void **state)
70{
Radek Krejci1798aae2020-07-14 13:26:06 +020071 struct lyd_node *tree;
72 struct lyd_node_term *leaf;
Radek Iša56ca9e42020-09-08 18:42:00 +020073 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +020074
Radek Iša56ca9e42020-09-08 18:42:00 +020075 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010076
Radek Iša56ca9e42020-09-08 18:42:00 +020077 data = "{\"a:foo\":\"foo value\"}";
78 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
79 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 +010080 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +020081 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
Radek Krejci1798aae2020-07-14 13:26:06 +020082
Radek Iša56ca9e42020-09-08 18:42:00 +020083 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
84 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010085 leaf = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020086
87 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +020088 assert_true(leaf->flags & LYD_DEFAULT);
89
Radek Iša56ca9e42020-09-08 18:42:00 +020090 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +020091 lyd_free_all(tree);
92
93 /* make foo2 explicit */
94 data = "{\"a:foo2\":\"default-val\"}";
Radek Iša56ca9e42020-09-08 18:42:00 +020095 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +020096 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +010097 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020098 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
99 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100100 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200101 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +0200102 assert_false(leaf->flags & LYD_DEFAULT);
103
Radek Iša56ca9e42020-09-08 18:42:00 +0200104 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200105 lyd_free_all(tree);
106
107 /* parse foo2 but make it implicit */
Radek Krejci5536d282020-08-04 23:27:44 +0200108 data = "{\"a:foo2\":\"default-val\",\"@a:foo2\":{\"ietf-netconf-with-defaults:default\":true}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200109 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200110 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100111 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200112 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
113 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100114 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200115 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +0200116 assert_true(leaf->flags & LYD_DEFAULT);
117
Michal Vaskob68c3b42022-05-20 10:36:40 +0200118 /* print default values */
119 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL_TAG, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200120 lyd_free_all(tree);
121
Michal Vaskoa4af6252022-05-20 10:38:54 +0200122 /* skip leaf */
123 data = "{\"a:cp\":{\"x\":\"val\",\"y\":\"valy\",\"z\":5}}";
124 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
125 assert_non_null(tree);
126
127 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:cp\":{\"y\":\"valy\",\"z\":5}}");
128 lyd_free_all(tree);
129
Radek Krejci1798aae2020-07-14 13:26:06 +0200130 /* multiple meatadata hint and unknown metadata xxx supposed to be skipped since it is from missing schema */
Radek Krejci5536d282020-08-04 23:27:44 +0200131 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 +0200132 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
133 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
134 CHECK_LYD_META(tree->meta, 1, "hint", 1, 1, INT8, "1", 1);
135 CHECK_LYD_META(tree->meta->next, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200136 assert_null(tree->meta->next->next);
137
Radek Iša56ca9e42020-09-08 18:42:00 +0200138 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
139 "{\"a:foo\":\"xxx\",\"@a:foo\":{\"a:hint\":1,\"a:hint\":2}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200140 lyd_free_all(tree);
141
Radek Iša56ca9e42020-09-08 18:42:00 +0200142 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200143 "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 +0200144
145 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200146 PARSER_CHECK_ERROR("{\"@a:foo\" : { \"a:hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200147 "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 +0200148
149 /* missing namespace for meatadata*/
Radek Iša56ca9e42020-09-08 18:42:00 +0200150 PARSER_CHECK_ERROR("{\"a:foo\" : \"value\", \"@a:foo\" : { \"hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200151 "Metadata in JSON must be namespace-qualified, missing prefix for \"hint\".",
152 "Schema location \"/a:foo\", line number 1.");
aPiecek49e2a3a2021-05-13 10:36:46 +0200153
Michal Vaskodbf3e652022-10-21 08:46:25 +0200154 /* invalid JSON type */
155 data = "{\"a:l1\" : [{ \"a\" : \"val-a\", \"b\" : \"val-b\", \"c\" : 1, \"cont\" : { \"e\" : \"0\" } }]}";
156 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
157 "Invalid non-boolean-encoded boolean value \"0\".",
158 "Data location \"/a:l1[a='val-a'][b='val-b'][c='1']/cont/e\", line number 1.");
159
aPiecek49e2a3a2021-05-13 10:36:46 +0200160 /* reverse solidus in JSON object member name */
161 data = "{\"@a:foo\":{\"a:hi\\nt\":1},\"a:foo\":\"xxx\"}";
162 assert_int_equal(LY_EINVAL, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
Radek Krejci1798aae2020-07-14 13:26:06 +0200163}
164
165static void
166test_leaflist(void **state)
167{
Radek Iša56ca9e42020-09-08 18:42:00 +0200168 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200169 struct lyd_node *tree;
170 struct lyd_node_term *ll;
171
Radek Iša56ca9e42020-09-08 18:42:00 +0200172 data = "{\"a:ll1\":[10,11]}";
173 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200174 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100175 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200176 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
177 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100178 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200179 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200180
181 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200182 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
183 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100184 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200185 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
Radek Krejci1798aae2020-07-14 13:26:06 +0200186
Radek Iša56ca9e42020-09-08 18:42:00 +0200187 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200188 lyd_free_all(tree);
189
Michal Vasko4e26adc2022-03-30 11:01:16 +0200190 /* accept empty */
191 data = "{\"a:ll1\":[]}";
192 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
193 assert_null(tree);
194
Radek Krejci1798aae2020-07-14 13:26:06 +0200195 /* simple metadata */
Radek Krejci5536d282020-08-04 23:27:44 +0200196 data = "{\"a:ll1\":[10,11],\"@a:ll1\":[null,{\"a:hint\":2}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200197 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200198 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100199 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200200 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
201 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100202 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200203 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200204 assert_null(ll->meta);
205
206 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200207 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
208 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100209 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200210 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
211 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200212 assert_null(ll->meta->next);
213
Radek Iša56ca9e42020-09-08 18:42:00 +0200214 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200215 lyd_free_all(tree);
216
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100217 /* 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 +0200218 data = "{\"@a:ll1\" : [{\"a:hint\" : 1, \"x:xxx\" : { \"value\" : \"/x:no/x:yes\" }, "
219 "\"a:hint\" : 10},null,{\"a:hint\" : 3}], \"a:ll1\" : [1,2,3]}";
220 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200221 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100222 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200223 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
224 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100225 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200226 CHECK_LYD_VALUE(ll->value, UINT8, "1", 1);
227 CHECK_LYD_META(ll->meta, 1, "hint", 1, 1, INT8, "1", 1);
228 CHECK_LYD_META(ll->meta->next, 1, "hint", 0, 1, INT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200229
230 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200231 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
232 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100233 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200234 CHECK_LYD_VALUE(ll->value, UINT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200235 assert_null(ll->meta);
236
237 assert_non_null(tree->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200238 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
239 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100240 ll = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200241 CHECK_LYD_VALUE(ll->value, UINT8, "3", 3);
242 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "3", 3);
Radek Krejci1798aae2020-07-14 13:26:06 +0200243 assert_null(ll->meta->next);
244
Radek Iša56ca9e42020-09-08 18:42:00 +0200245 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
246 "{\"a:ll1\":[1,2,3],\"@a:ll1\":[{\"a:hint\":1,\"a:hint\":10},null,{\"a:hint\":3}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200247 lyd_free_all(tree);
248
249 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200250 PARSER_CHECK_ERROR("{\"@a:ll1\":[{\"a:hint\":1}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200251 "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 +0200252
Radek Iša56ca9e42020-09-08 18:42:00 +0200253 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 +0200254 "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 +0200255
Radek Iša56ca9e42020-09-08 18:42:00 +0200256 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 +0200257 tree, LY_EVALID, "Missing JSON data instance #3 to be coupled with @a:ll1 metadata.",
258 "Data location \"/@a:ll1\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200259}
260
261static void
262test_anydata(void **state)
263{
Radek Krejci1798aae2020-07-14 13:26:06 +0200264 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200265 struct lyd_node *tree;
266
Radek Krejci1798aae2020-07-14 13:26:06 +0200267 data = "{\"a:any\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200268 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200269 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100270 tree = tree->next;
Michal Vasko76096ec2022-02-24 16:06:16 +0100271 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any",
Radek Iša56ca9e42020-09-08 18:42:00 +0200272 1, LYS_ANYDATA, 0, 0, NULL, 0);
273 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200274 lyd_free_all(tree);
Michal Vaskofbc4a252022-02-03 13:15:58 +0100275
276 data = "{\"a:any\":{}}";
277 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
278 assert_non_null(tree);
279 tree = tree->next;
Michal Vasko76096ec2022-02-24 16:06:16 +0100280 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 +0100281 1, LYS_ANYDATA, 0, 0, NULL, 0);
282 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
283 lyd_free_all(tree);
Michal Vasko76096ec2022-02-24 16:06:16 +0100284
285 data = "{\"a:any\":[null]}";
286 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
287 assert_non_null(tree);
288 tree = tree->next;
289 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any",
290 1, LYS_ANYDATA, 0, 0, NULL, 0);
291 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
292 lyd_free_all(tree);
293}
294
295static void
296test_anyxml(void **state)
297{
298 const char *data;
299 struct lyd_node *tree;
300
301 data = "{\"a:axml\":\"some-value in string\"}";
302 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
303 assert_non_null(tree);
304 tree = tree->next;
305 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
306 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
307 lyd_free_all(tree);
308
309 data = "{\"a:axml\":\"\"}";
310 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
311 assert_non_null(tree);
312 tree = tree->next;
313 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
314 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
315 lyd_free_all(tree);
316
317 data = "{\"a:axml\":55}";
318 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
319 assert_non_null(tree);
320 tree = tree->next;
321 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
322 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
323 lyd_free_all(tree);
324
325 data = "{\"a:axml\":false}";
326 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
327 assert_non_null(tree);
328 tree = tree->next;
329 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
330 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
331 lyd_free_all(tree);
332
333 data = "{\"a:axml\":null}";
334 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
335 assert_non_null(tree);
336 tree = tree->next;
337 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
338 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
339 lyd_free_all(tree);
340
341 data = "{\"a:axml\":[null,true,false]}";
342 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
343 assert_non_null(tree);
344 tree = tree->next;
345 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
346 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
347 lyd_free_all(tree);
348
349 data = "{\"a:axml\":[null,true,{\"name\":[25,40, false]}]}";
350 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
351 assert_non_null(tree);
352 tree = tree->next;
353 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
354 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
355 lyd_free_all(tree);
356
357 /* same as anydata tests */
358 data = "{\"a:axml\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
359 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
360 assert_non_null(tree);
361 tree = tree->next;
362 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
363 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
364 lyd_free_all(tree);
365
366 data = "{\"a:axml\":{}}";
367 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
368 assert_non_null(tree);
369 tree = tree->next;
370 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
371 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
372 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200373}
374
375static void
376test_list(void **state)
377{
Radek Iša56ca9e42020-09-08 18:42:00 +0200378 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200379 struct lyd_node *tree, *iter;
380 struct lyd_node_inner *list;
381 struct lyd_node_term *leaf;
382
383 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200384 data = "{\"a:l1\":[{\"a\":\"one\",\"b\":\"one\",\"c\":1}]}";
385 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
386 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
387 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100388 list = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200389 LY_LIST_FOR(list->child, iter) {
390 assert_int_not_equal(0, iter->hash);
391 }
Radek Iša56ca9e42020-09-08 18:42:00 +0200392 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200393 lyd_free_all(tree);
394
Michal Vasko4e26adc2022-03-30 11:01:16 +0200395 /* accept empty */
396 data = "{\"a:l1\":[]}";
397 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
398 assert_null(tree);
399
Radek Krejci1798aae2020-07-14 13:26:06 +0200400 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200401 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200402 "List instance is missing its key \"a\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200403 "Data location \"/a:l1[b='b'][c='1']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200404
Radek Iša56ca9e42020-09-08 18:42:00 +0200405 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200406 "List instance is missing its key \"b\".", "Data location \"/a:l1[a='a']\", 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\": [ {\"b\" : \"b\", \"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vaskodbf3e652022-10-21 08:46:25 +0200409 "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 +0200410
411 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200412 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\", \"a\" : \"a\", \"c\" : 1}]}", 0, LYD_VALIDATE_PRESENT,
Michal Vasko959f8d82022-06-16 07:51:50 +0200413 tree, LY_EVALID, "Duplicate instance of \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200414 "Data location \"/a:l1[a='a'][b='b'][c='1'][c='1']/c\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200415
416 /* keys order, in contrast to XML, JSON accepts keys in any order even in strict mode */
Radek Iša56ca9e42020-09-08 18:42:00 +0200417 CHECK_PARSE_LYD("{ \"a:l1\": [ {\"d\" : \"d\", \"a\" : \"a\", \"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree);
418 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
419 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100420 list = (struct lyd_node_inner *)tree;
421 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200422 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 +0100423 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200424 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 +0100425 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200426 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100427 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200428 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 +0200429 CHECK_LOG_CTX(NULL, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200430
Radek Iša56ca9e42020-09-08 18:42:00 +0200431 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
432 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1,\"d\":\"d\"}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200433 lyd_free_all(tree);
434
435 /* */
Radek Iša56ca9e42020-09-08 18:42:00 +0200436 CHECK_PARSE_LYD("{\"a:l1\":[{\"c\":1,\"b\":\"b\",\"a\":\"a\"}]}", LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree);
437 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
438 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100439 list = (struct lyd_node_inner *)tree;
440 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200441 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a",
442 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100443 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
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, "b",
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, "c",
448 1, LYS_LEAF, 1, 0, NULL, 0);
449 CHECK_LOG_CTX(NULL, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200450
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
Radek Krejci5536d282020-08-04 23:27:44 +0200455 data = "{\"a:cp\":{\"@\":{\"a:hint\":1}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200456 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200457 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100458 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100459 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 +0200460 1, LYS_CONTAINER, 0, 0, NULL, 0);
461 CHECK_LYD_META(tree->meta, 1, "hint", 0, 1, INT8, "1", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200462 assert_null(tree->meta->next);
Radek Krejci5536d282020-08-04 23:27:44 +0200463
Radek Iša56ca9e42020-09-08 18:42:00 +0200464 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200465 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200466}
467
468static void
469test_container(void **state)
470{
Radek Iša56ca9e42020-09-08 18:42:00 +0200471 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200472 struct lyd_node *tree;
473 struct lyd_node_inner *cont;
474
Radek Iša56ca9e42020-09-08 18:42:00 +0200475 CHECK_PARSE_LYD("{\"a:c\":{}}", 0, LYD_VALIDATE_PRESENT, tree);
476 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c",
477 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100478 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200479 assert_true(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200480
Radek Iša56ca9e42020-09-08 18:42:00 +0200481 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200482 lyd_free_all(tree);
483
Radek Krejci5536d282020-08-04 23:27:44 +0200484 data = "{\"a:cp\":{}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200485 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200486 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100487 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100488 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 +0200489 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100490 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200491 assert_false(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200492
Radek Iša56ca9e42020-09-08 18:42:00 +0200493 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200494 lyd_free_all(tree);
Michal Vaskoa4af6252022-05-20 10:38:54 +0200495
496 /* skip container */
497 CHECK_PARSE_LYD("{\"a:unknown\":{\"a\":\"val\",\"b\":5}}", 0, LYD_VALIDATE_PRESENT, tree);
498 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
499 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200500}
501
502static void
503test_opaq(void **state)
504{
Radek Krejci1798aae2020-07-14 13:26:06 +0200505 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200506 struct lyd_node *tree;
507
Radek Krejci1798aae2020-07-14 13:26:06 +0200508 /* invalid value, no flags */
509 data = "{\"a:foo3\":[null]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200510 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200511 "Invalid non-number-encoded uint32 value \"\".", "Schema location \"/a:foo3\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200512
513 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200514 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200515 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 +0200516 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200517 lyd_free_all(tree);
518
519 /* missing key, no flags */
520 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"d\":\"val_d\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200521 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200522 "List instance is missing its key \"c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200523 "Data location \"/a:l1[a='val_a'][b='val_b']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200524
525 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200526 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200527 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 +0200528 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200529 lyd_free_all(tree);
530
531 /* invalid key, no flags */
532 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200533 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200534 "Invalid non-number-encoded int16 value \"val_c\".",
Michal Vaskodbf3e652022-10-21 08:46:25 +0200535 "Data location \"/a:l1[a='val_a'][b='val_b']\", line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200536
537 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200538 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200539 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 +0200540 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200541 lyd_free_all(tree);
542
543 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":{\"val\":\"val_c\"}}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200544 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200545 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 +0200546 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200547 lyd_free_all(tree);
548
549 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\"}]}";
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);
aPiecekb7b29e62021-05-11 10:02:43 +0200554
Michal Vasko6a6e3082022-05-10 10:32:38 +0200555 /* invalid metadata */
aPiecekc5f36a72021-05-18 14:12:31 +0200556 data = "{\"@a:foo\":\"str\",\"@a:foo3\":1,\"a:foo3\":2}";
Michal Vasko6a6e3082022-05-10 10:32:38 +0200557 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko959f8d82022-06-16 07:51:50 +0200558 "Unknown module of node \"@a:foo\".", "Data location \"/@a:foo\", line number 1.");
aPiecekc5f36a72021-05-18 14:12:31 +0200559
aPiecekb7b29e62021-05-11 10:02:43 +0200560 /* empty name */
561 PARSER_CHECK_ERROR("{\"@a:foo\":{\"\":0}}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
562 "A JSON object member name cannot be a zero-length string.", "Line number 1.");
Michal Vasko1a85d332021-08-27 10:35:28 +0200563
564 /* opaque data tree format print */
565 data =
566 "{\n"
567 " \"ietf-netconf-nmda:get-data\": {\n"
568 " \"data\": {\n"
569 " \"ietf-keystore:keystore\": {\n"
570 " \"asymmetric-keys\": {\n"
571 " \"asymmetric-key\": [\n"
572 " {\n"
573 " \"name\": \"genkey\",\n"
574 " \"algorithm\": \"rsa2048\"\n"
575 " }\n"
576 " ]\n"
577 " }\n"
578 " },\n"
579 " \"ietf-netconf-server:netconf-server\": {\n"
580 " \"listen\": {\n"
581 " \"idle-timeout\": 3600,\n"
582 " \"endpoint\": [\n"
583 " {\n"
584 " \"name\": \"default-ssh\",\n"
585 " \"ssh\": {\n"
586 " \"tcp-server-parameters\": {\n"
587 " \"local-address\": \"0.0.0.0\",\n"
588 " \"local-port\": 830\n"
589 " },\n"
590 " \"ssh-server-parameters\": {\n"
591 " \"server-identity\": {\n"
592 " \"host-key\": [\n"
593 " {\n"
594 " \"name\": \"default-key\",\n"
595 " \"public-key\": {\n"
596 " \"keystore-reference\": \"genkey\"\n"
597 " }\n"
598 " }\n"
599 " ]\n"
600 " },\n"
601 " \"client-authentication\": {\n"
602 " \"supported-authentication-methods\": {\n"
603 " \"publickey\": [null],\n"
604 " \"passsword\": [null],\n"
605 " \"other\": [\n"
606 " \"interactive\",\n"
607 " \"gssapi\"\n"
608 " ]\n"
609 " }\n"
610 " }\n"
611 " }\n"
612 " }\n"
613 " }\n"
614 " ]\n"
615 " }\n"
616 " }\n"
617 " }\n"
618 " }\n"
619 "}\n";
620 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
621 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
622 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200623}
624
625static void
626test_rpc(void **state)
627{
Radek Krejci1798aae2020-07-14 13:26:06 +0200628 const char *data;
629 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200630 struct lyd_node *tree, *op;
631 const struct lyd_node *node;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100632 const char *dsc = "Edit data in an NMDA datastore.\n"
633 "\n"
634 "If an error condition occurs such that an error severity\n"
635 "<rpc-error> element is generated, the server will stop\n"
636 "processing the <edit-data> operation and restore the\n"
637 "specified configuration to its complete state at\n"
638 "the start of this <edit-data> operation.";
Radek Krejci1798aae2020-07-14 13:26:06 +0200639
Michal Vaskobbdadda2022-01-06 11:40:10 +0100640 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
Radek Krejci1798aae2020-07-14 13:26:06 +0200641
Michal Vaskobbdadda2022-01-06 11:40:10 +0100642 data = "{\"ietf-netconf-nmda:edit-data\":{"
643 "\"datastore\":\"ietf-datastores:running\","
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100644 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
645 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
Michal Vasko2552ea32020-12-08 15:32:34 +0100646 "}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200647 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100648 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 +0200649 ly_in_free(in, 0);
650
651 assert_non_null(op);
Radek Krejci1798aae2020-07-14 13:26:06 +0200652
Radek Krejci2a9fc652021-01-22 17:44:34 +0100653 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100654 1, 0, 0, 1, "edit-data", LYS_RPC,
655 0, 0, 0, 0, 0, NULL, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200656
Michal Vasko2552ea32020-12-08 15:32:34 +0100657 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100658 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100659 1, 0, 0, 1, "edit-data", LYS_RPC,
660 0, 0, 0, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200661 node = lyd_child(node)->next;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100662 CHECK_LYSC_NODE(node->schema, "Inline config content.", 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config",
663 0, LYS_ANYDATA, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200664
665 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100666 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 +0200667 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200668 node = lyd_child(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200669 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200670 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 +0200671 node = node->parent->next;
672 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200673 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 +0200674
Radek Iša56ca9e42020-09-08 18:42:00 +0200675 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200676 lyd_free_all(tree);
677
678 /* wrong namespace, element name, whatever... */
679 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200680}
681
682static void
683test_action(void **state)
684{
Radek Krejci1798aae2020-07-14 13:26:06 +0200685 const char *data;
686 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200687 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200688
Michal Vasko2552ea32020-12-08 15:32:34 +0100689 data = "{\"a:c\":{\"act\":{\"al\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200690 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100691 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 +0200692 ly_in_free(in, 0);
693
694 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100695 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200696 1, 0, 0, 1, "act", LYS_ACTION,
697 1, 0, 0, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200698
Radek Iša56ca9e42020-09-08 18:42:00 +0200699 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200700 lyd_free_all(tree);
701
702 /* wrong namespace, element name, whatever... */
703 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200704}
705
706static void
707test_notification(void **state)
708{
Radek Krejci1798aae2020-07-14 13:26:06 +0200709 const char *data;
710 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200711 struct lyd_node *tree, *ntf;
Radek Krejci1798aae2020-07-14 13:26:06 +0200712
Michal Vasko2552ea32020-12-08 15:32:34 +0100713 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200714 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100715 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 +0200716 ly_in_free(in, 0);
717
718 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100719 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 +0200720
Michal Vasko2552ea32020-12-08 15:32:34 +0100721 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 +0200722
Radek Iša56ca9e42020-09-08 18:42:00 +0200723 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200724 lyd_free_all(tree);
725
Radek Krejci1798aae2020-07-14 13:26:06 +0200726 data = "{\"a:n2\":{}}";
727 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100728 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 +0200729 ly_in_free(in, 0);
730
731 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100732 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 +0200733
734 assert_non_null(tree);
735 assert_ptr_equal(ntf, tree);
736
Radek Iša56ca9e42020-09-08 18:42:00 +0200737 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200738 lyd_free_all(tree);
739
740 /* wrong namespace, element name, whatever... */
741 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200742}
743
744static void
745test_reply(void **state)
746{
Radek Krejci1798aae2020-07-14 13:26:06 +0200747 const char *data;
748 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100749 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200750 const struct lyd_node *node;
751
Michal Vasko2552ea32020-12-08 15:32:34 +0100752 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200753 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100754 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 +0200755 ly_in_free(in, 0);
756
Radek Krejci1798aae2020-07-14 13:26:06 +0200757 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100758 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200759 1, 0, 0, 1, "act", LYS_ACTION,
760 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200761 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100762 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 +0200763
Michal Vasko2552ea32020-12-08 15:32:34 +0100764 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 +0200765
766 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200767 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:al\":25}");
Michal Vasko2552ea32020-12-08 15:32:34 +0100768 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:c\":{\"act\":{\"al\":25}}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200769 lyd_free_all(tree);
770
771 /* wrong namespace, element name, whatever... */
772 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200773}
774
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100775int
776main(void)
Radek Krejci1798aae2020-07-14 13:26:06 +0200777{
778 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200779 UTEST(test_leaf, setup),
780 UTEST(test_leaflist, setup),
781 UTEST(test_anydata, setup),
Michal Vasko76096ec2022-02-24 16:06:16 +0100782 UTEST(test_anyxml, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200783 UTEST(test_list, setup),
784 UTEST(test_container, setup),
785 UTEST(test_opaq, setup),
786 UTEST(test_rpc, setup),
787 UTEST(test_action, setup),
788 UTEST(test_notification, setup),
789 UTEST(test_reply, setup),
Radek Krejci1798aae2020-07-14 13:26:06 +0200790 };
791
792 return cmocka_run_group_tests(tests, NULL, NULL);
793}