blob: 553a487fb5d413576ac766d6f16c9a6e4dce4984 [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 Vaskoe78faec2021-04-08 17:24:43 +020058 " leaf-list ll2 {\n"
59 " config false;\n"
60 " type string;\n"
61 " }\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020062 " list l2 {\n"
63 " config false;\n"
64 " container c {\n"
65 " leaf x {\n"
66 " type string;\n"
67 " }\n"
68 " }\n"
69 " }\n"
Michal Vaskoe78faec2021-04-08 17:24:43 +020070 " container c2 {\n"
71 " config false;\n"
72 " list l3 {\n"
73 " leaf x {\n"
74 " type string;\n"
75 " }\n"
76 " leaf y {\n"
77 " type string;\n"
78 " }\n"
79 " }\n"
80 " }\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020081 " rpc oper {\n"
82 " input {\n"
83 " leaf param {\n"
84 " type string;\n"
85 " }\n"
86 " }\n"
87 " output {\n"
88 " leaf param {\n"
89 " type int8;\n"
90 " }\n"
91 " }\n"
92 " }\n"
93 "}\n";
Michal Vasko004d3152020-06-11 19:59:22 +020094
95static void
96test_top_level(void **state)
97{
Michal Vasko4de7d072021-07-09 09:13:18 +020098 struct lys_module *mod;
Radek Krejci41ac9942020-11-02 14:47:56 +010099 struct lyd_node *node, *rpc;
Michal Vasko004d3152020-06-11 19:59:22 +0200100
Radek Iša56ca9e42020-09-08 18:42:00 +0200101 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
Michal Vasko004d3152020-06-11 19:59:22 +0200102
103 /* list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100104 assert_int_equal(lyd_new_list(NULL, mod, "l1", 0, &node, "val_a", "val_b"), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200105 lyd_free_tree(node);
106
Radek Krejci41ac9942020-11-02 14:47:56 +0100107 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[]", 0, &node), LY_EVALID);
Michal Vasko959f8d82022-06-16 07:51:50 +0200108 CHECK_LOG_CTX("Unexpected XPath token \"]\" (\"]\").", "Schema location \"/a:l1\".");
Michal Vasko004d3152020-06-11 19:59:22 +0200109
Radek Krejci41ac9942020-11-02 14:47:56 +0100110 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[key1='a'][key2='b']", 0, &node), LY_ENOTFOUND);
Michal Vasko959f8d82022-06-16 07:51:50 +0200111 CHECK_LOG_CTX("Not found node \"key1\" in path.", "Schema location \"/a:l1\".");
Michal Vasko004d3152020-06-11 19:59:22 +0200112
Radek Krejci41ac9942020-11-02 14:47:56 +0100113 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 +0200114 CHECK_LOG_CTX("Key expected instead of leaf \"c\" in path.", "Schema location \"/a:l1\".");
Michal Vasko004d3152020-06-11 19:59:22 +0200115
Radek Krejci41ac9942020-11-02 14:47:56 +0100116 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 +0200117 CHECK_LOG_CTX("List node \"c\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200118
Radek Krejci41ac9942020-11-02 14:47:56 +0100119 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200120 lyd_free_tree(node);
121
Radek Krejci41ac9942020-11-02 14:47:56 +0100122 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a=''][b='']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200123 lyd_free_tree(node);
124
Radek Krejci41ac9942020-11-02 14:47:56 +0100125 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 +0200126 lyd_free_tree(node);
127
Radek Krejci41ac9942020-11-02 14:47:56 +0100128 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 +0200129 lyd_free_tree(node);
130
131 /* leaf */
Radek Krejci41ac9942020-11-02 14:47:56 +0100132 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 +0200133 CHECK_LOG_CTX("Invalid type uint16 value \"[a='a'][b='b'][c='c']\".", "Schema location \"/a:foo\".");
Michal Vasko004d3152020-06-11 19:59:22 +0200134
Radek Krejci41ac9942020-11-02 14:47:56 +0100135 assert_int_equal(lyd_new_term(NULL, mod, "c", "value", 0, &node), LY_ENOTFOUND);
Radek Iša56ca9e42020-09-08 18:42:00 +0200136 CHECK_LOG_CTX("Term node \"c\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200137
Radek Krejci41ac9942020-11-02 14:47:56 +0100138 assert_int_equal(lyd_new_term(NULL, mod, "foo", "256", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200139 lyd_free_tree(node);
140
141 /* leaf-list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100142 assert_int_equal(lyd_new_term(NULL, mod, "ll", "ahoy", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200143 lyd_free_tree(node);
144
145 /* container */
Radek Krejci41ac9942020-11-02 14:47:56 +0100146 assert_int_equal(lyd_new_inner(NULL, mod, "c", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200147 lyd_free_tree(node);
148
Radek Krejci41ac9942020-11-02 14:47:56 +0100149 assert_int_equal(lyd_new_inner(NULL, mod, "l1", 0, &node), LY_ENOTFOUND);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200150 CHECK_LOG_CTX("Inner node (container, notif, RPC, or action) \"l1\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200151
Radek Krejci41ac9942020-11-02 14:47:56 +0100152 assert_int_equal(lyd_new_inner(NULL, mod, "l2", 0, &node), LY_ENOTFOUND);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200153 CHECK_LOG_CTX("Inner node (container, notif, RPC, or action) \"l2\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200154
155 /* anydata */
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100156 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 +0200157 lyd_free_tree(node);
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100158 assert_int_equal(lyd_new_any(NULL, mod, "any", "<node>val</node>", 0, LYD_ANYDATA_STRING, 0, &node), LY_SUCCESS);
159 lyd_free_tree(node);
Michal Vasko004d3152020-06-11 19:59:22 +0200160
161 /* key-less list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100162 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "[a='a'][b='b']", 0, &node), LY_EVALID);
Michal Vasko959f8d82022-06-16 07:51:50 +0200163 CHECK_LOG_CTX("List predicate defined for keyless list \"l2\" in path.", "Schema location \"/a:l2\".");
Michal Vasko004d3152020-06-11 19:59:22 +0200164
Radek Krejci41ac9942020-11-02 14:47:56 +0100165 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200166 lyd_free_tree(node);
167
Radek Krejci41ac9942020-11-02 14:47:56 +0100168 assert_int_equal(lyd_new_list2(NULL, mod, "l2", NULL, 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200169 lyd_free_tree(node);
170
Radek Krejci41ac9942020-11-02 14:47:56 +0100171 assert_int_equal(lyd_new_list(NULL, mod, "l2", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200172 lyd_free_tree(node);
173
Radek Krejci41ac9942020-11-02 14:47:56 +0100174 /* RPC */
175 assert_int_equal(lyd_new_inner(NULL, mod, "oper", 0, &rpc), LY_SUCCESS);
176 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 0, &node), LY_SUCCESS);
177 assert_int_equal(LY_TYPE_STRING, ((struct lysc_node_leaf *)node->schema)->type->basetype);
178 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 1, &node), LY_SUCCESS);
179 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf *)node->schema)->type->basetype);
180 lyd_free_tree(rpc);
Michal Vasko004d3152020-06-11 19:59:22 +0200181}
182
Michal Vasko00cbf532020-06-15 13:58:47 +0200183static void
184test_opaq(void **state)
185{
Michal Vasko00cbf532020-06-15 13:58:47 +0200186 struct lyd_node *root, *node;
187 struct lyd_node_opaq *opq;
188
Radek Iša56ca9e42020-09-08 18:42:00 +0200189 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL);
190
Michal Vasko0ab974d2021-02-24 13:18:26 +0100191 assert_int_equal(lyd_new_opaq(NULL, UTEST_LYCTX, "node1", NULL, NULL, "my-module", &root), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200192 assert_null(root->schema);
193 opq = (struct lyd_node_opaq *)root;
Michal Vaskoad92b672020-11-12 13:11:31 +0100194 assert_string_equal(opq->name.name, "node1");
195 assert_string_equal(opq->name.module_name, "my-module");
Michal Vasko501af032020-11-11 20:27:44 +0100196 assert_string_equal(opq->value, "");
Michal Vasko00cbf532020-06-15 13:58:47 +0200197
Michal Vasko0ab974d2021-02-24 13:18:26 +0100198 assert_int_equal(lyd_new_opaq(root, NULL, "node2", "value", NULL, "my-module2", &node), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200199 assert_null(node->schema);
200 opq = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100201 assert_string_equal(opq->name.name, "node2");
202 assert_string_equal(opq->name.module_name, "my-module2");
Michal Vasko501af032020-11-11 20:27:44 +0100203 assert_string_equal(opq->value, "value");
Michal Vasko00cbf532020-06-15 13:58:47 +0200204 assert_ptr_equal(opq->parent, root);
205
206 lyd_free_tree(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200207}
208
209static void
210test_path(void **state)
211{
Michal Vasko00cbf532020-06-15 13:58:47 +0200212 LY_ERR ret;
213 struct lyd_node *root, *node, *parent;
Michal Vasko4de7d072021-07-09 09:13:18 +0200214 struct lys_module *mod;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200215 char *str;
Michal Vasko00cbf532020-06-15 13:58:47 +0200216
Michal Vaskoe78faec2021-04-08 17:24:43 +0200217 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
Radek Iša56ca9e42020-09-08 18:42:00 +0200218
Michal Vasko00cbf532020-06-15 13:58:47 +0200219 /* create 2 nodes */
Radek Krejci09c77442021-04-26 11:10:34 +0200220 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c/x[.='val']", "vvv", 0, 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200221 assert_int_equal(ret, LY_SUCCESS);
222 assert_non_null(root);
223 assert_string_equal(root->schema->name, "c");
224 assert_non_null(node);
225 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200226 assert_string_equal("val", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200227
228 /* append another */
Radek Krejci09c77442021-04-26 11:10:34 +0200229 ret = lyd_new_path2(root, NULL, "/a:c/x", "val2", 0, 0, 0, &parent, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200230 assert_int_equal(ret, LY_SUCCESS);
231 assert_ptr_equal(parent, node);
232 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200233 assert_string_equal("val2", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200234
235 /* and a last one */
Radek Krejci09c77442021-04-26 11:10:34 +0200236 ret = lyd_new_path2(root, NULL, "x", "val3", 0, 0, 0, &parent, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200237 assert_int_equal(ret, LY_SUCCESS);
238 assert_ptr_equal(parent, node);
239 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200240 assert_string_equal("val3", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200241
242 lyd_free_tree(root);
243
244 /* try LYD_NEWOPT_OPAQ */
Radek Krejci09c77442021-04-26 11:10:34 +0200245 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200246 assert_int_equal(ret, LY_EINVAL);
Michal Vasko959f8d82022-06-16 07:51:50 +0200247 CHECK_LOG_CTX("Predicate missing for list \"l1\" in path \"/a:l1\".", "Schema location \"/a:l1\".");
Michal Vasko00cbf532020-06-15 13:58:47 +0200248
Radek Krejci09c77442021-04-26 11:10:34 +0200249 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 +0200250 assert_int_equal(ret, LY_SUCCESS);
251 assert_non_null(root);
252 assert_null(root->schema);
253
254 lyd_free_tree(root);
255
Radek Krejci09c77442021-04-26 11:10:34 +0200256 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200257 assert_int_equal(ret, LY_EVALID);
Michal Vasko959f8d82022-06-16 07:51:50 +0200258 CHECK_LOG_CTX("Invalid type uint16 empty value.", "Schema location \"/a:foo\".");
Michal Vasko00cbf532020-06-15 13:58:47 +0200259
Radek Krejci09c77442021-04-26 11:10:34 +0200260 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 +0200261 assert_int_equal(ret, LY_SUCCESS);
262 assert_non_null(root);
263 assert_null(root->schema);
264
265 lyd_free_tree(root);
266
Michal Vasko106f0862021-11-02 11:49:27 +0100267 ret = lyd_new_path(NULL, UTEST_LYCTX, "/a:l11", NULL, LYD_NEW_PATH_OPAQ, &root);
268 assert_int_equal(ret, LY_SUCCESS);
269 assert_non_null(root);
270 assert_null(root->schema);
271
272 ret = lyd_new_path(root, NULL, "a", NULL, LYD_NEW_PATH_OPAQ, NULL);
273 assert_int_equal(ret, LY_SUCCESS);
274 assert_non_null(lyd_child(root));
275 assert_null(lyd_child(root)->schema);
276
277 ret = lyd_new_path(root, NULL, "b", NULL, LYD_NEW_PATH_OPAQ, NULL);
278 assert_int_equal(ret, LY_SUCCESS);
279 assert_non_null(lyd_child(root)->next);
280 assert_null(lyd_child(root)->next->schema);
281
282 lyd_free_tree(root);
283
Michal Vaskoe78faec2021-04-08 17:24:43 +0200284 /* key-less list */
Radek Krejci09c77442021-04-26 11:10:34 +0200285 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c2/l3/x", "val1", 0, 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200286 assert_int_equal(ret, LY_SUCCESS);
287 assert_non_null(root);
288 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200289 assert_string_equal("val1", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200290
Radek Krejci09c77442021-04-26 11:10:34 +0200291 ret = lyd_new_path2(root, NULL, "/a:c2/l3[1]", NULL, 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200292 assert_int_equal(ret, LY_EEXIST);
Michal Vasko62af3692023-02-09 14:00:09 +0100293 CHECK_LOG_CTX("Path \"/a:c2/l3[1]\" already exists.", "Data location \"/a:c2/l3[1]\".");
Michal Vasko00cbf532020-06-15 13:58:47 +0200294
Radek Krejci09c77442021-04-26 11:10:34 +0200295 ret = lyd_new_path2(root, NULL, "/a:c2/l3[2]/x", "val2", 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200296 assert_int_equal(ret, LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200297
Radek Krejci09c77442021-04-26 11:10:34 +0200298 ret = lyd_new_path2(root, NULL, "/a:c2/l3/x", "val3", 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200299 assert_int_equal(ret, LY_SUCCESS);
300 assert_non_null(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[4]/x", "empty", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200303 assert_int_equal(ret, LY_SUCCESS);
304 assert_non_null(node);
305
Radek Krejci09c77442021-04-26 11:10:34 +0200306 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 +0200307 assert_int_equal(ret, LY_SUCCESS);
308 assert_non_null(node);
309
Radek Krejci09c77442021-04-26 11:10:34 +0200310 ret = lyd_new_path2(root, NULL, "/a:c2/l3[5]/x", "val5", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200311 assert_int_equal(ret, LY_SUCCESS);
312 assert_non_null(node);
313
Radek Krejci09c77442021-04-26 11:10:34 +0200314 ret = lyd_new_path2(root, NULL, "/a:c2/l3[6]/x", "val6", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200315 assert_int_equal(ret, LY_SUCCESS);
316 assert_non_null(node);
317
318 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
319 assert_string_equal(str,
320 "<c2 xmlns=\"urn:tests:a\">\n"
321 " <l3>\n"
322 " <x>val1</x>\n"
323 " </l3>\n"
324 " <l3>\n"
325 " <x>val2</x>\n"
326 " </l3>\n"
327 " <l3>\n"
328 " <x>val3</x>\n"
329 " </l3>\n"
330 " <l3>\n"
331 " <x>val4</x>\n"
332 " </l3>\n"
333 " <l3>\n"
334 " <x>val5</x>\n"
335 " </l3>\n"
336 " <l3>\n"
337 " <x>val6</x>\n"
338 " </l3>\n"
339 "</c2>\n");
340 free(str);
341 lyd_free_siblings(root);
342
343 /* state leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +0200344 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:ll2", "val_first", 0, 0, 0, &root, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200345 assert_int_equal(ret, LY_SUCCESS);
346 assert_non_null(root);
347 assert_string_equal(node->schema->name, "ll2");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200348 assert_string_equal("val_first", lyd_get_value(node));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200349
Radek Krejci09c77442021-04-26 11:10:34 +0200350 ret = lyd_new_path2(root, NULL, "/a:ll2[1]", "", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200351 assert_int_equal(ret, LY_EEXIST);
Michal Vasko62af3692023-02-09 14:00:09 +0100352 CHECK_LOG_CTX("Path \"/a:ll2[1]\" already exists.", "Data location \"/a:ll2[1]\".");
Michal Vaskoe78faec2021-04-08 17:24:43 +0200353
Radek Krejci09c77442021-04-26 11:10:34 +0200354 ret = lyd_new_path2(root, NULL, "/a:ll2[2]", "val2", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200355 assert_int_equal(ret, LY_SUCCESS);
356
Radek Krejci09c77442021-04-26 11:10:34 +0200357 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 +0200358 assert_int_equal(ret, LY_SUCCESS);
359 assert_non_null(node);
360
Radek Krejci09c77442021-04-26 11:10:34 +0200361 ret = lyd_new_path2(root, UTEST_LYCTX, "/a:ll2", "val3", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200362 assert_int_equal(ret, LY_SUCCESS);
363 assert_non_null(node);
364
Radek Krejci09c77442021-04-26 11:10:34 +0200365 ret = lyd_new_path2(root, NULL, "/a:ll2[3][.='val3']", NULL, 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200366 assert_int_equal(ret, LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100367 CHECK_LOG_CTX("Unparsed characters \"[.='val3']\" left at the end of path.", NULL);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200368
369 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
370 assert_string_equal(str,
371 "<ll2 xmlns=\"urn:tests:a\">val</ll2>\n"
372 "<ll2 xmlns=\"urn:tests:a\">val2</ll2>\n"
373 "<ll2 xmlns=\"urn:tests:a\">val3</ll2>\n");
374 free(str);
375 lyd_free_siblings(root);
Michal Vaskoea0f96c2022-12-20 08:34:39 +0100376
377 /* anydata */
378 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:any", "<elem>val</elem>", 0, LYD_ANYDATA_XML, 0, &root, NULL);
379 assert_int_equal(ret, LY_SUCCESS);
380 assert_non_null(root);
381
382 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
383 assert_string_equal(str,
384 "<any xmlns=\"urn:tests:a\">\n"
385 " <elem>val</elem>\n"
386 "</any>\n");
387 free(str);
388 lyd_print_mem(&str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
389 assert_string_equal(str,
390 "{\n"
391 " \"a:any\": {\n"
392 " \"elem\": \"val\"\n"
393 " }\n"
394 "}\n");
395 free(str);
396 lyd_free_siblings(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200397}
398
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100399static void
400test_path_ext(void **state)
401{
402 LY_ERR ret;
403 struct lyd_node *root, *node;
Michal Vasko4de7d072021-07-09 09:13:18 +0200404 struct lys_module *mod;
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100405 const char *mod_str = "module ext {yang-version 1.1; namespace urn:tests:extensions:ext; prefix e;"
406 "import ietf-restconf {revision-date 2017-01-26; prefix rc;}"
407 "rc:yang-data template {container c {leaf x {type string;} leaf y {type string;} leaf z {type string;}}}}";
408
409 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
410 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
411
412 UTEST_ADD_MODULE(mod_str, LYS_IN_YANG, NULL, &mod);
413
414 /* create x */
415 ret = lyd_new_ext_path(NULL, &mod->compiled->exts[0], "/ext:c/x", "xxx", 0, &root);
416 assert_int_equal(ret, LY_SUCCESS);
417 assert_non_null(root);
418 assert_string_equal(root->schema->name, "c");
419 assert_non_null(node = lyd_child(root));
420 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200421 assert_string_equal("xxx", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100422
423 /* append y */
424 ret = lyd_new_ext_path(root, &mod->compiled->exts[0], "/ext:c/y", "yyy", 0, &node);
425 assert_int_equal(ret, LY_SUCCESS);
426 assert_string_equal(node->schema->name, "y");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200427 assert_string_equal("yyy", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100428
429 /* append z */
430 ret = lyd_new_path(root, NULL, "ext:z", "zzz", 0, &node);
431 assert_int_equal(ret, LY_SUCCESS);
432 assert_string_equal(node->schema->name, "z");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200433 assert_string_equal("zzz", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100434
435 lyd_free_tree(root);
436}
437
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100438int
439main(void)
Michal Vasko004d3152020-06-11 19:59:22 +0200440{
441 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200442 UTEST(test_top_level),
443 UTEST(test_opaq),
444 UTEST(test_path),
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100445 UTEST(test_path_ext),
Michal Vasko004d3152020-06-11 19:59:22 +0200446 };
447
448 return cmocka_run_group_tests(tests, NULL, NULL);
449}