view plugins/ciphertest.c @ 11719:109ee3bfeac5

[gaim-migrate @ 14010] SF Patch #1333770 from corfe83 "Many times in gaim we use the function g_slist_remove(list,node->data) to remove an element from a GSList. If we already have the pointer to the node we want to delete, it is faster to send it the pointer to the node to delete rather than the data of the node (we can do this by calling g_slist_delete_link(list,node)). This change was made while looking at glib's documentation and the code in glib's gslist.c. This is because as the remove/delete function traverses each node in the list, it doesn't need to spend an extra memory access to retrieve the data for each element in the node it is traversing and then compare, it can simply compare the pointer. In my tests outside of gaim, this makes a big difference if the node you are deleting is at a high index in the list. However, even if you're deleting the first node, it about breaks even. So, I've found each case in gaim where we are calling g_slist_remove, and we already have the pointer to the appropriate node to delete (this is often the case when we're doing a for or while loop on a GSList). I've then replaced it with the appropriate call to g_slist_delete_link. I, however, didn't do this in situations where we are explicitly removing the first element in the list, because in those situations it is an unnecessary change. There should be no difference in behavior, but just in case I've tried running it with valgrind, which reports the same number of memory leaks after my patch as before my patch. Of course, I can't guarantee that my normal behavior on gaim is hitting all the functions I've changed, but in general testing it Works For Me (tm)." As with the last patch, this one may not have a practical performance impact (or maybe it does, I have no idea), but it's not worse for any case. Given two ways of doing things where one is always at least as fast and may be faster under some cases, I like to prefer that faster way. This doesn't make the code any uglier, so I'm applying. committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Sat, 22 Oct 2005 20:48:18 +0000
parents bb0d7b719af2
children cfc808463763
line wrap: on
line source

/*
 * A plugin to test the ciphers that ship with gaim
 *
 * Copyright (C) 2004, Gary Kramlich <amc_grim@users.sf.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifndef GAIM_PLUGINS
#define GAIM_PLUGINS
#endif

#include "internal.h"

#include <glib.h>
#include <string.h>

#include "cipher.h"
#include "debug.h"
#include "plugin.h"
#include "version.h"

struct test {
	const guchar *question;
	const guchar *answer;
};

/**************************************************************************
 * MD5 Stuff
 **************************************************************************/
struct test md5_tests[8] = {
	{							"",	"d41d8cd98f00b204e9800998ecf8427e"},
	{						   "a",	"0cc175b9c0f1b6a831c399e269772661"},
	{						 "abc",	"900150983cd24fb0d6963f7d28e17f72"},
	{			  "message digest",	"f96b697d7cb7938d525a2f31aaf161d0"},
	{ "abcdefghijklmnopqrstuvwxyz",	"c3fcd3d76192e4007dfb496cca67e13b"},
	{ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	  "abcdefghijklmnopqrstuvwxyz"
					  "0123456789",	"d174ab98d277d9f5a5611c2c9f419d9f"},
	{"123456789012345678901234567"
	 "890123456789012345678901234"
	  "56789012345678901234567890",	"57edf4a22be3c955ac49da2e2107b67a"},
	{						  NULL, NULL							  }
};

static void
cipher_test_md5() {
	GaimCipher *cipher;
	GaimCipherContext *context;
	gchar digest[32];
	gboolean ret;
	gint i = 0;

	cipher = gaim_ciphers_find_cipher("md5");
	if(!cipher) {
		gaim_debug_info("cipher-test",
						"could not find md5 cipher, not testing\n");
		return;
	}

	gaim_debug_info("cipher-test", "Running md5 tests\n");

	context = gaim_cipher_context_new(cipher, NULL);

	while(md5_tests[i].answer) {
		gaim_debug_info("cipher-test", "Test %02d:\n", i);
		gaim_debug_info("cipher-test", "Testing '%s'\n", md5_tests[i].question);

		gaim_cipher_context_append(context, md5_tests[i].question,
								   strlen(md5_tests[i].question));

		ret = gaim_cipher_context_digest_to_str(context, sizeof(digest),
												digest, NULL);

		if(!ret) {
			gaim_debug_info("cipher-test", "failed\n");
		} else {
			gaim_debug_info("cipher-test", "\tGot:    %s\n", digest);
			gaim_debug_info("cipher-test", "\tWanted: %s\n",
							md5_tests[i].answer);
		}

		gaim_cipher_context_reset(context, NULL);
		i++;
	}

	gaim_cipher_context_destroy(context);

	gaim_debug_info("cipher-test", "md5 tests completed\n\n");
}

