Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Config/SocketOptions.pm

raw
1 package ProFTPD::Tests::Config::SocketOptions;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Spec;
8 use IO::Handle;
9 use Socket qw(:all);
10
11 use ProFTPD::TestSuite::FTP;
12 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
13
14 $| = 1;
15
16 my $order = 0;
17
18 my $TESTS = {
19 socketoptions_none => {
20 order => ++$order,
21 test_class => [qw(forking)],
22 },
23
24 socketoptions_rcvbuf_bug3607 => {
25 order => ++$order,
26 test_class => [qw(bug forking)],
27 },
28
29 socketoptions_sndbuf_bug3607 => {
30 order => ++$order,
31 test_class => [qw(bug forking)],
32 },
33
34 socketoptions_keepalive_on => {
35 order => ++$order,
36 test_class => [qw(bug forking)],
37 },
38
39 socketoptions_keepalive_off => {
40 order => ++$order,
41 test_class => [qw(bug forking)],
42 },
43
44 socketoptions_keepalive_details_issue1402 => {
45 order => ++$order,
46 test_class => [qw(bug forking)],
47 },
48
49 };
50
51 sub new {
52 return shift()->SUPER::new(@_);
53 }
54
55 sub list_tests {
56 return testsuite_get_runnable_tests($TESTS);
57 }
58
59 sub socketoptions_none {
60 my $self = shift;
61 my $tmpdir = $self->{tmpdir};
62 my $setup = test_setup($tmpdir, 'config');
63
64 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
65 if (open(my $fh, "> $test_file")) {
66 print $fh "ABCD" x 8192;
67 unless (close($fh)) {
68 die("Can't write $test_file: $!");
69 }
70
71 } else {
72 die("Can't open $test_file: $!");
73 }
74
75 my $config = {
76 PidFile => $setup->{pid_file},
77 ScoreboardFile => $setup->{scoreboard_file},
78 SystemLog => $setup->{log_file},
79 TraceLog => $setup->{log_file},
80 Trace => 'DEFAULT:0 data:10',
81
82 AuthUserFile => $setup->{auth_user_file},
83 AuthGroupFile => $setup->{auth_group_file},
84 AuthOrder => 'mod_auth_file.c',
85
86 IfModules => {
87 'mod_delay.c' => {
88 DelayEngine => 'off',
89 },
90 },
91 };
92
93 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
94 $config);
95
96 # Open pipes, for use between the parent and child processes. Specifically,
97 # the child will indicate when it's done with its test by writing a message
98 # to the parent.
99 my ($rfh, $wfh);
100 unless (pipe($rfh, $wfh)) {
101 die("Can't open pipe: $!");
102 }
103
104 my $ex;
105
106 # Fork child
107 $self->handle_sigchld();
108 defined(my $pid = fork()) or die("Can't fork: $!");
109 if ($pid) {
110 eval {
111 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
112 $client->login($setup->{user}, $setup->{passwd});
113
114 my $conn = $client->retr_raw('test.txt');
115 unless ($conn) {
116 die("Failed to RETR: " . $client->response_code() . " " .
117 $client->response_msg());
118 }
119
120 my $buf;
121 while ($conn->read($buf, 16384, 25)) {
122 }
123 eval { $conn->close() };
124
125 my $resp_code = $client->response_code();
126 my $resp_msg = $client->response_msg();
127
128 my $expected;
129
130 $expected = 226;
131 $self->assert($expected == $resp_code,
132 test_msg("Expected response code $expected, got $resp_code"));
133
134 $expected = "Transfer complete";
135 $self->assert($expected eq $resp_msg,
136 test_msg("Expected response message '$expected', got '$resp_msg'"));
137
138 $client->quit();
139 };
140 if ($@) {
141 $ex = $@;
142 }
143
144 $wfh->print("done\n");
145 $wfh->flush();
146
147 } else {
148 eval { server_wait($setup->{config_file}, $rfh) };
149 if ($@) {
150 warn($@);
151 exit 1;
152 }
153
154 exit 0;
155 }
156
157 # Stop server
158 server_stop($setup->{pid_file});
159 $self->assert_child_ok($pid);
160
161 if ($ex) {
162 test_cleanup($setup->{log_file}, $ex);
163 die($ex);
164 }
165
166 # Examine the Trace messages for the 'data' channel, to see what buffer
167 # sizes were allocated for this download
168 eval {
169 if (open(my $fh, "< $setup->{log_file}")) {
170 while (my $line = <$fh>) {
171 chomp($line);
172
173 if ($line =~ /<data\:\d+>\:\s+allocated\s+.*?\s+buffer\s+of\s+(\d+)\s+bytes/) {
174 my $bufsz = $1;
175 $self->assert($bufsz > 0,
176 test_msg("Expected greater than 0, got $bufsz"));
177 }
178 }
179
180 close($fh);
181
182 } else {
183 die("Can't read $setup->{log_file}: $!");
184 }
185 };
186 if ($@) {
187 $ex = $@;
188 }
189
190 test_cleanup($setup->{log_file}, $ex);
191 }
192
193 sub socketoptions_rcvbuf_bug3607 {
194 my $self = shift;
195 my $tmpdir = $self->{tmpdir};
196 my $setup = test_setup($tmpdir, 'config');
197
198 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
199
200 my $rcvbufsz = 2502440;
201
202 my $config = {
203 PidFile => $setup->{pid_file},
204 ScoreboardFile => $setup->{scoreboard_file},
205 SystemLog => $setup->{log_file},
206 TraceLog => $setup->{log_file},
207 Trace => 'DEFAULT:0 data:10',
208
209 AuthUserFile => $setup->{auth_user_file},
210 AuthGroupFile => $setup->{auth_group_file},
211 AuthOrder => 'mod_auth_file.c',
212
213 SocketOptions => "rcvbuf $rcvbufsz",
214
215 IfModules => {
216 'mod_delay.c' => {
217 DelayEngine => 'off',
218 },
219 },
220 };
221
222 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
223 $config);
224
225 # Open pipes, for use between the parent and child processes. Specifically,
226 # the child will indicate when it's done with its test by writing a message
227 # to the parent.
228 my ($rfh, $wfh);
229 unless (pipe($rfh, $wfh)) {
230 die("Can't open pipe: $!");
231 }
232
233 my $ex;
234
235 # Fork child
236 $self->handle_sigchld();
237 defined(my $pid = fork()) or die("Can't fork: $!");
238 if ($pid) {
239 eval {
240 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
241 $client->login($setup->{user}, $setup->{passwd});
242
243 my $conn = $client->stor_raw('test.txt');
244 unless ($conn) {
245 die("Failed to STOR: " . $client->response_code() . " " .
246 $client->response_msg());
247 }
248
249 my $buf = "ABCD" x 8192;
250 $conn->write($buf, length($buf), 25);
251 eval { $conn->close() };
252
253 my $resp_code = $client->response_code();
254 my $resp_msg = $client->response_msg();
255
256 my $expected;
257
258 $expected = 226;
259 $self->assert($expected == $resp_code,
260 test_msg("Expected response code $expected, got $resp_code"));
261
262 $expected = "Transfer complete";
263 $self->assert($expected eq $resp_msg,
264 test_msg("Expected response message '$expected', got '$resp_msg'"));
265
266 $client->quit();
267 };
268 if ($@) {
269 $ex = $@;
270 }
271
272 $wfh->print("done\n");
273 $wfh->flush();
274
275 } else {
276 eval { server_wait($setup->{config_file}, $rfh) };
277 if ($@) {
278 warn($@);
279 exit 1;
280 }
281
282 exit 0;
283 }
284
285 # Stop server
286 server_stop($setup->{pid_file});
287 $self->assert_child_ok($pid);
288
289 if ($ex) {
290 test_cleanup($setup->{log_file}, $ex);
291 die($ex);
292 }
293
294 # Examine the Trace messages for the 'data' channel, to see what buffer
295 # sizes were allocated for this download
296 eval {
297 if (open(my $fh, "< $setup->{log_file}")) {
298 while (my $line = <$fh>) {
299 chomp($line);
300
301 if ($ENV{TEST_VERBOSE}) {
302 print STDERR "# $line\n";
303 }
304
305 if ($line =~ /<data\:\d+>\:\s+allocated\s+.*?\s+buffer\s+of\s+(\d+)\s+bytes/) {
306 my $bufsz = $1;
307
308 $self->assert($bufsz == $rcvbufsz,
309 test_msg("Expected $rcvbufsz got $bufsz"));
310
311 } elsif ($line =~ /<data\:\d+>\:\s+set\s+socket\s+rcvbuf\s+of\s+(\d+)\s+bytes/) {
312 my $bufsz = $1;
313
314 $self->assert($bufsz == $rcvbufsz,
315 test_msg("Expected $rcvbufsz got $bufsz"));
316 }
317 }
318
319 close($fh);
320
321 } else {
322 die("Can't read $setup->{log_file}: $!");
323 }
324 };
325 if ($@) {
326 $ex = $@;
327 }
328
329 test_cleanup($setup->{log_file}, $ex);
330 }
331
332 sub socketoptions_sndbuf_bug3607 {
333 my $self = shift;
334 my $tmpdir = $self->{tmpdir};
335 my $setup = test_setup($tmpdir, 'config');
336
337 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
338 if (open(my $fh, "> $test_file")) {
339 print $fh "ABCD" x 8192;
340 unless (close($fh)) {
341 die("Can't write $test_file: $!");
342 }
343
344 } else {
345 die("Can't open $test_file: $!");
346 }
347
348 my $sndbufsz = 2502440;
349
350 my $config = {
351 PidFile => $setup->{pid_file},
352 ScoreboardFile => $setup->{scoreboard_file},
353 SystemLog => $setup->{log_file},
354 TraceLog => $setup->{log_file},
355 Trace => 'DEFAULT:0 data:10',
356
357 AuthUserFile => $setup->{auth_user_file},
358 AuthGroupFile => $setup->{auth_group_file},
359 AuthOrder => 'mod_auth_file.c',
360
361 SocketOptions => "sndbuf $sndbufsz",
362
363 IfModules => {
364 'mod_delay.c' => {
365 DelayEngine => 'off',
366 },
367 },
368 };
369
370 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
371 $config);
372
373 # Open pipes, for use between the parent and child processes. Specifically,
374 # the child will indicate when it's done with its test by writing a message
375 # to the parent.
376 my ($rfh, $wfh);
377 unless (pipe($rfh, $wfh)) {
378 die("Can't open pipe: $!");
379 }
380
381 my $ex;
382
383 # Fork child
384 $self->handle_sigchld();
385 defined(my $pid = fork()) or die("Can't fork: $!");
386 if ($pid) {
387 eval {
388 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
389 $client->login($setup->{user}, $setup->{passwd});
390
391 my $conn = $client->retr_raw('test.txt');
392 unless ($conn) {
393 die("Failed to RETR: " . $client->response_code() . " " .
394 $client->response_msg());
395 }
396
397 my $buf;
398 while ($conn->read($buf, 16384, 25)) {
399 }
400 eval { $conn->close() };
401
402 my $resp_code = $client->response_code();
403 my $resp_msg = $client->response_msg();
404
405 my $expected;
406
407 $expected = 226;
408 $self->assert($expected == $resp_code,
409 test_msg("Expected response code $expected, got $resp_code"));
410
411 $expected = "Transfer complete";
412 $self->assert($expected eq $resp_msg,
413 test_msg("Expected response message '$expected', got '$resp_msg'"));
414
415 $client->quit();
416 };
417 if ($@) {
418 $ex = $@;
419 }
420
421 $wfh->print("done\n");
422 $wfh->flush();
423
424 } else {
425 eval { server_wait($setup->{config_file}, $rfh) };
426 if ($@) {
427 warn($@);
428 exit 1;
429 }
430
431 exit 0;
432 }
433
434 # Stop server
435 server_stop($setup->{pid_file});
436 $self->assert_child_ok($pid);
437
438 if ($ex) {
439 test_cleanup($setup->{log_file}, $ex);
440 die($ex);
441 }
442
443 # Examine the Trace messages for the 'data' channel, to see what buffer
444 # sizes were allocated for this download
445 eval {
446 if (open(my $fh, "< $setup->{log_file}")) {
447 while (my $line = <$fh>) {
448 chomp($line);
449
450 if ($ENV{TEST_VERBOSE}) {
451 print STDERR "# $line\n";
452 }
453
454 if ($line =~ /<data\:\d+>\:\s+allocated\s+.*?\s+buffer\s+of\s+(\d+)\s+bytes/) {
455 my $bufsz = $1;
456
457 $self->assert($bufsz == $sndbufsz,
458 test_msg("Expected $sndbufsz got $bufsz"));
459
460 } elsif ($line =~ /<data\:\d+>\:\s+set\s+socket\s+sndbuf\s+of\s+(\d+)\s+bytes/) {
461 my $bufsz = $1;
462
463 $self->assert($bufsz == $sndbufsz,
464 test_msg("Expected $sndbufsz got $bufsz"));
465 }
466 }
467
468 close($fh);
469
470 } else {
471 die("Can't read $setup->{log_file}: $!");
472 }
473 };
474 if ($@) {
475 $ex = $@;
476 }
477
478 test_cleanup($setup->{log_file}, $ex);
479 }
480
481 sub socketoptions_keepalive_on {
482 my $self = shift;
483 my $tmpdir = $self->{tmpdir};
484 my $setup = test_setup($tmpdir, 'config');
485
486 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
487 if (open(my $fh, "> $test_file")) {
488 print $fh "ABCD" x 8192;
489 unless (close($fh)) {
490 die("Can't write $test_file: $!");
491 }
492
493 } else {
494 die("Can't open $test_file: $!");
495 }
496
497 # See:
498 # https://forums.proftpd.org/smf/index.php/topic,11514.0.html
499
500 my $config = {
501 PidFile => $setup->{pid_file},
502 ScoreboardFile => $setup->{scoreboard_file},
503 SystemLog => $setup->{log_file},
504 TraceLog => $setup->{log_file},
505 Trace => 'DEFAULT:0 data:10 inet:20',
506
507 AuthUserFile => $setup->{auth_user_file},
508 AuthGroupFile => $setup->{auth_group_file},
509 AuthOrder => 'mod_auth_file.c',
510
511 PassivePorts => "41200 43400",
512 SocketOptions => "keepalive on",
513
514 IfModules => {
515 'mod_delay.c' => {
516 DelayEngine => 'off',
517 },
518 },
519 };
520
521 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
522 $config);
523
524 # Open pipes, for use between the parent and child processes. Specifically,
525 # the child will indicate when it's done with its test by writing a message
526 # to the parent.
527 my ($rfh, $wfh);
528 unless (pipe($rfh, $wfh)) {
529 die("Can't open pipe: $!");
530 }
531
532 my $ex;
533
534 # Fork child
535 $self->handle_sigchld();
536 defined(my $pid = fork()) or die("Can't fork: $!");
537 if ($pid) {
538 eval {
539 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
540 $client->login($setup->{user}, $setup->{passwd});
541
542 for (my $i = 0; $i < 250; $i++) {
543 my $conn = $client->retr_raw('test.txt');
544 unless ($conn) {
545 die("Failed to RETR: " . $client->response_code() . " " .
546 $client->response_msg());
547 }
548
549 my $buf;
550 while ($conn->read($buf, 16384, 25)) {
551 }
552 eval { $conn->close() };
553
554 my $resp_code = $client->response_code();
555 my $resp_msg = $client->response_msg();
556 $self->assert_transfer_ok($resp_code, $resp_msg);
557 }
558
559 $client->quit();
560 };
561 if ($@) {
562 $ex = $@;
563 }
564
565 $wfh->print("done\n");
566 $wfh->flush();
567
568 } else {
569 eval { server_wait($setup->{config_file}, $rfh) };
570 if ($@) {
571 warn($@);
572 exit 1;
573 }
574
575 exit 0;
576 }
577
578 # Stop server
579 server_stop($setup->{pid_file});
580 $self->assert_child_ok($pid);
581
582 test_cleanup($setup->{log_file}, $ex);
583 }
584
585 sub socketoptions_keepalive_off {
586 my $self = shift;
587 my $tmpdir = $self->{tmpdir};
588 my $setup = test_setup($tmpdir, 'config');
589
590 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
591 if (open(my $fh, "> $test_file")) {
592 print $fh "ABCD" x 8192;
593 unless (close($fh)) {
594 die("Can't write $test_file: $!");
595 }
596
597 } else {
598 die("Can't open $test_file: $!");
599 }
600
601 my $config = {
602 PidFile => $setup->{pid_file},
603 ScoreboardFile => $setup->{scoreboard_file},
604 SystemLog => $setup->{log_file},
605 TraceLog => $setup->{log_file},
606 Trace => 'DEFAULT:0 data:10 inet:20',
607
608 AuthUserFile => $setup->{auth_user_file},
609 AuthGroupFile => $setup->{auth_group_file},
610 AuthOrder => 'mod_auth_file.c',
611
612 PassivePorts => "41200 43400",
613 SocketOptions => "keepalive off",
614
615 IfModules => {
616 'mod_delay.c' => {
617 DelayEngine => 'off',
618 },
619 },
620 };
621
622 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
623 $config);
624
625 # Open pipes, for use between the parent and child processes. Specifically,
626 # the child will indicate when it's done with its test by writing a message
627 # to the parent.
628 my ($rfh, $wfh);
629 unless (pipe($rfh, $wfh)) {
630 die("Can't open pipe: $!");
631 }
632
633 my $ex;
634
635 # Fork child
636 $self->handle_sigchld();
637 defined(my $pid = fork()) or die("Can't fork: $!");
638 if ($pid) {
639 eval {
640 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
641 $client->login($setup->{user}, $setup->{passwd});
642
643 for (my $i = 0; $i < 5; $i++) {
644 my $conn = $client->retr_raw('test.txt');
645 unless ($conn) {
646 die("Failed to RETR: " . $client->response_code() . " " .
647 $client->response_msg());
648 }
649
650 my $buf;
651 while ($conn->read($buf, 16384, 25)) {
652 }
653 eval { $conn->close() };
654
655 my $resp_code = $client->response_code();
656 my $resp_msg = $client->response_msg();
657 $self->assert_transfer_ok($resp_code, $resp_msg);
658 }
659
660 $client->quit();
661 };
662 if ($@) {
663 $ex = $@;
664 }
665
666 $wfh->print("done\n");
667 $wfh->flush();
668
669 } else {
670 eval { server_wait($setup->{config_file}, $rfh) };
671 if ($@) {
672 warn($@);
673 exit 1;
674 }
675
676 exit 0;
677 }
678
679 # Stop server
680 server_stop($setup->{pid_file});
681 $self->assert_child_ok($pid);
682
683 test_cleanup($setup->{log_file}, $ex);
684 }
685
686 sub socketoptions_keepalive_details_issue1402 {
687 my $self = shift;
688 my $tmpdir = $self->{tmpdir};
689 my $setup = test_setup($tmpdir, 'config');
690
691 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
692 if (open(my $fh, "> $test_file")) {
693 print $fh "ABCD" x 8192;
694 unless (close($fh)) {
695 die("Can't write $test_file: $!");
696 }
697
698 } else {
699 die("Can't open $test_file: $!");
700 }
701
702 my $config = {
703 PidFile => $setup->{pid_file},
704 ScoreboardFile => $setup->{scoreboard_file},
705 SystemLog => $setup->{log_file},
706 TraceLog => $setup->{log_file},
707 Trace => 'DEFAULT:0 data:10 inet:20',
708
709 AuthUserFile => $setup->{auth_user_file},
710 AuthGroupFile => $setup->{auth_group_file},
711 AuthOrder => 'mod_auth_file.c',
712
713 SocketOptions => "keepalive 60:9:75",
714
715 IfModules => {
716 'mod_delay.c' => {
717 DelayEngine => 'off',
718 },
719 },
720 };
721
722 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
723 $config);
724
725 # Open pipes, for use between the parent and child processes. Specifically,
726 # the child will indicate when it's done with its test by writing a message
727 # to the parent.
728 my ($rfh, $wfh);
729 unless (pipe($rfh, $wfh)) {
730 die("Can't open pipe: $!");
731 }
732
733 my $ex;
734
735 # Fork child
736 $self->handle_sigchld();
737 defined(my $pid = fork()) or die("Can't fork: $!");
738 if ($pid) {
739 eval {
740 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
741 $client->login($setup->{user}, $setup->{passwd});
742 $client->quit();
743 };
744 if ($@) {
745 $ex = $@;
746 }
747
748 $wfh->print("done\n");
749 $wfh->flush();
750
751 } else {
752 eval { server_wait($setup->{config_file}, $rfh) };
753 if ($@) {
754 warn($@);
755 exit 1;
756 }
757
758 exit 0;
759 }
760
761 # Stop server
762 server_stop($setup->{pid_file});
763 $self->assert_child_ok($pid);
764
765 eval {
766 if (open(my $fh, "< $setup->{log_file}")) {
767 my $have_keep_idle = 0;
768 my $have_keep_count = 0;
769 my $have_keep_intvl = 0;
770
771 while (my $line = <$fh>) {
772 chomp($line);
773
774 next unless $line =~ /\<inet:15\>/;
775
776 if ($ENV{TEST_VERBOSE}) {
777 print STDERR "# $line\n";
778 }
779
780 if ($line =~ /enabled TCP_KEEPIDLE 60/) {
781 $have_keep_idle = 1;
782 next;
783 }
784
785 if ($line =~ /enabled TCP_KEEPCNT 9/) {
786 $have_keep_count = 1;
787 next;
788 }
789
790 if ($line =~ /enabled TCP_KEEPINTVL 75/) {
791 $have_keep_intvl = 1;
792 last;
793 }
794 }
795 close($fh);
796
797 $self->assert($have_keep_idle,
798 test_msg("Did not see expected TCP_KEEPIDLE TraceLog message"));
799 $self->assert($have_keep_count,
800 test_msg("Did not see expected TCP_KEEPCNT TraceLog message"));
801 $self->assert($have_keep_intvl,
802 test_msg("Did not see expected TCP_KEEPINTVL TraceLog message"));
803
804 } else {
805 die("Can't read $setup->{log_file}: $!");
806 }
807 };
808 if ($@) {
809 $ex = $@;
810 }
811
812 test_cleanup($setup->{log_file}, $ex);
813 }
814
815 1;