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