blob: b79f3ec3b290be0aca11f3a57640d4c7842669c8 [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};
24
25static void
26logger(LY_LOG_LEVEL level, const char *msg, const char *path)
27{
28 (void) level; /* unused */
29 (void) path; /* unused */
30
31 strncpy(logbuf, msg, BUFSIZE - 1);
32}
33
34static int
35logger_setup(void **state)
36{
37 (void) state; /* unused */
38
39 ly_set_log_clb(logger, 0);
40
41 return 0;
42}
43
44static void
Radek Krejci44ceedc2018-10-02 15:54:31 +020045test_utf8(void **state)
46{
47 (void) state; /* unused */
48
49 char buf[5] = {0};
50 const char *str = buf;
51 unsigned int c;
52 size_t len;
53
54 /* test invalid UTF-8 characters in lyxml_getutf8
55 * - https://en.wikipedia.org/wiki/UTF-8 */
56 buf[0] = 0x04;
57 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
58 buf[0] = 0x80;
59 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
60
61 buf[0] = 0xc0;
62 buf[1] = 0x00;
63 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
64 buf[1] = 0x80;
65 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
66
67 buf[0] = 0xe0;
68 buf[1] = 0x00;
69 buf[2] = 0x80;
70 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
71 buf[1] = 0x80;
72 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
73
74 buf[0] = 0xf0;
75 buf[1] = 0x00;
76 buf[2] = 0x80;
77 buf[3] = 0x80;
78 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
79 buf[1] = 0x80;
80 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
81}
82
Radek Krejci1b5ef9d2018-11-27 12:49:59 +010083#ifndef APPLE
Radek Krejci36bac2b2018-09-19 11:15:29 +020084void *__real_realloc(void *ptr, size_t size);
85void *__wrap_realloc(void *ptr, size_t size)
86{
87 int wrap = mock_type(int);
88
89 if (wrap) {
90 /* memory allocation failed */
91 return NULL;
92 } else {
93 return __real_realloc(ptr, size);
94 }
95}
96
97static void
98test_lyrealloc(void **state)
99{
100 (void) state; /* unused */
101
102 char *ptr;
103
104 ptr = malloc(1);
105 assert_non_null(ptr);
106
107 /* realloc */
108 will_return(__wrap_realloc, 0);
109 ptr = ly_realloc(ptr, 2048);
110 assert_non_null(ptr);
111 ptr[2047] = 0; /* test write */
112
113 /* realloc fails */
114 will_return(__wrap_realloc, 1);
115 ptr = ly_realloc(ptr, 2048);
116 assert_null(ptr);
117
118 /* ptr should be freed by ly_realloc() */
119}
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100120#endif /* not APPLE */
Radek Krejci36bac2b2018-09-19 11:15:29 +0200121
122int main(void)
123{
124 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200125 cmocka_unit_test_setup(test_utf8, logger_setup),
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100126#ifndef APPLE
Radek Krejci36bac2b2018-09-19 11:15:29 +0200127 cmocka_unit_test(test_lyrealloc),
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100128#endif
Radek Krejci36bac2b2018-09-19 11:15:29 +0200129 };
130
131 return cmocka_run_group_tests(tests, NULL, NULL);
132}