Commit 683094e3 authored by Joey's avatar Joey Committed by Evan Hunt
Browse files

convert mem_test

parent 20cef35e
......@@ -12,7 +12,7 @@ tap_test_program{name='hmac_test'}
tap_test_program{name='ht_test'}
tap_test_program{name='lex_test'}
tap_test_program{name='md_test'}
atf_test_program{name='mem_test'}
tap_test_program{name='mem_test'}
tap_test_program{name='netaddr_test'}
tap_test_program{name='parse_test'}
tap_test_program{name='pool_test'}
......
......@@ -111,8 +111,9 @@ md_test@EXEEXT@: md_test.@O@ ${ISCDEPLIBS}
md_test.@O@ ${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
mem_test@EXEEXT@: mem_test.@O@ isctest.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
mem_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
${LDFLAGS} -o $@ mem_test.@O@ isctest.@O@ \
${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
netaddr_test@EXEEXT@: netaddr_test.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
......
......@@ -10,27 +10,49 @@
*/
#include <config.h>
#if HAVE_CMOCKA
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <atf-c.h>
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/file.h>
#include <isc/mem.h>
#include <isc/print.h>
#include <isc/result.h>
#include <isc/stdio.h>
#include <isc/util.h>
#include "../mem_p.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 void *
default_memalloc(void *arg, size_t size) {
UNUSED(arg);
if (size == 0U)
if (size == 0U) {
size = 1;
}
return (malloc(size));
}
......@@ -40,9 +62,13 @@ default_memfree(void *arg, void *ptr) {
free(ptr);
}
ATF_TC(isc_mem);
ATF_TC_HEAD(isc_mem, tc) {
atf_tc_set_md_var(tc, "descr", "general memory system tests");
static int
_teardown(void **state) {
UNUSED(state);
isc_test_end();
return (0);
}
#define MP1_FREEMAX 10
......@@ -52,7 +78,9 @@ ATF_TC_HEAD(isc_mem, tc) {
#define MP2_FREEMAX 25
#define MP2_FILLCNT 25
ATF_TC_BODY(isc_mem, tc) {
/* general memory system tests */
static void
isc_mem_test(void **state) {
isc_result_t result;
void *items1[50];
void *items2[50];
......@@ -62,17 +90,16 @@ ATF_TC_BODY(isc_mem, tc) {
unsigned int i, j;
int rval;
result = isc_test_begin(NULL, true, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
UNUSED(state);
result = isc_mem_create(0, 0, &localmctx);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_mempool_create(localmctx, 24, &mp1);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_mempool_create(localmctx, 31, &mp2);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
isc_mempool_setfreemax(mp1, MP1_FREEMAX);
isc_mempool_setfillcount(mp1, MP1_FILLCNT);
......@@ -83,14 +110,14 @@ ATF_TC_BODY(isc_mem, tc) {
*/
for (i = 0; i < MP1_MAXALLOC; i++) {
items1[i] = isc_mempool_get(mp1);
ATF_CHECK(items1[i] != NULL);
assert_non_null(items1[i]);
}
/*
* Try to allocate one more. This should fail.
*/
tmp = isc_mempool_get(mp1);
ATF_CHECK_EQ(tmp, NULL);
assert_null(tmp);
/*
* Free the first 11 items. Verify that there are 10 free items on
......@@ -102,10 +129,10 @@ ATF_TC_BODY(isc_mem, tc) {
}
rval = isc_mempool_getfreecount(mp1);
ATF_CHECK_EQ(rval, 10);
assert_int_equal(rval, 10);
rval = isc_mempool_getallocated(mp1);
ATF_CHECK_EQ(rval, 19);
assert_int_equal(rval, 19);
/*
* Now, beat up on mp2 for a while. Allocate 50 items, then free
......@@ -118,7 +145,7 @@ ATF_TC_BODY(isc_mem, tc) {
for (j = 0; j < 500000; j++) {
for (i = 0; i < 50; i++) {
items2[i] = isc_mempool_get(mp2);
ATF_CHECK(items2[i] != NULL);
assert_non_null(items2[i]);
}
for (i = 0; i < 50; i++) {
isc_mempool_put(mp2, items2[i]);
......@@ -142,42 +169,40 @@ ATF_TC_BODY(isc_mem, tc) {
result = isc_mem_createx(0, 0, default_memalloc, default_memfree,
NULL, &localmctx,
ISC_MEMFLAG_FILL | ISC_MEMFLAG_INTERNAL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_mempool_create(localmctx, 2, &mp1);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
tmp = isc_mempool_get(mp1);
ATF_CHECK(tmp != NULL);
assert_non_null(tmp);
isc_mempool_put(mp1, tmp);
isc_mempool_destroy(&mp1);
isc_test_end();
}
isc_mem_destroy(&localmctx);
ATF_TC(isc_mem_total);
ATF_TC_HEAD(isc_mem_total, tc) {
atf_tc_set_md_var(tc, "descr", "test TotalUse calculation");
}
ATF_TC_BODY(isc_mem_total, tc) {
/* test TotalUse calculation */
static void
isc_mem_total_test(void **state) {
isc_result_t result;
isc_mem_t *mctx2 = NULL;
size_t before, after;
ssize_t diff;
int i;
result = isc_test_begin(NULL, true, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
UNUSED(state);
/* Local alloc, free */
mctx2 = NULL;
result = isc_mem_createx(0, 0, default_memalloc, default_memfree,
NULL, &mctx2, 0);
if (result != ISC_R_SUCCESS)
if (result != ISC_R_SUCCESS) {
goto out;
}
before = isc_mem_total(mctx2);
......@@ -191,11 +216,8 @@ ATF_TC_BODY(isc_mem_total, tc) {
after = isc_mem_total(mctx2);
diff = after - before;
printf("total_before=%lu, total_after=%lu, total_diff=%lu\n",
(unsigned long)before, (unsigned long)after,
(unsigned long)diff);
/* 2048 +8 bytes extra for size_info */
ATF_CHECK_EQ(diff, (2048 + 8) * 100000);
assert_int_equal(diff, (2048 + 8) * 100000);
/* ISC_MEMFLAG_INTERNAL */
......@@ -211,67 +233,55 @@ ATF_TC_BODY(isc_mem_total, tc) {
after = isc_mem_total(mctx);
diff = after - before;
printf("total_before=%lu, total_after=%lu, total_diff=%lu\n",
(unsigned long)before, (unsigned long)after,
(unsigned long)diff);
/* 2048 +8 bytes extra for size_info */
ATF_CHECK_EQ(diff, (2048 + 8) * 100000);
assert_int_equal(diff, (2048 + 8) * 100000);
out:
if (mctx2 != NULL)
if (mctx2 != NULL) {
isc_mem_destroy(&mctx2);
}
isc_test_end();
}
ATF_TC(isc_mem_inuse);
ATF_TC_HEAD(isc_mem_inuse, tc) {
atf_tc_set_md_var(tc, "descr", "test InUse calculation");
}
ATF_TC_BODY(isc_mem_inuse, tc) {
/* test InUse calculation */
static void
isc_mem_inuse_test(void **state) {
isc_result_t result;
isc_mem_t *mctx2 = NULL;
size_t before, during, after;
size_t before, after;
ssize_t diff;
void *ptr;
result = isc_test_begin(NULL, true, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
UNUSED(state);
mctx2 = NULL;
result = isc_mem_createx(0, 0, default_memalloc, default_memfree,
NULL, &mctx2, 0);
if (result != ISC_R_SUCCESS)
if (result != ISC_R_SUCCESS) {
goto out;
}
before = isc_mem_inuse(mctx2);
ptr = isc_mem_allocate(mctx2, 1024000);
during = isc_mem_inuse(mctx2);
isc_mem_free(mctx2, ptr);
after = isc_mem_inuse(mctx2);
diff = after - before;
printf("inuse_before=%lu, inuse_during=%lu, inuse_after=%lu\n",
(unsigned long)before, (unsigned long)during,
(unsigned long)after);
ATF_REQUIRE_EQ(diff, 0);
assert_int_equal(diff, 0);
out:
if (mctx2 != NULL)
if (mctx2 != NULL) {
isc_mem_destroy(&mctx2);
}
isc_test_end();
}
#if ISC_MEM_TRACKLINES
ATF_TC(isc_mem_noflags);
ATF_TC_HEAD(isc_mem_noflags, tc) {
atf_tc_set_md_var(tc, "descr", "test mem with no flags");
}
ATF_TC_BODY(isc_mem_noflags, tc) {
/* test mem with no flags */
static void
isc_mem_noflags_test(void **state) {
isc_result_t result;
isc_mem_t *mctx2 = NULL;
char buf[4096], *p, *q;
......@@ -279,17 +289,16 @@ ATF_TC_BODY(isc_mem_noflags, tc) {
void *ptr;
result = isc_stdio_open("mem.output", "w", &f);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_test_begin(NULL, true, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
UNUSED(state);
result = isc_mem_createx(0, 0, default_memalloc, default_memfree,
NULL, &mctx2, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
isc_mem_debugging = 0;
ptr = isc_mem_get(mctx2, 2048);
ATF_CHECK(ptr != NULL);
assert_non_null(ptr);
isc__mem_printactive(mctx2, f);
isc_mem_put(mctx2, ptr, 2048);
isc_mem_destroy(&mctx2);
......@@ -297,33 +306,29 @@ ATF_TC_BODY(isc_mem_noflags, tc) {
memset(buf, 0, sizeof(buf));
result = isc_stdio_open("mem.output", "r", &f);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
ATF_REQUIRE_EQ(result, ISC_R_EOF);
assert_int_equal(result, ISC_R_EOF);
isc_stdio_close(f);
isc_file_remove("mem.output");
buf[sizeof(buf) - 1] = 0;
p = strchr(buf, '\n');
ATF_REQUIRE(p != NULL);
ATF_REQUIRE(p < buf + sizeof(buf) - 2);
assert_non_null(p);
assert_in_range(p, 0, buf + sizeof(buf) - 3);
p += 2;
q = strchr(p, '\n');
ATF_REQUIRE(q != NULL);
assert_non_null(q);
*q = '\0';
ATF_CHECK_STREQ(p, "None.");
assert_string_equal(p, "None.");
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
isc_test_end();
}
ATF_TC(isc_mem_recordflag);
ATF_TC_HEAD(isc_mem_recordflag, tc) {
atf_tc_set_md_var(tc, "descr", "test mem with record flag");
}
ATF_TC_BODY(isc_mem_recordflag, tc) {
/* test mem with record flag */
static void
isc_mem_recordflag_test(void **state) {
isc_result_t result;
isc_mem_t *mctx2 = NULL;
char buf[4096], *p;
......@@ -331,16 +336,15 @@ ATF_TC_BODY(isc_mem_recordflag, tc) {
void *ptr;
result = isc_stdio_open("mem.output", "w", &f);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_test_begin(NULL, false, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
UNUSED(state);
result = isc_mem_createx(0, 0, default_memalloc, default_memfree,
NULL, &mctx2, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
ptr = isc_mem_get(mctx2, 2048);
ATF_CHECK(ptr != NULL);
assert_non_null(ptr);
isc__mem_printactive(mctx2, f);
isc_mem_put(mctx2, ptr, 2048);
isc_mem_destroy(&mctx2);
......@@ -348,31 +352,27 @@ ATF_TC_BODY(isc_mem_recordflag, tc) {
memset(buf, 0, sizeof(buf));
result = isc_stdio_open("mem.output", "r", &f);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
ATF_REQUIRE_EQ(result, ISC_R_EOF);
assert_int_equal(result, ISC_R_EOF);
isc_stdio_close(f);
isc_file_remove("mem.output");
buf[sizeof(buf) - 1] = 0;
p = strchr(buf, '\n');
ATF_REQUIRE(p != NULL);
ATF_REQUIRE(p < buf + sizeof(buf) - 2);
ATF_CHECK(strncmp(p + 2, "ptr ", 4) == 0);
assert_non_null(p);
assert_in_range(p, 0, buf + sizeof(buf) - 3);
assert_memory_equal(p + 2, "ptr ", 4);
p = strchr(p + 1, '\n');
ATF_REQUIRE(p != NULL);
ATF_CHECK(strlen(p) == 1);
assert_non_null(p);
assert_int_equal(strlen(p), 1);
isc_test_end();
}
ATF_TC(isc_mem_traceflag);
ATF_TC_HEAD(isc_mem_traceflag, tc) {
atf_tc_set_md_var(tc, "descr", "test mem with trace flag");
}
ATF_TC_BODY(isc_mem_traceflag, tc) {
/* test mem with trace flag */
static void
isc_mem_traceflag_test(void **state) {
isc_result_t result;
isc_mem_t *mctx2 = NULL;
char buf[4096], *p;
......@@ -381,17 +381,16 @@ ATF_TC_BODY(isc_mem_traceflag, tc) {
/* redirect stderr so we can check trace output */
f = freopen("mem.output", "w", stderr);
ATF_REQUIRE(f != NULL);
assert_non_null(f);
result = isc_test_begin(NULL, true, 0);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
UNUSED(state);
result = isc_mem_createx(0, 0, default_memalloc, default_memfree,
NULL, &mctx2, 0);
isc_mem_debugging = ISC_MEM_DEBUGTRACE;
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
ptr = isc_mem_get(mctx2, 2048);
ATF_CHECK(ptr != NULL);
assert_non_null(ptr);
isc__mem_printactive(mctx2, f);
isc_mem_put(mctx2, ptr, 2048);
isc_mem_destroy(&mctx2);
......@@ -399,9 +398,9 @@ ATF_TC_BODY(isc_mem_traceflag, tc) {
memset(buf, 0, sizeof(buf));
result = isc_stdio_open("mem.output", "r", &f);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_stdio_read(buf, sizeof(buf), 1, f, NULL);
ATF_REQUIRE_EQ(result, ISC_R_EOF);
assert_int_equal(result, ISC_R_EOF);
isc_stdio_close(f);
isc_file_remove("mem.output");
......@@ -410,34 +409,57 @@ ATF_TC_BODY(isc_mem_traceflag, tc) {
buf[sizeof(buf) - 1] = 0;
ATF_CHECK(strncmp(buf, "add ", 4) == 0);
assert_memory_equal(buf, "add ", 4);
p = strchr(buf, '\n');
ATF_REQUIRE(p != NULL);
assert_non_null(p);
p = strchr(p + 1, '\n');
ATF_REQUIRE(p != NULL);
ATF_REQUIRE(p < buf + sizeof(buf) - 2);
ATF_CHECK(strncmp(p + 2, "ptr ", 4) == 0);
assert_non_null(p);
assert_in_range(p, 0, buf + sizeof(buf) - 3);
assert_memory_equal(p + 2, "ptr ", 4);
p = strchr(p + 1, '\n');
ATF_REQUIRE(p != NULL);
ATF_CHECK(strncmp(p + 1, "del ", 4) == 0);
assert_non_null(p);
assert_memory_equal(p + 1, "del ", 4);
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
isc_test_end();
}
#endif
/*
* Main
*/
ATF_TP_ADD_TCS(tp) {
ATF_TP_ADD_TC(tp, isc_mem);
ATF_TP_ADD_TC(tp, isc_mem_total);
ATF_TP_ADD_TC(tp, isc_mem_inuse);
int
main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(isc_mem_test,
_setup, _teardown),
cmocka_unit_test_setup_teardown(isc_mem_total_test,
_setup, _teardown),
cmocka_unit_test_setup_teardown(isc_mem_inuse_test,
_setup, _teardown),
#if ISC_MEM_TRACKLINES
ATF_TP_ADD_TC(tp, isc_mem_noflags);
ATF_TP_ADD_TC(tp, isc_mem_recordflag);
ATF_TP_ADD_TC(tp, isc_mem_traceflag);
cmocka_unit_test_setup_teardown(isc_mem_noflags_test,
_setup, _teardown),
cmocka_unit_test_setup_teardown(isc_mem_recordflag_test,
_setup, _teardown),
cmocka_unit_test_setup_teardown(isc_mem_traceflag_test,
_setup, _teardown),
#endif
};
return (atf_no_error());
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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment