blob: 0499c42abfc114e87971339fa37cdfc4d157dec4 [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
Radek Krejci36bac2b2018-09-19 11:15:29 +020017#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21
22#include "libyang.h"
23
24#define BUFSIZE 1024
25char logbuf[BUFSIZE] = {0};
26
27static void
28logger(LY_LOG_LEVEL level, const char *msg, const char *path)
29{
30 (void) level; /* unused */
31 (void) path; /* unused */
32
33 strncpy(logbuf, msg, BUFSIZE - 1);
34}
35
36static int
37logger_setup(void **state)
38{
39 (void) state; /* unused */
40
41 ly_set_log_clb(logger, 0);
42
43 return 0;
44}
45
46static void
Radek Krejci44ceedc2018-10-02 15:54:31 +020047test_utf8(void **state)
48{
49 (void) state; /* unused */
50
51 char buf[5] = {0};
52 const char *str = buf;
53 unsigned int c;
54 size_t len;
55
56 /* test invalid UTF-8 characters in lyxml_getutf8
57 * - https://en.wikipedia.org/wiki/UTF-8 */
58 buf[0] = 0x04;
59 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
60 buf[0] = 0x80;
61 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
62
63 buf[0] = 0xc0;
64 buf[1] = 0x00;
65 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
66 buf[1] = 0x80;
67 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
68
69 buf[0] = 0xe0;
70 buf[1] = 0x00;
71 buf[2] = 0x80;
72 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
73 buf[1] = 0x80;
74 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
75
76 buf[0] = 0xf0;
77 buf[1] = 0x00;
78 buf[2] = 0x80;
79 buf[3] = 0x80;
80 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
81 buf[1] = 0x80;
82 assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
83}
84
Radek Krejci36bac2b2018-09-19 11:15:29 +020085void *__real_realloc(void *ptr, size_t size);
86void *__wrap_realloc(void *ptr, size_t size)
87{
88 int wrap = mock_type(int);
89
90 if (wrap) {
91 /* memory allocation failed */
92 return NULL;
93 } else {
94 return __real_realloc(ptr, size);
95 }
96}
97
98static void
99test_lyrealloc(void **state)
100{
101 (void) state; /* unused */
102
103 char *ptr;
104
105 ptr = malloc(1);
106 assert_non_null(ptr);
107
108 /* realloc */
109 will_return(__wrap_realloc, 0);
110 ptr = ly_realloc(ptr, 2048);
111 assert_non_null(ptr);
112 ptr[2047] = 0; /* test write */
113
114 /* realloc fails */
115 will_return(__wrap_realloc, 1);
116 ptr = ly_realloc(ptr, 2048);
117 assert_null(ptr);
118
119 /* ptr should be freed by ly_realloc() */
120}
121
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 Krejci36bac2b2018-09-19 11:15:29 +0200126 cmocka_unit_test(test_lyrealloc),
127 };
128
129 return cmocka_run_group_tests(tests, NULL, NULL);
130}