blob: 92d34cb40145274973947d12666d3664d162f460 [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"
Radek Krejcibbbbda92019-05-16 12:16:28 +020016#include "../../src/compat.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010017#include "../../src/log.c"
Radek Krejci36bac2b2018-09-19 11:15:29 +020018
Radek Krejci36bac2b2018-09-19 11:15:29 +020019#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
Radek Krejci1b5ef9d2018-11-27 12:49:59 +010087#ifndef APPLE
Radek Krejci36bac2b2018-09-19 11:15:29 +020088void *__real_realloc(void *ptr, size_t size);
89void *__wrap_realloc(void *ptr, size_t size)
90{
91 int wrap = mock_type(int);
92
93 if (wrap) {
94 /* memory allocation failed */
95 return NULL;
96 } else {
97 return __real_realloc(ptr, size);
98 }
99}
100
101static void
102test_lyrealloc(void **state)
103{
104 (void) state; /* unused */
105
106 char *ptr;
107
108 ptr = malloc(1);
109 assert_non_null(ptr);
110
111 /* realloc */
112 will_return(__wrap_realloc, 0);
113 ptr = ly_realloc(ptr, 2048);
114 assert_non_null(ptr);
115 ptr[2047] = 0; /* test write */
116
117 /* realloc fails */
118 will_return(__wrap_realloc, 1);
119 ptr = ly_realloc(ptr, 2048);
120 assert_null(ptr);
121
122 /* ptr should be freed by ly_realloc() */
123}
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100124#endif /* not APPLE */
Radek Krejci36bac2b2018-09-19 11:15:29 +0200125
126int main(void)
127{
128 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200129 cmocka_unit_test_setup(test_utf8, logger_setup),
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100130#ifndef APPLE
Radek Krejci36bac2b2018-09-19 11:15:29 +0200131 cmocka_unit_test(test_lyrealloc),
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100132#endif
Radek Krejci36bac2b2018-09-19 11:15:29 +0200133 };
134
135 return cmocka_run_group_tests(tests, NULL, NULL);
136}