blob: 314bed4bec83a4a9780ba9332efcffc6dcd25b23 [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"
Radek Iša56ca9e42020-09-08 18:42:00 +020043 " type string;\n"
44 " }\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"
Radek Iša56ca9e42020-09-08 18:42:00 +020049 " list l2 {\n"
50 " config false;\n"
51 " container c {\n"
52 " leaf x {\n"
53 " type string;\n"
54 " }\n"
55 " }\n"
56 " }\n"
57 " rpc oper {\n"
58 " input {\n"
59 " leaf param {\n"
60 " type string;\n"
61 " }\n"
62 " }\n"
63 " output {\n"
64 " leaf param {\n"
65 " type int8;\n"
66 " }\n"
67 " }\n"
68 " }\n"
69 "}\n";
Michal Vasko004d3152020-06-11 19:59:22 +020070
71static void
72test_top_level(void **state)
73{
Michal Vasko004d3152020-06-11 19:59:22 +020074 const struct lys_module *mod;
Radek Krejci41ac9942020-11-02 14:47:56 +010075 struct lyd_node *node, *rpc;
Michal Vasko004d3152020-06-11 19:59:22 +020076
Radek Iša56ca9e42020-09-08 18:42:00 +020077 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
Michal Vasko004d3152020-06-11 19:59:22 +020078
79 /* list */
Radek Krejci41ac9942020-11-02 14:47:56 +010080 assert_int_equal(lyd_new_list(NULL, mod, "l1", 0, &node, "val_a", "val_b"), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +020081 lyd_free_tree(node);
82
Radek Krejci41ac9942020-11-02 14:47:56 +010083 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[]", 0, &node), LY_EVALID);
Radek Krejci2efc45b2020-12-22 16:25:44 +010084 CHECK_LOG_CTX("Unexpected XPath token \"]\" (\"]\").", "Schema location /a:l1.");
Michal Vasko004d3152020-06-11 19:59:22 +020085
Radek Krejci41ac9942020-11-02 14:47:56 +010086 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[key1='a'][key2='b']", 0, &node), LY_ENOTFOUND);
Radek Krejci2efc45b2020-12-22 16:25:44 +010087 CHECK_LOG_CTX("Not found node \"key1\" in path.", "Schema location /a:l1.");
Michal Vasko004d3152020-06-11 19:59:22 +020088
Radek Krejci41ac9942020-11-02 14:47:56 +010089 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 +010090 CHECK_LOG_CTX("Key expected instead of leaf \"c\" in path.", "Schema location /a:l1.");
Michal Vasko004d3152020-06-11 19:59:22 +020091
Radek Krejci41ac9942020-11-02 14:47:56 +010092 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 +020093 CHECK_LOG_CTX("List node \"c\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +020094
Radek Krejci41ac9942020-11-02 14:47:56 +010095 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b']", 0, &node), 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", "[a=''][b='']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +020099 lyd_free_tree(node);
100
Radek Krejci41ac9942020-11-02 14:47:56 +0100101 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 +0200102 lyd_free_tree(node);
103
Radek Krejci41ac9942020-11-02 14:47:56 +0100104 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 +0200105 lyd_free_tree(node);
106
107 /* leaf */
Radek Krejci41ac9942020-11-02 14:47:56 +0100108 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 +0100109 CHECK_LOG_CTX("Invalid uint16 value \"[a='a'][b='b'][c='c']\".", "Schema location /a:foo.");
Michal Vasko004d3152020-06-11 19:59:22 +0200110
Radek Krejci41ac9942020-11-02 14:47:56 +0100111 assert_int_equal(lyd_new_term(NULL, mod, "c", "value", 0, &node), LY_ENOTFOUND);
Radek Iša56ca9e42020-09-08 18:42:00 +0200112 CHECK_LOG_CTX("Term node \"c\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200113
Radek Krejci41ac9942020-11-02 14:47:56 +0100114 assert_int_equal(lyd_new_term(NULL, mod, "foo", "256", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200115 lyd_free_tree(node);
116
117 /* leaf-list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100118 assert_int_equal(lyd_new_term(NULL, mod, "ll", "ahoy", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200119 lyd_free_tree(node);
120
121 /* container */
Radek Krejci41ac9942020-11-02 14:47:56 +0100122 assert_int_equal(lyd_new_inner(NULL, mod, "c", 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_inner(NULL, mod, "l1", 0, &node), LY_ENOTFOUND);
Radek Krejci8247bae2021-03-12 11:47:02 +0100126 CHECK_LOG_CTX("Inner node (not a list) \"l1\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200127
Radek Krejci41ac9942020-11-02 14:47:56 +0100128 assert_int_equal(lyd_new_inner(NULL, mod, "l2", 0, &node), LY_ENOTFOUND);
Radek Krejci8247bae2021-03-12 11:47:02 +0100129 CHECK_LOG_CTX("Inner node (not a list) \"l2\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200130
131 /* anydata */
Michal Vasko2a4ab2b2021-03-04 15:52:40 +0100132 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 +0200133 lyd_free_tree(node);
134
135 /* key-less list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100136 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "[a='a'][b='b']", 0, &node), LY_EVALID);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100137 CHECK_LOG_CTX("List predicate defined for keyless list \"l2\" in path.", "Schema location /a:l2.");
Michal Vasko004d3152020-06-11 19:59:22 +0200138
Radek Krejci41ac9942020-11-02 14:47:56 +0100139 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200140 lyd_free_tree(node);
141
Radek Krejci41ac9942020-11-02 14:47:56 +0100142 assert_int_equal(lyd_new_list2(NULL, mod, "l2", NULL, 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200143 lyd_free_tree(node);
144
Radek Krejci41ac9942020-11-02 14:47:56 +0100145 assert_int_equal(lyd_new_list(NULL, mod, "l2", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200146 lyd_free_tree(node);
147
Radek Krejci41ac9942020-11-02 14:47:56 +0100148 /* RPC */
149 assert_int_equal(lyd_new_inner(NULL, mod, "oper", 0, &rpc), LY_SUCCESS);
150 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 0, &node), LY_SUCCESS);
151 assert_int_equal(LY_TYPE_STRING, ((struct lysc_node_leaf *)node->schema)->type->basetype);
152 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 1, &node), LY_SUCCESS);
153 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf *)node->schema)->type->basetype);
154 lyd_free_tree(rpc);
Michal Vasko004d3152020-06-11 19:59:22 +0200155}
156
Michal Vasko00cbf532020-06-15 13:58:47 +0200157static void
158test_opaq(void **state)
159{
Michal Vasko00cbf532020-06-15 13:58:47 +0200160 struct lyd_node *root, *node;
161 struct lyd_node_opaq *opq;
162
Radek Iša56ca9e42020-09-08 18:42:00 +0200163 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL);
164
Michal Vasko0ab974d2021-02-24 13:18:26 +0100165 assert_int_equal(lyd_new_opaq(NULL, UTEST_LYCTX, "node1", NULL, NULL, "my-module", &root), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200166 assert_null(root->schema);
167 opq = (struct lyd_node_opaq *)root;
Michal Vaskoad92b672020-11-12 13:11:31 +0100168 assert_string_equal(opq->name.name, "node1");
169 assert_string_equal(opq->name.module_name, "my-module");
Michal Vasko501af032020-11-11 20:27:44 +0100170 assert_string_equal(opq->value, "");
Michal Vasko00cbf532020-06-15 13:58:47 +0200171
Michal Vasko0ab974d2021-02-24 13:18:26 +0100172 assert_int_equal(lyd_new_opaq(root, NULL, "node2", "value", NULL, "my-module2", &node), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200173 assert_null(node->schema);
174 opq = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100175 assert_string_equal(opq->name.name, "node2");
176 assert_string_equal(opq->name.module_name, "my-module2");
Michal Vasko501af032020-11-11 20:27:44 +0100177 assert_string_equal(opq->value, "value");
Michal Vasko00cbf532020-06-15 13:58:47 +0200178 assert_ptr_equal(opq->parent, root);
179
180 lyd_free_tree(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200181}
182
183static void
184test_path(void **state)
185{
Michal Vasko00cbf532020-06-15 13:58:47 +0200186 LY_ERR ret;
187 struct lyd_node *root, *node, *parent;
Michal Vasko00cbf532020-06-15 13:58:47 +0200188
Radek Iša56ca9e42020-09-08 18:42:00 +0200189 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL);
190
Michal Vasko00cbf532020-06-15 13:58:47 +0200191 /* create 2 nodes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200192 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c/x[.='val']", "vvv", 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200193 assert_int_equal(ret, LY_SUCCESS);
194 assert_non_null(root);
195 assert_string_equal(root->schema->name, "c");
196 assert_non_null(node);
197 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200198 assert_string_equal("val", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200199
200 /* append another */
201 ret = lyd_new_path2(root, NULL, "/a:c/x", "val2", 0, 0, &parent, &node);
202 assert_int_equal(ret, LY_SUCCESS);
203 assert_ptr_equal(parent, node);
204 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200205 assert_string_equal("val2", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200206
207 /* and a last one */
208 ret = lyd_new_path2(root, NULL, "x", "val3", 0, 0, &parent, &node);
209 assert_int_equal(ret, LY_SUCCESS);
210 assert_ptr_equal(parent, node);
211 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200212 assert_string_equal("val3", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200213
214 lyd_free_tree(root);
215
216 /* try LYD_NEWOPT_OPAQ */
Radek Iša56ca9e42020-09-08 18:42:00 +0200217 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200218 assert_int_equal(ret, LY_EINVAL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100219 CHECK_LOG_CTX("Predicate missing for list \"l1\" in path \"/a:l1\".", "Schema location /a:l1.");
Michal Vasko00cbf532020-06-15 13:58:47 +0200220
Radek Iša56ca9e42020-09-08 18:42:00 +0200221 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, LYD_NEW_PATH_OPAQ, NULL, &root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200222 assert_int_equal(ret, LY_SUCCESS);
223 assert_non_null(root);
224 assert_null(root->schema);
225
226 lyd_free_tree(root);
227
Radek Iša56ca9e42020-09-08 18:42:00 +0200228 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200229 assert_int_equal(ret, LY_EVALID);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100230 CHECK_LOG_CTX("Invalid empty uint16 value.", "Schema location /a:foo.");
Michal Vasko00cbf532020-06-15 13:58:47 +0200231
Radek Iša56ca9e42020-09-08 18:42:00 +0200232 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, LYD_NEW_PATH_OPAQ, NULL, &root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200233 assert_int_equal(ret, LY_SUCCESS);
234 assert_non_null(root);
235 assert_null(root->schema);
236
237 lyd_free_tree(root);
238
239 /* try LYD_NEWOPT_UPDATE */
Radek Iša56ca9e42020-09-08 18:42:00 +0200240 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l2[1]/c/x", "val", 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200241 assert_int_equal(ret, LY_SUCCESS);
242 assert_non_null(root);
243 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200244 assert_string_equal("val", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200245
246 ret = lyd_new_path2(root, NULL, "/a:l2[1]/c/x", "val", 0, 0, NULL, &node);
247 assert_int_equal(ret, LY_EEXIST);
248
Radek Krejci092e33c2020-10-12 15:33:10 +0200249 ret = lyd_new_path2(root, NULL, "/a:l2[1]/c/x", "val", 0, LYD_NEW_PATH_UPDATE, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200250 assert_int_equal(ret, LY_SUCCESS);
251 assert_null(node);
252
Radek Krejci092e33c2020-10-12 15:33:10 +0200253 ret = lyd_new_path2(root, NULL, "/a:l2[1]/c/x", "val2", 0, LYD_NEW_PATH_UPDATE, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200254 assert_int_equal(ret, LY_SUCCESS);
255 assert_non_null(node);
256 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200257 assert_string_equal("val2", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200258
259 lyd_free_tree(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200260}
261
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100262static void
263test_path_ext(void **state)
264{
265 LY_ERR ret;
266 struct lyd_node *root, *node;
267 const struct lys_module *mod;
268 const char *mod_str = "module ext {yang-version 1.1; namespace urn:tests:extensions:ext; prefix e;"
269 "import ietf-restconf {revision-date 2017-01-26; prefix rc;}"
270 "rc:yang-data template {container c {leaf x {type string;} leaf y {type string;} leaf z {type string;}}}}";
271
272 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
273 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
274
275 UTEST_ADD_MODULE(mod_str, LYS_IN_YANG, NULL, &mod);
276
277 /* create x */
278 ret = lyd_new_ext_path(NULL, &mod->compiled->exts[0], "/ext:c/x", "xxx", 0, &root);
279 assert_int_equal(ret, LY_SUCCESS);
280 assert_non_null(root);
281 assert_string_equal(root->schema->name, "c");
282 assert_non_null(node = lyd_child(root));
283 assert_string_equal(node->schema->name, "x");
284 assert_string_equal("xxx", LYD_CANON_VALUE(node));
285
286 /* append y */
287 ret = lyd_new_ext_path(root, &mod->compiled->exts[0], "/ext:c/y", "yyy", 0, &node);
288 assert_int_equal(ret, LY_SUCCESS);
289 assert_string_equal(node->schema->name, "y");
290 assert_string_equal("yyy", LYD_CANON_VALUE(node));
291
292 /* append z */
293 ret = lyd_new_path(root, NULL, "ext:z", "zzz", 0, &node);
294 assert_int_equal(ret, LY_SUCCESS);
295 assert_string_equal(node->schema->name, "z");
296 assert_string_equal("zzz", LYD_CANON_VALUE(node));
297
298 lyd_free_tree(root);
299}
300
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100301int
302main(void)
Michal Vasko004d3152020-06-11 19:59:22 +0200303{
304 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200305 UTEST(test_top_level),
306 UTEST(test_opaq),
307 UTEST(test_path),
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100308 UTEST(test_path_ext),
Michal Vasko004d3152020-06-11 19:59:22 +0200309 };
310
311 return cmocka_run_group_tests(tests, NULL, NULL);
312}