2014-11-14 15:58:54 -08:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2014-11-14 15:58:54 -08:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* 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 http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2014-11-14 15:58:54 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2018-10-30 13:33:25 +01:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
2014-11-14 15:58:54 -08:00
|
|
|
#include <stdlib.h>
|
2018-10-30 13:33:25 +01:00
|
|
|
#include <string.h>
|
2014-11-14 15:58:54 -08:00
|
|
|
|
|
|
|
#include <atf-c.h>
|
|
|
|
|
|
|
|
#include "isctest.h"
|
|
|
|
|
|
|
|
#include <isc/buffer.h>
|
2018-10-30 08:06:34 -07:00
|
|
|
#include <isc/print.h>
|
2018-10-30 13:33:25 +01:00
|
|
|
#include <isc/region.h>
|
2014-11-14 15:58:54 -08:00
|
|
|
#include <isc/result.h>
|
2018-10-30 13:33:25 +01:00
|
|
|
#include <isc/types.h>
|
2014-11-14 15:58:54 -08:00
|
|
|
|
|
|
|
ATF_TC(isc_buffer_reserve);
|
|
|
|
ATF_TC_HEAD(isc_buffer_reserve, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr", "reserve space in dynamic buffers");
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC_BODY(isc_buffer_reserve, tc) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_buffer_t *b;
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
result = isc_test_begin(NULL, true, 0);
|
2014-11-14 15:58:54 -08:00
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
b = NULL;
|
|
|
|
result = isc_buffer_allocate(mctx, &b, 1024);
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
ATF_CHECK_EQ(b->length, 1024);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 1024 bytes should already be available, so this call does
|
|
|
|
* nothing.
|
|
|
|
*/
|
|
|
|
result = isc_buffer_reserve(&b, 1024);
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
ATF_CHECK(ISC_BUFFER_VALID(b));
|
2015-09-30 14:14:47 +10:00
|
|
|
ATF_REQUIRE(b != NULL);
|
2014-11-14 15:58:54 -08:00
|
|
|
ATF_CHECK_EQ(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);
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
ATF_CHECK(ISC_BUFFER_VALID(b));
|
2015-09-30 14:14:47 +10:00
|
|
|
ATF_REQUIRE(b != NULL);
|
2014-11-14 15:58:54 -08:00
|
|
|
ATF_CHECK_EQ(b->length, 2048);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 2048 bytes should already be available, so this call does
|
|
|
|
* nothing.
|
|
|
|
*/
|
2014-11-15 00:56:17 -08:00
|
|
|
result = isc_buffer_reserve(&b, 2000);
|
2014-11-14 15:58:54 -08:00
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
ATF_CHECK(ISC_BUFFER_VALID(b));
|
2015-09-30 14:14:47 +10:00
|
|
|
ATF_REQUIRE(b != NULL);
|
2014-11-14 15:58:54 -08:00
|
|
|
ATF_CHECK_EQ(b->length, 2048);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This call should grow it to 4096 bytes as only 2048 bytes are
|
|
|
|
* available in the buffer.
|
|
|
|
*/
|
2014-11-15 00:56:17 -08:00
|
|
|
result = isc_buffer_reserve(&b, 3000);
|
2014-11-14 15:58:54 -08:00
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
ATF_CHECK(ISC_BUFFER_VALID(b));
|
2015-09-30 14:14:47 +10:00
|
|
|
ATF_REQUIRE(b != NULL);
|
2014-11-14 15:58:54 -08:00
|
|
|
ATF_CHECK_EQ(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);
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_NOMEMORY);
|
|
|
|
ATF_CHECK(ISC_BUFFER_VALID(b));
|
2015-09-30 14:14:47 +10:00
|
|
|
ATF_REQUIRE(b != NULL);
|
2014-11-14 15:58:54 -08:00
|
|
|
ATF_CHECK_EQ(b->length, 4096);
|
|
|
|
|
|
|
|
isc_buffer_free(&b);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
}
|
|
|
|
|
2016-05-26 21:23:19 +02:00
|
|
|
ATF_TC(isc_buffer_dynamic);
|
|
|
|
ATF_TC_HEAD(isc_buffer_dynamic, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr", "dynamic buffer automatic reallocation");
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC_BODY(isc_buffer_dynamic, tc) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_buffer_t *b;
|
|
|
|
size_t last_length = 10;
|
|
|
|
int i;
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
result = isc_test_begin(NULL, true, 0);
|
2016-05-26 21:23:19 +02:00
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
b = NULL;
|
|
|
|
result = isc_buffer_allocate(mctx, &b, last_length);
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
ATF_REQUIRE(b != NULL);
|
|
|
|
ATF_CHECK_EQ(b->length, last_length);
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
isc_buffer_setautorealloc(b, true);
|
2016-05-26 21:23:19 +02:00
|
|
|
|
|
|
|
isc_buffer_putuint8(b, 1);
|
|
|
|
|
|
|
|
for (i = 0; i < 1000; i++) {
|
|
|
|
isc_buffer_putstr(b, "thisisa24charslongstring");
|
|
|
|
}
|
|
|
|
ATF_CHECK(b->length-last_length >= 1000*24);
|
|
|
|
last_length+=1000*24;
|
|
|
|
|
|
|
|
for (i = 0; i < 10000; i++) {
|
|
|
|
isc_buffer_putuint8(b, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_CHECK(b->length-last_length >= 10000*1);
|
|
|
|
last_length += 10000*1;
|
|
|
|
|
|
|
|
for (i = 0; i < 10000; i++) {
|
|
|
|
isc_buffer_putuint16(b, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_CHECK(b->length-last_length >= 10000*2);
|
|
|
|
|
|
|
|
last_length += 10000*2;
|
|
|
|
for (i = 0; i < 10000; i++) {
|
|
|
|
isc_buffer_putuint24(b, 1);
|
|
|
|
}
|
|
|
|
ATF_CHECK(b->length-last_length >= 10000*3);
|
|
|
|
|
|
|
|
last_length+=10000*3;
|
|
|
|
|
|
|
|
for (i = 0; i < 10000; i++) {
|
|
|
|
isc_buffer_putuint32(b, 1);
|
|
|
|
}
|
|
|
|
ATF_CHECK(b->length-last_length >= 10000*4);
|
|
|
|
|
|
|
|
|
|
|
|
isc_buffer_free(&b);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
}
|
|
|
|
|
2018-10-30 13:33:25 +01:00
|
|
|
ATF_TC(isc_buffer_copyregion);
|
|
|
|
ATF_TC_HEAD(isc_buffer_copyregion, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr", "copy a region into a buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC_BODY(isc_buffer_copyregion, tc) {
|
|
|
|
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),
|
|
|
|
};
|
|
|
|
|
|
|
|
result = isc_test_begin(NULL, true, 0);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
result = isc_buffer_allocate(mctx, &b, sizeof(data));
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill originally allocated buffer space.
|
|
|
|
*/
|
|
|
|
result = isc_buffer_copyregion(b, &r);
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Appending more data to the buffer should fail.
|
|
|
|
*/
|
|
|
|
result = isc_buffer_copyregion(b, &r);
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_NOSPACE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable auto reallocation and retry. Appending should now succeed.
|
|
|
|
*/
|
|
|
|
isc_buffer_setautorealloc(b, true);
|
|
|
|
result = isc_buffer_copyregion(b, &r);
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
isc_buffer_free(&b);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
}
|
|
|
|
|
2017-10-09 11:43:07 +02:00
|
|
|
ATF_TC(isc_buffer_printf);
|
|
|
|
ATF_TC_HEAD(isc_buffer_printf, tc) {
|
|
|
|
atf_tc_set_md_var(tc, "descr", "printf() into a buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
ATF_TC_BODY(isc_buffer_printf, tc) {
|
|
|
|
unsigned int used, prev_used;
|
2017-10-11 09:28:44 +02:00
|
|
|
const char *empty_fmt;
|
2017-10-09 11:43:07 +02:00
|
|
|
isc_result_t result;
|
|
|
|
isc_buffer_t *b, sb;
|
|
|
|
char buf[8];
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
result = isc_test_begin(NULL, true, 0);
|
2017-10-09 11:43:07 +02:00
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare a buffer with auto-reallocation enabled.
|
|
|
|
*/
|
|
|
|
b = NULL;
|
|
|
|
result = isc_buffer_allocate(mctx, &b, 0);
|
|
|
|
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
2018-04-17 08:29:14 -07:00
|
|
|
isc_buffer_setautorealloc(b, true);
|
2017-10-09 11:43:07 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sanity check.
|
|
|
|
*/
|
|
|
|
result = isc_buffer_printf(b, "foo");
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
used = isc_buffer_usedlength(b);
|
|
|
|
ATF_CHECK_EQ(used, 3);
|
|
|
|
|
|
|
|
result = isc_buffer_printf(b, "bar");
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
used = isc_buffer_usedlength(b);
|
|
|
|
ATF_CHECK_EQ(used, 3 + 3);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Also check the terminating NULL byte is there, even though it is not
|
|
|
|
* part of the buffer's used region.
|
|
|
|
*/
|
|
|
|
ATF_CHECK_EQ(memcmp(isc_buffer_current(b), "foobar", 7), 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
ATF_CHECK_EQ(used - prev_used, 2);
|
|
|
|
|
|
|
|
isc_buffer_printf(b, "baz%1X", 42);
|
|
|
|
used = isc_buffer_usedlength(b);
|
|
|
|
ATF_CHECK_EQ(used - prev_used, 2 + 5);
|
|
|
|
|
|
|
|
isc_buffer_printf(b, "%6.1f", 42.42f);
|
|
|
|
used = isc_buffer_usedlength(b);
|
|
|
|
ATF_CHECK_EQ(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.
|
|
|
|
*/
|
|
|
|
ATF_CHECK_EQ(memcmp(isc_buffer_current(b), "42baz2A 42.4", 14), 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check an empty format string is properly handled.
|
2018-05-01 18:12:41 +10:00
|
|
|
*
|
|
|
|
* 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.
|
2017-10-09 11:43:07 +02:00
|
|
|
*/
|
|
|
|
prev_used = used;
|
|
|
|
empty_fmt = "";
|
2018-05-01 18:12:41 +10:00
|
|
|
result = isc_buffer_printf(b, empty_fmt, "");
|
2017-10-09 11:43:07 +02:00
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
used = isc_buffer_usedlength(b);
|
|
|
|
ATF_CHECK_EQ(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");
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
used = isc_buffer_usedlength(&sb);
|
|
|
|
ATF_CHECK_EQ(used, 6);
|
|
|
|
|
|
|
|
result = isc_buffer_printf(&sb, "789");
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_NOSPACE);
|
|
|
|
used = isc_buffer_usedlength(&sb);
|
|
|
|
ATF_CHECK_EQ(used, 6);
|
|
|
|
|
|
|
|
result = isc_buffer_printf(&sb, "78");
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_NOSPACE);
|
|
|
|
used = isc_buffer_usedlength(&sb);
|
|
|
|
ATF_CHECK_EQ(used, 6);
|
|
|
|
|
|
|
|
result = isc_buffer_printf(&sb, "7");
|
|
|
|
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
|
|
|
used = isc_buffer_usedlength(&sb);
|
|
|
|
ATF_CHECK_EQ(used, 7);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
}
|
|
|
|
|
2014-11-14 15:58:54 -08:00
|
|
|
/*
|
|
|
|
* Main
|
|
|
|
*/
|
|
|
|
ATF_TP_ADD_TCS(tp) {
|
|
|
|
ATF_TP_ADD_TC(tp, isc_buffer_reserve);
|
2016-05-26 21:23:19 +02:00
|
|
|
ATF_TP_ADD_TC(tp, isc_buffer_dynamic);
|
2018-10-30 13:33:25 +01:00
|
|
|
ATF_TP_ADD_TC(tp, isc_buffer_copyregion);
|
2017-10-09 11:43:07 +02:00
|
|
|
ATF_TP_ADD_TC(tp, isc_buffer_printf);
|
2014-11-14 15:58:54 -08:00
|
|
|
return (atf_no_error());
|
|
|
|
}
|