blob: 1c5ca3deac7eb1f56dadd39b68f67ef5f0b98b6d [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 Krejci19a96102018-11-15 13:38:09 +010020#include "../../src/tree_schema_free.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020021#include "../../src/tree_schema_helpers.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010022#include "../../src/hash_table.c"
23#include "../../src/xpath.c"
24#include "../../src/context.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020025
26#include <stdarg.h>
27#include <stddef.h>
Radek Krejci3b1f9292018-11-08 10:58:35 +010028#include <stdio.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020029#include <setjmp.h>
30#include <cmocka.h>
31
32#include "libyang.h"
33
34#define BUFSIZE 1024
35char logbuf[BUFSIZE] = {0};
Radek Krejci3b1f9292018-11-08 10:58:35 +010036int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci86d106e2018-10-18 09:53:19 +020037
Radek Krejci313d9902018-11-08 09:42:58 +010038/* set to 0 to printing error messages to stderr instead of checking them in code */
39#define ENABLE_LOGGER_CHECKING 1
40
Radek Krejci3b1f9292018-11-08 10:58:35 +010041#if ENABLE_LOGGER_CHECKING
Radek Krejci86d106e2018-10-18 09:53:19 +020042static void
43logger(LY_LOG_LEVEL level, const char *msg, const char *path)
44{
45 (void) level; /* unused */
Radek Krejci3b1f9292018-11-08 10:58:35 +010046 if (store) {
47 if (path && path[0]) {
48 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
49 } else {
50 strncpy(logbuf, msg, BUFSIZE - 1);
51 }
52 if (store > 0) {
53 --store;
54 }
55 }
Radek Krejci86d106e2018-10-18 09:53:19 +020056}
Radek Krejci3b1f9292018-11-08 10:58:35 +010057#endif
Radek Krejci86d106e2018-10-18 09:53:19 +020058
59static int
60logger_setup(void **state)
61{
62 (void) state; /* unused */
63
64 ly_set_log_clb(logger, 0);
65
66 return 0;
67}
68
Radek Krejcia3045382018-11-22 14:30:31 +010069static int
70logger_teardown(void **state)
71{
72 (void) state; /* unused */
73#if ENABLE_LOGGER_CHECKING
74 if (*state) {
75 fprintf(stderr, "%s\n", logbuf);
76 }
77#endif
78 return 0;
79}
80
Radek Krejci313d9902018-11-08 09:42:58 +010081void
82logbuf_clean(void)
83{
84 logbuf[0] = '\0';
85}
86
Radek Krejcia93621b2018-10-18 11:13:38 +020087#if ENABLE_LOGGER_CHECKING
88# define logbuf_assert(str) assert_string_equal(logbuf, str)
89#else
90# define logbuf_assert(str)
91#endif
92
Radek Krejci86d106e2018-10-18 09:53:19 +020093static void
94test_date(void **state)
95{
Radek Krejcia3045382018-11-22 14:30:31 +010096 *state = test_date;
Radek Krejci86d106e2018-10-18 09:53:19 +020097
98 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020099 logbuf_assert("Invalid argument date (lysp_check_date()).");
Radek Krejci86d106e2018-10-18 09:53:19 +0200100 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200101 logbuf_assert("Invalid argument date_len (lysp_check_date()).");
Radek Krejci86d106e2018-10-18 09:53:19 +0200102 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200103 logbuf_assert("Invalid value \"nonsencexx\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200104 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200105 logbuf_assert("Invalid value \"123x-11-11\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200106 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200107 logbuf_assert("Invalid value \"2018-13-11\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200108 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200109 logbuf_assert("Invalid value \"2018-11-41\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200110 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200111 logbuf_assert("Invalid value \"2018-02-29\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200112 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200113 logbuf_assert("Invalid value \"2018.02-28\" 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
117 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
118 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
119 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
Radek Krejcia3045382018-11-22 14:30:31 +0100120
121 *state = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200122}
123
Radek Krejcia93621b2018-10-18 11:13:38 +0200124static void
125test_revisions(void **state)
126{
127 (void) state; /* unused */
128
129 struct lysp_revision *revs = NULL, *rev;
130
Radek Krejci313d9902018-11-08 09:42:58 +0100131 logbuf_clean();
Radek Krejcia93621b2018-10-18 11:13:38 +0200132 /* no error, it just does nothing */
133 lysp_sort_revisions(NULL);
134 logbuf_assert("");
135
136 /* revisions are stored in wrong order - the newest is the last */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200137 LY_ARRAY_NEW_RET(NULL, revs, rev,);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200138 strcpy(rev->date, "2018-01-01");
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-12-31");
Radek Krejcia93621b2018-10-18 11:13:38 +0200141
142 assert_int_equal(2, LY_ARRAY_SIZE(revs));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200143 assert_string_equal("2018-01-01", &revs[0]);
144 assert_string_equal("2018-12-31", &revs[1]);
Radek Krejcia93621b2018-10-18 11:13:38 +0200145 /* the order should be fixed, so the newest revision will be the first in the array */
146 lysp_sort_revisions(revs);
Radek Krejci2c4e7172018-10-19 15:56:26 +0200147 assert_string_equal("2018-12-31", &revs[0]);
148 assert_string_equal("2018-01-01", &revs[1]);
Radek Krejcia93621b2018-10-18 11:13:38 +0200149
Radek Krejci2c4e7172018-10-19 15:56:26 +0200150 LY_ARRAY_FREE(revs);
Radek Krejcia93621b2018-10-18 11:13:38 +0200151}
152
Radek Krejci3b1f9292018-11-08 10:58:35 +0100153static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
154 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
155 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
156{
157 *module_data = user_data;
158 *format = LYS_IN_YANG;
159 *free_module_data = NULL;
160 return LY_SUCCESS;
161}
162
Radek Krejci313d9902018-11-08 09:42:58 +0100163static void
164test_typedef(void **state)
165{
Radek Krejcia3045382018-11-22 14:30:31 +0100166 *state = test_typedef;
Radek Krejci313d9902018-11-08 09:42:58 +0100167
168 struct ly_ctx *ctx = NULL;
169 const char *str;
170
171 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
172
Radek Krejci3b1f9292018-11-08 10:58:35 +0100173 str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
174 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
175 logbuf_assert("Invalid name \"binary\" of typedef - name collision with a built-in type.");
176 str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
177 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
178 logbuf_assert("Invalid name \"bits\" of typedef - name collision with a built-in type.");
179 str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
180 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
181 logbuf_assert("Invalid name \"boolean\" of typedef - name collision with a built-in type.");
182 str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
183 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
184 logbuf_assert("Invalid name \"decimal64\" of typedef - name collision with a built-in type.");
185 str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
186 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
187 logbuf_assert("Invalid name \"empty\" of typedef - name collision with a built-in type.");
188 str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
189 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
190 logbuf_assert("Invalid name \"enumeration\" of typedef - name collision with a built-in type.");
191 str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
192 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
193 logbuf_assert("Invalid name \"int8\" of typedef - name collision with a built-in type.");
194 str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
195 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
196 logbuf_assert("Invalid name \"int16\" of typedef - name collision with a built-in type.");
197 str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
198 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
199 logbuf_assert("Invalid name \"int32\" of typedef - name collision with a built-in type.");
200 str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
201 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
202 logbuf_assert("Invalid name \"int64\" of typedef - name collision with a built-in type.");
203 str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
204 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
205 logbuf_assert("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type.");
206 str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
207 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
208 logbuf_assert("Invalid name \"identityref\" of typedef - name collision with a built-in type.");
209 str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
210 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
211 logbuf_assert("Invalid name \"leafref\" of typedef - name collision with a built-in type.");
212 str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
213 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
214 logbuf_assert("Invalid name \"string\" of typedef - name collision with a built-in type.");
215 str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
216 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
217 logbuf_assert("Invalid name \"union\" of typedef - name collision with a built-in type.");
218 str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
219 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
220 logbuf_assert("Invalid name \"uint8\" of typedef - name collision with a built-in type.");
221 str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
222 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
223 logbuf_assert("Invalid name \"uint16\" of typedef - name collision with a built-in type.");
224 str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
225 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
226 logbuf_assert("Invalid name \"uint32\" of typedef - name collision with a built-in type.");
227 str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
228 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
229 logbuf_assert("Invalid name \"uint64\" of typedef - name collision with a built-in type.");
230
231 str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} "
232 "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
233 "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
234 "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
235 "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
236 assert_non_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
237
Radek Krejci313d9902018-11-08 09:42:58 +0100238 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
239 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
240 logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type.");
241
242 str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
243 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
244 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
245
246 str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
247 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
248 logbuf_assert("Invalid name \"y\" of typedef - name collision with another scoped type.");
249
Radek Krejci3b1f9292018-11-08 10:58:35 +0100250 str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}";
251 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
252 logbuf_assert("Invalid name \"y\" of typedef - name collision with sibling type.");
253
254 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
255 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
256 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
257 logbuf_assert("Invalid name \"x\" of typedef - name collision with another top-level type.");
258
259 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}");
260 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
261 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
262 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
263
264 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}");
265 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
266 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
267 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
268
Radek Krejcia3045382018-11-22 14:30:31 +0100269 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100270 ly_ctx_destroy(ctx, NULL);
271}
272
Radek Krejcia3045382018-11-22 14:30:31 +0100273static void
274test_parse_nodeid(void **state)
275{
276 (void) state; /* unused */
277 const char *str;
278 const char *prefix, *name;
279 size_t prefix_len, name_len;
280
281 str = "123";
282 assert_int_equal(LY_EINVAL, lys_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len));
283
284 str = "a12_-.!";
285 assert_int_equal(LY_SUCCESS, lys_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len));
286 assert_null(prefix);
287 assert_int_equal(0, prefix_len);
288 assert_non_null(name);
289 assert_int_equal(6, name_len);
290 assert_int_equal(0, strncmp("a12_-.", name, name_len));
291 assert_string_equal("!", str);
292
293 str = "a12_-.:_b2 xxx";
294 assert_int_equal(LY_SUCCESS, lys_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len));
295 assert_non_null(prefix);
296 assert_int_equal(6, prefix_len);
297 assert_int_equal(0, strncmp("a12_-.", prefix, prefix_len));
298 assert_non_null(name);
299 assert_int_equal(3, name_len);
300 assert_int_equal(0, strncmp("_b2", name, name_len));
301 assert_string_equal(" xxx", str);
302}
303
Radek Krejci86d106e2018-10-18 09:53:19 +0200304int main(void)
305{
306 const struct CMUnitTest tests[] = {
Radek Krejcia3045382018-11-22 14:30:31 +0100307 cmocka_unit_test_setup_teardown(test_date, logger_setup, logger_teardown),
Radek Krejcia93621b2018-10-18 11:13:38 +0200308 cmocka_unit_test_setup(test_revisions, logger_setup),
Radek Krejcia3045382018-11-22 14:30:31 +0100309 cmocka_unit_test_setup_teardown(test_typedef, logger_setup, logger_teardown),
310 cmocka_unit_test_setup(test_parse_nodeid, logger_setup),
Radek Krejci86d106e2018-10-18 09:53:19 +0200311 };
312
313 return cmocka_run_group_tests(tests, NULL, NULL);
314}