blob: e9b591eff7bd427782b4f60d8ee0e6f8a12bf911 [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"
35 " leaf foo {\n"
36 " type uint16;\n"
37 " }\n"
38 " leaf-list ll {\n"
39 " type string;\n"
40 " }\n"
41 " container c {\n"
42 " leaf-list x {\n"
Michal Vaskoe78faec2021-04-08 17:24:43 +020043 " type string;\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020044 " }\n"
Radek Krejci95ccd1b2021-03-12 14:57:22 +010045 " }\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020046 " anydata any {\n"
47 " config false;\n"
Radek Krejci95ccd1b2021-03-12 14:57:22 +010048 " }\n"
Michal Vaskoe78faec2021-04-08 17:24:43 +020049 " leaf-list ll2 {\n"
50 " config false;\n"
51 " type string;\n"
52 " }\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020053 " list l2 {\n"
54 " config false;\n"
55 " container c {\n"
56 " leaf x {\n"
57 " type string;\n"
58 " }\n"
59 " }\n"
60 " }\n"
Michal Vaskoe78faec2021-04-08 17:24:43 +020061 " container c2 {\n"
62 " config false;\n"
63 " list l3 {\n"
64 " leaf x {\n"
65 " type string;\n"
66 " }\n"
67 " leaf y {\n"
68 " type string;\n"
69 " }\n"
70 " }\n"
71 " }\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020072 " rpc oper {\n"
73 " input {\n"
74 " leaf param {\n"
75 " type string;\n"
76 " }\n"
77 " }\n"
78 " output {\n"
79 " leaf param {\n"
80 " type int8;\n"
81 " }\n"
82 " }\n"
83 " }\n"
84 "}\n";
Michal Vasko004d3152020-06-11 19:59:22 +020085
86static void
87test_top_level(void **state)
88{
Michal Vasko4de7d072021-07-09 09:13:18 +020089 struct lys_module *mod;
Radek Krejci41ac9942020-11-02 14:47:56 +010090 struct lyd_node *node, *rpc;
Michal Vasko004d3152020-06-11 19:59:22 +020091
Radek Iša56ca9e42020-09-08 18:42:00 +020092 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
Michal Vasko004d3152020-06-11 19:59:22 +020093
94 /* list */
Radek Krejci41ac9942020-11-02 14:47:56 +010095 assert_int_equal(lyd_new_list(NULL, mod, "l1", 0, &node, "val_a", "val_b"), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +020096 lyd_free_tree(node);
97
Radek Krejci41ac9942020-11-02 14:47:56 +010098 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[]", 0, &node), LY_EVALID);
Radek Krejci2efc45b2020-12-22 16:25:44 +010099 CHECK_LOG_CTX("Unexpected XPath token \"]\" (\"]\").", "Schema location /a:l1.");
Michal Vasko004d3152020-06-11 19:59:22 +0200100
Radek Krejci41ac9942020-11-02 14:47:56 +0100101 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[key1='a'][key2='b']", 0, &node), LY_ENOTFOUND);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100102 CHECK_LOG_CTX("Not found node \"key1\" in path.", "Schema location /a:l1.");
Michal Vasko004d3152020-06-11 19:59:22 +0200103
Radek Krejci41ac9942020-11-02 14:47:56 +0100104 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100105 CHECK_LOG_CTX("Key expected instead of leaf \"c\" in path.", "Schema location /a:l1.");
Michal Vasko004d3152020-06-11 19:59:22 +0200106
Radek Krejci41ac9942020-11-02 14:47:56 +0100107 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 +0200108 CHECK_LOG_CTX("List node \"c\" not found.", NULL);
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", "[a='a'][b='b']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200111 lyd_free_tree(node);
112
Radek Krejci41ac9942020-11-02 14:47:56 +0100113 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a=''][b='']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200114 lyd_free_tree(node);
115
Radek Krejci41ac9942020-11-02 14:47:56 +0100116 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 +0200117 lyd_free_tree(node);
118
Radek Krejci41ac9942020-11-02 14:47:56 +0100119 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 +0200120 lyd_free_tree(node);
121
122 /* leaf */
Radek Krejci41ac9942020-11-02 14:47:56 +0100123 assert_int_equal(lyd_new_term(NULL, mod, "foo", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100124 CHECK_LOG_CTX("Invalid uint16 value \"[a='a'][b='b'][c='c']\".", "Schema location /a:foo.");
Michal Vasko004d3152020-06-11 19:59:22 +0200125
Radek Krejci41ac9942020-11-02 14:47:56 +0100126 assert_int_equal(lyd_new_term(NULL, mod, "c", "value", 0, &node), LY_ENOTFOUND);
Radek Iša56ca9e42020-09-08 18:42:00 +0200127 CHECK_LOG_CTX("Term node \"c\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200128
Radek Krejci41ac9942020-11-02 14:47:56 +0100129 assert_int_equal(lyd_new_term(NULL, mod, "foo", "256", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200130 lyd_free_tree(node);
131
132 /* leaf-list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100133 assert_int_equal(lyd_new_term(NULL, mod, "ll", "ahoy", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200134 lyd_free_tree(node);
135
136 /* container */
Radek Krejci41ac9942020-11-02 14:47:56 +0100137 assert_int_equal(lyd_new_inner(NULL, mod, "c", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200138 lyd_free_tree(node);
139
Radek Krejci41ac9942020-11-02 14:47:56 +0100140 assert_int_equal(lyd_new_inner(NULL, mod, "l1", 0, &node), LY_ENOTFOUND);
Radek Krejci8247bae2021-03-12 11:47:02 +0100141 CHECK_LOG_CTX("Inner node (not a list) \"l1\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200142
Radek Krejci41ac9942020-11-02 14:47:56 +0100143 assert_int_equal(lyd_new_inner(NULL, mod, "l2", 0, &node), LY_ENOTFOUND);
Radek Krejci8247bae2021-03-12 11:47:02 +0100144 CHECK_LOG_CTX("Inner node (not a list) \"l2\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200145
146 /* anydata */
Michal Vasko2a4ab2b2021-03-04 15:52:40 +0100147 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 +0200148 lyd_free_tree(node);
149
150 /* key-less list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100151 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "[a='a'][b='b']", 0, &node), LY_EVALID);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100152 CHECK_LOG_CTX("List predicate defined for keyless list \"l2\" in path.", "Schema location /a:l2.");
Michal Vasko004d3152020-06-11 19:59:22 +0200153
Radek Krejci41ac9942020-11-02 14:47:56 +0100154 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200155 lyd_free_tree(node);
156
Radek Krejci41ac9942020-11-02 14:47:56 +0100157 assert_int_equal(lyd_new_list2(NULL, mod, "l2", NULL, 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_list(NULL, mod, "l2", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200161 lyd_free_tree(node);
162
Radek Krejci41ac9942020-11-02 14:47:56 +0100163 /* RPC */
164 assert_int_equal(lyd_new_inner(NULL, mod, "oper", 0, &rpc), LY_SUCCESS);
165 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 0, &node), LY_SUCCESS);
166 assert_int_equal(LY_TYPE_STRING, ((struct lysc_node_leaf *)node->schema)->type->basetype);
167 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 1, &node), LY_SUCCESS);
168 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf *)node->schema)->type->basetype);
169 lyd_free_tree(rpc);
Michal Vasko004d3152020-06-11 19:59:22 +0200170}
171
Michal Vasko00cbf532020-06-15 13:58:47 +0200172static void
173test_opaq(void **state)
174{
Michal Vasko00cbf532020-06-15 13:58:47 +0200175 struct lyd_node *root, *node;
176 struct lyd_node_opaq *opq;
177
Radek Iša56ca9e42020-09-08 18:42:00 +0200178 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL);
179
Michal Vasko0ab974d2021-02-24 13:18:26 +0100180 assert_int_equal(lyd_new_opaq(NULL, UTEST_LYCTX, "node1", NULL, NULL, "my-module", &root), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200181 assert_null(root->schema);
182 opq = (struct lyd_node_opaq *)root;
Michal Vaskoad92b672020-11-12 13:11:31 +0100183 assert_string_equal(opq->name.name, "node1");
184 assert_string_equal(opq->name.module_name, "my-module");
Michal Vasko501af032020-11-11 20:27:44 +0100185 assert_string_equal(opq->value, "");
Michal Vasko00cbf532020-06-15 13:58:47 +0200186
Michal Vasko0ab974d2021-02-24 13:18:26 +0100187 assert_int_equal(lyd_new_opaq(root, NULL, "node2", "value", NULL, "my-module2", &node), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200188 assert_null(node->schema);
189 opq = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100190 assert_string_equal(opq->name.name, "node2");
191 assert_string_equal(opq->name.module_name, "my-module2");
Michal Vasko501af032020-11-11 20:27:44 +0100192 assert_string_equal(opq->value, "value");
Michal Vasko00cbf532020-06-15 13:58:47 +0200193 assert_ptr_equal(opq->parent, root);
194
195 lyd_free_tree(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200196}
197
198static void
199test_path(void **state)
200{
Michal Vasko00cbf532020-06-15 13:58:47 +0200201 LY_ERR ret;
202 struct lyd_node *root, *node, *parent;
Michal Vasko4de7d072021-07-09 09:13:18 +0200203 struct lys_module *mod;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200204 char *str;
Michal Vasko00cbf532020-06-15 13:58:47 +0200205
Michal Vaskoe78faec2021-04-08 17:24:43 +0200206 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
Radek Iša56ca9e42020-09-08 18:42:00 +0200207
Michal Vasko00cbf532020-06-15 13:58:47 +0200208 /* create 2 nodes */
Radek Krejci09c77442021-04-26 11:10:34 +0200209 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c/x[.='val']", "vvv", 0, 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200210 assert_int_equal(ret, LY_SUCCESS);
211 assert_non_null(root);
212 assert_string_equal(root->schema->name, "c");
213 assert_non_null(node);
214 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200215 assert_string_equal("val", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200216
217 /* append another */
Radek Krejci09c77442021-04-26 11:10:34 +0200218 ret = lyd_new_path2(root, NULL, "/a:c/x", "val2", 0, 0, 0, &parent, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200219 assert_int_equal(ret, LY_SUCCESS);
220 assert_ptr_equal(parent, node);
221 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200222 assert_string_equal("val2", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200223
224 /* and a last one */
Radek Krejci09c77442021-04-26 11:10:34 +0200225 ret = lyd_new_path2(root, NULL, "x", "val3", 0, 0, 0, &parent, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200226 assert_int_equal(ret, LY_SUCCESS);
227 assert_ptr_equal(parent, node);
228 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200229 assert_string_equal("val3", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200230
231 lyd_free_tree(root);
232
233 /* try LYD_NEWOPT_OPAQ */
Radek Krejci09c77442021-04-26 11:10:34 +0200234 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200235 assert_int_equal(ret, LY_EINVAL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100236 CHECK_LOG_CTX("Predicate missing for list \"l1\" in path \"/a:l1\".", "Schema location /a:l1.");
Michal Vasko00cbf532020-06-15 13:58:47 +0200237
Radek Krejci09c77442021-04-26 11:10:34 +0200238 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 +0200239 assert_int_equal(ret, LY_SUCCESS);
240 assert_non_null(root);
241 assert_null(root->schema);
242
243 lyd_free_tree(root);
244
Radek Krejci09c77442021-04-26 11:10:34 +0200245 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200246 assert_int_equal(ret, LY_EVALID);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100247 CHECK_LOG_CTX("Invalid empty uint16 value.", "Schema location /a:foo.");
Michal Vasko00cbf532020-06-15 13:58:47 +0200248
Radek Krejci09c77442021-04-26 11:10:34 +0200249 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 +0200250 assert_int_equal(ret, LY_SUCCESS);
251 assert_non_null(root);
252 assert_null(root->schema);
253
254 lyd_free_tree(root);
255
Michal Vaskoe78faec2021-04-08 17:24:43 +0200256 /* key-less list */
Radek Krejci09c77442021-04-26 11:10:34 +0200257 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c2/l3/x", "val1", 0, 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200258 assert_int_equal(ret, LY_SUCCESS);
259 assert_non_null(root);
260 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200261 assert_string_equal("val1", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200262
Radek Krejci09c77442021-04-26 11:10:34 +0200263 ret = lyd_new_path2(root, NULL, "/a:c2/l3[1]", NULL, 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200264 assert_int_equal(ret, LY_EEXIST);
265
Radek Krejci09c77442021-04-26 11:10:34 +0200266 ret = lyd_new_path2(root, NULL, "/a:c2/l3[2]/x", "val2", 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200267 assert_int_equal(ret, LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200268
Radek Krejci09c77442021-04-26 11:10:34 +0200269 ret = lyd_new_path2(root, NULL, "/a:c2/l3/x", "val3", 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200270 assert_int_equal(ret, LY_SUCCESS);
271 assert_non_null(node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200272
Radek Krejci09c77442021-04-26 11:10:34 +0200273 ret = lyd_new_path2(root, NULL, "/a:c2/l3[4]/x", "empty", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200274 assert_int_equal(ret, LY_SUCCESS);
275 assert_non_null(node);
276
Radek Krejci09c77442021-04-26 11:10:34 +0200277 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 +0200278 assert_int_equal(ret, LY_SUCCESS);
279 assert_non_null(node);
280
Radek Krejci09c77442021-04-26 11:10:34 +0200281 ret = lyd_new_path2(root, NULL, "/a:c2/l3[5]/x", "val5", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200282 assert_int_equal(ret, LY_SUCCESS);
283 assert_non_null(node);
284
Radek Krejci09c77442021-04-26 11:10:34 +0200285 ret = lyd_new_path2(root, NULL, "/a:c2/l3[6]/x", "val6", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200286 assert_int_equal(ret, LY_SUCCESS);
287 assert_non_null(node);
288
289 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
290 assert_string_equal(str,
291 "<c2 xmlns=\"urn:tests:a\">\n"
292 " <l3>\n"
293 " <x>val1</x>\n"
294 " </l3>\n"
295 " <l3>\n"
296 " <x>val2</x>\n"
297 " </l3>\n"
298 " <l3>\n"
299 " <x>val3</x>\n"
300 " </l3>\n"
301 " <l3>\n"
302 " <x>val4</x>\n"
303 " </l3>\n"
304 " <l3>\n"
305 " <x>val5</x>\n"
306 " </l3>\n"
307 " <l3>\n"
308 " <x>val6</x>\n"
309 " </l3>\n"
310 "</c2>\n");
311 free(str);
312 lyd_free_siblings(root);
313
314 /* state leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +0200315 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:ll2", "val_first", 0, 0, 0, &root, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200316 assert_int_equal(ret, LY_SUCCESS);
317 assert_non_null(root);
318 assert_string_equal(node->schema->name, "ll2");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200319 assert_string_equal("val_first", lyd_get_value(node));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200320
Radek Krejci09c77442021-04-26 11:10:34 +0200321 ret = lyd_new_path2(root, NULL, "/a:ll2[1]", "", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200322 assert_int_equal(ret, LY_EEXIST);
323
Radek Krejci09c77442021-04-26 11:10:34 +0200324 ret = lyd_new_path2(root, NULL, "/a:ll2[2]", "val2", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200325 assert_int_equal(ret, LY_SUCCESS);
326
Radek Krejci09c77442021-04-26 11:10:34 +0200327 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 +0200328 assert_int_equal(ret, LY_SUCCESS);
329 assert_non_null(node);
330
Radek Krejci09c77442021-04-26 11:10:34 +0200331 ret = lyd_new_path2(root, UTEST_LYCTX, "/a:ll2", "val3", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200332 assert_int_equal(ret, LY_SUCCESS);
333 assert_non_null(node);
334
Radek Krejci09c77442021-04-26 11:10:34 +0200335 ret = lyd_new_path2(root, NULL, "/a:ll2[3][.='val3']", NULL, 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200336 assert_int_equal(ret, LY_EVALID);
337
338 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
339 assert_string_equal(str,
340 "<ll2 xmlns=\"urn:tests:a\">val</ll2>\n"
341 "<ll2 xmlns=\"urn:tests:a\">val2</ll2>\n"
342 "<ll2 xmlns=\"urn:tests:a\">val3</ll2>\n");
343 free(str);
344 lyd_free_siblings(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200345}
346
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100347static void
348test_path_ext(void **state)
349{
350 LY_ERR ret;
351 struct lyd_node *root, *node;
Michal Vasko4de7d072021-07-09 09:13:18 +0200352 struct lys_module *mod;
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100353 const char *mod_str = "module ext {yang-version 1.1; namespace urn:tests:extensions:ext; prefix e;"
354 "import ietf-restconf {revision-date 2017-01-26; prefix rc;}"
355 "rc:yang-data template {container c {leaf x {type string;} leaf y {type string;} leaf z {type string;}}}}";
356
357 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
358 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
359
360 UTEST_ADD_MODULE(mod_str, LYS_IN_YANG, NULL, &mod);
361
362 /* create x */
363 ret = lyd_new_ext_path(NULL, &mod->compiled->exts[0], "/ext:c/x", "xxx", 0, &root);
364 assert_int_equal(ret, LY_SUCCESS);
365 assert_non_null(root);
366 assert_string_equal(root->schema->name, "c");
367 assert_non_null(node = lyd_child(root));
368 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200369 assert_string_equal("xxx", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100370
371 /* append y */
372 ret = lyd_new_ext_path(root, &mod->compiled->exts[0], "/ext:c/y", "yyy", 0, &node);
373 assert_int_equal(ret, LY_SUCCESS);
374 assert_string_equal(node->schema->name, "y");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200375 assert_string_equal("yyy", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100376
377 /* append z */
378 ret = lyd_new_path(root, NULL, "ext:z", "zzz", 0, &node);
379 assert_int_equal(ret, LY_SUCCESS);
380 assert_string_equal(node->schema->name, "z");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200381 assert_string_equal("zzz", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100382
383 lyd_free_tree(root);
384}
385
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100386int
387main(void)
Michal Vasko004d3152020-06-11 19:59:22 +0200388{
389 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200390 UTEST(test_top_level),
391 UTEST(test_opaq),
392 UTEST(test_path),
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100393 UTEST(test_path_ext),
Michal Vasko004d3152020-06-11 19:59:22 +0200394 };
395
396 return cmocka_run_group_tests(tests, NULL, NULL);
397}