blob: 5cee9032ec621b4e480a7f83c383f277fec699af [file] [log] [blame]
Michal Vasko004d3152020-06-11 19:59:22 +02001/**
2 * @file test_new.c
3 * @author: Michal Vasko <mvasko@cesnet.cz>
4 * @brief unit tests for functions for creating data
5 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
Radek Krejcib4ac5a92020-11-23 17:54:33 +010015#include "utests.h"
Michal Vasko004d3152020-06-11 19:59:22 +020016
Radek Iša56ca9e42020-09-08 18:42:00 +020017#include "libyang.h"
Michal Vasko004d3152020-06-11 19:59:22 +020018
Radek Iša56ca9e42020-09-08 18:42:00 +020019/* common module for the tests */
20const char *schema_a = "module a {\n"
21 " namespace urn:tests:a;\n"
22 " prefix a;yang-version 1.1;\n"
23 " list l1 {\n"
24 " key \"a b\";\n"
25 " leaf a {\n"
26 " type string;\n"
27 " }\n"
28 " leaf b {\n"
29 " type string;\n"
30 " }\n"
31 " leaf c {\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020032 " type string;\n"
33 " }\n"
Radek Krejci95ccd1b2021-03-12 14:57:22 +010034 " }\n"
Michal Vasko106f0862021-11-02 11:49:27 +010035 " list l11 {\n"
36 " key \"a\";\n"
37 " leaf a {\n"
38 " type uint32;\n"
39 " }\n"
40 " leaf b {\n"
41 " type uint32;\n"
42 " }\n"
43 " }\n"
Radek Krejci95ccd1b2021-03-12 14:57:22 +010044 " leaf foo {\n"
45 " type uint16;\n"
46 " }\n"
47 " leaf-list ll {\n"
48 " type string;\n"
49 " }\n"
50 " container c {\n"
51 " leaf-list x {\n"
Michal Vaskoe78faec2021-04-08 17:24:43 +020052 " type string;\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020053 " }\n"
Radek Krejci95ccd1b2021-03-12 14:57:22 +010054 " }\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020055 " anydata any {\n"
56 " config false;\n"
Radek Krejci95ccd1b2021-03-12 14:57:22 +010057 " }\n"
Michal Vasko54bf9e22023-04-27 08:20:30 +020058 " anyxml anyx;\n"
Michal Vaskoe78faec2021-04-08 17:24:43 +020059 " leaf-list ll2 {\n"
60 " config false;\n"
61 " type string;\n"
62 " }\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020063 " list l2 {\n"
64 " config false;\n"
65 " container c {\n"
66 " leaf x {\n"
67 " type string;\n"
68 " }\n"
69 " }\n"
70 " }\n"
Michal Vaskoe78faec2021-04-08 17:24:43 +020071 " container c2 {\n"
72 " config false;\n"
73 " list l3 {\n"
74 " leaf x {\n"
75 " type string;\n"
76 " }\n"
77 " leaf y {\n"
78 " type string;\n"
79 " }\n"
80 " }\n"
81 " }\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020082 " rpc oper {\n"
83 " input {\n"
84 " leaf param {\n"
85 " type string;\n"
86 " }\n"
87 " }\n"
88 " output {\n"
89 " leaf param {\n"
90 " type int8;\n"
91 " }\n"
92 " }\n"
93 " }\n"
94 "}\n";
Michal Vasko004d3152020-06-11 19:59:22 +020095
96static void
97test_top_level(void **state)
98{
Michal Vasko4de7d072021-07-09 09:13:18 +020099 struct lys_module *mod;
Radek Krejci41ac9942020-11-02 14:47:56 +0100100 struct lyd_node *node, *rpc;
Michal Vasko004d3152020-06-11 19:59:22 +0200101
Radek Iša56ca9e42020-09-08 18:42:00 +0200102 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
Michal Vasko004d3152020-06-11 19:59:22 +0200103
104 /* list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100105 assert_int_equal(lyd_new_list(NULL, mod, "l1", 0, &node, "val_a", "val_b"), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200106 lyd_free_tree(node);
107
Radek Krejci41ac9942020-11-02 14:47:56 +0100108 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[]", 0, &node), LY_EVALID);
Michal Vasko959f8d82022-06-16 07:51:50 +0200109 CHECK_LOG_CTX("Unexpected XPath token \"]\" (\"]\").", "Schema location \"/a:l1\".");
Michal Vasko004d3152020-06-11 19:59:22 +0200110
Radek Krejci41ac9942020-11-02 14:47:56 +0100111 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[key1='a'][key2='b']", 0, &node), LY_ENOTFOUND);
Michal Vasko959f8d82022-06-16 07:51:50 +0200112 CHECK_LOG_CTX("Not found node \"key1\" in path.", "Schema location \"/a:l1\".");
Michal Vasko004d3152020-06-11 19:59:22 +0200113
Radek Krejci41ac9942020-11-02 14:47:56 +0100114 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID);
Michal Vasko959f8d82022-06-16 07:51:50 +0200115 CHECK_LOG_CTX("Key expected instead of leaf \"c\" in path.", "Schema location \"/a:l1\".");
Michal Vasko004d3152020-06-11 19:59:22 +0200116
Radek Krejci41ac9942020-11-02 14:47:56 +0100117 assert_int_equal(lyd_new_list2(NULL, mod, "c", "[a='a'][b='b']", 0, &node), LY_ENOTFOUND);
Radek Iša56ca9e42020-09-08 18:42:00 +0200118 CHECK_LOG_CTX("List node \"c\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200119
Radek Krejci41ac9942020-11-02 14:47:56 +0100120 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200121 lyd_free_tree(node);
122
Radek Krejci41ac9942020-11-02 14:47:56 +0100123 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a=''][b='']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200124 lyd_free_tree(node);
125
Radek Krejci41ac9942020-11-02 14:47:56 +0100126 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a:a='a'][a:b='b']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200127 lyd_free_tree(node);
128
Radek Krejci41ac9942020-11-02 14:47:56 +0100129 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a= 'a']\n[b =\t'b']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200130 lyd_free_tree(node);
131
Michal Vasko2c1e3272023-10-17 14:08:35 +0200132 const char *key_vals[] = {"a", "b"};
Michal Vaskof7931562023-10-17 14:18:03 +0200133
Michal Vasko2c1e3272023-10-17 14:08:35 +0200134 assert_int_equal(lyd_new_list3(NULL, mod, "l1", key_vals, NULL, 0, &node), LY_SUCCESS);
135 lyd_free_tree(node);
136
137 uint32_t val_lens[] = {1, 1};
Michal Vaskof7931562023-10-17 14:18:03 +0200138
Michal Vasko2c1e3272023-10-17 14:08:35 +0200139 assert_int_equal(lyd_new_list3_bin(NULL, mod, "l1", (const void **)key_vals, val_lens, 0, &node), LY_SUCCESS);
140 lyd_free_tree(node);
141
Michal Vasko004d3152020-06-11 19:59:22 +0200142 /* leaf */
Radek Krejci41ac9942020-11-02 14:47:56 +0100143 assert_int_equal(lyd_new_term(NULL, mod, "foo", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID);
Michal Vasko959f8d82022-06-16 07:51:50 +0200144 CHECK_LOG_CTX("Invalid type uint16 value \"[a='a'][b='b'][c='c']\".", "Schema location \"/a:foo\".");
Michal Vasko004d3152020-06-11 19:59:22 +0200145
Radek Krejci41ac9942020-11-02 14:47:56 +0100146 assert_int_equal(lyd_new_term(NULL, mod, "c", "value", 0, &node), LY_ENOTFOUND);
Radek Iša56ca9e42020-09-08 18:42:00 +0200147 CHECK_LOG_CTX("Term node \"c\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200148
Radek Krejci41ac9942020-11-02 14:47:56 +0100149 assert_int_equal(lyd_new_term(NULL, mod, "foo", "256", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200150 lyd_free_tree(node);
151
152 /* leaf-list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100153 assert_int_equal(lyd_new_term(NULL, mod, "ll", "ahoy", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200154 lyd_free_tree(node);
155
156 /* container */
Radek Krejci41ac9942020-11-02 14:47:56 +0100157 assert_int_equal(lyd_new_inner(NULL, mod, "c", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200158 lyd_free_tree(node);
159
Radek Krejci41ac9942020-11-02 14:47:56 +0100160 assert_int_equal(lyd_new_inner(NULL, mod, "l1", 0, &node), LY_ENOTFOUND);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200161 CHECK_LOG_CTX("Inner node (container, notif, RPC, or action) \"l1\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200162
Radek Krejci41ac9942020-11-02 14:47:56 +0100163 assert_int_equal(lyd_new_inner(NULL, mod, "l2", 0, &node), LY_ENOTFOUND);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200164 CHECK_LOG_CTX("Inner node (container, notif, RPC, or action) \"l2\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200165
166 /* anydata */
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100167 assert_int_equal(lyd_new_any(NULL, mod, "any", "{\"node\":\"val\"}", 0, LYD_ANYDATA_STRING, 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200168 lyd_free_tree(node);
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100169 assert_int_equal(lyd_new_any(NULL, mod, "any", "<node>val</node>", 0, LYD_ANYDATA_STRING, 0, &node), LY_SUCCESS);
170 lyd_free_tree(node);
Michal Vasko004d3152020-06-11 19:59:22 +0200171
172 /* key-less list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100173 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "[a='a'][b='b']", 0, &node), LY_EVALID);
Michal Vasko959f8d82022-06-16 07:51:50 +0200174 CHECK_LOG_CTX("List predicate defined for keyless list \"l2\" in path.", "Schema location \"/a:l2\".");
Michal Vasko004d3152020-06-11 19:59:22 +0200175
Radek Krejci41ac9942020-11-02 14:47:56 +0100176 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200177 lyd_free_tree(node);
178
Radek Krejci41ac9942020-11-02 14:47:56 +0100179 assert_int_equal(lyd_new_list2(NULL, mod, "l2", NULL, 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200180 lyd_free_tree(node);
181
Radek Krejci41ac9942020-11-02 14:47:56 +0100182 assert_int_equal(lyd_new_list(NULL, mod, "l2", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200183 lyd_free_tree(node);
184
Radek Krejci41ac9942020-11-02 14:47:56 +0100185 /* RPC */
186 assert_int_equal(lyd_new_inner(NULL, mod, "oper", 0, &rpc), LY_SUCCESS);
187 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 0, &node), LY_SUCCESS);
188 assert_int_equal(LY_TYPE_STRING, ((struct lysc_node_leaf *)node->schema)->type->basetype);
189 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 1, &node), LY_SUCCESS);
190 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf *)node->schema)->type->basetype);
191 lyd_free_tree(rpc);
Michal Vasko004d3152020-06-11 19:59:22 +0200192}
193
Michal Vasko00cbf532020-06-15 13:58:47 +0200194static void
195test_opaq(void **state)
196{
Michal Vasko00cbf532020-06-15 13:58:47 +0200197 struct lyd_node *root, *node;
198 struct lyd_node_opaq *opq;
199
Radek Iša56ca9e42020-09-08 18:42:00 +0200200 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL);
201
Michal Vasko0ab974d2021-02-24 13:18:26 +0100202 assert_int_equal(lyd_new_opaq(NULL, UTEST_LYCTX, "node1", NULL, NULL, "my-module", &root), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200203 assert_null(root->schema);
204 opq = (struct lyd_node_opaq *)root;
Michal Vaskoad92b672020-11-12 13:11:31 +0100205 assert_string_equal(opq->name.name, "node1");
206 assert_string_equal(opq->name.module_name, "my-module");
Michal Vasko501af032020-11-11 20:27:44 +0100207 assert_string_equal(opq->value, "");
Michal Vasko00cbf532020-06-15 13:58:47 +0200208
Michal Vasko0ab974d2021-02-24 13:18:26 +0100209 assert_int_equal(lyd_new_opaq(root, NULL, "node2", "value", NULL, "my-module2", &node), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200210 assert_null(node->schema);
211 opq = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100212 assert_string_equal(opq->name.name, "node2");
213 assert_string_equal(opq->name.module_name, "my-module2");
Michal Vasko501af032020-11-11 20:27:44 +0100214 assert_string_equal(opq->value, "value");
Michal Vasko00cbf532020-06-15 13:58:47 +0200215 assert_ptr_equal(opq->parent, root);
216
217 lyd_free_tree(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200218}
219
220static void
221test_path(void **state)
222{
Michal Vasko00cbf532020-06-15 13:58:47 +0200223 LY_ERR ret;
224 struct lyd_node *root, *node, *parent;
Michal Vasko4de7d072021-07-09 09:13:18 +0200225 struct lys_module *mod;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200226 char *str;
Michal Vasko00cbf532020-06-15 13:58:47 +0200227
Michal Vaskoe78faec2021-04-08 17:24:43 +0200228 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
Radek Iša56ca9e42020-09-08 18:42:00 +0200229
Michal Vasko00cbf532020-06-15 13:58:47 +0200230 /* create 2 nodes */
Radek Krejci09c77442021-04-26 11:10:34 +0200231 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c/x[.='val']", "vvv", 0, 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200232 assert_int_equal(ret, LY_SUCCESS);
233 assert_non_null(root);
234 assert_string_equal(root->schema->name, "c");
235 assert_non_null(node);
236 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200237 assert_string_equal("val", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200238
239 /* append another */
Radek Krejci09c77442021-04-26 11:10:34 +0200240 ret = lyd_new_path2(root, NULL, "/a:c/x", "val2", 0, 0, 0, &parent, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200241 assert_int_equal(ret, LY_SUCCESS);
242 assert_ptr_equal(parent, node);
243 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200244 assert_string_equal("val2", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200245
246 /* and a last one */
Radek Krejci09c77442021-04-26 11:10:34 +0200247 ret = lyd_new_path2(root, NULL, "x", "val3", 0, 0, 0, &parent, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200248 assert_int_equal(ret, LY_SUCCESS);
249 assert_ptr_equal(parent, node);
250 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200251 assert_string_equal("val3", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200252
253 lyd_free_tree(root);
254
255 /* try LYD_NEWOPT_OPAQ */
Radek Krejci09c77442021-04-26 11:10:34 +0200256 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200257 assert_int_equal(ret, LY_EINVAL);
Michal Vasko959f8d82022-06-16 07:51:50 +0200258 CHECK_LOG_CTX("Predicate missing for list \"l1\" in path \"/a:l1\".", "Schema location \"/a:l1\".");
Michal Vasko00cbf532020-06-15 13:58:47 +0200259
Radek Krejci09c77442021-04-26 11:10:34 +0200260 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, 0, LYD_NEW_PATH_OPAQ, NULL, &root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200261 assert_int_equal(ret, LY_SUCCESS);
262 assert_non_null(root);
263 assert_null(root->schema);
264
265 lyd_free_tree(root);
266
Radek Krejci09c77442021-04-26 11:10:34 +0200267 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200268 assert_int_equal(ret, LY_EVALID);
Michal Vasko959f8d82022-06-16 07:51:50 +0200269 CHECK_LOG_CTX("Invalid type uint16 empty value.", "Schema location \"/a:foo\".");
Michal Vasko00cbf532020-06-15 13:58:47 +0200270
Radek Krejci09c77442021-04-26 11:10:34 +0200271 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, 0, LYD_NEW_PATH_OPAQ, NULL, &root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200272 assert_int_equal(ret, LY_SUCCESS);
273 assert_non_null(root);
274 assert_null(root->schema);
275
276 lyd_free_tree(root);
277
Michal Vasko106f0862021-11-02 11:49:27 +0100278 ret = lyd_new_path(NULL, UTEST_LYCTX, "/a:l11", NULL, LYD_NEW_PATH_OPAQ, &root);
279 assert_int_equal(ret, LY_SUCCESS);
280 assert_non_null(root);
281 assert_null(root->schema);
282
283 ret = lyd_new_path(root, NULL, "a", NULL, LYD_NEW_PATH_OPAQ, NULL);
284 assert_int_equal(ret, LY_SUCCESS);
285 assert_non_null(lyd_child(root));
286 assert_null(lyd_child(root)->schema);
287
288 ret = lyd_new_path(root, NULL, "b", NULL, LYD_NEW_PATH_OPAQ, NULL);
289 assert_int_equal(ret, LY_SUCCESS);
290 assert_non_null(lyd_child(root)->next);
291 assert_null(lyd_child(root)->next->schema);
292
293 lyd_free_tree(root);
294
Michal Vaskoe78faec2021-04-08 17:24:43 +0200295 /* key-less list */
Radek Krejci09c77442021-04-26 11:10:34 +0200296 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c2/l3/x", "val1", 0, 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200297 assert_int_equal(ret, LY_SUCCESS);
298 assert_non_null(root);
299 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200300 assert_string_equal("val1", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200301
Radek Krejci09c77442021-04-26 11:10:34 +0200302 ret = lyd_new_path2(root, NULL, "/a:c2/l3[1]", NULL, 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200303 assert_int_equal(ret, LY_EEXIST);
Michal Vasko62af3692023-02-09 14:00:09 +0100304 CHECK_LOG_CTX("Path \"/a:c2/l3[1]\" already exists.", "Data location \"/a:c2/l3[1]\".");
Michal Vasko00cbf532020-06-15 13:58:47 +0200305
Radek Krejci09c77442021-04-26 11:10:34 +0200306 ret = lyd_new_path2(root, NULL, "/a:c2/l3[2]/x", "val2", 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200307 assert_int_equal(ret, LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200308
Radek Krejci09c77442021-04-26 11:10:34 +0200309 ret = lyd_new_path2(root, NULL, "/a:c2/l3/x", "val3", 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200310 assert_int_equal(ret, LY_SUCCESS);
311 assert_non_null(node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200312
Radek Krejci09c77442021-04-26 11:10:34 +0200313 ret = lyd_new_path2(root, NULL, "/a:c2/l3[4]/x", "empty", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200314 assert_int_equal(ret, LY_SUCCESS);
315 assert_non_null(node);
316
Radek Krejci09c77442021-04-26 11:10:34 +0200317 ret = lyd_new_path2(root, NULL, "/a:c2/l3[4]/x", "val4", 0, 0, LYD_NEW_PATH_UPDATE, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200318 assert_int_equal(ret, LY_SUCCESS);
319 assert_non_null(node);
320
Radek Krejci09c77442021-04-26 11:10:34 +0200321 ret = lyd_new_path2(root, NULL, "/a:c2/l3[5]/x", "val5", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200322 assert_int_equal(ret, LY_SUCCESS);
323 assert_non_null(node);
324
Radek Krejci09c77442021-04-26 11:10:34 +0200325 ret = lyd_new_path2(root, NULL, "/a:c2/l3[6]/x", "val6", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200326 assert_int_equal(ret, LY_SUCCESS);
327 assert_non_null(node);
328
329 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
330 assert_string_equal(str,
331 "<c2 xmlns=\"urn:tests:a\">\n"
332 " <l3>\n"
333 " <x>val1</x>\n"
334 " </l3>\n"
335 " <l3>\n"
336 " <x>val2</x>\n"
337 " </l3>\n"
338 " <l3>\n"
339 " <x>val3</x>\n"
340 " </l3>\n"
341 " <l3>\n"
342 " <x>val4</x>\n"
343 " </l3>\n"
344 " <l3>\n"
345 " <x>val5</x>\n"
346 " </l3>\n"
347 " <l3>\n"
348 " <x>val6</x>\n"
349 " </l3>\n"
350 "</c2>\n");
351 free(str);
352 lyd_free_siblings(root);
353
354 /* state leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +0200355 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:ll2", "val_first", 0, 0, 0, &root, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200356 assert_int_equal(ret, LY_SUCCESS);
357 assert_non_null(root);
358 assert_string_equal(node->schema->name, "ll2");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200359 assert_string_equal("val_first", lyd_get_value(node));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200360
Radek Krejci09c77442021-04-26 11:10:34 +0200361 ret = lyd_new_path2(root, NULL, "/a:ll2[1]", "", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200362 assert_int_equal(ret, LY_EEXIST);
Michal Vasko62af3692023-02-09 14:00:09 +0100363 CHECK_LOG_CTX("Path \"/a:ll2[1]\" already exists.", "Data location \"/a:ll2[1]\".");
Michal Vaskoe78faec2021-04-08 17:24:43 +0200364
Radek Krejci09c77442021-04-26 11:10:34 +0200365 ret = lyd_new_path2(root, NULL, "/a:ll2[2]", "val2", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200366 assert_int_equal(ret, LY_SUCCESS);
367
Radek Krejci09c77442021-04-26 11:10:34 +0200368 ret = lyd_new_path2(root, NULL, "/a:ll2[1]", "val", 0, 0, LYD_NEW_PATH_UPDATE, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200369 assert_int_equal(ret, LY_SUCCESS);
370 assert_non_null(node);
371
Radek Krejci09c77442021-04-26 11:10:34 +0200372 ret = lyd_new_path2(root, UTEST_LYCTX, "/a:ll2", "val3", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200373 assert_int_equal(ret, LY_SUCCESS);
374 assert_non_null(node);
375
Radek Krejci09c77442021-04-26 11:10:34 +0200376 ret = lyd_new_path2(root, NULL, "/a:ll2[3][.='val3']", NULL, 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200377 assert_int_equal(ret, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100378 CHECK_LOG_CTX("Unparsed characters \"[.='val3']\" left at the end of path.", NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200379
380 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
381 assert_string_equal(str,
382 "<ll2 xmlns=\"urn:tests:a\">val</ll2>\n"
383 "<ll2 xmlns=\"urn:tests:a\">val2</ll2>\n"
384 "<ll2 xmlns=\"urn:tests:a\">val3</ll2>\n");
385 free(str);
386 lyd_free_siblings(root);
Michal Vaskoea0f96c2022-12-20 08:34:39 +0100387
388 /* anydata */
389 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:any", "<elem>val</elem>", 0, LYD_ANYDATA_XML, 0, &root, NULL);
390 assert_int_equal(ret, LY_SUCCESS);
391 assert_non_null(root);
392
393 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
394 assert_string_equal(str,
395 "<any xmlns=\"urn:tests:a\">\n"
396 " <elem>val</elem>\n"
397 "</any>\n");
398 free(str);
399 lyd_print_mem(&str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
400 assert_string_equal(str,
401 "{\n"
402 " \"a:any\": {\n"
403 " \"elem\": \"val\"\n"
404 " }\n"
405 "}\n");
406 free(str);
407 lyd_free_siblings(root);
Michal Vasko54bf9e22023-04-27 08:20:30 +0200408
409 /* anyxml */
410 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:anyx", "<a/><b/><c/>", 0, LYD_ANYDATA_XML, 0, &root, NULL);
411 assert_int_equal(ret, LY_SUCCESS);
412 assert_non_null(root);
413
414 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
415 assert_string_equal(str,
416 "<anyx xmlns=\"urn:tests:a\">\n"
417 " <a/>\n"
418 " <b/>\n"
419 " <c/>\n"
420 "</anyx>\n");
421 free(str);
422 lyd_print_mem(&str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
423 assert_string_equal(str,
424 "{\n"
425 " \"a:anyx\": {\n"
426 " \"a\": [null],\n"
427 " \"b\": [null],\n"
428 " \"c\": [null]\n"
429 " }\n"
430 "}\n");
431 free(str);
432 lyd_free_siblings(root);
433
434 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:anyx", "{\"a\":[null],\"b\":[null],\"c\":[null]}", 0, LYD_ANYDATA_JSON, 0, &root, NULL);
435 assert_int_equal(ret, LY_SUCCESS);
436 assert_non_null(root);
437
438 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
439 assert_string_equal(str,
440 "<anyx xmlns=\"urn:tests:a\">\n"
441 " <a/>\n"
442 " <b/>\n"
443 " <c/>\n"
444 "</anyx>\n");
445 free(str);
446 lyd_print_mem(&str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
447 assert_string_equal(str,
448 "{\n"
449 " \"a:anyx\": {\n"
450 " \"a\": [null],\n"
451 " \"b\": [null],\n"
452 " \"c\": [null]\n"
453 " }\n"
454 "}\n");
455 free(str);
456 lyd_free_siblings(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200457}
458
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100459static void
460test_path_ext(void **state)
461{
462 LY_ERR ret;
463 struct lyd_node *root, *node;
Michal Vasko4de7d072021-07-09 09:13:18 +0200464 struct lys_module *mod;
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100465 const char *mod_str = "module ext {yang-version 1.1; namespace urn:tests:extensions:ext; prefix e;"
466 "import ietf-restconf {revision-date 2017-01-26; prefix rc;}"
467 "rc:yang-data template {container c {leaf x {type string;} leaf y {type string;} leaf z {type string;}}}}";
468
469 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
470 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
471
472 UTEST_ADD_MODULE(mod_str, LYS_IN_YANG, NULL, &mod);
473
474 /* create x */
475 ret = lyd_new_ext_path(NULL, &mod->compiled->exts[0], "/ext:c/x", "xxx", 0, &root);
476 assert_int_equal(ret, LY_SUCCESS);
477 assert_non_null(root);
478 assert_string_equal(root->schema->name, "c");
479 assert_non_null(node = lyd_child(root));
480 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200481 assert_string_equal("xxx", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100482
483 /* append y */
484 ret = lyd_new_ext_path(root, &mod->compiled->exts[0], "/ext:c/y", "yyy", 0, &node);
485 assert_int_equal(ret, LY_SUCCESS);
486 assert_string_equal(node->schema->name, "y");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200487 assert_string_equal("yyy", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100488
489 /* append z */
490 ret = lyd_new_path(root, NULL, "ext:z", "zzz", 0, &node);
491 assert_int_equal(ret, LY_SUCCESS);
492 assert_string_equal(node->schema->name, "z");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200493 assert_string_equal("zzz", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100494
495 lyd_free_tree(root);
496}
497
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100498int
499main(void)
Michal Vasko004d3152020-06-11 19:59:22 +0200500{
501 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200502 UTEST(test_top_level),
503 UTEST(test_opaq),
504 UTEST(test_path),
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100505 UTEST(test_path_ext),
Michal Vasko004d3152020-06-11 19:59:22 +0200506 };
507
508 return cmocka_run_group_tests(tests, NULL, NULL);
509}