blob: 8a298b69014edbbfd86049bebc7f224fd5c29f00 [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
15#include "../../src/tree_schema_helpers.c"
16
17#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21
22#include "libyang.h"
23
24#define BUFSIZE 1024
25char logbuf[BUFSIZE] = {0};
26
Radek Krejci313d9902018-11-08 09:42:58 +010027/* set to 0 to printing error messages to stderr instead of checking them in code */
28#define ENABLE_LOGGER_CHECKING 1
29
Radek Krejci86d106e2018-10-18 09:53:19 +020030static void
31logger(LY_LOG_LEVEL level, const char *msg, const char *path)
32{
33 (void) level; /* unused */
34 (void) path; /* unused */
35
36 strncpy(logbuf, msg, BUFSIZE - 1);
37}
38
39static int
40logger_setup(void **state)
41{
42 (void) state; /* unused */
43
44 ly_set_log_clb(logger, 0);
45
46 return 0;
47}
48
Radek Krejci313d9902018-11-08 09:42:58 +010049void
50logbuf_clean(void)
51{
52 logbuf[0] = '\0';
53}
54
Radek Krejcia93621b2018-10-18 11:13:38 +020055#if ENABLE_LOGGER_CHECKING
56# define logbuf_assert(str) assert_string_equal(logbuf, str)
57#else
58# define logbuf_assert(str)
59#endif
60
Radek Krejci86d106e2018-10-18 09:53:19 +020061static void
62test_date(void **state)
63{
64 (void) state; /* unused */
65
66 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020067 logbuf_assert("Invalid argument date (lysp_check_date()).");
Radek Krejci86d106e2018-10-18 09:53:19 +020068 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020069 logbuf_assert("Invalid argument date_len (lysp_check_date()).");
Radek Krejci86d106e2018-10-18 09:53:19 +020070 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020071 logbuf_assert("Invalid value \"nonsencexx\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020072 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020073 logbuf_assert("Invalid value \"123x-11-11\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020074 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020075 logbuf_assert("Invalid value \"2018-13-11\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020076 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020077 logbuf_assert("Invalid value \"2018-11-41\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020078 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020079 logbuf_assert("Invalid value \"2018-02-29\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020080 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020081 logbuf_assert("Invalid value \"2018.02-28\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020082 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020083 logbuf_assert("Invalid value \"2018-02.28\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020084
85 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
86 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
87 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
88}
89
Radek Krejcia93621b2018-10-18 11:13:38 +020090static void
91test_revisions(void **state)
92{
93 (void) state; /* unused */
94
95 struct lysp_revision *revs = NULL, *rev;
96
Radek Krejci313d9902018-11-08 09:42:58 +010097 logbuf_clean();
Radek Krejcia93621b2018-10-18 11:13:38 +020098 /* no error, it just does nothing */
99 lysp_sort_revisions(NULL);
100 logbuf_assert("");
101
102 /* revisions are stored in wrong order - the newest is the last */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200103 LY_ARRAY_NEW_RET(NULL, revs, rev,);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200104 strcpy(rev->date, "2018-01-01");
Radek Krejci2c4e7172018-10-19 15:56:26 +0200105 LY_ARRAY_NEW_RET(NULL, revs, rev,);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200106 strcpy(rev->date, "2018-12-31");
Radek Krejcia93621b2018-10-18 11:13:38 +0200107
108 assert_int_equal(2, LY_ARRAY_SIZE(revs));
Radek Krejci2c4e7172018-10-19 15:56:26 +0200109 assert_string_equal("2018-01-01", &revs[0]);
110 assert_string_equal("2018-12-31", &revs[1]);
Radek Krejcia93621b2018-10-18 11:13:38 +0200111 /* the order should be fixed, so the newest revision will be the first in the array */
112 lysp_sort_revisions(revs);
Radek Krejci2c4e7172018-10-19 15:56:26 +0200113 assert_string_equal("2018-12-31", &revs[0]);
114 assert_string_equal("2018-01-01", &revs[1]);
Radek Krejcia93621b2018-10-18 11:13:38 +0200115
Radek Krejci2c4e7172018-10-19 15:56:26 +0200116 LY_ARRAY_FREE(revs);
Radek Krejcia93621b2018-10-18 11:13:38 +0200117}
118
Radek Krejci313d9902018-11-08 09:42:58 +0100119static void
120test_typedef(void **state)
121{
122 (void) state; /* unused */
123
124 struct ly_ctx *ctx = NULL;
125 const char *str;
126
127 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
128
129 str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}";
130 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
131 logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type.");
132
133 str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}";
134 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
135 logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type.");
136
137 str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}";
138 assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG));
139 logbuf_assert("Invalid name \"y\" of typedef - name collision with another scoped type.");
140
141 ly_ctx_destroy(ctx, NULL);
142}
143
Radek Krejci86d106e2018-10-18 09:53:19 +0200144int main(void)
145{
146 const struct CMUnitTest tests[] = {
147 cmocka_unit_test_setup(test_date, logger_setup),
Radek Krejcia93621b2018-10-18 11:13:38 +0200148 cmocka_unit_test_setup(test_revisions, logger_setup),
Radek Krejci313d9902018-11-08 09:42:58 +0100149 cmocka_unit_test_setup(test_typedef, logger_setup),
Radek Krejci86d106e2018-10-18 09:53:19 +0200150 };
151
152 return cmocka_run_group_tests(tests, NULL, NULL);
153}