/soc/2013/ankitkv/gobjectification: a9e7ea4338a7: Refactored the...

Ankit Vani a at nevitus.org
Sat Jun 15 16:26:03 EDT 2013


Changeset: a9e7ea4338a7be31715ed452e1b3d7fb16b97fc0
Author:	 Ankit Vani <a at nevitus.org>
Date:	 2013-06-16 01:55 +0530
Branch:	 soc.2013.gobjectification
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/a9e7ea4338a7

Description:

Refactored the cipher test suite to use the GObject-based PurpleCipher

diffstat:

 libpurple/tests/test_cipher.c |  155 ++++++++++++++++++-----------------------
 1 files changed, 68 insertions(+), 87 deletions(-)

diffs (truncated from 440 to 300 lines):

diff --git a/libpurple/tests/test_cipher.c b/libpurple/tests/test_cipher.c
--- a/libpurple/tests/test_cipher.c
+++ b/libpurple/tests/test_cipher.c
@@ -7,29 +7,32 @@
 
 #include "tests.h"
 
-#include "../cipher.h"
+#include "../ciphers/des3.h"
+#include "../ciphers/des.h"
+#include "../ciphers/hmac.h"
+#include "../ciphers/md4.h"
+#include "../ciphers/md5.h"
+#include "../ciphers/sha1.h"
+#include "../ciphers/sha256.h"
 
 /******************************************************************************
  * MD4 Tests
  *****************************************************************************/
 #define MD4_TEST(data, digest) { \
 	PurpleCipher *cipher = NULL; \
-	PurpleCipherContext *context = NULL; \
 	gchar cdigest[33]; \
 	gboolean ret = FALSE; \
 	\
-	cipher = purple_ciphers_find_cipher("md4"); \
-	context = purple_cipher_context_new(cipher, NULL); \
-	purple_cipher_context_append(context, (guchar *)(data), strlen((data))); \
+	cipher = purple_md4_cipher_new(); \
+	purple_cipher_append(cipher, (guchar *)(data), strlen((data))); \
 	\
-	ret = purple_cipher_context_digest_to_str(context, sizeof(cdigest), cdigest, \
-	                                        NULL); \
+	ret = purple_cipher_digest_to_str(cipher, cdigest, sizeof(cdigest)); \
 	\
 	fail_unless(ret == TRUE, NULL); \
 	\
 	fail_unless(strcmp((digest), cdigest) == 0, NULL); \
 	\
