Merge fix for logging of mode changes by charly.
[jelmer/ctrlproxy.git] / testsuite / test-client.c
1 /*
2     ircdtorture: an IRC RFC compliancy tester
3         (c) 2006 Jelmer Vernooij <jelmer@nl.linux.org>
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 3 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the Free Software
17         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <check.h>
23 #include "ctrlproxy.h"
24 #include "torture.h"
25 #include "internals.h"
26
27 START_TEST(test_create_no_network)
28         GIOChannel *ch = g_io_channel_unix_new(0);
29         client_init_iochannel(NULL, ch, "desc");
30 END_TEST
31
32 START_TEST(test_create_introduction)
33         GIOChannel *ch1, *ch2;
34         g_io_channel_pair(&ch1, &ch2);
35         client_init_iochannel(NULL, ch1, "desc");
36 END_TEST
37
38 START_TEST(test_network_first)
39         GIOChannel *ch1, *ch2;
40         struct irc_client *c;
41         char *raw;
42         g_io_channel_pair(&ch1, &ch2);
43         c = client_init_iochannel(NULL, ch1, "desc");
44         g_io_channel_unref(ch1);
45         fail_unless(g_io_channel_write_chars(ch2, "NICK bla\r\n"
46                                 "USER a a a a\r\n", -1, NULL, NULL) == G_IO_STATUS_NORMAL);
47         fail_unless(g_io_channel_flush(ch2, NULL) == G_IO_STATUS_NORMAL);
48         g_main_iteration(FALSE);
49         g_main_iteration(FALSE);
50         g_main_iteration(FALSE);
51         g_io_channel_read_to_end(ch2, &raw, NULL, NULL);
52         fail_unless(!strcmp(raw, "ERROR :Please select a network first, or specify one in your configuration file\r\n"), "Got: %s", raw);
53 END_TEST
54
55
56 START_TEST(test_login)
57         GIOChannel *ch1, *ch2;
58         struct irc_client *c;
59         struct global *g = TORTURE_GLOBAL;
60         struct irc_network_info ni = { };
61         struct irc_network n = { 
62                 .info = &ni,
63                 .name = "test",
64                 .global = g,
65         };
66         g_io_channel_pair(&ch1, &ch2);
67         c = client_init_iochannel(&n, ch1, "desc");
68         g_io_channel_unref(ch1);
69         fail_unless(g_io_channel_write_chars(ch2, "NICK bla\r\n"
70                                 "USER a a a a\r\n", -1, NULL, NULL) == G_IO_STATUS_NORMAL);
71         fail_unless(g_io_channel_flush(ch2, NULL) == G_IO_STATUS_NORMAL);
72         fail_unless(g_io_channel_flush(ch1, NULL) == G_IO_STATUS_NORMAL);
73         g_main_iteration(FALSE);
74         fail_if(c->state == NULL);
75         fail_if(c->state->me.nick == NULL);
76         fail_unless(!strcmp(c->state->me.nick, "bla"));
77 END_TEST
78
79 START_TEST(test_read_nonutf8)
80         GIOChannel *ch1, *ch2;
81         struct irc_client *c;
82         struct global *g = TORTURE_GLOBAL;
83         struct irc_network_info ni = { .name = "test" };
84         struct irc_network n = { 
85                 .info = &ni,
86                 .global = g,
87         };
88         g_io_channel_pair(&ch1, &ch2);
89         c = client_init_iochannel(&n, ch1, "desc");
90         g_io_channel_unref(ch1);
91         fail_unless(g_io_channel_write_chars(ch2, "NICK bla\r\n"
92                                 "USER a a a a\r\n"
93                                 "PRIVMSG foo :èeeé\\r\n", -1, NULL, NULL) == G_IO_STATUS_NORMAL);
94         fail_unless(g_io_channel_flush(ch2, NULL) == G_IO_STATUS_NORMAL);
95         g_main_iteration(FALSE);
96         fail_if(c->state->me.nick == NULL);
97         fail_unless(!strcmp(c->state->me.nick, "bla"));
98         client_send_args(c, "PRIVMSG", "foo", "\x024:öéé", NULL);
99 END_TEST
100
101
102
103 START_TEST(test_disconnect)
104         GIOChannel *ch1, *ch2;
105         struct irc_client *client;
106         char *raw;
107         GError *error = NULL;
108         gsize length;
109         g_io_channel_pair(&ch1, &ch2);
110         client = client_init_iochannel(NULL, ch1, "desc");
111         g_io_channel_unref(ch1);
112         client_disconnect(client, "Because");
113         g_main_iteration(FALSE);
114         g_main_iteration(FALSE);
115         g_io_channel_read_to_end(ch2, &raw, &length, &error);
116         fail_unless(!strcmp(raw, "ERROR :Because\r\n"));
117 END_TEST
118
119 START_TEST(test_replace_hostmask)
120         struct irc_line *nl, *l;
121         struct network_nick old = { 
122                 .nick = "foo", 
123                 .hostname = "foohost", 
124                 .username = "foouser", 
125                 .hostmask = "foo!foouser@foohost"
126         };
127         struct network_nick new = { 
128                 .nick = "foo", 
129                 .hostname = "barhost", 
130                 .username = "baruser", 
131                 .hostmask = "foo!baruser@barhost"
132         };
133         l = irc_parse_line(":foo!foouser@foohost JOIN #bar");
134         fail_unless(NULL == irc_line_replace_hostmask(l, NULL, &old, &old));
135         nl = irc_line_replace_hostmask(l, NULL, &old, &new);
136         fail_if(nl == NULL);
137         fail_unless(!strcmp(irc_line_string(nl), ":foo!baruser@barhost JOIN #bar"));
138         l = irc_parse_line(":bar!lala@host JOIN #bar");
139         fail_unless(NULL == irc_line_replace_hostmask(l, NULL, &old, &new));
140         l = irc_parse_line(":server 302 foo :unused=+unused@host foo=+foouser@foohost");
141         nl = irc_line_replace_hostmask(l, NULL, &old, &new);
142         fail_if(nl == NULL);
143         fail_unless(!strcmp(irc_line_string(nl), ":server 302 foo :unused=+unused@host foo=+baruser@barhost"), "was %s", irc_line_string(nl));
144         l = irc_parse_line(":server 311 foo foo foouser foohost :someinfo");
145         nl = irc_line_replace_hostmask(l, NULL, &old, &new);
146         fail_if(nl == NULL);
147         fail_unless(!strcmp(irc_line_string(nl), ":server 311 foo foo baruser barhost :someinfo"), "was %s", irc_line_string(nl));
148         l = irc_parse_line("NOTICE * boe");
149         nl = irc_line_replace_hostmask(l, NULL, &old, &new);
150         fail_unless(nl == NULL);
151 END_TEST
152
153 START_TEST(test_login_nonetwork)
154         GIOChannel *ch1, *ch2;
155         struct irc_client *c;
156         g_io_channel_pair(&ch1, &ch2);
157         c = client_init_iochannel(NULL, ch1, "desc");
158         g_io_channel_unref(ch1);
159         fail_unless(g_io_channel_write_chars(ch2, "NICK bla\r\n"
160                                 "USER a a a a\r\n", -1, NULL, NULL) == G_IO_STATUS_NORMAL);
161         fail_unless(g_io_channel_flush(ch2, NULL) == G_IO_STATUS_NORMAL);
162         g_main_iteration(FALSE);
163         fail_if(c->state->me.nick == NULL);
164 END_TEST
165
166
167
168 Suite *client_suite()
169 {
170         Suite *s = suite_create("client");
171         TCase *tc_core = tcase_create("core");
172         suite_add_tcase(s, tc_core);
173         tcase_add_test(tc_core, test_create_no_network);
174         tcase_add_test(tc_core, test_create_introduction);
175         tcase_add_test(tc_core, test_disconnect);
176         tcase_add_test(tc_core, test_login);
177         tcase_add_test(tc_core, test_login_nonetwork);
178         tcase_add_test(tc_core, test_network_first);
179         tcase_add_test(tc_core, test_read_nonutf8);
180         tcase_add_test(tc_core, test_replace_hostmask);
181         return s;
182 }