blob: f7311617f8dfa2feed21514fc38377263f0b29ae [file] [log] [blame]
Michal Vasko5b414dd2023-04-13 10:29:58 +02001/**
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_parser_json.c
Michal Vasko5b414dd2023-04-13 10:29:58 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief unit tests for JSON parser
Radek Krejci1798aae2020-07-14 13:26:06 +02006 *
Michal Vasko5b414dd2023-04-13 10:29:58 +02007 * Copyright (c) 2019 - 2023 CESNET, z.s.p.o.
Radek Krejci1798aae2020-07-14 13:26:06 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Radek Iša56ca9e42020-09-08 18:42:00 +020015#define _UTEST_MAIN_
16#include "utests.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020017
Radek Krejci1798aae2020-07-14 13:26:06 +020018#include "context.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020019#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020020#include "out.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010021#include "parser_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020022#include "printer_data.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020023#include "tests_config.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020024#include "tree_data_internal.h"
25#include "tree_schema.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020026
27static int
28setup(void **state)
29{
Radek Iša56ca9e42020-09-08 18:42:00 +020030 const char *schema = "module a {namespace urn:tests:a;prefix a;yang-version 1.1; import ietf-yang-metadata {prefix md;}"
Radek Krejci1798aae2020-07-14 13:26:06 +020031 "md:annotation hint { type int8;}"
Michal Vaskodbf3e652022-10-21 08:46:25 +020032 "list l1 { key \"a b c\"; leaf a {type string;} leaf b {type string;} leaf c {type int16;}"
33 " leaf d {type string;}"
34 " container cont {leaf e {type boolean;}}"
35 "}"
Radek Krejci1798aae2020-07-14 13:26:06 +020036 "leaf foo { type string;}"
37 "container c {"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010038 " leaf x {type string;}"
39 " action act { input { leaf al {type string;} } output { leaf al {type uint8;} } }"
40 " notification n1 { leaf nl {type string;} }"
Radek Krejci1798aae2020-07-14 13:26:06 +020041 "}"
42 "container cp {presence \"container switch\"; leaf y {type string;} leaf z {type int8;}}"
43 "anydata any {config false;}"
Michal Vasko76096ec2022-02-24 16:06:16 +010044 "anyxml axml;"
Radek Krejci1798aae2020-07-14 13:26:06 +020045 "leaf-list ll1 { type uint8; }"
46 "leaf foo2 { type string; default \"default-val\"; }"
47 "leaf foo3 { type uint32; }"
Michal Vasko583b4642023-05-25 10:39:34 +020048 "leaf foo4 { type uint64; }"
Michal Vasko5df28522023-08-25 08:04:37 +020049 "rpc r1 {input {leaf l1 {type string;} leaf l2 {type string;}}}"
Michal Vaskodd03ff12023-09-11 10:30:48 +020050 "notification n2;"
51 "}";
Radek Krejci1798aae2020-07-14 13:26:06 +020052
Radek Iša56ca9e42020-09-08 18:42:00 +020053 UTEST_SETUP;
Radek Krejci1798aae2020-07-14 13:26:06 +020054
Radek Iša56ca9e42020-09-08 18:42:00 +020055 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
56 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
Radek Krejci1798aae2020-07-14 13:26:06 +020057
58 return 0;
59}
60
Radek Iša56ca9e42020-09-08 18:42:00 +020061#define CHECK_PARSE_LYD(INPUT, PARSE_OPTION, VALIDATE_OPTION, TREE) \
62 CHECK_PARSE_LYD_PARAM(INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, LY_SUCCESS, TREE)
Radek Krejci1798aae2020-07-14 13:26:06 +020063
Michal Vasko7a266772024-01-23 11:02:38 +010064#define PARSER_CHECK_ERROR(INPUT, PARSE_OPTION, VALIDATE_OPTION, MODEL, RET_VAL, ERR_MESSAGE, ERR_PATH, ERR_LINE) \
Radek Iša56ca9e42020-09-08 18:42:00 +020065 assert_int_equal(RET_VAL, lyd_parse_data_mem(UTEST_LYCTX, INPUT, LYD_JSON, PARSE_OPTION, VALIDATE_OPTION, &MODEL));\
Michal Vasko7a266772024-01-23 11:02:38 +010066 CHECK_LOG_CTX(ERR_MESSAGE, ERR_PATH, ERR_LINE);\
Radek Iša56ca9e42020-09-08 18:42:00 +020067 assert_null(MODEL)
Radek Krejci1798aae2020-07-14 13:26:06 +020068
Radek Iša56ca9e42020-09-08 18:42:00 +020069#define CHECK_LYD_STRING(IN_MODEL, PRINT_OPTION, TEXT) \
70 CHECK_LYD_STRING_PARAM(IN_MODEL, TEXT, LYD_JSON, PRINT_OPTION)
Radek Krejci1798aae2020-07-14 13:26:06 +020071
72static void
73test_leaf(void **state)
74{
Radek Krejci1798aae2020-07-14 13:26:06 +020075 struct lyd_node *tree;
76 struct lyd_node_term *leaf;
Radek Iša56ca9e42020-09-08 18:42:00 +020077 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +020078
Radek Iša56ca9e42020-09-08 18:42:00 +020079 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-with-defaults", "2011-06-01", NULL));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010080
Radek Iša56ca9e42020-09-08 18:42:00 +020081 data = "{\"a:foo\":\"foo value\"}";
82 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
83 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010084 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +020085 CHECK_LYD_VALUE(leaf->value, STRING, "foo value");
Radek Krejci1798aae2020-07-14 13:26:06 +020086
Radek Iša56ca9e42020-09-08 18:42:00 +020087 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
88 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010089 leaf = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +020090
91 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +020092 assert_true(leaf->flags & LYD_DEFAULT);
93
Radek Iša56ca9e42020-09-08 18:42:00 +020094 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +020095 lyd_free_all(tree);
96
97 /* make foo2 explicit */
98 data = "{\"a:foo2\":\"default-val\"}";
Radek Iša56ca9e42020-09-08 18:42:00 +020099 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200100 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100101 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200102 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
103 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100104 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200105 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +0200106 assert_false(leaf->flags & LYD_DEFAULT);
107
Radek Iša56ca9e42020-09-08 18:42:00 +0200108 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200109 lyd_free_all(tree);
110
111 /* parse foo2 but make it implicit */
Radek Krejci5536d282020-08-04 23:27:44 +0200112 data = "{\"a:foo2\":\"default-val\",\"@a:foo2\":{\"ietf-netconf-with-defaults:default\":true}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200113 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200114 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100115 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200116 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_SET_DFLT, 1, "foo2",
117 1, LYS_LEAF, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100118 leaf = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200119 CHECK_LYD_VALUE(leaf->value, STRING, "default-val");
Radek Krejci1798aae2020-07-14 13:26:06 +0200120 assert_true(leaf->flags & LYD_DEFAULT);
121
Michal Vaskob68c3b42022-05-20 10:36:40 +0200122 /* print default values */
123 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL_TAG, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200124 lyd_free_all(tree);
125
Michal Vaskoa4af6252022-05-20 10:38:54 +0200126 /* skip leaf */
127 data = "{\"a:cp\":{\"x\":\"val\",\"y\":\"valy\",\"z\":5}}";
128 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
129 assert_non_null(tree);
130
131 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:cp\":{\"y\":\"valy\",\"z\":5}}");
132 lyd_free_all(tree);
133
Radek Krejci1798aae2020-07-14 13:26:06 +0200134 /* multiple meatadata hint and unknown metadata xxx supposed to be skipped since it is from missing schema */
Radek Krejci5536d282020-08-04 23:27:44 +0200135 data = "{\"@a:foo\":{\"a:hint\":1,\"a:hint\":2,\"x:xxx\":{\"value\":\"/x:no/x:yes\"}},\"a:foo\":\"xxx\"}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200136 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
137 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "foo", 1, LYS_LEAF, 0, 0, NULL, 0);
138 CHECK_LYD_META(tree->meta, 1, "hint", 1, 1, INT8, "1", 1);
139 CHECK_LYD_META(tree->meta->next, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200140 assert_null(tree->meta->next->next);
141
Radek Iša56ca9e42020-09-08 18:42:00 +0200142 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
143 "{\"a:foo\":\"xxx\",\"@a:foo\":{\"a:hint\":1,\"a:hint\":2}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200144 lyd_free_all(tree);
145
Radek Iša56ca9e42020-09-08 18:42:00 +0200146 PARSER_CHECK_ERROR(data, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100147 "Unknown (or not implemented) YANG module \"x\" of metadata \"x:xxx\".", "/@a:foo", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200148
149 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200150 PARSER_CHECK_ERROR("{\"@a:foo\" : { \"a:hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100151 "Missing JSON data instance to be coupled with @a:foo metadata.", "/@a:foo", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200152
153 /* missing namespace for meatadata*/
Radek Iša56ca9e42020-09-08 18:42:00 +0200154 PARSER_CHECK_ERROR("{\"a:foo\" : \"value\", \"@a:foo\" : { \"hint\" : 1 }}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100155 "Metadata in JSON must be namespace-qualified, missing prefix for \"hint\".", "/a:foo", 1);
aPiecek49e2a3a2021-05-13 10:36:46 +0200156
Michal Vaskodbf3e652022-10-21 08:46:25 +0200157 /* invalid JSON type */
158 data = "{\"a:l1\" : [{ \"a\" : \"val-a\", \"b\" : \"val-b\", \"c\" : 1, \"cont\" : { \"e\" : \"0\" } }]}";
159 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100160 "Invalid non-boolean-encoded boolean value \"0\".", "/a:l1[a='val-a'][b='val-b'][c='1']/cont/e", 1);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200161
aPiecek49e2a3a2021-05-13 10:36:46 +0200162 /* reverse solidus in JSON object member name */
163 data = "{\"@a:foo\":{\"a:hi\\nt\":1},\"a:foo\":\"xxx\"}";
164 assert_int_equal(LY_EINVAL, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
Michal Vasko7a266772024-01-23 11:02:38 +0100165 CHECK_LOG_CTX("Annotation definition for attribute \"a:hi\nt\" not found.", "/@a:foo/@a:hi\nt", 1);
stewegb3e420c2024-04-05 08:05:29 +0200166
167 data = "{\"a:foo\": null}";
168 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, "Invalid non-string-encoded string value \"\".", "/a:foo", 1);
169 CHECK_PARSE_LYD(data, LYD_PARSE_JSON_NULL, LYD_VALIDATE_PRESENT, tree);
170 assert_null(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200171}
172
173static void
174test_leaflist(void **state)
175{
Radek Iša56ca9e42020-09-08 18:42:00 +0200176 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200177 struct lyd_node *tree;
178 struct lyd_node_term *ll;
179
Radek Iša56ca9e42020-09-08 18:42:00 +0200180 data = "{\"a:ll1\":[10,11]}";
181 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200182 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100183 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200184 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
185 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100186 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200187 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200188
189 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200190 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
191 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100192 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200193 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
Radek Krejci1798aae2020-07-14 13:26:06 +0200194
Radek Iša56ca9e42020-09-08 18:42:00 +0200195 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200196 lyd_free_all(tree);
197
Michal Vasko4e26adc2022-03-30 11:01:16 +0200198 /* accept empty */
199 data = "{\"a:ll1\":[]}";
200 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
201 assert_null(tree);
202
Radek Krejci1798aae2020-07-14 13:26:06 +0200203 /* simple metadata */
Radek Krejci5536d282020-08-04 23:27:44 +0200204 data = "{\"a:ll1\":[10,11],\"@a:ll1\":[null,{\"a:hint\":2}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200205 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200206 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100207 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200208 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
209 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100210 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200211 CHECK_LYD_VALUE(ll->value, UINT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200212
213 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200214 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
215 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100216 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200217 CHECK_LYD_VALUE(ll->value, UINT8, "11", 11);
218 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200219 assert_null(ll->meta->next);
220
Radek Iša56ca9e42020-09-08 18:42:00 +0200221 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200222 lyd_free_all(tree);
223
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100224 /* 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 +0200225 data = "{\"@a:ll1\" : [{\"a:hint\" : 1, \"x:xxx\" : { \"value\" : \"/x:no/x:yes\" }, "
226 "\"a:hint\" : 10},null,{\"a:hint\" : 3}], \"a:ll1\" : [1,2,3]}";
227 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200228 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100229 tree = tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200230 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
231 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100232 ll = (struct lyd_node_term *)tree;
Radek Iša56ca9e42020-09-08 18:42:00 +0200233 CHECK_LYD_VALUE(ll->value, UINT8, "1", 1);
aPiecek6cf1d162023-11-08 16:07:00 +0100234 CHECK_LYD_META(ll->meta->next, 1, "hint", 1, 1, INT8, "1", 1);
235 CHECK_LYD_META(ll->meta->next->next, 1, "hint", 0, 1, INT8, "10", 10);
Radek Krejci1798aae2020-07-14 13:26:06 +0200236
237 assert_non_null(tree->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200238 CHECK_LYSC_NODE(tree->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
239 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100240 ll = (struct lyd_node_term *)tree->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200241 CHECK_LYD_VALUE(ll->value, UINT8, "2", 2);
Radek Krejci1798aae2020-07-14 13:26:06 +0200242 assert_null(ll->meta);
243
244 assert_non_null(tree->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200245 CHECK_LYSC_NODE(tree->next->next->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "ll1",
246 1, LYS_LEAFLIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100247 ll = (struct lyd_node_term *)tree->next->next;
Radek Iša56ca9e42020-09-08 18:42:00 +0200248 CHECK_LYD_VALUE(ll->value, UINT8, "3", 3);
249 CHECK_LYD_META(ll->meta, 1, "hint", 0, 1, INT8, "3", 3);
Radek Krejci1798aae2020-07-14 13:26:06 +0200250 assert_null(ll->meta->next);
251
Radek Iša56ca9e42020-09-08 18:42:00 +0200252 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
253 "{\"a:ll1\":[1,2,3],\"@a:ll1\":[{\"a:hint\":1,\"a:hint\":10},null,{\"a:hint\":3}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200254 lyd_free_all(tree);
255
256 /* missing referenced metadata node */
Radek Iša56ca9e42020-09-08 18:42:00 +0200257 PARSER_CHECK_ERROR("{\"@a:ll1\":[{\"a:hint\":1}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100258 "Missing JSON data instance to be coupled with @a:ll1 metadata.", "/@a:ll1", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200259
Radek Iša56ca9e42020-09-08 18:42:00 +0200260 PARSER_CHECK_ERROR("{\"a:ll1\":[1],\"@a:ll1\":[{\"a:hint\":1},{\"a:hint\":2}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100261 "Missing JSON data instance #2 of a:ll1 to be coupled with metadata.", "/a:ll1", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200262
Radek Iša56ca9e42020-09-08 18:42:00 +0200263 PARSER_CHECK_ERROR("{\"@a:ll1\":[{\"a:hint\":1},{\"a:hint\":2},{\"a:hint\":3}],\"a:ll1\" : [1, 2]}", 0, LYD_VALIDATE_PRESENT,
Michal Vasko7a266772024-01-23 11:02:38 +0100264 tree, LY_EVALID, "Missing JSON data instance #3 to be coupled with @a:ll1 metadata.", "/@a:ll1", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200265}
266
267static void
268test_anydata(void **state)
269{
Radek Krejci1798aae2020-07-14 13:26:06 +0200270 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200271 struct lyd_node *tree;
272
Radek Krejci1798aae2020-07-14 13:26:06 +0200273 data = "{\"a:any\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200274 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200275 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100276 tree = tree->next;
Michal Vasko76096ec2022-02-24 16:06:16 +0100277 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any",
Radek Iša56ca9e42020-09-08 18:42:00 +0200278 1, LYS_ANYDATA, 0, 0, NULL, 0);
279 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200280 lyd_free_all(tree);
Michal Vaskofbc4a252022-02-03 13:15:58 +0100281
282 data = "{\"a:any\":{}}";
283 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
284 assert_non_null(tree);
285 tree = tree->next;
Michal Vasko76096ec2022-02-24 16:06:16 +0100286 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any",
Michal Vaskofbc4a252022-02-03 13:15:58 +0100287 1, LYS_ANYDATA, 0, 0, NULL, 0);
288 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
289 lyd_free_all(tree);
Michal Vasko76096ec2022-02-24 16:06:16 +0100290
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100291 data = "{\"a:any\":{\"node\":20}}";
Michal Vasko76096ec2022-02-24 16:06:16 +0100292 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
293 assert_non_null(tree);
294 tree = tree->next;
295 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_R | LYS_SET_CONFIG, 1, "any",
296 1, LYS_ANYDATA, 0, 0, NULL, 0);
297 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
298 lyd_free_all(tree);
stewegb3e420c2024-04-05 08:05:29 +0200299
300 data = "{\"a:any\": null}";
301 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, "Expecting JSON name/object but anydata \"any\" is represented in input data as name/null.", NULL, 1);
302 CHECK_PARSE_LYD(data, LYD_PARSE_JSON_NULL, LYD_VALIDATE_PRESENT, tree);
303 assert_null(tree);
Michal Vasko76096ec2022-02-24 16:06:16 +0100304}
305
306static void
307test_anyxml(void **state)
308{
309 const char *data;
310 struct lyd_node *tree;
311
312 data = "{\"a:axml\":\"some-value in string\"}";
313 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
314 assert_non_null(tree);
315 tree = tree->next;
316 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
317 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
318 lyd_free_all(tree);
319
320 data = "{\"a:axml\":\"\"}";
321 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
322 assert_non_null(tree);
323 tree = tree->next;
324 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
325 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
326 lyd_free_all(tree);
327
328 data = "{\"a:axml\":55}";
329 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
330 assert_non_null(tree);
331 tree = tree->next;
332 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
333 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
334 lyd_free_all(tree);
335
336 data = "{\"a:axml\":false}";
337 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
338 assert_non_null(tree);
339 tree = tree->next;
340 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
341 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
342 lyd_free_all(tree);
343
344 data = "{\"a:axml\":null}";
345 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
346 assert_non_null(tree);
347 tree = tree->next;
348 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
349 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
350 lyd_free_all(tree);
351
352 data = "{\"a:axml\":[null,true,false]}";
353 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
354 assert_non_null(tree);
355 tree = tree->next;
356 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
357 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
358 lyd_free_all(tree);
359
360 data = "{\"a:axml\":[null,true,{\"name\":[25,40, false]}]}";
361 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
362 assert_non_null(tree);
363 tree = tree->next;
364 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
365 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
366 lyd_free_all(tree);
367
368 /* same as anydata tests */
369 data = "{\"a:axml\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
370 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
371 assert_non_null(tree);
372 tree = tree->next;
373 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
374 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
375 lyd_free_all(tree);
376
377 data = "{\"a:axml\":{}}";
378 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
379 assert_non_null(tree);
380 tree = tree->next;
381 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_STATUS_CURR | LYS_CONFIG_W, 1, "axml", 1, LYS_ANYXML, 0, 0, NULL, 0);
382 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
383 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200384}
385
386static void
387test_list(void **state)
388{
Radek Iša56ca9e42020-09-08 18:42:00 +0200389 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200390 struct lyd_node *tree, *iter;
391 struct lyd_node_inner *list;
392 struct lyd_node_term *leaf;
393
394 /* check hashes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200395 data = "{\"a:l1\":[{\"a\":\"one\",\"b\":\"one\",\"c\":1}]}";
396 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
397 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
398 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100399 list = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200400 LY_LIST_FOR(list->child, iter) {
401 assert_int_not_equal(0, iter->hash);
402 }
Radek Iša56ca9e42020-09-08 18:42:00 +0200403 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200404 lyd_free_all(tree);
405
Michal Vasko4e26adc2022-03-30 11:01:16 +0200406 /* accept empty */
407 data = "{\"a:l1\":[]}";
408 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
409 assert_null(tree);
410
Radek Krejci1798aae2020-07-14 13:26:06 +0200411 /* missing keys */
Radek Iša56ca9e42020-09-08 18:42:00 +0200412 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100413 "List instance is missing its key \"a\".", "/a:l1[b='b'][c='1']", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200414
Radek Iša56ca9e42020-09-08 18:42:00 +0200415 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100416 "List instance is missing its key \"b\".", "/a:l1[a='a']", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200417
Radek Iša56ca9e42020-09-08 18:42:00 +0200418 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"b\" : \"b\", \"a\" : \"a\"}]}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100419 "List instance is missing its key \"c\".", "/a:l1[a='a'][b='b']", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200420
421 /* key duplicate */
Radek Iša56ca9e42020-09-08 18:42:00 +0200422 PARSER_CHECK_ERROR("{ \"a:l1\": [ {\"c\" : 1, \"b\" : \"b\", \"a\" : \"a\", \"c\" : 1}]}", 0, LYD_VALIDATE_PRESENT,
Michal Vasko7a266772024-01-23 11:02:38 +0100423 tree, LY_EVALID, "Duplicate instance of \"c\".", "/a:l1[a='a'][b='b'][c='1'][c='1']/c", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200424
425 /* keys order, in contrast to XML, JSON accepts keys in any order even in strict mode */
Radek Iša56ca9e42020-09-08 18:42:00 +0200426 CHECK_PARSE_LYD("{ \"a:l1\": [ {\"d\" : \"d\", \"a\" : \"a\", \"c\" : 1, \"b\" : \"b\"}]}", 0, LYD_VALIDATE_PRESENT, tree);
427 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
428 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100429 list = (struct lyd_node_inner *)tree;
430 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200431 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a", 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100432 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200433 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 +0100434 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200435 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 +0100436 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200437 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "d", 1, LYS_LEAF, 1, 0, NULL, 0);
Michal Vasko7a266772024-01-23 11:02:38 +0100438 CHECK_LOG_CTX(NULL, NULL, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200439 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
440 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1,\"d\":\"d\"}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200441 lyd_free_all(tree);
442
Radek Iša56ca9e42020-09-08 18:42:00 +0200443 CHECK_PARSE_LYD("{\"a:l1\":[{\"c\":1,\"b\":\"b\",\"a\":\"a\"}]}", LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, tree);
444 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, 1, "l1",
445 1, LYS_LIST, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100446 list = (struct lyd_node_inner *)tree;
447 assert_non_null(leaf = (struct lyd_node_term *)list->child);
Radek Iša56ca9e42020-09-08 18:42:00 +0200448 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "a",
449 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100450 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200451 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "b",
452 1, LYS_LEAF, 1, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100453 assert_non_null(leaf = (struct lyd_node_term *)leaf->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200454 CHECK_LYSC_NODE(leaf->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_KEY, 1, "c",
455 1, LYS_LEAF, 1, 0, NULL, 0);
Michal Vasko7a266772024-01-23 11:02:38 +0100456 CHECK_LOG_CTX(NULL, NULL, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200457 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS,
458 "{\"a:l1\":[{\"a\":\"a\",\"b\":\"b\",\"c\":1}]}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200459 lyd_free_all(tree);
460
Michal Vasko87c5d6b2023-02-14 15:27:41 +0100461 /* skip unknown nested nodes */
462 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":3,\"counters\":{\"count1\":\"c1\",\"count2\":\"c2\"}}]}";
463 CHECK_PARSE_LYD(data, LYD_PARSE_ONLY, 0, tree);
464 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":3}]}");
465 lyd_free_all(tree);
466
Radek Krejci5536d282020-08-04 23:27:44 +0200467 data = "{\"a:cp\":{\"@\":{\"a:hint\":1}}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200468 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200469 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100470 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100471 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 +0200472 1, LYS_CONTAINER, 0, 0, NULL, 0);
473 CHECK_LYD_META(tree->meta, 1, "hint", 0, 1, INT8, "1", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200474 assert_null(tree->meta->next);
Radek Iša56ca9e42020-09-08 18:42:00 +0200475 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200476 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200477}
478
479static void
480test_container(void **state)
481{
Radek Iša56ca9e42020-09-08 18:42:00 +0200482 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200483 struct lyd_node *tree;
484 struct lyd_node_inner *cont;
485
Radek Iša56ca9e42020-09-08 18:42:00 +0200486 CHECK_PARSE_LYD("{\"a:c\":{}}", 0, LYD_VALIDATE_PRESENT, tree);
487 CHECK_LYSC_NODE(tree->schema, NULL, 0, LYS_CONFIG_W | LYS_STATUS_CURR, 1, "c",
488 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100489 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200490 assert_true(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200491
Radek Iša56ca9e42020-09-08 18:42:00 +0200492 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200493 lyd_free_all(tree);
494
Radek Krejci5536d282020-08-04 23:27:44 +0200495 data = "{\"a:cp\":{}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200496 CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200497 assert_non_null(tree);
Michal Vasko26123192020-11-09 21:02:34 +0100498 tree = tree->next;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100499 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 +0200500 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100501 cont = (struct lyd_node_inner *)tree;
Radek Krejci1798aae2020-07-14 13:26:06 +0200502 assert_false(cont->flags & LYD_DEFAULT);
Radek Krejci5536d282020-08-04 23:27:44 +0200503
Radek Iša56ca9e42020-09-08 18:42:00 +0200504 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200505 lyd_free_all(tree);
Michal Vaskoa4af6252022-05-20 10:38:54 +0200506
507 /* skip container */
508 CHECK_PARSE_LYD("{\"a:unknown\":{\"a\":\"val\",\"b\":5}}", 0, LYD_VALIDATE_PRESENT, tree);
509 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
510 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200511}
512
513static void
514test_opaq(void **state)
515{
Radek Krejci1798aae2020-07-14 13:26:06 +0200516 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200517 struct lyd_node *tree;
518
Radek Krejci1798aae2020-07-14 13:26:06 +0200519 /* invalid value, no flags */
520 data = "{\"a:foo3\":[null]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200521 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100522 "Invalid non-number-encoded uint32 value \"\".", "/a:foo3", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200523
524 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200525 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200526 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 +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
Michal Vasko5e2742d2023-05-25 10:01:42 +0200530 /* special chars */
531 data = "{\"a:foo3\":\"ab\\\"\\\\\\r\\t\"}";
532 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
533 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
534 lyd_free_all(tree);
535
Michal Vasko583b4642023-05-25 10:39:34 +0200536 /* wrong encoding */
537 data = "{\"a:foo3\":\"25\"}";
538 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
539 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo3", 0, 0, NULL, 0, "25");
540 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
541 lyd_free_all(tree);
542
543 data = "{\"a:foo4\":25}";
544 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
545 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo4", 0, 0, NULL, 0, "25");
546 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
547 lyd_free_all(tree);
548
Radek Krejci1798aae2020-07-14 13:26:06 +0200549 /* missing key, no flags */
550 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"d\":\"val_d\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200551 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100552 "List instance is missing its key \"c\".", "/a:l1[a='val_a'][b='val_b']", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200553
554 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200555 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200556 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 +0200557 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200558 lyd_free_all(tree);
559
560 /* invalid key, no flags */
561 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200562 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100563 "Invalid non-number-encoded int16 value \"val_c\".", "/a:l1[a='val_a'][b='val_b']/c", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200564
565 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200566 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200567 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 +0200568 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200569 lyd_free_all(tree);
570
571 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":{\"val\":\"val_c\"}}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200572 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200573 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 +0200574 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200575 lyd_free_all(tree);
576
577 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\"}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200578 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200579 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 +0200580 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200581 lyd_free_all(tree);
aPiecekb7b29e62021-05-11 10:02:43 +0200582
Michal Vasko6a6e3082022-05-10 10:32:38 +0200583 /* invalid metadata */
aPiecekc5f36a72021-05-18 14:12:31 +0200584 data = "{\"@a:foo\":\"str\",\"@a:foo3\":1,\"a:foo3\":2}";
Michal Vasko7a266772024-01-23 11:02:38 +0100585 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, "Unknown module of node \"@a:foo\".", "/", 0);
586 CHECK_LOG_CTX("Missing JSON data instance to be coupled with @a:foo metadata.", "/@a:foo", 1);
aPiecekc5f36a72021-05-18 14:12:31 +0200587
aPiecekb7b29e62021-05-11 10:02:43 +0200588 /* empty name */
589 PARSER_CHECK_ERROR("{\"@a:foo\":{\"\":0}}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100590 "JSON object member name cannot be a zero-length string.", "/@a:foo", 1);
Michal Vasko1a85d332021-08-27 10:35:28 +0200591
592 /* opaque data tree format print */
593 data =
594 "{\n"
595 " \"ietf-netconf-nmda:get-data\": {\n"
596 " \"data\": {\n"
597 " \"ietf-keystore:keystore\": {\n"
598 " \"asymmetric-keys\": {\n"
599 " \"asymmetric-key\": [\n"
600 " {\n"
601 " \"name\": \"genkey\",\n"
602 " \"algorithm\": \"rsa2048\"\n"
603 " }\n"
604 " ]\n"
605 " }\n"
606 " },\n"
607 " \"ietf-netconf-server:netconf-server\": {\n"
608 " \"listen\": {\n"
609 " \"idle-timeout\": 3600,\n"
610 " \"endpoint\": [\n"
611 " {\n"
612 " \"name\": \"default-ssh\",\n"
613 " \"ssh\": {\n"
614 " \"tcp-server-parameters\": {\n"
615 " \"local-address\": \"0.0.0.0\",\n"
616 " \"local-port\": 830\n"
617 " },\n"
618 " \"ssh-server-parameters\": {\n"
619 " \"server-identity\": {\n"
620 " \"host-key\": [\n"
621 " {\n"
622 " \"name\": \"default-key\",\n"
623 " \"public-key\": {\n"
624 " \"keystore-reference\": \"genkey\"\n"
625 " }\n"
626 " }\n"
627 " ]\n"
628 " },\n"
629 " \"client-authentication\": {\n"
630 " \"supported-authentication-methods\": {\n"
631 " \"publickey\": [null],\n"
632 " \"passsword\": [null],\n"
633 " \"other\": [\n"
634 " \"interactive\",\n"
635 " \"gssapi\"\n"
636 " ]\n"
637 " }\n"
638 " }\n"
639 " }\n"
640 " }\n"
641 " }\n"
642 " ]\n"
643 " }\n"
644 " }\n"
645 " }\n"
646 " }\n"
647 "}\n";
648 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
649 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
650 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200651}
652
653static void
654test_rpc(void **state)
655{
Radek Krejci1798aae2020-07-14 13:26:06 +0200656 const char *data;
Michal Vasko5df28522023-08-25 08:04:37 +0200657 char *str;
Radek Krejci1798aae2020-07-14 13:26:06 +0200658 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200659 struct lyd_node *tree, *op;
660 const struct lyd_node *node;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100661 const char *dsc = "Edit data in an NMDA datastore.\n"
662 "\n"
663 "If an error condition occurs such that an error severity\n"
664 "<rpc-error> element is generated, the server will stop\n"
665 "processing the <edit-data> operation and restore the\n"
666 "specified configuration to its complete state at\n"
667 "the start of this <edit-data> operation.";
Radek Krejci1798aae2020-07-14 13:26:06 +0200668
Michal Vaskobbdadda2022-01-06 11:40:10 +0100669 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
Radek Krejci1798aae2020-07-14 13:26:06 +0200670
Michal Vaskobbdadda2022-01-06 11:40:10 +0100671 data = "{\"ietf-netconf-nmda:edit-data\":{"
672 "\"datastore\":\"ietf-datastores:running\","
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100673 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
674 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
Michal Vasko2552ea32020-12-08 15:32:34 +0100675 "}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200676 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100677 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 +0200678 ly_in_free(in, 0);
679
680 assert_non_null(op);
Radek Krejci1798aae2020-07-14 13:26:06 +0200681
Radek Krejci2a9fc652021-01-22 17:44:34 +0100682 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100683 1, 0, 0, 1, "edit-data", LYS_RPC,
684 0, 0, 0, 0, 0, NULL, 0);
Radek Iša56ca9e42020-09-08 18:42:00 +0200685
Michal Vasko2552ea32020-12-08 15:32:34 +0100686 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100687 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100688 1, 0, 0, 1, "edit-data", LYS_RPC,
689 0, 0, 0, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200690 node = lyd_child(node)->next;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100691 CHECK_LYSC_NODE(node->schema, "Inline config content.", 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config",
692 0, LYS_ANYDATA, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200693
694 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100695 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 +0200696 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200697 node = lyd_child(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200698 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200699 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 +0200700 node = node->parent->next;
701 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200702 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 +0200703
Radek Iša56ca9e42020-09-08 18:42:00 +0200704 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200705 lyd_free_all(tree);
706
Michal Vasko5df28522023-08-25 08:04:37 +0200707 /* append to parent */
708 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:r1", NULL, 0, &op));
709 assert_int_equal(LY_SUCCESS, ly_in_new_memory("{\"l1\": \"some str\", \"l2\": \"some other str\"}", &in));
710 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, op, in, LYD_JSON, LYD_TYPE_RPC_YANG, &tree, NULL));
711 ly_in_free(in, 0);
712
713 assert_int_equal(LY_SUCCESS, lyd_print_mem(&str, op, LYD_JSON, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK));
714 lyd_free_tree(op);
715 assert_string_equal(str, "{\"a:r1\":{\"l1\":\"some str\",\"l2\":\"some other str\"}}");
716 free(str);
Radek Krejci1798aae2020-07-14 13:26:06 +0200717}
718
719static void
720test_action(void **state)
721{
Radek Krejci1798aae2020-07-14 13:26:06 +0200722 const char *data;
723 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200724 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200725
Michal Vasko2552ea32020-12-08 15:32:34 +0100726 data = "{\"a:c\":{\"act\":{\"al\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200727 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100728 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_RPC_YANG, &tree, &op));
Radek Krejci1798aae2020-07-14 13:26:06 +0200729 ly_in_free(in, 0);
730
731 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100732 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200733 1, 0, 0, 1, "act", LYS_ACTION,
734 1, 0, 0, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200735
Radek Iša56ca9e42020-09-08 18:42:00 +0200736 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200737 lyd_free_all(tree);
738
739 /* wrong namespace, element name, whatever... */
740 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200741}
742
743static void
744test_notification(void **state)
745{
Radek Krejci1798aae2020-07-14 13:26:06 +0200746 const char *data;
747 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200748 struct lyd_node *tree, *ntf;
Radek Krejci1798aae2020-07-14 13:26:06 +0200749
Michal Vasko2552ea32020-12-08 15:32:34 +0100750 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200751 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100752 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 +0200753 ly_in_free(in, 0);
754
755 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100756 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 +0200757
Michal Vasko2552ea32020-12-08 15:32:34 +0100758 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 +0200759
Radek Iša56ca9e42020-09-08 18:42:00 +0200760 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200761 lyd_free_all(tree);
762
Radek Krejci1798aae2020-07-14 13:26:06 +0200763 data = "{\"a:n2\":{}}";
764 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100765 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 +0200766 ly_in_free(in, 0);
767
768 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100769 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 +0200770
771 assert_non_null(tree);
772 assert_ptr_equal(ntf, tree);
773
Radek Iša56ca9e42020-09-08 18:42:00 +0200774 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200775 lyd_free_all(tree);
776
777 /* wrong namespace, element name, whatever... */
778 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200779}
780
781static void
782test_reply(void **state)
783{
Radek Krejci1798aae2020-07-14 13:26:06 +0200784 const char *data;
785 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100786 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200787 const struct lyd_node *node;
788
Michal Vasko2552ea32020-12-08 15:32:34 +0100789 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200790 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100791 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 +0200792 ly_in_free(in, 0);
793
Radek Krejci1798aae2020-07-14 13:26:06 +0200794 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100795 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200796 1, 0, 0, 1, "act", LYS_ACTION,
797 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200798 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100799 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 +0200800
Michal Vasko2552ea32020-12-08 15:32:34 +0100801 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 +0200802
803 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200804 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:al\":25}");
Michal Vasko2552ea32020-12-08 15:32:34 +0100805 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:c\":{\"act\":{\"al\":25}}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200806 lyd_free_all(tree);
807
808 /* wrong namespace, element name, whatever... */
809 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200810}
811
Michal Vasko820efe82023-05-12 15:47:43 +0200812static void
813test_restconf_rpc(void **state)
814{
815 const char *data;
816 struct ly_in *in;
817 struct lyd_node *tree, *envp;
818
819 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
820
821 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/ietf-netconf-nmda:edit-data", NULL, 0, &tree));
822
823 data = "{\"ietf-netconf-nmda:input\":{"
824 "\"datastore\":\"ietf-datastores:running\","
825 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
826 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
827 "}}";
828 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
829 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, tree, in, LYD_JSON, LYD_TYPE_RPC_RESTCONF, &envp, NULL));
830 ly_in_free(in, 0);
831
832 /* the same just connected to the edit-data RPC */
833 data = "{\"ietf-netconf-nmda:edit-data\":{"
834 "\"datastore\":\"ietf-datastores:running\","
835 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
836 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
837 "}}";
838 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
839 lyd_free_all(tree);
840 lyd_free_all(envp);
841}
842
843static void
844test_restconf_notification(void **state)
845{
846 const char *data;
847 struct ly_in *in;
848 struct lyd_node *tree, *ntf;
849
850 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\",\"a:c\":{\"n1\":{\"nl\":\"value\"}}}}";
851 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
852 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
853 ly_in_free(in, 0);
854
855 /* envelopes separately */
856 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\"}}";
857 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
858
859 /* notification with the parent node */
860 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
861 CHECK_LYD_STRING(lyd_parent(ntf), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
862
863 lyd_free_all(tree);
864 lyd_free_all(ntf);
Michal Vasko4a1e3e82023-09-05 08:45:01 +0200865
866 /* wrong order */
867 data = "{\"ietf-restconf:notification\":{\"a:n2\":{},\"eventTime\":\"2013-12-21T00:01:00Z\"}}";
868 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
869 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
870 ly_in_free(in, 0);
871
872 lyd_free_all(tree);
873 lyd_free_all(ntf);
Michal Vasko32d3d972023-09-05 13:37:03 +0200874
875 /* unknown notification */
876 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\",\"invalid:n2\":{}}}";
877 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
878 assert_int_equal(LY_EVALID, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
879 UTEST_LOG_CTX_CLEAN;
880 ly_in_free(in, 0);
881 lyd_free_all(tree);
Michal Vasko820efe82023-05-12 15:47:43 +0200882}
883
884static void
885test_restconf_reply(void **state)
886{
887 const char *data;
888 struct ly_in *in;
889 struct lyd_node *tree, *envp;
890
891 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:c/act", NULL, 0, &tree));
892
893 data = "{\"a:output\":{\"al\":25}}";
894 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
895 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, lyd_child(tree), in, LYD_JSON, LYD_TYPE_REPLY_RESTCONF, &envp, NULL));
896 ly_in_free(in, 0);
897
898 /* connected to the RPC with the parent */
899 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
900 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
901 lyd_free_all(tree);
902 lyd_free_all(envp);
903}
904
Michal Vaskodd03ff12023-09-11 10:30:48 +0200905static void
906test_metadata(void **state)
907{
908 const char *data;
909 struct lyd_node *tree;
910
911 /* invalid metadata value */
912 data = "{\"a:c\":{\"x\":\"xval\",\"@x\":{\"a:hint\":\"value\"}}}";
913 assert_int_equal(LY_EVALID, lyd_parse_data_mem(_UC->ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
914 assert_null(tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100915 CHECK_LOG_CTX("Invalid non-number-encoded int8 value \"value\".", "/a:c/x/@a:hint", 1);
Michal Vaskodd03ff12023-09-11 10:30:48 +0200916}
917
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100918int
919main(void)
Radek Krejci1798aae2020-07-14 13:26:06 +0200920{
921 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200922 UTEST(test_leaf, setup),
923 UTEST(test_leaflist, setup),
924 UTEST(test_anydata, setup),
Michal Vasko76096ec2022-02-24 16:06:16 +0100925 UTEST(test_anyxml, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200926 UTEST(test_list, setup),
927 UTEST(test_container, setup),
928 UTEST(test_opaq, setup),
929 UTEST(test_rpc, setup),
930 UTEST(test_action, setup),
931 UTEST(test_notification, setup),
932 UTEST(test_reply, setup),
Michal Vasko820efe82023-05-12 15:47:43 +0200933 UTEST(test_restconf_rpc, setup),
934 UTEST(test_restconf_notification, setup),
935 UTEST(test_restconf_reply, setup),
Michal Vaskodd03ff12023-09-11 10:30:48 +0200936 UTEST(test_metadata, setup),
Radek Krejci1798aae2020-07-14 13:26:06 +0200937 };
938
939 return cmocka_run_group_tests(tests, NULL, NULL);
940}