blob: 1c6654309db827fed052c8c654e5582a3db24271 [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"
32 " type string;}\n"
33 " }\n"
34 " leaf foo {\n"
35 " type uint16;\n"
36 " }\n"
37 " leaf-list ll {\n"
38 " type string;\n"
39 " }\n"
40 " container c {\n"
41 " leaf-list x {\n"
42 " type string;\n"
43 " }\n"
44 " }"
45 " anydata any {\n"
46 " config false;\n"
47 " }"
48 " list l2 {\n"
49 " config false;\n"
50 " container c {\n"
51 " leaf x {\n"
52 " type string;\n"
53 " }\n"
54 " }\n"
55 " }\n"
56 " rpc oper {\n"
57 " input {\n"
58 " leaf param {\n"
59 " type string;\n"
60 " }\n"
61 " }\n"
62 " output {\n"
63 " leaf param {\n"
64 " type int8;\n"
65 " }\n"
66 " }\n"
67 " }\n"
68 "}\n";
Michal Vasko004d3152020-06-11 19:59:22 +020069
70static void
71test_top_level(void **state)
72{
Michal Vasko004d3152020-06-11 19:59:22 +020073 const struct lys_module *mod;
Radek Krejci41ac9942020-11-02 14:47:56 +010074 struct lyd_node *node, *rpc;
Michal Vasko004d3152020-06-11 19:59:22 +020075
Radek Iša56ca9e42020-09-08 18:42:00 +020076 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
Michal Vasko004d3152020-06-11 19:59:22 +020077
78 /* list */
Radek Krejci41ac9942020-11-02 14:47:56 +010079 assert_int_equal(lyd_new_list(NULL, mod, "l1", 0, &node, "val_a", "val_b"), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +020080 lyd_free_tree(node);
81
Radek Krejci41ac9942020-11-02 14:47:56 +010082 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[]", 0, &node), LY_EVALID);
Radek Krejci2efc45b2020-12-22 16:25:44 +010083 CHECK_LOG_CTX("Unexpected XPath token \"]\" (\"]\").", "Schema location /a:l1.");
Michal Vasko004d3152020-06-11 19:59:22 +020084
Radek Krejci41ac9942020-11-02 14:47:56 +010085 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[key1='a'][key2='b']", 0, &node), LY_ENOTFOUND);
Radek Krejci2efc45b2020-12-22 16:25:44 +010086 CHECK_LOG_CTX("Not found node \"key1\" in path.", "Schema location /a:l1.");
Michal Vasko004d3152020-06-11 19:59:22 +020087
Radek Krejci41ac9942020-11-02 14:47:56 +010088 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 +010089 CHECK_LOG_CTX("Key expected instead of leaf \"c\" in path.", "Schema location /a:l1.");
Michal Vasko004d3152020-06-11 19:59:22 +020090
Radek Krejci41ac9942020-11-02 14:47:56 +010091 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 +020092 CHECK_LOG_CTX("List node \"c\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +020093
Radek Krejci41ac9942020-11-02 14:47:56 +010094 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +020095 lyd_free_tree(node);
96
Radek Krejci41ac9942020-11-02 14:47:56 +010097 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a=''][b='']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +020098 lyd_free_tree(node);
99
Radek Krejci41ac9942020-11-02 14:47:56 +0100100 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 +0200101 lyd_free_tree(node);
102
Radek Krejci41ac9942020-11-02 14:47:56 +0100103 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 +0200104 lyd_free_tree(node);
105
106 /* leaf */
Radek Krejci41ac9942020-11-02 14:47:56 +0100107 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 +0100108 CHECK_LOG_CTX("Invalid uint16 value \"[a='a'][b='b'][c='c']\".", "Schema location /a:foo.");
Michal Vasko004d3152020-06-11 19:59:22 +0200109
Radek Krejci41ac9942020-11-02 14:47:56 +0100110 assert_int_equal(lyd_new_term(NULL, mod, "c", "value", 0, &node), LY_ENOTFOUND);
Radek Iša56ca9e42020-09-08 18:42:00 +0200111 CHECK_LOG_CTX("Term node \"c\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200112
Radek Krejci41ac9942020-11-02 14:47:56 +0100113 assert_int_equal(lyd_new_term(NULL, mod, "foo", "256", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200114 lyd_free_tree(node);
115
116 /* leaf-list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100117 assert_int_equal(lyd_new_term(NULL, mod, "ll", "ahoy", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200118 lyd_free_tree(node);
119
120 /* container */
Radek Krejci41ac9942020-11-02 14:47:56 +0100121 assert_int_equal(lyd_new_inner(NULL, mod, "c", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200122 lyd_free_tree(node);
123
Radek Krejci41ac9942020-11-02 14:47:56 +0100124 assert_int_equal(lyd_new_inner(NULL, mod, "l1", 0, &node), LY_ENOTFOUND);
Radek Krejci8247bae2021-03-12 11:47:02 +0100125 CHECK_LOG_CTX("Inner node (not a list) \"l1\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200126
Radek Krejci41ac9942020-11-02 14:47:56 +0100127 assert_int_equal(lyd_new_inner(NULL, mod, "l2", 0, &node), LY_ENOTFOUND);
Radek Krejci8247bae2021-03-12 11:47:02 +0100128 CHECK_LOG_CTX("Inner node (not a list) \"l2\" not found.", NULL);
Michal Vasko004d3152020-06-11 19:59:22 +0200129
130 /* anydata */
Michal Vasko2a4ab2b2021-03-04 15:52:40 +0100131 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 +0200132 lyd_free_tree(node);
133
134 /* key-less list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100135 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "[a='a'][b='b']", 0, &node), LY_EVALID);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100136 CHECK_LOG_CTX("List predicate defined for keyless list \"l2\" in path.", "Schema location /a:l2.");
Michal Vasko004d3152020-06-11 19:59:22 +0200137
Radek Krejci41ac9942020-11-02 14:47:56 +0100138 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200139 lyd_free_tree(node);
140
Radek Krejci41ac9942020-11-02 14:47:56 +0100141 assert_int_equal(lyd_new_list2(NULL, mod, "l2", NULL, 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200142 lyd_free_tree(node);
143
Radek Krejci41ac9942020-11-02 14:47:56 +0100144 assert_int_equal(lyd_new_list(NULL, mod, "l2", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200145 lyd_free_tree(node);
146
Radek Krejci41ac9942020-11-02 14:47:56 +0100147 /* RPC */
148 assert_int_equal(lyd_new_inner(NULL, mod, "oper", 0, &rpc), LY_SUCCESS);
149 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 0, &node), LY_SUCCESS);
150 assert_int_equal(LY_TYPE_STRING, ((struct lysc_node_leaf *)node->schema)->type->basetype);
151 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 1, &node), LY_SUCCESS);
152 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf *)node->schema)->type->basetype);
153 lyd_free_tree(rpc);
Michal Vasko004d3152020-06-11 19:59:22 +0200154}
155
Michal Vasko00cbf532020-06-15 13:58:47 +0200156static void
157test_opaq(void **state)
158{
Michal Vasko00cbf532020-06-15 13:58:47 +0200159 struct lyd_node *root, *node;
160 struct lyd_node_opaq *opq;
161
Radek Iša56ca9e42020-09-08 18:42:00 +0200162 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL);
163
Michal Vasko0ab974d2021-02-24 13:18:26 +0100164 assert_int_equal(lyd_new_opaq(NULL, UTEST_LYCTX, "node1", NULL, NULL, "my-module", &root), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200165 assert_null(root->schema);
166 opq = (struct lyd_node_opaq *)root;
Michal Vaskoad92b672020-11-12 13:11:31 +0100167 assert_string_equal(opq->name.name, "node1");
168 assert_string_equal(opq->name.module_name, "my-module");
Michal Vasko501af032020-11-11 20:27:44 +0100169 assert_string_equal(opq->value, "");
Michal Vasko00cbf532020-06-15 13:58:47 +0200170
Michal Vasko0ab974d2021-02-24 13:18:26 +0100171 assert_int_equal(lyd_new_opaq(root, NULL, "node2", "value", NULL, "my-module2", &node), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200172 assert_null(node->schema);
173 opq = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100174 assert_string_equal(opq->name.name, "node2");
175 assert_string_equal(opq->name.module_name, "my-module2");
Michal Vasko501af032020-11-11 20:27:44 +0100176 assert_string_equal(opq->value, "value");
Michal Vasko00cbf532020-06-15 13:58:47 +0200177 assert_ptr_equal(opq->parent, root);
178
179 lyd_free_tree(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200180}
181
182static void
183test_path(void **state)
184{
Michal Vasko00cbf532020-06-15 13:58:47 +0200185 LY_ERR ret;
186 struct lyd_node *root, *node, *parent;
Michal Vasko00cbf532020-06-15 13:58:47 +0200187
Radek Iša56ca9e42020-09-08 18:42:00 +0200188 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL);
189
Michal Vasko00cbf532020-06-15 13:58:47 +0200190 /* create 2 nodes */
Radek Iša56ca9e42020-09-08 18:42:00 +0200191 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c/x[.='val']", "vvv", 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200192 assert_int_equal(ret, LY_SUCCESS);
193 assert_non_null(root);
194 assert_string_equal(root->schema->name, "c");
195 assert_non_null(node);
196 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200197 assert_string_equal("val", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200198
199 /* append another */
200 ret = lyd_new_path2(root, NULL, "/a:c/x", "val2", 0, 0, &parent, &node);
201 assert_int_equal(ret, LY_SUCCESS);
202 assert_ptr_equal(parent, node);
203 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200204 assert_string_equal("val2", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200205
206 /* and a last one */
207 ret = lyd_new_path2(root, NULL, "x", "val3", 0, 0, &parent, &node);
208 assert_int_equal(ret, LY_SUCCESS);
209 assert_ptr_equal(parent, node);
210 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200211 assert_string_equal("val3", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200212
213 lyd_free_tree(root);
214
215 /* try LYD_NEWOPT_OPAQ */
Radek Iša56ca9e42020-09-08 18:42:00 +0200216 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200217 assert_int_equal(ret, LY_EINVAL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100218 CHECK_LOG_CTX("Predicate missing for list \"l1\" in path \"/a:l1\".", "Schema location /a:l1.");
Michal Vasko00cbf532020-06-15 13:58:47 +0200219
Radek Iša56ca9e42020-09-08 18:42:00 +0200220 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, LYD_NEW_PATH_OPAQ, NULL, &root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200221 assert_int_equal(ret, LY_SUCCESS);
222 assert_non_null(root);
223 assert_null(root->schema);
224
225 lyd_free_tree(root);
226
Radek Iša56ca9e42020-09-08 18:42:00 +0200227 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200228 assert_int_equal(ret, LY_EVALID);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100229 CHECK_LOG_CTX("Invalid empty uint16 value.", "Schema location /a:foo.");
Michal Vasko00cbf532020-06-15 13:58:47 +0200230
Radek Iša56ca9e42020-09-08 18:42:00 +0200231 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, LYD_NEW_PATH_OPAQ, NULL, &root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200232 assert_int_equal(ret, LY_SUCCESS);
233 assert_non_null(root);
234 assert_null(root->schema);
235
236 lyd_free_tree(root);
237
238 /* try LYD_NEWOPT_UPDATE */
Radek Iša56ca9e42020-09-08 18:42:00 +0200239 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l2[1]/c/x", "val", 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200240 assert_int_equal(ret, LY_SUCCESS);
241 assert_non_null(root);
242 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200243 assert_string_equal("val", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200244
245 ret = lyd_new_path2(root, NULL, "/a:l2[1]/c/x", "val", 0, 0, NULL, &node);
246 assert_int_equal(ret, LY_EEXIST);
247
Radek Krejci092e33c2020-10-12 15:33:10 +0200248 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 +0200249 assert_int_equal(ret, LY_SUCCESS);
250 assert_null(node);
251
Radek Krejci092e33c2020-10-12 15:33:10 +0200252 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 +0200253 assert_int_equal(ret, LY_SUCCESS);
254 assert_non_null(node);
255 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200256 assert_string_equal("val2", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200257
258 lyd_free_tree(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200259}
260
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100261int
262main(void)
Michal Vasko004d3152020-06-11 19:59:22 +0200263{
264 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200265 UTEST(test_top_level),
266 UTEST(test_opaq),
267 UTEST(test_path),
Michal Vasko004d3152020-06-11 19:59:22 +0200268 };
269
270 return cmocka_run_group_tests(tests, NULL, NULL);
271}