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