blob: e9fb70579946880695bb5378e247f7f391512572 [file] [log] [blame]
Radek Krejci1798aae2020-07-14 13:26:06 +02001/*
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_parser_json.c
Radek Krejci1798aae2020-07-14 13:26:06 +02003 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_xml.c
5 *
6 * Copyright (c) 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020016
Radek Krejci1798aae2020-07-14 13:26:06 +020017#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020018#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020019#include "out.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010020#include "parser_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020021#include "printer_data.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020022#include "tests_config.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020023#include "tree_data_internal.h"
24#include "tree_schema.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020025
26static int
27setup(void **state)
28{
Radek Iša56ca9e42020-09-08 18:42:00 +020029 const char *schema = "module a {namespace urn:tests:a;prefix a;yang-version 1.1; import ietf-yang-metadata {prefix md;}"
Radek Krejci1798aae2020-07-14 13:26:06 +020030 "md:annotation hint { type int8;}"
31 "list l1 { key \"a b c\"; leaf a {type string;} leaf b {type string;} leaf c {type int16;} leaf d {type string;}}"
32 "leaf foo { type string;}"
33 "container c {"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010034 " leaf x {type string;}"
35 " action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }"
36 " notification n1 { leaf nl {type string;} }"
Radek Krejci1798aae2020-07-14 13:26:06 +020037 "}"
38 "container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}"
39 "anydata any {config false;}"
40 "leaf-list ll1 { type uint8; }"
41 "leaf foo2 { type string; default \"default-val\"; }"
42 "leaf foo3 { type uint32; }"
43 "notification n2;}";
Radek Krejci1798aae2020-07-14 13:26:06 +020044
Radek Iša56ca9e42020-09-08 18:42:00 +020045 UTEST_SETUP;
Radek Krejci1798aae2020-07-14 13:26:06 +020046
Radek Iša56ca9e42020-09-08 18:42:00 +020047 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
48 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Radek Krejci1798aae2020-07-14 13:26:06 +020049
50 return 0;
51}
52
Radek Iša56ca9e42020-09-08 18:42:00 +020053#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
54 CHECK_PARSE_LYD_PARAM(INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci1798aae2020-07-14 13:26:06 +020055
Radek Iša56ca9e42020-09-08 18:42:00 +020056#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH) \
57 assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
58 CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH);\
59 assert_null(MODEL)
Radek Krejci1798aae2020-07-14 13:26:06 +020060
Radek Iša56ca9e42020-09-08 18:42:00 +020061#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
62 CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_JSON, PRINT_OPTION)
Radek Krejci1798aae2020-07-14 13:26:06 +020063
64static void
65test_leaf(void **state)
66{
Radek Krejci1798aae2020-07-14 13:26:06 +020067 struct lyd_node *tree;
68 struct lyd_node_term *leaf;
Radek Iša56ca9e42020-09-08 18:42:00 +020069 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +020070
Radek Iša56ca9e42020-09-08 18:42:00 +020071 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010072
Radek Iša56ca9e42020-09-08 18:42:00 +020073 data = "{\"a:foo\":\"foo value\"}";
74 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
75 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 +010076 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +020077 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
Radek Krejci1798aae2020-07-14 13:26:06 +020078
Radek Iša56ca9e42020-09-08 18:42:00 +020079 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
80 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010081 leaf = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020082
83 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +020084 assert_true(leaf->flags & LYD_DEFAULT);
85
Radek Iša56ca9e42020-09-08 18:42:00 +020086 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +020087 lyd_free_all(tree);
88
89 /* make foo2 explicit */
90 data = "{\"a:foo2\":\"default-val\"}";
Radek Iša56ca9e42020-09-08 18:42:00 +020091 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +020092 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +010093 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020094 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
95 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010096 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +020097 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +020098 assert_false(leaf->flags & LYD_DEFAULT);
99
Radek Iša56ca9e42020-09-08 18:42:00 +0200100 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200101 lyd_free_all(tree);
102
103 /* parse foo2 but make it implicit */
Radek Krejci5536d282020-08-04 23:27:44 +0200104 data = "{\"a:foo2\":\"default-val\",\"@a:foo2\":{\"ietf-netconf-with-defaults:default\":true}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200105 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200106 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100107 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200108 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
109 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100110 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200111 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +0200112 assert_true(leaf->flags & LYD_DEFAULT);
113
Radek Krejci5536d282020-08-04 23:27:44 +0200114 /* TODO default values
Radek Krejci52f65552020-09-01 17:03:35 +0200115 lyd_print_tree(out, tree, LYD_JSON, LYD_PRINT_SHRINK);
Radek Krejci5536d282020-08-04 23:27:44 +0200116 assert_string_equal(printed, data);
117 ly_out_reset(out);
118 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200119 lyd_free_all(tree);
120
121 /* multiple meatadata hint and unknown metadata xxx supposed to be skipped since it is from missing schema */
Radek Krejci5536d282020-08-04 23:27:44 +0200122 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 +0200123 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
124 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
125 CHECK_LYD_META(tree->meta, 1, "hint", 1, 1, INT8, "1", 1);
126 CHECK_LYD_META(tree->meta->next, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200127 assert_null(tree->meta->next->next);
128
Radek Iša56ca9e42020-09-08 18:42:00 +0200129 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
130 "{\"a:foo\":\"xxx\",\"@a:foo\":{\"a:hint\":1,\"a:hint\":2}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200131 lyd_free_all(tree);
132
Radek Iša56ca9e42020-09-08 18:42:00 +0200133 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko4eab5622021-07-22 15:36:45 +0200134 "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 +0200135
136 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200137 PARSER_CHECK_ERROR("{\"@a:foo\" : { \"a:hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100138 "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 +0200139
140 /* missing namespace for meatadata*/
Radek Iša56ca9e42020-09-08 18:42:00 +0200141 PARSER_CHECK_ERROR("{\"a:foo\" : \"value\", \"@a:foo\" : { \"hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100142 "Metadata in JSON must be namespace-qualified, missing prefix for \"hint\".", "Schema location /a:foo, line number 1.");
aPiecek49e2a3a2021-05-13 10:36:46 +0200143
144 /* reverse solidus in JSON object member name */
145 data = "{\"@a:foo\":{\"a:hi\\nt\":1},\"a:foo\":\"xxx\"}";
146 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 +0200147}
148
149static void
150test_leaflist(void **state)
151{
Radek Iša56ca9e42020-09-08 18:42:00 +0200152 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200153 struct lyd_node *tree;
154 struct lyd_node_term *ll;
155
Radek Iša56ca9e42020-09-08 18:42:00 +0200156 data = "{\"a:ll1\":[10,11]}";
157 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200158 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100159 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200160 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
161 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100162 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200163 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200164
165 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200166 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
167 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100168 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200169 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
Radek Krejci1798aae2020-07-14 13:26:06 +0200170
Radek Iša56ca9e42020-09-08 18:42:00 +0200171 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200172 lyd_free_all(tree);
173
174 /* simple metadata */
Radek Krejci5536d282020-08-04 23:27:44 +0200175 data = "{\"a:ll1\":[10,11],\"@a:ll1\":[null,{\"a:hint\":2}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200176 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200177 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100178 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200179 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
180 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100181 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200182 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200183 assert_null(ll->meta);
184
185 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200186 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
187 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100188 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200189 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
190 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200191 assert_null(ll->meta->next);
192
Radek Iša56ca9e42020-09-08 18:42:00 +0200193 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200194 lyd_free_all(tree);
195
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100196 /* 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 +0200197 data = "{\"@a:ll1\" : [{\"a:hint\" : 1, \"x:xxx\" : { \"value\" : \"/x:no/x:yes\" }, "
198 "\"a:hint\" : 10},null,{\"a:hint\" : 3}], \"a:ll1\" : [1,2,3]}";
199 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200200 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100201 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200202 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
203 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100204 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200205 CHECK_LYD_VALUE(ll->value, UINT8, "1", 1);
206 CHECK_LYD_META(ll->meta, 1, "hint", 1, 1, INT8, "1", 1);
207 CHECK_LYD_META(ll->meta->next, 1, "hint", 0, 1, INT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200208
209 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200210 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
211 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100212 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200213 CHECK_LYD_VALUE(ll->value, UINT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200214 assert_null(ll->meta);
215
216 assert_non_null(tree->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200217 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
218 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100219 ll = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200220 CHECK_LYD_VALUE(ll->value, UINT8, "3", 3);
221 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "3", 3);
Radek Krejci1798aae2020-07-14 13:26:06 +0200222 assert_null(ll->meta->next);
223
Radek Iša56ca9e42020-09-08 18:42:00 +0200224 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
225 "{\"a:ll1\":[1,2,3],\"@a:ll1\":[{\"a:hint\":1,\"a:hint\":10},null,{\"a:hint\":3}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200226 lyd_free_all(tree);
227
228 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200229 PARSER_CHECK_ERROR("{\"@a:ll1\":[{\"a:hint\":1}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100230 "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 +0200231
Radek Iša56ca9e42020-09-08 18:42:00 +0200232 PARSER_CHECK_ERROR("{\"a:ll1\":[1],\"@a:ll1\":[{\"a:hint\":1},{\"a:hint\":2}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko54ba8912021-07-23 12:46:23 +0200233 "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 +0200234
Radek Iša56ca9e42020-09-08 18:42:00 +0200235 PARSER_CHECK_ERROR("{\"@a:ll1\":[{\"a:hint\":1},{\"a:hint\":2},{\"a:hint\":3}],\"a:ll1\" : [1, 2]}", 0, LYD_VALIDATE_PRESENT,
Michal Vasko54ba8912021-07-23 12:46:23 +0200236 tree, LY_EVALID, "Missing JSON data instance #3 to be coupled with @a:ll1 metadata.", "Data location /@a:ll1, line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200237}
238
239static void
240test_anydata(void **state)
241{
Radek Krejci1798aae2020-07-14 13:26:06 +0200242 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200243 struct lyd_node *tree;
244
Radek Krejci1798aae2020-07-14 13:26:06 +0200245 data = "{\"a:any\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200246 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200247 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100248 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200249 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_SET_ENUM | LYS_CONFIG_R | LYS_YIN_ARGUMENT, 1, "any",
250 1, LYS_ANYDATA, 0, 0, NULL, 0);
251 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200252 lyd_free_all(tree);
Michal Vaskoa9d4d642022-02-03 13:15:58 +0100253
254 data = "{\"a:any\":{}}";
255 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
256 assert_non_null(tree);
257 tree = tree->next;
258 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_SET_ENUM | LYS_CONFIG_R | LYS_YIN_ARGUMENT, 1, "any",
259 1, LYS_ANYDATA, 0, 0, NULL, 0);
260 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
261 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200262}
263
264static void
265test_list(void **state)
266{
Radek Iša56ca9e42020-09-08 18:42:00 +0200267 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200268 struct lyd_node *tree, *iter;
269 struct lyd_node_inner *list;
270 struct lyd_node_term *leaf;
271
272 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200273 data = "{\"a:l1\":[{\"a\":\"one\",\"b\":\"one\",\"c\":1}]}";
274 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
275 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
276 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100277 list = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200278 LY_LIST_FOR(list->child, iter) {
279 assert_int_not_equal(0, iter->hash);
280 }
Radek Krejci5536d282020-08-04 23:27:44 +0200281
Radek Iša56ca9e42020-09-08 18:42:00 +0200282 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200283 lyd_free_all(tree);
284
285 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200286 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100287 "List instance is missing its key \"a\".", "Schema location /a:l1, data location /a:l1[b='b'][c='1'], line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200288
Radek Iša56ca9e42020-09-08 18:42:00 +0200289 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100290 "List instance is missing its key \"b\".", "Schema location /a:l1, data location /a:l1[a='a'], line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200291
Radek Iša56ca9e42020-09-08 18:42:00 +0200292 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"b\" : \"b\", \"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100293 "List instance is missing its key \"c\".", "Schema location /a:l1, data location /a:l1[a='a'][b='b'], line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200294
295 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200296 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\", \"a\" : \"a\", \"c\" : 1}]}", 0, LYD_VALIDATE_PRESENT,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100297 tree, LY_EVALID, "Duplicate instance of \"c\".", "Schema location /a:l1/c, data location /a:l1[a='a'][b='b'][c='1'][c='1']/c, line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200298
299 /* keys order, in contrast to XML, JSON accepts keys in any order even in strict mode */
Radek Iša56ca9e42020-09-08 18:42:00 +0200300 CHECK_PARSE_LYD("{ \"a:l1\": [ {\"d\" : \"d\", \"a\" : \"a\", \"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree);
301 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
302 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100303 list = (struct lyd_node_inner *)tree;
304 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200305 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 +0100306 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200307 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 +0100308 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200309 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 +0100310 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200311 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "d", 0, LYS_LEAF, 1, 0, NULL, 0);
312 CHECK_LOG_CTX(NULL, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200313
Radek Iša56ca9e42020-09-08 18:42:00 +0200314 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
315 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1,\"d\":\"d\"}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200316 lyd_free_all(tree);
317
318 /* */
Radek Iša56ca9e42020-09-08 18:42:00 +0200319 CHECK_PARSE_LYD("{\"a:l1\":[{\"c\":1,\"b\":\"b\",\"a\":\"a\"}]}", LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree);
320 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
321 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100322 list = (struct lyd_node_inner *)tree;
323 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200324 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a",
325 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100326 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200327 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b",
328 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100329 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200330 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c",
331 1, LYS_LEAF, 1, 0, NULL, 0);
332 CHECK_LOG_CTX(NULL, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200333
Radek Iša56ca9e42020-09-08 18:42:00 +0200334 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
335 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200336 lyd_free_all(tree);
337
Radek Krejci5536d282020-08-04 23:27:44 +0200338 data = "{\"a:cp\":{\"@\":{\"a:hint\":1}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200339 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200340 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100341 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100342 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 +0200343 1, LYS_CONTAINER, 0, 0, NULL, 0);
344 CHECK_LYD_META(tree->meta, 1, "hint", 0, 1, INT8, "1", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200345 assert_null(tree->meta->next);
Radek Krejci5536d282020-08-04 23:27:44 +0200346
Radek Iša56ca9e42020-09-08 18:42:00 +0200347 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200348 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200349}
350
351static void
352test_container(void **state)
353{
Radek Iša56ca9e42020-09-08 18:42:00 +0200354 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200355 struct lyd_node *tree;
356 struct lyd_node_inner *cont;
357
Radek Iša56ca9e42020-09-08 18:42:00 +0200358 CHECK_PARSE_LYD("{\"a:c\":{}}", 0, LYD_VALIDATE_PRESENT, tree);
359 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c",
360 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100361 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200362 assert_true(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200363
Radek Iša56ca9e42020-09-08 18:42:00 +0200364 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200365 lyd_free_all(tree);
366
Radek Krejci5536d282020-08-04 23:27:44 +0200367 data = "{\"a:cp\":{}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200368 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200369 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100370 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100371 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 +0200372 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100373 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200374 assert_false(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200375
Radek Iša56ca9e42020-09-08 18:42:00 +0200376 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200377 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200378}
379
380static void
381test_opaq(void **state)
382{
Radek Krejci1798aae2020-07-14 13:26:06 +0200383 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200384 struct lyd_node *tree;
385
Radek Krejci1798aae2020-07-14 13:26:06 +0200386 /* invalid value, no flags */
387 data = "{\"a:foo3\":[null]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200388 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100389 "Invalid non-number-encoded uint32 value \"\".", "Schema location /a:foo3, line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200390
391 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200392 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200393 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 +0200394 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200395 lyd_free_all(tree);
396
397 /* missing key, no flags */
398 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"d\":\"val_d\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200399 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100400 "List instance is missing its key \"c\".", "Schema location /a:l1, data location /a:l1[a='val_a'][b='val_b'], line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200401
402 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200403 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200404 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 +0200405 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200406 lyd_free_all(tree);
407
408 /* invalid key, no flags */
409 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200410 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100411 "Invalid non-number-encoded int16 value \"val_c\".", "Schema location /a:l1/c, data location /a:l1[a='val_a'][b='val_b'], line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200412
413 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200414 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200415 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 +0200416 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200417 lyd_free_all(tree);
418
419 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":{\"val\":\"val_c\"}}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200420 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200421 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 +0200422 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200423 lyd_free_all(tree);
424
425 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\"}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200426 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200427 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 +0200428 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200429 lyd_free_all(tree);
aPiecekb7b29e62021-05-11 10:02:43 +0200430
aPiecekc5f36a72021-05-18 14:12:31 +0200431 /* mixing with metadata that is ignored */
432 data = "{\"@a:foo\":\"str\",\"@a:foo3\":1,\"a:foo3\":2}";
433 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
434 lyd_free_all(tree);
435
aPiecekb7b29e62021-05-11 10:02:43 +0200436 /* empty name */
437 PARSER_CHECK_ERROR("{\"@a:foo\":{\"\":0}}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
438 "A JSON object member name cannot be a zero-length string.", "Line number 1.");
Michal Vasko1a85d332021-08-27 10:35:28 +0200439
440 /* opaque data tree format print */
441 data =
442 "{\n"
443 " \"ietf-netconf-nmda:get-data\": {\n"
444 " \"data\": {\n"
445 " \"ietf-keystore:keystore\": {\n"
446 " \"asymmetric-keys\": {\n"
447 " \"asymmetric-key\": [\n"
448 " {\n"
449 " \"name\": \"genkey\",\n"
450 " \"algorithm\": \"rsa2048\"\n"
451 " }\n"
452 " ]\n"
453 " }\n"
454 " },\n"
455 " \"ietf-netconf-server:netconf-server\": {\n"
456 " \"listen\": {\n"
457 " \"idle-timeout\": 3600,\n"
458 " \"endpoint\": [\n"
459 " {\n"
460 " \"name\": \"default-ssh\",\n"
461 " \"ssh\": {\n"
462 " \"tcp-server-parameters\": {\n"
463 " \"local-address\": \"0.0.0.0\",\n"
464 " \"local-port\": 830\n"
465 " },\n"
466 " \"ssh-server-parameters\": {\n"
467 " \"server-identity\": {\n"
468 " \"host-key\": [\n"
469 " {\n"
470 " \"name\": \"default-key\",\n"
471 " \"public-key\": {\n"
472 " \"keystore-reference\": \"genkey\"\n"
473 " }\n"
474 " }\n"
475 " ]\n"
476 " },\n"
477 " \"client-authentication\": {\n"
478 " \"supported-authentication-methods\": {\n"
479 " \"publickey\": [null],\n"
480 " \"passsword\": [null],\n"
481 " \"other\": [\n"
482 " \"interactive\",\n"
483 " \"gssapi\"\n"
484 " ]\n"
485 " }\n"
486 " }\n"
487 " }\n"
488 " }\n"
489 " }\n"
490 " ]\n"
491 " }\n"
492 " }\n"
493 " }\n"
494 " }\n"
495 "}\n";
496 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
497 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
498 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200499}
500
501static void
502test_rpc(void **state)
503{
Radek Krejci1798aae2020-07-14 13:26:06 +0200504 const char *data;
505 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200506 struct lyd_node *tree, *op;
507 const struct lyd_node *node;
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100508 const char *dsc = "Edit data in an NMDA datastore.\n"
509 "\n"
510 "If an error condition occurs such that an error severity\n"
511 "<rpc-error> element is generated, the server will stop\n"
512 "processing the <edit-data> operation and restore the\n"
513 "specified configuration to its complete state at\n"
514 "the start of this <edit-data> operation.";
Radek Krejci1798aae2020-07-14 13:26:06 +0200515
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100516 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
Radek Krejci1798aae2020-07-14 13:26:06 +0200517
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100518 data = "{\"ietf-netconf-nmda:edit-data\":{"
519 "\"datastore\":\"ietf-datastores:running\","
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100520 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
521 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
Michal Vasko2552ea32020-12-08 15:32:34 +0100522 "}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200523 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100524 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 +0200525 ly_in_free(in, 0);
526
527 assert_non_null(op);
Radek Krejci1798aae2020-07-14 13:26:06 +0200528
Radek Krejci2a9fc652021-01-22 17:44:34 +0100529 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100530 1, 0, 0, 1, "edit-data", LYS_RPC,
531 0, 0, 0, 0, 0, NULL, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200532
Michal Vasko2552ea32020-12-08 15:32:34 +0100533 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100534 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100535 1, 0, 0, 1, "edit-data", LYS_RPC,
536 0, 0, 0, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200537 node = lyd_child(node)->next;
Michal Vaskoca0b23b2022-01-06 11:40:10 +0100538 CHECK_LYSC_NODE(node->schema, "Inline config content.", 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config",
539 0, LYS_ANYDATA, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200540
541 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100542 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 +0200543 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200544 node = lyd_child(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200545 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200546 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 +0200547 node = node->parent->next;
548 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200549 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 +0200550
Radek Iša56ca9e42020-09-08 18:42:00 +0200551 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200552 lyd_free_all(tree);
553
554 /* wrong namespace, element name, whatever... */
555 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200556}
557
558static void
559test_action(void **state)
560{
Radek Krejci1798aae2020-07-14 13:26:06 +0200561 const char *data;
562 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200563 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200564
Michal Vasko2552ea32020-12-08 15:32:34 +0100565 data = "{\"a:c\":{\"act\":{\"al\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200566 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100567 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 +0200568 ly_in_free(in, 0);
569
570 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100571 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200572 1, 0, 0, 1, "act", LYS_ACTION,
573 1, 0, 0, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200574
Radek Iša56ca9e42020-09-08 18:42:00 +0200575 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200576 lyd_free_all(tree);
577
578 /* wrong namespace, element name, whatever... */
579 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200580}
581
582static void
583test_notification(void **state)
584{
Radek Krejci1798aae2020-07-14 13:26:06 +0200585 const char *data;
586 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200587 struct lyd_node *tree, *ntf;
Radek Krejci1798aae2020-07-14 13:26:06 +0200588
Michal Vasko2552ea32020-12-08 15:32:34 +0100589 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200590 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100591 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 +0200592 ly_in_free(in, 0);
593
594 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100595 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 +0200596
Michal Vasko2552ea32020-12-08 15:32:34 +0100597 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 +0200598
Radek Iša56ca9e42020-09-08 18:42:00 +0200599 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200600 lyd_free_all(tree);
601
Radek Krejci1798aae2020-07-14 13:26:06 +0200602 data = "{\"a:n2\":{}}";
603 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100604 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 +0200605 ly_in_free(in, 0);
606
607 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100608 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 +0200609
610 assert_non_null(tree);
611 assert_ptr_equal(ntf, tree);
612
Radek Iša56ca9e42020-09-08 18:42:00 +0200613 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200614 lyd_free_all(tree);
615
616 /* wrong namespace, element name, whatever... */
617 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200618}
619
620static void
621test_reply(void **state)
622{
Radek Krejci1798aae2020-07-14 13:26:06 +0200623 const char *data;
624 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100625 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200626 const struct lyd_node *node;
627
Michal Vasko2552ea32020-12-08 15:32:34 +0100628 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200629 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100630 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 +0200631 ly_in_free(in, 0);
632
Radek Krejci1798aae2020-07-14 13:26:06 +0200633 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100634 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200635 1, 0, 0, 1, "act", LYS_ACTION,
636 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200637 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100638 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 +0200639
Michal Vasko2552ea32020-12-08 15:32:34 +0100640 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 +0200641
642 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200643 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:al\":25}");
Michal Vasko2552ea32020-12-08 15:32:34 +0100644 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:c\":{\"act\":{\"al\":25}}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200645 lyd_free_all(tree);
646
647 /* wrong namespace, element name, whatever... */
648 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200649}
650
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100651int
652main(void)
Radek Krejci1798aae2020-07-14 13:26:06 +0200653{
654 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200655 UTEST(test_leaf, setup),
656 UTEST(test_leaflist, setup),
657 UTEST(test_anydata, setup),
658 UTEST(test_list, setup),
659 UTEST(test_container, setup),
660 UTEST(test_opaq, setup),
661 UTEST(test_rpc, setup),
662 UTEST(test_action, setup),
663 UTEST(test_notification, setup),
664 UTEST(test_reply, setup),
Radek Krejci1798aae2020-07-14 13:26:06 +0200665 };
666
667 return cmocka_run_group_tests(tests, NULL, NULL);
668}