diff options
author | djm@openbsd.org <djm@openbsd.org> | 2015-01-15 07:36:28 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2015-01-15 21:39:18 +1100 |
commit | d333f89abf7179021e5c3f28673f469abe032062 (patch) | |
tree | 8399e567e65d2d0c241f344c09b4ed1eb2cfbae3 /regress/unittests | |
parent | 7613f828f49c55ff356007ae9645038ab6682556 (diff) |
upstream commit
unit tests for KRL bitmap
Diffstat (limited to 'regress/unittests')
-rw-r--r-- | regress/unittests/Makefile | 2 | ||||
-rw-r--r-- | regress/unittests/bitmap/Makefile | 12 | ||||
-rw-r--r-- | regress/unittests/bitmap/tests.c | 131 | ||||
-rw-r--r-- | regress/unittests/test_helper/test_helper.c | 25 | ||||
-rw-r--r-- | regress/unittests/test_helper/test_helper.h | 4 |
5 files changed, 167 insertions, 7 deletions
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 | ||
3 | SUBDIR= test_helper sshbuf sshkey | 3 | SUBDIR= 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 | |||
3 | TEST_ENV= "MALLOC_OPTIONS=AFGJPRX" | ||
4 | |||
5 | PROG=test_bitmap | ||
6 | SRCS=tests.c | ||
7 | REGRESS_TARGETS=run-regress-${PROG} | ||
8 | |||
9 | run-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 | |||
23 | void | ||
24 | tests(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; | |||
114 | static test_onerror_func_t *test_onerror = NULL; | 114 | static test_onerror_func_t *test_onerror = NULL; |
115 | static void *onerror_ctx = NULL; | 115 | static void *onerror_ctx = NULL; |
116 | static const char *data_dir = NULL; | 116 | static const char *data_dir = NULL; |
117 | static char subtest_info[512]; | ||
117 | 118 | ||
118 | int | 119 | int |
119 | main(int argc, char **argv) | 120 | main(int argc, char **argv) |
@@ -185,8 +186,9 @@ test_data_file(const char *name) | |||
185 | void | 186 | void |
186 | test_info(char *s, size_t len) | 187 | test_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) | |||
223 | void | 226 | void |
224 | test_done(void) | 227 | test_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 | ||
237 | void | 241 | void |
242 | test_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 | |||
251 | void | ||
238 | ssl_err_check(const char *file, int line) | 252 | ssl_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 | |||
280 | test_header(const char *file, int line, const char *a1, const char *a2, | 294 | test_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); | |||
43 | void test_info(char *s, size_t len); | 43 | void test_info(char *s, size_t len); |
44 | void set_onerror_func(test_onerror_func_t *f, void *ctx); | 44 | void set_onerror_func(test_onerror_func_t *f, void *ctx); |
45 | void test_done(void); | 45 | void test_done(void); |
46 | void test_subtest_info(const char *fmt, ...) | ||
47 | __attribute__((format(printf, 1, 2))); | ||
46 | void ssl_err_check(const char *file, int line); | 48 | void ssl_err_check(const char *file, int line); |
47 | void assert_bignum(const char *file, int line, | 49 | void assert_bignum(const char *file, int line, |
48 | const char *a1, const char *a2, | 50 | const char *a1, const char *a2, |