2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
17 #include <openssl/opensslconf.h>
18 #include <openssl/lhash.h>
19 #include <openssl/err.h>
20 #include <openssl/crypto.h>
23 #include "test_main.h"
27 * The macros below generate unused functions which error out one of the clang
28 * builds. We disable this check here.
31 #pragma clang diagnostic ignored "-Wunused-function"
36 static int int_tests[] = { 65537, 13, 1, 3, -5, 6, 7, 4, -10, -12, -14, 22, 9,
37 -17, 16, 17, -23, 35, 37, 173, 11 };
38 static const unsigned int n_int_tests = OSSL_NELEM(int_tests);
39 static short int_found[OSSL_NELEM(int_tests)];
41 static unsigned long int int_hash(const int *p)
43 return 3 & *p; /* To force collisions */
46 static int int_cmp(const int *p, const int *q)
51 static int int_find(int n)
55 for (i = 0; i < n_int_tests; i++)
56 if (int_tests[i] == n)
61 static void int_doall(int *v)
63 int_found[int_find(*v)]++;
66 static void int_doall_arg(int *p, short *f)
71 IMPLEMENT_LHASH_DOALL_ARG(int, short);
73 static int test_int_lhash(void)
86 const unsigned int n_dels = OSSL_NELEM(dels);
87 LHASH_OF(int) *h = lh_int_new(&int_hash, &int_cmp);
89 int testresult = 0, j, *p;
92 fprintf(stderr, "test lhash int allocation\n");
97 for (i = 0; i < n_int_tests; i++)
98 if (lh_int_insert(h, int_tests + i) != NULL) {
99 fprintf(stderr, "test lhash int insert %d\n", i);
104 if (lh_int_num_items(h) != n_int_tests) {
105 fprintf(stderr, "test lhash int num items\n");
110 for (i = 0; i < n_int_tests; i++)
111 if (*lh_int_retrieve(h, int_tests + i) != int_tests[i]) {
112 fprintf(stderr, "test lhash int retrieve value %d\n", i);
115 for (i = 0; i < n_int_tests; i++)
116 if (lh_int_retrieve(h, int_tests + i) != int_tests + i) {
117 fprintf(stderr, "test lhash int retrieve address %d\n", i);
121 if (lh_int_retrieve(h, &j) != int_tests + 2) {
122 fprintf(stderr, "test lhash int retrieve other\n");
128 if ((p = lh_int_insert(h, &j)) == NULL) {
129 fprintf(stderr, "test lhash int replacement insert\n");
132 if (p != int_tests + 1) {
133 fprintf(stderr, "test lhash int replacement pointer\n");
136 if (lh_int_retrieve(h, int_tests + 1) != &j) {
137 fprintf(stderr, "test lhash int replacement variable\n");
142 memset(int_found, 0, sizeof(int_found));
143 lh_int_doall(h, &int_doall);
144 for (i = 0; i < n_int_tests; i++)
145 if (int_found[i] != 1) {
146 fprintf(stderr, "test lhash int doall %d\n", i);
151 memset(int_found, 0, sizeof(int_found));
152 lh_int_doall_short(h, int_doall_arg, int_found);
153 for (i = 0; i < n_int_tests; i++)
154 if (int_found[i] != 1) {
155 fprintf(stderr, "test lhash int doall arg %d\n", i);
160 for (i = 0; i < n_dels; i++) {
161 const int b = lh_int_delete(h, &dels[i].data) == NULL;
162 if ((b ^ dels[i].null) != 0) {
163 fprintf(stderr, "test lhash int delete %d\n", i);
169 if (lh_int_error(h) != 0) {
170 fprintf(stderr, "test lhash int error\n");
180 static unsigned long int stress_hash(const int *p)
185 static int test_stress(void)
187 LHASH_OF(int) *h = lh_int_new(&stress_hash, &int_cmp);
188 const unsigned int n = 2500000;
190 int testresult = 0, *p;
193 fprintf(stderr, "test lhash stress allocation\n");
198 for (i = 0; i < n; i++) {
199 p = OPENSSL_malloc(sizeof(i));
201 fprintf(stderr, "test lhash stress out of memory %d\n", i);
209 if (lh_int_num_items(h) != n) {
210 fprintf(stderr, "test lhash stress num items\n");
214 fprintf(stderr, "hash full statistics:\n");
215 OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);
216 fprintf(stderr, "\nhash full node usage:\n");
217 OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);
219 /* delete in a different order */
220 for (i = 0; i < n; i++) {
221 const int j = (7 * i + 4) % n * 3 + 1;
223 if ((p = lh_int_delete(h, &j)) == NULL) {
224 fprintf(stderr, "test lhash stress delete %d\n", i);
228 fprintf(stderr, "test lhash stress bad value %d\n", i);
234 fprintf(stderr, "\nhash empty statistics:\n");
235 OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);
236 fprintf(stderr, "\nhash empty node usage:\n");
237 OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);
245 void register_tests(void)
247 ADD_TEST(test_int_lhash);
248 ADD_TEST(test_stress);