Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 21 | #include "libyang.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 22 | |
| 23 | #define BUFSIZE 1024 |
| 24 | char logbuf[BUFSIZE] = {0}; |
| 25 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 26 | |
| 27 | struct 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 |
| 33 | static void |
| 34 | logger(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 | |
| 50 | static int |
| 51 | setup(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 Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 68 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, schema_a, LYS_IN_YANG, NULL)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int |
| 74 | teardown(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 | |
| 90 | void |
| 91 | logbuf_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 | |
| 102 | static void |
| 103 | test_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 Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 115 | assert_int_equal(lyd_new_list(NULL, mod, "l1", &node, "val_a", "val_b"), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 116 | lyd_free_tree(node); |
| 117 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 118 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[]", &node), LY_EVALID); |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 119 | logbuf_assert("Unexpected XPath token \"]\" (\"]\")."); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 120 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 121 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[key1='a'][key2='b']", &node), LY_ENOTFOUND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 122 | logbuf_assert("Not found node \"key1\" in path."); |
| 123 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 124 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b'][c='c']", &node), LY_EVALID); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 125 | logbuf_assert("Key expected instead of leaf \"c\" in path. /a:l1/c"); |
| 126 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 127 | assert_int_equal(lyd_new_list2(NULL, mod, "c", "[a='a'][b='b']", &node), LY_ENOTFOUND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 128 | logbuf_assert("List node \"c\" not found."); |
| 129 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 130 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a='a'][b='b']", &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 131 | lyd_free_tree(node); |
| 132 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 133 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a=''][b='']", &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 134 | lyd_free_tree(node); |
| 135 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 136 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a:a='a'][a:b='b']", &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 137 | lyd_free_tree(node); |
| 138 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 139 | assert_int_equal(lyd_new_list2(NULL, mod, "l1", "[a= 'a']\n[b =\t'b']", &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 140 | lyd_free_tree(node); |
| 141 | |
| 142 | /* leaf */ |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 143 | assert_int_equal(lyd_new_term(NULL, mod, "foo", "[a='a'][b='b'][c='c']", &node), LY_EVALID); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 144 | logbuf_assert("Invalid uint16 value \"[a='a'][b='b'][c='c']\". /a:foo"); |
| 145 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 146 | assert_int_equal(lyd_new_term(NULL, mod, "c", "value", &node), LY_ENOTFOUND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 147 | logbuf_assert("Term node \"c\" not found."); |
| 148 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 149 | assert_int_equal(lyd_new_term(NULL, mod, "foo", "256", &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 150 | lyd_free_tree(node); |
| 151 | |
| 152 | /* leaf-list */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 153 | assert_int_equal(lyd_new_term(NULL, mod, "ll", "ahoy", &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 154 | lyd_free_tree(node); |
| 155 | |
| 156 | /* container */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 157 | assert_int_equal(lyd_new_inner(NULL, mod, "c", &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 158 | lyd_free_tree(node); |
| 159 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 160 | assert_int_equal(lyd_new_inner(NULL, mod, "l1", &node), LY_ENOTFOUND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 161 | logbuf_assert("Inner node (and not a list) \"l1\" not found."); |
| 162 | |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 163 | assert_int_equal(lyd_new_inner(NULL, mod, "l2", &node), LY_ENOTFOUND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 164 | logbuf_assert("Inner node (and not a list) \"l2\" not found."); |
| 165 | |
| 166 | /* anydata */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 167 | assert_int_equal(lyd_new_any(NULL, mod, "any", "some-value", LYD_ANYDATA_STRING, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 168 | lyd_free_tree(node); |
| 169 | |
| 170 | /* key-less list */ |
Michal Vasko | 6027eb9 | 2020-07-15 16:37:30 +0200 | [diff] [blame] | 171 | assert_int_equal(lyd_new_list2(NULL, mod, "l2", "[a='a'][b='b']", &node), LY_EVALID); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 172 | logbuf_assert("List predicate defined for keyless list \"l2\" in path."); |
| 173 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 174 | assert_int_equal(lyd_new_list2(NULL, mod, "l2", "", &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 175 | lyd_free_tree(node); |
| 176 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 177 | assert_int_equal(lyd_new_list2(NULL, mod, "l2", NULL, &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 178 | lyd_free_tree(node); |
| 179 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 180 | assert_int_equal(lyd_new_list(NULL, mod, "l2", &node), LY_SUCCESS); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 181 | lyd_free_tree(node); |
| 182 | |
| 183 | *state = NULL; |
| 184 | } |
| 185 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 186 | static void |
| 187 | test_opaq(void **state) |
| 188 | { |
| 189 | *state = test_opaq; |
| 190 | |
| 191 | struct lyd_node *root, *node; |
| 192 | struct lyd_node_opaq *opq; |
| 193 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 194 | assert_int_equal(lyd_new_opaq(NULL, ctx, "node1", NULL, "my-module", &root), LY_SUCCESS); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 195 | 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 Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 199 | assert_string_equal(opq->prefix.module_name, "my-module"); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 200 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 201 | assert_int_equal(lyd_new_opaq(root, NULL, "node2", "value", "my-module2", &node), LY_SUCCESS); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 202 | 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 Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 206 | assert_string_equal(opq->prefix.module_name, "my-module2"); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 207 | assert_ptr_equal(opq->parent, root); |
| 208 | |
| 209 | lyd_free_tree(root); |
| 210 | |
| 211 | *state = NULL; |
| 212 | } |
| 213 | |
| 214 | static void |
| 215 | test_path(void **state) |
| 216 | { |
| 217 | *state = test_path; |
| 218 | |
| 219 | LY_ERR ret; |
| 220 | struct lyd_node *root, *node, *parent; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 221 | |
| 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 Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 229 | assert_string_equal("val", LYD_CANON_VALUE(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 230 | |
| 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 Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 236 | assert_string_equal("val2", LYD_CANON_VALUE(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 237 | |
| 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 Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 243 | assert_string_equal("val3", LYD_CANON_VALUE(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 244 | |
| 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 Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 252 | ret = lyd_new_path2(NULL, ctx, "/a:l1", NULL, 0, LYD_NEW_PATH_OPAQ, NULL, &root); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 253 | 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 Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 263 | ret = lyd_new_path2(NULL, ctx, "/a:foo", NULL, 0, LYD_NEW_PATH_OPAQ, NULL, &root); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 264 | 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 Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 275 | assert_string_equal("val", LYD_CANON_VALUE(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 276 | |
| 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 Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 280 | ret = lyd_new_path2(root, NULL, "/a:l2[1]/c/x", "val", 0, LYD_NEW_PATH_UPDATE, NULL, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 281 | assert_int_equal(ret, LY_SUCCESS); |
| 282 | assert_null(node); |
| 283 | |
Radek Krejci | 092e33c | 2020-10-12 15:33:10 +0200 | [diff] [blame] | 284 | ret = lyd_new_path2(root, NULL, "/a:l2[1]/c/x", "val2", 0, LYD_NEW_PATH_UPDATE, NULL, &node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 285 | assert_int_equal(ret, LY_SUCCESS); |
| 286 | assert_non_null(node); |
| 287 | assert_string_equal(node->schema->name, "x"); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 288 | assert_string_equal("val2", LYD_CANON_VALUE(node)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 289 | |
| 290 | lyd_free_tree(root); |
| 291 | |
| 292 | *state = NULL; |
| 293 | } |
| 294 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 295 | int main(void) |
| 296 | { |
| 297 | const struct CMUnitTest tests[] = { |
| 298 | cmocka_unit_test_setup_teardown(test_top_level, setup, teardown), |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 299 | cmocka_unit_test_setup_teardown(test_opaq, setup, teardown), |
| 300 | cmocka_unit_test_setup_teardown(test_path, setup, teardown), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 304 | } |