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