summaryrefslogtreecommitdiffstats
path: root/src/lib/test-connection.c
blob: 22677c3fe5a3ac9dadb8753d69f6293c50588459 (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/* Copyright (c) 2018 Dovecot authors, see the included COPYING file */

#include "test-lib.h"
#include "ioloop.h"
#include "connection.h"
#include "istream.h"
#include "ostream.h"
#include "strnum.h"
#include "strescape.h"

#include <unistd.h>

static const struct connection_settings client_set =
{
	.service_name_in = "TEST-S",
	.service_name_out = "TEST-C",
	.major_version = 1,
	.minor_version = 0,
	.client = TRUE,
	.input_max_size = SIZE_MAX,
	.output_max_size = SIZE_MAX,
};

static const struct connection_settings server_set =
{
	.service_name_in = "TEST-C",
	.service_name_out = "TEST-S",
	.major_version = 1,
	.minor_version = 0,
	.client = FALSE,
	.input_max_size = SIZE_MAX,
	.output_max_size = SIZE_MAX,
};

static bool received_quit = FALSE;
static bool was_resumed = FALSE;
static bool was_idle_killed = FALSE;
static int received_count = 0;

static void test_connection_run(const struct connection_settings *set_s,
				const struct connection_settings *set_c,
				const struct connection_vfuncs *v_s,
				const struct connection_vfuncs *v_c,
				unsigned int iter_count)
{
	int fds[2];

	struct ioloop *loop = io_loop_create();
	struct connection_list *clients = connection_list_init(set_c, v_c);
	struct connection_list *servers = connection_list_init(set_s, v_s);
	struct connection *conn_c = i_new(struct connection, 1);
	struct connection *conn_s = i_new(struct connection, 1);

	conn_s->ioloop = loop;
	conn_c->ioloop = loop;

	for(unsigned int iters = 0; iters < iter_count; iters++) {
		test_assert(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0);

		fd_set_nonblock(fds[0], TRUE);
		fd_set_nonblock(fds[1], TRUE);
		connection_init_server(servers, conn_s, "client", fds[1], fds[1]);
		connection_init_client_fd(clients, conn_c, "server", fds[0], fds[0]);

		io_loop_run(loop);

		connection_deinit(conn_c);
		connection_deinit(conn_s);
	}

	i_free(conn_c);
	i_free(conn_s);

	connection_list_deinit(&clients);
	connection_list_deinit(&servers);

	io_loop_destroy(&loop);
}

/* BEGIN SIMPLE TEST */

static void test_connection_simple_client_connected(struct connection *conn, bool success)
{
	if (conn->list->set.client)
		o_stream_nsend_str(conn->output, "QUIT\n");
	test_assert(success);
};

static int
test_connection_simple_input_args(struct connection *conn, const char *const *args)
{
	if (strcmp(args[0], "QUIT") == 0) {
		received_quit = TRUE;
		connection_disconnect(conn);
		return 0;
	}
	i_error("invalid input");
	return -1;
}

static void test_connection_simple_destroy(struct connection *conn)
{
	io_loop_stop(conn->ioloop);
	connection_disconnect(conn);
}

static const struct connection_vfuncs simple_v =
{
	.client_connected = test_connection_simple_client_connected,
	.input_args = test_connection_simple_input_args,
	.destroy = test_connection_simple_destroy,
};

static void test_connection_simple(void)
{
	test_begin("connection simple");

	test_connection_run(&server_set, &client_set, &simple_v, &simple_v, 10);

	test_assert(received_quit);
	received_quit = FALSE;

	test_end();
}

/* BEGIN NO INPUT TEST */

static const struct connection_settings no_input_client_set =
{
	.service_name_in = "TEST-S",
	.service_name_out = "TEST-C",
	.major_version = 1,
	.minor_version = 0,
	.client = TRUE,
	.input_max_size = 0,
	.output_max_size = SIZE_MAX,
};

static const struct connection_settings no_input_server_set =
{
	.service_name_in = "TEST-C",
	.service_name_out = "TEST-S",
	.major_version = 1,
	.minor_version = 0,
	.client = FALSE,
	.input_max_size = 0,
	.output_max_size = SIZE_MAX,
};

static void
test_connection_no_input_input(struct connection *conn)
{
	const char *input;
	struct istream *is = i_stream_create_fd(conn->fd_in, SIZE_MAX);
	i_stream_set_blocking(is, FALSE);
	while ((input = i_stream_read_next_line(is)) != NULL) {
		const char *const *args = t_strsplit_tabescaped(input);
		if (!conn->handshake_received) {
			if (connection_handshake_args_default(conn, args) > -1)
				conn->handshake_received = TRUE;
			continue;
		}
		if (strcmp(args[0], "QUIT") == 0) {
			received_quit = TRUE;
			io_loop_stop(conn->ioloop);
			break;
		}
	}
	i_stream_unref(&is);
}

static const struct connection_vfuncs no_input_v =
{
	.client_connected = test_connection_simple_client_connected,
	.input = test_connection_no_input_input,
	.destroy = test_connection_simple_destroy,
};

static void test_connection_no_input(void)
{
	test_begin("connection no input stream");

	test_connection_run(&no_input_server_set, &no_input_client_set,
			    &no_input_v, &no_input_v, 1);

	test_assert(received_quit);
	received_quit = FALSE;

	test_end();
}

/* BEGIN HANDSHAKE TEST */
static void test_connection_custom_handshake_client_connected(struct connection *conn, bool success)
{
	if (conn->list->set.client)
		o_stream_nsend_str(conn->output, "HANDSHAKE\tFRIEND\n");
	test_assert(success);
};

static int test_connection_custom_handshake_args(struct connection *conn,
						 const char *const *args)
{
	if (!conn->version_received) {
		if (connection_handshake_args_default(conn, args) < 0)
			return -1;
		return 0;
	}
	if (!conn->handshake_received) {
		if (strcmp(args[0], "HANDSHAKE") == 0 &&
		    strcmp(args[1], "FRIEND") == 0) {
			if (!conn->list->set.client)
				o_stream_nsend_str(conn->output, "HANDSHAKE\tFRIEND\n");
			else
				o_stream_nsend_str(conn->output, "QUIT\n");
			return 1;
		}
		return -1;
	}
	return 1;
}

static const struct connection_vfuncs custom_handshake_v =
{
	.client_connected = test_connection_custom_handshake_client_connected,
	.input_args = test_connection_simple_input_args,
	.handshake_args = test_connection_custom_handshake_args,
	.destroy = test_connection_simple_destroy,
};

static void test_connection_custom_handshake(void)
{
	test_begin("connection custom handshake");

	test_connection_run(&server_set, &client_set, &custom_handshake_v,
			    &custom_handshake_v, 10);

	test_assert(received_quit);
	received_quit = FALSE;

	test_end();
}

/* BEGIN PING PONG TEST */

static int test_connection_ping_pong_input_args(struct connection *conn, const char *const *args)
{
	unsigned int n;
	test_assert(args[0] != NULL && args[1] != NULL);
	if (args[0] == NULL || args[1] == NULL)
		return -1;
	if (str_to_uint(args[1], &n) < 0)
		return -1;
	if (n > 10)
		o_stream_nsend_str(conn->output, "QUIT\t0\n");
	else if (strcmp(args[0], "QUIT") == 0)
		connection_disconnect(conn);
	else if (strcmp(args[0], "PING") == 0) {
		received_count++;
		o_stream_nsend_str(conn->output, t_strdup_printf("PONG\t%u\n", n+1));
	} else if (strcmp(args[0], "PONG") == 0)
		o_stream_nsend_str(conn->output, t_strdup_printf("PING\t%u\n", n));
	else
		return -1;
	return 1;
}

static void test_connection_ping_pong_client_connected(struct connection *conn, bool success)
{
	o_stream_nsend_str(conn->output, "PING\t1\n");
	test_assert(success);
};

static const struct connection_vfuncs ping_pong_v =
{
	.client_connected = test_connection_ping_pong_client_connected,
	.input_args = test_connection_ping_pong_input_args,
	.destroy = test_connection_simple_destroy,
};

static void test_connection_ping_pong(void)
{
	test_begin("connection ping pong");

	test_connection_run(&server_set, &client_set, &ping_pong_v,
			    &ping_pong_v, 10);

	test_assert(received_count == 100);

	test_end();
}

/* BEGIN INPUT FULL TEST */

static const struct connection_settings input_full_client_set =
{
	.service_name_in = "TEST-S",
	.service_name_out = "TEST-C",
	.major_version = 1,
	.minor_version = 0,
	.client = TRUE,
	.input_max_size = 100,
	.output_max_size = SIZE_MAX,
};

static int test_connection_input_full_input_args(struct connection *conn,
						 const char *const *args ATTR_UNUSED)
{
	/* send a long line */
	for (unsigned int i = 0; i < 200; i++)
		o_stream_nsend(conn->output, "c", 1);
	return 1;
}

static void test_connection_input_full_destroy(struct connection *conn)
{
	test_assert(conn->disconnect_reason == CONNECTION_DISCONNECT_BUFFER_FULL ||
		    conn->list->set.client == FALSE);
	test_connection_simple_destroy(conn);
}

static const struct connection_vfuncs input_full_v =
{
	.client_connected = test_connection_simple_client_connected,
	.input_args = test_connection_input_full_input_args,
	.destroy = test_connection_input_full_destroy,
};

static void test_connection_input_full(void)
{
	test_begin("connection input full");

	test_connection_run(&server_set, &input_full_client_set, &input_full_v,
			    &simple_v, 10);
	test_end();
}

/* BEGIN RESUME TEST */
static struct timeout *to_send_quit = NULL;
static struct timeout *to_resume = NULL;

static void test_connection_resume_client_connected(struct connection *conn, bool success)
{
	test_assert(success);
	o_stream_nsend_str(conn->output, "BEGIN\n");
}

static void test_connection_resume_continue(struct connection *conn)
{
	timeout_remove(&to_resume);
	/* ensure QUIT wasn't received early */
	was_resumed = !received_quit;
	connection_input_resume(conn);
}

static void test_connection_resume_send_quit(struct connection *conn)
{
	timeout_remove(&to_send_quit);
	o_stream_nsend_str(conn->output, "QUIT\n");
}

static int test_connection_resume_input_args(struct connection *conn,
					     const char *const *args)
{
	test_assert(args[0] != NULL);
	if (args[0] == NULL)
		return -1;

	if (strcmp(args[0], "BEGIN") == 0) {
		o_stream_nsend_str(conn->output, "HALT\n");
		to_send_quit = timeout_add_short(10, test_connection_resume_send_quit, conn);
	} else if (strcmp(args[0], "HALT") == 0) {
		connection_input_halt(conn);
		to_resume = timeout_add_short(100, test_connection_resume_continue, conn);
	} else if (strcmp(args[0], "QUIT") == 0) {
		received_quit = TRUE;
		connection_disconnect(conn);
	}

	return 1;
}

static const struct connection_vfuncs resume_v =
{
	.client_connected = test_connection_resume_client_connected,
	.input_args = test_connection_resume_input_args,
	.destroy = test_connection_simple_destroy,
};

static void test_connection_resume(void)
{
	test_begin("connection resume");

	was_resumed = received_quit = FALSE;
	test_connection_run(&server_set, &client_set, &resume_v, &resume_v, 1);

	test_assert(was_resumed);
	test_assert(received_quit);
	was_resumed = received_quit = FALSE;

	test_end();
}

/* BEGIN RESUME PIPELINED TEST */
static int test_connection_resume_pipelined_input_args(struct connection *conn,
						       const char *const *args)
{
	test_assert(args[0] != NULL);
	if (args[0] == NULL)
		return -1;

	if (strcmp(args[0], "BEGIN") == 0) {
		o_stream_nsend_str(conn->output, "HALT\nQUIT\n");
	} else if (strcmp(args[0], "HALT") == 0) {
		connection_input_halt(conn);
		to_resume = timeout_add_short(100, test_connection_resume_continue, conn);
		return 0;
	} else if (strcmp(args[0], "QUIT") == 0) {
		received_quit = TRUE;
		connection_disconnect(conn);
	}

	return 1;
}

static const struct connection_vfuncs resume_pipelined_v =
{
	.client_connected = test_connection_resume_client_connected,
	.input_args = test_connection_resume_pipelined_input_args,
	.destroy = test_connection_simple_destroy,
};

static void test_connection_resume_pipelined(void)
{
	test_begin("connection resume pipelined");

	was_resumed = received_quit = FALSE;
	test_connection_run(&server_set, &client_set,
			    &resume_pipelined_v, &resume_pipelined_v, 1);

	test_assert(was_resumed);
	test_assert(received_quit);
	was_resumed = received_quit = FALSE;

	test_end();
}

/* BEGIN IDLE KILL TEST */

static void
test_connection_idle_kill_client_connected(struct connection *conn ATTR_UNUSED,
					   bool success)
{
	test_assert(success);
};

static const struct connection_settings idle_kill_server_set =
{
	.service_name_in = "TEST-C",
	.service_name_out = "TEST-S",
	.major_version = 1,
	.minor_version = 0,
	.client = FALSE,
	.input_max_size = SIZE_MAX,
	.output_max_size = SIZE_MAX,
	.input_idle_timeout_secs = 1,
};

static void test_connection_idle_kill_timeout(struct connection *conn)
{
	was_idle_killed = TRUE;
	o_stream_nsend_str(conn->output, "QUIT\n");
}

static const struct connection_vfuncs idle_kill_v =
{
	.client_connected = test_connection_idle_kill_client_connected,
	.input_args = test_connection_simple_input_args,
	.destroy = test_connection_simple_destroy,
	.idle_timeout = test_connection_idle_kill_timeout,
};

static void test_connection_idle_kill(void)
{
	test_begin("connection idle kill");

	was_idle_killed = received_quit = FALSE;
	test_connection_run(&idle_kill_server_set, &client_set, &idle_kill_v,
			    &idle_kill_v, 1);

	test_assert(received_quit);
	test_assert(was_idle_killed);
	was_idle_killed = received_quit = FALSE;

	test_end();
}

/* BEGIN HANDSHAKE FAILED TEST (version) */

static void test_connection_handshake_failed_destroy(struct connection *conn)
{
	test_assert(conn->disconnect_reason == CONNECTION_DISCONNECT_HANDSHAKE_FAILED);
	test_connection_simple_destroy(conn);
}

static const struct connection_vfuncs handshake_failed_version_v =
{
	.client_connected = test_connection_simple_client_connected,
	.input_args = test_connection_simple_input_args,
	.destroy = test_connection_handshake_failed_destroy,
};

static void test_connection_handshake_failed_version(void)
{
	static const struct connection_settings client_sets[] = {
	{
		.service_name_in = "TEST-S",
		.service_name_out = "TEST-S",
		.major_version = 1,
		.minor_version = 0,
		.client = TRUE,
		.input_max_size = SIZE_MAX,
		.output_max_size = SIZE_MAX,
	},
	{
		.service_name_in = "TEST-C",
		.service_name_out = "TEST-C",
		.major_version = 1,
		.minor_version = 0,
		.client = TRUE,
		.input_max_size = SIZE_MAX,
		.output_max_size = SIZE_MAX,
	},
	{
		.service_name_in = "TEST-S",
		.service_name_out = "TEST-C",
		.major_version = 2,
		.minor_version = 0,
		.client = TRUE,
		.input_max_size = SIZE_MAX,
		.output_max_size = SIZE_MAX,
	}
	};

	static const struct connection_settings client_set_minor = {
		.service_name_in = "TEST-S",
		.service_name_out = "TEST-C",
		.major_version = 1,
		.minor_version = 2,
		.client = TRUE,
		.input_max_size = SIZE_MAX,
		.output_max_size = SIZE_MAX,
	};

	test_begin("connection handshake failed (version)");

	test_expect_errors(N_ELEMENTS(client_sets));

	/* this should stay FALSE during the version mismatch sets */
	received_quit = FALSE;
	for (size_t i = 0; i < N_ELEMENTS(client_sets); i++) {
		test_connection_run(&server_set, &client_sets[i], &simple_v,
				    &handshake_failed_version_v, 1);
		test_assert(!received_quit);
	}

	received_quit = FALSE;
	test_connection_run(&server_set, &client_set_minor, &simple_v,
			    &simple_v, 1);
	test_assert(received_quit);
	received_quit = FALSE;

	test_end();
}

/* BEGIN HANDSHAKE FAILED TEST (args) */

static int test_connection_handshake_failed_1_args(struct connection *conn ATTR_UNUSED,
						   const char *const *args ATTR_UNUSED)
{
	/* just fail */
	return -1;
}

static const struct connection_vfuncs handshake_failed_1_v =
{
	.client_connected = test_connection_simple_client_connected,
	.input_args = test_connection_simple_input_args,
	.handshake_args = test_connection_handshake_failed_1_args,
	.destroy = test_connection_handshake_failed_destroy,
};

static void test_connection_handshake_failed_args(void)
{
	test_begin("connection handshake failed (handshake_args)");

	test_connection_run(&server_set, &client_set, &simple_v,
			    &handshake_failed_1_v, 10);

	test_end();
}

/* BEGIN HANDSHAKE FAILED TEST (handshake_line) */

static int test_connection_handshake_failed_2_line(struct connection *conn ATTR_UNUSED,
						   const char *line ATTR_UNUSED)
{
	return -1;
}

static const struct connection_vfuncs handshake_failed_2_v =
{
	.client_connected = test_connection_simple_client_connected,
	.input_args = test_connection_simple_input_args,
	.handshake_line = test_connection_handshake_failed_2_line,
	.destroy = test_connection_handshake_failed_destroy,
};

static void test_connection_handshake_failed_line(void)
{
	test_begin("connection handshake failed (handshake_line)");

	test_connection_run(&server_set, &client_set, &simple_v,
			    &handshake_failed_2_v, 10);

	test_end();
}

/* BEGIN HANDSHAKE FAILED TEST (handshake) */

static int test_connection_handshake_failed_3(struct connection *conn ATTR_UNUSED)
{
	return -1;
}

static const struct connection_vfuncs handshake_failed_3_v =
{
	.client_connected = test_connection_simple_client_connected,
	.input_args = test_connection_simple_input_args,
	.handshake = test_connection_handshake_failed_3,
	.destroy = test_connection_handshake_failed_destroy,
};

static void test_connection_handshake_failed_input(void)
{
	test_begin("connection handshake failed (handshake)");

	test_connection_run(&server_set, &client_set, &simple_v,
			    &handshake_failed_3_v, 10);

	test_end();
}

/* BEGIN CONNECTION ERRORED TEST (ensure correct error) */

static void test_connection_errored_client_connected(struct connection *conn,
						     bool success)
{
	test_assert(success);
	o_stream_nsend_str(conn->output, "HELLO\n");
}

static void test_connection_errored_destroy(struct connection *conn)
{
	test_assert(conn->disconnect_reason == CONNECTION_DISCONNECT_DEINIT);
	test_connection_simple_destroy(conn);
}

static int test_connection_errored_input_line(struct connection *conn ATTR_UNUSED,
					      const char *line)
{
	if (str_begins(line, "VERSION"))
		return 1;
	return -1;
}

static const struct connection_vfuncs test_connection_errored_1_v =
{
	.client_connected = test_connection_errored_client_connected,
	.input_line = test_connection_errored_input_line,
	.destroy = test_connection_errored_destroy,
};

static void test_connection_input_error_reason(void)
{
	test_begin("connection input error (correct disconnect reason)");

	test_connection_run(&server_set, &client_set, &test_connection_errored_1_v,
			    &test_connection_errored_1_v, 10);

	test_end();
}

/* END CONNECTION ERRORED TEST */

/* BEGIN NO VERSION TEST */

static const struct connection_settings no_version_client_set =
{
	.major_version = 0,
	.minor_version = 0,
	.client = TRUE,
	.input_max_size = SIZE_MAX,
	.output_max_size = SIZE_MAX,
	.dont_send_version = TRUE,
};

static const struct connection_settings no_version_server_set =
{
	.major_version = 0,
	.minor_version = 0,
	.input_max_size = SIZE_MAX,
	.output_max_size = SIZE_MAX,
	.dont_send_version = TRUE,
};

static void test_connection_no_version(void)
{
        test_begin("connection no version sent");

        test_connection_run(&no_version_server_set, &no_version_client_set,
			    &simple_v, &simple_v, 10);

        test_end();
}

/* END NO VERSION TEST */

void test_connection(void)
{
	test_connection_simple();
	test_connection_no_input();
	test_connection_custom_handshake();
	test_connection_ping_pong();
	test_connection_input_full();
	test_connection_resume();
	test_connection_resume_pipelined();
	test_connection_idle_kill();
	test_connection_handshake_failed_version();
	test_connection_handshake_failed_args();
	test_connection_handshake_failed_line();
	test_connection_handshake_failed_input();
	test_connection_input_error_reason();
	test_connection_no_version();
}