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