blob: b55172ff4e281b6336c5301d5cc0c5ae2a7b2539 [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 Vasko2a4ab2b2021-03-04 15:52:40 +0100156 assert_int_equal(lyd_new_any(NULL, mod, "any", "some-value", 0, LYD_ANYDATA_STRING, 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200157 lyd_free_tree(node);
158
159 /* key-less list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100160 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "[a='a'][b='b']", 0, &node), LY_EVALID);
Michal Vasko959f8d82022-06-16 07:51:50 +0200161 CHECK_LOG_CTX("List predicate defined for keyless list \"l2\" in path.", "Schema location \"/a:l2\".");
Michal Vasko004d3152020-06-11 19:59:22 +0200162
Radek Krejci41ac9942020-11-02 14:47:56 +0100163 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200164 lyd_free_tree(node);
165
Radek Krejci41ac9942020-11-02 14:47:56 +0100166 assert_int_equal(lyd_new_list2(NULL, mod, "l2", NULL, 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200167 lyd_free_tree(node);
168
Radek Krejci41ac9942020-11-02 14:47:56 +0100169 assert_int_equal(lyd_new_list(NULL, mod, "l2", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200170 lyd_free_tree(node);
171
Radek Krejci41ac9942020-11-02 14:47:56 +0100172 /* RPC */
173 assert_int_equal(lyd_new_inner(NULL, mod, "oper", 0, &rpc), LY_SUCCESS);
174 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 0, &node), LY_SUCCESS);
175 assert_int_equal(LY_TYPE_STRING, ((struct lysc_node_leaf *)node->schema)->type->basetype);
176 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 1, &node), LY_SUCCESS);
177 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf *)node->schema)->type->basetype);
178 lyd_free_tree(rpc);
Michal Vasko004d3152020-06-11 19:59:22 +0200179}
180
Michal Vasko00cbf532020-06-15 13:58:47 +0200181static void
182test_opaq(void **state)
183{
Michal Vasko00cbf532020-06-15 13:58:47 +0200184 struct lyd_node *root, *node;
185 struct lyd_node_opaq *opq;
186
Radek Iša56ca9e42020-09-08 18:42:00 +0200187 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL);
188
Michal Vasko0ab974d2021-02-24 13:18:26 +0100189 assert_int_equal(lyd_new_opaq(NULL, UTEST_LYCTX, "node1", NULL, NULL, "my-module", &root), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200190 assert_null(root->schema);
191 opq = (struct lyd_node_opaq *)root;
Michal Vaskoad92b672020-11-12 13:11:31 +0100192 assert_string_equal(opq->name.name, "node1");
193 assert_string_equal(opq->name.module_name, "my-module");
Michal Vasko501af032020-11-11 20:27:44 +0100194 assert_string_equal(opq->value, "");
Michal Vasko00cbf532020-06-15 13:58:47 +0200195
Michal Vasko0ab974d2021-02-24 13:18:26 +0100196 assert_int_equal(lyd_new_opaq(root, NULL, "node2", "value", NULL, "my-module2", &node), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200197 assert_null(node->schema);
198 opq = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100199 assert_string_equal(opq->name.name, "node2");
200 assert_string_equal(opq->name.module_name, "my-module2");
Michal Vasko501af032020-11-11 20:27:44 +0100201 assert_string_equal(opq->value, "value");
Michal Vasko00cbf532020-06-15 13:58:47 +0200202 assert_ptr_equal(opq->parent, root);
203
204 lyd_free_tree(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200205}
206
207static void
208test_path(void **state)
209{
Michal Vasko00cbf532020-06-15 13:58:47 +0200210 LY_ERR ret;
211 struct lyd_node *root, *node, *parent;
Michal Vasko4de7d072021-07-09 09:13:18 +0200212 struct lys_module *mod;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200213 char *str;
Michal Vasko00cbf532020-06-15 13:58:47 +0200214
Michal Vaskoe78faec2021-04-08 17:24:43 +0200215 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
Radek Iša56ca9e42020-09-08 18:42:00 +0200216
Michal Vasko00cbf532020-06-15 13:58:47 +0200217 /* create 2 nodes */
Radek Krejci09c77442021-04-26 11:10:34 +0200218 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c/x[.='val']", "vvv", 0, 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200219 assert_int_equal(ret, LY_SUCCESS);
220 assert_non_null(root);
221 assert_string_equal(root->schema->name, "c");
222 assert_non_null(node);
223 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200224 assert_string_equal("val", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200225
226 /* append another */
Radek Krejci09c77442021-04-26 11:10:34 +0200227 ret = lyd_new_path2(root, NULL, "/a:c/x", "val2", 0, 0, 0, &parent, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200228 assert_int_equal(ret, LY_SUCCESS);
229 assert_ptr_equal(parent, node);
230 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200231 assert_string_equal("val2", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200232
233 /* and a last one */
Radek Krejci09c77442021-04-26 11:10:34 +0200234 ret = lyd_new_path2(root, NULL, "x", "val3", 0, 0, 0, &parent, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200235 assert_int_equal(ret, LY_SUCCESS);
236 assert_ptr_equal(parent, node);
237 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200238 assert_string_equal("val3", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200239
240 lyd_free_tree(root);
241
242 /* try LYD_NEWOPT_OPAQ */
Radek Krejci09c77442021-04-26 11:10:34 +0200243 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200244 assert_int_equal(ret, LY_EINVAL);
Michal Vasko959f8d82022-06-16 07:51:50 +0200245 CHECK_LOG_CTX("Predicate missing for list \"l1\" in path \"/a:l1\".", "Schema location \"/a:l1\".");
Michal Vasko00cbf532020-06-15 13:58:47 +0200246
Radek Krejci09c77442021-04-26 11:10:34 +0200247 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 +0200248 assert_int_equal(ret, LY_SUCCESS);
249 assert_non_null(root);
250 assert_null(root->schema);
251
252 lyd_free_tree(root);
253
Radek Krejci09c77442021-04-26 11:10:34 +0200254 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200255 assert_int_equal(ret, LY_EVALID);
Michal Vasko959f8d82022-06-16 07:51:50 +0200256 CHECK_LOG_CTX("Invalid type uint16 empty value.", "Schema location \"/a:foo\".");
Michal Vasko00cbf532020-06-15 13:58:47 +0200257
Radek Krejci09c77442021-04-26 11:10:34 +0200258 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 +0200259 assert_int_equal(ret, LY_SUCCESS);
260 assert_non_null(root);
261 assert_null(root->schema);
262
263 lyd_free_tree(root);
264
Michal Vasko106f0862021-11-02 11:49:27 +0100265 ret = lyd_new_path(NULL, UTEST_LYCTX, "/a:l11", NULL, LYD_NEW_PATH_OPAQ, &root);
266 assert_int_equal(ret, LY_SUCCESS);
267 assert_non_null(root);
268 assert_null(root->schema);
269
270 ret = lyd_new_path(root, NULL, "a", NULL, LYD_NEW_PATH_OPAQ, NULL);
271 assert_int_equal(ret, LY_SUCCESS);
272 assert_non_null(lyd_child(root));
273 assert_null(lyd_child(root)->schema);
274
275 ret = lyd_new_path(root, NULL, "b", NULL, LYD_NEW_PATH_OPAQ, NULL);
276 assert_int_equal(ret, LY_SUCCESS);
277 assert_non_null(lyd_child(root)->next);
278 assert_null(lyd_child(root)->next->schema);
279
280 lyd_free_tree(root);
281
Michal Vaskoe78faec2021-04-08 17:24:43 +0200282 /* key-less list */
Radek Krejci09c77442021-04-26 11:10:34 +0200283 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c2/l3/x", "val1", 0, 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200284 assert_int_equal(ret, LY_SUCCESS);
285 assert_non_null(root);
286 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200287 assert_string_equal("val1", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200288
Radek Krejci09c77442021-04-26 11:10:34 +0200289 ret = lyd_new_path2(root, NULL, "/a:c2/l3[1]", NULL, 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200290 assert_int_equal(ret, LY_EEXIST);
291
Radek Krejci09c77442021-04-26 11:10:34 +0200292 ret = lyd_new_path2(root, NULL, "/a:c2/l3[2]/x", "val2", 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200293 assert_int_equal(ret, LY_SUCCESS);
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/x", "val3", 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200296 assert_int_equal(ret, LY_SUCCESS);
297 assert_non_null(node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200298
Radek Krejci09c77442021-04-26 11:10:34 +0200299 ret = lyd_new_path2(root, NULL, "/a:c2/l3[4]/x", "empty", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200300 assert_int_equal(ret, LY_SUCCESS);
301 assert_non_null(node);
302
Radek Krejci09c77442021-04-26 11:10:34 +0200303 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 +0200304 assert_int_equal(ret, LY_SUCCESS);
305 assert_non_null(node);
306
Radek Krejci09c77442021-04-26 11:10:34 +0200307 ret = lyd_new_path2(root, NULL, "/a:c2/l3[5]/x", "val5", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200308 assert_int_equal(ret, LY_SUCCESS);
309 assert_non_null(node);
310
Radek Krejci09c77442021-04-26 11:10:34 +0200311 ret = lyd_new_path2(root, NULL, "/a:c2/l3[6]/x", "val6", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200312 assert_int_equal(ret, LY_SUCCESS);
313 assert_non_null(node);
314
315 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
316 assert_string_equal(str,
317 "<c2 xmlns=\"urn:tests:a\">\n"
318 " <l3>\n"
319 " <x>val1</x>\n"
320 " </l3>\n"
321 " <l3>\n"
322 " <x>val2</x>\n"
323 " </l3>\n"
324 " <l3>\n"
325 " <x>val3</x>\n"
326 " </l3>\n"
327 " <l3>\n"
328 " <x>val4</x>\n"
329 " </l3>\n"
330 " <l3>\n"
331 " <x>val5</x>\n"
332 " </l3>\n"
333 " <l3>\n"
334 " <x>val6</x>\n"
335 " </l3>\n"
336 "</c2>\n");
337 free(str);
338 lyd_free_siblings(root);
339
340 /* state leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +0200341 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:ll2", "val_first", 0, 0, 0, &root, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200342 assert_int_equal(ret, LY_SUCCESS);
343 assert_non_null(root);
344 assert_string_equal(node->schema->name, "ll2");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200345 assert_string_equal("val_first", lyd_get_value(node));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200346
Radek Krejci09c77442021-04-26 11:10:34 +0200347 ret = lyd_new_path2(root, NULL, "/a:ll2[1]", "", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200348 assert_int_equal(ret, LY_EEXIST);
349
Radek Krejci09c77442021-04-26 11:10:34 +0200350 ret = lyd_new_path2(root, NULL, "/a:ll2[2]", "val2", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200351 assert_int_equal(ret, LY_SUCCESS);
352
Radek Krejci09c77442021-04-26 11:10:34 +0200353 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 +0200354 assert_int_equal(ret, LY_SUCCESS);
355 assert_non_null(node);
356
Radek Krejci09c77442021-04-26 11:10:34 +0200357 ret = lyd_new_path2(root, UTEST_LYCTX, "/a:ll2", "val3", 0, 0, 0, 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, NULL, "/a:ll2[3][.='val3']", NULL, 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200362 assert_int_equal(ret, LY_EVALID);
363
364 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
365 assert_string_equal(str,
366 "<ll2 xmlns=\"urn:tests:a\">val</ll2>\n"
367 "<ll2 xmlns=\"urn:tests:a\">val2</ll2>\n"
368 "<ll2 xmlns=\"urn:tests:a\">val3</ll2>\n");
369 free(str);
370 lyd_free_siblings(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200371}
372
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100373static void
374test_path_ext(void **state)
375{
376 LY_ERR ret;
377 struct lyd_node *root, *node;
Michal Vasko4de7d072021-07-09 09:13:18 +0200378 struct lys_module *mod;
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100379 const char *mod_str = "module ext {yang-version 1.1; namespace urn:tests:extensions:ext; prefix e;"
380 "import ietf-restconf {revision-date 2017-01-26; prefix rc;}"
381 "rc:yang-data template {container c {leaf x {type string;} leaf y {type string;} leaf z {type string;}}}}";
382
383 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
384 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
385
386 UTEST_ADD_MODULE(mod_str, LYS_IN_YANG, NULL, &mod);
387
388 /* create x */
389 ret = lyd_new_ext_path(NULL, &mod->compiled->exts[0], "/ext:c/x", "xxx", 0, &root);
390 assert_int_equal(ret, LY_SUCCESS);
391 assert_non_null(root);
392 assert_string_equal(root->schema->name, "c");
393 assert_non_null(node = lyd_child(root));
394 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200395 assert_string_equal("xxx", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100396
397 /* append y */
398 ret = lyd_new_ext_path(root, &mod->compiled->exts[0], "/ext:c/y", "yyy", 0, &node);
399 assert_int_equal(ret, LY_SUCCESS);
400 assert_string_equal(node->schema->name, "y");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200401 assert_string_equal("yyy", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100402
403 /* append z */
404 ret = lyd_new_path(root, NULL, "ext:z", "zzz", 0, &node);
405 assert_int_equal(ret, LY_SUCCESS);
406 assert_string_equal(node->schema->name, "z");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200407 assert_string_equal("zzz", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100408
409 lyd_free_tree(root);
410}
411
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100412int
413main(void)
Michal Vasko004d3152020-06-11 19:59:22 +0200414{
415 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200416 UTEST(test_top_level),
417 UTEST(test_opaq),
418 UTEST(test_path),
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100419 UTEST(test_path_ext),
Michal Vasko004d3152020-06-11 19:59:22 +0200420 };
421
422 return cmocka_run_group_tests(tests, NULL, NULL);
423}