Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 1 | /* |
| 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 Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 15 | #include <stdarg.h> |
| 16 | #include <stddef.h> |
| 17 | #include <setjmp.h> |
| 18 | #include <cmocka.h> |
| 19 | |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 20 | #include "../../src/common.h" |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 21 | |
| 22 | #define BUFSIZE 1024 |
| 23 | char logbuf[BUFSIZE] = {0}; |
| 24 | |
| 25 | static void |
| 26 | logger(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 | |
| 34 | static int |
| 35 | logger_setup(void **state) |
| 36 | { |
| 37 | (void) state; /* unused */ |
| 38 | |
| 39 | ly_set_log_clb(logger, 0); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static void |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 45 | test_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 Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 83 | #ifndef APPLE |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 84 | void *__real_realloc(void *ptr, size_t size); |
| 85 | void *__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 | |
| 97 | static void |
| 98 | test_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 Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 120 | #endif /* not APPLE */ |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 121 | |
| 122 | int main(void) |
| 123 | { |
| 124 | const struct CMUnitTest tests[] = { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 125 | cmocka_unit_test_setup(test_utf8, logger_setup), |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 126 | #ifndef APPLE |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 127 | cmocka_unit_test(test_lyrealloc), |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame] | 128 | #endif |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 132 | } |