blob: a640dc8949c438172171aa35d67f5a59d0e19ddd [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 */
14
15#include <stdarg.h>
16#include <stddef.h>
17#include <stdio.h>
18#include <setjmp.h>
19#include <cmocka.h>
20
Radek Krejci70593c12020-06-13 20:48:09 +020021#include "libyang.h"
Michal Vasko004d3152020-06-11 19:59:22 +020022
23#define BUFSIZE 1024
24char logbuf[BUFSIZE] = {0};
25int store = -1; /* negative for infinite logging, positive for limited logging */
26
27struct ly_ctx *ctx; /* context for tests */
28
29/* set to 0 to printing error messages to stderr instead of checking them in code */
30#define ENABLE_LOGGER_CHECKING 1
31
32#if ENABLE_LOGGER_CHECKING
33static void
34logger(LY_LOG_LEVEL level, const char *msg, const char *path)
35{
36 (void) level; /* unused */
37 if (store) {
38 if (path && path[0]) {
39 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
40 } else {
41 strncpy(logbuf, msg, BUFSIZE - 1);
42 }
43 if (store > 0) {
44 --store;
45 }
46 }
47}
48#endif
49
50static int
51setup(void **state)
52{
53 (void) state; /* unused */
54
55 const char *schema_a = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;"
56 "list l1 { key \"a b\"; leaf a {type string;} leaf b {type string;} leaf c {type string;}}"
57 "leaf foo { type uint16;}"
58 "leaf-list ll { type string;}"
59 "container c {leaf-list x {type string;}}"
60 "anydata any {config false;}"
61 "list l2 {config false; container c{leaf x {type string;}}}}";
62
63#if ENABLE_LOGGER_CHECKING
64 ly_set_log_clb(logger, 1);
65#endif
66
67 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
Michal Vasko3a41dff2020-07-15 14:30:28 +020068 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_a, LYS_IN_YANG, NULL));
Michal Vasko004d3152020-06-11 19:59:22 +020069
70 return 0;
71}
72
73static int
74teardown(void **state)
75{
76#if ENABLE_LOGGER_CHECKING
77 if (*state) {
78 fprintf(stderr, "%s\n", logbuf);
79 }
80#else
81 (void) state; /* unused */
82#endif
83
84 ly_ctx_destroy(ctx, NULL);
85 ctx = NULL;
86
87 return 0;
88}
89
90void
91logbuf_clean(void)
92{
93 logbuf[0] = '\0';
94}
95
96#if ENABLE_LOGGER_CHECKING
97# define logbuf_assert(str) assert_string_equal(logbuf, str)
98#else
99# define logbuf_assert(str)
100#endif
101
102static void
103test_top_level(void **state)
104{
105 *state = test_top_level;
106
107 const struct lys_module *mod;
108 struct lyd_node *node;
109
110 /* we need the module first */
111 mod = ly_ctx_get_module_implemented(ctx, "a");
112 assert_non_null(mod);
113
114 /* list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200115 assert_int_equal(lyd_new_list(NULL, mod, "l1", &node, "val_a", "val_b"), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200116 lyd_free_tree(node);
117
Michal Vasko3a41dff2020-07-15 14:30:28 +0200118 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[]", &node), LY_EVALID);
Michal Vasko0b468e62020-10-19 09:33:04 +0200119 logbuf_assert("Unexpected XPath token \"]\" (\"]\").");
Michal Vasko004d3152020-06-11 19:59:22 +0200120
Michal Vasko6027eb92020-07-15 16:37:30 +0200121 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[key1='a'][key2='b']", &node), LY_ENOTFOUND);
Michal Vasko004d3152020-06-11 19:59:22 +0200122 logbuf_assert("Not found node \"key1\" in path.");
123
Michal Vasko6027eb92020-07-15 16:37:30 +0200124 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b'][c='c']", &node), LY_EVALID);
Michal Vasko004d3152020-06-11 19:59:22 +0200125 logbuf_assert("Key expected instead of leaf \"c\" in path. /a:l1/c");
126
Michal Vasko6027eb92020-07-15 16:37:30 +0200127 assert_int_equal(lyd_new_list2(NULL, mod, "c", "[a='a'][b='b']", &node), LY_ENOTFOUND);
Michal Vasko004d3152020-06-11 19:59:22 +0200128 logbuf_assert("List node \"c\" not found.");
129
Michal Vasko3a41dff2020-07-15 14:30:28 +0200130 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b']", &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200131 lyd_free_tree(node);
132
Michal Vasko3a41dff2020-07-15 14:30:28 +0200133 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a=''][b='']", &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200134 lyd_free_tree(node);
135
Michal Vasko3a41dff2020-07-15 14:30:28 +0200136 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a:a='a'][a:b='b']", &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200137 lyd_free_tree(node);
138
Michal Vasko3a41dff2020-07-15 14:30:28 +0200139 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a= 'a']\n[b =\t'b']", &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200140 lyd_free_tree(node);
141
142 /* leaf */
Michal Vasko6027eb92020-07-15 16:37:30 +0200143 assert_int_equal(lyd_new_term(NULL, mod, "foo", "[a='a'][b='b'][c='c']", &node), LY_EVALID);
Michal Vasko004d3152020-06-11 19:59:22 +0200144 logbuf_assert("Invalid uint16 value \"[a='a'][b='b'][c='c']\". /a:foo");
145
Michal Vasko6027eb92020-07-15 16:37:30 +0200146 assert_int_equal(lyd_new_term(NULL, mod, "c", "value", &node), LY_ENOTFOUND);
Michal Vasko004d3152020-06-11 19:59:22 +0200147 logbuf_assert("Term node \"c\" not found.");
148
Michal Vasko3a41dff2020-07-15 14:30:28 +0200149 assert_int_equal(lyd_new_term(NULL, mod, "foo", "256", &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200150 lyd_free_tree(node);
151
152 /* leaf-list */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200153 assert_int_equal(lyd_new_term(NULL, mod, "ll", "ahoy", &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200154 lyd_free_tree(node);
155
156 /* container */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200157 assert_int_equal(lyd_new_inner(NULL, mod, "c", &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200158 lyd_free_tree(node);
159
Michal Vasko6027eb92020-07-15 16:37:30 +0200160 assert_int_equal(lyd_new_inner(NULL, mod, "l1", &node), LY_ENOTFOUND);
Michal Vasko004d3152020-06-11 19:59:22 +0200161 logbuf_assert("Inner node (and not a list) \"l1\" not found.");
162
Michal Vasko6027eb92020-07-15 16:37:30 +0200163 assert_int_equal(lyd_new_inner(NULL, mod, "l2", &node), LY_ENOTFOUND);
Michal Vasko004d3152020-06-11 19:59:22 +0200164 logbuf_assert("Inner node (and not a list) \"l2\" not found.");
165
166 /* anydata */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200167 assert_int_equal(lyd_new_any(NULL, mod, "any", "some-value", LYD_ANYDATA_STRING, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200168 lyd_free_tree(node);
169
170 /* key-less list */
Michal Vasko6027eb92020-07-15 16:37:30 +0200171 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "[a='a'][b='b']", &node), LY_EVALID);
Michal Vasko004d3152020-06-11 19:59:22 +0200172 logbuf_assert("List predicate defined for keyless list \"l2\" in path.");
173
Michal Vasko3a41dff2020-07-15 14:30:28 +0200174 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "", &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200175 lyd_free_tree(node);
176
Michal Vasko3a41dff2020-07-15 14:30:28 +0200177 assert_int_equal(lyd_new_list2(NULL, mod, "l2", NULL, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200178 lyd_free_tree(node);
179
Michal Vasko3a41dff2020-07-15 14:30:28 +0200180 assert_int_equal(lyd_new_list(NULL, mod, "l2", &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200181 lyd_free_tree(node);
182
183 *state = NULL;
184}
185
Michal Vasko00cbf532020-06-15 13:58:47 +0200186static void
187test_opaq(void **state)
188{
189 *state = test_opaq;
190
191 struct lyd_node *root, *node;
192 struct lyd_node_opaq *opq;
193
Michal Vasko3a41dff2020-07-15 14:30:28 +0200194 assert_int_equal(lyd_new_opaq(NULL, ctx, "node1", NULL, "my-module", &root), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200195 assert_null(root->schema);
196 opq = (struct lyd_node_opaq *)root;
197 assert_string_equal(opq->name, "node1");
198 assert_string_equal(opq->value, "");
Radek Krejci1798aae2020-07-14 13:26:06 +0200199 assert_string_equal(opq->prefix.module_name, "my-module");
Michal Vasko00cbf532020-06-15 13:58:47 +0200200
Michal Vasko3a41dff2020-07-15 14:30:28 +0200201 assert_int_equal(lyd_new_opaq(root, NULL, "node2", "value", "my-module2", &node), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200202 assert_null(node->schema);
203 opq = (struct lyd_node_opaq *)node;
204 assert_string_equal(opq->name, "node2");
205 assert_string_equal(opq->value, "value");
Radek Krejci1798aae2020-07-14 13:26:06 +0200206 assert_string_equal(opq->prefix.module_name, "my-module2");
Michal Vasko00cbf532020-06-15 13:58:47 +0200207 assert_ptr_equal(opq->parent, root);
208
209 lyd_free_tree(root);
210
211 *state = NULL;
212}
213
214static void
215test_path(void **state)
216{
217 *state = test_path;
218
219 LY_ERR ret;
220 struct lyd_node *root, *node, *parent;
Michal Vasko00cbf532020-06-15 13:58:47 +0200221
222 /* create 2 nodes */
223 ret = lyd_new_path2(NULL, ctx, "/a:c/x[.='val']", "vvv", 0, 0, &root, &node);
224 assert_int_equal(ret, LY_SUCCESS);
225 assert_non_null(root);
226 assert_string_equal(root->schema->name, "c");
227 assert_non_null(node);
228 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200229 assert_string_equal("val", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200230
231 /* append another */
232 ret = lyd_new_path2(root, NULL, "/a:c/x", "val2", 0, 0, &parent, &node);
233 assert_int_equal(ret, LY_SUCCESS);
234 assert_ptr_equal(parent, node);
235 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200236 assert_string_equal("val2", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200237
238 /* and a last one */
239 ret = lyd_new_path2(root, NULL, "x", "val3", 0, 0, &parent, &node);
240 assert_int_equal(ret, LY_SUCCESS);
241 assert_ptr_equal(parent, node);
242 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200243 assert_string_equal("val3", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200244
245 lyd_free_tree(root);
246
247 /* try LYD_NEWOPT_OPAQ */
248 ret = lyd_new_path2(NULL, ctx, "/a:l1", NULL, 0, 0, NULL, NULL);
249 assert_int_equal(ret, LY_EINVAL);
250 logbuf_assert("Predicate missing for list \"l1\" in path.");
251
Radek Krejci092e33c2020-10-12 15:33:10 +0200252 ret = lyd_new_path2(NULL, ctx, "/a:l1", NULL, 0, LYD_NEW_PATH_OPAQ, NULL, &root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200253 assert_int_equal(ret, LY_SUCCESS);
254 assert_non_null(root);
255 assert_null(root->schema);
256
257 lyd_free_tree(root);
258
259 ret = lyd_new_path2(NULL, ctx, "/a:foo", NULL, 0, 0, NULL, NULL);
260 assert_int_equal(ret, LY_EVALID);
261 logbuf_assert("Invalid empty uint16 value. /a:foo");
262
Radek Krejci092e33c2020-10-12 15:33:10 +0200263 ret = lyd_new_path2(NULL, ctx, "/a:foo", NULL, 0, LYD_NEW_PATH_OPAQ, NULL, &root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200264 assert_int_equal(ret, LY_SUCCESS);
265 assert_non_null(root);
266 assert_null(root->schema);
267
268 lyd_free_tree(root);
269
270 /* try LYD_NEWOPT_UPDATE */
271 ret = lyd_new_path2(NULL, ctx, "/a:l2[1]/c/x", "val", 0, 0, &root, &node);
272 assert_int_equal(ret, LY_SUCCESS);
273 assert_non_null(root);
274 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200275 assert_string_equal("val", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200276
277 ret = lyd_new_path2(root, NULL, "/a:l2[1]/c/x", "val", 0, 0, NULL, &node);
278 assert_int_equal(ret, LY_EEXIST);
279
Radek Krejci092e33c2020-10-12 15:33:10 +0200280 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 +0200281 assert_int_equal(ret, LY_SUCCESS);
282 assert_null(node);
283
Radek Krejci092e33c2020-10-12 15:33:10 +0200284 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 +0200285 assert_int_equal(ret, LY_SUCCESS);
286 assert_non_null(node);
287 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200288 assert_string_equal("val2", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200289
290 lyd_free_tree(root);
291
292 *state = NULL;
293}
294
Michal Vasko004d3152020-06-11 19:59:22 +0200295int main(void)
296{
297 const struct CMUnitTest tests[] = {
298 cmocka_unit_test_setup_teardown(test_top_level, setup, teardown),
Michal Vasko00cbf532020-06-15 13:58:47 +0200299 cmocka_unit_test_setup_teardown(test_opaq, setup, teardown),
300 cmocka_unit_test_setup_teardown(test_path, setup, teardown),
Michal Vasko004d3152020-06-11 19:59:22 +0200301 };
302
303 return cmocka_run_group_tests(tests, NULL, NULL);
304}