summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2015-01-15 07:36:28 +0000
committerDamien Miller <djm@mindrot.org>2015-01-15 21:39:18 +1100
commitd333f89abf7179021e5c3f28673f469abe032062 (patch)
tree8399e567e65d2d0c241f344c09b4ed1eb2cfbae3
parent7613f828f49c55ff356007ae9645038ab6682556 (diff)
upstream commit
unit tests for KRL bitmap
-rw-r--r--Makefile.in16
-rw-r--r--regress/unittests/Makefile2
-rw-r--r--regress/unittests/bitmap/Makefile12
-rw-r--r--regress/unittests/bitmap/tests.c131
-rw-r--r--regress/unittests/test_helper/test_helper.c25
-rw-r--r--regress/unittests/test_helper/test_helper.h4
6 files changed, 182 insertions, 8 deletions
diff --git a/Makefile.in b/Makefile.in
index d7ba813a9..147152059 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -232,6 +232,8 @@ clean: regressclean
232 rm -f regress/unittests/sshbuf/test_sshbuf 232 rm -f regress/unittests/sshbuf/test_sshbuf
233 rm -f regress/unittests/sshkey/*.o 233 rm -f regress/unittests/sshkey/*.o
234 rm -f regress/unittests/sshkey/test_sshkey 234 rm -f regress/unittests/sshkey/test_sshkey
235 rm -f regress/unittests/bitmap/*.o
236 rm -f regress/unittests/bitmap/test_bitmap
235 (cd openbsd-compat && $(MAKE) clean) 237 (cd openbsd-compat && $(MAKE) clean)
236 238
237distclean: regressclean 239distclean: regressclean
@@ -246,6 +248,8 @@ distclean: regressclean
246 rm -f regress/unittests/sshbuf/test_sshbuf 248 rm -f regress/unittests/sshbuf/test_sshbuf
247 rm -f regress/unittests/sshkey/*.o 249 rm -f regress/unittests/sshkey/*.o
248 rm -f regress/unittests/sshkey/test_sshkey 250 rm -f regress/unittests/sshkey/test_sshkey
251 rm -f regress/unittests/bitmap/*.o
252 rm -f regress/unittests/bitmap/test_bitmap
249 (cd openbsd-compat && $(MAKE) distclean) 253 (cd openbsd-compat && $(MAKE) distclean)
250 if test -d pkg ; then \ 254 if test -d pkg ; then \
251 rm -fr pkg ; \ 255 rm -fr pkg ; \
@@ -479,12 +483,22 @@ regress/unittests/sshkey/test_sshkey$(EXEEXT): ${UNITTESTS_TEST_SSHKEY_OBJS} \
479 regress/unittests/test_helper/libtest_helper.a \ 483 regress/unittests/test_helper/libtest_helper.a \
480 -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS) 484 -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
481 485
486UNITTESTS_TEST_BITMAP_OBJS=\
487 regress/unittests/sshkey/tests.o
488
489regress/unittests/sshkey/test_bitmap$(EXEEXT): ${UNITTESTS_TEST_BITMAP_OBJS} \
490 regress/unittests/test_helper/libtest_helper.a libssh.a
491 $(LD) -o $@ $(LDFLAGS) $(UNITTESTS_TEST_BITMAP_OBJS) \
492 regress/unittests/test_helper/libtest_helper.a \
493 -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
494
482REGRESS_BINARIES=\ 495REGRESS_BINARIES=\
483 regress/modpipe$(EXEEXT) \ 496 regress/modpipe$(EXEEXT) \
484 regress/setuid-allowed$(EXEEXT) \ 497 regress/setuid-allowed$(EXEEXT) \
485 regress/netcat$(EXEEXT) \ 498 regress/netcat$(EXEEXT) \
486 regress/unittests/sshbuf/test_sshbuf$(EXEEXT) \ 499 regress/unittests/sshbuf/test_sshbuf$(EXEEXT) \
487 regress/unittests/sshkey/test_sshkey$(EXEEXT) 500 regress/unittests/sshkey/test_sshkey$(EXEEXT) \
501 regress/unittests/sshkey/test_bitmap$(EXEEXT)
488 502
489tests interop-tests t-exec: regress-prep $(TARGETS) $(REGRESS_BINARIES) 503tests interop-tests t-exec: regress-prep $(TARGETS) $(REGRESS_BINARIES)
490 BUILDDIR=`pwd`; \ 504 BUILDDIR=`pwd`; \
diff --git a/regress/unittests/Makefile b/regress/unittests/Makefile
index bdb4574e2..722940a1b 100644
--- a/regress/unittests/Makefile
+++ b/regress/unittests/Makefile
@@ -1,5 +1,5 @@
1# $OpenBSD: Makefile,v 1.1 2014/04/30 05:32:00 djm Exp $ 1# $OpenBSD: Makefile,v 1.1 2014/04/30 05:32:00 djm Exp $
2 2
3SUBDIR= test_helper sshbuf sshkey 3SUBDIR= test_helper sshbuf sshkey bitmap
4 4
5.include <bsd.subdir.mk> 5.include <bsd.subdir.mk>
diff --git a/regress/unittests/bitmap/Makefile b/regress/unittests/bitmap/Makefile
new file mode 100644
index 000000000..b704d22d6
--- /dev/null
+++ b/regress/unittests/bitmap/Makefile
@@ -0,0 +1,12 @@
1# $OpenBSD: Makefile,v 1.1 2015/01/15 07:36:28 djm Exp $
2
3TEST_ENV= "MALLOC_OPTIONS=AFGJPRX"
4
5PROG=test_bitmap
6SRCS=tests.c
7REGRESS_TARGETS=run-regress-${PROG}
8
9run-regress-${PROG}: ${PROG}
10 env ${TEST_ENV} ./${PROG}
11
12.include <bsd.regress.mk>
diff --git a/regress/unittests/bitmap/tests.c b/regress/unittests/bitmap/tests.c
new file mode 100644
index 000000000..6789661ce
--- /dev/null
+++ b/regress/unittests/bitmap/tests.c
@@ -0,0 +1,131 @@
1/* $OpenBSD: tests.c,v 1.1 2015/01/15 07:36:28 djm Exp $ */
2/*
3 * Regress test for bitmap.h bitmap API
4 *
5 * Placed in the public domain
6 */
7
8#include <sys/types.h>
9#include <sys/param.h>
10#include <stdio.h>
11#include <stdint.h>
12#include <stdlib.h>
13#include <string.h>
14
15#include <openssl/bn.h>
16
17#include "test_helper.h"
18
19#include "bitmap.h"
20
21#define NTESTS 131
22
23void
24tests(void)
25{
26 struct bitmap *b;
27 BIGNUM *bn;
28 size_t len;
29 int i, j, k, n;
30 u_char bbuf[1024], bnbuf[1024];
31 int r;
32
33 TEST_START("bitmap_new");
34 b = bitmap_new();
35 ASSERT_PTR_NE(b, NULL);
36 bn = BN_new();
37 ASSERT_PTR_NE(bn, NULL);
38 TEST_DONE();
39
40 TEST_START("bitmap_set_bit / bitmap_test_bit");
41 for (i = -1; i < NTESTS; i++) {
42 for (j = -1; j < NTESTS; j++) {
43 for (k = -1; k < NTESTS; k++) {
44 bitmap_zero(b);
45 BN_clear(bn);
46
47 test_subtest_info("set %d/%d/%d", i, j, k);
48 /* Set bits */
49 if (i >= 0) {
50 ASSERT_INT_EQ(bitmap_set_bit(b, i), 0);
51 ASSERT_INT_EQ(BN_set_bit(bn, i), 1);
52 }
53 if (j >= 0) {
54 ASSERT_INT_EQ(bitmap_set_bit(b, j), 0);
55 ASSERT_INT_EQ(BN_set_bit(bn, j), 1);
56 }
57 if (k >= 0) {
58 ASSERT_INT_EQ(bitmap_set_bit(b, k), 0);
59 ASSERT_INT_EQ(BN_set_bit(bn, k), 1);
60 }
61
62 /* Check perfect match between bitmap and bn */
63 test_subtest_info("match %d/%d/%d", i, j, k);
64 for (n = 0; n < NTESTS; n++) {
65 ASSERT_INT_EQ(BN_is_bit_set(bn, n),
66 bitmap_test_bit(b, n));
67 }
68
69 /* Test length calculations */
70 test_subtest_info("length %d/%d/%d", i, j, k);
71 ASSERT_INT_EQ(BN_num_bits(bn),
72 (int)bitmap_nbits(b));
73 ASSERT_INT_EQ(BN_num_bytes(bn),
74 (int)bitmap_nbytes(b));
75
76 /* Test serialisation */
77 test_subtest_info("serialise %d/%d/%d",
78 i, j, k);
79 len = bitmap_nbytes(b);
80 memset(bbuf, 0xfc, sizeof(bbuf));
81 ASSERT_INT_EQ(bitmap_to_string(b, bbuf,
82 sizeof(bbuf)), 0);
83 for (n = len; n < (int)sizeof(bbuf); n++)
84 ASSERT_U8_EQ(bbuf[n], 0xfc);
85 r = BN_bn2bin(bn, bnbuf);
86 ASSERT_INT_GE(r, 0);
87 ASSERT_INT_EQ(r, (int)len);
88 ASSERT_MEM_EQ(bbuf, bnbuf, len);
89
90 /* Test deserialisation */
91 test_subtest_info("deserialise %d/%d/%d",
92 i, j, k);
93 bitmap_zero(b);
94 ASSERT_INT_EQ(bitmap_from_string(b, bnbuf,
95 len), 0);
96 for (n = 0; n < NTESTS; n++) {
97 ASSERT_INT_EQ(BN_is_bit_set(bn, n),
98 bitmap_test_bit(b, n));
99 }
100
101 /* Test clearing bits */
102 test_subtest_info("clear %d/%d/%d",
103 i, j, k);
104 for (n = 0; n < NTESTS; n++) {
105 ASSERT_INT_EQ(bitmap_set_bit(b, n), 0);
106 ASSERT_INT_EQ(BN_set_bit(bn, n), 1);
107 }
108 if (i >= 0) {
109 bitmap_clear_bit(b, i);
110 BN_clear_bit(bn, i);
111 }
112 if (j >= 0) {
113 bitmap_clear_bit(b, j);
114 BN_clear_bit(bn, j);
115 }
116 if (k >= 0) {
117 bitmap_clear_bit(b, k);
118 BN_clear_bit(bn, k);
119 }
120 for (n = 0; n < NTESTS; n++) {
121 ASSERT_INT_EQ(BN_is_bit_set(bn, n),
122 bitmap_test_bit(b, n));
123 }
124 }
125 }
126 }
127 bitmap_free(b);
128 BN_free(bn);
129 TEST_DONE();
130}
131
diff --git a/regress/unittests/test_helper/test_helper.c b/regress/unittests/test_helper/test_helper.c
index 6f7f381c7..400555ed2 100644
--- a/regress/unittests/test_helper/test_helper.c
+++ b/regress/unittests/test_helper/test_helper.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: test_helper.c,v 1.3 2015/01/13 14:51:51 djm Exp $ */ 1/* $OpenBSD: test_helper.c,v 1.4 2015/01/15 07:36:28 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2011 Damien Miller <djm@mindrot.org> 3 * Copyright (c) 2011 Damien Miller <djm@mindrot.org>
4 * 4 *
@@ -114,6 +114,7 @@ static u_int test_number = 0;
114static test_onerror_func_t *test_onerror = NULL; 114static test_onerror_func_t *test_onerror = NULL;
115static void *onerror_ctx = NULL; 115static void *onerror_ctx = NULL;
116static const char *data_dir = NULL; 116static const char *data_dir = NULL;
117static char subtest_info[512];
117 118
118int 119int
119main(int argc, char **argv) 120main(int argc, char **argv)
@@ -185,8 +186,9 @@ test_data_file(const char *name)
185void 186void
186test_info(char *s, size_t len) 187test_info(char *s, size_t len)
187{ 188{
188 snprintf(s, len, "In test %u - \"%s\"\n", test_number, 189 snprintf(s, len, "In test %u: \"%s\"%s%s\n", test_number,
189 active_test_name == NULL ? "<none>" : active_test_name); 190 active_test_name == NULL ? "<none>" : active_test_name,
191 *subtest_info != '\0' ? " - " : "", subtest_info);
190} 192}
191 193
192#ifdef SIGINFO 194#ifdef SIGINFO
@@ -205,6 +207,7 @@ test_start(const char *n)
205{ 207{
206 assert(active_test_name == NULL); 208 assert(active_test_name == NULL);
207 assert((active_test_name = strdup(n)) != NULL); 209 assert((active_test_name = strdup(n)) != NULL);
210 *subtest_info = '\0';
208 if (verbose_mode) 211 if (verbose_mode)
209 printf("test %u - \"%s\": ", test_number, active_test_name); 212 printf("test %u - \"%s\": ", test_number, active_test_name);
210 test_number++; 213 test_number++;
@@ -223,6 +226,7 @@ set_onerror_func(test_onerror_func_t *f, void *ctx)
223void 226void
224test_done(void) 227test_done(void)
225{ 228{
229 *subtest_info = '\0';
226 assert(active_test_name != NULL); 230 assert(active_test_name != NULL);
227 free(active_test_name); 231 free(active_test_name);
228 active_test_name = NULL; 232 active_test_name = NULL;
@@ -235,6 +239,16 @@ test_done(void)
235} 239}
236 240
237void 241void
242test_subtest_info(const char *fmt, ...)
243{
244 va_list ap;
245
246 va_start(ap, fmt);
247 vsnprintf(subtest_info, sizeof(subtest_info), fmt, ap);
248 va_end(ap);
249}
250
251void
238ssl_err_check(const char *file, int line) 252ssl_err_check(const char *file, int line)
239{ 253{
240 long openssl_error = ERR_get_error(); 254 long openssl_error = ERR_get_error();
@@ -280,8 +294,9 @@ static void
280test_header(const char *file, int line, const char *a1, const char *a2, 294test_header(const char *file, int line, const char *a1, const char *a2,
281 const char *name, enum test_predicate pred) 295 const char *name, enum test_predicate pred)
282{ 296{
283 fprintf(stderr, "\n%s:%d test #%u \"%s\"\n", 297 fprintf(stderr, "\n%s:%d test #%u \"%s\"%s%s\n",
284 file, line, test_number, active_test_name); 298 file, line, test_number, active_test_name,
299 *subtest_info != '\0' ? " - " : "", subtest_info);
285 fprintf(stderr, "ASSERT_%s_%s(%s%s%s) failed:\n", 300 fprintf(stderr, "ASSERT_%s_%s(%s%s%s) failed:\n",
286 name, pred_name(pred), a1, 301 name, pred_name(pred), a1,
287 a2 != NULL ? ", " : "", a2 != NULL ? a2 : ""); 302 a2 != NULL ? ", " : "", a2 != NULL ? a2 : "");
diff --git a/regress/unittests/test_helper/test_helper.h b/regress/unittests/test_helper/test_helper.h
index b6a39ea3e..48c888ac3 100644
--- a/regress/unittests/test_helper/test_helper.h
+++ b/regress/unittests/test_helper/test_helper.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: test_helper.h,v 1.4 2015/01/13 14:51:51 djm Exp $ */ 1/* $OpenBSD: test_helper.h,v 1.5 2015/01/15 07:36:28 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2011 Damien Miller <djm@mindrot.org> 3 * Copyright (c) 2011 Damien Miller <djm@mindrot.org>
4 * 4 *
@@ -43,6 +43,8 @@ void test_start(const char *n);
43void test_info(char *s, size_t len); 43void test_info(char *s, size_t len);
44void set_onerror_func(test_onerror_func_t *f, void *ctx); 44void set_onerror_func(test_onerror_func_t *f, void *ctx);
45void test_done(void); 45void test_done(void);
46void test_subtest_info(const char *fmt, ...)
47 __attribute__((format(printf, 1, 2)));
46void ssl_err_check(const char *file, int line); 48void ssl_err_check(const char *file, int line);
47void assert_bignum(const char *file, int line, 49void assert_bignum(const char *file, int line,
48 const char *a1, const char *a2, 50 const char *a1, const char *a2,