aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.bzrignore1
-rw-r--r--Makefile3
-rw-r--r--irc_commands.c2
-rw-r--r--nick.c4
-rw-r--r--tests/Makefile16
-rw-r--r--tests/check.c32
-rw-r--r--tests/check_nick.c70
-rw-r--r--tests/check_util.c53
8 files changed, 178 insertions, 3 deletions
diff --git a/.bzrignore b/.bzrignore
index 802cb1a0..cddd9fb1 100644
--- a/.bzrignore
+++ b/.bzrignore
@@ -11,3 +11,4 @@ decode
encode
bitlbee.pc
.gdb_history
+tests/check
diff --git a/Makefile b/Makefile
index 51ffc83f..6f1d2d4a 100644
--- a/Makefile
+++ b/Makefile
@@ -44,6 +44,9 @@ distclean: clean $(subdirs)
rm -f Makefile.settings config.h
find . -name 'DEADJOE' -o -name '*.orig' -o -name '*.rej' -o -name '*~' -exec rm -f {} \;
+check:
+ $(MAKE) -C tests
+
install-doc:
$(MAKE) -C doc install
diff --git a/irc_commands.c b/irc_commands.c
index f410bb52..3bb24fdb 100644
--- a/irc_commands.c
+++ b/irc_commands.c
@@ -320,7 +320,7 @@ static void irc_cmd_userhost( irc_t *irc, char **cmd )
static void irc_cmd_ison( irc_t *irc, char **cmd )
{
user_t *u;
- char buff[IRC_MAX_LINE], *s;
+ char buff[IRC_MAX_LINE];
int lenleft, i;
buff[0] = '\0';
diff --git a/nick.c b/nick.c
index 771d2288..68dd9802 100644
--- a/nick.c
+++ b/nick.c
@@ -162,7 +162,7 @@ void nick_strip( char * nick )
{
int i, j;
- for( i = j = 0; nick[i] && i < MAX_NICK_LENGTH; i++ )
+ for( i = j = 0; nick[i] && j < MAX_NICK_LENGTH; i++ )
{
if( strchr( nick_lc_chars, nick[i] ) ||
strchr( nick_uc_chars, nick[i] ) )
@@ -171,7 +171,7 @@ void nick_strip( char * nick )
j++;
}
}
- while( j < MAX_NICK_LENGTH )
+ while( j <= MAX_NICK_LENGTH )
nick[j++] = '\0';
}
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100644
index 00000000..ce8ed690
--- /dev/null
+++ b/tests/Makefile
@@ -0,0 +1,16 @@
+include ../Makefile.settings
+
+LFLAGS +=-lcheck
+
+all: check
+ ./check
+
+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
+
+check: check.o check_util.o check_nick.o $(addprefix ../, $(main_objs)) ../protocols/protocols.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..5cfb7dfd
--- /dev/null
+++ b/tests/check.c
@@ -0,0 +1,32 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include "bitlbee.h"
+
+global_t global; /* Against global namespace pollution */
+
+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);
+
+int main (void)
+{
+ int nf;
+ SRunner *sr = srunner_create(util_suite());
+ srunner_add_suite(sr, nick_suite());
+ srunner_run_all (sr, CK_NORMAL);
+ nf = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
diff --git a/tests/check_nick.c b/tests/check_nick.c
new file mode 100644
index 00000000..5858e512
--- /dev/null
+++ b/tests/check_nick.c
@@ -0,0 +1,70 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include "irc.h"
+#include "set.h"
+#include "util.h"
+
+START_TEST(test_nick_strip)
+{
+ int i;
+ const char *get[] = { "test:", "test", "test\n",
+ "thisisaveryveryveryverylongnick",
+ "thisisave:ryveryveryverylongnick",
+ "t::::est",
+ NULL };
+ const char *expected[] = { "test", "test", "test",
+ "thisisaveryveryveryveryl",
+ "thisisaveryveryveryveryl",
+ "test",
+ NULL };
+
+ for (i = 0; get[i]; i++) {
+ char copy[30];
+ 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", "bar", "bla[", "blie]",
+ "BreEZaH", "\\od^~", 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%", 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_util.c b/tests/check_util.c
new file mode 100644
index 00000000..e771238f
--- /dev/null
+++ b/tests/check_util.c
@@ -0,0 +1,53 @@
+#include <stdlib.h>
+#include <glib.h>
+#include <gmodule.h>
+#include <check.h>
+#include <string.h>
+#include "irc.h"
+#include "set.h"
+#include "util.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
+
+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);
+ return s;
+}