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 | |
| 15 | #include "../../src/common.c" |
Radek Krejci | f3f4784 | 2018-11-15 11:22:15 +0100 | [diff] [blame] | 16 | #include "../../src/log.c" |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 17 | |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 18 | #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 |
| 26 | char logbuf[BUFSIZE] = {0}; |
| 27 | |
| 28 | static void |
| 29 | logger(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 | |
| 37 | static int |
| 38 | logger_setup(void **state) |
| 39 | { |
| 40 | (void) state; /* unused */ |
| 41 | |
| 42 | ly_set_log_clb(logger, 0); |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static void |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 48 | test_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 Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame^] | 86 | #ifndef APPLE |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 87 | void *__real_realloc(void *ptr, size_t size); |
| 88 | void *__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 | |
| 100 | static void |
| 101 | test_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 Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame^] | 123 | #endif /* not APPLE */ |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 124 | |
| 125 | int main(void) |
| 126 | { |
| 127 | const struct CMUnitTest tests[] = { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 128 | cmocka_unit_test_setup(test_utf8, logger_setup), |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame^] | 129 | #ifndef APPLE |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 130 | cmocka_unit_test(test_lyrealloc), |
Radek Krejci | 1b5ef9d | 2018-11-27 12:49:59 +0100 | [diff] [blame^] | 131 | #endif |
Radek Krejci | 36bac2b | 2018-09-19 11:15:29 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 135 | } |