blob: 67f4138af8cf3273987ad2512ffa6dddb79e1f3b [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
27static void
28logger(LY_LOG_LEVEL level, const char *msg, const char *path)
29{
30 (void) level; /* unused */
31 (void) path; /* unused */
32
33 strncpy(logbuf, msg, BUFSIZE - 1);
34}
35
36static int
37logger_setup(void **state)
38{
39 (void) state; /* unused */
40
41 ly_set_log_clb(logger, 0);
42
43 return 0;
44}
45
Radek Krejcia93621b2018-10-18 11:13:38 +020046#if ENABLE_LOGGER_CHECKING
47# define logbuf_assert(str) assert_string_equal(logbuf, str)
48#else
49# define logbuf_assert(str)
50#endif
51
Radek Krejci86d106e2018-10-18 09:53:19 +020052static void
53test_date(void **state)
54{
55 (void) state; /* unused */
56
57 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020058 logbuf_assert("Invalid argument date (lysp_check_date()).");
Radek Krejci86d106e2018-10-18 09:53:19 +020059 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020060 logbuf_assert("Invalid argument date_len (lysp_check_date()).");
Radek Krejci86d106e2018-10-18 09:53:19 +020061 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020062 logbuf_assert("Invalid value \"nonsencexx\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020063 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020064 logbuf_assert("Invalid value \"123x-11-11\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020065 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020066 logbuf_assert("Invalid value \"2018-13-11\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020067 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020068 logbuf_assert("Invalid value \"2018-11-41\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020069 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020070 logbuf_assert("Invalid value \"2018-02-29\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020071 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020072 logbuf_assert("Invalid value \"2018.02-28\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020073 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
Radek Krejcia93621b2018-10-18 11:13:38 +020074 logbuf_assert("Invalid value \"2018-02.28\" of \"date\".");
Radek Krejci86d106e2018-10-18 09:53:19 +020075
76 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
77 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
78 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
79}
80
Radek Krejcia93621b2018-10-18 11:13:38 +020081static void
82test_revisions(void **state)
83{
84 (void) state; /* unused */
85
86 struct lysp_revision *revs = NULL, *rev;
87
88 /* no error, it just does nothing */
89 lysp_sort_revisions(NULL);
90 logbuf_assert("");
91
92 /* revisions are stored in wrong order - the newest is the last */
93 LYSP_ARRAY_NEW_RET(NULL, revs, rev,);
94 strcpy(rev->rev, "2018-01-01");
95 LYSP_ARRAY_NEW_RET(NULL, revs, rev,);
96 strcpy(rev->rev, "2018-12-31");
97
98 assert_int_equal(2, LY_ARRAY_SIZE(revs));
99 assert_string_equal("2018-01-01", LY_ARRAY_INDEX(revs, 0));
100 assert_string_equal("2018-12-31", LY_ARRAY_INDEX(revs, 1));
101 /* the order should be fixed, so the newest revision will be the first in the array */
102 lysp_sort_revisions(revs);
103 assert_string_equal("2018-12-31", LY_ARRAY_INDEX(revs, 0));
104 assert_string_equal("2018-01-01", LY_ARRAY_INDEX(revs, 1));
105
106 free(revs);
107}
108
Radek Krejci86d106e2018-10-18 09:53:19 +0200109int main(void)
110{
111 const struct CMUnitTest tests[] = {
112 cmocka_unit_test_setup(test_date, logger_setup),
Radek Krejcia93621b2018-10-18 11:13:38 +0200113 cmocka_unit_test_setup(test_revisions, logger_setup),
Radek Krejci86d106e2018-10-18 09:53:19 +0200114 };
115
116 return cmocka_run_group_tests(tests, NULL, NULL);
117}