xref: /freebsd/crypto/openssh/regress/unittests/bitmap/tests.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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 "includes.h"
9 
10 #include <sys/types.h>
11 #include <sys/param.h>
12 #include <stdio.h>
13 #ifdef HAVE_STDINT_H
14 #include <stdint.h>
15 #endif
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #ifdef WITH_OPENSSL
20 #include <openssl/bn.h>
21 #endif
22 
23 #include "../test_helper/test_helper.h"
24 
25 #include "bitmap.h"
26 
27 #define NTESTS 131
28 
29 void
30 tests(void)
31 {
32 #ifdef WITH_OPENSSL
33 	struct bitmap *b;
34 	BIGNUM *bn;
35 	size_t len;
36 	int i, j, k, n;
37 	u_char bbuf[1024], bnbuf[1024];
38 	int r;
39 
40 	TEST_START("bitmap_new");
41 	b = bitmap_new();
42 	ASSERT_PTR_NE(b, NULL);
43 	bn = BN_new();
44 	ASSERT_PTR_NE(bn, NULL);
45 	TEST_DONE();
46 
47 	TEST_START("bitmap_set_bit / bitmap_test_bit");
48 	for (i = -1; i < NTESTS; i++) {
49 		for (j = -1; j < NTESTS; j++) {
50 			for (k = -1; k < NTESTS; k++) {
51 				bitmap_zero(b);
52 				BN_clear(bn);
53 
54 				test_subtest_info("set %d/%d/%d", i, j, k);
55 				/* Set bits */
56 				if (i >= 0) {
57 					ASSERT_INT_EQ(bitmap_set_bit(b, i), 0);
58 					ASSERT_INT_EQ(BN_set_bit(bn, i), 1);
59 				}
60 				if (j >= 0) {
61 					ASSERT_INT_EQ(bitmap_set_bit(b, j), 0);
62 					ASSERT_INT_EQ(BN_set_bit(bn, j), 1);
63 				}
64 				if (k >= 0) {
65 					ASSERT_INT_EQ(bitmap_set_bit(b, k), 0);
66 					ASSERT_INT_EQ(BN_set_bit(bn, k), 1);
67 				}
68 
69 				/* Check perfect match between bitmap and bn */
70 				test_subtest_info("match %d/%d/%d", i, j, k);
71 				for (n = 0; n < NTESTS; n++) {
72 					ASSERT_INT_EQ(BN_is_bit_set(bn, n),
73 					    bitmap_test_bit(b, n));
74 				}
75 
76 				/* Test length calculations */
77 				test_subtest_info("length %d/%d/%d", i, j, k);
78 				ASSERT_INT_EQ(BN_num_bits(bn),
79 				    (int)bitmap_nbits(b));
80 				ASSERT_INT_EQ(BN_num_bytes(bn),
81 				    (int)bitmap_nbytes(b));
82 
83 				/* Test serialisation */
84 				test_subtest_info("serialise %d/%d/%d",
85 				    i, j, k);
86 				len = bitmap_nbytes(b);
87 				memset(bbuf, 0xfc, sizeof(bbuf));
88 				ASSERT_INT_EQ(bitmap_to_string(b, bbuf,
89 				    sizeof(bbuf)), 0);
90 				for (n = len; n < (int)sizeof(bbuf); n++)
91 					ASSERT_U8_EQ(bbuf[n], 0xfc);
92 				r = BN_bn2bin(bn, bnbuf);
93 				ASSERT_INT_GE(r, 0);
94 				ASSERT_INT_EQ(r, (int)len);
95 				ASSERT_MEM_EQ(bbuf, bnbuf, len);
96 
97 				/* Test deserialisation */
98 				test_subtest_info("deserialise %d/%d/%d",
99 				    i, j, k);
100 				bitmap_zero(b);
101 				ASSERT_INT_EQ(bitmap_from_string(b, bnbuf,
102 				    len), 0);
103 				for (n = 0; n < NTESTS; n++) {
104 					ASSERT_INT_EQ(BN_is_bit_set(bn, n),
105 					    bitmap_test_bit(b, n));
106 				}
107 
108 				/* Test clearing bits */
109 				test_subtest_info("clear %d/%d/%d",
110 				    i, j, k);
111 				for (n = 0; n < NTESTS; n++) {
112 					ASSERT_INT_EQ(bitmap_set_bit(b, n), 0);
113 					ASSERT_INT_EQ(BN_set_bit(bn, n), 1);
114 				}
115 				if (i >= 0) {
116 					bitmap_clear_bit(b, i);
117 					BN_clear_bit(bn, i);
118 				}
119 				if (j >= 0) {
120 					bitmap_clear_bit(b, j);
121 					BN_clear_bit(bn, j);
122 				}
123 				if (k >= 0) {
124 					bitmap_clear_bit(b, k);
125 					BN_clear_bit(bn, k);
126 				}
127 				for (n = 0; n < NTESTS; n++) {
128 					ASSERT_INT_EQ(BN_is_bit_set(bn, n),
129 					    bitmap_test_bit(b, n));
130 				}
131 			}
132 		}
133 	}
134 	bitmap_free(b);
135 	BN_free(bn);
136 	TEST_DONE();
137 #endif
138 }
139 
140