From f6fa4581b0a0df9670b83dbea62c123b651a60bf Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Mon, 3 Aug 2015 02:13:35 +0200 Subject: sha1: cleanup & separate unit test --- libs/ardour/sha1.c | 64 ++----------------------------------------- libs/ardour/test/sha1_test.cc | 51 ++++++++++++++++++++++++++++++++++ libs/ardour/test/sha1_test.h | 16 +++++++++++ libs/ardour/wscript | 2 ++ 4 files changed, 72 insertions(+), 61 deletions(-) create mode 100644 libs/ardour/test/sha1_test.cc create mode 100644 libs/ardour/test/sha1_test.h diff --git a/libs/ardour/sha1.c b/libs/ardour/sha1.c index 5398ad58ae..3011abc9ae 100644 --- a/libs/ardour/sha1.c +++ b/libs/ardour/sha1.c @@ -19,8 +19,6 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -// gcc -Wall -DSELFTEST_SHA1 -o /tmp/sha1test libs/ardour/sha1.c && /tmp/sha1test - #ifndef EXPORT_SHA #define EXPORT_SHA static #endif @@ -30,7 +28,6 @@ #include #include - #ifdef __BIG_ENDIAN__ # define SHA_BIG_ENDIAN #elif defined _BIG_ENDIAN @@ -46,17 +43,11 @@ # endif #endif - -#define HASH_LENGTH 20 -#define BLOCK_LENGTH 64 - typedef struct { - uint32_t buffer[BLOCK_LENGTH/4]; - uint32_t state[HASH_LENGTH/4]; + uint32_t buffer[16]; + uint32_t state[5]; uint32_t byteCount; uint8_t bufferOffset; - uint8_t keyBuffer[BLOCK_LENGTH]; - uint8_t innerHash[HASH_LENGTH]; } Sha1Digest; @@ -111,7 +102,7 @@ static void sha1_addUncounted (Sha1Digest *s, const uint8_t data) { b[s->bufferOffset ^ 3] = data; #endif s->bufferOffset++; - if (s->bufferOffset == BLOCK_LENGTH) { + if (s->bufferOffset == 64) { sha1_hashBlock (s); s->bufferOffset = 0; } @@ -182,52 +173,3 @@ EXPORT_SHA void sha1_result_hash (Sha1Digest *s, char *rv) { sprintf (&rv[2*i], "%02x", hash[i]); } } - - -/*** self-test ***/ -#ifdef SELFTEST_SHA1 -void printHash (Sha1Digest *s) { - char hash[41]; - sha1_result_hash (s, hash); - printf ("%s\n", hash); -} - -int main (int argc, char **argv) { - uint32_t a; - Sha1Digest s; - - // SHA tests - printf ("Test: FIPS 180-2 C.1 and RFC3174 7.3 TEST1\n"); - printf ("Expect:a9993e364706816aba3e25717850c26c9cd0d89d\n"); - printf ("Result:"); - sha1_init (&s); - sha1_write (&s, "abc", 3); - printHash (&s); - printf ("\n\n"); - - printf ("Test: FIPS 180-2 C.2 and RFC3174 7.3 TEST2\n"); - printf ("Expect:84983e441c3bd26ebaae4aa1f95129e5e54670f1\n"); - printf ("Result:"); - sha1_init (&s); - sha1_write (&s, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56); - printHash (&s); - printf ("\n\n"); - - printf ("Test: RFC3174 7.3 TEST4\n"); - printf ("Expect:dea356a2cddd90c7a7ecedc5ebb563934f460452\n"); - printf ("Result:"); - sha1_init (&s); - for (a = 0; a < 80; ++a) sha1_write (&s, "01234567", 8); - printHash (&s); - printf ("\n\n"); - - printf ("Test: FIPS 180-2 C.3 and RFC3174 7.3 TEST3\n"); - printf ("Expect:34aa973cd4c4daa4f61eeb2bdbad27316534016f\n"); - printf ("Result:"); - sha1_init (&s); - for (a = 0; a < 1000000; ++a) sha1_writebyte (&s, 'a'); - printHash (&s); - - return 0; -} -#endif /* self-test */ diff --git a/libs/ardour/test/sha1_test.cc b/libs/ardour/test/sha1_test.cc new file mode 100644 index 0000000000..e2b271ccb0 --- /dev/null +++ b/libs/ardour/test/sha1_test.cc @@ -0,0 +1,51 @@ +#include +#include +#include "sha1.c" +#include "sha1_test.h" + +CPPUNIT_TEST_SUITE_REGISTRATION (Sha1Test); + +using namespace std; + +void +Sha1Test::basicTest () +{ + uint32_t a; + char hash[41]; + Sha1Digest s; + + sha1_init (&s); + sha1_write (&s, (const uint8_t *) "abc", 3); + sha1_result_hash (&s, hash); + printf ("Expect:a9993e364706816aba3e25717850c26c9cd0d89d\n"); + printf ("Result:%s\n", hash); + CPPUNIT_ASSERT_MESSAGE ("Sha1: FIPS 180-2 C.1 and RFC3174 7.3 TEST1", + !strcmp ("a9993e364706816aba3e25717850c26c9cd0d89d", hash)); + + + sha1_init (&s); + sha1_write (&s, (const uint8_t *) "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56); + sha1_result_hash (&s, hash); + printf ("Expect:84983e441c3bd26ebaae4aa1f95129e5e54670f1\n"); + printf ("Result:%s\n", hash); + CPPUNIT_ASSERT_MESSAGE ("Sha1: FIPS 180-2 C.2 and RFC3174 7.3 TEST2", + !strcmp ("84983e441c3bd26ebaae4aa1f95129e5e54670f1", hash)); + + + sha1_init (&s); + for (a = 0; a < 80; ++a) sha1_write (&s, (const uint8_t *) "01234567", 8); + sha1_result_hash (&s, hash); + printf ("Expect:dea356a2cddd90c7a7ecedc5ebb563934f460452\n"); + printf ("Result:%s\n", hash); + CPPUNIT_ASSERT_MESSAGE ("Sha1: RFC3174 7.3 TEST4", + !strcmp ("dea356a2cddd90c7a7ecedc5ebb563934f460452", hash)); + + + sha1_init (&s); + for (a = 0; a < 1000000; ++a) sha1_writebyte (&s, 'a'); + sha1_result_hash (&s, hash); + printf ("Expect:34aa973cd4c4daa4f61eeb2bdbad27316534016f\n"); + printf ("Result:%s\n", hash); + CPPUNIT_ASSERT_MESSAGE ("Sha1: FIPS 180-2 C.3 and RFC3174 7.3 TEST3", + !strcmp ("34aa973cd4c4daa4f61eeb2bdbad27316534016f", hash)); +} diff --git a/libs/ardour/test/sha1_test.h b/libs/ardour/test/sha1_test.h new file mode 100644 index 0000000000..d2fe6d5d6a --- /dev/null +++ b/libs/ardour/test/sha1_test.h @@ -0,0 +1,16 @@ +#include +#include +#include + +class Sha1Test : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE (Sha1Test); + CPPUNIT_TEST (basicTest); + CPPUNIT_TEST_SUITE_END (); + +public: + void setUp () {} + void tearDown () {} + + void basicTest (); +}; diff --git a/libs/ardour/wscript b/libs/ardour/wscript index ff2d6e40fd..e5082bf5a9 100644 --- a/libs/ardour/wscript +++ b/libs/ardour/wscript @@ -488,6 +488,7 @@ def build(bld): create_ardour_test_program(bld, obj.includes, 'region_naming', 'test_region_naming', ['test/region_naming_test.cc']) create_ardour_test_program(bld, obj.includes, 'control_surface', 'test_control_surfaces', ['test/control_surfaces_test.cc']) create_ardour_test_program(bld, obj.includes, 'mtdm_test', 'test_mtdm', ['test/mtdm_test.cc']) + create_ardour_test_program(bld, obj.includes, 'sha1_test', 'test_sha1', ['test/sha1_test.cc']) create_ardour_test_program(bld, obj.includes, 'session_test', 'test_session', ['test/session_test.cc']) test_sources = ''' @@ -507,6 +508,7 @@ def build(bld): test/region_naming_test.cc test/control_surfaces_test.cc test/mtdm_test.cc + test/sha1_test.cc test/session_test.cc '''.split() -- cgit v1.2.3