tests.c 3.0 KB

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