blob: 87a440081a2b96102b17ba84dcd1ac05418fefd0 [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,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100233 "Missing JSON data instance no. 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,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100236 tree, LY_EVALID, "Missing 3rd JSON data instance 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);
Radek Krejci1798aae2020-07-14 13:26:06 +0200253}
254
255static void
256test_list(void **state)
257{
Radek Iša56ca9e42020-09-08 18:42:00 +0200258 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200259 struct lyd_node *tree, *iter;
260 struct lyd_node_inner *list;
261 struct lyd_node_term *leaf;
262
263 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200264 data = "{\"a:l1\":[{\"a\":\"one\",\"b\":\"one\",\"c\":1}]}";
265 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
266 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
267 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100268 list = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200269 LY_LIST_FOR(list->child, iter) {
270 assert_int_not_equal(0, iter->hash);
271 }
Radek Krejci5536d282020-08-04 23:27:44 +0200272
Radek Iša56ca9e42020-09-08 18:42:00 +0200273 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200274 lyd_free_all(tree);
275
276 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200277 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100278 "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 +0200279
Radek Iša56ca9e42020-09-08 18:42:00 +0200280 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100281 "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 +0200282
Radek Iša56ca9e42020-09-08 18:42:00 +0200283 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"b\" : \"b\", \"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100284 "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 +0200285
286 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200287 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\", \"a\" : \"a\", \"c\" : 1}]}", 0, LYD_VALIDATE_PRESENT,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100288 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 +0200289
290 /* keys order, in contrast to XML, JSON accepts keys in any order even in strict mode */
Radek Iša56ca9e42020-09-08 18:42:00 +0200291 CHECK_PARSE_LYD("{ \"a:l1\": [ {\"d\" : \"d\", \"a\" : \"a\", \"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree);
292 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
293 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100294 list = (struct lyd_node_inner *)tree;
295 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200296 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 +0100297 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200298 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 +0100299 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200300 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 +0100301 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200302 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "d", 0, LYS_LEAF, 1, 0, NULL, 0);
303 CHECK_LOG_CTX(NULL, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200304
Radek Iša56ca9e42020-09-08 18:42:00 +0200305 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
306 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1,\"d\":\"d\"}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200307 lyd_free_all(tree);
308
309 /* */
Radek Iša56ca9e42020-09-08 18:42:00 +0200310 CHECK_PARSE_LYD("{\"a:l1\":[{\"c\":1,\"b\":\"b\",\"a\":\"a\"}]}", LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree);
311 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
312 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100313 list = (struct lyd_node_inner *)tree;
314 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200315 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a",
316 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100317 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200318 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b",
319 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100320 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200321 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c",
322 1, LYS_LEAF, 1, 0, NULL, 0);
323 CHECK_LOG_CTX(NULL, NULL);
Radek Krejci5536d282020-08-04 23:27:44 +0200324
Radek Iša56ca9e42020-09-08 18:42:00 +0200325 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
326 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200327 lyd_free_all(tree);
328
Radek Krejci5536d282020-08-04 23:27:44 +0200329 data = "{\"a:cp\":{\"@\":{\"a:hint\":1}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200330 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200331 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100332 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100333 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 +0200334 1, LYS_CONTAINER, 0, 0, NULL, 0);
335 CHECK_LYD_META(tree->meta, 1, "hint", 0, 1, INT8, "1", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200336 assert_null(tree->meta->next);
Radek Krejci5536d282020-08-04 23:27:44 +0200337
Radek Iša56ca9e42020-09-08 18:42:00 +0200338 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200339 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200340}
341
342static void
343test_container(void **state)
344{
Radek Iša56ca9e42020-09-08 18:42:00 +0200345 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200346 struct lyd_node *tree;
347 struct lyd_node_inner *cont;
348
Radek Iša56ca9e42020-09-08 18:42:00 +0200349 CHECK_PARSE_LYD("{\"a:c\":{}}", 0, LYD_VALIDATE_PRESENT, tree);
350 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c",
351 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100352 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200353 assert_true(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200354
Radek Iša56ca9e42020-09-08 18:42:00 +0200355 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200356 lyd_free_all(tree);
357
Radek Krejci5536d282020-08-04 23:27:44 +0200358 data = "{\"a:cp\":{}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200359 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200360 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100361 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100362 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 +0200363 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100364 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200365 assert_false(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200366
Radek Iša56ca9e42020-09-08 18:42:00 +0200367 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200368 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200369}
370
371static void
372test_opaq(void **state)
373{
Radek Krejci1798aae2020-07-14 13:26:06 +0200374 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200375 struct lyd_node *tree;
376
Radek Krejci1798aae2020-07-14 13:26:06 +0200377 /* invalid value, no flags */
378 data = "{\"a:foo3\":[null]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200379 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100380 "Invalid non-number-encoded uint32 value \"\".", "Schema location /a:foo3, line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200381
382 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200383 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, LYD_VALIDATE_PRESENT, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200384 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 +0200385 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200386 lyd_free_all(tree);
387
388 /* missing key, no flags */
389 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"d\":\"val_d\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200390 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100391 "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 +0200392
393 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200394 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, LYD_VALIDATE_PRESENT, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200395 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 +0200396 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200397 lyd_free_all(tree);
398
399 /* invalid key, no flags */
400 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200401 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100402 "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 +0200403
404 /* opaq flag */
Radek Iša56ca9e42020-09-08 18:42:00 +0200405 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, LYD_VALIDATE_PRESENT, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200406 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 +0200407 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200408 lyd_free_all(tree);
409
410 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":{\"val\":\"val_c\"}}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200411 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, LYD_VALIDATE_PRESENT, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200412 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 +0200413 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200414 lyd_free_all(tree);
415
416 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200417 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, LYD_VALIDATE_PRESENT, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200418 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 +0200419 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200420 lyd_free_all(tree);
aPiecekb7b29e62021-05-11 10:02:43 +0200421
aPiecekc5f36a72021-05-18 14:12:31 +0200422 /* mixing with metadata that is ignored */
423 data = "{\"@a:foo\":\"str\",\"@a:foo3\":1,\"a:foo3\":2}";
424 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
425 lyd_free_all(tree);
426
aPiecekb7b29e62021-05-11 10:02:43 +0200427 /* empty name */
428 PARSER_CHECK_ERROR("{\"@a:foo\":{\"\":0}}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
429 "A JSON object member name cannot be a zero-length string.", "Line number 1.");
Radek Krejci1798aae2020-07-14 13:26:06 +0200430}
431
432static void
433test_rpc(void **state)
434{
Radek Krejci1798aae2020-07-14 13:26:06 +0200435 const char *data;
436 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200437 struct lyd_node *tree, *op;
438 const struct lyd_node *node;
Radek Iša56ca9e42020-09-08 18:42:00 +0200439 const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
440 "configuration to the specified target configuration.";
441 const char *ref = "RFC 6241, Section 7.2";
442 const char *feats[] = {"writable-running", NULL};
Radek Krejci1798aae2020-07-14 13:26:06 +0200443
Radek Iša56ca9e42020-09-08 18:42:00 +0200444 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
Radek Krejci1798aae2020-07-14 13:26:06 +0200445
Michal Vasko2552ea32020-12-08 15:32:34 +0100446 data = "{\"ietf-netconf:edit-config\":{"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100447 "\"target\":{\"running\":[null]},"
448 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
449 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
Michal Vasko2552ea32020-12-08 15:32:34 +0100450 "}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200451 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100452 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 +0200453 ly_in_free(in, 0);
454
455 assert_non_null(op);
Radek Krejci1798aae2020-07-14 13:26:06 +0200456
Radek Krejci2a9fc652021-01-22 17:44:34 +0100457 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200458 1, 0, 0, 1, "edit-config", LYS_RPC,
459 0, 0, 0, 0, 0, ref, 0);
460
Michal Vasko2552ea32020-12-08 15:32:34 +0100461 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100462 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200463 1, 0, 0, 1, "edit-config", LYS_RPC,
464 0, 0, 0, 0, 0, ref, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200465 node = lyd_child(node)->next;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100466 CHECK_LYSC_NODE(node->schema, "Inline Config content.", 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config",
Radek Iša56ca9e42020-09-08 18:42:00 +0200467 0, LYS_ANYXML, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200468
469 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100470 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 +0200471 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200472 node = lyd_child(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200473 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200474 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 +0200475 node = node->parent->next;
476 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200477 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 +0200478
Radek Iša56ca9e42020-09-08 18:42:00 +0200479 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200480 lyd_free_all(tree);
481
482 /* wrong namespace, element name, whatever... */
483 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200484}
485
486static void
487test_action(void **state)
488{
Radek Krejci1798aae2020-07-14 13:26:06 +0200489 const char *data;
490 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200491 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200492
Michal Vasko2552ea32020-12-08 15:32:34 +0100493 data = "{\"a:c\":{\"act\":{\"al\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200494 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100495 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 +0200496 ly_in_free(in, 0);
497
498 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100499 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200500 1, 0, 0, 1, "act", LYS_ACTION,
501 1, 0, 0, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200502
Radek Iša56ca9e42020-09-08 18:42:00 +0200503 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200504 lyd_free_all(tree);
505
506 /* wrong namespace, element name, whatever... */
507 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200508}
509
510static void
511test_notification(void **state)
512{
Radek Krejci1798aae2020-07-14 13:26:06 +0200513 const char *data;
514 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200515 struct lyd_node *tree, *ntf;
Radek Krejci1798aae2020-07-14 13:26:06 +0200516
Michal Vasko2552ea32020-12-08 15:32:34 +0100517 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200518 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100519 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 +0200520 ly_in_free(in, 0);
521
522 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100523 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 +0200524
Michal Vasko2552ea32020-12-08 15:32:34 +0100525 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 +0200526
Radek Iša56ca9e42020-09-08 18:42:00 +0200527 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200528 lyd_free_all(tree);
529
Radek Krejci1798aae2020-07-14 13:26:06 +0200530 data = "{\"a:n2\":{}}";
531 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100532 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 +0200533 ly_in_free(in, 0);
534
535 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100536 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 +0200537
538 assert_non_null(tree);
539 assert_ptr_equal(ntf, tree);
540
Radek Iša56ca9e42020-09-08 18:42:00 +0200541 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200542 lyd_free_all(tree);
543
544 /* wrong namespace, element name, whatever... */
545 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200546}
547
548static void
549test_reply(void **state)
550{
Radek Krejci1798aae2020-07-14 13:26:06 +0200551 const char *data;
552 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100553 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200554 const struct lyd_node *node;
555
Michal Vasko2552ea32020-12-08 15:32:34 +0100556 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200557 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100558 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 +0200559 ly_in_free(in, 0);
560
Radek Krejci1798aae2020-07-14 13:26:06 +0200561 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100562 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200563 1, 0, 0, 1, "act", LYS_ACTION,
564 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200565 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100566 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 +0200567
Michal Vasko2552ea32020-12-08 15:32:34 +0100568 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 +0200569
570 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200571 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:al\":25}");
Michal Vasko2552ea32020-12-08 15:32:34 +0100572 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:c\":{\"act\":{\"al\":25}}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200573 lyd_free_all(tree);
574
575 /* wrong namespace, element name, whatever... */
576 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200577}
578
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100579int
580main(void)
Radek Krejci1798aae2020-07-14 13:26:06 +0200581{
582 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200583 UTEST(test_leaf, setup),
584 UTEST(test_leaflist, setup),
585 UTEST(test_anydata, setup),
586 UTEST(test_list, setup),
587 UTEST(test_container, setup),
588 UTEST(test_opaq, setup),
589 UTEST(test_rpc, setup),
590 UTEST(test_action, setup),
591 UTEST(test_notification, setup),
592 UTEST(test_reply, setup),
Radek Krejci1798aae2020-07-14 13:26:06 +0200593 };
594
595 return cmocka_run_group_tests(tests, NULL, NULL);
596}