summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-10-06 14:02:10 +0300
committerLennart Poettering <lennart@poettering.net>2015-10-06 14:02:10 +0300
commit20d2f7851ac44bd6845d060a952461f5a10e9c87 (patch)
tree8fd714c7ffa680e3811361d6e5aa7d08f226247d /src/test
parentc48eb61fa72205615e3a2bec9fb6576a5973fc6b (diff)
parent1e2527a6fede996a429bd44b30a15e76ee293437 (diff)
Merge pull request #1465 from teg/siphash24
hashmap/siphash24: refactor hash functions
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test-hashmap-plain.c4
-rw-r--r--src/test/test-prioq.c7
-rw-r--r--src/test/test-siphash24.c78
3 files changed, 82 insertions, 7 deletions
diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c
index 057b6c1dc1..78f9c19f5b 100644
--- a/src/test/test-hashmap-plain.c
+++ b/src/test/test-hashmap-plain.c
@@ -692,8 +692,8 @@ static void test_hashmap_get2(void) {
hashmap_free_free_free(m);
}
-static unsigned long crippled_hashmap_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
- return trivial_hash_func(p, hash_key) & 0xff;
+static void crippled_hashmap_func(const void *p, struct siphash *state) {
+ return trivial_hash_func(INT_TO_PTR(PTR_TO_INT(p) & 0xff), state);
}
static const struct hash_ops crippled_hashmap_ops = {
diff --git a/src/test/test-prioq.c b/src/test/test-prioq.c
index dfedc9b8dc..1e2e42cbca 100644
--- a/src/test/test-prioq.c
+++ b/src/test/test-prioq.c
@@ -89,13 +89,10 @@ static int test_compare(const void *a, const void *b) {
return 0;
}
-static unsigned long test_hash(const void *a, const uint8_t hash_key[HASH_KEY_SIZE]) {
+static void test_hash(const void *a, struct siphash *state) {
const struct test *x = a;
- uint64_t u;
- siphash24((uint8_t*) &u, &x->value, sizeof(x->value), hash_key);
-
- return (unsigned long) u;
+ siphash24_compress(&x->value, sizeof(x->value), state);
}
static const struct hash_ops test_hash_ops = {
diff --git a/src/test/test-siphash24.c b/src/test/test-siphash24.c
new file mode 100644
index 0000000000..65eb2b6f35
--- /dev/null
+++ b/src/test/test-siphash24.c
@@ -0,0 +1,78 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2015 Tom Gundersen
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "util.h"
+#include "siphash24.h"
+
+#define ITERATIONS 10000000ULL
+
+/* see https://131002.net/siphash/siphash.pdf, Appendix A */
+int main(int argc, char *argv[]) {
+ struct siphash state = {};
+ const uint8_t in[15] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e };
+ const uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
+ uint64_t out = 0;
+ unsigned i, j, k;
+ usec_t ts;
+
+ siphash24((uint8_t *)&out, in, sizeof(in), key);
+ assert_se(out == 0xa129ca6149be45e5);
+
+ assert_se(out == 0xa129ca6149be45e5ULL);
+
+ ts = now(CLOCK_MONOTONIC);
+ for (k = 0; k < ITERATIONS; k++)
+ siphash24((uint8_t *)&out, in, sizeof(in), key);
+ ts = now(CLOCK_MONOTONIC) - ts;
+
+ log_info("%llu iterations per second", (ITERATIONS * USEC_PER_SEC) / ts);
+
+ /* verify the internal state as given in the above paper */
+ siphash_init(&state, key);
+ assert_se(state.v0 == 0x7469686173716475);
+ assert_se(state.v1 == 0x6b617f6d656e6665);
+ assert_se(state.v2 == 0x6b7f62616d677361);
+ assert_se(state.v3 == 0x7b6b696e727e6c7b);
+ siphash24_compress(in, sizeof(in), &state);
+ assert_se(state.v0 == 0x4a017198de0a59e0);
+ assert_se(state.v1 == 0x0d52f6f62a4f59a4);
+ assert_se(state.v2 == 0x634cb3577b01fd3d);
+ assert_se(state.v3 == 0xa5224d6f55c7d9c8);
+ assert_se(siphash24_finalize(&state) == 0xa129ca6149be45e5);
+ assert_se(state.v0 == 0xf6bcd53893fecff1);
+ assert_se(state.v1 == 0x54b9964c7ea0d937);
+ assert_se(state.v2 == 0x1b38329c099bb55a);
+ assert_se(state.v3 == 0x1814bb89ad7be679);
+
+ /* verify that decomposing the input in three chunks gives the
+ same result */
+ for (i = 0; i < sizeof(in); i++) {
+ for (j = i; j < sizeof(in); j++) {
+ siphash_init(&state, key);
+ siphash24_compress(in, i, &state);
+ siphash24_compress(&in[i], j - i, &state);
+ siphash24_compress(&in[j], sizeof(in) - j, &state);
+ assert_se(siphash24_finalize(&state) == 0xa129ca6149be45e5);
+ }
+ }
+}