| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
#include <sched.h> /* IWYU pragma: keep */ |
| 16 |
#include <setjmp.h> |
| 17 |
#include <stdarg.h> |
| 18 |
#include <stddef.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
#define UNIT_TESTING |
| 25 |
#include <cmocka.h> |
| 26 |
|
| 27 |
#include <isc/buffer.h> |
| 28 |
#include <isc/hash.h> |
| 29 |
#include <isc/hex.h> |
| 30 |
#include <isc/region.h> |
| 31 |
#include <isc/string.h> |
| 32 |
#include <isc/util.h> |
| 33 |
|
| 34 |
#include <tests/isc.h> |
| 35 |
|
| 36 |
/* Hash function test */ |
| 37 |
ISC_RUN_TEST_IMPL(isc_hash32) { |
| 38 |
uint32_t h1; |
| 39 |
uint32_t h2; |
| 40 |
|
| 41 |
/* Immutability of hash function */ |
| 42 |
h1 = isc_hash32(NULL, 0, true); |
| 43 |
h2 = isc_hash32(NULL, 0, true); |
| 44 |
|
| 45 |
assert_int_equal(h1, h2); |
| 46 |
|
| 47 |
/* Hash function characteristics */ |
| 48 |
h1 = isc_hash32("Hello world", 12, true); |
| 49 |
h2 = isc_hash32("Hello world", 12, true); |
| 50 |
|
| 51 |
assert_int_equal(h1, h2); |
| 52 |
|
| 53 |
/* Case */ |
| 54 |
h1 = isc_hash32("Hello world", 12, false); |
| 55 |
h2 = isc_hash32("heLLo WorLd", 12, false); |
| 56 |
|
| 57 |
assert_int_equal(h1, h2); |
| 58 |
|
| 59 |
/* Unequal */ |
| 60 |
h1 = isc_hash32("Hello world", 12, true); |
| 61 |
h2 = isc_hash32("heLLo WorLd", 12, true); |
| 62 |
|
| 63 |
assert_int_not_equal(h1, h2); |
| 64 |
} |
| 65 |
|
| 66 |
/* Hash function test */ |
| 67 |
ISC_RUN_TEST_IMPL(isc_hash64) { |
| 68 |
uint64_t h1; |
| 69 |
uint64_t h2; |
| 70 |
|
| 71 |
/* Immutability of hash function */ |
| 72 |
h1 = isc_hash64(NULL, 0, true); |
| 73 |
h2 = isc_hash64(NULL, 0, true); |
| 74 |
|
| 75 |
assert_int_equal(h1, h2); |
| 76 |
|
| 77 |
/* Hash function characteristics */ |
| 78 |
h1 = isc_hash64("Hello world", 12, true); |
| 79 |
h2 = isc_hash64("Hello world", 12, true); |
| 80 |
|
| 81 |
assert_int_equal(h1, h2); |
| 82 |
|
| 83 |
/* Case */ |
| 84 |
h1 = isc_hash64("Hello world", 12, false); |
| 85 |
h2 = isc_hash64("heLLo WorLd", 12, false); |
| 86 |
|
| 87 |
assert_int_equal(h1, h2); |
| 88 |
|
| 89 |
/* Unequal */ |
| 90 |
h1 = isc_hash64("Hello world", 12, true); |
| 91 |
h2 = isc_hash64("heLLo WorLd", 12, true); |
| 92 |
|
| 93 |
assert_int_not_equal(h1, h2); |
| 94 |
} |
| 95 |
|
| 96 |
/* Hash function initializer test */ |
| 97 |
ISC_RUN_TEST_IMPL(isc_hash_initializer) { |
| 98 |
uint64_t h1; |
| 99 |
uint64_t h2; |
| 100 |
|
| 101 |
h1 = isc_hash64("Hello world", 12, true); |
| 102 |
h2 = isc_hash64("Hello world", 12, true); |
| 103 |
|
| 104 |
assert_int_equal(h1, h2); |
| 105 |
|
| 106 |
isc_hash_set_initializer(isc_hash_get_initializer()); |
| 107 |
|
| 108 |
/* Hash value must not change */ |
| 109 |
h2 = isc_hash64("Hello world", 12, true); |
| 110 |
|
| 111 |
assert_int_equal(h1, h2); |
| 112 |
} |
| 113 |
|
| 114 |
ISC_TEST_LIST_START |
| 115 |
|
| 116 |
ISC_TEST_ENTRY(isc_hash32) |
| 117 |
ISC_TEST_ENTRY(isc_hash64) |
| 118 |
ISC_TEST_ENTRY(isc_hash_initializer) |
| 119 |
|
| 120 |
ISC_TEST_LIST_END |
| 121 |
|
| 122 |
ISC_TEST_MAIN |