From 331db6f5656bb9bacaab14181bf55fdfcbe36135 Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Sun, 18 Oct 2015 03:05:38 +0300 Subject: [PATCH] tests: added test vectors for MD5 --- .gitignore | 1 + Makefile.am | 7 +++-- tests/test_md5.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 tests/test_md5.c diff --git a/.gitignore b/.gitignore index 0b1ef2d..bf9d891 100644 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,7 @@ tests/test_base64 tests/test_ctx tests/test_hash tests/test_jid +tests/test_md5 tests/test_rand tests/test_sasl tests/test_scram diff --git a/Makefile.am b/Makefile.am index eaaf646..a16f6e3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -65,8 +65,8 @@ examples_uuid_LDADD = $(STROPHE_LIBS) ## Tests -TESTS = tests/check_parser tests/test_sha1 tests/test_rand tests/test_scram \ - tests/test_base64 tests/test_snprintf +TESTS = tests/check_parser tests/test_sha1 tests/test_md5 tests/test_rand \ + tests/test_scram tests/test_base64 tests/test_snprintf check_PROGRAMS = $(TESTS) tests_check_parser_SOURCES = tests/check_parser.c tests/test.h @@ -89,5 +89,8 @@ tests_test_scram_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src tests_test_sha1_SOURCES = tests/test_sha1.c src/sha1.c tests_test_sha1_CFLAGS = -I$(top_srcdir)/src +tests_test_md5_SOURCES = tests/test_md5.c tests/test.c src/md5.c +tests_test_md5_CFLAGS = -I$(top_srcdir)/src + tests_test_snprintf_SOURCES = tests/test_snprintf.c tests_test_snprintf_CFLAGS = -I$(top_srcdir)/src diff --git a/tests/test_md5.c b/tests/test_md5.c new file mode 100644 index 0000000..2a59038 --- /dev/null +++ b/tests/test_md5.c @@ -0,0 +1,77 @@ +/* test_md5.c + * strophe XMPP client library -- test vectors for MD5 + * + * Copyright (C) 2015 Dmitry Podgorny + * + * This software is provided AS-IS with no warranty, either express + * or implied. + * + * This program is dual licensed under the MIT and GPLv3 licenses. + */ + +/* gcc -o test_md5 -I./src tests/test_md5.c tests/test.c src/md5.c */ + +#include +#include + +#include "test.h" +#include "md5.h" + +/* + * Test vectors for MD5 (RFC1321). + */ +static const struct { + const char *data; + const char *md5; +} tests[] = { + { + .data = "", + .md5 = "d41d8cd98f00b204e9800998ecf8427e", + }, + { + .data = "a", + .md5 = "0cc175b9c0f1b6a831c399e269772661", + }, + { + .data = "abc", + .md5 = "900150983cd24fb0d6963f7d28e17f72", + }, + { + .data = "message digest", + .md5 = "f96b697d7cb7938d525a2f31aaf161d0", + }, + { + .data = "abcdefghijklmnopqrstuvwxyz", + .md5 = "c3fcd3d76192e4007dfb496cca67e13b", + }, + { + .data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde" + "fghijklmnopqrstuvwxyz0123456789", + .md5 = "d174ab98d277d9f5a5611c2c9f419d9f", + }, + { + .data = "1234567890123456789012345678901" + "2345678901234567890123456789012" + "345678901234567890", + .md5 = "57edf4a22be3c955ac49da2e2107b67a", + }, +}; + +int main(int argc, char **argv) +{ + struct MD5Context ctx; + unsigned char digest[16]; + size_t i; + + printf("MD5 tests.\n"); + + for (i = 0; i < ARRAY_SIZE(tests); ++i) { + printf("Test #%zu: ", i + 1); + MD5Init(&ctx); + MD5Update(&ctx, (unsigned char *)tests[i].data, strlen(tests[i].data)); + MD5Final(digest, &ctx); + COMPARE(tests[i].md5, test_bin_to_hex(digest, sizeof(digest))); + printf("ok\n"); + } + return 0; +}