Commit cf9ac1507f62899d92718fdc216689c2fc98e1fe

Guillem Jover 2018-10-06T04:33:02

test: Refactor and extend message digest testing Generate the base test function with a maro, because all of them follow the same pattern. And extend it by covering more of the API.

diff --git a/test/Makefile.am b/test/Makefile.am
index 136debc..9417069 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -7,6 +7,10 @@ AM_CPPFLAGS = \
 
 LDADD = $(top_builddir)/src/libmd.la
 
+check_HEADERS = \
+	test.h \
+	$(nil)
+
 check_PROGRAMS = \
 	md2 \
 	md4 \
diff --git a/test/md2.c b/test/md2.c
index 36ba9e4..3ed7a35 100644
--- a/test/md2.c
+++ b/test/md2.c
@@ -26,24 +26,18 @@
 
 #include <config.h>
 
-#include <assert.h>
 #include <md2.h>
-#include <string.h>
 
-void
-test_md2(const char *digest, const char *string)
-{
-	char result[MD2_DIGEST_STRING_LENGTH];
+#include "test.h"
 
-	assert(strcmp(digest, MD2Data(string, strlen(string), result)) == 0);
-}
+DEF_TEST_DIGEST(MD2, MD2)
 
 int
 main()
 {
-	test_md2("8350e5a3e24c153df2275c9f80692773", "");
-	test_md2("da853b0d3f88d99b30283a69e6ded6bb", "abc");
-	test_md2("2fe3cb9e21922819e79a2781af74e36d", "12345");
+	test_MD2("8350e5a3e24c153df2275c9f80692773", "");
+	test_MD2("da853b0d3f88d99b30283a69e6ded6bb", "abc");
+	test_MD2("2fe3cb9e21922819e79a2781af74e36d", "12345");
 
 	return 0;
 }
diff --git a/test/md4.c b/test/md4.c
index e78077e..f3b1fe3 100644
--- a/test/md4.c
+++ b/test/md4.c
@@ -26,24 +26,18 @@
 
 #include <config.h>
 
-#include <assert.h>
 #include <md4.h>
-#include <string.h>
 
-void
-test_md4(const char *digest, const char *string)
-{
-	char result[MD4_DIGEST_STRING_LENGTH];
+#include "test.h"
 
-	assert(strcmp(digest, MD4Data(string, strlen(string), result)) == 0);
-}
+DEF_TEST_DIGEST(MD4, MD4)
 
 int
 main()
 {
-	test_md4("31d6cfe0d16ae931b73c59d7e0c089c0", "");
-	test_md4("a448017aaf21d8525fc10ae87aa6729d", "abc");
-	test_md4("23580e2a459f7ea40f9efa148b63cafb", "12345");
+	test_MD4("31d6cfe0d16ae931b73c59d7e0c089c0", "");
+	test_MD4("a448017aaf21d8525fc10ae87aa6729d", "abc");
+	test_MD4("23580e2a459f7ea40f9efa148b63cafb", "12345");
 
 	return 0;
 }
diff --git a/test/md5.c b/test/md5.c
index 5dac6e1..080d285 100644
--- a/test/md5.c
+++ b/test/md5.c
@@ -30,20 +30,16 @@
 #include <md5.h>
 #include <string.h>
 
-void
-test_md5(const char *digest, const char *string)
-{
-	char result[MD5_DIGEST_STRING_LENGTH];
+#include "test.h"
 
-	assert(strcmp(digest, MD5Data(string, strlen(string), result)) == 0);
-}
+DEF_TEST_DIGEST(MD5, MD5)
 
 int
 main()
 {
-	test_md5("d41d8cd98f00b204e9800998ecf8427e", "");
-	test_md5("900150983cd24fb0d6963f7d28e17f72", "abc");
-	test_md5("827ccb0eea8a706c4c34a16891f84e7b", "12345");
+	test_MD5("d41d8cd98f00b204e9800998ecf8427e", "");
+	test_MD5("900150983cd24fb0d6963f7d28e17f72", "abc");
+	test_MD5("827ccb0eea8a706c4c34a16891f84e7b", "12345");
 
 	return 0;
 }
diff --git a/test/rmd160.c b/test/rmd160.c
index b4dcd54..cd27800 100644
--- a/test/rmd160.c
+++ b/test/rmd160.c
@@ -26,24 +26,18 @@
 
 #include <config.h>
 
-#include <assert.h>
 #include <rmd160.h>
-#include <string.h>
 
-void
-test_rmd160(const char *digest, const char *string)
-{
-	char result[RMD160_DIGEST_STRING_LENGTH];
+#include "test.h"
 
-	assert(strcmp(digest, RMD160Data(string, strlen(string), result)) == 0);
-}
+DEF_TEST_DIGEST(RMD160, RMD160)
 
 int
 main()
 {
-	test_rmd160("9c1185a5c5e9fc54612808977ee8f548b2258d31", "");
-	test_rmd160("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc", "abc");
-	test_rmd160("e9cbd2ea8015a084ce9cf83a3c65b51f8fa10a39", "12345");
+	test_RMD160("9c1185a5c5e9fc54612808977ee8f548b2258d31", "");
+	test_RMD160("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc", "abc");
+	test_RMD160("e9cbd2ea8015a084ce9cf83a3c65b51f8fa10a39", "12345");
 
 	return 0;
 }
diff --git a/test/sha1.c b/test/sha1.c
index 4cda990..7145667 100644
--- a/test/sha1.c
+++ b/test/sha1.c
@@ -26,24 +26,18 @@
 
 #include <config.h>
 
-#include <assert.h>
 #include <sha1.h>
-#include <string.h>
 
