diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile | 13 | ||||
-rw-r--r-- | tests/check.c | 61 | ||||
-rw-r--r-- | tests/check_irc.c | 65 | ||||
-rw-r--r-- | tests/check_md5.c | 56 | ||||
-rw-r--r-- | tests/check_nick.c | 4 | ||||
-rw-r--r-- | tests/check_util.c | 2 | ||||
-rw-r--r-- | tests/testsuite.h | 6 |
7 files changed, 197 insertions, 10 deletions
diff --git a/tests/Makefile b/tests/Makefile index ce8ed690..9dd3df7c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -2,12 +2,17 @@ include ../Makefile.settings LFLAGS +=-lcheck -all: check - ./check +all: check + ./check $(CHECKFLAGS) -main_objs = account.o bitlbee.o conf.o crypting.o help.o ini.o ipc.o irc.o irc_commands.o log.o nick.o query.o root_commands.o set.o storage.o storage_text.o url.o user.o util.o +clean: + rm -f check *.o -check: check.o check_util.o check_nick.o $(addprefix ../, $(main_objs)) ../protocols/protocols.o +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_irc.o + +check: $(test_objs) $(addprefix ../, $(main_objs)) ../protocols/protocols.o ../lib/lib.o @echo '*' Linking $@ @$(CC) $(CFLAGS) -o $@ $^ $(LFLAGS) $(EFLAGS) diff --git a/tests/check.c b/tests/check.c index 5cfb7dfd..42ff067f 100644 --- a/tests/check.c +++ b/tests/check.c @@ -3,9 +3,23 @@ #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; +} + double gettime() { struct timeval time[1]; @@ -20,12 +34,53 @@ Suite *util_suite(void); /* From check_nick.c */ Suite *nick_suite(void); -int main (void) +/* From check_md5.c */ +Suite *md5_suite(void); + +/* From check_irc.c */ +Suite *irc_suite(void); + +int main (int argc, char **argv) { int nf; - SRunner *sr = srunner_create(util_suite()); + 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 ); + log_link( LOGLVL_DEBUG, LOGOUTPUT_CONSOLE ); + 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_run_all (sr, CK_NORMAL); + srunner_add_suite(sr, md5_suite()); + srunner_add_suite(sr, irc_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_irc.c b/tests/check_irc.c new file mode 100644 index 00000000..c1cf05a5 --- /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\n" + "USER a a a a\r\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_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 index 5858e512..714c4fdc 100644 --- a/tests/check_nick.c +++ b/tests/check_nick.c @@ -5,7 +5,7 @@ #include <string.h> #include "irc.h" #include "set.h" -#include "util.h" +#include "misc.h" START_TEST(test_nick_strip) { @@ -22,7 +22,7 @@ START_TEST(test_nick_strip) NULL }; for (i = 0; get[i]; i++) { - char copy[30]; + char copy[60]; strcpy(copy, get[i]); nick_strip(copy); fail_unless (strcmp(copy, expected[i]) == 0, diff --git a/tests/check_util.c b/tests/check_util.c index e771238f..ee365735 100644 --- a/tests/check_util.c +++ b/tests/check_util.c @@ -5,7 +5,7 @@ #include <string.h> #include "irc.h" #include "set.h" -#include "util.h" +#include "misc.h" START_TEST(test_strip_linefeed) { diff --git a/tests/testsuite.h b/tests/testsuite.h new file mode 100644 index 00000000..dacaf8a3 --- /dev/null +++ b/tests/testsuite.h @@ -0,0 +1,6 @@ +#ifndef __BITLBEE_CHECK_H__ +#define __BITLBEE_CHECK_H__ + +gboolean g_io_channel_pair(GIOChannel **ch1, GIOChannel **ch2); + +#endif /* __BITLBEE_CHECK_H__ */ |