From efa3749d8f5de4234a65c25c3070dbcfbfccfd44 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Sun, 11 Jun 2017 00:03:28 +0200 Subject: [PATCH] siphash: Test implementation --- expat/Makefile.in | 2 +- expat/tests/runtests.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/expat/Makefile.in b/expat/Makefile.in index cf338fe4..6757eea9 100644 --- a/expat/Makefile.in +++ b/expat/Makefile.in @@ -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 diff --git a/expat/tests/runtests.c b/expat/tests/runtests.c index f8475801..5fa093ca 100644 --- a/expat/tests/runtests.c +++ b/expat/tests/runtests.c @@ -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);