-void
-test_sha1(const char *digest, const char *string)
-{
-	char result[SHA1_DIGEST_STRING_LENGTH];
+#include "test.h"
 
-	assert(strcmp(digest, SHA1Data(string, strlen(string), result)) == 0);
-}
+DEF_TEST_DIGEST(SHA1, SHA1)
 
 int
 main()
 {
-	test_sha1("da39a3ee5e6b4b0d3255bfef95601890afd80709", "");
-	test_sha1("a9993e364706816aba3e25717850c26c9cd0d89d", "abc");
-	test_sha1("8cb2237d0679ca88db6464eac60da96345513964", "12345");
+	test_SHA1("da39a3ee5e6b4b0d3255bfef95601890afd80709", "");
+	test_SHA1("a9993e364706816aba3e25717850c26c9cd0d89d", "abc");
+	test_SHA1("8cb2237d0679ca88db6464eac60da96345513964", "12345");
 
 	return 0;
 }
diff --git a/test/sha2.c b/test/sha2.c
index 0cd274e..c8dd346 100644
--- a/test/sha2.c
+++ b/test/sha2.c
@@ -26,56 +26,36 @@
 
 #include <config.h>
 
-#include <assert.h>
 #include <sha2.h>
-#include <string.h>
 
-void
-test_sha256(const char *digest, const char *string)
-{
-	char result[SHA256_DIGEST_STRING_LENGTH];
-
-	assert(strcmp(digest, SHA256Data(string, strlen(string), result)) == 0);
-}
-
-void
-test_sha384(const char *digest, const char *string)
-{
-	char result[SHA384_DIGEST_STRING_LENGTH];
+#include "test.h"
 
-	assert(strcmp(digest, SHA384Data(string, strlen(string), result)) == 0);
-}
-
-void
-test_sha512(const char *digest, const char *string)
-{
-	char result[SHA512_DIGEST_STRING_LENGTH];
-
-	assert(strcmp(digest, SHA512Data(string, strlen(string), result)) == 0);
-}
+DEF_TEST_DIGEST(SHA256, SHA2)
+DEF_TEST_DIGEST(SHA384, SHA2)
+DEF_TEST_DIGEST(SHA512, SHA2)
 
 int
 main()
 {
-	test_sha256("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
+	test_SHA256("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
 	            "");
-	test_sha256("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
+	test_SHA256("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
 	            "abc");
-	test_sha256("5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5",
+	test_SHA256("5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5",
 	            "12345");
 
-	test_sha384("38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b",
+	test_SHA384("38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b",
 	            "");
-	test_sha384("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
+	test_SHA384("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
 	            "abc");
-	test_sha384("0fa76955abfa9dafd83facca8343a92aa09497f98101086611b0bfa95dbc0dcc661d62e9568a5a032ba81960f3e55d4a",
+	test_SHA384("0fa76955abfa9dafd83facca8343a92aa09497f98101086611b0bfa95dbc0dcc661d62e9568a5a032ba81960f3e55d4a",
 	            "12345");
 
-	test_sha512("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
+	test_SHA512("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
 	            "");
-	test_sha512("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
+	test_SHA512("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
 	            "abc");
-	test_sha512("3627909a29c31381a071ec27f7c9ca97726182aed29a7ddd2e54353322cfb30abb9e3a6df2ac2c20fe23436311d678564d0c8d305930575f60e2d3d048184d79",
+	test_SHA512("3627909a29c31381a071ec27f7c9ca97726182aed29a7ddd2e54353322cfb30abb9e3a6df2ac2c20fe23436311d678564d0c8d305930575f60e2d3d048184d79",
 	            "12345");
 
 	return 0;
diff --git a/test/test.h b/test/test.h
new file mode 100644
index 0000000..fb3780d
--- /dev/null
+++ b/test/test.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright © 2018 Guillem Jover <guillem@hadrons.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <assert.h>
+#include <string.h>
+
+static int
+hexchar2bin(int c)
+{
+	if (c >= '0' && c <= '9')
+		return c - '0';
+	else if (c >= 'a' && c <= 'f')
+		return c - 'a' + 10;
+	else if (c >= 'A' && c <= 'F')
+		return c - 'A' + 10;
+	assert(!"invalid hexadecimal input");
+}
+
+static void
+hex2bin(uint8_t *bin, const char *str, size_t bin_len)
+{
+	int i;
+
+	for (i = 0; i < bin_len; i++)
+		bin[i] = hexchar2bin(str[i * 2]) << 4 |
+			 hexchar2bin(str[i * 2 + 1]);
+}
+
+#define DEF_TEST_DIGEST(name, type) \
+void \
+test_##name(const char *hash_str_ref, const char *data) \
+{ \
+	uint8_t hash_bin_ref[name##_DIGEST_LENGTH]; \
+	uint8_t hash_bin_got[name##_DIGEST_LENGTH]; \
+	char hash_str_got[name##_DIGEST_STRING_LENGTH]; \
+	type##_CTX ctx; \
+\
+	hex2bin(hash_bin_ref, hash_str_ref, name##_DIGEST_LENGTH); \
+\
+	name##Data(data, strlen(data), hash_str_got); \
+	assert(strcmp(hash_str_ref, hash_str_got) == 0); \
+\
+	name##Init(&ctx); \
+	name##Update(&ctx, data, strlen(data)); \
+	name##End(&ctx, hash_str_got); \
+	assert(strcmp(hash_str_ref, hash_str_got) == 0); \
+\
+	name##Init(&ctx); \
+	name##Update(&ctx, data, strlen(data)); \
+	name##Final(hash_bin_got, &ctx); \
+	assert(memcmp(hash_bin_ref, hash_bin_got, sizeof(hash_bin_ref)) == 0); \
+}