-	purple_cipher_context_destroy(context); \
+	g_object_unref(cipher); \
 }
 
 START_TEST(test_md4_empty_string) {
@@ -77,22 +80,19 @@ END_TEST
  *****************************************************************************/
 #define MD5_TEST(data, digest) { \
 	PurpleCipher *cipher = NULL; \
-	PurpleCipherContext *context = NULL; \
 	gchar cdigest[33]; \
 	gboolean ret = FALSE; \
 	\
-	cipher = purple_ciphers_find_cipher("md5"); \
-	context = purple_cipher_context_new(cipher, NULL); \
-	purple_cipher_context_append(context, (guchar *)(data), strlen((data))); \
+	cipher = purple_md5_cipher_new(); \
+	purple_cipher_append(cipher, (guchar *)(data), strlen((data))); \
 	\
-	ret = purple_cipher_context_digest_to_str(context, sizeof(cdigest), cdigest, \
-	                                        NULL); \
+	ret = purple_cipher_digest_to_str(cipher, cdigest, sizeof(cdigest)); \
 	\
 	fail_unless(ret == TRUE, NULL); \
 	\
 	fail_unless(strcmp((digest), cdigest) == 0, NULL); \
 	\
-	purple_cipher_context_destroy(context); \
+	g_object_unref(cipher); \
 }
 
 START_TEST(test_md5_empty_string) {
@@ -139,16 +139,14 @@ END_TEST
  *****************************************************************************/
 #define SHA1_TEST(data, digest) { \
 	PurpleCipher *cipher = NULL; \
-	PurpleCipherContext *context = NULL; \
 	gchar cdigest[41]; \
 	gboolean ret = FALSE; \
 	gchar *input = data; \
 	\
-	cipher = purple_ciphers_find_cipher("sha1"); \
-	context = purple_cipher_context_new(cipher, NULL); \
+	cipher = purple_sha1_cipher_new(); \
 	\
 	if (input) { \
-		purple_cipher_context_append(context, (guchar *)input, strlen(input)); \
+		purple_cipher_append(cipher, (guchar *)input, strlen(input)); \
 	} else { \
 		gint j; \
 		guchar buff[1000]; \
@@ -156,17 +154,16 @@ END_TEST
 		memset(buff, 'a', 1000); \
 		\
 		for(j = 0; j < 1000; j++) \
-			purple_cipher_context_append(context, buff, 1000); \
+			purple_cipher_append(cipher, buff, 1000); \
 	} \
 	\
-	ret = purple_cipher_context_digest_to_str(context, sizeof(cdigest), cdigest, \
-	                                        NULL); \
+	ret = purple_cipher_digest_to_str(cipher, cdigest, sizeof(cdigest)); \
 	\
 	fail_unless(ret == TRUE, NULL); \
 	\
 	fail_unless(strcmp((digest), cdigest) == 0, NULL); \
 	\
-	purple_cipher_context_destroy(context); \
+	g_object_unref(cipher); \
 }
 
 START_TEST(test_sha1_empty_string) {
@@ -200,16 +197,14 @@ END_TEST
  *****************************************************************************/
 #define SHA256_TEST(data, digest) { \
 	PurpleCipher *cipher = NULL; \
-	PurpleCipherContext *context = NULL; \
 	gchar cdigest[65]; \
 	gboolean ret = FALSE; \
 	gchar *input = data; \
 	\
-	cipher = purple_ciphers_find_cipher("sha256"); \
-	context = purple_cipher_context_new(cipher, NULL); \
+	cipher = purple_sha256_cipher_new(); \
 	\
 	if (input) { \
-		purple_cipher_context_append(context, (guchar *)input, strlen(input)); \
+		purple_cipher_append(cipher, (guchar *)input, strlen(input)); \
 	} else { \
 		gint j; \
 		guchar buff[1000]; \
@@ -217,17 +212,16 @@ END_TEST
 		memset(buff, 'a', 1000); \
 		\
 		for(j = 0; j < 1000; j++) \
-			purple_cipher_context_append(context, buff, 1000); \
+			purple_cipher_append(cipher, buff, 1000); \
 	} \
 	\
-	ret = purple_cipher_context_digest_to_str(context, sizeof(cdigest), cdigest, \
-	                                        NULL); \
+	ret = purple_cipher_digest_to_str(cipher, cdigest, sizeof(cdigest)); \
 	\
 	fail_unless(ret == TRUE, NULL); \
 	\
 	fail_unless(strcmp((digest), cdigest) == 0, NULL); \
 	\
-	purple_cipher_context_destroy(context); \
+	g_object_unref(cipher); \
 }
 
 START_TEST(test_sha256_empty_string) {
@@ -261,29 +255,24 @@ END_TEST
  *****************************************************************************/
 #define DES_TEST(in, keyz, out, len) { \
 	PurpleCipher *cipher = NULL; \
-	PurpleCipherContext *context = NULL; \
 	guchar answer[len+1]; \
 	gint ret = 0; \
 	guchar decrypt[len+1] = in; \
 	guchar key[8+1] = keyz;\
 	guchar encrypt[len+1] = out;\
-	size_t outlen; \
 	\
-	cipher = purple_ciphers_find_cipher("des"); \
-	context = purple_cipher_context_new(cipher, NULL); \
-	purple_cipher_context_set_key(context, key); \
+	cipher = purple_des_cipher_new(); \
+	purple_cipher_set_key(cipher, key, 8); \
 	\
-	ret = purple_cipher_context_encrypt(context, decrypt, len, answer, &outlen); \
-	fail_unless(ret == 0, NULL); \
-	fail_unless(outlen == (len), NULL); \
+	ret = purple_cipher_encrypt(cipher, decrypt, len, answer, len); \
+	fail_unless(ret != 0, NULL); \
 	fail_unless(memcmp(encrypt, answer, len) == 0, NULL); \
 	\
-	ret = purple_cipher_context_decrypt(context, encrypt, len, answer, &outlen); \
-	fail_unless(ret == 0, NULL); \
-	fail_unless(outlen == (len), NULL); \
+	ret = purple_cipher_decrypt(cipher, encrypt, len, answer, len); \
+	fail_unless(ret != 0, NULL); \
 	fail_unless(memcmp(decrypt, answer, len) == 0, NULL); \
 	\
-	purple_cipher_context_destroy(context); \
+	g_object_unref(cipher); \
 }
 
 START_TEST(test_des_12345678) {
@@ -310,30 +299,25 @@ END_TEST
 
 #define DES3_TEST(in, key, iv, out, len, mode) { \
 	PurpleCipher *cipher = NULL; \
-	PurpleCipherContext *context = NULL; \
 	guchar answer[len+1]; \
 	guchar decrypt[len+1] = in; \
 	guchar encrypt[len+1] = out; \
-	size_t outlen; \
 	gint ret = 0; \
 	\
-	cipher = purple_ciphers_find_cipher("des3"); \
-	context = purple_cipher_context_new(cipher, NULL); \
-	purple_cipher_context_set_key(context, (guchar *)key); \
-	purple_cipher_context_set_batch_mode(context, (mode)); \
-	purple_cipher_context_set_iv(context, (guchar *)iv, 8); \
+	cipher = purple_des3_cipher_new(); \
+	purple_cipher_set_key(cipher, (guchar *)key, 24); \
+	purple_cipher_set_batch_mode(cipher, (mode)); \
+	purple_cipher_set_iv(cipher, (guchar *)iv, 8); \
 	\
-	ret = purple_cipher_context_encrypt(context, decrypt, len, answer, &outlen); \
-	fail_unless(ret == 0, NULL); \
-	fail_unless(outlen == (len), NULL); \
+	ret = purple_cipher_encrypt(cipher, decrypt, len, answer, len); \
+	fail_unless(ret != 0, NULL); \
 	fail_unless(memcmp(encrypt, answer, len) == 0, NULL); \
 	\
-	ret = purple_cipher_context_decrypt(context, encrypt, len, answer, &outlen); \
-	fail_unless(ret == 0, NULL); \
-	fail_unless(outlen == (len), NULL); \
+	ret = purple_cipher_decrypt(cipher, encrypt, len, answer, len); \
+	fail_unless(ret != 0, NULL); \
 	fail_unless(memcmp(decrypt, answer, len) == 0, NULL); \
 	\
-	purple_cipher_context_destroy(context); \
+	g_object_unref(cipher); \
 }
 
 START_TEST(test_des3_ecb_nist1) {
@@ -480,24 +464,21 @@ END_TEST
  *****************************************************************************/
 
 #define HMAC_TEST(data, data_len, key, key_len, type, digest) { \
-	PurpleCipher *cipher = NULL; \
-	PurpleCipherContext *context = NULL; \
+	PurpleCipher *cipher = NULL, *hash = NULL; \
 	gchar cdigest[41]; \
 	gboolean ret = FALSE; \
 	\
-	cipher = purple_ciphers_find_cipher("hmac"); \
-	context = purple_cipher_context_new(cipher, NULL); \
-	purple_cipher_context_set_option(context, "hash", type); \
-	purple_cipher_context_set_key_with_len(context, (guchar *)key, (key_len)); \
+	hash = purple_##type##_cipher_new(); \
+	cipher = purple_hmac_cipher_new(hash); \
+	purple_cipher_set_key(cipher, (guchar *)key, (key_len)); \
 	\
-	purple_cipher_context_append(context, (guchar *)(data), (data_len)); \
-	ret = purple_cipher_context_digest_to_str(context, sizeof(cdigest), cdigest, \
-	                                        NULL); \
+	purple_cipher_append(cipher, (guchar *)(data), (data_len)); \
+	ret = purple_cipher_digest_to_str(cipher, cdigest, sizeof(cdigest)); \
 	\
 	fail_unless(ret == TRUE, NULL); \
 	fail_unless(strcmp((digest), cdigest) == 0, NULL); \
 	\
-	purple_cipher_context_destroy(context); \
+	g_object_unref(cipher); \
 }
 
 /* HMAC MD5 */
