blob: c1fec03b3c044dc00b171e2f5e0f9f3e05213460 [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;}"
Radek Krejci41ac9942020-11-02 14:47:56 +010061 "list l2 {config false; container c{leaf x {type string;}}}"
62 "rpc oper {input {leaf param {type string;}} output {leaf param {type int8;}}}}";
Michal Vasko004d3152020-06-11 19:59:22 +020063
64#if ENABLE_LOGGER_CHECKING
65 ly_set_log_clb(logger, 1);
66#endif
67
68 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
Michal Vasko3a41dff2020-07-15 14:30:28 +020069 assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_a, LYS_IN_YANG, NULL));
Michal Vasko004d3152020-06-11 19:59:22 +020070
71 return 0;
72}
73
74static int
75teardown(void **state)
76{
77#if ENABLE_LOGGER_CHECKING
78 if (*state) {
79 fprintf(stderr, "%s\n", logbuf);
80 }
81#else
82 (void) state; /* unused */
83#endif
84
85 ly_ctx_destroy(ctx, NULL);
86 ctx = NULL;
87
88 return 0;
89}
90
91void
92logbuf_clean(void)
93{
94 logbuf[0] = '\0';
95}
96
97#if ENABLE_LOGGER_CHECKING
98# define logbuf_assert(str) assert_string_equal(logbuf, str)
99#else
100# define logbuf_assert(str)
101#endif
102
103static void
104test_top_level(void **state)
105{
106 *state = test_top_level;
107
108 const struct lys_module *mod;
Radek Krejci41ac9942020-11-02 14:47:56 +0100109 struct lyd_node *node, *rpc;
Michal Vasko004d3152020-06-11 19:59:22 +0200110
111 /* we need the module first */
112 mod = ly_ctx_get_module_implemented(ctx, "a");
113 assert_non_null(mod);
114
115 /* list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100116 assert_int_equal(lyd_new_list(NULL, mod, "l1", 0, &node, "val_a", "val_b"), 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", "[]", 0, &node), LY_EVALID);
Michal Vasko0b468e62020-10-19 09:33:04 +0200120 logbuf_assert("Unexpected XPath token \"]\" (\"]\").");
Michal Vasko004d3152020-06-11 19:59:22 +0200121
Radek Krejci41ac9942020-11-02 14:47:56 +0100122 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[key1='a'][key2='b']", 0, &node), LY_ENOTFOUND);
Michal Vasko004d3152020-06-11 19:59:22 +0200123 logbuf_assert("Not found node \"key1\" in path.");
124
Radek Krejci41ac9942020-11-02 14:47:56 +0100125 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID);
Michal Vasko004d3152020-06-11 19:59:22 +0200126 logbuf_assert("Key expected instead of leaf \"c\" in path. /a:l1/c");
127
Radek Krejci41ac9942020-11-02 14:47:56 +0100128 assert_int_equal(lyd_new_list2(NULL, mod, "c", "[a='a'][b='b']", 0, &node), LY_ENOTFOUND);
Michal Vasko004d3152020-06-11 19:59:22 +0200129 logbuf_assert("List node \"c\" not found.");
130
Radek Krejci41ac9942020-11-02 14:47:56 +0100131 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200132 lyd_free_tree(node);
133
Radek Krejci41ac9942020-11-02 14:47:56 +0100134 assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a=''][b='']", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200135 lyd_free_tree(node);
136
Radek Krejci41ac9942020-11-02 14:47:56 +0100137 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 +0200138 lyd_free_tree(node);
139
Radek Krejci41ac9942020-11-02 14:47:56 +0100140 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 +0200141 lyd_free_tree(node);
142
143 /* leaf */
Radek Krejci41ac9942020-11-02 14:47:56 +0100144 assert_int_equal(lyd_new_term(NULL, mod, "foo", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID);
Michal Vasko004d3152020-06-11 19:59:22 +0200145 logbuf_assert("Invalid uint16 value \"[a='a'][b='b'][c='c']\". /a:foo");
146
Radek Krejci41ac9942020-11-02 14:47:56 +0100147 assert_int_equal(lyd_new_term(NULL, mod, "c", "value", 0, &node), LY_ENOTFOUND);
Michal Vasko004d3152020-06-11 19:59:22 +0200148 logbuf_assert("Term node \"c\" not found.");
149
Radek Krejci41ac9942020-11-02 14:47:56 +0100150 assert_int_equal(lyd_new_term(NULL, mod, "foo", "256", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200151 lyd_free_tree(node);
152
153 /* leaf-list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100154 assert_int_equal(lyd_new_term(NULL, mod, "ll", "ahoy", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200155 lyd_free_tree(node);
156
157 /* container */
Radek Krejci41ac9942020-11-02 14:47:56 +0100158 assert_int_equal(lyd_new_inner(NULL, mod, "c", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200159 lyd_free_tree(node);
160
Radek Krejci41ac9942020-11-02 14:47:56 +0100161 assert_int_equal(lyd_new_inner(NULL, mod, "l1", 0, &node), LY_ENOTFOUND);
Michal Vasko004d3152020-06-11 19:59:22 +0200162 logbuf_assert("Inner node (and not a list) \"l1\" not found.");
163
Radek Krejci41ac9942020-11-02 14:47:56 +0100164 assert_int_equal(lyd_new_inner(NULL, mod, "l2", 0, &node), LY_ENOTFOUND);
Michal Vasko004d3152020-06-11 19:59:22 +0200165 logbuf_assert("Inner node (and not a list) \"l2\" not found.");
166
167 /* anydata */
Radek Krejci41ac9942020-11-02 14:47:56 +0100168 assert_int_equal(lyd_new_any(NULL, mod, "any", "some-value", LYD_ANYDATA_STRING, 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200169 lyd_free_tree(node);
170
171 /* key-less list */
Radek Krejci41ac9942020-11-02 14:47:56 +0100172 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "[a='a'][b='b']", 0, &node), LY_EVALID);
Michal Vasko004d3152020-06-11 19:59:22 +0200173 logbuf_assert("List predicate defined for keyless list \"l2\" in path.");
174
Radek Krejci41ac9942020-11-02 14:47:56 +0100175 assert_int_equal(lyd_new_list2(NULL, mod, "l2", "", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200176 lyd_free_tree(node);
177
Radek Krejci41ac9942020-11-02 14:47:56 +0100178 assert_int_equal(lyd_new_list2(NULL, mod, "l2", NULL, 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200179 lyd_free_tree(node);
180
Radek Krejci41ac9942020-11-02 14:47:56 +0100181 assert_int_equal(lyd_new_list(NULL, mod, "l2", 0, &node), LY_SUCCESS);
Michal Vasko004d3152020-06-11 19:59:22 +0200182 lyd_free_tree(node);
183
Radek Krejci41ac9942020-11-02 14:47:56 +0100184 /* RPC */
185 assert_int_equal(lyd_new_inner(NULL, mod, "oper", 0, &rpc), LY_SUCCESS);
186 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 0, &node), LY_SUCCESS);
187 assert_int_equal(LY_TYPE_STRING, ((struct lysc_node_leaf *)node->schema)->type->basetype);
188 assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 1, &node), LY_SUCCESS);
189 assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf *)node->schema)->type->basetype);
190 lyd_free_tree(rpc);
191
Michal Vasko004d3152020-06-11 19:59:22 +0200192 *state = NULL;
193}
194
Michal Vasko00cbf532020-06-15 13:58:47 +0200195static void
196test_opaq(void **state)
197{
198 *state = test_opaq;
199
200 struct lyd_node *root, *node;
201 struct lyd_node_opaq *opq;
202
Michal Vasko3a41dff2020-07-15 14:30:28 +0200203 assert_int_equal(lyd_new_opaq(NULL, ctx, "node1", NULL, "my-module", &root), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200204 assert_null(root->schema);
205 opq = (struct lyd_node_opaq *)root;
206 assert_string_equal(opq->name, "node1");
Radek Krejci1798aae2020-07-14 13:26:06 +0200207 assert_string_equal(opq->prefix.module_name, "my-module");
Michal Vasko501af032020-11-11 20:27:44 +0100208 assert_string_equal(opq->value, "");
Michal Vasko00cbf532020-06-15 13:58:47 +0200209
Michal Vasko3a41dff2020-07-15 14:30:28 +0200210 assert_int_equal(lyd_new_opaq(root, NULL, "node2", "value", "my-module2", &node), LY_SUCCESS);
Michal Vasko00cbf532020-06-15 13:58:47 +0200211 assert_null(node->schema);
212 opq = (struct lyd_node_opaq *)node;
213 assert_string_equal(opq->name, "node2");
Radek Krejci1798aae2020-07-14 13:26:06 +0200214 assert_string_equal(opq->prefix.module_name, "my-module2");
Michal Vasko501af032020-11-11 20:27:44 +0100215 assert_string_equal(opq->value, "value");
Michal Vasko00cbf532020-06-15 13:58:47 +0200216 assert_ptr_equal(opq->parent, root);
217
218 lyd_free_tree(root);
219
220 *state = NULL;
221}
222
223static void
224test_path(void **state)
225{
226 *state = test_path;
227
228 LY_ERR ret;
229 struct lyd_node *root, *node, *parent;
Michal Vasko00cbf532020-06-15 13:58:47 +0200230
231 /* create 2 nodes */
232 ret = lyd_new_path2(NULL, ctx, "/a:c/x[.='val']", "vvv", 0, 0, &root, &node);
233 assert_int_equal(ret, LY_SUCCESS);
234 assert_non_null(root);
235 assert_string_equal(root->schema->name, "c");
236 assert_non_null(node);
237 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200238 assert_string_equal("val", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200239
240 /* append another */
241 ret = lyd_new_path2(root, NULL, "/a:c/x", "val2", 0, 0, &parent, &node);
242 assert_int_equal(ret, LY_SUCCESS);
243 assert_ptr_equal(parent, node);
244 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200245 assert_string_equal("val2", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200246
247 /* and a last one */
248 ret = lyd_new_path2(root, NULL, "x", "val3", 0, 0, &parent, &node);
249 assert_int_equal(ret, LY_SUCCESS);
250 assert_ptr_equal(parent, node);
251 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200252 assert_string_equal("val3", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200253
254 lyd_free_tree(root);
255
256 /* try LYD_NEWOPT_OPAQ */
257 ret = lyd_new_path2(NULL, ctx, "/a:l1", NULL, 0, 0, NULL, NULL);
258 assert_int_equal(ret, LY_EINVAL);
259 logbuf_assert("Predicate missing for list \"l1\" in path.");
260
Radek Krejci092e33c2020-10-12 15:33:10 +0200261 ret = lyd_new_path2(NULL, ctx, "/a:l1", NULL, 0, LYD_NEW_PATH_OPAQ, NULL, &root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200262 assert_int_equal(ret, LY_SUCCESS);
263 assert_non_null(root);
264 assert_null(root->schema);
265
266 lyd_free_tree(root);
267
268 ret = lyd_new_path2(NULL, ctx, "/a:foo", NULL, 0, 0, NULL, NULL);
269 assert_int_equal(ret, LY_EVALID);
270 logbuf_assert("Invalid empty uint16 value. /a:foo");
271
Radek Krejci092e33c2020-10-12 15:33:10 +0200272 ret = lyd_new_path2(NULL, ctx, "/a:foo", NULL, 0, LYD_NEW_PATH_OPAQ, NULL, &root);
Michal Vasko00cbf532020-06-15 13:58:47 +0200273 assert_int_equal(ret, LY_SUCCESS);
274 assert_non_null(root);
275 assert_null(root->schema);
276
277 lyd_free_tree(root);
278
279 /* try LYD_NEWOPT_UPDATE */
280 ret = lyd_new_path2(NULL, ctx, "/a:l2[1]/c/x", "val", 0, 0, &root, &node);
281 assert_int_equal(ret, LY_SUCCESS);
282 assert_non_null(root);
283 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200284 assert_string_equal("val", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200285
286 ret = lyd_new_path2(root, NULL, "/a:l2[1]/c/x", "val", 0, 0, NULL, &node);
287 assert_int_equal(ret, LY_EEXIST);
288
Radek Krejci092e33c2020-10-12 15:33:10 +0200289 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 +0200290 assert_int_equal(ret, LY_SUCCESS);
291 assert_null(node);
292
Radek Krejci092e33c2020-10-12 15:33:10 +0200293 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 +0200294 assert_int_equal(ret, LY_SUCCESS);
295 assert_non_null(node);
296 assert_string_equal(node->schema->name, "x");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200297 assert_string_equal("val2", LYD_CANON_VALUE(node));
Michal Vasko00cbf532020-06-15 13:58:47 +0200298
299 lyd_free_tree(root);
300
301 *state = NULL;
302}
303
Michal Vasko004d3152020-06-11 19:59:22 +0200304int main(void)
305{
306 const struct CMUnitTest tests[] = {
307 cmocka_unit_test_setup_teardown(test_top_level, setup, teardown),
Michal Vasko00cbf532020-06-15 13:58:47 +0200308 cmocka_unit_test_setup_teardown(test_opaq, setup, teardown),
309 cmocka_unit_test_setup_teardown(test_path, setup, teardown),
Michal Vasko004d3152020-06-11 19:59:22 +0200310 };
311
312 return cmocka_run_group_tests(tests, NULL, NULL);
313}