|
| 1 | +/* |
| 2 | + * OpenVPN -- An application to securely tunnel IP networks |
| 3 | + * over a single UDP port, with support for SSL/TLS-based |
| 4 | + * session authentication and key exchange, |
| 5 | + * packet encryption, packet authentication, and |
| 6 | + * packet compression. |
| 7 | + * |
| 8 | + * Copyright (C) 2025 OpenVPN Inc. <[email protected]> |
| 9 | + * |
| 10 | + * This program is free software; you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License version 2 |
| 12 | + * as published by the Free Software Foundation. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License along |
| 20 | + * with this program; if not, see <https://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | + |
| 23 | +#ifdef HAVE_CONFIG_H |
| 24 | +#include "config.h" |
| 25 | +#endif |
| 26 | + |
| 27 | +#include "syshead.h" |
| 28 | + |
| 29 | +#include <setjmp.h> |
| 30 | +#include <cmocka.h> |
| 31 | + |
| 32 | +#include "buffer.h" |
| 33 | +#include "multi.h" |
| 34 | +#include "mbuf.h" |
| 35 | +#include "test_common.h" |
| 36 | + |
| 37 | +static void |
| 38 | +test_mbuf_init(void **state) |
| 39 | +{ |
| 40 | + struct mbuf_set *ms = mbuf_init(256); |
| 41 | + assert_int_equal(ms->capacity, 256); |
| 42 | + assert_false(mbuf_defined(ms)); |
| 43 | + assert_non_null(ms->array); |
| 44 | + mbuf_free(ms); |
| 45 | + |
| 46 | + ms = mbuf_init(257); |
| 47 | + assert_int_equal(ms->capacity, 512); |
| 48 | + mbuf_free(ms); |
| 49 | + |
| 50 | +#ifdef UNIT_TEST_ALLOW_BIG_ALLOC /* allocates up to 2GB of memory */ |
| 51 | + ms = mbuf_init(MBUF_SIZE_MAX); |
| 52 | + assert_int_equal(ms->capacity, MBUF_SIZE_MAX); |
| 53 | + mbuf_free(ms); |
| 54 | + |
| 55 | +/* NOTE: expect_assert_failure does not seem to work with MSVC */ |
| 56 | +#ifndef _MSC_VER |
| 57 | + expect_assert_failure(mbuf_init(MBUF_SIZE_MAX + 1)); |
| 58 | +#endif |
| 59 | +#endif |
| 60 | +} |
| 61 | + |
| 62 | +static void |
| 63 | +test_mbuf_add_remove(void **state) |
| 64 | +{ |
| 65 | + struct mbuf_set *ms = mbuf_init(4); |
| 66 | + assert_int_equal(ms->capacity, 4); |
| 67 | + assert_false(mbuf_defined(ms)); |
| 68 | + assert_non_null(ms->array); |
| 69 | + |
| 70 | + /* instances */ |
| 71 | + struct multi_instance mi = { 0 }; |
| 72 | + struct multi_instance mi2 = { 0 }; |
| 73 | + /* buffers */ |
| 74 | + struct buffer buf = alloc_buf(16); |
| 75 | + struct mbuf_buffer *mbuf_buf = mbuf_alloc_buf(&buf); |
| 76 | + assert_int_equal(mbuf_buf->refcount, 1); |
| 77 | + struct mbuf_buffer *mbuf_buf2 = mbuf_alloc_buf(&buf); |
| 78 | + assert_int_equal(mbuf_buf2->refcount, 1); |
| 79 | + free_buf(&buf); |
| 80 | + /* items */ |
| 81 | + struct mbuf_item mb_item = { .buffer = mbuf_buf, .instance = &mi }; |
| 82 | + struct mbuf_item mb_item2 = { .buffer = mbuf_buf2, .instance = &mi2 }; |
| 83 | + |
| 84 | + mbuf_add_item(ms, &mb_item); |
| 85 | + assert_int_equal(mbuf_buf->refcount, 2); |
| 86 | + assert_int_equal(mbuf_buf2->refcount, 1); |
| 87 | + assert_int_equal(mbuf_len(ms), 1); |
| 88 | + assert_int_equal(mbuf_maximum_queued(ms), 1); |
| 89 | + assert_int_equal(ms->head, 0); |
| 90 | + assert_ptr_equal(mbuf_peek(ms), &mi); |
| 91 | + |
| 92 | + mbuf_add_item(ms, &mb_item2); |
| 93 | + assert_int_equal(mbuf_buf->refcount, 2); |
| 94 | + assert_int_equal(mbuf_buf2->refcount, 2); |
| 95 | + assert_int_equal(mbuf_len(ms), 2); |
| 96 | + assert_int_equal(mbuf_maximum_queued(ms), 2); |
| 97 | + assert_int_equal(ms->head, 0); |
| 98 | + assert_ptr_equal(mbuf_peek(ms), &mi); |
| 99 | + |
| 100 | + mbuf_add_item(ms, &mb_item2); |
| 101 | + assert_int_equal(mbuf_buf->refcount, 2); |
| 102 | + assert_int_equal(mbuf_buf2->refcount, 3); |
| 103 | + assert_int_equal(mbuf_len(ms), 3); |
| 104 | + assert_int_equal(mbuf_maximum_queued(ms), 3); |
| 105 | + assert_int_equal(ms->head, 0); |
| 106 | + assert_ptr_equal(mbuf_peek(ms), &mi); |
| 107 | + |
| 108 | + mbuf_add_item(ms, &mb_item2); |
| 109 | + mbuf_add_item(ms, &mb_item2); /* overflow, first item gets removed */ |
| 110 | + assert_int_equal(mbuf_buf->refcount, 1); |
| 111 | + assert_int_equal(mbuf_buf2->refcount, 5); |
| 112 | + assert_int_equal(mbuf_len(ms), 4); |
| 113 | + assert_int_equal(mbuf_maximum_queued(ms), 4); |
| 114 | + assert_int_equal(ms->head, 1); |
| 115 | + assert_ptr_equal(mbuf_peek(ms), &mi2); |
| 116 | + |
| 117 | + mbuf_add_item(ms, &mb_item); |
| 118 | + assert_int_equal(mbuf_buf->refcount, 2); |
| 119 | + assert_int_equal(mbuf_buf2->refcount, 4); |
| 120 | + assert_int_equal(mbuf_len(ms), 4); |
| 121 | + assert_int_equal(mbuf_maximum_queued(ms), 4); |
| 122 | + assert_int_equal(ms->head, 2); |
| 123 | + assert_ptr_equal(mbuf_peek(ms), &mi2); |
| 124 | + |
| 125 | + struct mbuf_item out_item; |
| 126 | + assert_true(mbuf_extract_item(ms, &out_item)); |
| 127 | + assert_ptr_equal(out_item.instance, mb_item2.instance); |
| 128 | + assert_int_equal(mbuf_buf->refcount, 2); |
| 129 | + assert_int_equal(mbuf_buf2->refcount, 4); |
| 130 | + assert_int_equal(mbuf_len(ms), 3); |
| 131 | + assert_int_equal(mbuf_maximum_queued(ms), 4); |
| 132 | + assert_int_equal(ms->head, 3); |
| 133 | + assert_ptr_equal(mbuf_peek(ms), &mi2); |
| 134 | + mbuf_free_buf(out_item.buffer); |
| 135 | + |
| 136 | + mbuf_dereference_instance(ms, &mi2); |
| 137 | + assert_int_equal(mbuf_buf->refcount, 2); |
| 138 | + assert_int_equal(mbuf_buf2->refcount, 1); |
| 139 | + assert_int_equal(mbuf_len(ms), 3); |
| 140 | + assert_int_equal(mbuf_maximum_queued(ms), 4); |
| 141 | + assert_int_equal(ms->head, 3); |
| 142 | + assert_ptr_equal(mbuf_peek(ms), &mi); |
| 143 | + |
| 144 | + mbuf_free(ms); |
| 145 | + assert_int_equal(mbuf_buf->refcount, 1); |
| 146 | + mbuf_free_buf(mbuf_buf); |
| 147 | + assert_int_equal(mbuf_buf2->refcount, 1); |
| 148 | + mbuf_free_buf(mbuf_buf2); |
| 149 | +} |
| 150 | + |
| 151 | +int |
| 152 | +main(void) |
| 153 | +{ |
| 154 | + const struct CMUnitTest tests[] = { |
| 155 | + cmocka_unit_test(test_mbuf_init), |
| 156 | + cmocka_unit_test(test_mbuf_add_remove), |
| 157 | + }; |
| 158 | + |
| 159 | + return cmocka_run_group_tests_name("mbuf", tests, NULL, NULL); |
| 160 | +} |
0 commit comments