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