]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/lhash_test.c
2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
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
14 #include <openssl/opensslconf.h>
15 #include <openssl/lhash.h>
16 #include <openssl/err.h>
17 #include <openssl/crypto.h>
19 #include "internal/nelem.h"
23 * The macros below generate unused functions which error out one of the clang
24 * builds. We disable this check here.
27 #pragma clang diagnostic ignored "-Wunused-function"
32 static int int_tests
[] = { 65537, 13, 1, 3, -5, 6, 7, 4, -10, -12, -14, 22, 9,
33 -17, 16, 17, -23, 35, 37, 173, 11 };
34 static const unsigned int n_int_tests
= OSSL_NELEM(int_tests
);
35 static short int_found
[OSSL_NELEM(int_tests
)];
37 static unsigned long int int_hash(const int *p
)
39 return 3 & *p
; /* To force collisions */
42 static int int_cmp(const int *p
, const int *q
)
47 static int int_find(int n
)
51 for (i
= 0; i
< n_int_tests
; i
++)
52 if (int_tests
[i
] == n
)
57 static void int_doall(int *v
)
59 int_found
[int_find(*v
)]++;
62 static void int_doall_arg(int *p
, short *f
)
67 IMPLEMENT_LHASH_DOALL_ARG(int, short);
69 static int test_int_lhash(void)
82 const unsigned int n_dels
= OSSL_NELEM(dels
);
83 LHASH_OF(int) *h
= lh_int_new(&int_hash
, &int_cmp
);
85 int testresult
= 0, j
, *p
;
91 for (i
= 0; i
< n_int_tests
; i
++)
92 if (!TEST_ptr_null(lh_int_insert(h
, int_tests
+ i
))) {
93 TEST_info("int insert %d", i
);
98 if (!TEST_int_eq(lh_int_num_items(h
), n_int_tests
))
102 for (i
= 0; i
< n_int_tests
; i
++)
103 if (!TEST_int_eq(*lh_int_retrieve(h
, int_tests
+ i
), int_tests
[i
])) {
104 TEST_info("lhash int retrieve value %d", i
);
107 for (i
= 0; i
< n_int_tests
; i
++)
108 if (!TEST_ptr_eq(lh_int_retrieve(h
, int_tests
+ i
), int_tests
+ i
)) {
109 TEST_info("lhash int retrieve address %d", i
);
113 if (!TEST_ptr_eq(lh_int_retrieve(h
, &j
), int_tests
+ 2))
118 if (!TEST_ptr(p
= lh_int_insert(h
, &j
)))
120 if (!TEST_ptr_eq(p
, int_tests
+ 1))
122 if (!TEST_ptr_eq(lh_int_retrieve(h
, int_tests
+ 1), &j
))
126 memset(int_found
, 0, sizeof(int_found
));
127 lh_int_doall(h
, &int_doall
);
128 for (i
= 0; i
< n_int_tests
; i
++)
129 if (!TEST_int_eq(int_found
[i
], 1)) {
130 TEST_info("lhash int doall %d", i
);
135 memset(int_found
, 0, sizeof(int_found
));
136 lh_int_doall_short(h
, int_doall_arg
, int_found
);
137 for (i
= 0; i
< n_int_tests
; i
++)
138 if (!TEST_int_eq(int_found
[i
], 1)) {
139 TEST_info("lhash int doall arg %d", i
);
144 for (i
= 0; i
< n_dels
; i
++) {
145 const int b
= lh_int_delete(h
, &dels
[i
].data
) == NULL
;
146 if (!TEST_int_eq(b
^ dels
[i
].null
, 0)) {
147 TEST_info("lhash int delete %d", i
);
153 if (!TEST_int_eq(lh_int_error(h
), 0))
162 static unsigned long int stress_hash(const int *p
)
167 static int test_stress(void)
169 LHASH_OF(int) *h
= lh_int_new(&stress_hash
, &int_cmp
);
170 const unsigned int n
= 2500000;
172 int testresult
= 0, *p
;
178 for (i
= 0; i
< n
; i
++) {
179 p
= OPENSSL_malloc(sizeof(i
));
181 TEST_info("lhash stress out of memory %d", i
);
189 if (!TEST_int_eq(lh_int_num_items(h
), n
))
192 TEST_info("hash full statistics:");
193 OPENSSL_LH_stats_bio((OPENSSL_LHASH
*)h
, bio_err
);
194 TEST_note("hash full node usage:");
195 OPENSSL_LH_node_usage_stats_bio((OPENSSL_LHASH
*)h
, bio_err
);
197 /* delete in a different order */
198 for (i
= 0; i
< n
; i
++) {
199 const int j
= (7 * i
+ 4) % n
* 3 + 1;
201 if (!TEST_ptr(p
= lh_int_delete(h
, &j
))) {
202 TEST_info("lhash stress delete %d\n", i
);
205 if (!TEST_int_eq(*p
, j
)) {
206 TEST_info("lhash stress bad value %d", i
);
212 TEST_info("hash empty statistics:");
213 OPENSSL_LH_stats_bio((OPENSSL_LHASH
*)h
, bio_err
);
214 TEST_note("hash empty node usage:");
215 OPENSSL_LH_node_usage_stats_bio((OPENSSL_LHASH
*)h
, bio_err
);
223 int setup_tests(void)
225 ADD_TEST(test_int_lhash
);
226 ADD_TEST(test_stress
);