/**************************************************************************
 * SHA-1 stuff
 **************************************************************************/
struct test sha1_tests[5] = {
	{"a", "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8"},
	{"abc", "a9993e364706816aba3e25717850c26c9cd0d89d"} ,
	{"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "84983e441c3bd26ebaae4aa1f95129e5e54670f1"} ,
    {NULL, "34aa973cd4c4daa4f61eeb2bdbad27316534016f"},
	{NULL, NULL}
};

static void
cipher_test_sha1() {
	GaimCipher *cipher;
	GaimCipherContext *context;
	gchar digest[40];
	gint i = 0;
	gboolean ret;

	cipher = gaim_ciphers_find_cipher("sha1");
	if(!cipher) {
		gaim_debug_info("cipher-test",
						"could not find sha1 cipher, not testing\n");
		return;
	}

	gaim_debug_info("cipher-test", "Running sha1 tests\n");

	context = gaim_cipher_context_new(cipher, NULL);

	while(sha1_tests[i].answer) {
		gaim_debug_info("cipher-test", "Test %02d:\n", i);
		gaim_debug_info("cipher-test", "Testing '%s'\n",
						(sha1_tests[i].question != NULL) ?
						sha1_tests[i].question : "'a'x1000, 1000 times");

		if(sha1_tests[i].question) {
			gaim_cipher_context_append(context, sha1_tests[i].question,
									   strlen(sha1_tests[i].question));
		} 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(digest),
												digest, NULL);

		if(!ret) {
			gaim_debug_info("cipher-test", "failed\n");
		} else {
			gaim_debug_info("cipher-test", "\tGot:    %s\n", digest);
			gaim_debug_info("cipher-test", "\tWanted: %s\n",
							sha1_tests[i].answer);
		}

		gaim_cipher_context_reset(context, NULL);
		i++;
	}

	gaim_cipher_context_destroy(context);

	gaim_debug_info("cipher-test", "sha1 tests completed\n\n");
}
/**************************************************************************
 * Plugin stuff
 **************************************************************************/
static gboolean
plugin_load(GaimPlugin *plugin) {
	cipher_test_md5();
	cipher_test_sha1();

	return TRUE;
}

static gboolean
plugin_unload(GaimPlugin *plugin) {
	return TRUE;
}

static GaimPluginInfo info =
{
	GAIM_PLUGIN_MAGIC,
	GAIM_MAJOR_VERSION,
	GAIM_MINOR_VERSION,
	GAIM_PLUGIN_STANDARD,								/**< type           */
	NULL,												/**< ui_requirement */
	0,													/**< flags          */
	NULL,												/**< dependencies   */
	GAIM_PRIORITY_DEFAULT,								/**< priority       */

	"core-cipher-test",									/**< id             */
	N_("Cipher Test"),									/**< name           */
	VERSION,											/**< version        */
														/**  summary        */
	N_("Tests the ciphers that ship with gaim."),
														/**  description    */
	N_("Tests the ciphers that ship with gaim."),
	"Gary Kramlich <amc_grim@users.sf.net>",			/**< author         */
	GAIM_WEBSITE,										/**< homepage       */

	plugin_load,										/**< load           */
	plugin_unload,										/**< unload         */
	NULL,												/**< destroy        */

	NULL,												/**< ui_info        */
	NULL,												/**< extra_info     */
	NULL,
	NULL
};

static void
init_plugin(GaimPlugin *plugin) {
}

GAIM_INIT_PLUGIN(ciper_test, init_plugin, info)