/pidgin/main: 6e53efaa5dfd: Add tests for sha256

Gary Kramlich grim at reaperworld.com
Wed Mar 30 19:25:27 EDT 2016


Changeset: 6e53efaa5dfd195ca98a1ac795279afaf90e8c70
Author:	 Gary Kramlich <grim at reaperworld.com>
Date:	 2016-03-09 22:22 -0600
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/6e53efaa5dfd

Description:

Add tests for sha256

diffstat:

 libpurple/tests/Makefile.am       |   6 ++-
 libpurple/tests/test_sha256hash.c |  79 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 1 deletions(-)

diffs (107 lines):

diff --git a/libpurple/tests/Makefile.am b/libpurple/tests/Makefile.am
--- a/libpurple/tests/Makefile.am
+++ b/libpurple/tests/Makefile.am
@@ -8,7 +8,8 @@ COMMON_LIBS=\
 test_programs=\
 	test_md4hash \
 	test_md5hash \
-	test_sha1hash
+	test_sha1hash \
+	test_sha256hash
 
 test_md4hash_SOURCES=test_md4hash.c
 test_md4hash_LDADD=$(COMMON_LIBS)
@@ -19,6 +20,9 @@ test_md5hash_LDADD=$(COMMON_LIBS)
 test_sha1hash_SOURCES=test_sha1hash.c
 test_sha1hash_LDADD=$(COMMON_LIBS)
 
+test_sha256hash_SOURCES=test_sha256hash.c
+test_sha256hash_LDADD=$(COMMON_LIBS)
+
 AM_CPPFLAGS = \
 	-I$(top_srcdir)/libpurple \
 	-I$(top_builddir)/libpurple \
diff --git a/libpurple/tests/test_sha256hash.c b/libpurple/tests/test_sha256hash.c
new file mode 100644
--- /dev/null
+++ b/libpurple/tests/test_sha256hash.c
@@ -0,0 +1,79 @@
+#include <glib.h>
+
+#include <purple.h>
+
+#include "ciphers/sha256hash.h"
+
+static void
+test_sha256hash(gchar *data, gchar *digest) {
+	PurpleHash *hash = NULL;
+	gchar cdigest[65];
+	gboolean ret = FALSE;
+
+	hash = purple_sha256_hash_new();
+
+	if(data) {
+		purple_hash_append(hash, (guchar *)data, strlen(data));
+	} else {
+		gint j;
+		guchar buff[1000];
+
+		memset(buff, 'a', 1000);
+
+		for(j = 0; j < 1000; j++)
+			purple_hash_append(hash, buff, 1000);
+	}
+
+	ret = purple_hash_digest_to_str(hash, cdigest, sizeof(cdigest));
+
+	g_assert(ret);
+	g_assert_cmpstr(digest, ==, cdigest);
+}
+
+static void
+test_sha256hash_empty_string(void) {
+	test_sha256hash("",
+	                "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+}
+
+static void
+test_sha256hash_a(void) {
+	test_sha256hash("a",
+	                "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb");
+}
+
+static void
+test_sha256hash_abc(void) {
+	test_sha256hash("abc",
+	                "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
+}
+
+static void
+test_sha256hash_abcd_gibberish(void) {
+	test_sha256hash("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
+	                "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
+}
+
+static void
+test_sha256hash_1000_as_1000_times(void) {
+	test_sha256hash(NULL,
+	                "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
+}
+
+gint
+main(gint argc, gchar **argv) {
+	g_test_init(&argc, &argv, NULL);
+
+	g_test_add_func("/hash/sha256/empty-string",
+	                test_sha256hash_empty_string);
+	g_test_add_func("/hash/sha256/a",
+	                test_sha256hash_a);
+	g_test_add_func("/hash/sha256/abc",
+	                test_sha256hash_abc);
+	g_test_add_func("/hash/sha256/abcd_gibberish",
+	                test_sha256hash_abcd_gibberish);
+	g_test_add_func("/hash/sha256/1000 a's 1000 times",
+	                test_sha256hash_1000_as_1000_times);
+
+	return g_test_run();
+}



More information about the Commits mailing list