blob: 13da39b6dde40613003a94c0adde698b3d217ad8 [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 Krejci1b5ef9d2018-11-27 12:49:59 +010086#ifndef APPLE
Radek Krejci36bac2b2018-09-19 11:15:29 +020087void *__real_realloc(void *ptr, size_t size);
88void *__wrap_realloc(void *ptr, size_t size)
89{
90 int wrap = mock_type(int);
91
92 if (wrap) {
93 /* memory allocation failed */
94 return NULL;
95 } else {
96 return __real_realloc(ptr, size);
97 }
98}
99
100static void
101test_lyrealloc(void **state)
102{
103 (void) state; /* unused */
104
105 char *ptr;
106
107 ptr = malloc(1);
108 assert_non_null(ptr);
109
110 /* realloc */
111 will_return(__wrap_realloc, 0);
112 ptr = ly_realloc(ptr, 2048);
113 assert_non_null(ptr);
114 ptr[2047] = 0; /* test write */
115
116 /* realloc fails */
117 will_return(__wrap_realloc, 1);
118 ptr = ly_realloc(ptr, 2048);
119 assert_null(ptr);
120
121 /* ptr should be freed by ly_realloc() */
122}
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100123#endif /* not APPLE */
Radek Krejci36bac2b2018-09-19 11:15:29 +0200124
125int main(void)
126{
127 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200128 cmocka_unit_test_setup(test_utf8, logger_setup),
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100129#ifndef APPLE
Radek Krejci36bac2b2018-09-19 11:15:29 +0200130 cmocka_unit_test(test_lyrealloc),
Radek Krejci1b5ef9d2018-11-27 12:49:59 +0100131#endif
Radek Krejci36bac2b2018-09-19 11:15:29 +0200132 };
133
134 return cmocka_run_group_tests(tests, NULL, NULL);
135}