blob: 6a50913a8fb0493d31878f4ddc8a7746e5b9abdc [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);
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 */
Michal Vasko1a85d332021-08-27 10:35:28 +0200383 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, 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 */
Michal Vasko1a85d332021-08-27 10:35:28 +0200394 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, 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 */
Michal Vasko1a85d332021-08-27 10:35:28 +0200405 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, 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\"}}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200411 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, 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\"}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200417 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, 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.");
Michal Vasko1a85d332021-08-27 10:35:28 +0200430
431 /* opaque data tree format print */
432 data =
433 "{\n"
434 " \"ietf-netconf-nmda:get-data\": {\n"
435 " \"data\": {\n"
436 " \"ietf-keystore:keystore\": {\n"
437 " \"asymmetric-keys\": {\n"
438 " \"asymmetric-key\": [\n"
439 " {\n"
440 " \"name\": \"genkey\",\n"
441 " \"algorithm\": \"rsa2048\"\n"
442 " }\n"
443 " ]\n"
444 " }\n"
445 " },\n"
446 " \"ietf-netconf-server:netconf-server\": {\n"
447 " \"listen\": {\n"
448 " \"idle-timeout\": 3600,\n"
449 " \"endpoint\": [\n"
450 " {\n"
451 " \"name\": \"default-ssh\",\n"
452 " \"ssh\": {\n"
453 " \"tcp-server-parameters\": {\n"
454 " \"local-address\": \"0.0.0.0\",\n"
455 " \"local-port\": 830\n"
456 " },\n"
457 " \"ssh-server-parameters\": {\n"
458 " \"server-identity\": {\n"
459 " \"host-key\": [\n"
460 " {\n"
461 " \"name\": \"default-key\",\n"
462 " \"public-key\": {\n"
463 " \"keystore-reference\": \"genkey\"\n"
464 " }\n"
465 " }\n"
466 " ]\n"
467 " },\n"
468 " \"client-authentication\": {\n"
469 " \"supported-authentication-methods\": {\n"
470 " \"publickey\": [null],\n"
471 " \"passsword\": [null],\n"
472 " \"other\": [\n"
473 " \"interactive\",\n"
474 " \"gssapi\"\n"
475 " ]\n"
476 " }\n"
477 " }\n"
478 " }\n"
479 " }\n"
480 " }\n"
481 " ]\n"
482 " }\n"
483 " }\n"
484 " }\n"
485 " }\n"
486 "}\n";
487 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
488 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
489 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200490}
491
492static void
493test_rpc(void **state)
494{
Radek Krejci1798aae2020-07-14 13:26:06 +0200495 const char *data;
496 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200497 struct lyd_node *tree, *op;
498 const struct lyd_node *node;
Radek Iša56ca9e42020-09-08 18:42:00 +0200499 const char *dsc = "The <edit-config> operation loads all or part of a specified\n"
500 "configuration to the specified target configuration.";
501 const char *ref = "RFC 6241, Section 7.2";
502 const char *feats[] = {"writable-running", NULL};
Radek Krejci1798aae2020-07-14 13:26:06 +0200503
Radek Iša56ca9e42020-09-08 18:42:00 +0200504 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf", "2011-06-01", feats)));
Radek Krejci1798aae2020-07-14 13:26:06 +0200505
Michal Vasko2552ea32020-12-08 15:32:34 +0100506 data = "{\"ietf-netconf:edit-config\":{"
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100507 "\"target\":{\"running\":[null]},"
508 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
509 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
Michal Vasko2552ea32020-12-08 15:32:34 +0100510 "}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200511 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100512 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 +0200513 ly_in_free(in, 0);
514
515 assert_non_null(op);
Radek Krejci1798aae2020-07-14 13:26:06 +0200516
Radek Krejci2a9fc652021-01-22 17:44:34 +0100517 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200518 1, 0, 0, 1, "edit-config", LYS_RPC,
519 0, 0, 0, 0, 0, ref, 0);
520
Michal Vasko2552ea32020-12-08 15:32:34 +0100521 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100522 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200523 1, 0, 0, 1, "edit-config", LYS_RPC,
524 0, 0, 0, 0, 0, ref, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200525 node = lyd_child(node)->next;
Michal Vaskod1e53b92021-01-28 13:11:06 +0100526 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 +0200527 0, LYS_ANYXML, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200528
529 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100530 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 +0200531 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200532 node = lyd_child(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200533 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200534 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 +0200535 node = node->parent->next;
536 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200537 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 +0200538
Radek Iša56ca9e42020-09-08 18:42:00 +0200539 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200540 lyd_free_all(tree);
541
542 /* wrong namespace, element name, whatever... */
543 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200544}
545
546static void
547test_action(void **state)
548{
Radek Krejci1798aae2020-07-14 13:26:06 +0200549 const char *data;
550 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200551 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200552
Michal Vasko2552ea32020-12-08 15:32:34 +0100553 data = "{\"a:c\":{\"act\":{\"al\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200554 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100555 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 +0200556 ly_in_free(in, 0);
557
558 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100559 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200560 1, 0, 0, 1, "act", LYS_ACTION,
561 1, 0, 0, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200562
Radek Iša56ca9e42020-09-08 18:42:00 +0200563 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200564 lyd_free_all(tree);
565
566 /* wrong namespace, element name, whatever... */
567 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200568}
569
570static void
571test_notification(void **state)
572{
Radek Krejci1798aae2020-07-14 13:26:06 +0200573 const char *data;
574 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200575 struct lyd_node *tree, *ntf;
Radek Krejci1798aae2020-07-14 13:26:06 +0200576
Michal Vasko2552ea32020-12-08 15:32:34 +0100577 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200578 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100579 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 +0200580 ly_in_free(in, 0);
581
582 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100583 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 +0200584
Michal Vasko2552ea32020-12-08 15:32:34 +0100585 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 +0200586
Radek Iša56ca9e42020-09-08 18:42:00 +0200587 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200588 lyd_free_all(tree);
589
Radek Krejci1798aae2020-07-14 13:26:06 +0200590 data = "{\"a:n2\":{}}";
591 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100592 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 +0200593 ly_in_free(in, 0);
594
595 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100596 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 +0200597
598 assert_non_null(tree);
599 assert_ptr_equal(ntf, tree);
600
Radek Iša56ca9e42020-09-08 18:42:00 +0200601 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200602 lyd_free_all(tree);
603
604 /* wrong namespace, element name, whatever... */
605 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200606}
607
608static void
609test_reply(void **state)
610{
Radek Krejci1798aae2020-07-14 13:26:06 +0200611 const char *data;
612 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100613 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200614 const struct lyd_node *node;
615
Michal Vasko2552ea32020-12-08 15:32:34 +0100616 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200617 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100618 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 +0200619 ly_in_free(in, 0);
620
Radek Krejci1798aae2020-07-14 13:26:06 +0200621 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100622 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200623 1, 0, 0, 1, "act", LYS_ACTION,
624 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200625 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100626 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 +0200627
Michal Vasko2552ea32020-12-08 15:32:34 +0100628 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 +0200629
630 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200631 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:al\":25}");
Michal Vasko2552ea32020-12-08 15:32:34 +0100632 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:c\":{\"act\":{\"al\":25}}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200633 lyd_free_all(tree);
634
635 /* wrong namespace, element name, whatever... */
636 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200637}
638
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100639int
640main(void)
Radek Krejci1798aae2020-07-14 13:26:06 +0200641{
642 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200643 UTEST(test_leaf, setup),
644 UTEST(test_leaflist, setup),
645 UTEST(test_anydata, setup),
646 UTEST(test_list, setup),
647 UTEST(test_container, setup),
648 UTEST(test_opaq, setup),
649 UTEST(test_rpc, setup),
650 UTEST(test_action, setup),
651 UTEST(test_notification, setup),
652 UTEST(test_reply, setup),
Radek Krejci1798aae2020-07-14 13:26:06 +0200653 };
654
655 return cmocka_run_group_tests(tests, NULL, NULL);
656}