Mercurial > pidgin
changeset 15043:603960420450
[gaim-migrate @ 17827]
Added tests for MD4, MD5, and SHA-1.
Started the basics of making check_libgaim a libgaim "ui"
Other assorted things.
committer: Tailor Script <tailor@pidgin.im>
author | Gary Kramlich <grim@reaperworld.com> |
---|---|
date | Mon, 27 Nov 2006 02:59:33 +0000 |
parents | edd78a0c6698 |
children | 7236b72de90b |
files | libgaim/tests/Makefile.am libgaim/tests/check_libgaim.c libgaim/tests/test_cipher.c libgaim/tests/tests.h |
diffstat | 4 files changed, 305 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/libgaim/tests/Makefile.am Sun Nov 26 23:31:16 2006 +0000 +++ b/libgaim/tests/Makefile.am Mon Nov 27 02:59:33 2006 +0000 @@ -1,7 +1,23 @@ if HAVE_CHECK -TESTS = check_libgaim -check_PROGRAMS = check_libgaim -check_libgaim_SOURCES = check_libgaim.c tests.h test_util.c $(top_builddir)/libgaim/util.h -check_libgaim_CFLAGS = @CHECK_CFLAGS@ $(GLIB_CFLAGS) -check_libgaim_LDADD = @CHECK_LIBS@ $(GLIB_LIBS) $(top_builddir)/libgaim/libgaim.la +TESTS=check_libgaim + +check_PROGRAMS=check_libgaim + +check_libgaim_SOURCES=\ + check_libgaim.c \ + tests.h \ + test_cipher.c \ + test_util.c \ + $(top_builddir)/libgaim/util.h + +check_libgaim_CFLAGS=\ + @CHECK_CFLAGS@ \ + $(GLIB_CFLAGS) \ + -DBUILDDIR=\"$(top_builddir)\" + +check_libgaim_LDADD=\ + @CHECK_LIBS@ \ + $(GLIB_LIBS) \ + $(top_builddir)/libgaim/libgaim.la + endif
--- a/libgaim/tests/check_libgaim.c Sun Nov 26 23:31:16 2006 +0000 +++ b/libgaim/tests/check_libgaim.c Mon Nov 27 02:59:33 2006 +0000 @@ -1,6 +1,46 @@ -#include "tests.h" +#include <glib.h> #include <stdlib.h> +#include "../core.h" +#include "../eventloop.h" + +#include "tests.h" + +/****************************************************************************** + * libgaim goodies + *****************************************************************************/ +static guint +gaim_check_input_add(gint fd, GaimInputCondition condition, + GaimInputFunction function, gpointer data) +{ + /* this is a no-op for now, feel free to implement it */ + return 0; +} + +static GaimEventLoopUiOps eventloop_ui_ops = { + g_timeout_add, + (guint (*)(guint))g_source_remove, + gaim_check_input_add, + (guint (*)(guint))g_source_remove, +}; + +static void +gaim_check_init(void) { + gchar *home_dir; + + gaim_eventloop_set_ui_ops(&eventloop_ui_ops); + + /* build our fake home directory */ + home_dir = g_build_path(BUILDDIR, "libgaim", "tests", "home", NULL); + gaim_util_set_user_dir(home_dir); + g_free(home_dir); + + gaim_core_init("check"); +} + +/****************************************************************************** + * Check meat and potatoes + *****************************************************************************/ Suite* master_suite(void) { @@ -15,9 +55,16 @@ SRunner *sr = srunner_create (master_suite()); srunner_add_suite(sr, util_suite()); + srunner_add_suite(sr, cipher_suite()); + + /* make this a libgaim "ui" */ + gaim_check_init(); srunner_run_all (sr, CK_NORMAL); number_failed = srunner_ntests_failed (sr); srunner_free (sr); + + gaim_core_quit(); + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libgaim/tests/test_cipher.c Mon Nov 27 02:59:33 2006 +0000 @@ -0,0 +1,235 @@ +#include <glib.h> +#include <check.h> +#include <stdlib.h> +#include <string.h> + +#undef HAVE_DBUS + +#include "../cipher.h" +#include "../signal.h" + +/****************************************************************************** + * MD4 Tests + *****************************************************************************/ +#define MD4_TEST(data, digest) { \ + GaimCipher *cipher = NULL; \ + GaimCipherContext *context = NULL; \ + gchar cdigest[33]; \ + gchar *sdigest = NULL; \ + gboolean ret = FALSE; \ + \ + cipher = gaim_ciphers_find_cipher("md4"); \ + context = gaim_cipher_context_new(cipher, NULL); \ + gaim_cipher_context_append(context, (guchar *)(data), strlen((data))); \ + \ + ret = gaim_cipher_context_digest_to_str(context, sizeof(cdigest), cdigest, \ + NULL); \ + \ + fail_unless(ret == TRUE, NULL); \ + \ + fail_unless(strcmp((digest), cdigest) == 0, NULL); \ + \ + gaim_cipher_context_destroy(context); \ +} + +START_TEST(test_md4_empty_string) { + MD4_TEST("", "31d6cfe0d16ae931b73c59d7e0c089c0"); +} +END_TEST + +START_TEST(test_md4_a) { + MD4_TEST("a", "bde52cb31de33e46245e05fbdbd6fb24"); +} +END_TEST + +START_TEST(test_md4_abc) { + MD4_TEST("abc", "a448017aaf21d8525fc10ae87aa6729d"); +} +END_TEST + +START_TEST(test_md4_message_digest) { + MD4_TEST("message digest", "d9130a8164549fe818874806e1c7014b"); +} +END_TEST + +START_TEST(test_md4_a_to_z) { + MD4_TEST("abcdefghijklmnopqrstuvwxyz", + "d79e1c308aa5bbcdeea8ed63df412da9"); +} +END_TEST + +START_TEST(test_md4_A_to_Z_a_to_z_0_to_9) { + MD4_TEST("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "043f8582f241db351ce627e153e7f0e4"); +} +END_TEST + +START_TEST(test_md4_1_to_0_8_times) { + MD4_TEST("123456789012345678901234567890123456789012345678901234567890" + "12345678901234567890", + "e33b4ddc9c38f2199c3e7b164fcc0536"); +} +END_TEST + + +/****************************************************************************** + * MD5 Tests + *****************************************************************************/ +#define MD5_TEST(data, digest) { \ + GaimCipher *cipher = NULL; \ + GaimCipherContext *context = NULL; \ + gchar cdigest[33]; \ + gchar *sdigest = NULL; \ + gboolean ret = FALSE; \ + \ + cipher = gaim_ciphers_find_cipher("md5"); \ + context = gaim_cipher_context_new(cipher, NULL); \ + gaim_cipher_context_append(context, (guchar *)(data), strlen((data))); \ + \ + ret = gaim_cipher_context_digest_to_str(context, sizeof(cdigest), cdigest, \ + NULL); \ + \ + fail_unless(ret == TRUE, NULL); \ + \ + fail_unless(strcmp((digest), cdigest) == 0, NULL); \ + \ + gaim_cipher_context_destroy(context); \ +} + +START_TEST(test_md5_empty_string) { + MD5_TEST("", "d41d8cd98f00b204e9800998ecf8427e"); +} +END_TEST + +START_TEST(test_md5_a) { + MD5_TEST("a", "0cc175b9c0f1b6a831c399e269772661"); +} +END_TEST + +START_TEST(test_md5_abc) { + MD5_TEST("abc", "900150983cd24fb0d6963f7d28e17f72"); +} +END_TEST + +START_TEST(test_md5_message_digest) { + MD5_TEST("message digest", "f96b697d7cb7938d525a2f31aaf161d0"); +} +END_TEST + +START_TEST(test_md5_a_to_z) { + MD5_TEST("abcdefghijklmnopqrstuvwxyz", + "c3fcd3d76192e4007dfb496cca67e13b"); +} +END_TEST + +START_TEST(test_md5_A_to_Z_a_to_z_0_to_9) { + MD5_TEST("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "d174ab98d277d9f5a5611c2c9f419d9f"); +} +END_TEST + +START_TEST(test_md5_1_to_0_8_times) { + MD5_TEST("123456789012345678901234567890123456789012345678901234567890" + "12345678901234567890", + "57edf4a22be3c955ac49da2e2107b67a"); +} +END_TEST + +/****************************************************************************** + * SHA-1 Tests + *****************************************************************************/ +#define SHA1_TEST(data, digest) { \ + GaimCipher *cipher = NULL; \ + GaimCipherContext *context = NULL; \ + gchar cdigest[41]; \ + gchar *sdigest = NULL; \ + gboolean ret = FALSE; \ + \ + cipher = gaim_ciphers_find_cipher("sha1"); \ + context = gaim_cipher_context_new(cipher, NULL); \ + \ + if((data)) { \ + gaim_cipher_context_append(context, (guchar *)(data), strlen((data))); \ + } else { \ + gint j; \ + guchar buff[1000]; \ + \ + memset(buff, 'a', 1000); \ + \ + for(j = 0; j < 1000; j++) \ + gaim_cipher_context_append(context, buff, 1000); \ + } \ + \ + ret = gaim_cipher_context_digest_to_str(context, sizeof(cdigest), cdigest, \ + NULL); \ + \ + fail_unless(ret == TRUE, NULL); \ + \ + fail_unless(strcmp((digest), cdigest) == 0, NULL); \ + \ + gaim_cipher_context_destroy(context); \ +} + +START_TEST(test_sha1_a) { + SHA1_TEST("a", "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8"); +} +END_TEST + +START_TEST(test_sha1_abc) { + SHA1_TEST("abc", "a9993e364706816aba3e25717850c26c9cd0d89d"); +} +END_TEST + +START_TEST(test_sha1_abcd_gibberish) { + SHA1_TEST("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "84983e441c3bd26ebaae4aa1f95129e5e54670f1"); +} +END_TEST + +START_TEST(test_sha1_1000_as_1000_times) { + SHA1_TEST(NULL, "34aa973cd4c4daa4f61eeb2bdbad27316534016f"); +} +END_TEST + +/****************************************************************************** + * Suite + *****************************************************************************/ +Suite * +cipher_suite(void) { + Suite *s = suite_create("Cipher Suite"); + TCase *tc = NULL; + + /* md5 tests */ + tc = tcase_create("MD4"); + tcase_add_test(tc, test_md4_empty_string); + tcase_add_test(tc, test_md4_a); + tcase_add_test(tc, test_md4_abc); + tcase_add_test(tc, test_md4_message_digest); + tcase_add_test(tc, test_md4_a_to_z); + tcase_add_test(tc, test_md4_A_to_Z_a_to_z_0_to_9); + tcase_add_test(tc, test_md4_1_to_0_8_times); + suite_add_tcase(s, tc); + + /* md5 tests */ + tc = tcase_create("MD5"); + tcase_add_test(tc, test_md5_empty_string); + tcase_add_test(tc, test_md5_a); + tcase_add_test(tc, test_md5_abc); + tcase_add_test(tc, test_md5_message_digest); + tcase_add_test(tc, test_md5_a_to_z); + tcase_add_test(tc, test_md5_A_to_Z_a_to_z_0_to_9); + tcase_add_test(tc, test_md5_1_to_0_8_times); + suite_add_tcase(s, tc); + + /* sha1 tests */ + tc = tcase_create("SHA1"); + tcase_add_test(tc, test_sha1_a); + tcase_add_test(tc, test_sha1_abc); + tcase_add_test(tc, test_sha1_abcd_gibberish); + tcase_add_test(tc, test_sha1_1000_as_1000_times); + suite_add_tcase(s, tc); + + return s; +} + +
--- a/libgaim/tests/tests.h Sun Nov 26 23:31:16 2006 +0000 +++ b/libgaim/tests/tests.h Mon Nov 27 02:59:33 2006 +0000 @@ -7,6 +7,7 @@ /* define the test suites here */ /* remember to add the suite to the runner in check_libgaim.c */ Suite * util_suite(void); +Suite * cipher_suite(void); /* helper macros */ #define assert_string_equal(expected, actual) { \