lhash_test.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <openssl/opensslconf.h>
  13. #include <openssl/lhash.h>
  14. #include <openssl/err.h>
  15. #include <openssl/crypto.h>
  16. #include "internal/nelem.h"
  17. #include "testutil.h"
  18. /*
  19. * The macros below generate unused functions which error out one of the clang
  20. * builds. We disable this check here.
  21. */
  22. #ifdef __clang__
  23. #pragma clang diagnostic ignored "-Wunused-function"
  24. #endif
  25. DEFINE_LHASH_OF(int);
  26. static int int_tests[] = { 65537, 13, 1, 3, -5, 6, 7, 4, -10, -12, -14, 22, 9,
  27. -17, 16, 17, -23, 35, 37, 173, 11 };
  28. static const unsigned int n_int_tests = OSSL_NELEM(int_tests);
  29. static short int_found[OSSL_NELEM(int_tests)];
  30. static unsigned long int int_hash(const int *p)
  31. {
  32. return 3 & *p; /* To force collisions */
  33. }
  34. static int int_cmp(const int *p, const int *q)
  35. {
  36. return *p != *q;
  37. }
  38. static int int_find(int n)
  39. {
  40. unsigned int i;
  41. for (i = 0; i < n_int_tests; i++)
  42. if (int_tests[i] == n)
  43. return i;
  44. return -1;
  45. }
  46. static void int_doall(int *v)
  47. {
  48. int_found[int_find(*v)]++;
  49. }
  50. static void int_doall_arg(int *p, short *f)
  51. {
  52. f[int_find(*p)]++;
  53. }
  54. IMPLEMENT_LHASH_DOALL_ARG(int, short);
  55. static int test_int_lhash(void)
  56. {
  57. static struct {
  58. int data;
  59. int null;
  60. } dels[] = {
  61. { 65537, 0 },
  62. { 173, 0 },
  63. { 999, 1 },
  64. { 37, 0 },
  65. { 1, 0 },
  66. { 34, 1 }
  67. };
  68. const unsigned int n_dels = OSSL_NELEM(dels);
  69. LHASH_OF(int) *h = lh_int_new(&int_hash, &int_cmp);
  70. unsigned int i;
  71. int testresult = 0, j, *p;
  72. if (!TEST_ptr(h))
  73. goto end;
  74. /* insert */
  75. for (i = 0; i < n_int_tests; i++)
  76. if (!TEST_ptr_null(lh_int_insert(h, int_tests + i))) {
  77. TEST_info("int insert %d", i);
  78. goto end;
  79. }
  80. /* num_items */
  81. if (!TEST_int_eq(lh_int_num_items(h), n_int_tests))
  82. goto end;
  83. /* retrieve */
  84. for (i = 0; i < n_int_tests; i++)
  85. if (!TEST_int_eq(*lh_int_retrieve(h, int_tests + i), int_tests[i])) {
  86. TEST_info("lhash int retrieve value %d", i);
  87. goto end;
  88. }
  89. for (i = 0; i < n_int_tests; i++)
  90. if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + i), int_tests + i)) {
  91. TEST_info("lhash int retrieve address %d", i);
  92. goto end;
  93. }
  94. j = 1;
  95. if (!TEST_ptr_eq(lh_int_retrieve(h, &j), int_tests + 2))
  96. goto end;
  97. /* replace */
  98. j = 13;
  99. if (!TEST_ptr(p = lh_int_insert(h, &j)))
  100. goto end;
  101. if (!TEST_ptr_eq(p, int_tests + 1))
  102. goto end;
  103. if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + 1), &j))
  104. goto end;
  105. /* do_all */
  106. memset(int_found, 0, sizeof(int_found));
  107. lh_int_doall(h, &int_doall);
  108. for (i = 0; i < n_int_tests; i++)
  109. if (!TEST_int_eq(int_found[i], 1)) {
  110. TEST_info("lhash int doall %d", i);
  111. goto end;
  112. }
  113. /* do_all_arg */
  114. memset(int_found, 0, sizeof(int_found));
  115. lh_int_doall_short(h, int_doall_arg, int_found);
  116. for (i = 0; i < n_int_tests; i++)
  117. if (!TEST_int_eq(int_found[i], 1)) {
  118. TEST_info("lhash int doall arg %d", i);
  119. goto end;
  120. }
  121. /* delete */
  122. for (i = 0; i < n_dels; i++) {
  123. const int b = lh_int_delete(h, &dels[i].data) == NULL;
  124. if (!TEST_int_eq(b ^ dels[i].null, 0)) {
  125. TEST_info("lhash int delete %d", i);
  126. goto end;
  127. }
  128. }
  129. /* error */
  130. if (!TEST_int_eq(lh_int_error(h), 0))
  131. goto end;
  132. testresult = 1;
  133. end:
  134. lh_int_free(h);
  135. return testresult;
  136. }
  137. static unsigned long int stress_hash(const int *p)
  138. {
  139. return *p;
  140. }
  141. static int test_stress(void)
  142. {
  143. LHASH_OF(int) *h = lh_int_new(&stress_hash, &int_cmp);
  144. const unsigned int n = 2500000;
  145. unsigned int i;
  146. int testresult = 0, *p;
  147. if (!TEST_ptr(h))
  148. goto end;
  149. /* insert */
  150. for (i = 0; i < n; i++) {
  151. p = OPENSSL_malloc(sizeof(i));
  152. if (!TEST_ptr(p)) {
  153. TEST_info("lhash stress out of memory %d", i);
  154. goto end;
  155. }
  156. *p = 3 * i + 1;
  157. lh_int_insert(h, p);
  158. }
  159. /* num_items */
  160. if (!TEST_int_eq(lh_int_num_items(h), n))
  161. goto end;
  162. TEST_info("hash full statistics:");
  163. OPENSSL_LH_stats_bio((OPENSSL_LHASH *)h, bio_err);
  164. TEST_note("hash full node usage:");
  165. OPENSSL_LH_node_usage_stats_bio((OPENSSL_LHASH *)h, bio_err);
  166. /* delete in a different order */
  167. for (i = 0; i < n; i++) {
  168. const int j = (7 * i + 4) % n * 3 + 1;
  169. if (!TEST_ptr(p = lh_int_delete(h, &j))) {
  170. TEST_info("lhash stress delete %d\n", i);
  171. goto end;
  172. }
  173. if (!TEST_int_eq(*p, j)) {
  174. TEST_info("lhash stress bad value %d", i);
  175. goto end;
  176. }
  177. OPENSSL_free(p);
  178. }
  179. TEST_info("hash empty statistics:");
  180. OPENSSL_LH_stats_bio((OPENSSL_LHASH *)h, bio_err);
  181. TEST_note("hash empty node usage:");
  182. OPENSSL_LH_node_usage_stats_bio((OPENSSL_LHASH *)h, bio_err);
  183. testresult = 1;
  184. end:
  185. lh_int_free(h);
  186. return testresult;
  187. }
  188. int setup_tests(void)
  189. {
  190. ADD_TEST(test_int_lhash);
  191. ADD_TEST(test_stress);
  192. return 1;
  193. }