blob: 76429606a345be049687a57ea94aed2e099dd98b [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);
293
Radek Krejci09c77442021-04-26 11:10:34 +0200294 ret = lyd_new_path2(root, NULL, "/a:c2/l3[2]/x", "val2", 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200295 assert_int_equal(ret, LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200296
Radek Krejci09c77442021-04-26 11:10:34 +0200297 ret = lyd_new_path2(root, NULL, "/a:c2/l3/x", "val3", 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200298 assert_int_equal(ret, LY_SUCCESS);
299 assert_non_null(node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200300
Radek Krejci09c77442021-04-26 11:10:34 +0200301 ret = lyd_new_path2(root, NULL, "/a:c2/l3[4]/x", "empty", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200302 assert_int_equal(ret, LY_SUCCESS);
303 assert_non_null(node);
304
Radek Krejci09c77442021-04-26 11:10:34 +0200305 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 +0200306 assert_int_equal(ret, LY_SUCCESS);
307 assert_non_null(node);
308
Radek Krejci09c77442021-04-26 11:10:34 +0200309 ret = lyd_new_path2(root, NULL, "/a:c2/l3[5]/x", "val5", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200310 assert_int_equal(ret, LY_SUCCESS);
311 assert_non_null(node);
312
Radek Krejci09c77442021-04-26 11:10:34 +0200313 ret = lyd_new_path2(root, NULL, "/a:c2/l3[6]/x", "val6", 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
317 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
318 assert_string_equal(str,
319 "<c2 xmlns=\"urn:tests:a\">\n"
320 " <l3>\n"
321 " <x>val1</x>\n"
322 " </l3>\n"
323 " <l3>\n"
324 " <x>val2</x>\n"
325 " </l3>\n"
326 " <l3>\n"
327 " <x>val3</x>\n"
328 " </l3>\n"
329 " <l3>\n"
330 " <x>val4</x>\n"
331 " </l3>\n"
332 " <l3>\n"
333 " <x>val5</x>\n"
334 " </l3>\n"
335 " <l3>\n"
336 " <x>val6</x>\n"
337 " </l3>\n"
338 "</c2>\n");
339 free(str);
340 lyd_free_siblings(root);
341
342 /* state leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +0200343 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:ll2", "val_first", 0, 0, 0, &root, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200344 assert_int_equal(ret, LY_SUCCESS);
345 assert_non_null(root);
346 assert_string_equal(node->schema->name, "ll2");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200347 assert_string_equal("val_first", lyd_get_value(node));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200348
Radek Krejci09c77442021-04-26 11:10:34 +0200349 ret = lyd_new_path2(root, NULL, "/a:ll2[1]", "", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200350 assert_int_equal(ret, LY_EEXIST);
351
Radek Krejci09c77442021-04-26 11:10:34 +0200352 ret = lyd_new_path2(root, NULL, "/a:ll2[2]", "val2", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200353 assert_int_equal(ret, LY_SUCCESS);
354
Radek Krejci09c77442021-04-26 11:10:34 +0200355 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 +0200356 assert_int_equal(ret, LY_SUCCESS);
357 assert_non_null(node);
358
Radek Krejci09c77442021-04-26 11:10:34 +0200359 ret = lyd_new_path2(root, UTEST_LYCTX, "/a:ll2", "val3", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200360 assert_int_equal(ret, LY_SUCCESS);
361 assert_non_null(node);
362
Radek Krejci09c77442021-04-26 11:10:34 +0200363 ret = lyd_new_path2(root, NULL, "/a:ll2[3][.='val3']", NULL, 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200364 assert_int_equal(ret, LY_EVALID);
365
366 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
367 assert_string_equal(str,
368 "<ll2 xmlns=\"urn:tests:a\">val</ll2>\n"
369 "<ll2 xmlns=\"urn:tests:a\">val2</ll2>\n"
370 "<ll2 xmlns=\"urn:tests:a\">val3</ll2>\n");
371 free(str);
372 lyd_free_siblings(root);
Michal Vaskoea0f96c2022-12-20 08:34:39 +0100373
374 /* anydata */
375 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:any", "<elem>val</elem>", 0, LYD_ANYDATA_XML, 0, &root, NULL);
376 assert_int_equal(ret, LY_SUCCESS);
377 assert_non_null(root);
378
379 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
380 assert_string_equal(str,
381 "<any xmlns=\"urn:tests:a\">\n"
382 " <elem>val</elem>\n"
383 "</any>\n");
384 free(str);
385 lyd_print_mem(&str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
386 assert_string_equal(str,
387 "{\n"
388 " \"a:any\": {\n"
389 " \"elem\": \"val\"\n"
390 " }\n"
391 "}\n");
392 free(str);
393 lyd_free_siblings(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200394}
395
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100396static void
397test_path_ext(void **state)
398{
399 LY_ERR ret;
400 struct lyd_node *root, *node;
Michal Vasko4de7d072021-07-09 09:13:18 +0200401 struct lys_module *mod;
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100402 const char *mod_str = "module ext {yang-version 1.1; namespace urn:tests:extensions:ext; prefix e;"
403 "import ietf-restconf {revision-date 2017-01-26; prefix rc;}"
404 "rc:yang-data template {container c {leaf x {type string;} leaf y {type string;} leaf z {type string;}}}}";
405
406 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
407 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
408
409 UTEST_ADD_MODULE(mod_str, LYS_IN_YANG, NULL, &mod);
410
411 /* create x */
412 ret = lyd_new_ext_path(NULL, &mod->compiled->exts[0], "/ext:c/x", "xxx", 0, &root);
413 assert_int_equal(ret, LY_SUCCESS);
414 assert_non_null(root);
415 assert_string_equal(root->schema->name, "c");
416 assert_non_null(node = lyd_child(root));
417 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200418 assert_string_equal("xxx", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100419
420 /* append y */
421 ret = lyd_new_ext_path(root, &mod->compiled->exts[0], "/ext:c/y", "yyy", 0, &node);
422 assert_int_equal(ret, LY_SUCCESS);
423 assert_string_equal(node->schema->name, "y");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200424 assert_string_equal("yyy", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100425
426 /* append z */
427 ret = lyd_new_path(root, NULL, "ext:z", "zzz", 0, &node);
428 assert_int_equal(ret, LY_SUCCESS);
429 assert_string_equal(node->schema->name, "z");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200430 assert_string_equal("zzz", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100431
432 lyd_free_tree(root);
433}
434
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100435int
436main(void)
Michal Vasko004d3152020-06-11 19:59:22 +0200437{
438 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200439 UTEST(test_top_level),
440 UTEST(test_opaq),
441 UTEST(test_path),
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100442 UTEST(test_path_ext),
Michal Vasko004d3152020-06-11 19:59:22 +0200443 };
444
445 return cmocka_run_group_tests(tests, NULL, NULL);
446}