blob: 7194893e132c08fb18e535b51ca5339679d541a0 [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 Vasko54bf9e22023-04-27 08:20:30 +020058 " anyxml anyx;\n"
Michal Vaskoe78faec2021-04-08 17:24:43 +020059 " leaf-list ll2 {\n"
60 " config false;\n"
61 " type string;\n"
62 " }\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020063 " list l2 {\n"
64 " config false;\n"
65 " container c {\n"
66 " leaf x {\n"
67 " type string;\n"
68 " }\n"
69 " }\n"
70 " }\n"
Michal Vaskoe78faec2021-04-08 17:24:43 +020071 " container c2 {\n"
72 " config false;\n"
73 " list l3 {\n"
74 " leaf x {\n"
75 " type string;\n"
76 " }\n"
77 " leaf y {\n"
78 " type string;\n"
79 " }\n"
80 " }\n"
81 " }\n"
Radek Iša56ca9e42020-09-08 18:42:00 +020082 " rpc oper {\n"
83 " input {\n"
84 " leaf param {\n"
85 " type string;\n"
86 " }\n"
87 " }\n"
88 " output {\n"
89 " leaf param {\n"
90 " type int8;\n"
91 " }\n"
92 " }\n"
93 " }\n"
94 "}\n";
Michal Vasko004d3152020-06-11 19:59:22 +020095
96static void
97test_top_level(void **state)
98{
Michal Vasko4de7d072021-07-09 09:13:18 +020099 struct lys_module *mod;
Radek Krejci41ac9942020-11-02 14:47:56 +0100100 struct lyd_node *node, *rpc;
Michal Vasko004d3152020-06-11 19:59:22 +0200101
Radek Iša56ca9e42020-09-08 18:42:00 +0200102 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
Michal Vasko004d3152020-06-11 19:59:22 +0200103
104 /* list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100105 assert_int_equal(lyd_new_list(NULL, mod, "l1", 0, &node, "val_a", "val_b"), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200106 lyd_free_tree(node);
107
Radek Krejci41ac9942020-11-02 14:47:56 +0100108 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[]", 0, &node), LY_EVALID);
Michal Vasko7a266772024-01-23 11:02:38 +0100109 CHECK_LOG_CTX("Unexpected XPath token \"]\" (\"]\").", "/a:l1", 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200110
Radek Krejci41ac9942020-11-02 14:47:56 +0100111 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[key1='a'][key2='b']", 0, &node), LY_ENOTFOUND);
Michal Vasko7a266772024-01-23 11:02:38 +0100112 CHECK_LOG_CTX("Not found node \"key1\" in path.", "/a:l1", 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200113
Radek Krejci41ac9942020-11-02 14:47:56 +0100114 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID);
Michal Vasko7a266772024-01-23 11:02:38 +0100115 CHECK_LOG_CTX("Key expected instead of leaf \"c\" in path.", "/a:l1", 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200116
Radek Krejci41ac9942020-11-02 14:47:56 +0100117 assert_int_equal(lyd_new_list2(NULL, mod, "c", "[a='a'][b='b']", 0, &node), LY_ENOTFOUND);
Michal Vasko7a266772024-01-23 11:02:38 +0100118 CHECK_LOG_CTX("List node \"c\" not found.", NULL, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200119
Radek Krejci41ac9942020-11-02 14:47:56 +0100120 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200121 lyd_free_tree(node);
122
Radek Krejci41ac9942020-11-02 14:47:56 +0100123 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a=''][b='']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200124 lyd_free_tree(node);
125
Radek Krejci41ac9942020-11-02 14:47:56 +0100126 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 +0200127 lyd_free_tree(node);
128
Radek Krejci41ac9942020-11-02 14:47:56 +0100129 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 +0200130 lyd_free_tree(node);
131
Michal Vasko2c1e3272023-10-17 14:08:35 +0200132 const char *key_vals[] = {"a", "b"};
Michal Vaskof7931562023-10-17 14:18:03 +0200133
Michal Vasko2c1e3272023-10-17 14:08:35 +0200134 assert_int_equal(lyd_new_list3(NULL, mod, "l1", key_vals, NULL, 0, &node), LY_SUCCESS);
135 lyd_free_tree(node);
136
137 uint32_t val_lens[] = {1, 1};
Michal Vaskof7931562023-10-17 14:18:03 +0200138
Michal Vaskof8c0b582024-02-21 09:55:53 +0100139 assert_int_equal(lyd_new_list3(NULL, mod, "l1", key_vals, val_lens, LYD_NEW_VAL_BIN, &node), LY_SUCCESS);
Michal Vasko2c1e3272023-10-17 14:08:35 +0200140 lyd_free_tree(node);
stewegd4cde642024-02-21 08:34:16 +0100141
Michal Vasko6a027db2024-02-21 09:55:34 +0100142 assert_int_equal(lyd_new_list3(NULL, mod, "l1", key_vals, val_lens, LYD_NEW_VAL_CANON, &node), LY_SUCCESS);
stewegd4cde642024-02-21 08:34:16 +0100143 lyd_free_tree(node);
Michal Vasko6a027db2024-02-21 09:55:34 +0100144 assert_int_equal(lyd_new_list3(NULL, mod, "l1", key_vals, val_lens, LYD_NEW_VAL_CANON | LYD_NEW_VAL_STORE_ONLY, &node), LY_EINVAL);
Michal Vaskof8c0b582024-02-21 09:55:53 +0100145 CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (lyd_new_list3()).", NULL, 0);
stewegd4cde642024-02-21 08:34:16 +0100146
Michal Vasko6a027db2024-02-21 09:55:34 +0100147 assert_int_equal(lyd_new_list(NULL, mod, "l1", LYD_NEW_VAL_BIN, &node, "val_a", 5, "val_b", 5), LY_SUCCESS);
stewegd4cde642024-02-21 08:34:16 +0100148 lyd_free_tree(node);
Michal Vasko6a027db2024-02-21 09:55:34 +0100149 assert_int_equal(lyd_new_list(NULL, mod, "l1", LYD_NEW_VAL_BIN | LYD_NEW_VAL_STORE_ONLY, &node, "val_a", 5, "val_b", 5), LY_EINVAL);
stewegd4cde642024-02-21 08:34:16 +0100150 CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (lyd_new_list()).", NULL, 0);
151
Michal Vasko6a027db2024-02-21 09:55:34 +0100152 assert_int_equal(lyd_new_list(NULL, mod, "l1", LYD_NEW_VAL_CANON, &node, "val_a", "val_b"), LY_SUCCESS);
stewegd4cde642024-02-21 08:34:16 +0100153 lyd_free_tree(node);
Michal Vasko6a027db2024-02-21 09:55:34 +0100154 assert_int_equal(lyd_new_list(NULL, mod, "l1", LYD_NEW_VAL_CANON | LYD_NEW_VAL_STORE_ONLY, &node, "val_a", "val_b"), LY_EINVAL);
stewegd4cde642024-02-21 08:34:16 +0100155 CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (lyd_new_list()).", NULL, 0);
Michal Vasko2c1e3272023-10-17 14:08:35 +0200156
Michal Vasko004d3152020-06-11 19:59:22 +0200157 /* leaf */
Radek Krejci41ac9942020-11-02 14:47:56 +0100158 assert_int_equal(lyd_new_term(NULL, mod, "foo", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID);
Michal Vasko7a266772024-01-23 11:02:38 +0100159 CHECK_LOG_CTX("Invalid type uint16 value \"[a='a'][b='b'][c='c']\".", "/a:foo", 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200160
Radek Krejci41ac9942020-11-02 14:47:56 +0100161 assert_int_equal(lyd_new_term(NULL, mod, "c", "value", 0, &node), LY_ENOTFOUND);
Michal Vasko7a266772024-01-23 11:02:38 +0100162 CHECK_LOG_CTX("Term node \"c\" not found.", NULL, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200163
Radek Krejci41ac9942020-11-02 14:47:56 +0100164 assert_int_equal(lyd_new_term(NULL, mod, "foo", "256", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200165 lyd_free_tree(node);
166
Michal Vasko6a027db2024-02-21 09:55:34 +0100167 assert_int_equal(lyd_new_term(NULL, mod, "foo", "25", LYD_NEW_VAL_BIN, &node), LY_EINVAL);
stewegd4cde642024-02-21 08:34:16 +0100168 CHECK_LOG_CTX("Invalid argument !(options & 0x04) (lyd_new_term()).", NULL, 0);
Michal Vasko6a027db2024-02-21 09:55:34 +0100169 assert_int_equal(lyd_new_term_bin(NULL, mod, "foo", "25", 2, LYD_NEW_VAL_BIN, &node), LY_SUCCESS);
stewegd4cde642024-02-21 08:34:16 +0100170 lyd_free_tree(node);
171 assert_int_equal(lyd_new_term_bin(NULL, mod, "foo", "25", 2, LYD_NEW_VAL_STORE_ONLY, &node), LY_EINVAL);
172 CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (_lyd_new_term()).", NULL, 0);
173
Michal Vasko6a027db2024-02-21 09:55:34 +0100174 assert_int_equal(lyd_new_term(NULL, mod, "foo", "25", LYD_NEW_VAL_CANON, &node), LY_SUCCESS);
stewegd4cde642024-02-21 08:34:16 +0100175 lyd_free_tree(node);
Michal Vasko6a027db2024-02-21 09:55:34 +0100176 assert_int_equal(lyd_new_term(NULL, mod, "foo", "25", LYD_NEW_VAL_CANON | LYD_NEW_VAL_STORE_ONLY, &node), LY_EINVAL);
stewegd4cde642024-02-21 08:34:16 +0100177 CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (_lyd_new_term()).", NULL, 0);
178
Michal Vasko004d3152020-06-11 19:59:22 +0200179 /* leaf-list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100180 assert_int_equal(lyd_new_term(NULL, mod, "ll", "ahoy", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200181 lyd_free_tree(node);
182
183 /* container */
Radek Krejci41ac9942020-11-02 14:47:56 +0100184 assert_int_equal(lyd_new_inner(NULL, mod, "c", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200185 lyd_free_tree(node);
186
Radek Krejci41ac9942020-11-02 14:47:56 +0100187 assert_int_equal(lyd_new_inner(NULL, mod, "l1", 0, &node), LY_ENOTFOUND);
Michal Vasko7a266772024-01-23 11:02:38 +0100188 CHECK_LOG_CTX("Inner node (container, notif, RPC, or action) \"l1\" not found.", NULL, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200189
Radek Krejci41ac9942020-11-02 14:47:56 +0100190 assert_int_equal(lyd_new_inner(NULL, mod, "l2", 0, &node), LY_ENOTFOUND);
Michal Vasko7a266772024-01-23 11:02:38 +0100191 CHECK_LOG_CTX("Inner node (container, notif, RPC, or action) \"l2\" not found.", NULL, 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200192
193 /* anydata */
Michal Vasko6a027db2024-02-21 09:55:34 +0100194 assert_int_equal(lyd_new_any(NULL, mod, "any", "{\"node\":\"val\"}", LYD_ANYDATA_STRING, 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200195 lyd_free_tree(node);
Michal Vasko6a027db2024-02-21 09:55:34 +0100196 assert_int_equal(lyd_new_any(NULL, mod, "any", "<node>val</node>", LYD_ANYDATA_STRING, 0, &node), LY_SUCCESS);
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100197 lyd_free_tree(node);
Michal Vasko004d3152020-06-11 19:59:22 +0200198
199 /* key-less list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100200 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "[a='a'][b='b']", 0, &node), LY_EVALID);
Michal Vasko7a266772024-01-23 11:02:38 +0100201 CHECK_LOG_CTX("List predicate defined for keyless list \"l2\" in path.", "/a:l2", 0);
Michal Vasko004d3152020-06-11 19:59:22 +0200202
Radek Krejci41ac9942020-11-02 14:47:56 +0100203 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200204 lyd_free_tree(node);
205
Radek Krejci41ac9942020-11-02 14:47:56 +0100206 assert_int_equal(lyd_new_list2(NULL, mod, "l2", NULL, 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200207 lyd_free_tree(node);
208
Radek Krejci41ac9942020-11-02 14:47:56 +0100209 assert_int_equal(lyd_new_list(NULL, mod, "l2", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200210 lyd_free_tree(node);
211
Radek Krejci41ac9942020-11-02 14:47:56 +0100212 /* RPC */
213 assert_int_equal(lyd_new_inner(NULL, mod, "oper", 0, &rpc), LY_SUCCESS);
214 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 0, &node), LY_SUCCESS);
215 assert_int_equal(LY_TYPE_STRING, ((struct lysc_node_leaf *)node->schema)->type->basetype);
stewegd4cde642024-02-21 08:34:16 +0100216 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", LYD_NEW_VAL_OUTPUT, &node), LY_SUCCESS);
Radek Krejci41ac9942020-11-02 14:47:56 +0100217 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf *)node->schema)->type->basetype);
218 lyd_free_tree(rpc);
Michal Vasko004d3152020-06-11 19:59:22 +0200219}
220
Michal Vasko00cbf532020-06-15 13:58:47 +0200221static void
222test_opaq(void **state)
223{
Michal Vasko00cbf532020-06-15 13:58:47 +0200224 struct lyd_node *root, *node;
225 struct lyd_node_opaq *opq;
226
Radek Iša56ca9e42020-09-08 18:42:00 +0200227 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL);
228
Michal Vasko0ab974d2021-02-24 13:18:26 +0100229 assert_int_equal(lyd_new_opaq(NULL, UTEST_LYCTX, "node1", NULL, NULL, "my-module", &root), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200230 assert_null(root->schema);
231 opq = (struct lyd_node_opaq *)root;
Michal Vaskoad92b672020-11-12 13:11:31 +0100232 assert_string_equal(opq->name.name, "node1");
233 assert_string_equal(opq->name.module_name, "my-module");
Michal Vasko501af032020-11-11 20:27:44 +0100234 assert_string_equal(opq->value, "");
Michal Vasko00cbf532020-06-15 13:58:47 +0200235
Michal Vasko0ab974d2021-02-24 13:18:26 +0100236 assert_int_equal(lyd_new_opaq(root, NULL, "node2", "value", NULL, "my-module2", &node), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200237 assert_null(node->schema);
238 opq = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100239 assert_string_equal(opq->name.name, "node2");
240 assert_string_equal(opq->name.module_name, "my-module2");
Michal Vasko501af032020-11-11 20:27:44 +0100241 assert_string_equal(opq->value, "value");
Michal Vasko00cbf532020-06-15 13:58:47 +0200242 assert_ptr_equal(opq->parent, root);
243
244 lyd_free_tree(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200245}
246
247static void
248test_path(void **state)
249{
Michal Vasko00cbf532020-06-15 13:58:47 +0200250 LY_ERR ret;
251 struct lyd_node *root, *node, *parent;
Michal Vasko4de7d072021-07-09 09:13:18 +0200252 struct lys_module *mod;
Michal Vaskoe78faec2021-04-08 17:24:43 +0200253 char *str;
Michal Vasko00cbf532020-06-15 13:58:47 +0200254
Michal Vaskoe78faec2021-04-08 17:24:43 +0200255 UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, &mod);
Radek Iša56ca9e42020-09-08 18:42:00 +0200256
Michal Vasko00cbf532020-06-15 13:58:47 +0200257 /* create 2 nodes */
Radek Krejci09c77442021-04-26 11:10:34 +0200258 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c/x[.='val']", "vvv", 0, 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200259 assert_int_equal(ret, LY_SUCCESS);
260 assert_non_null(root);
261 assert_string_equal(root->schema->name, "c");
262 assert_non_null(node);
263 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200264 assert_string_equal("val", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200265
266 /* append another */
Radek Krejci09c77442021-04-26 11:10:34 +0200267 ret = lyd_new_path2(root, NULL, "/a:c/x", "val2", 0, 0, 0, &parent, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200268 assert_int_equal(ret, LY_SUCCESS);
269 assert_ptr_equal(parent, node);
270 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200271 assert_string_equal("val2", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200272
273 /* and a last one */
Radek Krejci09c77442021-04-26 11:10:34 +0200274 ret = lyd_new_path2(root, NULL, "x", "val3", 0, 0, 0, &parent, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200275 assert_int_equal(ret, LY_SUCCESS);
276 assert_ptr_equal(parent, node);
277 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200278 assert_string_equal("val3", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200279
280 lyd_free_tree(root);
281
282 /* try LYD_NEWOPT_OPAQ */
Radek Krejci09c77442021-04-26 11:10:34 +0200283 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:l1", NULL, 0, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200284 assert_int_equal(ret, LY_EINVAL);
Michal Vasko7a266772024-01-23 11:02:38 +0100285 CHECK_LOG_CTX("Predicate missing for list \"l1\" in path \"/a:l1\".", "/a:l1", 0);
Michal Vasko00cbf532020-06-15 13:58:47 +0200286
Radek Krejci09c77442021-04-26 11:10:34 +0200287 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 +0200288 assert_int_equal(ret, LY_SUCCESS);
289 assert_non_null(root);
290 assert_null(root->schema);
291
292 lyd_free_tree(root);
293
Radek Krejci09c77442021-04-26 11:10:34 +0200294 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:foo", NULL, 0, 0, 0, NULL, NULL);
Michal Vasko00cbf532020-06-15 13:58:47 +0200295 assert_int_equal(ret, LY_EVALID);
Michal Vasko7a266772024-01-23 11:02:38 +0100296 CHECK_LOG_CTX("Invalid type uint16 empty value.", "/a:foo", 0);
Michal Vasko00cbf532020-06-15 13:58:47 +0200297
Radek Krejci09c77442021-04-26 11:10:34 +0200298 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 +0200299 assert_int_equal(ret, LY_SUCCESS);
300 assert_non_null(root);
301 assert_null(root->schema);
302
303 lyd_free_tree(root);
304
Michal Vasko106f0862021-11-02 11:49:27 +0100305 ret = lyd_new_path(NULL, UTEST_LYCTX, "/a:l11", NULL, LYD_NEW_PATH_OPAQ, &root);
306 assert_int_equal(ret, LY_SUCCESS);
307 assert_non_null(root);
308 assert_null(root->schema);
309
310 ret = lyd_new_path(root, NULL, "a", NULL, LYD_NEW_PATH_OPAQ, NULL);
311 assert_int_equal(ret, LY_SUCCESS);
312 assert_non_null(lyd_child(root));
313 assert_null(lyd_child(root)->schema);
314
315 ret = lyd_new_path(root, NULL, "b", NULL, LYD_NEW_PATH_OPAQ, NULL);
316 assert_int_equal(ret, LY_SUCCESS);
317 assert_non_null(lyd_child(root)->next);
318 assert_null(lyd_child(root)->next->schema);
319
320 lyd_free_tree(root);
321
Michal Vaskoe78faec2021-04-08 17:24:43 +0200322 /* key-less list */
Radek Krejci09c77442021-04-26 11:10:34 +0200323 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:c2/l3/x", "val1", 0, 0, 0, &root, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200324 assert_int_equal(ret, LY_SUCCESS);
325 assert_non_null(root);
326 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200327 assert_string_equal("val1", lyd_get_value(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200328
Radek Krejci09c77442021-04-26 11:10:34 +0200329 ret = lyd_new_path2(root, NULL, "/a:c2/l3[1]", NULL, 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200330 assert_int_equal(ret, LY_EEXIST);
Michal Vasko7a266772024-01-23 11:02:38 +0100331 CHECK_LOG_CTX("Path \"/a:c2/l3[1]\" already exists.", "/a:c2/l3[1]", 0);
Michal Vasko00cbf532020-06-15 13:58:47 +0200332
Radek Krejci09c77442021-04-26 11:10:34 +0200333 ret = lyd_new_path2(root, NULL, "/a:c2/l3[2]/x", "val2", 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200334 assert_int_equal(ret, LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200335
Radek Krejci09c77442021-04-26 11:10:34 +0200336 ret = lyd_new_path2(root, NULL, "/a:c2/l3/x", "val3", 0, 0, 0, NULL, &node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200337 assert_int_equal(ret, LY_SUCCESS);
338 assert_non_null(node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200339
Radek Krejci09c77442021-04-26 11:10:34 +0200340 ret = lyd_new_path2(root, NULL, "/a:c2/l3[4]/x", "empty", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200341 assert_int_equal(ret, LY_SUCCESS);
342 assert_non_null(node);
343
Radek Krejci09c77442021-04-26 11:10:34 +0200344 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 +0200345 assert_int_equal(ret, LY_SUCCESS);
346 assert_non_null(node);
347
Radek Krejci09c77442021-04-26 11:10:34 +0200348 ret = lyd_new_path2(root, NULL, "/a:c2/l3[5]/x", "val5", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200349 assert_int_equal(ret, LY_SUCCESS);
350 assert_non_null(node);
351
Radek Krejci09c77442021-04-26 11:10:34 +0200352 ret = lyd_new_path2(root, NULL, "/a:c2/l3[6]/x", "val6", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200353 assert_int_equal(ret, LY_SUCCESS);
354 assert_non_null(node);
355
356 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
357 assert_string_equal(str,
358 "<c2 xmlns=\"urn:tests:a\">\n"
359 " <l3>\n"
360 " <x>val1</x>\n"
361 " </l3>\n"
362 " <l3>\n"
363 " <x>val2</x>\n"
364 " </l3>\n"
365 " <l3>\n"
366 " <x>val3</x>\n"
367 " </l3>\n"
368 " <l3>\n"
369 " <x>val4</x>\n"
370 " </l3>\n"
371 " <l3>\n"
372 " <x>val5</x>\n"
373 " </l3>\n"
374 " <l3>\n"
375 " <x>val6</x>\n"
376 " </l3>\n"
377 "</c2>\n");
378 free(str);
379 lyd_free_siblings(root);
380
381 /* state leaf-list */
Radek Krejci09c77442021-04-26 11:10:34 +0200382 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:ll2", "val_first", 0, 0, 0, &root, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200383 assert_int_equal(ret, LY_SUCCESS);
384 assert_non_null(root);
385 assert_string_equal(node->schema->name, "ll2");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200386 assert_string_equal("val_first", lyd_get_value(node));
Michal Vaskoe78faec2021-04-08 17:24:43 +0200387
Radek Krejci09c77442021-04-26 11:10:34 +0200388 ret = lyd_new_path2(root, NULL, "/a:ll2[1]", "", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200389 assert_int_equal(ret, LY_EEXIST);
Michal Vasko7a266772024-01-23 11:02:38 +0100390 CHECK_LOG_CTX("Path \"/a:ll2[1]\" already exists.", "/a:ll2[1]", 0);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200391
Radek Krejci09c77442021-04-26 11:10:34 +0200392 ret = lyd_new_path2(root, NULL, "/a:ll2[2]", "val2", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200393 assert_int_equal(ret, LY_SUCCESS);
394
Radek Krejci09c77442021-04-26 11:10:34 +0200395 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 +0200396 assert_int_equal(ret, LY_SUCCESS);
397 assert_non_null(node);
398
Radek Krejci09c77442021-04-26 11:10:34 +0200399 ret = lyd_new_path2(root, UTEST_LYCTX, "/a:ll2", "val3", 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200400 assert_int_equal(ret, LY_SUCCESS);
401 assert_non_null(node);
402
Radek Krejci09c77442021-04-26 11:10:34 +0200403 ret = lyd_new_path2(root, NULL, "/a:ll2[3][.='val3']", NULL, 0, 0, 0, NULL, &node);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200404 assert_int_equal(ret, LY_EVALID);
Michal Vasko7a266772024-01-23 11:02:38 +0100405 CHECK_LOG_CTX("Unparsed characters \"[.='val3']\" left at the end of path.", NULL, 0);
Michal Vaskoe78faec2021-04-08 17:24:43 +0200406
407 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
408 assert_string_equal(str,
409 "<ll2 xmlns=\"urn:tests:a\">val</ll2>\n"
410 "<ll2 xmlns=\"urn:tests:a\">val2</ll2>\n"
411 "<ll2 xmlns=\"urn:tests:a\">val3</ll2>\n");
412 free(str);
413 lyd_free_siblings(root);
Michal Vaskoea0f96c2022-12-20 08:34:39 +0100414
415 /* anydata */
416 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:any", "<elem>val</elem>", 0, LYD_ANYDATA_XML, 0, &root, NULL);
417 assert_int_equal(ret, LY_SUCCESS);
418 assert_non_null(root);
419
420 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
421 assert_string_equal(str,
422 "<any xmlns=\"urn:tests:a\">\n"
423 " <elem>val</elem>\n"
424 "</any>\n");
425 free(str);
426 lyd_print_mem(&str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
427 assert_string_equal(str,
428 "{\n"
429 " \"a:any\": {\n"
430 " \"elem\": \"val\"\n"
431 " }\n"
432 "}\n");
433 free(str);
434 lyd_free_siblings(root);
Michal Vasko54bf9e22023-04-27 08:20:30 +0200435
436 /* anyxml */
437 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:anyx", "<a/><b/><c/>", 0, LYD_ANYDATA_XML, 0, &root, NULL);
438 assert_int_equal(ret, LY_SUCCESS);
439 assert_non_null(root);
440
441 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
442 assert_string_equal(str,
443 "<anyx xmlns=\"urn:tests:a\">\n"
444 " <a/>\n"
445 " <b/>\n"
446 " <c/>\n"
447 "</anyx>\n");
448 free(str);
449 lyd_print_mem(&str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
450 assert_string_equal(str,
451 "{\n"
452 " \"a:anyx\": {\n"
453 " \"a\": [null],\n"
454 " \"b\": [null],\n"
455 " \"c\": [null]\n"
456 " }\n"
457 "}\n");
458 free(str);
459 lyd_free_siblings(root);
460
461 ret = lyd_new_path2(NULL, UTEST_LYCTX, "/a:anyx", "{\"a\":[null],\"b\":[null],\"c\":[null]}", 0, LYD_ANYDATA_JSON, 0, &root, NULL);
462 assert_int_equal(ret, LY_SUCCESS);
463 assert_non_null(root);
464
465 lyd_print_mem(&str, root, LYD_XML, LYD_PRINT_WITHSIBLINGS);
466 assert_string_equal(str,
467 "<anyx xmlns=\"urn:tests:a\">\n"
468 " <a/>\n"
469 " <b/>\n"
470 " <c/>\n"
471 "</anyx>\n");
472 free(str);
473 lyd_print_mem(&str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
474 assert_string_equal(str,
475 "{\n"
476 " \"a:anyx\": {\n"
477 " \"a\": [null],\n"
478 " \"b\": [null],\n"
479 " \"c\": [null]\n"
480 " }\n"
481 "}\n");
482 free(str);
483 lyd_free_siblings(root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200484}
485
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100486static void
487test_path_ext(void **state)
488{
489 LY_ERR ret;
490 struct lyd_node *root, *node;
Michal Vasko4de7d072021-07-09 09:13:18 +0200491 struct lys_module *mod;
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100492 const char *mod_str = "module ext {yang-version 1.1; namespace urn:tests:extensions:ext; prefix e;"
493 "import ietf-restconf {revision-date 2017-01-26; prefix rc;}"
494 "rc:yang-data template {container c {leaf x {type string;} leaf y {type string;} leaf z {type string;}}}}";
495
496 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
497 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-restconf", "2017-01-26", NULL));
498
499 UTEST_ADD_MODULE(mod_str, LYS_IN_YANG, NULL, &mod);
500
501 /* create x */
502 ret = lyd_new_ext_path(NULL, &mod->compiled->exts[0], "/ext:c/x", "xxx", 0, &root);
503 assert_int_equal(ret, LY_SUCCESS);
504 assert_non_null(root);
505 assert_string_equal(root->schema->name, "c");
506 assert_non_null(node = lyd_child(root));
507 assert_string_equal(node->schema->name, "x");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200508 assert_string_equal("xxx", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100509
510 /* append y */
511 ret = lyd_new_ext_path(root, &mod->compiled->exts[0], "/ext:c/y", "yyy", 0, &node);
512 assert_int_equal(ret, LY_SUCCESS);
513 assert_string_equal(node->schema->name, "y");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200514 assert_string_equal("yyy", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100515
516 /* append z */
517 ret = lyd_new_path(root, NULL, "ext:z", "zzz", 0, &node);
518 assert_int_equal(ret, LY_SUCCESS);
519 assert_string_equal(node->schema->name, "z");
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200520 assert_string_equal("zzz", lyd_get_value(node));
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100521
522 lyd_free_tree(root);
523}
524
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100525int
526main(void)
Michal Vasko004d3152020-06-11 19:59:22 +0200527{
528 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200529 UTEST(test_top_level),
530 UTEST(test_opaq),
531 UTEST(test_path),
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100532 UTEST(test_path_ext),
Michal Vasko004d3152020-06-11 19:59:22 +0200533 };
534
535 return cmocka_run_group_tests(tests, NULL, NULL);
536}