blob: 580d4882f0e5d0bbac08d4cca79506308227e674 [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 Krejci86d106e2018-10-18 09:53:19 +020020#include "../../src/tree_schema_helpers.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010021#include "../../src/hash_table.c"
22#include "../../src/xpath.c"
23#include "../../src/context.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020024
25#include <stdarg.h>
26#include <stddef.h>
Radek Krejci3b1f9292018-11-08 10:58:35 +010027#include <stdio.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020028#include <setjmp.h>
29#include <cmocka.h>
30
31#include "libyang.h"
32
33#define BUFSIZE 1024
34char logbuf[BUFSIZE] = {0};
Radek Krejci3b1f9292018-11-08 10:58:35 +010035int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci86d106e2018-10-18 09:53:19 +020036
Radek Krejci313d9902018-11-08 09:42:58 +010037/* set to 0 to printing error messages to stderr instead of checking them in code */
38#define ENABLE_LOGGER_CHECKING 1
39
Radek Krejci3b1f9292018-11-08 10:58:35 +010040#if ENABLE_LOGGER_CHECKING
Radek Krejci86d106e2018-10-18 09:53:19 +020041static void
42logger(LY_LOG_LEVEL level, const char *msg, const char *path)
43{
44 (void) level; /* unused */
Radek Krejci3b1f9292018-11-08 10:58:35 +010045 if (store) {
46 if (path && path[0]) {
47 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
48 } else {
49 strncpy(logbuf, msg, BUFSIZE - 1);
50 }
51 if (store > 0) {
52 --store;
53 }
54 }
Radek Krejci86d106e2018-10-18 09:53:19 +020055}
Radek Krejci3b1f9292018-11-08 10:58:35 +010056#endif
Radek Krejci86d106e2018-10-18 09:53:19 +020057
58static int
59logger_setup(void **state)
60{
61 (void) state; /* unused */
62
63 ly_set_log_clb(logger, 0);
64
65 return 0;
66}
67
Radek Krejci313d9902018-11-08 09:42:58 +010068void
69logbuf_clean(void)
70{
71 logbuf[0] = '\0';
72}
73
Radek Krejcia93621b2018-10-18 11:13:38 +020074#if ENABLE_LOGGER_CHECKING
75# define logbuf_assert(str) assert_string_equal(logbuf, str)
76#else
77# define logbuf_assert(str)
78#endif
79
Radek Krejci86d106e2018-10-18 09:53:19 +020080static void
81test_date(void **state)
82{
83 (void) state; /* unused */
84
85 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020086 logbuf_assert("Invalid argument date (lysp_check_date()).");
Radek Krejci86d106e2018-10-18 09:53:19 +020087 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020088 logbuf_assert("Invalid argument date_len (lysp_check_date()).");
Radek Krejci86d106e2018-10-18 09:53:19 +020089 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020090 logbuf_assert("Invalid value \"nonsencexx\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020091 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020092 logbuf_assert("Invalid value \"123x-11-11\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020093 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020094 logbuf_assert("Invalid value \"2018-13-11\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020095 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020096 logbuf_assert("Invalid value \"2018-11-41\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020097 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020098 logbuf_assert("Invalid value \"2018-02-29\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020099 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200100 logbuf_assert("Invalid value \"2018.02-28\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200101 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +0200102 logbuf_assert("Invalid value \"2018-02.28\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +0200103
104 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
105 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
106 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
107}
108
Radek Krejcia93621b2018-10-18 11:13:38 +0200109static void
110test_revisions(void **state)
111{
112 (void) state; /* unused */
113
114 struct lysp_revision *revs = NULL, *rev;
115
Radek Krejci313d9902018-11-08 09:42:58 +0100116 logbuf_clean();
Radek Krejcia93621b2018-10-18 11:13:38 +0200117 /* no error, it just does nothing */
118 lysp_sort_revisions(NULL);
119 logbuf_assert("");
120
121 /* revisions are stored in wrong order - the newest is the last */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200122 LY_ARRAY_NEW_RET(NULL, revs, rev,);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200123 strcpy(rev->date, "2018-01-01");
Radek Krejci2c4e7172018-10-19 15:56:26 +0200124 LY_ARRAY_NEW_RET(NULL, revs, rev,);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200125 strcpy(rev->date, "2018-12-31");
Radek Krejcia93621b2018-10-18 11:13:38 +0200126
127 assert_int_equal(2, LY_ARRAY_SIZE(revs));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200128 assert_string_equal("2018-01-01", &revs[0]);
129 assert_string_equal("2018-12-31", &revs[1]);
Radek Krejcia93621b2018-10-18 11:13:38 +0200130 /* the order should be fixed, so the newest revision will be the first in the array */
131 lysp_sort_revisions(revs);
Radek Krejci2c4e7172018-10-19 15:56:26 +0200132 assert_string_equal("2018-12-31", &revs[0]);
133 assert_string_equal("2018-01-01", &revs[1]);
Radek Krejcia93621b2018-10-18 11:13:38 +0200134
Radek Krejci2c4e7172018-10-19 15:56:26 +0200135 LY_ARRAY_FREE(revs);
Radek Krejcia93621b2018-10-18 11:13:38 +0200136}
137
Radek Krejci3b1f9292018-11-08 10:58:35 +0100138static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
139 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
140 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
141{
142 *module_data = user_data;
143 *format = LYS_IN_YANG;
144 *free_module_data = NULL;
145 return LY_SUCCESS;
146}
147
Radek Krejci313d9902018-11-08 09:42:58 +0100148static void
149test_typedef(void **state)
150{
151 (void) state; /* unused */
152
153 struct ly_ctx *ctx = NULL;
154 const char *str;
155
156 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
157
Radek Krejci3b1f9292018-11-08 10:58:35 +0100158 str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}";
159 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
160 logbuf_assert("Invalid name \"binary\" of typedef - name collision with a built-in type.");
161 str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}";
162 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
163 logbuf_assert("Invalid name \"bits\" of typedef - name collision with a built-in type.");
164 str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}";
165 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
166 logbuf_assert("Invalid name \"boolean\" of typedef - name collision with a built-in type.");
167 str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}";
168 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
169 logbuf_assert("Invalid name \"decimal64\" of typedef - name collision with a built-in type.");
170 str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}";
171 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
172 logbuf_assert("Invalid name \"empty\" of typedef - name collision with a built-in type.");
173 str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}";
174 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
175 logbuf_assert("Invalid name \"enumeration\" of typedef - name collision with a built-in type.");
176 str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}";
177 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
178 logbuf_assert("Invalid name \"int8\" of typedef - name collision with a built-in type.");
179 str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}";
180 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
181 logbuf_assert("Invalid name \"int16\" of typedef - name collision with a built-in type.");
182 str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}";
183 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
184 logbuf_assert("Invalid name \"int32\" of typedef - name collision with a built-in type.");
185 str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}";
186 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
187 logbuf_assert("Invalid name \"int64\" of typedef - name collision with a built-in type.");
188 str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}";
189 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
190 logbuf_assert("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type.");
191 str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}";
192 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
193 logbuf_assert("Invalid name \"identityref\" of typedef - name collision with a built-in type.");
194 str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}";
195 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
196 logbuf_assert("Invalid name \"leafref\" of typedef - name collision with a built-in type.");
197 str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}";
198 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
199 logbuf_assert("Invalid name \"string\" of typedef - name collision with a built-in type.");
200 str = "module a {namespace urn:a; prefix a; typedef union {type string;}}";
201 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
202 logbuf_assert("Invalid name \"union\" of typedef - name collision with a built-in type.");
203 str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}";
204 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
205 logbuf_assert("Invalid name \"uint8\" of typedef - name collision with a built-in type.");
206 str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}";
207 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
208 logbuf_assert("Invalid name \"uint16\" of typedef - name collision with a built-in type.");
209 str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}";
210 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
211 logbuf_assert("Invalid name \"uint32\" of typedef - name collision with a built-in type.");
212 str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}";
213 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
214 logbuf_assert("Invalid name \"uint64\" of typedef - name collision with a built-in type.");
215
216 str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} "
217 "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}"
218 "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}"
219 "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}"
220 "typedef uint32_ {type string;} typedef uint64_ {type string;}}";
221 assert_non_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
222
Radek Krejci313d9902018-11-08 09:42:58 +0100223 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
224 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
225 logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type.");
226
227 str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
228 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
229 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
230
231 str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
232 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
233 logbuf_assert("Invalid name \"y\" of typedef - name collision with another scoped type.");
234
Radek Krejci3b1f9292018-11-08 10:58:35 +0100235 str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}";
236 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
237 logbuf_assert("Invalid name \"y\" of typedef - name collision with sibling type.");
238
239 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}");
240 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
241 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
242 logbuf_assert("Invalid name \"x\" of typedef - name collision with another top-level type.");
243
244 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}");
245 str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}";
246 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
247 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
248
249 ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}");
250 str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}";
251 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
252 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
253
Radek Krejci313d9902018-11-08 09:42:58 +0100254 ly_ctx_destroy(ctx, NULL);
255}
256
Radek Krejci86d106e2018-10-18 09:53:19 +0200257int main(void)
258{
259 const struct CMUnitTest tests[] = {
260 cmocka_unit_test_setup(test_date, logger_setup),
Radek Krejcia93621b2018-10-18 11:13:38 +0200261 cmocka_unit_test_setup(test_revisions, logger_setup),
Radek Krejci313d9902018-11-08 09:42:58 +0100262 cmocka_unit_test_setup(test_typedef, logger_setup),
Radek Krejci86d106e2018-10-18 09:53:19 +0200263 };
264
265 return cmocka_run_group_tests(tests, NULL, NULL);
266}