blob: 046950fd3ae183ed9e47d227b49b73e0e792951d [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
Radek Krejci36bac2b2018-09-19 11:15:29 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
Radek Krejci2d7a47b2019-05-16 13:34:10 +020020#include "../../src/common.h"
Radek Krejci36bac2b2018-09-19 11:15:29 +020021
22#define BUFSIZE 1024
23char logbuf[BUFSIZE] = {0};
Radek Krejcib4a4a272019-06-10 12:44:52 +020024int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci36bac2b2018-09-19 11:15:29 +020025
Radek Krejcib4a4a272019-06-10 12:44:52 +020026/* set to 0 to printing error messages to stderr instead of checking them in code */
27#define ENABLE_LOGGER_CHECKING 1
28
29#if ENABLE_LOGGER_CHECKING
Radek Krejci36bac2b2018-09-19 11:15:29 +020030static void
31logger(LY_LOG_LEVEL level, const char *msg, const char *path)
32{
33 (void) level; /* unused */
Radek Krejcib4a4a272019-06-10 12:44:52 +020034 if (store) {
35 if (path && path[0]) {
36 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
37 } else {
38 strncpy(logbuf, msg, BUFSIZE - 1);
39 }
40 if (store > 0) {
41 --store;
42 }
43 }
Radek Krejci36bac2b2018-09-19 11:15:29 +020044}
Radek Krejcib4a4a272019-06-10 12:44:52 +020045#endif
Radek Krejci36bac2b2018-09-19 11:15:29 +020046
47static int
48logger_setup(void **state)
49{
50 (void) state; /* unused */
51
52 ly_set_log_clb(logger, 0);
53
54 return 0;
55}
56
Radek Krejcib4a4a272019-06-10 12:44:52 +020057static int
58logger_teardown(void **state)
59{
60 (void) state; /* unused */
61#if ENABLE_LOGGER_CHECKING
62 if (*state) {
63 fprintf(stderr, "%s\n", logbuf);
64 }
65#endif
66 return 0;
67}
68
69void
70logbuf_clean(void)
71{
72 logbuf[0] = '\0';
73}
74
75#if ENABLE_LOGGER_CHECKING
76# define logbuf_assert(str) assert_string_equal(logbuf, str)
77#else
78# define logbuf_assert(str)
79#endif
80
Radek Krejci36bac2b2018-09-19 11:15:29 +020081static void
Radek Krejci44ceedc2018-10-02 15:54:31 +020082test_utf8(void **state)
83{
84 (void) state; /* unused */
85
86 char buf[5] = {0};
87 const char *str = buf;
88 unsigned int c;
89 size_t len;
90
91 /* test invalid UTF-8 characters in lyxml_getutf8
92 * - https://en.wikipedia.org/wiki/UTF-8 */
93 buf[0] = 0x04;
94 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
95 buf[0] = 0x80;
96 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
97
98 buf[0] = 0xc0;
99 buf[1] = 0x00;
100 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
101 buf[1] = 0x80;
102 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
103
104 buf[0] = 0xe0;
105 buf[1] = 0x00;
106 buf[2] = 0x80;
107 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
108 buf[1] = 0x80;
109 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
110
111 buf[0] = 0xf0;
112 buf[1] = 0x00;
113 buf[2] = 0x80;
114 buf[3] = 0x80;
115 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
116 buf[1] = 0x80;
117 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
118}
119
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100120#ifndef APPLE
Radek Krejci36bac2b2018-09-19 11:15:29 +0200121void *__real_realloc(void *ptr, size_t size);
122void *__wrap_realloc(void *ptr, size_t size)
123{
124 int wrap = mock_type(int);
125
126 if (wrap) {
127 /* memory allocation failed */
128 return NULL;
129 } else {
130 return __real_realloc(ptr, size);
131 }
132}
133
134static void
135test_lyrealloc(void **state)
136{
137 (void) state; /* unused */
138
139 char *ptr;
140
141 ptr = malloc(1);
142 assert_non_null(ptr);
143
144 /* realloc */
145 will_return(__wrap_realloc, 0);
146 ptr = ly_realloc(ptr, 2048);
147 assert_non_null(ptr);
148 ptr[2047] = 0; /* test write */
149
150 /* realloc fails */
151 will_return(__wrap_realloc, 1);
152 ptr = ly_realloc(ptr, 2048);
153 assert_null(ptr);
154
155 /* ptr should be freed by ly_realloc() */
156}
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100157#endif /* not APPLE */
Radek Krejci36bac2b2018-09-19 11:15:29 +0200158
Radek Krejcib4a4a272019-06-10 12:44:52 +0200159static void
160test_parse_nodeid(void **state)
161{
162 (void) state; /* unused */
163 const char *str;
164 const char *prefix, *name;
165 size_t prefix_len, name_len;
166
167 str = "123";
168 assert_int_equal(LY_EINVAL, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len));
169
170 str = "a12_-.!";
171 assert_int_equal(LY_SUCCESS, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len));
172 assert_null(prefix);
173 assert_int_equal(0, prefix_len);
174 assert_non_null(name);
175 assert_int_equal(6, name_len);
176 assert_int_equal(0, strncmp("a12_-.", name, name_len));
177 assert_string_equal("!", str);
178
179 str = "a12_-.:_b2 xxx";
180 assert_int_equal(LY_SUCCESS, ly_parse_nodeid(&str, &prefix, &prefix_len, &name, &name_len));
181 assert_non_null(prefix);
182 assert_int_equal(6, prefix_len);
183 assert_int_equal(0, strncmp("a12_-.", prefix, prefix_len));
184 assert_non_null(name);
185 assert_int_equal(3, name_len);
186 assert_int_equal(0, strncmp("_b2", name, name_len));
187 assert_string_equal(" xxx", str);
188}
189
Radek Krejci36bac2b2018-09-19 11:15:29 +0200190int main(void)
191{
192 const struct CMUnitTest tests[] = {
Radek Krejcib4a4a272019-06-10 12:44:52 +0200193 cmocka_unit_test_setup_teardown(test_utf8, logger_setup, logger_teardown),
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100194#ifndef APPLE
Radek Krejci36bac2b2018-09-19 11:15:29 +0200195 cmocka_unit_test(test_lyrealloc),
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100196#endif
Radek Krejcib4a4a272019-06-10 12:44:52 +0200197 cmocka_unit_test_setup_teardown(test_parse_nodeid, logger_setup, logger_teardown),
Radek Krejci36bac2b2018-09-19 11:15:29 +0200198 };
199
200 return cmocka_run_group_tests(tests, NULL, NULL);
201}