aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile23
-rw-r--r--tests/check.c123
-rw-r--r--tests/check_arc.c105
-rw-r--r--tests/check_crypting.c47
-rw-r--r--tests/check_help.c33
-rw-r--r--tests/check_irc.c65
-rw-r--r--tests/check_jabber_sasl.c117
-rw-r--r--tests/check_md5.c56
-rw-r--r--tests/check_nick.c76
-rw-r--r--tests/check_set.c130
-rw-r--r--tests/check_user.c75
-rw-r--r--tests/check_util.c177
-rw-r--r--tests/testsuite.h9
13 files changed, 1036 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100644
index 00000000..ae76fef5
--- /dev/null
+++ b/tests/Makefile
@@ -0,0 +1,23 @@
+-include ../Makefile.settings
+
+LFLAGS +=-lcheck
+
+all: check
+ ./check $(CHECKFLAGS)
+
+clean:
+ rm -f check *.o
+
+distclean: clean
+
+main_objs = account.o bitlbee.o conf.o crypting.o help.o ipc.o irc.o irc_commands.o log.o nick.o query.o root_commands.o set.o storage.o storage_xml.o storage_text.o user.o
+
+test_objs = check.o check_util.o check_nick.o check_md5.o check_arc.o check_irc.o check_help.o check_user.o check_crypting.o check_set.o check_jabber_sasl.o
+
+check: $(test_objs) $(addprefix ../, $(main_objs)) ../protocols/protocols.o ../lib/lib.o
+ @echo '*' Linking $@
+ @$(CC) $(CFLAGS) -o $@ $^ $(LFLAGS) $(EFLAGS)
+
+%.o: %.c
+ @echo '*' Compiling $<
+ @$(CC) -c $(CFLAGS) $< -o $@
diff --git a/tests/check.c b/tests/check.c
new file mode 100644
index 00000000..b3ffb957
--- /dev/null
+++ b/tests/check.c
@@ -0,0 +1,123 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include "bitlbee.h"
+#include "testsuite.h"
+
+global_t global; /* Against global namespace pollution */
+
+gboolean g_io_channel_pair(GIOChannel **ch1, GIOChannel **ch2)
+{
+ int sock[2];
+ if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNIX, sock) < 0) {
+ perror("socketpair");
+ return FALSE;
+ }
+
+ *ch1 = g_io_channel_unix_new(sock[0]);
+ *ch2 = g_io_channel_unix_new(sock[1]);
+ return TRUE;
+}
+
+irc_t *torture_irc(void)
+{
+ irc_t *irc;
+ GIOChannel *ch1, *ch2;
+ if (!g_io_channel_pair(&ch1, &ch2))
+ return NULL;
+ irc = irc_new(g_io_channel_unix_get_fd(ch1));
+ return irc;
+}
+
+double gettime()
+{
+ struct timeval time[1];
+
+ gettimeofday( time, 0 );
+ return( (double) time->tv_sec + (double) time->tv_usec / 1000000 );
+}
+
+/* From check_util.c */
+Suite *util_suite(void);
+
+/* From check_nick.c */
+Suite *nick_suite(void);
+
+/* From check_md5.c */
+Suite *md5_suite(void);
+
+/* From check_arc.c */
+Suite *arc_suite(void);
+
+/* From check_irc.c */
+Suite *irc_suite(void);
+
+/* From check_help.c */
+Suite *help_suite(void);
+
+/* From check_user.c */
+Suite *user_suite(void);
+
+/* From check_crypting.c */
+Suite *crypting_suite(void);
+
+/* From check_set.c */
+Suite *set_suite(void);
+
+/* From check_jabber_sasl.c */
+Suite *jabber_sasl_suite(void);
+
+int main (int argc, char **argv)
+{
+ int nf;
+ SRunner *sr;
+ GOptionContext *pc;
+ gboolean no_fork = FALSE;
+ gboolean verbose = FALSE;
+ GOptionEntry options[] = {
+ {"no-fork", 'n', 0, G_OPTION_ARG_NONE, &no_fork, "Don't fork" },
+ {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
+ { NULL }
+ };
+ int i;
+
+ pc = g_option_context_new("");
+ g_option_context_add_main_entries(pc, options, NULL);
+
+ if(!g_option_context_parse(pc, &argc, &argv, NULL))
+ return 1;
+
+ g_option_context_free(pc);
+
+ log_init();
+
+ if (verbose) {
+ log_link( LOGLVL_ERROR, LOGOUTPUT_CONSOLE );
+#ifdef DEBUG
+ log_link( LOGLVL_DEBUG, LOGOUTPUT_CONSOLE );
+#endif
+ log_link( LOGLVL_INFO, LOGOUTPUT_CONSOLE );
+ log_link( LOGLVL_WARNING, LOGOUTPUT_CONSOLE );
+ }
+
+ global.conf = conf_load( 0, NULL);
+ global.conf->runmode = RUNMODE_DAEMON;
+
+ sr = srunner_create(util_suite());
+ srunner_add_suite(sr, nick_suite());
+ srunner_add_suite(sr, md5_suite());
+ srunner_add_suite(sr, arc_suite());
+ srunner_add_suite(sr, irc_suite());
+ srunner_add_suite(sr, help_suite());
+ srunner_add_suite(sr, user_suite());
+ srunner_add_suite(sr, crypting_suite());
+ srunner_add_suite(sr, set_suite());
+ srunner_add_suite(sr, jabber_sasl_suite());
+ if (no_fork)
+ srunner_set_fork_status(sr, CK_NOFORK);
+ srunner_run_all (sr, verbose?CK_VERBOSE:CK_NORMAL);
+ nf = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
diff --git a/tests/check_arc.c b/tests/check_arc.c
new file mode 100644
index 00000000..9d913dcd
--- /dev/null
+++ b/tests/check_arc.c
@@ -0,0 +1,105 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include <stdio.h>
+#include "arc.h"
+
+char *password = "ArcVier";
+
+char *clear_tests[] =
+{
+ "Wie dit leest is gek :-)",
+ "ItllBeBitlBee",
+ "One more boring password",
+ "Hoi hoi",
+ NULL
+};
+
+static void check_codec(int l)
+{
+ int i;
+
+ for( i = 0; clear_tests[i]; i++ )
+ {
+ tcase_fn_start (clear_tests[i], __FILE__, __LINE__);
+ unsigned char *crypted;
+ char *decrypted;
+ int len;
+
+ len = arc_encode( clear_tests[i], 0, &crypted, password, 12 );
+ len = arc_decode( crypted, len, &decrypted, password );
+
+ fail_if( strcmp( clear_tests[i], decrypted ) != 0,
+ "%s didn't decrypt back properly", clear_tests[i] );
+
+ g_free( crypted );
+ g_free( decrypted );
+ }
+}
+
+struct
+{
+ unsigned char crypted[30];
+ int len;
+ char *decrypted;
+} decrypt_tests[] = {
+ /* One block with padding. */
+ {
+ {
+ 0x3f, 0x79, 0xb0, 0xf5, 0x91, 0x56, 0xd2, 0x1b, 0xd1, 0x4b, 0x67, 0xac,
+ 0xb1, 0x31, 0xc9, 0xdb, 0xf9, 0xaa
+ }, 18, "short pass"
+ },
+
+ /* Two blocks with padding. */
+ {
+ {
+ 0xf9, 0xa6, 0xec, 0x5d, 0xc7, 0x06, 0xb8, 0x6b, 0x63, 0x9f, 0x2d, 0xb5,
+ 0x7d, 0xaa, 0x32, 0xbb, 0xd8, 0x08, 0xfd, 0x81, 0x2e, 0xca, 0xb4, 0xd7,
+ 0x2f, 0x36, 0x9c, 0xac, 0xa0, 0xbc
+ }, 30, "longer password"
+ },
+
+ /* This string is exactly two "blocks" long, to make sure unpadded strings also decrypt
+ properly. */
+ {
+ {
+ 0x95, 0x4d, 0xcf, 0x4d, 0x5e, 0x6c, 0xcf, 0xef, 0xb9, 0x80, 0x00, 0xef,
+ 0x25, 0xe9, 0x17, 0xf6, 0x29, 0x6a, 0x82, 0x79, 0x1c, 0xca, 0x68, 0xb5,
+ 0x4e, 0xd0, 0xc1, 0x41, 0x8e, 0xe6
+ }, 30, "OSCAR is really creepy.."
+ },
+ { "", 0, NULL }
+};
+
+static void check_decod(int l)
+{
+ int i;
+
+ for( i = 0; decrypt_tests[i].len; i++ )
+ {
+ tcase_fn_start (decrypt_tests[i].decrypted, __FILE__, __LINE__);
+ char *decrypted;
+ int len;
+
+ len = arc_decode( decrypt_tests[i].crypted, decrypt_tests[i].len,
+ &decrypted, password );
+
+ fail_if( strcmp( decrypt_tests[i].decrypted, decrypted ) != 0,
+ "`%s' didn't decrypt properly", decrypt_tests[i].decrypted );
+
+ g_free( decrypted );
+ }
+}
+
+Suite *arc_suite (void)
+{
+ Suite *s = suite_create("ArcFour");
+ TCase *tc_core = tcase_create("Core");
+ suite_add_tcase (s, tc_core);
+ tcase_add_test (tc_core, check_codec);
+ tcase_add_test (tc_core, check_decod);
+ return s;
+}
diff --git a/tests/check_crypting.c b/tests/check_crypting.c
new file mode 100644
index 00000000..b8e5e1e0
--- /dev/null
+++ b/tests/check_crypting.c
@@ -0,0 +1,47 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include "bitlbee.h"
+#include "crypting.h"
+#include "testsuite.h"
+
+START_TEST(test_check_pass_valid)
+ fail_unless (checkpass ("foo", "acbd18db4cc2f85cedef654fccc4a4d8") == 0);
+ fail_unless (checkpass ("invalidpass", "acbd18db4cc2f85cedef654fccc4a4d8") == -1);
+
+END_TEST
+
+START_TEST(test_hashpass)
+ fail_unless (strcmp(hashpass("foo"), "acbd18db4cc2f85cedef654fccc4a4d8") == 0);
+END_TEST
+
+START_TEST(test_obfucrypt)
+ char *raw = obfucrypt("some line", "bla");
+ fail_unless(strcmp(raw, "\xd5\xdb\xce\xc7\x8c\xcd\xcb\xda\xc6") == 0);
+END_TEST
+
+START_TEST(test_deobfucrypt)
+ char *raw = deobfucrypt("\xd5\xdb\xce\xc7\x8c\xcd\xcb\xda\xc6", "bla");
+ fail_unless(strcmp(raw, "some line") == 0);
+END_TEST
+
+START_TEST(test_obfucrypt_bidirectional)
+ char *plain = g_strdup("this is a line");
+ char *raw = obfucrypt(plain, "foo");
+ fail_unless(strcmp(plain, deobfucrypt(raw, "foo")) == 0);
+END_TEST
+
+Suite *crypting_suite (void)
+{
+ Suite *s = suite_create("Crypting");
+ TCase *tc_core = tcase_create("Core");
+ suite_add_tcase (s, tc_core);
+ tcase_add_test (tc_core, test_check_pass_valid);
+ tcase_add_test (tc_core, test_hashpass);
+ tcase_add_test (tc_core, test_obfucrypt);
+ tcase_add_test (tc_core, test_deobfucrypt);
+ tcase_add_test (tc_core, test_obfucrypt_bidirectional);
+ return s;
+}
diff --git a/tests/check_help.c b/tests/check_help.c
new file mode 100644
index 00000000..5a2f28d9
--- /dev/null
+++ b/tests/check_help.c
@@ -0,0 +1,33 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include <stdio.h>
+#include "help.h"
+
+START_TEST(test_help_initfree)
+ help_t *h, *r;
+ r = help_init(&h, "/dev/null");
+ fail_if(r == NULL);
+ fail_if(r != h);
+
+ help_free(&h);
+ fail_if(h != NULL);
+END_TEST
+
+START_TEST(test_help_nonexistent)
+ help_t *h, *r;
+ r = help_init(&h, "/dev/null");
+ fail_unless(help_get(&h, "nonexistent") == NULL);
+END_TEST
+
+Suite *help_suite (void)
+{
+ Suite *s = suite_create("Help");
+ TCase *tc_core = tcase_create("Core");
+ suite_add_tcase (s, tc_core);
+ tcase_add_test (tc_core, test_help_initfree);
+ tcase_add_test (tc_core, test_help_nonexistent);
+ return s;
+}
diff --git a/tests/check_irc.c b/tests/check_irc.c
new file mode 100644
index 00000000..66fe0021
--- /dev/null
+++ b/tests/check_irc.c
@@ -0,0 +1,65 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include <stdio.h>
+#include "irc.h"
+#include "testsuite.h"
+
+START_TEST(test_connect)
+ GIOChannel *ch1, *ch2;
+ irc_t *irc;
+ char *raw;
+ fail_unless(g_io_channel_pair(&ch1, &ch2));
+
+ irc = irc_new(g_io_channel_unix_get_fd(ch1));
+
+ irc_free(irc);
+
+ fail_unless(g_io_channel_read_to_end(ch2, &raw, NULL, NULL) == G_IO_STATUS_NORMAL);
+
+ fail_if(strcmp(raw, "") != 0);
+
+ g_free(raw);
+END_TEST
+
+START_TEST(test_login)
+ GIOChannel *ch1, *ch2;
+ irc_t *irc;
+ GError *error = NULL;
+ char *raw;
+ fail_unless(g_io_channel_pair(&ch1, &ch2));
+
+ g_io_channel_set_flags(ch1, G_IO_FLAG_NONBLOCK, NULL);
+ g_io_channel_set_flags(ch2, G_IO_FLAG_NONBLOCK, NULL);
+
+ irc = irc_new(g_io_channel_unix_get_fd(ch1));
+
+ fail_unless(g_io_channel_write_chars(ch2, "NICK bla\r\r\n"
+ "USER a a a a\n", -1, NULL, NULL) == G_IO_STATUS_NORMAL);
+ fail_unless(g_io_channel_flush(ch2, NULL) == G_IO_STATUS_NORMAL);
+
+ g_main_iteration(FALSE);
+ irc_free(irc);
+
+ fail_unless(g_io_channel_read_to_end(ch2, &raw, NULL, NULL) == G_IO_STATUS_NORMAL);
+
+ fail_unless(strstr(raw, "001") != NULL);
+ fail_unless(strstr(raw, "002") != NULL);
+ fail_unless(strstr(raw, "003") != NULL);
+ fail_unless(strstr(raw, "004") != NULL);
+ fail_unless(strstr(raw, "005") != NULL);
+
+ g_free(raw);
+END_TEST
+
+Suite *irc_suite (void)
+{
+ Suite *s = suite_create("IRC");
+ TCase *tc_core = tcase_create("Core");
+ suite_add_tcase (s, tc_core);
+ tcase_add_test (tc_core, test_connect);
+ tcase_add_test (tc_core, test_login);
+ return s;
+}
diff --git a/tests/check_jabber_sasl.c b/tests/check_jabber_sasl.c
new file mode 100644
index 00000000..6bceeb88
--- /dev/null
+++ b/tests/check_jabber_sasl.c
@@ -0,0 +1,117 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include <stdio.h>
+#include "arc.h"
+
+char *sasl_get_part( char *data, char *field );
+
+#define challenge1 "nonce=\"1669585310\",qop=\"auth\",charset=utf-8,algorithm=md5-sess," \
+ "something=\"Not \\\"standardized\\\"\""
+#define challenge2 "realm=\"quadpoint.org\", nonce=\"NPotlQpQf9RNYodOwierkQ==\", " \
+ "qop=\"auth, auth-int\", charset=utf-8, algorithm=md5-sess"
+#define challenge3 ", realm=\"localhost\", nonce=\"LlBV2txnO8RbB5hgs3KgiQ==\", " \
+ "qop=\"auth, auth-int, \", ,\n, charset=utf-8, algorithm=md5-sess,"
+
+struct
+{
+ char *challenge;
+ char *key;
+ char *value;
+} get_part_tests[] = {
+ {
+ challenge1,
+ "nonce",
+ "1669585310"
+ },
+ {
+ challenge1,
+ "charset",
+ "utf-8"
+ },
+ {
+ challenge1,
+ "harset",
+ NULL
+ },
+ {
+ challenge1,
+ "something",
+ "Not \"standardized\""
+ },
+ {
+ challenge1,
+ "something_else",
+ NULL
+ },
+ {
+ challenge2,
+ "realm",
+ "quadpoint.org",
+ },
+ {
+ challenge2,
+ "real",
+ NULL
+ },
+ {
+ challenge2,
+ "qop",
+ "auth, auth-int"
+ },
+ {
+ challenge3,
+ "realm",
+ "localhost"
+ },
+ {
+ challenge3,
+ "qop",
+ "auth, auth-int, "
+ },
+ {
+ challenge3,
+ "charset",
+ "utf-8"
+ },
+ { NULL, NULL, NULL }
+};
+
+static void check_get_part(int l)
+{
+ int i;
+
+ for( i = 0; get_part_tests[i].key; i++ )
+ {
+ tcase_fn_start( get_part_tests[i].key, __FILE__, i );
+ char *res;
+ int len;
+
+ res = sasl_get_part( get_part_tests[i].challenge,
+ get_part_tests[i].key );
+
+ if( get_part_tests[i].value == NULL )
+ fail_if( res != NULL, "Found key %s in %s while it shouldn't be there!",
+ get_part_tests[i].key, get_part_tests[i].challenge );
+ else if( res )
+ fail_unless( strcmp( res, get_part_tests[i].value ) == 0,
+ "Incorrect value for key %s in %s: %s",
+ get_part_tests[i].key, get_part_tests[i].challenge, res );
+ else
+ fail( "Could not find key %s in %s",
+ get_part_tests[i].key, get_part_tests[i].challenge );
+
+ g_free( res );
+ }
+}
+
+Suite *jabber_sasl_suite (void)
+{
+ Suite *s = suite_create("jabber/sasl");
+ TCase *tc_core = tcase_create("Core");
+ suite_add_tcase (s, tc_core);
+ tcase_add_test (tc_core, check_get_part);
+ return s;
+}
diff --git a/tests/check_md5.c b/tests/check_md5.c
new file mode 100644
index 00000000..4b99d300
--- /dev/null
+++ b/tests/check_md5.c
@@ -0,0 +1,56 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include <stdio.h>
+#include "md5.h"
+
+/* From RFC 1321 */
+struct md5_test {
+ const char *str;
+ md5_byte_t expected[16];
+} tests[] = {
+ { "",
+ { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } },
+ { "a",
+ { 0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8, 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 } },
+ { "abc",
+ { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } },
+ { "message digest",
+ { 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d, 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } },
+ { "abcdefghijklmnopqrstuvwxyz",
+ { 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b } },
+ { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
+ { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5, 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f } },
+ { "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
+ { 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55, 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } },
+
+ { NULL },
+};
+
+static void check_sums(int l)
+{
+ int i;
+ for (i = 0; tests[i].str; i++) {
+ md5_byte_t sum[16];
+ tcase_fn_start (tests[i].str, __FILE__, __LINE__);
+ md5_state_t state;
+ int di;
+
+ md5_init(&state);
+ md5_append(&state, (const md5_byte_t *)tests[i].str, strlen(tests[i].str));
+ md5_finish(&state, sum);
+
+ fail_if(memcmp(tests[i].expected, sum, 16) != 0, "%s failed", tests[i].str);
+ }
+}
+
+Suite *md5_suite (void)
+{
+ Suite *s = suite_create("MD5");
+ TCase *tc_core = tcase_create("Core");
+ suite_add_tcase (s, tc_core);
+ tcase_add_test (tc_core, check_sums);
+ return s;
+}
diff --git a/tests/check_nick.c b/tests/check_nick.c
new file mode 100644
index 00000000..6c4267cd
--- /dev/null
+++ b/tests/check_nick.c
@@ -0,0 +1,76 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include "irc.h"
+#include "set.h"
+#include "misc.h"
+
+START_TEST(test_nick_strip)
+{
+ int i;
+ const char *get[] = { "test:", "test", "test\n",
+ "thisisaveryveryveryverylongnick",
+ "thisisave:ryveryveryverylongnick",
+ "t::::est",
+ "test123",
+ "123test",
+ "123",
+ NULL };
+ const char *expected[] = { "test", "test", "test",
+ "thisisaveryveryveryveryl",
+ "thisisaveryveryveryveryl",
+ "test",
+ "test123",
+ "_123test",
+ "_123",
+ NULL };
+
+ for (i = 0; get[i]; i++) {
+ char copy[60];
+ strcpy(copy, get[i]);
+ nick_strip(copy);
+ fail_unless (strcmp(copy, expected[i]) == 0,
+ "(%d) nick_strip broken: %s -> %s (expected: %s)",
+ i, get[i], copy, expected[i]);
+ }
+}
+END_TEST
+
+START_TEST(test_nick_ok_ok)
+{
+ const char *nicks[] = { "foo", "bar123", "bla[", "blie]", "BreEZaH",
+ "\\od^~", "_123", "_123test", NULL };
+ int i;
+
+ for (i = 0; nicks[i]; i++) {
+ fail_unless (nick_ok(nicks[i]) == 1,
+ "nick_ok() failed: %s", nicks[i]);
+ }
+}
+END_TEST
+
+START_TEST(test_nick_ok_notok)
+{
+ const char *nicks[] = { "thisisaveryveryveryveryveryveryverylongnick",
+ "\nillegalchar", "", "nick%", "123test", NULL };
+ int i;
+
+ for (i = 0; nicks[i]; i++) {
+ fail_unless (nick_ok(nicks[i]) == 0,
+ "nick_ok() succeeded for invalid: %s", nicks[i]);
+ }
+}
+END_TEST
+
+Suite *nick_suite (void)
+{
+ Suite *s = suite_create("Nick");
+ TCase *tc_core = tcase_create("Core");
+ suite_add_tcase (s, tc_core);
+ tcase_add_test (tc_core, test_nick_ok_ok);
+ tcase_add_test (tc_core, test_nick_ok_notok);
+ tcase_add_test (tc_core, test_nick_strip);
+ return s;
+}
diff --git a/tests/check_set.c b/tests/check_set.c
new file mode 100644
index 00000000..b1ea973d
--- /dev/null
+++ b/tests/check_set.c
@@ -0,0 +1,130 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include "set.h"
+#include "testsuite.h"
+
+START_TEST(test_set_add)
+ void *data = "data";
+ set_t *s = NULL, *t;
+ t = set_add(&s, "name", "default", NULL, data);
+ fail_unless(s == t);
+ fail_unless(t->data == data);
+ fail_unless(strcmp(t->def, "default") == 0);
+END_TEST
+
+START_TEST(test_set_add_existing)
+ void *data = "data";
+ set_t *s = NULL, *t;
+ t = set_add(&s, "name", "default", NULL, data);
+ t = set_add(&s, "name", "newdefault", NULL, data);
+ fail_unless(s == t);
+ fail_unless(strcmp(t->def, "newdefault") == 0);
+END_TEST
+
+START_TEST(test_set_find_unknown)
+ set_t *s = NULL, *t;
+ fail_unless (set_find(&s, "foo") == NULL);
+END_TEST
+
+START_TEST(test_set_find)
+ void *data = "data";
+ set_t *s = NULL, *t;
+ t = set_add(&s, "name", "default", NULL, data);
+ fail_unless(s == t);
+ fail_unless(set_find(&s, "name") == t);
+END_TEST
+
+START_TEST(test_set_get_str_default)
+ void *data = "data";
+ set_t *s = NULL, *t;
+ t = set_add(&s, "name", "default", NULL, data);
+ fail_unless(s == t);
+ fail_unless(strcmp(set_getstr(&s, "name"), "default") == 0);
+END_TEST
+
+START_TEST(test_set_get_bool_default)
+ void *data = "data";
+ set_t *s = NULL, *t;
+ t = set_add(&s, "name", "true", NULL, data);
+ fail_unless(s == t);
+ fail_unless(set_getbool(&s, "name"));
+END_TEST
+
+START_TEST(test_set_get_bool_integer)
+ void *data = "data";
+ set_t *s = NULL, *t;
+ t = set_add(&s, "name", "3", NULL, data);
+ fail_unless(s == t);
+ fail_unless(set_getbool(&s, "name") == 3);
+END_TEST
+
+START_TEST(test_set_get_bool_unknown)
+ set_t *s = NULL;
+ fail_unless(set_getbool(&s, "name") == 0);
+END_TEST
+
+START_TEST(test_set_get_str_value)
+ void *data = "data";
+ set_t *s = NULL, *t;
+ t = set_add(&s, "name", "default", NULL, data);
+ set_setstr(&s, "name", "foo");
+ fail_unless(strcmp(set_getstr(&s, "name"), "foo") == 0);
+END_TEST
+
+START_TEST(test_set_get_str_unknown)
+ set_t *s = NULL;
+ fail_unless(set_getstr(&s, "name") == NULL);
+END_TEST
+
+START_TEST(test_setint)
+ void *data = "data";
+ set_t *s = NULL, *t;
+ t = set_add(&s, "name", "10", NULL, data);
+ set_setint(&s, "name", 3);
+ fail_unless(set_getint(&s, "name") == 3);
+END_TEST
+
+START_TEST(test_setstr)
+ void *data = "data";
+ set_t *s = NULL, *t;
+ t = set_add(&s, "name", "foo", NULL, data);
+ set_setstr(&s, "name", "bloe");
+ fail_unless(strcmp(set_getstr(&s, "name"), "bloe") == 0);
+END_TEST
+
+START_TEST(test_setstr_implicit)
+ void *data = "data";
+ set_t *s = NULL, *t;
+ set_setstr(&s, "name", "bloe");
+ fail_unless(set_find(&s, "name") != NULL);
+END_TEST
+
+START_TEST(test_set_get_int_unknown)
+ set_t *s = NULL;
+ fail_unless(set_getint(&s, "foo") == 0);
+END_TEST
+
+Suite *set_suite (void)
+{
+ Suite *s = suite_create("Set");
+ TCase *tc_core = tcase_create("Core");
+ suite_add_tcase (s, tc_core);
+ tcase_add_test (tc_core, test_set_add);
+ tcase_add_test (tc_core, test_set_add_existing);
+ tcase_add_test (tc_core, test_set_find_unknown);
+ tcase_add_test (tc_core, test_set_find);
+ tcase_add_test (tc_core, test_set_get_str_default);
+ tcase_add_test (tc_core, test_set_get_str_value);
+ tcase_add_test (tc_core, test_set_get_str_unknown);
+ tcase_add_test (tc_core, test_set_get_bool_default);
+ tcase_add_test (tc_core, test_set_get_bool_integer);
+ tcase_add_test (tc_core, test_set_get_bool_unknown);
+ tcase_add_test (tc_core, test_set_get_int_unknown);
+ tcase_add_test (tc_core, test_setint);
+ tcase_add_test (tc_core, test_setstr);
+ tcase_add_test (tc_core, test_setstr_implicit);
+ return s;
+}
diff --git a/tests/check_user.c b/tests/check_user.c
new file mode 100644
index 00000000..79248049
--- /dev/null
+++ b/tests/check_user.c
@@ -0,0 +1,75 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include "bitlbee.h"
+#include "user.h"
+#include "testsuite.h"
+
+START_TEST(test_user_add)
+ irc_t *irc = torture_irc();
+ user_t *user;
+ user = user_add(irc, "foo");
+ fail_if(user == NULL);
+ fail_if(strcmp(user->nick, "foo") != 0);
+ fail_unless(user_find(irc, "foo") == user);
+END_TEST
+
+START_TEST(test_user_add_exists)
+ irc_t *irc = torture_irc();
+ user_t *user;
+ user = user_add(irc, "foo");
+ fail_if(user == NULL);
+ user = user_add(irc, "foo");
+ fail_unless(user == NULL);
+END_TEST
+
+START_TEST(test_user_add_invalid)
+ irc_t *irc = torture_irc();
+ user_t *user;
+ user = user_add(irc, ":foo");
+ fail_unless(user == NULL);
+END_TEST
+
+START_TEST(test_user_del_invalid)
+ irc_t *irc = torture_irc();
+ fail_unless(user_del(irc, ":foo") == 0);
+END_TEST
+
+START_TEST(test_user_del)
+ irc_t *irc = torture_irc();
+ user_t *user;
+ user = user_add(irc, "foo");
+ fail_unless(user_del(irc, "foo") == 1);
+ fail_unless(user_find(irc, "foo") == NULL);
+END_TEST
+
+START_TEST(test_user_del_nonexistant)
+ irc_t *irc = torture_irc();
+ fail_unless(user_del(irc, "foo") == 0);
+END_TEST
+
+START_TEST(test_user_rename)
+ irc_t *irc = torture_irc();
+ user_t *user;
+ user = user_add(irc, "foo");
+ user_rename(irc, "foo", "bar");
+ fail_unless(user_find(irc, "foo") == NULL);
+ fail_if(user_find(irc, "bar") == NULL);
+END_TEST
+
+Suite *user_suite (void)
+{
+ Suite *s = suite_create("User");
+ TCase *tc_core = tcase_create("Core");
+ suite_add_tcase (s, tc_core);
+ tcase_add_test (tc_core, test_user_add);
+ tcase_add_test (tc_core, test_user_add_invalid);
+ tcase_add_test (tc_core, test_user_add_exists);
+ tcase_add_test (tc_core, test_user_del_invalid);
+ tcase_add_test (tc_core, test_user_del_nonexistant);
+ tcase_add_test (tc_core, test_user_del);
+ tcase_add_test (tc_core, test_user_rename);
+ return s;
+}
diff --git a/tests/check_util.c b/tests/check_util.c
new file mode 100644
index 00000000..b00d645b
--- /dev/null
+++ b/tests/check_util.c
@@ -0,0 +1,177 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include "irc.h"
+#include "set.h"
+#include "misc.h"
+#include "url.h"
+
+START_TEST(test_strip_linefeed)
+{
+ int i;
+ const char *get[] = { "Test", "Test\r", "Test\rX\r", NULL };
+ const char *expected[] = { "Test", "Test", "TestX", NULL };
+
+ for (i = 0; get[i]; i++) {
+ char copy[20];
+ strcpy(copy, get[i]);
+ strip_linefeed(copy);
+ fail_unless (strcmp(copy, expected[i]) == 0,
+ "(%d) strip_linefeed broken: %s -> %s (expected: %s)",
+ i, get[i], copy, expected[i]);
+ }
+}
+END_TEST
+
+START_TEST(test_strip_newlines)
+{
+ int i;
+ const char *get[] = { "Test", "Test\r\n", "Test\nX\n", NULL };
+ const char *expected[] = { "Test", "Test ", "Test X ", NULL };
+
+ for (i = 0; get[i]; i++) {
+ char copy[20], *ret;
+ strcpy(copy, get[i]);
+ ret = strip_newlines(copy);
+ fail_unless (strcmp(copy, expected[i]) == 0,
+ "(%d) strip_newlines broken: %s -> %s (expected: %s)",
+ i, get[i], copy, expected[i]);
+ fail_unless (copy == ret, "Original string not returned");
+ }
+}
+END_TEST
+
+START_TEST(test_set_url_http)
+ url_t url;
+
+ fail_if (0 == url_set(&url, "http://host/"));
+ fail_unless (!strcmp(url.host, "host"));
+ fail_unless (!strcmp(url.file, "/"));
+ fail_unless (!strcmp(url.user, ""));
+ fail_unless (!strcmp(url.pass, ""));
+ fail_unless (url.proto == PROTO_HTTP);
+ fail_unless (url.port == 80);
+END_TEST
+
+START_TEST(test_set_url_https)
+ url_t url;
+
+ fail_if (0 == url_set(&url, "https://ahost/AimeeMann"));
+ fail_unless (!strcmp(url.host, "ahost"));
+ fail_unless (!strcmp(url.file, "/AimeeMann"));
+ fail_unless (!strcmp(url.user, ""));
+ fail_unless (!strcmp(url.pass, ""));
+ fail_unless (url.proto == PROTO_HTTPS);
+ fail_unless (url.port == 443);
+END_TEST
+
+START_TEST(test_set_url_port)
+ url_t url;
+
+ fail_if (0 == url_set(&url, "https://ahost:200/Lost/In/Space"));
+ fail_unless (!strcmp(url.host, "ahost"));
+ fail_unless (!strcmp(url.file, "/Lost/In/Space"));
+ fail_unless (!strcmp(url.user, ""));
+ fail_unless (!strcmp(url.pass, ""));
+ fail_unless (url.proto == PROTO_HTTPS);
+ fail_unless (url.port == 200);
+END_TEST
+
+START_TEST(test_set_url_username)
+ url_t url;
+
+ fail_if (0 == url_set(&url, "socks4://user@ahost/Space"));
+ fail_unless (!strcmp(url.host, "ahost"));
+ fail_unless (!strcmp(url.file, "/Space"));
+ fail_unless (!strcmp(url.user, "user"));
+ fail_unless (!strcmp(url.pass, ""));
+ fail_unless (url.proto == PROTO_SOCKS4);
+ fail_unless (url.port == 1080);
+END_TEST
+
+START_TEST(test_set_url_username_pwd)
+ url_t url;
+
+ fail_if (0 == url_set(&url, "socks5://user:pass@ahost/"));
+ fail_unless (!strcmp(url.host, "ahost"));
+ fail_unless (!strcmp(url.file, "/"));
+ fail_unless (!strcmp(url.user, "user"));
+ fail_unless (!strcmp(url.pass, "pass"));
+ fail_unless (url.proto == PROTO_SOCKS5);
+ fail_unless (url.port == 1080);
+END_TEST
+
+struct
+{
+ char *orig;
+ int line_len;
+ char *wrapped;
+} word_wrap_tests[] = {
+ {
+ "Line-wrapping is not as easy as it seems?",
+ 16,
+ "Line-wrapping is\nnot as easy as\nit seems?"
+ },
+ {
+ "Line-wrapping is not as easy as it seems?",
+ 8,
+ "Line-\nwrapping\nis not\nas easy\nas it\nseems?"
+ },
+ {
+ "Line-wrapping is\nnot as easy as it seems?",
+ 8,
+ "Line-\nwrapping\nis\nnot as\neasy as\nit\nseems?"
+ },
+ {
+ "a aa aaa aaaa aaaaa aaaaaa aaaaaaa aaaaaaaa",
+ 5,
+ "a aa\naaa\naaaa\naaaaa\naaaaa\na\naaaaa\naa\naaaaa\naaa",
+ },
+ {
+ "aaaaaaaa aaaaaaa aaaaaa aaaaa aaaa aaa aa a",
+ 5,
+ "aaaaa\naaa\naaaaa\naa\naaaaa\na\naaaaa\naaaa\naaa\naa a",
+ },
+ {
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ 5,
+ "aaaaa\naaaaa\naaaaa\naaaaa\naaaaa\naaaaa\naaaaa\na",
+ },
+ {
+ NULL
+ }
+};
+
+START_TEST(test_word_wrap)
+ int i;
+
+ for( i = 0; word_wrap_tests[i].orig && *word_wrap_tests[i].orig; i ++ )
+ {
+ char *wrapped = word_wrap( word_wrap_tests[i].orig, word_wrap_tests[i].line_len );
+
+ fail_unless( strcmp( word_wrap_tests[i].wrapped, wrapped ) == 0,
+ "%s (line_len = %d) should wrap to `%s', not to `%s'",
+ word_wrap_tests[i].orig, word_wrap_tests[i].line_len,
+ word_wrap_tests[i].wrapped, wrapped );
+
+ g_free( wrapped );
+ }
+END_TEST
+
+Suite *util_suite (void)
+{
+ Suite *s = suite_create("Util");
+ TCase *tc_core = tcase_create("Core");
+ suite_add_tcase (s, tc_core);
+ tcase_add_test (tc_core, test_strip_linefeed);
+ tcase_add_test (tc_core, test_strip_newlines);
+ tcase_add_test (tc_core, test_set_url_http);
+ tcase_add_test (tc_core, test_set_url_https);
+ tcase_add_test (tc_core, test_set_url_port);
+ tcase_add_test (tc_core, test_set_url_username);
+ tcase_add_test (tc_core, test_set_url_username_pwd);
+ tcase_add_test (tc_core, test_word_wrap);
+ return s;
+}
diff --git a/tests/testsuite.h b/tests/testsuite.h
new file mode 100644
index 00000000..0b169198
--- /dev/null
+++ b/tests/testsuite.h
@@ -0,0 +1,9 @@
+#ifndef __BITLBEE_CHECK_H__
+#define __BITLBEE_CHECK_H__
+
+#include "irc.h"
+
+irc_t *torture_irc(void);
+gboolean g_io_channel_pair(GIOChannel **ch1, GIOChannel **ch2);
+
+#endif /* __BITLBEE_CHECK_H__ */