@@ -507,7 +488,7 @@ START_TEST(test_hmac_md5_Hi) {
 	          8,
 	          "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
 	          16,
-	          "md5",
+	          md5,
 	          "9294727a3638bb1c13f48ef8158bfc9d");
 }
 END_TEST
@@ -517,7 +498,7 @@ START_TEST(test_hmac_md5_what) {
 	          28,
 	          "Jefe",
 	          4,
-	          "md5",
+	          md5,
 	          "750c783e6ab0b503eaa86e310a5db738");
 }
 END_TEST
@@ -531,7 +512,7 @@ START_TEST(test_hmac_md5_dd) {
 	          50,
 	          "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
 	          16,
-	          "md5",
+	          md5,
 	          "56be34521d144c88dbb8c733f0e8b3f6");
 }
 END_TEST
@@ -547,7 +528,7 @@ START_TEST(test_hmac_md5_cd) {
 	          "\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
 	          "\x15\x16\x17\x18\x19",
 	          25,
-	          "md5",
+	          md5,
 	          "697eaf0aca3a3aea3a75164746ffaa79");
 }
 END_TEST
@@ -557,7 +538,7 @@ START_TEST(test_hmac_md5_truncation) {
 	          20,
 	          "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
 	          16,



More information about the Commits mailing list