blob: 1eff7819aa9c0b613900e525311dae0c99484f73 [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);
steweg68f6eaf2024-04-05 09:24:08 +0200511
512 data = "{\"a:c\": null}";
513 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, "Expecting JSON name/object but container \"c\" is represented in input data as name/null.", NULL, 1);
514 CHECK_PARSE_LYD(data, LYD_PARSE_JSON_NULL, LYD_VALIDATE_PRESENT, tree);
515 assert_null(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200516}
517
518static void
519test_opaq(void **state)
520{
Radek Krejci1798aae2020-07-14 13:26:06 +0200521 const char *data;
Radek Krejci1798aae2020-07-14 13:26:06 +0200522 struct lyd_node *tree;
523
Radek Krejci1798aae2020-07-14 13:26:06 +0200524 /* invalid value, no flags */
525 data = "{\"a:foo3\":[null]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200526 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100527 "Invalid non-number-encoded uint32 value \"\".", "/a:foo3", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200528
529 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200530 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200531 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 +0200532 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200533 lyd_free_all(tree);
534
Michal Vasko5e2742d2023-05-25 10:01:42 +0200535 /* special chars */
536 data = "{\"a:foo3\":\"ab\\\"\\\\\\r\\t\"}";
537 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
538 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
539 lyd_free_all(tree);
540
Michal Vasko583b4642023-05-25 10:39:34 +0200541 /* wrong encoding */
542 data = "{\"a:foo3\":\"25\"}";
543 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
544 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo3", 0, 0, NULL, 0, "25");
545 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
546 lyd_free_all(tree);
547
548 data = "{\"a:foo4\":25}";
549 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
550 CHECK_LYD_NODE_OPAQ((struct lyd_node_opaq *)tree, 0, 0, LY_VALUE_JSON, "foo4", 0, 0, NULL, 0, "25");
551 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
552 lyd_free_all(tree);
553
Radek Krejci1798aae2020-07-14 13:26:06 +0200554 /* missing key, no flags */
555 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"d\":\"val_d\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200556 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100557 "List instance is missing its key \"c\".", "/a:l1[a='val_a'][b='val_b']", 1);
Radek Krejci1798aae2020-07-14 13:26:06 +0200558
559 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200560 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200561 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 +0200562 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200563 lyd_free_all(tree);
564
565 /* invalid key, no flags */
566 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200567 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100568 "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 +0200569
570 /* opaq flag */
Michal Vasko1a85d332021-08-27 10:35:28 +0200571 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200572 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 +0200573 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200574 lyd_free_all(tree);
575
576 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\",\"c\":{\"val\":\"val_c\"}}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200577 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200578 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 +0200579 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200580 lyd_free_all(tree);
581
582 data = "{\"a:l1\":[{\"a\":\"val_a\",\"b\":\"val_b\"}]}";
Michal Vasko1a85d332021-08-27 10:35:28 +0200583 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
Radek Krejci8df109d2021-04-23 12:19:08 +0200584 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 +0200585 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200586 lyd_free_all(tree);
aPiecekb7b29e62021-05-11 10:02:43 +0200587
Michal Vasko6a6e3082022-05-10 10:32:38 +0200588 /* invalid metadata */
aPiecekc5f36a72021-05-18 14:12:31 +0200589 data = "{\"@a:foo\":\"str\",\"@a:foo3\":1,\"a:foo3\":2}";
Michal Vasko7a266772024-01-23 11:02:38 +0100590 PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, "Unknown module of node \"@a:foo\".", "/", 0);
591 CHECK_LOG_CTX("Missing JSON data instance to be coupled with @a:foo metadata.", "/@a:foo", 1);
aPiecekc5f36a72021-05-18 14:12:31 +0200592
aPiecekb7b29e62021-05-11 10:02:43 +0200593 /* empty name */
594 PARSER_CHECK_ERROR("{\"@a:foo\":{\"\":0}}", 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID,
Michal Vasko7a266772024-01-23 11:02:38 +0100595 "JSON object member name cannot be a zero-length string.", "/@a:foo", 1);
Michal Vasko1a85d332021-08-27 10:35:28 +0200596
597 /* opaque data tree format print */
598 data =
599 "{\n"
600 " \"ietf-netconf-nmda:get-data\": {\n"
601 " \"data\": {\n"
602 " \"ietf-keystore:keystore\": {\n"
603 " \"asymmetric-keys\": {\n"
604 " \"asymmetric-key\": [\n"
605 " {\n"
606 " \"name\": \"genkey\",\n"
607 " \"algorithm\": \"rsa2048\"\n"
608 " }\n"
609 " ]\n"
610 " }\n"
611 " },\n"
612 " \"ietf-netconf-server:netconf-server\": {\n"
613 " \"listen\": {\n"
614 " \"idle-timeout\": 3600,\n"
615 " \"endpoint\": [\n"
616 " {\n"
617 " \"name\": \"default-ssh\",\n"
618 " \"ssh\": {\n"
619 " \"tcp-server-parameters\": {\n"
620 " \"local-address\": \"0.0.0.0\",\n"
621 " \"local-port\": 830\n"
622 " },\n"
623 " \"ssh-server-parameters\": {\n"
624 " \"server-identity\": {\n"
625 " \"host-key\": [\n"
626 " {\n"
627 " \"name\": \"default-key\",\n"
628 " \"public-key\": {\n"
629 " \"keystore-reference\": \"genkey\"\n"
630 " }\n"
631 " }\n"
632 " ]\n"
633 " },\n"
634 " \"client-authentication\": {\n"
635 " \"supported-authentication-methods\": {\n"
636 " \"publickey\": [null],\n"
637 " \"passsword\": [null],\n"
638 " \"other\": [\n"
639 " \"interactive\",\n"
640 " \"gssapi\"\n"
641 " ]\n"
642 " }\n"
643 " }\n"
644 " }\n"
645 " }\n"
646 " }\n"
647 " ]\n"
648 " }\n"
649 " }\n"
650 " }\n"
651 " }\n"
652 "}\n";
653 CHECK_PARSE_LYD(data, LYD_PARSE_OPAQ | LYD_PARSE_ONLY, 0, tree);
654 CHECK_LYD_STRING(tree, LYD_PRINT_WITHSIBLINGS, data);
655 lyd_free_all(tree);
Radek Krejci1798aae2020-07-14 13:26:06 +0200656}
657
658static void
659test_rpc(void **state)
660{
Radek Krejci1798aae2020-07-14 13:26:06 +0200661 const char *data;
Michal Vasko5df28522023-08-25 08:04:37 +0200662 char *str;
Radek Krejci1798aae2020-07-14 13:26:06 +0200663 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200664 struct lyd_node *tree, *op;
665 const struct lyd_node *node;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100666 const char *dsc = "Edit data in an NMDA datastore.\n"
667 "\n"
668 "If an error condition occurs such that an error severity\n"
669 "<rpc-error> element is generated, the server will stop\n"
670 "processing the <edit-data> operation and restore the\n"
671 "specified configuration to its complete state at\n"
672 "the start of this <edit-data> operation.";
Radek Krejci1798aae2020-07-14 13:26:06 +0200673
Michal Vaskobbdadda2022-01-06 11:40:10 +0100674 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
Radek Krejci1798aae2020-07-14 13:26:06 +0200675
Michal Vaskobbdadda2022-01-06 11:40:10 +0100676 data = "{\"ietf-netconf-nmda:edit-data\":{"
677 "\"datastore\":\"ietf-datastores:running\","
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100678 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
679 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
Michal Vasko2552ea32020-12-08 15:32:34 +0100680 "}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200681 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100682 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 +0200683 ly_in_free(in, 0);
684
685 assert_non_null(op);
Radek Krejci1798aae2020-07-14 13:26:06 +0200686
Radek Krejci2a9fc652021-01-22 17:44:34 +0100687 CHECK_LYSC_ACTION((struct lysc_node_action *)op->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 Iša56ca9e42020-09-08 18:42:00 +0200690
Michal Vasko2552ea32020-12-08 15:32:34 +0100691 node = tree;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100692 CHECK_LYSC_ACTION((struct lysc_node_action *)node->schema, dsc, 0, LYS_STATUS_CURR,
Michal Vaskobbdadda2022-01-06 11:40:10 +0100693 1, 0, 0, 1, "edit-data", LYS_RPC,
694 0, 0, 0, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200695 node = lyd_child(node)->next;
Michal Vaskobbdadda2022-01-06 11:40:10 +0100696 CHECK_LYSC_NODE(node->schema, "Inline config content.", 0, LYS_STATUS_CURR | LYS_IS_INPUT, 1, "config",
697 0, LYS_ANYDATA, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200698
699 node = ((struct lyd_node_any *)node)->value.tree;
Michal Vaskoe16c7b72021-02-26 10:39:06 +0100700 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 +0200701 1, LYS_CONTAINER, 0, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200702 node = lyd_child(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200703 /* z has no value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200704 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 +0200705 node = node->parent->next;
706 /* l1 key c has invalid value so it is at the end */
Radek Krejci8df109d2021-04-23 12:19:08 +0200707 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 +0200708
Radek Iša56ca9e42020-09-08 18:42:00 +0200709 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200710 lyd_free_all(tree);
711
Michal Vasko5df28522023-08-25 08:04:37 +0200712 /* append to parent */
713 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:r1", NULL, 0, &op));
714 assert_int_equal(LY_SUCCESS, ly_in_new_memory("{\"l1\": \"some str\", \"l2\": \"some other str\"}", &in));
715 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, op, in, LYD_JSON, LYD_TYPE_RPC_YANG, &tree, NULL));
716 ly_in_free(in, 0);
717
718 assert_int_equal(LY_SUCCESS, lyd_print_mem(&str, op, LYD_JSON, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK));
719 lyd_free_tree(op);
720 assert_string_equal(str, "{\"a:r1\":{\"l1\":\"some str\",\"l2\":\"some other str\"}}");
721 free(str);
Radek Krejci1798aae2020-07-14 13:26:06 +0200722}
723
724static void
725test_action(void **state)
726{
Radek Krejci1798aae2020-07-14 13:26:06 +0200727 const char *data;
728 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200729 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200730
Michal Vasko2552ea32020-12-08 15:32:34 +0100731 data = "{\"a:c\":{\"act\":{\"al\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200732 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100733 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 +0200734 ly_in_free(in, 0);
735
736 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100737 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200738 1, 0, 0, 1, "act", LYS_ACTION,
739 1, 0, 0, 1, 0, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200740
Radek Iša56ca9e42020-09-08 18:42:00 +0200741 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200742 lyd_free_all(tree);
743
744 /* wrong namespace, element name, whatever... */
745 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200746}
747
748static void
749test_notification(void **state)
750{
Radek Krejci1798aae2020-07-14 13:26:06 +0200751 const char *data;
752 struct ly_in *in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200753 struct lyd_node *tree, *ntf;
Radek Krejci1798aae2020-07-14 13:26:06 +0200754
Michal Vasko2552ea32020-12-08 15:32:34 +0100755 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200756 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100757 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 +0200758 ly_in_free(in, 0);
759
760 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100761 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 +0200762
Michal Vasko2552ea32020-12-08 15:32:34 +0100763 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 +0200764
Radek Iša56ca9e42020-09-08 18:42:00 +0200765 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200766 lyd_free_all(tree);
767
Radek Krejci1798aae2020-07-14 13:26:06 +0200768 data = "{\"a:n2\":{}}";
769 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100770 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 +0200771 ly_in_free(in, 0);
772
773 assert_non_null(ntf);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100774 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 +0200775
776 assert_non_null(tree);
777 assert_ptr_equal(ntf, tree);
778
Radek Iša56ca9e42020-09-08 18:42:00 +0200779 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
Radek Krejci1798aae2020-07-14 13:26:06 +0200780 lyd_free_all(tree);
781
782 /* wrong namespace, element name, whatever... */
783 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200784}
785
786static void
787test_reply(void **state)
788{
Radek Krejci1798aae2020-07-14 13:26:06 +0200789 const char *data;
790 struct ly_in *in;
Michal Vasko2552ea32020-12-08 15:32:34 +0100791 struct lyd_node *tree, *op;
Radek Krejci1798aae2020-07-14 13:26:06 +0200792 const struct lyd_node *node;
793
Michal Vasko2552ea32020-12-08 15:32:34 +0100794 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
Radek Krejci1798aae2020-07-14 13:26:06 +0200795 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
Michal Vasko1e4c68e2021-02-18 15:03:01 +0100796 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 +0200797 ly_in_free(in, 0);
798
Radek Krejci1798aae2020-07-14 13:26:06 +0200799 assert_non_null(op);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100800 CHECK_LYSC_ACTION((struct lysc_node_action *)op->schema, NULL, 0, LYS_STATUS_CURR,
Radek Iša56ca9e42020-09-08 18:42:00 +0200801 1, 0, 0, 1, "act", LYS_ACTION,
802 1, 0, 0, 1, 0, NULL, 0);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200803 node = lyd_child(op);
Michal Vaskod1e53b92021-01-28 13:11:06 +0100804 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 +0200805
Michal Vasko2552ea32020-12-08 15:32:34 +0100806 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 +0200807
808 /* TODO print only rpc-reply node and then output subtree */
Radek Iša56ca9e42020-09-08 18:42:00 +0200809 CHECK_LYD_STRING(lyd_child(op), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:al\":25}");
Michal Vasko2552ea32020-12-08 15:32:34 +0100810 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{\"a:c\":{\"act\":{\"al\":25}}}");
Radek Krejci1798aae2020-07-14 13:26:06 +0200811 lyd_free_all(tree);
812
813 /* wrong namespace, element name, whatever... */
814 /* TODO */
Radek Krejci1798aae2020-07-14 13:26:06 +0200815}
816
Michal Vasko820efe82023-05-12 15:47:43 +0200817static void
818test_restconf_rpc(void **state)
819{
820 const char *data;
821 struct ly_in *in;
822 struct lyd_node *tree, *envp;
823
824 assert_non_null((ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-nmda", "2019-01-07", NULL)));
825
826 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/ietf-netconf-nmda:edit-data", NULL, 0, &tree));
827
828 data = "{\"ietf-netconf-nmda:input\":{"
829 "\"datastore\":\"ietf-datastores:running\","
830 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
831 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
832 "}}";
833 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
834 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, tree, in, LYD_JSON, LYD_TYPE_RPC_RESTCONF, &envp, NULL));
835 ly_in_free(in, 0);
836
837 /* the same just connected to the edit-data RPC */
838 data = "{\"ietf-netconf-nmda:edit-data\":{"
839 "\"datastore\":\"ietf-datastores:running\","
840 "\"config\":{\"a:cp\":{\"z\":[null],\"@z\":{\"ietf-netconf:operation\":\"replace\"}},"
841 "\"a:l1\":[{\"@\":{\"ietf-netconf:operation\":\"replace\"},\"a\":\"val_a\",\"b\":\"val_b\",\"c\":\"val_c\"}]}"
842 "}}";
843 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
844 lyd_free_all(tree);
845 lyd_free_all(envp);
846}
847
848static void
849test_restconf_notification(void **state)
850{
851 const char *data;
852 struct ly_in *in;
853 struct lyd_node *tree, *ntf;
854
855 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\",\"a:c\":{\"n1\":{\"nl\":\"value\"}}}}";
856 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
857 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
858 ly_in_free(in, 0);
859
860 /* envelopes separately */
861 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\"}}";
862 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
863
864 /* notification with the parent node */
865 data = "{\"a:c\":{\"n1\":{\"nl\":\"value\"}}}";
866 CHECK_LYD_STRING(lyd_parent(ntf), LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
867
868 lyd_free_all(tree);
869 lyd_free_all(ntf);
Michal Vasko4a1e3e82023-09-05 08:45:01 +0200870
871 /* wrong order */
872 data = "{\"ietf-restconf:notification\":{\"a:n2\":{},\"eventTime\":\"2013-12-21T00:01:00Z\"}}";
873 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
874 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
875 ly_in_free(in, 0);
876
877 lyd_free_all(tree);
878 lyd_free_all(ntf);
Michal Vasko32d3d972023-09-05 13:37:03 +0200879
880 /* unknown notification */
881 data = "{\"ietf-restconf:notification\":{\"eventTime\":\"2013-12-21T00:01:00Z\",\"invalid:n2\":{}}}";
882 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
883 assert_int_equal(LY_EVALID, lyd_parse_op(UTEST_LYCTX, NULL, in, LYD_JSON, LYD_TYPE_NOTIF_RESTCONF, &tree, &ntf));
884 UTEST_LOG_CTX_CLEAN;
885 ly_in_free(in, 0);
886 lyd_free_all(tree);
Michal Vasko820efe82023-05-12 15:47:43 +0200887}
888
889static void
890test_restconf_reply(void **state)
891{
892 const char *data;
893 struct ly_in *in;
894 struct lyd_node *tree, *envp;
895
896 assert_int_equal(LY_SUCCESS, lyd_new_path(NULL, UTEST_LYCTX, "/a:c/act", NULL, 0, &tree));
897
898 data = "{\"a:output\":{\"al\":25}}";
899 assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in));
900 assert_int_equal(LY_SUCCESS, lyd_parse_op(UTEST_LYCTX, lyd_child(tree), in, LYD_JSON, LYD_TYPE_REPLY_RESTCONF, &envp, NULL));
901 ly_in_free(in, 0);
902
903 /* connected to the RPC with the parent */
904 data = "{\"a:c\":{\"act\":{\"al\":25}}}";
905 CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, data);
906 lyd_free_all(tree);
907 lyd_free_all(envp);
908}
909
Michal Vaskodd03ff12023-09-11 10:30:48 +0200910static void
911test_metadata(void **state)
912{
913 const char *data;
914 struct lyd_node *tree;
915
916 /* invalid metadata value */
917 data = "{\"a:c\":{\"x\":\"xval\",\"@x\":{\"a:hint\":\"value\"}}}";
918 assert_int_equal(LY_EVALID, lyd_parse_data_mem(_UC->ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
919 assert_null(tree);
Michal Vasko7a266772024-01-23 11:02:38 +0100920 CHECK_LOG_CTX("Invalid non-number-encoded int8 value \"value\".", "/a:c/x/@a:hint", 1);
Michal Vaskodd03ff12023-09-11 10:30:48 +0200921}
922
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100923int
924main(void)
Radek Krejci1798aae2020-07-14 13:26:06 +0200925{
926 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200927 UTEST(test_leaf, setup),
928 UTEST(test_leaflist, setup),
929 UTEST(test_anydata, setup),
Michal Vasko76096ec2022-02-24 16:06:16 +0100930 UTEST(test_anyxml, setup),
Radek Iša56ca9e42020-09-08 18:42:00 +0200931 UTEST(test_list, setup),
932 UTEST(test_container, setup),
933 UTEST(test_opaq, setup),
934 UTEST(test_rpc, setup),
935 UTEST(test_action, setup),
936 UTEST(test_notification, setup),
937 UTEST(test_reply, setup),
Michal Vasko820efe82023-05-12 15:47:43 +0200938 UTEST(test_restconf_rpc, setup),
939 UTEST(test_restconf_notification, setup),
940 UTEST(test_restconf_reply, setup),
Michal Vaskodd03ff12023-09-11 10:30:48 +0200941 UTEST(test_metadata, setup),
Radek Krejci1798aae2020-07-14 13:26:06 +0200942 };
943
944 return cmocka_run_group_tests(tests, NULL, NULL);
945}