blob: 02d571a2150062d7428c8161f0825f0ba5e4421c [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/*
2 * @file set.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from common.c
5 *
6 * Copyright (c) 2018 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
Radek Krejcif3f47842018-11-15 11:22:15 +010015#include "../../src/common.c"
16#include "../../src/log.c"
17#include "../../src/set.c"
18#include "../../src/parser_yang.c"
19#include "../../src/tree_schema.c"
Radek Krejci096235c2019-01-11 11:12:19 +010020#include "../../src/tree_schema_compile.c"
Radek Krejci19a96102018-11-15 13:38:09 +010021#include "../../src/tree_schema_free.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020022#include "../../src/tree_schema_helpers.c"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "../../src/plugins_types.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010024#include "../../src/hash_table.c"
25#include "../../src/xpath.c"
26#include "../../src/context.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020027
28#include <stdarg.h>
29#include <stddef.h>
Radek Krejci3b1f9292018-11-08 10:58:35 +010030#include <stdio.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020031#include <setjmp.h>
32#include <cmocka.h>
33
34#include "libyang.h"
35
36#define BUFSIZE 1024
37char logbuf[BUFSIZE] = {0};
Radek Krejci3b1f9292018-11-08 10:58:35 +010038int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci86d106e2018-10-18 09:53:19 +020039
Radek Krejci313d9902018-11-08 09:42:58 +010040/* set to 0 to printing error messages to stderr instead of checking them in code */
41#define ENABLE_LOGGER_CHECKING 1
42
Radek Krejci3b1f9292018-11-08 10:58:35 +010043#if ENABLE_LOGGER_CHECKING
Radek Krejci86d106e2018-10-18 09:53:19 +020044static void
45logger(LY_LOG_LEVEL level, const char *msg, const char *path)
46{
47 (void) level; /* unused */
Radek Krejci3b1f9292018-11-08 10:58:35 +010048 if (store) {
49 if (path && path[0]) {
50 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
51 } else {
52 strncpy(logbuf, msg, BUFSIZE - 1);
53 }
54 if (store > 0) {
55 --store;
56 }
57 }
Radek Krejci86d106e2018-10-18 09:53:19 +020058}
Radek Krejci3b1f9292018-11-08 10:58:35 +010059#endif
Radek Krejci86d106e2018-10-18 09:53:19 +020060
61static int
62logger_setup(void **state)
63{
64 (void) state; /* unused */
65
66 ly_set_log_clb(logger, 0);
67
68 return 0;
69}
70
Radek Krejcia3045382018-11-22 14:30:31 +010071static int
72logger_teardown(void **state)
73{
74 (void) state; /* unused */
75#if ENABLE_LOGGER_CHECKING
76 if (*state) {
77 fprintf(stderr, "%s\n", logbuf);
78 }
79#endif
80 return 0;
81}
82
Radek Krejci313d9902018-11-08 09:42:58 +010083void
84logbuf_clean(void)
85{
86 logbuf[0] = '\0';
87}
88
Radek Krejcia93621b2018-10-18 11:13:38 +020089#if ENABLE_LOGGER_CHECKING
90# define logbuf_assert(str) assert_string_equal(logbuf, str)
91#else
92# define logbuf_assert(str)
93#endif
94
Radek Krejci86d106e2018-10-18 09:53:19 +020095static void
96test_date(void **state)
97{
Radek Krejcia3045382018-11-22 14:30:31 +010098 *state = test_date;
Radek Krejci86d106e2018-10-18 09:53:19 +020099
100 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200101 logbuf_assert("Invalid argument date (lysp_check_date()).");
Radek Krejci86d106e2018-10-18 09:53:19 +0200102 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200103 logbuf_assert("Invalid argument date_len (lysp_check_date()).");
Radek Krejci86d106e2018-10-18 09:53:19 +0200104 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200105 logbuf_assert("Invalid value \"nonsencexx\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200106 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200107 logbuf_assert("Invalid value \"123x-11-11\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200108 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200109 logbuf_assert("Invalid value \"2018-13-11\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200110 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200111 logbuf_assert("Invalid value \"2018-11-41\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200112 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200113 logbuf_assert("Invalid value \"2018-02-29\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200114 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200115 logbuf_assert("Invalid value \"2018.02-28\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200116 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200117 logbuf_assert("Invalid value \"2018-02.28\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200118
119 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
120 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
121 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
Radek Krejcia3045382018-11-22 14:30:31 +0100122
123 *state = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200124}
125
Radek Krejcia93621b2018-10-18 11:13:38 +0200126static void
127test_revisions(void **state)
128{
129 (void) state; /* unused */
130
131 struct lysp_revision *revs = NULL, *rev;
132
Radek Krejci313d9902018-11-08 09:42:58 +0100133 logbuf_clean();
Radek Krejcia93621b2018-10-18 11:13:38 +0200134 /* no error, it just does nothing */
135 lysp_sort_revisions(NULL);
136 logbuf_assert("");
137
138 /* revisions are stored in wrong order - the newest is the last */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200139 LY_ARRAY_NEW_RET(NULL, revs, rev,);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200140 strcpy(rev->date, "2018-01-01");
Radek Krejci2c4e7172018-10-19 15:56:26 +0200141 LY_ARRAY_NEW_RET(NULL, revs, rev,);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200142 strcpy(rev->date, "2018-12-31");
Radek Krejcia93621b2018-10-18 11:13:38 +0200143
144 assert_int_equal(2, LY_ARRAY_SIZE(revs));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200145 assert_string_equal("2018-01-01", &revs[0]);
146 assert_string_equal("2018-12-31", &revs[1]);
Radek Krejcia93621b2018-10-18 11:13:38 +0200147 /* the order should be fixed, so the newest revision will be the first in the array */
148 lysp_sort_revisions(revs);
Radek Krejci2c4e7172018-10-19 15:56:26 +0200149 assert_string_equal("2018-12-31", &revs[0]);
150 assert_string_equal("2018-01-01", &revs[1]);
Radek Krejcia93621b2018-10-18 11:13:38 +0200151
Radek Krejci2c4e7172018-10-19 15:56:26 +0200152 LY_ARRAY_FREE(revs);
Radek Krejcia93621b2018-10-18 11:13:38 +0200153}
154
Radek Krejci3b1f9292018-11-08 10:58:35 +0100155static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
156 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
157 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
158{
159 *module_data = user_data;
160 *format = LYS_IN_YANG;
161 *free_module_data = NULL;
162 return LY_SUCCESS;
163}
164
Radek Krejci313d9902018-11-08 09:42:58 +0100165static void
166test_typedef(void **state)
167{
Radek Krejcia3045382018-11-22 14:30:31 +0100168 *state = test_typedef;
Radek Krejci313d9902018-11-08 09:42:58 +0100169
170 struct ly_ctx *ctx = NULL;
171 const char *str;
172
173 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
174
Radek Krejci3b1f9292018-11-08 10:58:35 +0100175 str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
176 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
177 logbuf_assert("Invalid name \"binary\" of typedef - name collision with a built-in type.");
178 str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
179 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
180 logbuf_assert("Invalid name \"bits\" of typedef - name collision with a built-in type.");
181 str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
182 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
183 logbuf_assert("Invalid name \"boolean\" of typedef - name collision with a built-in type.");
184 str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
185 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
186 logbuf_assert("Invalid name \"decimal64\" of typedef - name collision with a built-in type.");
187 str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
188 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
189 logbuf_assert("Invalid name \"empty\" of typedef - name collision with a built-in type.");
190 str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
191 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
192 logbuf_assert("Invalid name \"enumeration\" of typedef - name collision with a built-in type.");
193 str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
194 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
195 logbuf_assert("Invalid name \"int8\" of typedef - name collision with a built-in type.");
196 str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
197 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
198 logbuf_assert("Invalid name \"int16\" of typedef - name collision with a built-in type.");
199 str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
200 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
201 logbuf_assert("Invalid name \"int32\" of typedef - name collision with a built-in type.");
202 str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
203 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
204 logbuf_assert("Invalid name \"int64\" of typedef - name collision with a built-in type.");
205 str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
206 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
207 logbuf_assert("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type.");
208 str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
209 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
210 logbuf_assert("Invalid name \"identityref\" of typedef - name collision with a built-in type.");
211 str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
212 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
213 logbuf_assert("Invalid name \"leafref\" of typedef - name collision with a built-in type.");
214 str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
215 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
216 logbuf_assert("Invalid name \"string\" of typedef - name collision with a built-in type.");
217 str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
218 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
219 logbuf_assert("Invalid name \"union\" of typedef - name collision with a built-in type.");
220 str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
221 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
222 logbuf_assert("Invalid name \"uint8\" of typedef - name collision with a built-in type.");
223 str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
224 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
225 logbuf_assert("Invalid name \"uint16\" of typedef - name collision with a built-in type.");
226 str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
227 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
228 logbuf_assert("Invalid name \"uint32\" of typedef - name collision with a built-in type.");
229 str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
230 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
231 logbuf_assert("Invalid name \"uint64\" of typedef - name collision with a built-in type.");
232
233 str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} "
234 "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
235 "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
236 "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
237 "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
238 assert_non_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
239
Radek Krejci313d9902018-11-08 09:42:58 +0100240 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
241 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
242 logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type.");
243
244 str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
245 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
246 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
247
248 str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
249 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
250 logbuf_assert("Invalid name \"y\" of typedef - name collision with another scoped type.");
251
Radek Krejci3b1f9292018-11-08 10:58:35 +0100252 str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}";
253 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
254 logbuf_assert("Invalid name \"y\" of typedef - name collision with sibling type.");
255
256 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
257 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
258 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
259 logbuf_assert("Invalid name \"x\" of typedef - name collision with another top-level type.");
260
261 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}");
262 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
263 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
264 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
265
266 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}");
267 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
268 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
269 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
270
Radek Krejcia3045382018-11-22 14:30:31 +0100271 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100272 ly_ctx_destroy(ctx, NULL);
273}
274
Radek Krejcia3045382018-11-22 14:30:31 +0100275static void
276test_parse_nodeid(void **state)
277{
278 (void) state; /* unused */
279 const char *str;
280 const char *prefix, *name;
281 size_t prefix_len, name_len;
282
283 str = "123";
284 assert_int_equal(LY_EINVAL, lys_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len));
285
286 str = "a12_-.!";
287 assert_int_equal(LY_SUCCESS, lys_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len));
288 assert_null(prefix);
289 assert_int_equal(0, prefix_len);
290 assert_non_null(name);
291 assert_int_equal(6, name_len);
292 assert_int_equal(0, strncmp("a12_-.", name, name_len));
293 assert_string_equal("!", str);
294
295 str = "a12_-.:_b2 xxx";
296 assert_int_equal(LY_SUCCESS, lys_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len));
297 assert_non_null(prefix);
298 assert_int_equal(6, prefix_len);
299 assert_int_equal(0, strncmp("a12_-.", prefix, prefix_len));
300 assert_non_null(name);
301 assert_int_equal(3, name_len);
302 assert_int_equal(0, strncmp("_b2", name, name_len));
303 assert_string_equal(" xxx", str);
304}
305
Radek Krejci86d106e2018-10-18 09:53:19 +0200306int main(void)
307{
308 const struct CMUnitTest tests[] = {
Radek Krejcia3045382018-11-22 14:30:31 +0100309 cmocka_unit_test_setup_teardown(test_date, logger_setup, logger_teardown),
Radek Krejcia93621b2018-10-18 11:13:38 +0200310 cmocka_unit_test_setup(test_revisions, logger_setup),
Radek Krejcia3045382018-11-22 14:30:31 +0100311 cmocka_unit_test_setup_teardown(test_typedef, logger_setup, logger_teardown),
312 cmocka_unit_test_setup(test_parse_nodeid, logger_setup),
Radek Krejci86d106e2018-10-18 09:53:19 +0200313 };
314
315 return cmocka_run_group_tests(tests, NULL, NULL);
316}