~rcr/rirc

ref: c677d85e88b1397d0eaa9b99ad3acf531f64aa3f rirc/test/components/server.c -rw-r--r-- 8.1 KiB
c677d85e — Richard Robbins add -m/--mode flag 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "test/test.h"
#include "src/components/buffer.c"
#include "src/components/channel.c"
#include "src/components/input.c"
#include "src/components/ircv3.c"
#include "src/components/mode.c"
#include "src/components/server.c"
#include "src/components/user.c"
#include "src/utils/utils.c"

void
newline(struct channel *c, enum buffer_line_type t, const char *f, const char *m)
{
	/* Mock */
	UNUSED(c);
	UNUSED(t);
	UNUSED(f);
	UNUSED(m);
}

void
newlinef(struct channel *c, enum buffer_line_type t, const char *f, const char *m, ...)
{
	/* Mock */
	UNUSED(c);
	UNUSED(t);
	UNUSED(f);
	UNUSED(m);
}

static void
test_server_list(void)
{
	/* Test add/del/get servers */

	struct server_list servers;
	struct server *s1 = NULL,
	              *s2 = NULL,
	              *s3 = NULL,
	              *s4 = NULL,
	              *s5 = NULL,
	              *s6 = NULL,
	              *s7 = NULL;

	memset(&servers, 0, sizeof(servers));

	s1 = server("host1", "port1",   NULL, "real", "user", "abc");
	s2 = server("host1", "port1", "foo1", "real", "user", "abc"); /* duplicate host, port (s1) */
	s3 = server("host1", "port2", "foo2", "real", "user", "abc"); /* duplicate host (s1), different port */
	s4 = server("host2", "port1", "foo3", "real", "user", NULL);  /* duplicate port (s1), different host */
	s5 = server("host2", "port2",   NULL, "real", "user", NULL);  /* duplicate host (s4), duplicate port (s3) */
	s6 = server("host1", "port2",   NULL, "real", "user", NULL);  /* duplicate host, port (s4) */
	s7 = server("host2", "port1",   NULL, "real", "user", NULL);  /* duplicate host, port (s5) */

	/* Test add */
	assert_ptr_eq(server_list_add(&servers, s1), NULL);
	assert_ptr_eq(server_list_add(&servers, s1), s1); /* duplicate pointer */
	assert_ptr_eq(server_list_add(&servers, s2), s2); /* duplicate by host/port */
	assert_ptr_eq(server_list_add(&servers, s3), NULL);
	assert_ptr_eq(server_list_add(&servers, s4), NULL);
	assert_ptr_eq(server_list_add(&servers, s5), NULL);
	assert_ptr_eq(server_list_add(&servers, s6), s6); /* duplicate by host/port */
	assert_ptr_eq(server_list_add(&servers, s7), s7); /* duplicate by host/port */

	/* Test del */
	assert_ptr_eq(server_list_del(&servers, s2), NULL); /* not in list */
	assert_ptr_eq(server_list_del(&servers, s6), NULL); /* not in list */
	assert_ptr_eq(server_list_del(&servers, s7), NULL); /* not in list */

	/* head              tail
	 *  v                 v
	 *  s1 -> s3 -> s4 -> s5
	 */
	assert_ptr_eq(servers.head, s1);
	assert_ptr_eq(servers.tail, s5);

	/* delete from middle */
	assert_ptr_eq(server_list_del(&servers, s4), s4);
	assert_ptr_eq(servers.head, s1);
	assert_ptr_eq(servers.tail, s5);

	/* delete head */
	assert_ptr_eq(server_list_del(&servers, s1), s1);
	assert_ptr_eq(servers.head, s3);
	assert_ptr_eq(servers.tail, s5);

	/* delete tail */
	assert_ptr_eq(server_list_del(&servers, s5), s5);
	assert_ptr_eq(servers.head, s3);
	assert_ptr_eq(servers.tail, s3);

	/* delete last */
	assert_ptr_eq(server_list_del(&servers, s3), s3);
	assert_ptr_eq(servers.head, NULL);
	assert_ptr_eq(servers.tail, NULL);

	/* server was removed from list */
	assert_ptr_eq(s1->next, NULL);
	assert_ptr_eq(s1->prev, NULL);

	/* server was never added to list */
	assert_ptr_eq(s7->next, NULL);
	assert_ptr_eq(s7->prev, NULL);

	server_free(s1);
	server_free(s2);
	server_free(s3);
	server_free(s4);
	server_free(s5);
	server_free(s6);
	server_free(s7);
}

