]> granicus.if.org Git - libexpat/commitdiff
siphash: Test implementation
authorSebastian Pipping <sebastian@pipping.org>
Sat, 10 Jun 2017 22:03:28 +0000 (00:03 +0200)
committerSebastian Pipping <sebastian@pipping.org>
Sat, 10 Jun 2017 23:20:41 +0000 (01:20 +0200)
expat/Makefile.in
expat/tests/runtests.c

index cf338fe46f781330a01b52cfb016bf9602dad044..6757eea9759e1d8977c56a448dec9a52a72db11a 100644 (file)
@@ -166,7 +166,7 @@ examples/outline: examples/outline.@OBJEXT@ $(LIBRARY)
 tests/chardata.@OBJEXT@: tests/chardata.c tests/chardata.h
 tests/minicheck.@OBJEXT@: tests/minicheck.c tests/minicheck.h
 tests/memcheck.@OBJEXT@: tests/memcheck.c tests/memcheck.h
-tests/runtests.@OBJEXT@: tests/runtests.c tests/chardata.h tests/memcheck.h
+tests/runtests.@OBJEXT@: tests/runtests.c tests/chardata.h tests/memcheck.h lib/siphash.h
 tests/runtests@EXEEXT@: tests/runtests.@OBJEXT@ tests/chardata.@OBJEXT@ tests/minicheck.@OBJEXT@ tests/memcheck.@OBJEXT@ $(LIBRARY)
        $(LINK_EXE) tests/runtests.@OBJEXT@ tests/chardata.@OBJEXT@ tests/minicheck.@OBJEXT@ tests/memcheck.@OBJEXT@ $(LIBRARY)
 tests/runtestspp.@OBJEXT@: tests/runtestspp.cpp tests/runtests.c tests/chardata.h tests/memcheck.h
index f84758017d96248edd97b55f6b27afd2bb06e225..5fa093ca51c2381e56442920ffacfe3f40ce2297 100644 (file)
@@ -25,6 +25,7 @@
 #include "internal.h"  /* for UNUSED_P only */
 #include "minicheck.h"
 #include "memcheck.h"
+#include "siphash.h"
 
 #ifdef XML_LARGE_SIZE
 #define XML_FMT_INT_MOD "ll"
@@ -233,6 +234,45 @@ START_TEST(test_u0000_char)
 }
 END_TEST
 
+START_TEST(test_siphash_self)
+{
+    if (! sip24_valid())
+        fail("SipHash self-test failed");
+}
+END_TEST
+
+START_TEST(test_siphash_spec)
+{
+    /* https://131002.net/siphash/siphash.pdf (page 19, "Test values") */
+    const char message[] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09"
+            "\x0a\x0b\x0c\x0d\x0e";
+    const size_t len = sizeof(message) - 1;
+    const uint64_t expected = 0xa129ca6149be45e5U;
+    struct siphash state;
+    struct sipkey key;
+    (void)sip_tobin;
+
+    sip_tokey(&key,
+            "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09"
+            "\x0a\x0b\x0c\x0d\x0e\x0f");
+    sip24_init(&state, &key);
+
+    /* Cover spread across calls */
+    sip24_update(&state, message, 4);
+    sip24_update(&state, message + 4, len - 4);
+
+    /* Cover null length */
+    sip24_update(&state, message, 0);
+
+    if (sip24_final(&state) != expected)
+        fail("sip24_final failed spec test\n");
+
+    /* Cover wrapper */
+    if (siphash24(message, len, &key) != expected)
+        fail("siphash24 failed spec test\n");
+}
+END_TEST
+
 START_TEST(test_bom_utf8)
 {
     /* This test is really just making sure we don't core on a UTF-8 BOM. */
@@ -3361,6 +3401,8 @@ make_suite(void)
     tcase_add_checked_fixture(tc_basic, basic_setup, basic_teardown);
     tcase_add_test(tc_basic, test_nul_byte);
     tcase_add_test(tc_basic, test_u0000_char);
+    tcase_add_test(tc_basic, test_siphash_self);
+    tcase_add_test(tc_basic, test_siphash_spec);
     tcase_add_test(tc_basic, test_bom_utf8);
     tcase_add_test(tc_basic, test_bom_utf16_be);
     tcase_add_test(tc_basic, test_bom_utf16_le);