test: siphash24 - add regression test
authorTom Gundersen <teg@jklm.no>
Sat, 3 Oct 2015 16:41:02 +0000 (18:41 +0200)
committerTom Gundersen <teg@jklm.no>
Mon, 5 Oct 2015 15:33:58 +0000 (17:33 +0200)
.gitignore
Makefile.am
src/test/test-siphash24.c [new file with mode: 0644]

index 6149b01..709c8b5 100644 (file)
 /test-sched-prio
 /test-set
 /test-sigbus
+/test-siphash24
 /test-sleep
 /test-socket-util
 /test-ssd
index 30989bf..685d263 100644 (file)
@@ -1396,6 +1396,7 @@ tests += \
        test-path \
        test-path-util \
        test-strxcpyx \
+       test-siphash24 \
        test-unit-name \
        test-unit-file \
        test-utf8 \
@@ -2010,6 +2011,12 @@ test_execute_CFLAGS = \
 test_execute_LDADD = \
        libcore.la
 
+test_siphash24_SOURCES = \
+       src/test/test-siphash24.c
+
+test_siphash24_LDADD = \
+       libshared.la
+
 test_strxcpyx_SOURCES = \
        src/test/test-strxcpyx.c
 
diff --git a/src/test/test-siphash24.c b/src/test/test-siphash24.c
new file mode 100644 (file)
index 0000000..ec9f646
--- /dev/null
@@ -0,0 +1,47 @@
+/*-*- 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[]) {
+        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 k;
+        usec_t ts;
+
+        siphash24((uint8_t *)&out, in, sizeof(in), key);
+
+        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);
+}