blob: 60c4f4888b2409aeb76b77d8bcb54f5351aa08cf [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
Radek Krejci44ceedc2018-10-02 15:54:31 +020049test_utf8(void **state)
50{
51 (void) state; /* unused */
52
53 char buf[5] = {0};
54 const char *str = buf;
55 unsigned int c;
56 size_t len;
57
58 /* test invalid UTF-8 characters in lyxml_getutf8
59 * - https://en.wikipedia.org/wiki/UTF-8 */
60 buf[0] = 0x04;
61 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
62 buf[0] = 0x80;
63 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
64
65 buf[0] = 0xc0;
66 buf[1] = 0x00;
67 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
68 buf[1] = 0x80;
69 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
70
71 buf[0] = 0xe0;
72 buf[1] = 0x00;
73 buf[2] = 0x80;
74 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
75 buf[1] = 0x80;
76 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
77
78 buf[0] = 0xf0;
79 buf[1] = 0x00;
80 buf[2] = 0x80;
81 buf[3] = 0x80;
82 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
83 buf[1] = 0x80;
84 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
85}
86
87static void
Radek Krejci36bac2b2018-09-19 11:15:29 +020088test_date(void **state)
89{
90 (void) state; /* unused */
91
92 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
93 assert_string_equal(logbuf, "Invalid argument date (lysp_check_date()).");
94 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
95 assert_string_equal(logbuf, "Invalid argument date_len (lysp_check_date()).");
96 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
97 assert_string_equal(logbuf, "Invalid value \"nonsencexx\" of \"date\".");
98 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
99 assert_string_equal(logbuf, "Invalid value \"123x-11-11\" of \"date\".");
100 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
101 assert_string_equal(logbuf, "Invalid value \"2018-13-11\" of \"date\".");
102 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
103 assert_string_equal(logbuf, "Invalid value \"2018-11-41\" of \"date\".");
104 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
105 assert_string_equal(logbuf, "Invalid value \"2018-02-29\" of \"date\".");
Radek Krejci8e57d5a2018-09-19 11:29:09 +0200106 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
107 assert_string_equal(logbuf, "Invalid value \"2018.02-28\" of \"date\".");
108 assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
109 assert_string_equal(logbuf, "Invalid value \"2018-02.28\" of \"date\".");
Radek Krejci36bac2b2018-09-19 11:15:29 +0200110
111 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
112 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
113 assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
114}
115
116void *__real_realloc(void *ptr, size_t size);
117void *__wrap_realloc(void *ptr, size_t size)
118{
119 int wrap = mock_type(int);
120
121 if (wrap) {
122 /* memory allocation failed */
123 return NULL;
124 } else {
125 return __real_realloc(ptr, size);
126 }
127}
128
129static void
130test_lyrealloc(void **state)
131{
132 (void) state; /* unused */
133
134 char *ptr;
135
136 ptr = malloc(1);
137 assert_non_null(ptr);
138
139 /* realloc */
140 will_return(__wrap_realloc, 0);
141 ptr = ly_realloc(ptr, 2048);
142 assert_non_null(ptr);
143 ptr[2047] = 0; /* test write */
144
145 /* realloc fails */
146 will_return(__wrap_realloc, 1);
147 ptr = ly_realloc(ptr, 2048);
148 assert_null(ptr);
149
150 /* ptr should be freed by ly_realloc() */
151}
152
153int main(void)
154{
155 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200156 cmocka_unit_test_setup(test_utf8, logger_setup),
Radek Krejci36bac2b2018-09-19 11:15:29 +0200157 cmocka_unit_test_setup(test_date, logger_setup),
158 cmocka_unit_test(test_lyrealloc),
159 };
160
161 return cmocka_run_group_tests(tests, NULL, NULL);
162}