Make default_method mostly compile-time
[openssl.git] / test / lhash_test.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
11  * Copyright (c) 2017 Oracle and/or its affiliates.  All rights reserved.
12  */
13
14 #include <stdio.h>
15 #include <string.h>
16
17 #include <openssl/opensslconf.h>
18 #include <openssl/lhash.h>
19 #include <openssl/err.h>
20 #include <openssl/crypto.h>
21
22 #include "e_os.h"
23 #include "test_main.h"
24 #include "testutil.h"
25
26 /*
27  * The macros below generate unused functions which error out one of the clang
28  * builds.  We disable this check here.
29  */
30 #ifdef __clang__
31 #pragma clang diagnostic ignored "-Wunused-function"
32 #endif
33
34 DEFINE_LHASH_OF(int);
35
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)];
40
41 static unsigned long int int_hash(const int *p)
42 {
43     return 3 & *p;      /* To force collisions */
44 }
45
46 static int int_cmp(const int *p, const int *q)
47 {
48     return *p != *q;
49 }
50
51 static int int_find(int n)
52 {
53     unsigned int i;
54
55     for (i = 0; i < n_int_tests; i++)
56         if (int_tests[i] == n)
57             return i;
58     return -1;
59 }
60
61 static void int_doall(int *v)
62 {
63     int_found[int_find(*v)]++;
64 }
65
66 static void int_doall_arg(int *p, short *f)
67 {
68     f[int_find(*p)]++;
69 }
70
71 IMPLEMENT_LHASH_DOALL_ARG(int, short);
72
73 static int test_int_lhash(void)
74 {
75     static struct {
76         int data;
77         int null;
78     } dels[] = {
79         { 65537,    0 },
80         { 173,      0 },
81         { 999,      1 },
82         { 37,       0 },
83         { 1,        0 },
84         { 34,       1 }     
85     };
86     const unsigned int n_dels = OSSL_NELEM(dels);
87     LHASH_OF(int) *h = lh_int_new(&int_hash, &int_cmp);
88     unsigned int i;
89     int testresult = 0, j, *p;
90
91     if (!TEST_ptr(h))
92         goto end;
93
94     /* insert */
95     for (i = 0; i < n_int_tests; i++)
96         if (!TEST_ptr_null(lh_int_insert(h, int_tests + i))) {
97             TEST_info("int insert %d", i);
98             goto end;
99         }
100
101     /* num_items */
102     if (!TEST_int_eq(lh_int_num_items(h), n_int_tests))
103         goto end;
104
105     /* retrieve */
106     for (i = 0; i < n_int_tests; i++)
107         if (!TEST_int_eq(*lh_int_retrieve(h, int_tests + i), int_tests[i])) {
108             TEST_info("lhash int retrieve value %d", i);
109             goto end;
110         }
111     for (i = 0; i < n_int_tests; i++)
112         if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + i), int_tests + i)) {
113             TEST_info("lhash int retrieve address %d", i);
114             goto end;
115         }
116     j = 1;
117     if (!TEST_ptr_eq(lh_int_retrieve(h, &j), int_tests + 2))
118         goto end;
119
120     /* replace */
121     j = 13;
122     if (!TEST_ptr(p = lh_int_insert(h, &j)))
123         goto end;
124     if (!TEST_ptr_eq(p, int_tests + 1))
125         goto end;
126     if (!TEST_ptr_eq(lh_int_retrieve(h, int_tests + 1), &j))
127         goto end;
128
129     /* do_all */
130     memset(int_found, 0, sizeof(int_found));
131     lh_int_doall(h, &int_doall);
132     for (i = 0; i < n_int_tests; i++)
133         if (!TEST_int_eq(int_found[i], 1)) {
134             TEST_info("lhash int doall %d", i);
135             goto end;
136         }
137     
138     /* do_all_arg */
139     memset(int_found, 0, sizeof(int_found));
140     lh_int_doall_short(h, int_doall_arg, int_found);
141     for (i = 0; i < n_int_tests; i++)
142         if (!TEST_int_eq(int_found[i], 1)) {
143             TEST_info("lhash int doall arg %d", i);
144             goto end;
145         }
146     
147     /* delete */
148     for (i = 0; i < n_dels; i++) {
149         const int b = lh_int_delete(h, &dels[i].data) == NULL;
150         if (!TEST_int_eq(b ^ dels[i].null,  0)) {
151             TEST_info("lhash int delete %d", i);
152             goto end;
153         }
154     }
155
156     /* error */
157     if (!TEST_int_eq(lh_int_error(h), 0))
158         goto end;
159
160     testresult = 1;
161 end:
162     lh_int_free(h);
163     return testresult;
164 }
165
166 static unsigned long int stress_hash(const int *p)
167 {
168     return *p;
169 }
170
171 static int test_stress(void)
172 {
173     LHASH_OF(int) *h = lh_int_new(&stress_hash, &int_cmp);
174     const unsigned int n = 2500000;
175     unsigned int i;
176     int testresult = 0, *p;
177
178     if (!TEST_ptr(h))
179         goto end;
180
181     /* insert */
182     for (i = 0; i < n; i++) {
183         p = OPENSSL_malloc(sizeof(i));
184         if (!TEST_ptr(p)) {
185             TEST_info("lhash stress out of memory %d", i);
186             goto end;
187         }
188         *p = 3 * i + 1;
189         lh_int_insert(h, p);
190     }
191
192     /* num_items */
193     if (!TEST_int_eq(lh_int_num_items(h), n))
194             goto end;
195
196     fprintf(stderr, "hash full statistics:\n");
197     OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);
198     fprintf(stderr, "\nhash full node usage:\n");
199     OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);
200
201     /* delete in a different order */
202     for (i = 0; i < n; i++) {
203         const int j = (7 * i + 4) % n * 3 + 1;
204
205         if (!TEST_ptr(p = lh_int_delete(h, &j))) {
206             TEST_info("lhash stress delete %d\n", i);
207             goto end;
208         }
209         if (!TEST_int_eq(*p, j)) {
210             TEST_info("lhash stress bad value %d", i);
211             goto end;
212         }
213         OPENSSL_free(p);
214     }
215
216     fprintf(stderr, "\nhash empty statistics:\n");
217     OPENSSL_LH_stats((OPENSSL_LHASH *)h, stderr);
218     fprintf(stderr, "\nhash empty node usage:\n");
219     OPENSSL_LH_node_usage_stats((OPENSSL_LHASH *)h, stderr);
220
221     testresult = 1;
222 end:
223     lh_int_free(h);
224     return testresult;
225 }
226
227 void register_tests(void)
228 {
229     ADD_TEST(test_int_lhash);
230     ADD_TEST(test_stress);
231 }