2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-30 05:57:52 +00:00

convert buffer_test

This commit is contained in:
Evan Hunt 2018-10-24 00:43:33 -07:00
parent dc88db015b
commit 02a5db7631
3 changed files with 132 additions and 107 deletions

View File

@ -2,7 +2,7 @@ syntax(2)
test_suite('bind9')
tap_test_program{name='aes_test'}
atf_test_program{name='buffer_test'}
tap_test_program{name='buffer_test'}
atf_test_program{name='counter_test'}
atf_test_program{name='errno_test'}
atf_test_program{name='file_test'}

View File

@ -59,8 +59,9 @@ aes_test@EXEEXT@: aes_test.@O@ ${ISCDEPLIBS}
${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
buffer_test@EXEEXT@: buffer_test.@O@ isctest.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
buffer_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
${LDFLAGS} -o $@ buffer_test.@O@ isctest.@O@ \
${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
counter_test@EXEEXT@: counter_test.@O@ isctest.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \

View File

@ -11,77 +11,104 @@
#include <config.h>
#if HAVE_CMOCKA
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <atf-c.h>
#include "isctest.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>
ATF_TC(isc_buffer_reserve);
ATF_TC_HEAD(isc_buffer_reserve, tc) {
atf_tc_set_md_var(tc, "descr", "reserve space in dynamic buffers");
#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);
}
ATF_TC_BODY(isc_buffer_reserve, tc) {
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;
result = isc_test_begin(NULL, true, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
UNUSED(state);
b = NULL;
result = isc_buffer_allocate(mctx, &b, 1024);
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
ATF_CHECK_EQ(b->length, 1024);
assert_int_equal(result, ISC_R_SUCCESS);
assert_int_equal(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));
ATF_REQUIRE(b != NULL);
ATF_CHECK_EQ(b->length, 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);
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
ATF_CHECK(ISC_BUFFER_VALID(b));
ATF_REQUIRE(b != NULL);
ATF_CHECK_EQ(b->length, 2048);
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);
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
ATF_CHECK(ISC_BUFFER_VALID(b));
ATF_REQUIRE(b != NULL);
ATF_CHECK_EQ(b->length, 2048);
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);
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
ATF_CHECK(ISC_BUFFER_VALID(b));
ATF_REQUIRE(b != NULL);
ATF_CHECK_EQ(b->length, 4096);
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);
@ -90,35 +117,29 @@ ATF_TC_BODY(isc_buffer_reserve, tc) {
* 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));
ATF_REQUIRE(b != NULL);
ATF_CHECK_EQ(b->length, 4096);
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);
isc_test_end();
}
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) {
/* dynamic buffer automatic reallocation */
static void
isc_buffer_dynamic_test(void **state) {
isc_result_t result;
isc_buffer_t *b;
size_t last_length = 10;
int i;
result = isc_test_begin(NULL, true, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
UNUSED(state);
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);
assert_int_equal(result, ISC_R_SUCCESS);
assert_non_null(b);
assert_int_equal(b->length, last_length);
isc_buffer_setautorealloc(b, true);
@ -127,47 +148,42 @@ ATF_TC_BODY(isc_buffer_dynamic, tc) {
for (i = 0; i < 1000; i++) {
isc_buffer_putstr(b, "thisisa24charslongstring");
}
ATF_CHECK(b->length-last_length >= 1000*24);
assert_true(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);
assert_true(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);
assert_true(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);
assert_true(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);
assert_true(b->length-last_length >= 10000*4);
isc_buffer_free(&b);
isc_test_end();
}
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) {
/* 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;
@ -177,77 +193,70 @@ ATF_TC_BODY(isc_buffer_copyregion, tc) {
.length = sizeof(data),
};
result = isc_test_begin(NULL, true, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
UNUSED(state);
result = isc_buffer_allocate(mctx, &b, sizeof(data));
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
/*
* Fill originally allocated buffer space.
*/
result = isc_buffer_copyregion(b, &r);
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
assert_int_equal(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);
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);
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
isc_buffer_free(&b);
isc_test_end();
}
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) {
/* 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];
result = isc_test_begin(NULL, true, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
UNUSED(state);
/*
* Prepare a buffer with auto-reallocation enabled.
*/
b = NULL;
result = isc_buffer_allocate(mctx, &b, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
isc_buffer_setautorealloc(b, true);
/*
* Sanity check.
*/
result = isc_buffer_printf(b, "foo");
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
used = isc_buffer_usedlength(b);
ATF_CHECK_EQ(used, 3);
assert_int_equal(used, 3);
result = isc_buffer_printf(b, "bar");
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
used = isc_buffer_usedlength(b);
ATF_CHECK_EQ(used, 3 + 3);
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.
*/
ATF_CHECK_EQ(memcmp(isc_buffer_current(b), "foobar", 7), 0);
assert_memory_equal(isc_buffer_current(b), "foobar", 7);
/*
* Skip over data from previous check to prevent failures in previous
@ -261,21 +270,21 @@ ATF_TC_BODY(isc_buffer_printf, tc) {
*/
isc_buffer_printf(b, "%d", 42);
used = isc_buffer_usedlength(b);
ATF_CHECK_EQ(used - prev_used, 2);
assert_int_equal(used - prev_used, 2);
isc_buffer_printf(b, "baz%1X", 42);
used = isc_buffer_usedlength(b);
ATF_CHECK_EQ(used - prev_used, 2 + 5);
assert_int_equal(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);
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.
*/
ATF_CHECK_EQ(memcmp(isc_buffer_current(b), "42baz2A 42.4", 14), 0);
assert_memory_equal(isc_buffer_current(b), "42baz2A 42.4", 14);
/*
* Check an empty format string is properly handled.
@ -288,9 +297,9 @@ ATF_TC_BODY(isc_buffer_printf, tc) {
prev_used = used;
empty_fmt = "";
result = isc_buffer_printf(b, empty_fmt, "");
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
used = isc_buffer_usedlength(b);
ATF_CHECK_EQ(prev_used, used);
assert_int_equal(prev_used, used);
isc_buffer_free(&b);
@ -299,35 +308,50 @@ ATF_TC_BODY(isc_buffer_printf, tc) {
*/
isc_buffer_init(&sb, buf, sizeof(buf));
result = isc_buffer_printf(&sb, "123456");
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
used = isc_buffer_usedlength(&sb);
ATF_CHECK_EQ(used, 6);
assert_int_equal(used, 6);
result = isc_buffer_printf(&sb, "789");
ATF_CHECK_EQ(result, ISC_R_NOSPACE);
assert_int_equal(result, ISC_R_NOSPACE);
used = isc_buffer_usedlength(&sb);
ATF_CHECK_EQ(used, 6);
assert_int_equal(used, 6);
result = isc_buffer_printf(&sb, "78");
ATF_CHECK_EQ(result, ISC_R_NOSPACE);
assert_int_equal(result, ISC_R_NOSPACE);
used = isc_buffer_usedlength(&sb);
ATF_CHECK_EQ(used, 6);
assert_int_equal(used, 6);
result = isc_buffer_printf(&sb, "7");
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
used = isc_buffer_usedlength(&sb);
ATF_CHECK_EQ(used, 7);
isc_test_end();
assert_int_equal(used, 7);
}
/*
* Main
*/
ATF_TP_ADD_TCS(tp) {
ATF_TP_ADD_TC(tp, isc_buffer_reserve);
ATF_TP_ADD_TC(tp, isc_buffer_dynamic);
ATF_TP_ADD_TC(tp, isc_buffer_copyregion);
ATF_TP_ADD_TC(tp, isc_buffer_printf);
return (atf_no_error());
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 (0);
}
#endif