blob: cfe169e043b40ab0f21e63a635af27750cd7574e [file] [log] [blame]
Radek Krejci36bac2b2018-09-19 11:15:29 +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/common.c"
16
17#define _BSD_SOURCE
18#define _DEFAULT_SOURCE
19#include <stdarg.h>
20#include <stddef.h>
21#include <setjmp.h>
22#include <cmocka.h>
23
24#include "libyang.h"
25
26#define BUFSIZE 1024
27char logbuf[BUFSIZE] = {0};
28
29static void
30logger(LY_LOG_LEVEL level, const char *msg, const char *path)
31{
32 (void) level; /* unused */
33 (void) path; /* unused */
34
35 strncpy(logbuf, msg, BUFSIZE - 1);
36}
37
38static int
39logger_setup(void **state)
40{
41 (void) state; /* unused */
42
43 ly_set_log_clb(logger, 0);
44
45 return 0;
46}
47
48static void
49test_date(void **state)
50{
51 (void) state; /* unused */
52
53 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
54 assert_string_equal(logbuf, "Invalid argument date (lysp_check_date()).");
55 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
56 assert_string_equal(logbuf, "Invalid argument date_len (lysp_check_date()).");
57 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
58 assert_string_equal(logbuf, "Invalid value \"nonsencexx\" of \"date\".");
59 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
60 assert_string_equal(logbuf, "Invalid value \"123x-11-11\" of \"date\".");
61 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
62 assert_string_equal(logbuf, "Invalid value \"2018-13-11\" of \"date\".");
63 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
64 assert_string_equal(logbuf, "Invalid value \"2018-11-41\" of \"date\".");
65 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
66 assert_string_equal(logbuf, "Invalid value \"2018-02-29\" of \"date\".");
Radek Krejci8e57d5a2018-09-19 11:29:09 +020067 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
68 assert_string_equal(logbuf, "Invalid value \"2018.02-28\" of \"date\".");
69 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
70 assert_string_equal(logbuf, "Invalid value \"2018-02.28\" of \"date\".");
Radek Krejci36bac2b2018-09-19 11:15:29 +020071
72 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
73 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
74 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
75}
76
77void *__real_realloc(void *ptr, size_t size);
78void *__wrap_realloc(void *ptr, size_t size)
79{
80 int wrap = mock_type(int);
81
82 if (wrap) {
83 /* memory allocation failed */
84 return NULL;
85 } else {
86 return __real_realloc(ptr, size);
87 }
88}
89
90static void
91test_lyrealloc(void **state)
92{
93 (void) state; /* unused */
94
95 char *ptr;
96
97 ptr = malloc(1);
98 assert_non_null(ptr);
99
100 /* realloc */
101 will_return(__wrap_realloc, 0);
102 ptr = ly_realloc(ptr, 2048);
103 assert_non_null(ptr);
104 ptr[2047] = 0; /* test write */
105
106 /* realloc fails */
107 will_return(__wrap_realloc, 1);
108 ptr = ly_realloc(ptr, 2048);
109 assert_null(ptr);
110
111 /* ptr should be freed by ly_realloc() */
112}
113
114int main(void)
115{
116 const struct CMUnitTest tests[] = {
117 cmocka_unit_test_setup(test_date, logger_setup),
118 cmocka_unit_test(test_lyrealloc),
119 };
120
121 return cmocka_run_group_tests(tests, NULL, NULL);
122}