static void
test_server_set_chans(void)
{
	struct channel *c;
	struct server *s;

	s = server("host1", "port", NULL, "real", "user", NULL);
	assert_eq(s->clist.count, 1);

	/* empty values, invalid formats */
	assert_eq(server_set_chans(s, ""), -1);
	assert_eq(s->clist.count, 1);
	assert_eq(server_set_chans(s, ","), -1);
	assert_eq(s->clist.count, 1);
	assert_eq(server_set_chans(s, ",,,"), -1);
	assert_eq(s->clist.count, 1);
	assert_eq(server_set_chans(s, ",#a,#b,c"), -1);
	assert_eq(s->clist.count, 1);
	assert_eq(server_set_chans(s, "#a,#b,c,"), -1);
	assert_eq(s->clist.count, 1);
	assert_eq(server_set_chans(s, "#a,#b,c "), -1);
	assert_eq(s->clist.count, 1);
	assert_eq(server_set_chans(s, "#a b #c"), -1);
	assert_eq(s->clist.count, 1);

	/* valid */
	assert_eq(server_set_chans(s, "#a,b,c,#d"), 0);
	assert_eq(s->clist.count, 5);

	if (!(c = channel_list_get(&(s->clist), "#a", s->casemapping)))
		test_abort("failed to find channel '#a'");
	assert_eq(c->type, CHANNEL_T_CHANNEL);

	if (!(c = channel_list_get(&(s->clist), "b", s->casemapping)))
		test_abort("failed to find channel 'b'");
	assert_eq(c->type, CHANNEL_T_PRIVMSG);

	if (!(c = channel_list_get(&(s->clist), "c", s->casemapping)))
		test_abort("failed to find channel '#c'");
	assert_eq(c->type, CHANNEL_T_PRIVMSG);

	if (!(c = channel_list_get(&(s->clist), "#d", s->casemapping)))
		test_abort("failed to find channel 'd'");
	assert_eq(c->type, CHANNEL_T_CHANNEL);

	server_free(s);
}

static void
test_server_set_nicks(void)
{
	struct server *s = server("host", "port", NULL, "real", "user", NULL);

	server_nicks_next(s);
	assert_strncmp(s->nick, "rirc", 4);

	/* empty values, invalid formats */
	assert_eq(server_set_nicks(s, ""), -1);
	assert_eq(server_set_nicks(s, ","), -1);
	assert_eq(server_set_nicks(s, ",,,"), -1);
	assert_eq(server_set_nicks(s, ",a,b,c"), -1);
	assert_eq(server_set_nicks(s, "a,b,c,"), -1);
	assert_eq(server_set_nicks(s, "a,b,c "), -1);
	assert_eq(server_set_nicks(s, "a b c"), -1);

	/* invalid nicks */
	assert_eq(server_set_nicks(s, "!"), -1);
	assert_eq(server_set_nicks(s, "0abc"), -1);
	assert_eq(server_set_nicks(s, "abc,-def"), -1);
	assert_eq(server_set_nicks(s, "abc,d!ef"), -1);

	assert_strncmp(s->nick, "rirc", 4);

	/* valid */
	assert_eq(server_set_nicks(s, "a"), 0);
	assert_eq((signed)s->nicks.size, 1);
	assert_eq(server_set_nicks(s, "abc,d"), 0);
	assert_eq((signed)s->nicks.size, 2);
	assert_eq(server_set_nicks(s, "ab0,d,e-g"), 0);
	assert_eq((signed)s->nicks.size, 3);
	assert_eq(server_set_nicks(s, "a_,b__,c___,d____"), 0);
	assert_eq((signed)s->nicks.size, 4);

	server_nicks_next(s);
	assert_strcmp(s->nick, "a_");
	server_nicks_next(s);
	assert_strcmp(s->nick, "b__");
	server_nicks_next(s);
	assert_strcmp(s->nick, "c___");
	server_nicks_next(s);
	assert_strcmp(s->nick, "d____");
	server_nicks_next(s);
	assert_strncmp(s->nick, "rirc", 4);

	server_reset(s);
	server_nicks_next(s);
	assert_strcmp(s->nick, "a_");

	server_free(s);
}

