blob: 01fe9f4dcc0b676b9f98c7cb4443af107fe7d899 [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 Krejci86d106e2018-10-18 09:53:19 +020015#include <stdarg.h>
16#include <stddef.h>
Radek Krejci3b1f9292018-11-08 10:58:35 +010017#include <stdio.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020018#include <setjmp.h>
19#include <cmocka.h>
20
Radek Krejci2d7a47b2019-05-16 13:34:10 +020021#include <string.h>
22
23#include "../../src/common.h"
24#include "../../src/context.h"
25#include "../../src/tree_schema_internal.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020026
27#define BUFSIZE 1024
28char logbuf[BUFSIZE] = {0};
Radek Krejci3b1f9292018-11-08 10:58:35 +010029int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci86d106e2018-10-18 09:53:19 +020030
Radek Krejci313d9902018-11-08 09:42:58 +010031/* set to 0 to printing error messages to stderr instead of checking them in code */
32#define ENABLE_LOGGER_CHECKING 1
33
Radek Krejci3b1f9292018-11-08 10:58:35 +010034#if ENABLE_LOGGER_CHECKING
Radek Krejci86d106e2018-10-18 09:53:19 +020035static void
36logger(LY_LOG_LEVEL level, const char *msg, const char *path)
37{
38 (void) level; /* unused */
Radek Krejci3b1f9292018-11-08 10:58:35 +010039 if (store) {
40 if (path && path[0]) {
41 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
42 } else {
43 strncpy(logbuf, msg, BUFSIZE - 1);
44 }
45 if (store > 0) {
46 --store;
47 }
48 }
Radek Krejci86d106e2018-10-18 09:53:19 +020049}
Radek Krejci3b1f9292018-11-08 10:58:35 +010050#endif
Radek Krejci86d106e2018-10-18 09:53:19 +020051
52static int
53logger_setup(void **state)
54{
55 (void) state; /* unused */
56
57 ly_set_log_clb(logger, 0);
58
59 return 0;
60}
61
Radek Krejcia3045382018-11-22 14:30:31 +010062static int
63logger_teardown(void **state)
64{
65 (void) state; /* unused */
66#if ENABLE_LOGGER_CHECKING
67 if (*state) {
68 fprintf(stderr, "%s\n", logbuf);
69 }
70#endif
71 return 0;
72}
73
Radek Krejci313d9902018-11-08 09:42:58 +010074void
75logbuf_clean(void)
76{
77 logbuf[0] = '\0';
78}
79
Radek Krejcia93621b2018-10-18 11:13:38 +020080#if ENABLE_LOGGER_CHECKING
81# define logbuf_assert(str) assert_string_equal(logbuf, str)
82#else
83# define logbuf_assert(str)
84#endif
85
Radek Krejci86d106e2018-10-18 09:53:19 +020086static void
87test_date(void **state)
88{
Radek Krejcia3045382018-11-22 14:30:31 +010089 *state = test_date;
Radek Krejci86d106e2018-10-18 09:53:19 +020090
91 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020092 logbuf_assert("Invalid argument date (lysp_check_date()).");
Radek Krejci86d106e2018-10-18 09:53:19 +020093 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020094 logbuf_assert("Invalid argument date_len (lysp_check_date()).");
Radek Krejci86d106e2018-10-18 09:53:19 +020095 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020096 logbuf_assert("Invalid value \"nonsencexx\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020097 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020098 logbuf_assert("Invalid value \"123x-11-11\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020099 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200100 logbuf_assert("Invalid value \"2018-13-11\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200101 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200102 logbuf_assert("Invalid value \"2018-11-41\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200103 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200104 logbuf_assert("Invalid value \"2018-02-29\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200105 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200106 logbuf_assert("Invalid value \"2018.02-28\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200107 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200108 logbuf_assert("Invalid value \"2018-02.28\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200109
110 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
111 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
112 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
Radek Krejcia3045382018-11-22 14:30:31 +0100113
114 *state = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200115}
116
Radek Krejcia93621b2018-10-18 11:13:38 +0200117static void
118test_revisions(void **state)
119{
120 (void) state; /* unused */
121
122 struct lysp_revision *revs = NULL, *rev;
123
Radek Krejci313d9902018-11-08 09:42:58 +0100124 logbuf_clean();
Radek Krejcia93621b2018-10-18 11:13:38 +0200125 /* no error, it just does nothing */
126 lysp_sort_revisions(NULL);
127 logbuf_assert("");
128
129 /* revisions are stored in wrong order - the newest is the last */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200130 LY_ARRAY_NEW_RET(NULL, revs, rev,);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200131 strcpy(rev->date, "2018-01-01");
Radek Krejci2c4e7172018-10-19 15:56:26 +0200132 LY_ARRAY_NEW_RET(NULL, revs, rev,);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200133 strcpy(rev->date, "2018-12-31");
Radek Krejcia93621b2018-10-18 11:13:38 +0200134
135 assert_int_equal(2, LY_ARRAY_SIZE(revs));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200136 assert_string_equal("2018-01-01", &revs[0]);
137 assert_string_equal("2018-12-31", &revs[1]);
Radek Krejcia93621b2018-10-18 11:13:38 +0200138 /* the order should be fixed, so the newest revision will be the first in the array */
139 lysp_sort_revisions(revs);
Radek Krejci2c4e7172018-10-19 15:56:26 +0200140 assert_string_equal("2018-12-31", &revs[0]);
141 assert_string_equal("2018-01-01", &revs[1]);
Radek Krejcia93621b2018-10-18 11:13:38 +0200142
Radek Krejci2c4e7172018-10-19 15:56:26 +0200143 LY_ARRAY_FREE(revs);
Radek Krejcia93621b2018-10-18 11:13:38 +0200144}
145
Radek Krejci3b1f9292018-11-08 10:58:35 +0100146static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
147 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
148 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
149{
150 *module_data = user_data;
151 *format = LYS_IN_YANG;
152 *free_module_data = NULL;
153 return LY_SUCCESS;
154}
155
Radek Krejci313d9902018-11-08 09:42:58 +0100156static void
157test_typedef(void **state)
158{
Radek Krejcia3045382018-11-22 14:30:31 +0100159 *state = test_typedef;
Radek Krejci313d9902018-11-08 09:42:58 +0100160
161 struct ly_ctx *ctx = NULL;
162 const char *str;
163
164 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
165
Radek Krejci3b1f9292018-11-08 10:58:35 +0100166 str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
167 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
168 logbuf_assert("Invalid name \"binary\" of typedef - name collision with a built-in type.");
169 str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
170 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
171 logbuf_assert("Invalid name \"bits\" of typedef - name collision with a built-in type.");
172 str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
173 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
174 logbuf_assert("Invalid name \"boolean\" of typedef - name collision with a built-in type.");
175 str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
176 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
177 logbuf_assert("Invalid name \"decimal64\" of typedef - name collision with a built-in type.");
178 str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
179 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
180 logbuf_assert("Invalid name \"empty\" of typedef - name collision with a built-in type.");
181 str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
182 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
183 logbuf_assert("Invalid name \"enumeration\" of typedef - name collision with a built-in type.");
184 str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
185 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
186 logbuf_assert("Invalid name \"int8\" of typedef - name collision with a built-in type.");
187 str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
188 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
189 logbuf_assert("Invalid name \"int16\" of typedef - name collision with a built-in type.");
190 str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
191 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
192 logbuf_assert("Invalid name \"int32\" of typedef - name collision with a built-in type.");
193 str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
194 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
195 logbuf_assert("Invalid name \"int64\" of typedef - name collision with a built-in type.");
196 str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
197 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
198 logbuf_assert("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type.");
199 str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
200 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
201 logbuf_assert("Invalid name \"identityref\" of typedef - name collision with a built-in type.");
202 str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
203 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
204 logbuf_assert("Invalid name \"leafref\" of typedef - name collision with a built-in type.");
205 str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
206 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
207 logbuf_assert("Invalid name \"string\" of typedef - name collision with a built-in type.");
208 str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
209 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
210 logbuf_assert("Invalid name \"union\" of typedef - name collision with a built-in type.");
211 str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
212 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
213 logbuf_assert("Invalid name \"uint8\" of typedef - name collision with a built-in type.");
214 str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
215 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
216 logbuf_assert("Invalid name \"uint16\" of typedef - name collision with a built-in type.");
217 str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
218 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
219 logbuf_assert("Invalid name \"uint32\" of typedef - name collision with a built-in type.");
220 str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
221 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
222 logbuf_assert("Invalid name \"uint64\" of typedef - name collision with a built-in type.");
223
224 str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} "
225 "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
226 "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
227 "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
228 "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
229 assert_non_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
230
Radek Krejci313d9902018-11-08 09:42:58 +0100231 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
232 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
233 logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type.");
234
235 str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
236 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
237 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
238
239 str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
240 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
241 logbuf_assert("Invalid name \"y\" of typedef - name collision with another scoped type.");
242
Radek Krejci3b1f9292018-11-08 10:58:35 +0100243 str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}";
244 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
245 logbuf_assert("Invalid name \"y\" of typedef - name collision with sibling type.");
246
247 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
248 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
249 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
250 logbuf_assert("Invalid name \"x\" of typedef - name collision with another top-level type.");
251
252 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}");
253 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
254 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
255 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
256
257 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}");
258 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
259 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
260 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
261
Radek Krejcia3045382018-11-22 14:30:31 +0100262 *state = NULL;
Radek Krejci313d9902018-11-08 09:42:58 +0100263 ly_ctx_destroy(ctx, NULL);
264}
265
Radek Krejci86d106e2018-10-18 09:53:19 +0200266int main(void)
267{
268 const struct CMUnitTest tests[] = {
Radek Krejcia3045382018-11-22 14:30:31 +0100269 cmocka_unit_test_setup_teardown(test_date, logger_setup, logger_teardown),
Radek Krejci9ea8ca12019-06-10 13:11:55 +0200270 cmocka_unit_test_setup_teardown(test_revisions, logger_setup, logger_teardown),
Radek Krejcia3045382018-11-22 14:30:31 +0100271 cmocka_unit_test_setup_teardown(test_typedef, logger_setup, logger_teardown),
Radek Krejci86d106e2018-10-18 09:53:19 +0200272 };
273
274 return cmocka_run_group_tests(tests, NULL, NULL);
275}