| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <fcntl.h> |
| 15 |
#include <inttypes.h> |
| 16 |
#include <limits.h> |
| 17 |
#include <sched.h> /* IWYU pragma: keep */ |
| 18 |
#include <setjmp.h> |
| 19 |
#include <stdarg.h> |
| 20 |
#include <stdbool.h> |
| 21 |
#include <stddef.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <unistd.h> |
| 25 |
|
| 26 |
#define UNIT_TESTING |
| 27 |
#include <cmocka.h> |
| 28 |
|
| 29 |
#include <isc/buffer.h> |
| 30 |
#include <isc/region.h> |
| 31 |
#include <isc/result.h> |
| 32 |
#include <isc/types.h> |
| 33 |
#include <isc/util.h> |
| 34 |
|
| 35 |
#include <tests/isc.h> |
| 36 |
|
| 37 |
/* reserve space in dynamic buffers */ |
| 38 |
ISC_RUN_TEST_IMPL(isc_buffer_reserve) { |
| 39 |
isc_result_t result; |
| 40 |
isc_buffer_t *b; |
| 41 |
|
| 42 |
UNUSED(state); |
| 43 |
|
| 44 |
b = NULL; |
| 45 |
isc_buffer_allocate(mctx, &b, ISC_BUFFER_INCR); |
| 46 |
assert_int_equal(b->length, ISC_BUFFER_INCR); |
| 47 |
|
| 48 |
/* |
| 49 |
* 512 bytes should already be available, so this call does |
| 50 |
* nothing. |
| 51 |
*/ |
| 52 |
result = isc_buffer_reserve(b, 512); |
| 53 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 54 |
assert_non_null(b); |
| 55 |
assert_int_equal(b->length, ISC_BUFFER_INCR); |
| 56 |
|
| 57 |
/* |
| 58 |
* This call should grow it to 1536 bytes as only 1024 bytes are |
| 59 |
* available in the buffer. |
| 60 |
*/ |
| 61 |
result = isc_buffer_reserve(b, 1025); |
| 62 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 63 |
assert_non_null(b); |
| 64 |
assert_int_equal(b->length, 3 * ISC_BUFFER_INCR); |
| 65 |
|
| 66 |
/* |
| 67 |
* 1536 bytes should already be available, so this call does |
| 68 |
* nothing. |
| 69 |
*/ |
| 70 |
result = isc_buffer_reserve(b, 1500); |
| 71 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 72 |
assert_non_null(b); |
| 73 |
assert_int_equal(b->length, 3 * ISC_BUFFER_INCR); |
| 74 |
|
| 75 |
/* |
| 76 |
* This call should grow it to 4096 bytes as only 1536 bytes are |
| 77 |
* available in the buffer. |
| 78 |
*/ |
| 79 |
result = isc_buffer_reserve(b, 3585); |
| 80 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 81 |
assert_non_null(b); |
| 82 |
assert_int_equal(b->length, 8 * ISC_BUFFER_INCR); |
| 83 |
|
| 84 |
/* Consume some of the buffer so we can run the next test. */ |
| 85 |
isc_buffer_add(b, 4096); |
| 86 |
|
| 87 |
/* |
| 88 |
* This call should fail and leave buffer untouched. |
| 89 |
*/ |
| 90 |
result = isc_buffer_reserve(b, UINT_MAX); |
| 91 |
assert_int_equal(result, ISC_R_NOMEMORY); |
| 92 |
assert_non_null(b); |
| 93 |
assert_int_equal(b->length, 8 * ISC_BUFFER_INCR); |
| 94 |
|
| 95 |
isc_buffer_free(&b); |
| 96 |
} |
| 97 |
|
| 98 |
/* dynamic buffer automatic reallocation */ |
| 99 |
ISC_RUN_TEST_IMPL(isc_buffer_dynamic) { |
| 100 |
isc_buffer_t *b; |
| 101 |
size_t last_length = 10; |
| 102 |
int i; |
| 103 |
|
| 104 |
UNUSED(state); |
| 105 |
|
| 106 |
b = NULL; |
| 107 |
isc_buffer_allocate(mctx, &b, last_length); |
| 108 |
assert_non_null(b); |
| 109 |
assert_int_equal(b->length, last_length); |
| 110 |
|
| 111 |
isc_buffer_putuint8(b, 1); |
| 112 |
|
| 113 |
for (i = 0; i < 1000; i++) { |
| 114 |
isc_buffer_putstr(b, "thisisa24charslongstring"); |
| 115 |
} |
| 116 |
assert_true(b->length - last_length >= 1000 * 24); |
| 117 |
last_length += 1000 * 24; |
| 118 |
|
| 119 |
for (i = 0; i < 10000; i++) { |
| 120 |
isc_buffer_putuint8(b, 1); |
| 121 |
} |
| 122 |
|
| 123 |
assert_true(b->length - last_length >= 10000 * 1); |
| 124 |
last_length += 10000 * 1; |
| 125 |
|
| 126 |
for (i = 0; i < 10000; i++) { |
| 127 |
isc_buffer_putuint16(b, 1); |
| 128 |
} |
| 129 |
|
| 130 |
assert_true(b->length - last_length >= 10000 * 2); |
| 131 |
|
| 132 |
for (i = 0; i < 10000; i++) { |
| 133 |
isc_buffer_putuint32(b, 1); |
| 134 |
} |
| 135 |
assert_true(b->length - last_length >= 10000 * 4); |
| 136 |
|
| 137 |
isc_buffer_free(&b); |
| 138 |
} |
| 139 |
|
| 140 |
/* copy a region into a buffer */ |
| 141 |
ISC_RUN_TEST_IMPL(isc_buffer_copyregion) { |
| 142 |
unsigned char data[] = { 0x11, 0x22, 0x33, 0x44 }; |
| 143 |
isc_buffer_t *b = NULL; |
| 144 |
isc_result_t result; |
| 145 |
|
| 146 |
isc_region_t r = { |
| 147 |
.base = data, |
| 148 |
.length = sizeof(data), |
| 149 |
}; |
| 150 |
|
| 151 |
UNUSED(state); |
| 152 |
|
| 153 |
isc_buffer_allocate(mctx, &b, sizeof(data)); |
| 154 |
|
| 155 |
/* |
| 156 |
* Fill originally allocated buffer space. |
| 157 |
*/ |
| 158 |
result = isc_buffer_copyregion(b, &r); |
| 159 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 160 |
|
| 161 |
/* |
| 162 |
* Appending should succeed. |
| 163 |
*/ |
| 164 |
result = isc_buffer_copyregion(b, &r); |
| 165 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 166 |
|
| 167 |
isc_buffer_free(&b); |
| 168 |
} |
| 169 |
|
| 170 |
/* sprintf() into a buffer */ |
| 171 |
ISC_RUN_TEST_IMPL(isc_buffer_printf) { |
| 172 |
unsigned int used, prev_used; |
| 173 |
const char *empty_fmt; |
| 174 |
isc_result_t result; |
| 175 |
isc_buffer_t *b, sb; |
| 176 |
char buf[8]; |
| 177 |
|
| 178 |
UNUSED(state); |
| 179 |
|
| 180 |
/* |
| 181 |
* Prepare a buffer with auto-reallocation enabled. |
| 182 |
*/ |
| 183 |
b = NULL; |
| 184 |
isc_buffer_allocate(mctx, &b, 0); |
| 185 |
|
| 186 |
/* |
| 187 |
* Sanity check. |
| 188 |
*/ |
| 189 |
result = isc_buffer_printf(b, "foo"); |
| 190 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 191 |
used = isc_buffer_usedlength(b); |
| 192 |
assert_int_equal(used, 3); |
| 193 |
|
| 194 |
result = isc_buffer_printf(b, "bar"); |
| 195 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 196 |
used = isc_buffer_usedlength(b); |
| 197 |
assert_int_equal(used, 3 + 3); |
| 198 |
|
| 199 |
/* |
| 200 |
* Also check the terminating NULL byte is there, even though it is not |
| 201 |
* part of the buffer's used region. |
| 202 |
*/ |
| 203 |
assert_memory_equal(isc_buffer_current(b), "foobar", 7); |
| 204 |
|
| 205 |
/* |
| 206 |
* Skip over data from previous check to prevent failures in previous |
| 207 |
* check from affecting this one. |
| 208 |
*/ |
| 209 |
prev_used = used; |
| 210 |
isc_buffer_forward(b, prev_used); |
| 211 |
|
| 212 |
/* |
| 213 |
* Some standard usage checks. |
| 214 |
*/ |
| 215 |
isc_buffer_printf(b, "%d", 42); |
| 216 |
used = isc_buffer_usedlength(b); |
| 217 |
assert_int_equal(used - prev_used, 2); |
| 218 |
|
| 219 |
isc_buffer_printf(b, "baz%1X", 42); |
| 220 |
used = isc_buffer_usedlength(b); |
| 221 |
assert_int_equal(used - prev_used, 2 + 5); |
| 222 |
|
| 223 |
isc_buffer_printf(b, "%6.1f", 42.42f); |
| 224 |
used = isc_buffer_usedlength(b); |
| 225 |
assert_int_equal(used - prev_used, 2 + 5 + 6); |
| 226 |
|
| 227 |
/* |
| 228 |
* Also check the terminating NULL byte is there, even though it is not |
| 229 |
* part of the buffer's used region. |
| 230 |
*/ |
| 231 |
assert_memory_equal(isc_buffer_current(b), "42baz2A 42.4", 14); |
| 232 |
|
| 233 |
/* |
| 234 |
* Check an empty format string is properly handled. |
| 235 |
* |
| 236 |
* Note: we don't use a string literal for the format string to |
| 237 |
* avoid triggering [-Werror=format-zero-length]. |
| 238 |
* Note: we have a dummy third argument as some compilers complain |
| 239 |
* without it. |
| 240 |
*/ |
| 241 |
prev_used = used; |
| 242 |
empty_fmt = ""; |
| 243 |
result = isc_buffer_printf(b, empty_fmt, ""); |
| 244 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 245 |
used = isc_buffer_usedlength(b); |
| 246 |
assert_int_equal(prev_used, used); |
| 247 |
|
| 248 |
isc_buffer_free(&b); |
| 249 |
|
| 250 |
/* |
| 251 |
* Check overflow on a static buffer. |
| 252 |
*/ |
| 253 |
isc_buffer_init(&sb, buf, sizeof(buf)); |
| 254 |
result = isc_buffer_printf(&sb, "123456"); |
| 255 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 256 |
used = isc_buffer_usedlength(&sb); |
| 257 |
assert_int_equal(used, 6); |
| 258 |
|
| 259 |
result = isc_buffer_printf(&sb, "789"); |
| 260 |
assert_int_equal(result, ISC_R_NOSPACE); |
| 261 |
used = isc_buffer_usedlength(&sb); |
| 262 |
assert_int_equal(used, 6); |
| 263 |
|
| 264 |
result = isc_buffer_printf(&sb, "78"); |
| 265 |
assert_int_equal(result, ISC_R_NOSPACE); |
| 266 |
used = isc_buffer_usedlength(&sb); |
| 267 |
assert_int_equal(used, 6); |
| 268 |
|
| 269 |
result = isc_buffer_printf(&sb, "7"); |
| 270 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 271 |
used = isc_buffer_usedlength(&sb); |
| 272 |
assert_int_equal(used, 7); |
| 273 |
} |
| 274 |
|
| 275 |
ISC_TEST_LIST_START |
| 276 |
|
| 277 |
ISC_TEST_ENTRY(isc_buffer_reserve) |
| 278 |
ISC_TEST_ENTRY(isc_buffer_dynamic) |
| 279 |
ISC_TEST_ENTRY(isc_buffer_copyregion) |
| 280 |
ISC_TEST_ENTRY(isc_buffer_printf) |
| 281 |
|
| 282 |
ISC_TEST_LIST_END |
| 283 |
|
| 284 |
ISC_TEST_MAIN |