static void
test_parse_005(void)
{
	/* Test numeric 005 parsing  */

	struct opt opt;

	char *ptr;

	char opts0[] = "";
	ptr = opts0;
	assert_eq(parse_005(&opt, &ptr), 0);

	char opts1[] = "=";
	ptr = opts1;
	assert_eq(parse_005(&opt, &ptr), 0);

	char opts2[] = " ";
	ptr = opts2;
	assert_eq(parse_005(&opt, &ptr), 0);

	char opts3[] = "=TESTING";
	ptr = opts3;
	assert_eq(parse_005(&opt, &ptr), 0);

	char opts4[] = ":test,trailing";
	ptr = opts4;
	assert_eq(parse_005(&opt, &ptr), 0);

#define CHECK(R, A, V) \
	assert_eq(parse_005(&opt, &ptr), (R)); \
	assert_strcmp(opt.arg, (A)); \
	assert_strcmp(opt.val, (V));

	char opts5[] = "TESTING";
	ptr = opts5;
	CHECK(1, "TESTING", NULL);
	CHECK(0, NULL,      NULL);

	char opts6[] = " TESTING ";
	ptr = opts6;
	CHECK(1, "TESTING", NULL);
	CHECK(0, NULL,      NULL);

	char opts7[] = "TESTING=";
	ptr = opts7;
	CHECK(1, "TESTING", NULL);
	CHECK(0, NULL,      NULL);

	char opts8[] = "TESTING1=TESTING2";
	ptr = opts8;
	CHECK(1, "TESTING1", "TESTING2");
	CHECK(0, NULL,       NULL);

	char opts9[] = "TESTING1=TESTING2 TESTING3=";
	ptr = opts9;
	CHECK(1, "TESTING1", "TESTING2");
	CHECK(1, "TESTING3", NULL);
	CHECK(0, NULL,       NULL);

	char opts10[] = "000 1=t 2=t! 3= 4=  5=t,t, 6= 7=7 8===D 9=9 10=10 11= 12 13 ";
	ptr = opts10;

	CHECK(1, "000", NULL);
	CHECK(1, "1",   "t");
	CHECK(1, "2",   "t!");
	CHECK(1, "3",   NULL);
	CHECK(1, "4",   NULL);
	CHECK(1, "5",   "t,t,");
	CHECK(1, "6",   NULL);
	CHECK(1, "7",   "7");
	CHECK(1, "8",   "==D");
	CHECK(1, "9",   "9");
	CHECK(1, "10",  "10");
	CHECK(1, "11",  NULL);
	CHECK(1, "12",  NULL);
	CHECK(1, "13",  NULL);
	CHECK(0, NULL,  NULL);

#undef CHECK
}

int
main(void)
{
	struct testcase tests[] = {
		TESTCASE(test_server_list),
		TESTCASE(test_server_set_chans),
		TESTCASE(test_server_set_nicks),
		TESTCASE(test_parse_005)
	};

	return run_tests(NULL, NULL, tests);
}