upstream commit

unit tests for KRL bitmap
This commit is contained in:
djm@openbsd.org 2015-01-15 07:36:28 +00:00 committed by Damien Miller
parent 7613f828f4
commit d333f89abf
6 changed files with 182 additions and 8 deletions

View File

@ -232,6 +232,8 @@ clean: regressclean
rm -f regress/unittests/sshbuf/test_sshbuf
rm -f regress/unittests/sshkey/*.o
rm -f regress/unittests/sshkey/test_sshkey
rm -f regress/unittests/bitmap/*.o
rm -f regress/unittests/bitmap/test_bitmap
(cd openbsd-compat && $(MAKE) clean)
distclean: regressclean
@ -246,6 +248,8 @@ distclean: regressclean
rm -f regress/unittests/sshbuf/test_sshbuf
rm -f regress/unittests/sshkey/*.o
rm -f regress/unittests/sshkey/test_sshkey
rm -f regress/unittests/bitmap/*.o
rm -f regress/unittests/bitmap/test_bitmap
(cd openbsd-compat && $(MAKE) distclean)
if test -d pkg ; then \
rm -fr pkg ; \
@ -479,12 +483,22 @@ regress/unittests/sshkey/test_sshkey$(EXEEXT): ${UNITTESTS_TEST_SSHKEY_OBJS} \
regress/unittests/test_helper/libtest_helper.a \
-lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
UNITTESTS_TEST_BITMAP_OBJS=\
regress/unittests/sshkey/tests.o
regress/unittests/sshkey/test_bitmap$(EXEEXT): ${UNITTESTS_TEST_BITMAP_OBJS} \
regress/unittests/test_helper/libtest_helper.a libssh.a
$(LD) -o $@ $(LDFLAGS) $(UNITTESTS_TEST_BITMAP_OBJS) \
regress/unittests/test_helper/libtest_helper.a \
-lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
REGRESS_BINARIES=\
regress/modpipe$(EXEEXT) \
regress/setuid-allowed$(EXEEXT) \
regress/netcat$(EXEEXT) \
regress/unittests/sshbuf/test_sshbuf$(EXEEXT) \
regress/unittests/sshkey/test_sshkey$(EXEEXT)
regress/unittests/sshkey/test_sshkey$(EXEEXT) \
regress/unittests/sshkey/test_bitmap$(EXEEXT)
tests interop-tests t-exec: regress-prep $(TARGETS) $(REGRESS_BINARIES)
BUILDDIR=`pwd`; \

View File

@ -1,5 +1,5 @@
# $OpenBSD: Makefile,v 1.1 2014/04/30 05:32:00 djm Exp $
SUBDIR= test_helper sshbuf sshkey
SUBDIR= test_helper sshbuf sshkey bitmap
.include <bsd.subdir.mk>

View File

@ -0,0 +1,12 @@
# $OpenBSD: Makefile,v 1.1 2015/01/15 07:36:28 djm Exp $
TEST_ENV= "MALLOC_OPTIONS=AFGJPRX"
PROG=test_bitmap
SRCS=tests.c
REGRESS_TARGETS=run-regress-${PROG}
run-regress-${PROG}: ${PROG}
env ${TEST_ENV} ./${PROG}
.include <bsd.regress.mk>

View File

@ -0,0 +1,131 @@
/* $OpenBSD: tests.c,v 1.1 2015/01/15 07:36:28 djm Exp $ */
/*
* Regress test for bitmap.h bitmap API
*
* Placed in the public domain
*/
#include <sys/types.h>
#include <sys/param.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/bn.h>
#include "test_helper.h"
#include "bitmap.h"
#define NTESTS 131
void
tests(void)
{
struct bitmap *b;
BIGNUM *bn;
size_t len;
int i, j, k, n;
u_char bbuf[1024], bnbuf[1024];
int r;
TEST_START("bitmap_new");
b = bitmap_new();
ASSERT_PTR_NE(b, NULL);
bn = BN_new();
ASSERT_PTR_NE(bn, NULL);
TEST_DONE();
TEST_START("bitmap_set_bit / bitmap_test_bit");
for (i = -1; i < NTESTS; i++) {
for (j = -1; j < NTESTS; j++) {
for (k = -1; k < NTESTS; k++) {
bitmap_zero(b);
BN_clear(bn);
test_subtest_info("set %d/%d/%d", i, j, k);
/* Set bits */
if (i >= 0) {
ASSERT_INT_EQ(bitmap_set_bit(b, i), 0);
ASSERT_INT_EQ(BN_set_bit(bn, i), 1);
}
if (j >= 0) {
ASSERT_INT_EQ(bitmap_set_bit(b, j), 0);
ASSERT_INT_EQ(BN_set_bit(bn, j), 1);
}
if (k >= 0) {
ASSERT_INT_EQ(bitmap_set_bit(b, k), 0);
ASSERT_INT_EQ(BN_set_bit(bn, k), 1);
}
/* Check perfect match between bitmap and bn */
test_subtest_info("match %d/%d/%d", i, j, k);
for (n = 0; n < NTESTS; n++) {
ASSERT_INT_EQ(BN_is_bit_set(bn, n),
bitmap_test_bit(b, n));
}
/* Test length calculations */
test_subtest_info("length %d/%d/%d", i, j, k);
ASSERT_INT_EQ(BN_num_bits(bn),
(int)bitmap_nbits(b));
ASSERT_INT_EQ(BN_num_bytes(bn),
(int)bitmap_nbytes(b));
/* Test serialisation */
test_subtest_info("serialise %d/%d/%d",
i, j, k);
len = bitmap_nbytes(b);
memset(bbuf, 0xfc, sizeof(bbuf));
ASSERT_INT_EQ(bitmap_to_string(b, bbuf,
sizeof(bbuf)), 0);
for (n = len; n < (int)sizeof(bbuf); n++)
ASSERT_U8_EQ(bbuf[n], 0xfc);
r = BN_bn2bin(bn, bnbuf);
ASSERT_INT_GE(r, 0);
ASSERT_INT_EQ(r, (int)len);
ASSERT_MEM_EQ(bbuf, bnbuf, len);
/* Test deserialisation */
test_subtest_info("deserialise %d/%d/%d",
i, j, k);
bitmap_zero(b);
ASSERT_INT_EQ(bitmap_from_string(b, bnbuf,
len), 0);
for (n = 0; n < NTESTS; n++) {
ASSERT_INT_EQ(BN_is_bit_set(bn, n),
bitmap_test_bit(b, n));
}
/* Test clearing bits */
test_subtest_info("clear %d/%d/%d",
i, j, k);
for (n = 0; n < NTESTS; n++) {
ASSERT_INT_EQ(bitmap_set_bit(b, n), 0);
ASSERT_INT_EQ(BN_set_bit(bn, n), 1);
}
if (i >= 0) {
bitmap_clear_bit(b, i);
BN_clear_bit(bn, i);
}
if (j >= 0) {
bitmap_clear_bit(b, j);
BN_clear_bit(bn, j);
}
if (k >= 0) {
bitmap_clear_bit(b, k);
BN_clear_bit(bn, k);
}
for (n = 0; n < NTESTS; n++) {
ASSERT_INT_EQ(BN_is_bit_set(bn, n),
bitmap_test_bit(b, n));
}
}
}
}
bitmap_free(b);
BN_free(bn);
TEST_DONE();
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: test_helper.c,v 1.3 2015/01/13 14:51:51 djm Exp $ */
/* $OpenBSD: test_helper.c,v 1.4 2015/01/15 07:36:28 djm Exp $ */
/*
* Copyright (c) 2011 Damien Miller <djm@mindrot.org>
*
@ -114,6 +114,7 @@ static u_int test_number = 0;
static test_onerror_func_t *test_onerror = NULL;
static void *onerror_ctx = NULL;
static const char *data_dir = NULL;
static char subtest_info[512];
int
main(int argc, char **argv)
@ -185,8 +186,9 @@ test_data_file(const char *name)
void
test_info(char *s, size_t len)
{
snprintf(s, len, "In test %u - \"%s\"\n", test_number,
active_test_name == NULL ? "<none>" : active_test_name);
snprintf(s, len, "In test %u: \"%s\"%s%s\n", test_number,
active_test_name == NULL ? "<none>" : active_test_name,
*subtest_info != '\0' ? " - " : "", subtest_info);
}
#ifdef SIGINFO
@ -205,6 +207,7 @@ test_start(const char *n)
{
assert(active_test_name == NULL);
assert((active_test_name = strdup(n)) != NULL);
*subtest_info = '\0';
if (verbose_mode)
printf("test %u - \"%s\": ", test_number, active_test_name);
test_number++;
@ -223,6 +226,7 @@ set_onerror_func(test_onerror_func_t *f, void *ctx)
void
test_done(void)
{
*subtest_info = '\0';
assert(active_test_name != NULL);
free(active_test_name);
active_test_name = NULL;
@ -234,6 +238,16 @@ test_done(void)
}
}
void
test_subtest_info(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(subtest_info, sizeof(subtest_info), fmt, ap);
va_end(ap);
}
void
ssl_err_check(const char *file, int line)
{
@ -280,8 +294,9 @@ static void
test_header(const char *file, int line, const char *a1, const char *a2,
const char *name, enum test_predicate pred)
{
fprintf(stderr, "\n%s:%d test #%u \"%s\"\n",
file, line, test_number, active_test_name);
fprintf(stderr, "\n%s:%d test #%u \"%s\"%s%s\n",
file, line, test_number, active_test_name,
*subtest_info != '\0' ? " - " : "", subtest_info);
fprintf(stderr, "ASSERT_%s_%s(%s%s%s) failed:\n",
name, pred_name(pred), a1,
a2 != NULL ? ", " : "", a2 != NULL ? a2 : "");

View File

@ -1,4 +1,4 @@
/* $OpenBSD: test_helper.h,v 1.4 2015/01/13 14:51:51 djm Exp $ */
/* $OpenBSD: test_helper.h,v 1.5 2015/01/15 07:36:28 djm Exp $ */
/*
* Copyright (c) 2011 Damien Miller <djm@mindrot.org>
*
@ -43,6 +43,8 @@ void test_start(const char *n);
void test_info(char *s, size_t len);
void set_onerror_func(test_onerror_func_t *f, void *ctx);
void test_done(void);
void test_subtest_info(const char *fmt, ...)
__attribute__((format(printf, 1, 2)));
void ssl_err_check(const char *file, int line);
void assert_bignum(const char *file, int line,
const char *a1, const char *a2,