Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::UseSendfile;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Copy;
8 use File::Path qw(mkpath);
9 use File::Spec;
10 use IO::Handle;
11
12 use ProFTPD::TestSuite::FTP;
13 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
14
15 $| = 1;
16
17 my $order = 0;
18
19 my $TESTS = {
20 usesendfile_on_ascii => {
21 order => ++$order,
22 test_class => [qw(feat_sendfile forking)],
23 },
24
25 usesendfile_off_ascii => {
26 order => ++$order,
27 test_class => [qw(feat_sendfile forking)],
28 },
29
30 usesendfile_on_binary => {
31 order => ++$order,
32 test_class => [qw(feat_sendfile forking)],
33 },
34
35 usesendfile_off_binary => {
36 order => ++$order,
37 test_class => [qw(feat_sendfile forking)],
38 },
39
40 usesendfile_on_binary_dir_bug3310 => {
41 order => ++$order,
42 test_class => [qw(bug feat_sendfile forking)],
43 },
44
45 usesendfile_off_binary_dir_bug3310 => {
46 order => ++$order,
47 test_class => [qw(bug feat_sendfile forking)],
48 },
49
50 usesendfile_on_binary_ftpaccess_bug3310 => {
51 order => ++$order,
52 test_class => [qw(bug feat_sendfile forking)],
53 },
54
55 usesendfile_off_binary_ftpaccess_bug3310 => {
56 order => ++$order,
57 test_class => [qw(bug feat_sendfile forking)],
58 },
59
60 usesendfile_len_binary_dir_bug3310 => {
61 order => ++$order,
62 test_class => [qw(bug feat_sendfile forking)],
63 },
64
65 usesendfile_len_binary_ftpaccess_bug3310 => {
66 order => ++$order,
67 test_class => [qw(bug feat_sendfile forking)],
68 },
69
70 usesendfile_len_ascii_ftpaccess_bug3310 => {
71 order => ++$order,
72 test_class => [qw(bug feat_sendfile forking)],
73 },
74
75 usesendfile_pct_binary_dir_bug3310 => {
76 order => ++$order,
77 test_class => [qw(bug feat_sendfile forking)],
78 },
79
80 usesendfile_pct_binary_ftpaccess_bug3310 => {
81 order => ++$order,
82 test_class => [qw(bug feat_sendfile forking)],
83 },
84
85 };
86
87 sub new {
88 return shift()->SUPER::new(@_);
89 }
90
91 sub list_tests {
92 return testsuite_get_runnable_tests($TESTS);
93 }
94
95 sub usesendfile_on_ascii {
96 my $self = shift;
97 my $tmpdir = $self->{tmpdir};
98
99 my $config_file = "$tmpdir/config.conf";
100 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
101 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
102
103 my $log_file = File::Spec->rel2abs('tests.log');
104
105 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
106 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
107
108 my $user = 'proftpd';
109 my $passwd = 'test';
110 my $home_dir = File::Spec->rel2abs($tmpdir);
111 my $uid = 500;
112 my $gid = 500;
113
114 # Make sure that, if we're running as root, that the home directory has
115 # permissions/privs set for the account we create
116 if ($< == 0) {
117 unless (chmod(0755, $home_dir)) {
118 die("Can't set perms on $home_dir to 0755: $!");
119 }
120
121 unless (chown($uid, $gid, $home_dir)) {
122 die("Can't set owner of $home_dir to $uid/$gid: $!");
123 }
124 }
125
126 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
127 '/bin/bash');
128 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
129
130 my $test_file = File::Spec->rel2abs($config_file);
131
132 my $config = {
133 PidFile => $pid_file,
134 ScoreboardFile => $scoreboard_file,
135 SystemLog => $log_file,
136
137 # We explicitly use DebugLevel 10 here, to get the sendfile log
138 # message emitted by mod_xfer.
139 DebugLevel => 10,
140
141 AuthUserFile => $auth_user_file,
142 AuthGroupFile => $auth_group_file,
143 AuthOrder => 'mod_auth_file.c',
144
145 UseSendfile => 'on',
146
147 IfModules => {
148 'mod_delay.c' => {
149 DelayEngine => 'off',
150 },
151 },
152 };
153
154 my ($port, $config_user, $config_group) = config_write($config_file, $config);
155
156 # Open pipes, for use between the parent and child processes. Specifically,
157 # the child will indicate when it's done with its test by writing a message
158 # to the parent.
159 my ($rfh, $wfh);
160 unless (pipe($rfh, $wfh)) {
161 die("Can't open pipe: $!");
162 }
163
164 my $ex;
165
166 # Fork child
167 $self->handle_sigchld();
168 defined(my $pid = fork()) or die("Can't fork: $!");
169 if ($pid) {
170 eval {
171 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
172 $client->login($user, $passwd);
173
174 my $conn = $client->retr_raw($test_file);
175 unless ($conn) {
176 die("RETR failed: " . $client->response_code() . " " .
177 $client->response_msg());
178 }
179
180 my $buf;
181 while ($conn->read($buf, 8192, 30)) {
182 }
183 eval { $conn->close() };
184
185 my $resp_code = $client->response_code();
186 my $resp_msg = $client->response_msg();
187 $self->assert_transfer_ok($resp_code, $resp_msg);
188
189 $client->quit();
190 };
191
192 if ($@) {
193 $ex = $@;
194 }
195
196 $wfh->print("done\n");
197 $wfh->flush();
198
199 } else {
200 eval { server_wait($config_file, $rfh) };
201 if ($@) {
202 warn($@);
203 exit 1;
204 }
205
206 exit 0;
207 }
208
209 # Stop server
210 server_stop($pid_file);
211
212 $self->assert_child_ok($pid);
213
214 if (open(my $fh, "< $log_file")) {
215 my $ok = 0;
216
217 my $expected = 'declining use of sendfile for ASCII data';
218
219 while (my $line = <$fh>) {
220 chomp($line);
221
222 if ($line =~ /$expected/) {
223 $ok = 1;
224 last;
225 }
226 }
227
228 close($fh);
229
230 $self->assert($ok,
231 test_msg("Expected log message '$expected' did not appear in SystemLog"));
232
233 } else {
234 die("Can't read $log_file: $!");
235 }
236
237 if ($ex) {
238 die($ex);
239 }
240
241 unlink($log_file);
242 }
243
244 sub usesendfile_off_ascii {
245 my $self = shift;
246 my $tmpdir = $self->{tmpdir};
247
248 my $config_file = "$tmpdir/config.conf";
249 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
250 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
251
252 my $log_file = File::Spec->rel2abs('tests.log');
253
254 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
255 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
256
257 my $user = 'proftpd';
258 my $passwd = 'test';
259 my $home_dir = File::Spec->rel2abs($tmpdir);
260 my $uid = 500;
261 my $gid = 500;
262
263 # Make sure that, if we're running as root, that the home directory has
264 # permissions/privs set for the account we create
265 if ($< == 0) {
266 unless (chmod(0755, $home_dir)) {
267 die("Can't set perms on $home_dir to 0755: $!");
268 }
269
270 unless (chown($uid, $gid, $home_dir)) {
271 die("Can't set owner of $home_dir to $uid/$gid: $!");
272 }
273 }
274
275 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
276 '/bin/bash');
277 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
278
279 my $test_file = File::Spec->rel2abs($config_file);
280
281 my $config = {
282 PidFile => $pid_file,
283 ScoreboardFile => $scoreboard_file,
284 SystemLog => $log_file,
285
286 # We explicitly use DebugLevel 10 here, to get the sendfile log
287 # message emitted by mod_xfer.
288 DebugLevel => 10,
289
290 AuthUserFile => $auth_user_file,
291 AuthGroupFile => $auth_group_file,
292 AuthOrder => 'mod_auth_file.c',
293
294 UseSendfile => 'off',
295
296 IfModules => {
297 'mod_delay.c' => {
298 DelayEngine => 'off',
299 },
300 },
301 };
302
303 my ($port, $config_user, $config_group) = config_write($config_file, $config);
304
305 # Open pipes, for use between the parent and child processes. Specifically,
306 # the child will indicate when it's done with its test by writing a message
307 # to the parent.
308 my ($rfh, $wfh);
309 unless (pipe($rfh, $wfh)) {
310 die("Can't open pipe: $!");
311 }
312
313 my $ex;
314
315 # Fork child
316 $self->handle_sigchld();
317 defined(my $pid = fork()) or die("Can't fork: $!");
318 if ($pid) {
319 eval {
320 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
321 $client->login($user, $passwd);
322
323 my $conn = $client->retr_raw($test_file);
324 unless ($conn) {
325 die("RETR failed: " . $client->response_code() . " " .
326 $client->response_msg());
327 }
328
329 my $buf;
330 while ($conn->read($buf, 8192, 30)) {
331 }
332 eval { $conn->close() };
333
334 my $resp_code = $client->response_code();
335 my $resp_msg = $client->response_msg();
336 $self->assert_transfer_ok($resp_code, $resp_msg);
337
338 $client->quit();
339 };
340
341 if ($@) {
342 $ex = $@;
343 }
344
345 $wfh->print("done\n");
346 $wfh->flush();
347
348 } else {
349 eval { server_wait($config_file, $rfh) };
350 if ($@) {
351 warn($@);
352 exit 1;
353 }
354
355 exit 0;
356 }
357
358 # Stop server
359 server_stop($pid_file);
360
361 $self->assert_child_ok($pid);
362
363 if (open(my $fh, "< $log_file")) {
364 my $ok = 0;
365
366 my $expected = 'declining use of sendfile due to UseSendfile configuration';
367
368 while (my $line = <$fh>) {
369 chomp($line);
370
371 if ($line =~ /$expected/) {
372 $ok = 1;
373 last;
374 }
375 }
376
377 close($fh);
378
379 $self->assert($ok,
380 test_msg("Expected log message '$expected' did not appear in SystemLog"));
381
382 } else {
383 die("Can't read $log_file: $!");
384 }
385
386 if ($ex) {
387 die($ex);
388 }
389
390 unlink($log_file);
391 }
392
393 sub usesendfile_on_binary {
394 my $self = shift;
395 my $tmpdir = $self->{tmpdir};
396
397 my $config_file = "$tmpdir/config.conf";
398 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
399 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
400
401 my $log_file = File::Spec->rel2abs('tests.log');
402
403 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
404 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
405
406 my $user = 'proftpd';
407 my $passwd = 'test';
408 my $home_dir = File::Spec->rel2abs($tmpdir);
409 my $uid = 500;
410 my $gid = 500;
411
412 # Make sure that, if we're running as root, that the home directory has
413 # permissions/privs set for the account we create
414 if ($< == 0) {
415 unless (chmod(0755, $home_dir)) {
416 die("Can't set perms on $home_dir to 0755: $!");
417 }
418
419 unless (chown($uid, $gid, $home_dir)) {
420 die("Can't set owner of $home_dir to $uid/$gid: $!");
421 }
422 }
423
424 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
425 '/bin/bash');
426 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
427
428 my $test_file = File::Spec->rel2abs($config_file);
429
430 my $config = {
431 PidFile => $pid_file,
432 ScoreboardFile => $scoreboard_file,
433 SystemLog => $log_file,
434
435 # We explicitly use DebugLevel 10 here, to get the sendfile log
436 # message emitted by mod_xfer.
437 DebugLevel => 10,
438
439 AuthUserFile => $auth_user_file,
440 AuthGroupFile => $auth_group_file,
441 AuthOrder => 'mod_auth_file.c',
442
443 UseSendfile => 'on',
444
445 IfModules => {
446 'mod_delay.c' => {
447 DelayEngine => 'off',
448 },
449 },
450 };
451
452 my ($port, $config_user, $config_group) = config_write($config_file, $config);
453
454 # Open pipes, for use between the parent and child processes. Specifically,
455 # the child will indicate when it's done with its test by writing a message
456 # to the parent.
457 my ($rfh, $wfh);
458 unless (pipe($rfh, $wfh)) {
459 die("Can't open pipe: $!");
460 }
461
462 my $ex;
463
464 # Fork child
465 $self->handle_sigchld();
466 defined(my $pid = fork()) or die("Can't fork: $!");
467 if ($pid) {
468 eval {
469 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
470 $client->login($user, $passwd);
471 $client->type('binary');
472
473 my $conn = $client->retr_raw($test_file);
474 unless ($conn) {
475 die("RETR failed: " . $client->response_code() . " " .
476 $client->response_msg());
477 }
478
479 my $buf;
480 while ($conn->read($buf, 8192, 30)) {
481 }
482 eval { $conn->close() };
483
484 my $resp_code = $client->response_code();
485 my $resp_msg = $client->response_msg();
486 $self->assert_transfer_ok($resp_code, $resp_msg);
487
488 $client->quit();
489 };
490
491 if ($@) {
492 $ex = $@;
493 }
494
495 $wfh->print("done\n");
496 $wfh->flush();
497
498 } else {
499 eval { server_wait($config_file, $rfh) };
500 if ($@) {
501 warn($@);
502 exit 1;
503 }
504
505 exit 0;
506 }
507
508 # Stop server
509 server_stop($pid_file);
510
511 $self->assert_child_ok($pid);
512
513 if (open(my $fh, "< $log_file")) {
514 my $ok = 0;
515
516 my $expected = 'using sendfile capability for transmitting data';
517
518 while (my $line = <$fh>) {
519 chomp($line);
520
521 if ($line =~ /$expected/) {
522 $ok = 1;
523 last;
524 }
525 }
526
527 close($fh);
528
529 $self->assert($ok,
530 test_msg("Expected log message '$expected' did not appear in SystemLog"));
531
532 } else {
533 die("Can't read $log_file: $!");
534 }
535
536 if ($ex) {
537 die($ex);
538 }
539
540 unlink($log_file);
541 }
542
543 sub usesendfile_off_binary {
544 my $self = shift;
545 my $tmpdir = $self->{tmpdir};
546
547 my $config_file = "$tmpdir/config.conf";
548 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
549 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
550
551 my $log_file = File::Spec->rel2abs('tests.log');
552
553 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
554 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
555
556 my $user = 'proftpd';
557 my $passwd = 'test';
558 my $home_dir = File::Spec->rel2abs($tmpdir);
559 my $uid = 500;
560 my $gid = 500;
561
562 # Make sure that, if we're running as root, that the home directory has
563 # permissions/privs set for the account we create
564 if ($< == 0) {
565 unless (chmod(0755, $home_dir)) {
566 die("Can't set perms on $home_dir to 0755: $!");
567 }
568
569 unless (chown($uid, $gid, $home_dir)) {
570 die("Can't set owner of $home_dir to $uid/$gid: $!");
571 }
572 }
573
574 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
575 '/bin/bash');
576 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
577
578 my $test_file = File::Spec->rel2abs($config_file);
579
580 my $config = {
581 PidFile => $pid_file,
582 ScoreboardFile => $scoreboard_file,
583 SystemLog => $log_file,
584
585 # We explicitly use DebugLevel 10 here, to get the sendfile log
586 # message emitted by mod_xfer.
587 DebugLevel => 10,
588
589 AuthUserFile => $auth_user_file,
590 AuthGroupFile => $auth_group_file,
591 AuthOrder => 'mod_auth_file.c',
592
593 UseSendfile => 'off',
594
595 IfModules => {
596 'mod_delay.c' => {
597 DelayEngine => 'off',
598 },
599 },
600 };
601
602 my ($port, $config_user, $config_group) = config_write($config_file, $config);
603
604 # Open pipes, for use between the parent and child processes. Specifically,
605 # the child will indicate when it's done with its test by writing a message
606 # to the parent.
607 my ($rfh, $wfh);
608 unless (pipe($rfh, $wfh)) {
609 die("Can't open pipe: $!");
610 }
611
612 my $ex;
613
614 # Fork child
615 $self->handle_sigchld();
616 defined(my $pid = fork()) or die("Can't fork: $!");
617 if ($pid) {
618 eval {
619 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
620 $client->login($user, $passwd);
621 $client->type('binary');
622
623 my $conn = $client->retr_raw($test_file);
624 unless ($conn) {
625 die("RETR failed: " . $client->response_code() . " " .
626 $client->response_msg());
627 }
628
629 my $buf;
630 while ($conn->read($buf, 8192, 30)) {
631 }
632 eval { $conn->close() };
633
634 my $resp_code = $client->response_code();
635 my $resp_msg = $client->response_msg();
636 $self->assert_transfer_ok($resp_code, $resp_msg);
637
638 $client->quit();
639 };
640
641 if ($@) {
642 $ex = $@;
643 }
644
645 $wfh->print("done\n");
646 $wfh->flush();
647
648 } else {
649 eval { server_wait($config_file, $rfh) };
650 if ($@) {
651 warn($@);
652 exit 1;
653 }
654
655 exit 0;
656 }
657
658 # Stop server
659 server_stop($pid_file);
660
661 $self->assert_child_ok($pid);
662
663 if (open(my $fh, "< $log_file")) {
664 my $ok = 0;
665
666 my $expected = 'declining use of sendfile due to UseSendfile configuration';
667
668 while (my $line = <$fh>) {
669 chomp($line);
670
671 if ($line =~ /$expected/) {
672 $ok = 1;
673 last;
674 }
675 }
676
677 close($fh);
678
679 $self->assert($ok,
680 test_msg("Expected log message '$expected' did not appear in SystemLog"));
681
682 } else {
683 die("Can't read $log_file: $!");
684 }
685
686 if ($ex) {
687 die($ex);
688 }
689
690 unlink($log_file);
691 }
692
693 sub usesendfile_on_binary_dir_bug3310 {
694 my $self = shift;
695 my $tmpdir = $self->{tmpdir};
696
697 my $config_file = "$tmpdir/config.conf";
698 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
699 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
700
701 my $log_file = File::Spec->rel2abs('tests.log');
702
703 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
704 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
705
706 my $user = 'proftpd';
707 my $passwd = 'test';
708 my $home_dir = File::Spec->rel2abs($tmpdir);
709 my $uid = 500;
710 my $gid = 500;
711
712 # Make sure that, if we're running as root, that the home directory has
713 # permissions/privs set for the account we create
714 if ($< == 0) {
715 unless (chmod(0755, $home_dir)) {
716 die("Can't set perms on $home_dir to 0755: $!");
717 }
718
719 unless (chown($uid, $gid, $home_dir)) {
720 die("Can't set owner of $home_dir to $uid/$gid: $!");
721 }
722 }
723
724 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
725 '/bin/bash');
726 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
727
728 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
729 mkpath($sub_dir);
730
731 my $config = {
732 PidFile => $pid_file,
733 ScoreboardFile => $scoreboard_file,
734 SystemLog => $log_file,
735
736 # We explicitly use DebugLevel 10 here, to get the sendfile log
737 # message emitted by mod_xfer.
738 DebugLevel => 10,
739
740 AuthUserFile => $auth_user_file,
741 AuthGroupFile => $auth_group_file,
742 AuthOrder => 'mod_auth_file.c',
743
744 IfModules => {
745 'mod_delay.c' => {
746 DelayEngine => 'off',
747 },
748 },
749 };
750
751 my ($port, $config_user, $config_group) = config_write($config_file, $config);
752
753 if (open(my $fh, ">> $config_file")) {
754 print $fh <<EOC;
755 <Directory ~>
756 UseSendfile off
757 </Directory>
758
759 <Directory ~/foo>
760 UseSendfile on
761 </Directory>
762 EOC
763
764 unless (close($fh)) {
765 die("Can't write $config_file: $!");
766 }
767
768 } else {
769 die("Can't open $config_file: $!");
770 }
771
772 my $path_without_sendfile = File::Spec->rel2abs($config_file);
773
774 # We copy the config file here, *after* it has been written out.
775 my $path_with_sendfile = File::Spec->rel2abs("$sub_dir/test.dat");
776 unless (copy($config_file, $path_with_sendfile)) {
777 die("Can't copy '$config_file' to '$path_with_sendfile': $!");
778 }
779
780 # Open pipes, for use between the parent and child processes. Specifically,
781 # the child will indicate when it's done with its test by writing a message
782 # to the parent.
783 my ($rfh, $wfh);
784 unless (pipe($rfh, $wfh)) {
785 die("Can't open pipe: $!");
786 }
787
788 my $ex;
789
790 # Fork child
791 $self->handle_sigchld();
792 defined(my $pid = fork()) or die("Can't fork: $!");
793 if ($pid) {
794 eval {
795 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
796 $client->login($user, $passwd);
797 $client->type('binary');
798
799 my ($buf, $conn, $expected, $resp_code, $resp_msg);
800
801 $conn = $client->retr_raw($path_without_sendfile);
802 unless ($conn) {
803 die("RETR failed: " . $client->response_code() . " " .
804 $client->response_msg());
805 }
806
807 while ($conn->read($buf, 8192, 30)) {
808 }
809 eval { $conn->close() };
810
811 $resp_code = $client->response_code();
812 $resp_msg = $client->response_msg();
813 $self->assert_transfer_ok($resp_code, $resp_msg);
814
815 $conn = $client->retr_raw($path_with_sendfile);
816 unless ($conn) {
817 die("RETR failed: " . $client->response_code() . " " .
818 $client->response_msg());
819 }
820
821 while ($conn->read($buf, 8192, 30)) {
822 }
823 eval { $conn->close() };
824
825 $resp_code = $client->response_code();
826 $resp_msg = $client->response_msg();
827 $self->assert_transfer_ok($resp_code, $resp_msg);
828
829 $client->quit();
830 };
831
832 if ($@) {
833 $ex = $@;
834 }
835
836 $wfh->print("done\n");
837 $wfh->flush();
838
839 } else {
840 eval { server_wait($config_file, $rfh) };
841 if ($@) {
842 warn($@);
843 exit 1;
844 }
845
846 exit 0;
847 }
848
849 # Stop server
850 server_stop($pid_file);
851
852 $self->assert_child_ok($pid);
853
854 if (open(my $fh, "< $log_file")) {
855 my $with_sendfile_ok = 0;
856 my $without_sendfile_ok = 0;
857
858 my $expected_with_sendfile = 'using sendfile capability for transmitting data';
859 my $expected_without_sendfile = 'declining use of sendfile due to UseSendfile configuration';
860
861 while (my $line = <$fh>) {
862 chomp($line);
863
864 # Look for the "without sendfile" log message first; only if that
865 # is found will we look for the "with sendfile" log message.
866 if ($with_sendfile_ok == 0 &&
867 $line =~ /$expected_without_sendfile/) {
868 $without_sendfile_ok = 1;
869 }
870
871 if ($without_sendfile_ok == 1 &&
872 $line =~ /$expected_with_sendfile/) {
873 $with_sendfile_ok = 1;
874 last;
875 }
876 }
877
878 close($fh);
879
880 $self->assert($without_sendfile_ok,
881 test_msg("Expected log message '$expected_without_sendfile' did not appear in SystemLog"));
882
883 $self->assert($with_sendfile_ok,
884 test_msg("Expected log message '$expected_with_sendfile' did not appear in SystemLog"));
885
886 } else {
887 die("Can't read $log_file: $!");
888 }
889
890 if ($ex) {
891 die($ex);
892 }
893
894 unlink($log_file);
895 }
896
897 sub usesendfile_off_binary_dir_bug3310 {
898 my $self = shift;
899 my $tmpdir = $self->{tmpdir};
900
901 my $config_file = "$tmpdir/config.conf";
902 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
903 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
904
905 my $log_file = File::Spec->rel2abs('tests.log');
906
907 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
908 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
909
910 my $user = 'proftpd';
911 my $passwd = 'test';
912 my $home_dir = File::Spec->rel2abs($tmpdir);
913 my $uid = 500;
914 my $gid = 500;
915
916 # Make sure that, if we're running as root, that the home directory has
917 # permissions/privs set for the account we create
918 if ($< == 0) {
919 unless (chmod(0755, $home_dir)) {
920 die("Can't set perms on $home_dir to 0755: $!");
921 }
922
923 unless (chown($uid, $gid, $home_dir)) {
924 die("Can't set owner of $home_dir to $uid/$gid: $!");
925 }
926 }
927
928 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
929 '/bin/bash');
930 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
931
932 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
933 mkpath($sub_dir);
934
935 my $config = {
936 PidFile => $pid_file,
937 ScoreboardFile => $scoreboard_file,
938 SystemLog => $log_file,
939
940 # We explicitly use DebugLevel 10 here, to get the sendfile log
941 # message emitted by mod_xfer.
942 DebugLevel => 10,
943
944 AuthUserFile => $auth_user_file,
945 AuthGroupFile => $auth_group_file,
946 AuthOrder => 'mod_auth_file.c',
947
948 IfModules => {
949 'mod_delay.c' => {
950 DelayEngine => 'off',
951 },
952 },
953 };
954
955 my ($port, $config_user, $config_group) = config_write($config_file, $config);
956
957 if (open(my $fh, ">> $config_file")) {
958 print $fh <<EOC;
959 <Directory ~>
960 UseSendfile on
961 </Directory>
962
963 <Directory ~/foo>
964 UseSendfile off
965 </Directory>
966 EOC
967
968 unless (close($fh)) {
969 die("Can't write $config_file: $!");
970 }
971
972 } else {
973 die("Can't open $config_file: $!");
974 }
975
976 my $path_with_sendfile = File::Spec->rel2abs($config_file);
977
978 # We copy the config file here, *after* it has been written out.
979 my $path_without_sendfile = File::Spec->rel2abs("$sub_dir/test.dat");
980 unless (copy($config_file, $path_without_sendfile)) {
981 die("Can't copy '$config_file' to '$path_without_sendfile': $!");
982 }
983
984 # Open pipes, for use between the parent and child processes. Specifically,
985 # the child will indicate when it's done with its test by writing a message
986 # to the parent.
987 my ($rfh, $wfh);
988 unless (pipe($rfh, $wfh)) {
989 die("Can't open pipe: $!");
990 }
991
992 my $ex;
993
994 # Fork child
995 $self->handle_sigchld();
996 defined(my $pid = fork()) or die("Can't fork: $!");
997 if ($pid) {
998 eval {
999 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1000 $client->login($user, $passwd);
1001 $client->type('binary');
1002
1003 my ($buf, $conn, $expected, $resp_code, $resp_msg);
1004
1005 $conn = $client->retr_raw($path_with_sendfile);
1006 unless ($conn) {
1007 die("RETR failed: " . $client->response_code() . " " .
1008 $client->response_msg());
1009 }
1010
1011 while ($conn->read($buf, 8192, 30)) {
1012 }
1013 eval { $conn->close() };
1014
1015 $resp_code = $client->response_code();
1016 $resp_msg = $client->response_msg();
1017 $self->assert_transfer_ok($resp_code, $resp_msg);
1018
1019 $conn = $client->retr_raw($path_without_sendfile);
1020 unless ($conn) {
1021 die("RETR failed: " . $client->response_code() . " " .
1022 $client->response_msg());
1023 }
1024
1025 while ($conn->read($buf, 8192, 30)) {
1026 }
1027 eval { $conn->close() };
1028
1029 $resp_code = $client->response_code();
1030 $resp_msg = $client->response_msg();
1031 $self->assert_transfer_ok($resp_code, $resp_msg);
1032
1033 $client->quit();
1034 };
1035
1036 if ($@) {
1037 $ex = $@;
1038 }
1039
1040 $wfh->print("done\n");
1041 $wfh->flush();
1042
1043 } else {
1044 eval { server_wait($config_file, $rfh) };
1045 if ($@) {
1046 warn($@);
1047 exit 1;
1048 }
1049
1050 exit 0;
1051 }
1052
1053 # Stop server
1054 server_stop($pid_file);
1055
1056 $self->assert_child_ok($pid);
1057
1058 if (open(my $fh, "< $log_file")) {
1059 my $with_sendfile_ok = 0;
1060 my $without_sendfile_ok = 0;
1061
1062 my $expected_with_sendfile = 'using sendfile capability for transmitting data';
1063 my $expected_without_sendfile = 'declining use of sendfile due to UseSendfile configuration';
1064
1065 while (my $line = <$fh>) {
1066 chomp($line);
1067
1068 # Look for the "with sendfile" log message first; only if that
1069 # is found will we look for the "without sendfile" log message.
1070 if ($without_sendfile_ok == 0 &&
1071 $line =~ /$expected_with_sendfile/) {
1072 $with_sendfile_ok = 1;
1073 }
1074
1075 if ($with_sendfile_ok == 1 &&
1076 $line =~ /$expected_without_sendfile/) {
1077 $without_sendfile_ok = 1;
1078 last;
1079 }
1080 }
1081
1082 close($fh);
1083
1084 $self->assert($with_sendfile_ok,
1085 test_msg("Expected log message '$expected_with_sendfile' did not appear in SystemLog"));
1086
1087 $self->assert($without_sendfile_ok,
1088 test_msg("Expected log message '$expected_without_sendfile' did not appear in SystemLog"));
1089
1090 } else {
1091 die("Can't read $log_file: $!");
1092 }
1093
1094 if ($ex) {
1095 die($ex);
1096 }
1097
1098 unlink($log_file);
1099 }
1100
1101 sub usesendfile_on_binary_ftpaccess_bug3310 {
1102 my $self = shift;
1103 my $tmpdir = $self->{tmpdir};
1104
1105 my $config_file = "$tmpdir/config.conf";
1106 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
1107 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
1108
1109 my $log_file = File::Spec->rel2abs('tests.log');
1110
1111 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
1112 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
1113
1114 my $user = 'proftpd';
1115 my $passwd = 'test';
1116 my $home_dir = File::Spec->rel2abs($tmpdir);
1117 my $uid = 500;
1118 my $gid = 500;
1119
1120 # Make sure that, if we're running as root, that the home directory has
1121 # permissions/privs set for the account we create
1122 if ($< == 0) {
1123 unless (chmod(0755, $home_dir)) {
1124 die("Can't set perms on $home_dir to 0755: $!");
1125 }
1126
1127 unless (chown($uid, $gid, $home_dir)) {
1128 die("Can't set owner of $home_dir to $uid/$gid: $!");
1129 }
1130 }
1131
1132 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1133 '/bin/bash');
1134 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
1135
1136 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
1137 mkpath($sub_dir);
1138
1139 my $ftpaccess_file = File::Spec->rel2abs("$sub_dir/.ftpaccess");
1140 if (open(my $fh, "> $ftpaccess_file")) {
1141 print $fh "UseSendfile on\n";
1142
1143 unless (close($fh)) {
1144 die("Can't write $ftpaccess_file: $!");
1145 }
1146
1147 } else {
1148 die("Can't open $ftpaccess_file: $!");
1149 }
1150
1151 my $config = {
1152 PidFile => $pid_file,
1153 ScoreboardFile => $scoreboard_file,
1154 SystemLog => $log_file,
1155
1156 # We explicitly use DebugLevel 10 here, to get the sendfile log
1157 # message emitted by mod_xfer.
1158 DebugLevel => 10,
1159
1160 AuthUserFile => $auth_user_file,
1161 AuthGroupFile => $auth_group_file,
1162 AuthOrder => 'mod_auth_file.c',
1163
1164 AllowOverride => 'on',
1165
1166 IfModules => {
1167 'mod_delay.c' => {
1168 DelayEngine => 'off',
1169 },
1170 },
1171 };
1172
1173 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1174
1175 if (open(my $fh, ">> $config_file")) {
1176 print $fh <<EOC;
1177 <Directory ~>
1178 UseSendfile off
1179 </Directory>
1180 EOC
1181
1182 unless (close($fh)) {
1183 die("Can't write $config_file: $!");
1184 }
1185
1186 } else {
1187 die("Can't open $config_file: $!");
1188 }
1189
1190 my $path_without_sendfile = File::Spec->rel2abs($config_file);
1191
1192 # We copy the config file here, *after* it has been written out.
1193 my $path_with_sendfile = File::Spec->rel2abs("$sub_dir/test.dat");
1194 unless (copy($config_file, $path_with_sendfile)) {
1195 die("Can't copy '$config_file' to '$path_with_sendfile': $!");
1196 }
1197
1198 # Open pipes, for use between the parent and child processes. Specifically,
1199 # the child will indicate when it's done with its test by writing a message
1200 # to the parent.
1201 my ($rfh, $wfh);
1202 unless (pipe($rfh, $wfh)) {
1203 die("Can't open pipe: $!");
1204 }
1205
1206 my $ex;
1207
1208 # Fork child
1209 $self->handle_sigchld();
1210 defined(my $pid = fork()) or die("Can't fork: $!");
1211 if ($pid) {
1212 eval {
1213 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1214 $client->login($user, $passwd);
1215 $client->type('binary');
1216
1217 my ($buf, $conn, $expected, $resp_code, $resp_msg);
1218
1219 $conn = $client->retr_raw($path_without_sendfile);
1220 unless ($conn) {
1221 die("RETR failed: " . $client->response_code() . " " .
1222 $client->response_msg());
1223 }
1224
1225 while ($conn->read($buf, 8192, 30)) {
1226 }
1227 eval { $conn->close() };
1228
1229 $resp_code = $client->response_code();
1230 $resp_msg = $client->response_msg();
1231 $self->assert_transfer_ok($resp_code, $resp_msg);
1232
1233 $conn = $client->retr_raw($path_with_sendfile);
1234 unless ($conn) {
1235 die("RETR failed: " . $client->response_code() . " " .
1236 $client->response_msg());
1237 }
1238
1239 while ($conn->read($buf, 8192, 30)) {
1240 }
1241 eval { $conn->close() };
1242
1243 $resp_code = $client->response_code();
1244 $resp_msg = $client->response_msg();
1245 $self->assert_transfer_ok($resp_code, $resp_msg);
1246
1247 $client->quit();
1248 };
1249
1250 if ($@) {
1251 $ex = $@;
1252 }
1253
1254 $wfh->print("done\n");
1255 $wfh->flush();
1256
1257 } else {
1258 eval { server_wait($config_file, $rfh) };
1259 if ($@) {
1260 warn($@);
1261 exit 1;
1262 }
1263
1264 exit 0;
1265 }
1266
1267 # Stop server
1268 server_stop($pid_file);
1269
1270 $self->assert_child_ok($pid);
1271
1272 if (open(my $fh, "< $log_file")) {
1273 my $with_sendfile_ok = 0;
1274 my $without_sendfile_ok = 0;
1275
1276 my $expected_with_sendfile = 'using sendfile capability for transmitting data';
1277 my $expected_without_sendfile = 'declining use of sendfile due to UseSendfile configuration';
1278
1279 while (my $line = <$fh>) {
1280 chomp($line);
1281
1282 # Look for the "without sendfile" log message first; only if that
1283 # is found will we look for the "with sendfile" log message.
1284 if ($with_sendfile_ok == 0 &&
1285 $line =~ /$expected_without_sendfile/) {
1286 $without_sendfile_ok = 1;
1287 }
1288
1289 if ($without_sendfile_ok == 1 &&
1290 $line =~ /$expected_with_sendfile/) {
1291 $with_sendfile_ok = 1;
1292 last;
1293 }
1294 }
1295
1296 close($fh);
1297
1298 $self->assert($without_sendfile_ok,
1299 test_msg("Expected log message '$expected_without_sendfile' did not appear in SystemLog"));
1300
1301 $self->assert($with_sendfile_ok,
1302 test_msg("Expected log message '$expected_with_sendfile' did not appear in SystemLog"));
1303
1304 } else {
1305 die("Can't read $log_file: $!");
1306 }
1307
1308 if ($ex) {
1309 die($ex);
1310 }
1311
1312 unlink($log_file);
1313 }
1314
1315 sub usesendfile_off_binary_ftpaccess_bug3310 {
1316 my $self = shift;
1317 my $tmpdir = $self->{tmpdir};
1318
1319 my $config_file = "$tmpdir/config.conf";
1320 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
1321 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
1322
1323 my $log_file = File::Spec->rel2abs('tests.log');
1324
1325 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
1326 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
1327
1328 my $user = 'proftpd';
1329 my $passwd = 'test';
1330 my $home_dir = File::Spec->rel2abs($tmpdir);
1331 my $uid = 500;
1332 my $gid = 500;
1333
1334 # Make sure that, if we're running as root, that the home directory has
1335 # permissions/privs set for the account we create
1336 if ($< == 0) {
1337 unless (chmod(0755, $home_dir)) {
1338 die("Can't set perms on $home_dir to 0755: $!");
1339 }
1340
1341 unless (chown($uid, $gid, $home_dir)) {
1342 die("Can't set owner of $home_dir to $uid/$gid: $!");
1343 }
1344 }
1345
1346 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1347 '/bin/bash');
1348 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
1349
1350 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
1351 mkpath($sub_dir);
1352
1353 my $ftpaccess_file = File::Spec->rel2abs("$sub_dir/.ftpaccess");
1354 if (open(my $fh, "> $ftpaccess_file")) {
1355 print $fh "UseSendfile off\n";
1356 unless (close($fh)) {
1357 die("Can't write $ftpaccess_file: $!");
1358 }
1359
1360 } else {
1361 die("Can't open $ftpaccess_file: $!");
1362 }
1363
1364 my $config = {
1365 PidFile => $pid_file,
1366 ScoreboardFile => $scoreboard_file,
1367 SystemLog => $log_file,
1368
1369 # We explicitly use DebugLevel 10 here, to get the sendfile log
1370 # message emitted by mod_xfer.
1371 DebugLevel => 10,
1372
1373 AuthUserFile => $auth_user_file,
1374 AuthGroupFile => $auth_group_file,
1375 AuthOrder => 'mod_auth_file.c',
1376
1377 AllowOverride => 'on',
1378
1379 IfModules => {
1380 'mod_delay.c' => {
1381 DelayEngine => 'off',
1382 },
1383 },
1384 };
1385
1386 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1387
1388 if (open(my $fh, ">> $config_file")) {
1389 print $fh <<EOC;
1390 <Directory ~>
1391 UseSendfile on
1392 </Directory>
1393 EOC
1394
1395 unless (close($fh)) {
1396 die("Can't write $config_file: $!");
1397 }
1398
1399 } else {
1400 die("Can't open $config_file: $!");
1401 }
1402
1403 my $path_with_sendfile = File::Spec->rel2abs($config_file);
1404
1405 # We copy the config file here, *after* it has been written out.
1406 my $path_without_sendfile = File::Spec->rel2abs("$sub_dir/test.dat");
1407 unless (copy($config_file, $path_without_sendfile)) {
1408 die("Can't copy '$config_file' to '$path_without_sendfile': $!");
1409 }
1410
1411 # Open pipes, for use between the parent and child processes. Specifically,
1412 # the child will indicate when it's done with its test by writing a message
1413 # to the parent.
1414 my ($rfh, $wfh);
1415 unless (pipe($rfh, $wfh)) {
1416 die("Can't open pipe: $!");
1417 }
1418
1419 my $ex;
1420
1421 # Fork child
1422 $self->handle_sigchld();
1423 defined(my $pid = fork()) or die("Can't fork: $!");
1424 if ($pid) {
1425 eval {
1426 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1427 $client->login($user, $passwd);
1428 $client->type('binary');
1429
1430 my ($buf, $conn, $expected, $resp_code, $resp_msg);
1431
1432 $conn = $client->retr_raw($path_with_sendfile);
1433 unless ($conn) {
1434 die("RETR failed: " . $client->response_code() . " " .
1435 $client->response_msg());
1436 }
1437
1438 while ($conn->read($buf, 8192, 30)) {
1439 }
1440 eval { $conn->close() };
1441
1442 $resp_code = $client->response_code();
1443 $resp_msg = $client->response_msg();
1444 $self->assert_transfer_ok($resp_code, $resp_msg);
1445
1446 $conn = $client->retr_raw($path_without_sendfile);
1447 unless ($conn) {
1448 die("RETR failed: " . $client->response_code() . " " .
1449 $client->response_msg());
1450 }
1451
1452 while ($conn->read($buf, 8192, 30)) {
1453 }
1454 eval { $conn->close() };
1455
1456 $resp_code = $client->response_code();
1457 $resp_msg = $client->response_msg();
1458 $self->assert_transfer_ok($resp_code, $resp_msg);
1459
1460 $client->quit();
1461 };
1462
1463 if ($@) {
1464 $ex = $@;
1465 }
1466
1467 $wfh->print("done\n");
1468 $wfh->flush();
1469
1470 } else {
1471 eval { server_wait($config_file, $rfh) };
1472 if ($@) {
1473 warn($@);
1474 exit 1;
1475 }
1476
1477 exit 0;
1478 }
1479
1480 # Stop server
1481 server_stop($pid_file);
1482
1483 $self->assert_child_ok($pid);
1484
1485 if (open(my $fh, "< $log_file")) {
1486 my $with_sendfile_ok = 0;
1487 my $without_sendfile_ok = 0;
1488
1489 my $expected_with_sendfile = 'using sendfile capability for transmitting data';
1490 my $expected_without_sendfile = 'declining use of sendfile due to UseSendfile configuration';
1491
1492 while (my $line = <$fh>) {
1493 chomp($line);
1494
1495 # Look for the "with sendfile" log message first; only if that
1496 # is found will we look for the "without sendfile" log message.
1497 if ($without_sendfile_ok == 0 &&
1498 $line =~ /$expected_with_sendfile/) {
1499 $with_sendfile_ok = 1;
1500 }
1501
1502 if ($with_sendfile_ok == 1 &&
1503 $line =~ /$expected_without_sendfile/) {
1504 $without_sendfile_ok = 1;
1505 last;
1506 }
1507 }
1508
1509 close($fh);
1510
1511 $self->assert($with_sendfile_ok,
1512 test_msg("Expected log message '$expected_with_sendfile' did not appear in SystemLog"));
1513
1514 $self->assert($without_sendfile_ok,
1515 test_msg("Expected log message '$expected_without_sendfile' did not appear in SystemLog"));
1516
1517 } else {
1518 die("Can't read $log_file: $!");
1519 }
1520
1521 if ($ex) {
1522 die($ex);
1523 }
1524
1525 unlink($log_file);
1526 }
1527
1528 sub usesendfile_len_binary_dir_bug3310 {
1529 my $self = shift;
1530 my $tmpdir = $self->{tmpdir};
1531
1532 my $config_file = "$tmpdir/config.conf";
1533 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
1534 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
1535
1536 my $log_file = File::Spec->rel2abs('tests.log');
1537
1538 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
1539 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
1540
1541 my $user = 'proftpd';
1542 my $passwd = 'test';
1543 my $home_dir = File::Spec->rel2abs($tmpdir);
1544 my $uid = 500;
1545 my $gid = 500;
1546
1547 # Make sure that, if we're running as root, that the home directory has
1548 # permissions/privs set for the account we create
1549 if ($< == 0) {
1550 unless (chmod(0755, $home_dir)) {
1551 die("Can't set perms on $home_dir to 0755: $!");
1552 }
1553
1554 unless (chown($uid, $gid, $home_dir)) {
1555 die("Can't set owner of $home_dir to $uid/$gid: $!");
1556 }
1557 }
1558
1559 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1560 '/bin/bash');
1561 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
1562
1563 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
1564 mkpath($sub_dir);
1565
1566 my $config = {
1567 PidFile => $pid_file,
1568 ScoreboardFile => $scoreboard_file,
1569 SystemLog => $log_file,
1570
1571 # We explicitly use DebugLevel 10 here, to get the sendfile log
1572 # message emitted by mod_xfer.
1573 DebugLevel => 10,
1574
1575 AuthUserFile => $auth_user_file,
1576 AuthGroupFile => $auth_group_file,
1577 AuthOrder => 'mod_auth_file.c',
1578
1579 IfModules => {
1580 'mod_delay.c' => {
1581 DelayEngine => 'off',
1582 },
1583 },
1584 };
1585
1586 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1587
1588 if (open(my $fh, ">> $config_file")) {
1589 print $fh <<EOC;
1590 <Directory ~>
1591 UseSendfile off
1592 </Directory>
1593
1594 <Directory ~/foo>
1595 UseSendfile 256 b
1596 </Directory>
1597 EOC
1598
1599 unless (close($fh)) {
1600 die("Can't write $config_file: $!");
1601 }
1602
1603 } else {
1604 die("Can't open $config_file: $!");
1605 }
1606
1607 my $path_without_sendfile = File::Spec->rel2abs($config_file);
1608
1609 # We copy the config file here, *after* it has been written out.
1610 my $path_with_sendfile = File::Spec->rel2abs("$sub_dir/test.dat");
1611 unless (copy($config_file, $path_with_sendfile)) {
1612 die("Can't copy '$config_file' to '$path_with_sendfile': $!");
1613 }
1614
1615 # Open pipes, for use between the parent and child processes. Specifically,
1616 # the child will indicate when it's done with its test by writing a message
1617 # to the parent.
1618 my ($rfh, $wfh);
1619 unless (pipe($rfh, $wfh)) {
1620 die("Can't open pipe: $!");
1621 }
1622
1623 my $ex;
1624
1625 # Fork child
1626 $self->handle_sigchld();
1627 defined(my $pid = fork()) or die("Can't fork: $!");
1628 if ($pid) {
1629 eval {
1630 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1631 $client->login($user, $passwd);
1632 $client->type('binary');
1633
1634 my ($buf, $conn, $expected, $resp_code, $resp_msg);
1635
1636 $conn = $client->retr_raw($path_without_sendfile);
1637 unless ($conn) {
1638 die("RETR failed: " . $client->response_code() . " " .
1639 $client->response_msg());
1640 }
1641
1642 while ($conn->read($buf, 8192, 30)) {
1643 }
1644 eval { $conn->close() };
1645
1646 $resp_code = $client->response_code();
1647 $resp_msg = $client->response_msg();
1648 $self->assert_transfer_ok($resp_code, $resp_msg);
1649
1650 $conn = $client->retr_raw($path_with_sendfile);
1651 unless ($conn) {
1652 die("RETR failed: " . $client->response_code() . " " .
1653 $client->response_msg());
1654 }
1655
1656 while ($conn->read($buf, 8192, 30)) {
1657 }
1658 eval { $conn->close() };
1659
1660 $resp_code = $client->response_code();
1661 $resp_msg = $client->response_msg();
1662 $self->assert_transfer_ok($resp_code, $resp_msg);
1663
1664 $client->quit();
1665 };
1666
1667 if ($@) {
1668 $ex = $@;
1669 }
1670
1671 $wfh->print("done\n");
1672 $wfh->flush();
1673
1674 } else {
1675 eval { server_wait($config_file, $rfh) };
1676 if ($@) {
1677 warn($@);
1678 exit 1;
1679 }
1680
1681 exit 0;
1682 }
1683
1684 # Stop server
1685 server_stop($pid_file);
1686
1687 $self->assert_child_ok($pid);
1688
1689 if (open(my $fh, "< $log_file")) {
1690 my $with_sendfile_ok = 0;
1691 my $with_sendfile_len_ok = 0;
1692 my $without_sendfile_ok = 0;
1693
1694 my $expected_with_sendfile = 'using sendfile capability for transmitting data';
1695 my $expected_with_sendfile_len = 'using sendfile with configured UseSendfile length';
1696 my $expected_without_sendfile = 'declining use of sendfile due to UseSendfile configuration';
1697
1698 while (my $line = <$fh>) {
1699 chomp($line);
1700
1701 # Look for the "without sendfile" log message first; only if that
1702 # is found will we look for the "with sendfile" log message.
1703 if ($with_sendfile_ok == 0 &&
1704 $line =~ /$expected_without_sendfile/) {
1705 $without_sendfile_ok = 1;
1706 }
1707
1708 if ($without_sendfile_ok == 1 &&
1709 $line =~ /$expected_with_sendfile/) {
1710 $with_sendfile_ok = 1;
1711 }
1712
1713 if ($with_sendfile_ok == 1 &&
1714 $line =~ /$expected_with_sendfile_len/) {
1715 $with_sendfile_len_ok = 1;
1716 last;
1717 }
1718 }
1719
1720 close($fh);
1721
1722 $self->assert($without_sendfile_ok,
1723 test_msg("Expected log message '$expected_without_sendfile' did not appear in SystemLog"));
1724
1725 $self->assert($with_sendfile_ok,
1726 test_msg("Expected log message '$expected_with_sendfile' did not appear in SystemLog"));
1727
1728 $self->assert($with_sendfile_len_ok,
1729 test_msg("Expected log message '$expected_with_sendfile_len' did not appear in SystemLog"));
1730
1731 } else {
1732 die("Can't read $log_file: $!");
1733 }
1734
1735 if ($ex) {
1736 die($ex);
1737 }
1738
1739 unlink($log_file);
1740 }
1741
1742 sub usesendfile_len_binary_ftpaccess_bug3310 {
1743 my $self = shift;
1744 my $tmpdir = $self->{tmpdir};
1745
1746 my $config_file = "$tmpdir/config.conf";
1747 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
1748 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
1749
1750 my $log_file = File::Spec->rel2abs('tests.log');
1751
1752 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
1753 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
1754
1755 my $user = 'proftpd';
1756 my $passwd = 'test';
1757 my $home_dir = File::Spec->rel2abs($tmpdir);
1758 my $uid = 500;
1759 my $gid = 500;
1760
1761 # Make sure that, if we're running as root, that the home directory has
1762 # permissions/privs set for the account we create
1763 if ($< == 0) {
1764 unless (chmod(0755, $home_dir)) {
1765 die("Can't set perms on $home_dir to 0755: $!");
1766 }
1767
1768 unless (chown($uid, $gid, $home_dir)) {
1769 die("Can't set owner of $home_dir to $uid/$gid: $!");
1770 }
1771 }
1772
1773 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1774 '/bin/bash');
1775 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
1776
1777 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
1778 mkpath($sub_dir);
1779
1780 my $ftpaccess_file = File::Spec->rel2abs("$sub_dir/.ftpaccess");
1781 if (open(my $fh, "> $ftpaccess_file")) {
1782 print $fh "UseSendfile 256 b\n";
1783 unless (close($fh)) {
1784 die("Can't write $ftpaccess_file: $!");
1785 }
1786
1787 } else {
1788 die("Can't open $ftpaccess_file: $!");
1789 }
1790
1791 my $config = {
1792 PidFile => $pid_file,
1793 ScoreboardFile => $scoreboard_file,
1794 SystemLog => $log_file,
1795
1796 # We explicitly use DebugLevel 10 here, to get the sendfile log
1797 # message emitted by mod_xfer.
1798 DebugLevel => 10,
1799
1800 AuthUserFile => $auth_user_file,
1801 AuthGroupFile => $auth_group_file,
1802 AuthOrder => 'mod_auth_file.c',
1803
1804 AllowOverride => 'on',
1805
1806 IfModules => {
1807 'mod_delay.c' => {
1808 DelayEngine => 'off',
1809 },
1810 },
1811 };
1812
1813 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1814
1815 if (open(my $fh, ">> $config_file")) {
1816 print $fh <<EOC;
1817 <Directory ~>
1818 UseSendfile off
1819 </Directory>
1820 EOC
1821
1822 unless (close($fh)) {
1823 die("Can't write $config_file: $!");
1824 }
1825
1826 } else {
1827 die("Can't open $config_file: $!");
1828 }
1829
1830 my $path_without_sendfile = File::Spec->rel2abs($config_file);
1831
1832 # We copy the config file here, *after* it has been written out.
1833 my $path_with_sendfile = File::Spec->rel2abs("$sub_dir/test.dat");
1834 unless (copy($config_file, $path_with_sendfile)) {
1835 die("Can't copy '$config_file' to '$path_with_sendfile': $!");
1836 }
1837
1838 # Open pipes, for use between the parent and child processes. Specifically,
1839 # the child will indicate when it's done with its test by writing a message
1840 # to the parent.
1841 my ($rfh, $wfh);
1842 unless (pipe($rfh, $wfh)) {
1843 die("Can't open pipe: $!");
1844 }
1845
1846 my $ex;
1847
1848 # Fork child
1849 $self->handle_sigchld();
1850 defined(my $pid = fork()) or die("Can't fork: $!");
1851 if ($pid) {
1852 eval {
1853 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1854 $client->login($user, $passwd);
1855 $client->type('binary');
1856
1857 my ($buf, $conn, $expected, $resp_code, $resp_msg);
1858
1859 $conn = $client->retr_raw($path_without_sendfile);
1860 unless ($conn) {
1861 die("RETR failed: " . $client->response_code() . " " .
1862 $client->response_msg());
1863 }
1864
1865 while ($conn->read($buf, 8192, 30)) {
1866 }
1867 eval { $conn->close() };
1868
1869 $resp_code = $client->response_code();
1870 $resp_msg = $client->response_msg();
1871 $self->assert_transfer_ok($resp_code, $resp_msg);
1872
1873 $conn = $client->retr_raw($path_with_sendfile);
1874 unless ($conn) {
1875 die("RETR failed: " . $client->response_code() . " " .
1876 $client->response_msg());
1877 }
1878
1879 while ($conn->read($buf, 8192, 30)) {
1880 }
1881 eval { $conn->close() };
1882
1883 $resp_code = $client->response_code();
1884 $resp_msg = $client->response_msg();
1885 $self->assert_transfer_ok($resp_code, $resp_msg);
1886
1887 $client->quit();
1888 };
1889
1890 if ($@) {
1891 $ex = $@;
1892 }
1893
1894 $wfh->print("done\n");
1895 $wfh->flush();
1896
1897 } else {
1898 eval { server_wait($config_file, $rfh) };
1899 if ($@) {
1900 warn($@);
1901 exit 1;
1902 }
1903
1904 exit 0;
1905 }
1906
1907 # Stop server
1908 server_stop($pid_file);
1909
1910 $self->assert_child_ok($pid);
1911
1912 if (open(my $fh, "< $log_file")) {
1913 my $with_sendfile_ok = 0;
1914 my $with_sendfile_len_ok = 0;
1915 my $without_sendfile_ok = 0;
1916
1917 my $expected_with_sendfile = 'using sendfile capability for transmitting data';
1918 my $expected_with_sendfile_len = 'using sendfile with configured UseSendfile length';
1919 my $expected_without_sendfile = 'declining use of sendfile due to UseSendfile configuration';
1920
1921 while (my $line = <$fh>) {
1922 chomp($line);
1923
1924 # Look for the "without sendfile" log message first; only if that
1925 # is found will we look for the "with sendfile" log message.
1926 if ($with_sendfile_ok == 0 &&
1927 $line =~ /$expected_without_sendfile/) {
1928 $without_sendfile_ok = 1;
1929 }
1930
1931 if ($without_sendfile_ok == 1 &&
1932 $line =~ /$expected_with_sendfile/) {
1933 $with_sendfile_ok = 1;
1934 }
1935
1936 if ($with_sendfile_ok == 1 &&
1937 $line =~ /$expected_with_sendfile_len/) {
1938 $with_sendfile_len_ok = 1;
1939 last;
1940 }
1941 }
1942
1943 close($fh);
1944
1945 $self->assert($without_sendfile_ok,
1946 test_msg("Expected log message '$expected_without_sendfile' did not appear in SystemLog"));
1947
1948 $self->assert($with_sendfile_ok,
1949 test_msg("Expected log message '$expected_with_sendfile' did not appear in SystemLog"));
1950
1951 $self->assert($with_sendfile_len_ok,
1952 test_msg("Expected log message '$expected_with_sendfile_len' did not appear in SystemLog"));
1953
1954 } else {
1955 die("Can't read $log_file: $!");
1956 }
1957
1958 if ($ex) {
1959 die($ex);
1960 }
1961
1962 unlink($log_file);
1963 }
1964
1965 sub usesendfile_len_ascii_ftpaccess_bug3310 {
1966 my $self = shift;
1967 my $tmpdir = $self->{tmpdir};
1968
1969 my $config_file = "$tmpdir/config.conf";
1970 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
1971 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
1972
1973 my $log_file = File::Spec->rel2abs('tests.log');
1974
1975 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
1976 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
1977
1978 my $user = 'proftpd';
1979 my $passwd = 'test';
1980 my $home_dir = File::Spec->rel2abs($tmpdir);
1981 my $uid = 500;
1982 my $gid = 500;
1983
1984 # Make sure that, if we're running as root, that the home directory has
1985 # permissions/privs set for the account we create
1986 if ($< == 0) {
1987 unless (chmod(0755, $home_dir)) {
1988 die("Can't set perms on $home_dir to 0755: $!");
1989 }
1990
1991 unless (chown($uid, $gid, $home_dir)) {
1992 die("Can't set owner of $home_dir to $uid/$gid: $!");
1993 }
1994 }
1995
1996 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1997 '/bin/bash');
1998 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
1999
2000 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
2001 mkpath($sub_dir);
2002
2003 my $ftpaccess_file = File::Spec->rel2abs("$sub_dir/.ftpaccess");
2004 if (open(my $fh, "> $ftpaccess_file")) {
2005 print $fh "UseSendfile 256 b\n";
2006 unless (close($fh)) {
2007 die("Can't write $ftpaccess_file: $!");
2008 }
2009
2010 } else {
2011 die("Can't open $ftpaccess_file: $!");
2012 }
2013
2014 my $config = {
2015 PidFile => $pid_file,
2016 ScoreboardFile => $scoreboard_file,
2017 SystemLog => $log_file,
2018
2019 # We explicitly use DebugLevel 10 here, to get the sendfile log
2020 # message emitted by mod_xfer.
2021 DebugLevel => 10,
2022
2023 AuthUserFile => $auth_user_file,
2024 AuthGroupFile => $auth_group_file,
2025 AuthOrder => 'mod_auth_file.c',
2026
2027 AllowOverride => 'on',
2028
2029 IfModules => {
2030 'mod_delay.c' => {
2031 DelayEngine => 'off',
2032 },
2033 },
2034 };
2035
2036 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2037
2038 if (open(my $fh, ">> $config_file")) {
2039 print $fh <<EOC;
2040 <Directory ~>
2041 UseSendfile off
2042 </Directory>
2043 EOC
2044
2045 unless (close($fh)) {
2046 die("Can't write $config_file: $!");
2047 }
2048
2049 } else {
2050 die("Can't open $config_file: $!");
2051 }
2052
2053 my $path_without_sendfile = File::Spec->rel2abs($config_file);
2054
2055 # We copy the config file here, *after* it has been written out.
2056 my $path_with_sendfile = File::Spec->rel2abs("$sub_dir/test.dat");
2057 unless (copy($config_file, $path_with_sendfile)) {
2058 die("Can't copy '$config_file' to '$path_with_sendfile': $!");
2059 }
2060
2061 # Open pipes, for use between the parent and child processes. Specifically,
2062 # the child will indicate when it's done with its test by writing a message
2063 # to the parent.
2064 my ($rfh, $wfh);
2065 unless (pipe($rfh, $wfh)) {
2066 die("Can't open pipe: $!");
2067 }
2068
2069 my $ex;
2070
2071 # Fork child
2072 $self->handle_sigchld();
2073 defined(my $pid = fork()) or die("Can't fork: $!");
2074 if ($pid) {
2075 eval {
2076 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2077 $client->login($user, $passwd);
2078 $client->type('ascii');
2079
2080 my ($buf, $conn, $expected, $resp_code, $resp_msg);
2081
2082 $conn = $client->retr_raw($path_without_sendfile);
2083 unless ($conn) {
2084 die("RETR failed: " . $client->response_code() . " " .
2085 $client->response_msg());
2086 }
2087
2088 while ($conn->read($buf, 8192, 30)) {
2089 }
2090 eval { $conn->close() };
2091
2092 $resp_code = $client->response_code();
2093 $resp_msg = $client->response_msg();
2094 $self->assert_transfer_ok($resp_code, $resp_msg);
2095
2096 $conn = $client->retr_raw($path_with_sendfile);
2097 unless ($conn) {
2098 die("RETR failed: " . $client->response_code() . " " .
2099 $client->response_msg());
2100 }
2101
2102 while ($conn->read($buf, 8192, 30)) {
2103 }
2104 eval { $conn->close() };
2105
2106 $resp_code = $client->response_code();
2107 $resp_msg = $client->response_msg();
2108 $self->assert_transfer_ok($resp_code, $resp_msg);
2109
2110 $client->quit();
2111 };
2112
2113 if ($@) {
2114 $ex = $@;
2115 }
2116
2117 $wfh->print("done\n");
2118 $wfh->flush();
2119
2120 } else {
2121 eval { server_wait($config_file, $rfh) };
2122 if ($@) {
2123 warn($@);
2124 exit 1;
2125 }
2126
2127 exit 0;
2128 }
2129
2130 # Stop server
2131 server_stop($pid_file);
2132
2133 $self->assert_child_ok($pid);
2134
2135 if (open(my $fh, "< $log_file")) {
2136 my $without_sendfile_config_ok = 0;
2137 my $without_sendfile_ascii_ok = 0;
2138
2139 my $expected_without_sendfile_config = 'declining use of sendfile due to UseSendfile configuration';
2140 my $expected_without_sendfile_ascii = 'declining use of sendfile for ASCII data';
2141
2142 while (my $line = <$fh>) {
2143 chomp($line);
2144
2145 # Look for the "without sendfile" log message first; only if that
2146 # is found will we look for the "with sendfile" log message.
2147 if ($without_sendfile_config_ok == 0 &&
2148 $line =~ /$expected_without_sendfile_config/) {
2149 $without_sendfile_config_ok = 1;
2150 }
2151
2152 if ($without_sendfile_config_ok == 1 &&
2153 $line =~ /$expected_without_sendfile_ascii/) {
2154 $without_sendfile_ascii_ok = 1;
2155 last;
2156 }
2157 }
2158
2159 close($fh);
2160
2161 $self->assert($without_sendfile_config_ok,
2162 test_msg("Expected log message '$expected_without_sendfile_config' did not appear in SystemLog"));
2163
2164 $self->assert($without_sendfile_ascii_ok,
2165 test_msg("Expected log message '$expected_without_sendfile_ascii' did not appear in SystemLog"));
2166
2167 } else {
2168 die("Can't read $log_file: $!");
2169 }
2170
2171 if ($ex) {
2172 die($ex);
2173 }
2174
2175 unlink($log_file);
2176 }
2177
2178 sub usesendfile_pct_binary_dir_bug3310 {
2179 my $self = shift;
2180 my $tmpdir = $self->{tmpdir};
2181
2182 my $config_file = "$tmpdir/config.conf";
2183 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
2184 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
2185
2186 my $log_file = File::Spec->rel2abs('tests.log');
2187
2188 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
2189 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
2190
2191 my $user = 'proftpd';
2192 my $passwd = 'test';
2193 my $home_dir = File::Spec->rel2abs($tmpdir);
2194 my $uid = 500;
2195 my $gid = 500;
2196
2197 # Make sure that, if we're running as root, that the home directory has
2198 # permissions/privs set for the account we create
2199 if ($< == 0) {
2200 unless (chmod(0755, $home_dir)) {
2201 die("Can't set perms on $home_dir to 0755: $!");
2202 }
2203
2204 unless (chown($uid, $gid, $home_dir)) {
2205 die("Can't set owner of $home_dir to $uid/$gid: $!");
2206 }
2207 }
2208
2209 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2210 '/bin/bash');
2211 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
2212
2213 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
2214 mkpath($sub_dir);
2215
2216 my $config = {
2217 PidFile => $pid_file,
2218 ScoreboardFile => $scoreboard_file,
2219 SystemLog => $log_file,
2220
2221 # We explicitly use DebugLevel 10 here, to get the sendfile log
2222 # message emitted by mod_xfer.
2223 DebugLevel => 10,
2224
2225 AuthUserFile => $auth_user_file,
2226 AuthGroupFile => $auth_group_file,
2227 AuthOrder => 'mod_auth_file.c',
2228
2229 IfModules => {
2230 'mod_delay.c' => {
2231 DelayEngine => 'off',
2232 },
2233 },
2234 };
2235
2236 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2237
2238 if (open(my $fh, ">> $config_file")) {
2239 print $fh <<EOC;
2240 <Directory ~>
2241 UseSendfile off
2242 </Directory>
2243
2244 <Directory ~/foo>
2245 UseSendfile 25%
2246 </Directory>
2247 EOC
2248
2249 unless (close($fh)) {
2250 die("Can't write $config_file: $!");
2251 }
2252
2253 } else {
2254 die("Can't open $config_file: $!");
2255 }
2256
2257 my $path_without_sendfile = File::Spec->rel2abs($config_file);
2258
2259 # We copy the config file here, *after* it has been written out.
2260 my $path_with_sendfile = File::Spec->rel2abs("$sub_dir/test.dat");
2261 unless (copy($config_file, $path_with_sendfile)) {
2262 die("Can't copy '$config_file' to '$path_with_sendfile': $!");
2263 }
2264
2265 # Open pipes, for use between the parent and child processes. Specifically,
2266 # the child will indicate when it's done with its test by writing a message
2267 # to the parent.
2268 my ($rfh, $wfh);
2269 unless (pipe($rfh, $wfh)) {
2270 die("Can't open pipe: $!");
2271 }
2272
2273 my $ex;
2274
2275 # Fork child
2276 $self->handle_sigchld();
2277 defined(my $pid = fork()) or die("Can't fork: $!");
2278 if ($pid) {
2279 eval {
2280 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2281 $client->login($user, $passwd);
2282 $client->type('binary');
2283
2284 my ($buf, $conn, $expected, $resp_code, $resp_msg);
2285
2286 $conn = $client->retr_raw($path_without_sendfile);
2287 unless ($conn) {
2288 die("RETR failed: " . $client->response_code() . " " .
2289 $client->response_msg());
2290 }
2291
2292 while ($conn->read($buf, 8192, 30)) {
2293 }
2294 eval { $conn->close() };
2295
2296 $resp_code = $client->response_code();
2297 $resp_msg = $client->response_msg();
2298 $self->assert_transfer_ok($resp_code, $resp_msg);
2299
2300 $conn = $client->retr_raw($path_with_sendfile);
2301 unless ($conn) {
2302 die("RETR failed: " . $client->response_code() . " " .
2303 $client->response_msg());
2304 }
2305
2306 while ($conn->read($buf, 8192, 30)) {
2307 }
2308 eval { $conn->close() };
2309
2310 $resp_code = $client->response_code();
2311 $resp_msg = $client->response_msg();
2312 $self->assert_transfer_ok($resp_code, $resp_msg);
2313
2314 $client->quit();
2315 };
2316
2317 if ($@) {
2318 $ex = $@;
2319 }
2320
2321 $wfh->print("done\n");
2322 $wfh->flush();
2323
2324 } else {
2325 eval { server_wait($config_file, $rfh) };
2326 if ($@) {
2327 warn($@);
2328 exit 1;
2329 }
2330
2331 exit 0;
2332 }
2333
2334 # Stop server
2335 server_stop($pid_file);
2336
2337 $self->assert_child_ok($pid);
2338
2339 if (open(my $fh, "< $log_file")) {
2340 my $with_sendfile_ok = 0;
2341 my $with_sendfile_pct_ok = 0;
2342 my $without_sendfile_ok = 0;
2343
2344 my $expected_with_sendfile = 'using sendfile capability for transmitting data';
2345 my $expected_with_sendfile_pct = 'using sendfile with configured UseSendfile percentage';
2346 my $expected_without_sendfile = 'declining use of sendfile due to UseSendfile configuration';
2347
2348 while (my $line = <$fh>) {
2349 chomp($line);
2350
2351 # Look for the "without sendfile" log message first; only if that
2352 # is found will we look for the "with sendfile" log message.
2353 if ($with_sendfile_ok == 0 &&
2354 $line =~ /$expected_without_sendfile/) {
2355 $without_sendfile_ok = 1;
2356 }
2357
2358 if ($without_sendfile_ok == 1 &&
2359 $line =~ /$expected_with_sendfile/) {
2360 $with_sendfile_ok = 1;
2361 }
2362
2363 if ($with_sendfile_ok == 1 &&
2364 $line =~ /$expected_with_sendfile_pct/) {
2365 $with_sendfile_pct_ok = 1;
2366 last;
2367 }
2368 }
2369
2370 close($fh);
2371
2372 $self->assert($without_sendfile_ok,
2373 test_msg("Expected log message '$expected_without_sendfile' did not appear in SystemLog"));
2374
2375 $self->assert($with_sendfile_ok,
2376 test_msg("Expected log message '$expected_with_sendfile' did not appear in SystemLog"));
2377
2378 $self->assert($with_sendfile_pct_ok,
2379 test_msg("Expected log message '$expected_with_sendfile_pct' did not appear in SystemLog"));
2380
2381 } else {
2382 die("Can't read $log_file: $!");
2383 }
2384
2385 if ($ex) {
2386 die($ex);
2387 }
2388
2389 unlink($log_file);
2390 }
2391
2392 sub usesendfile_pct_binary_ftpaccess_bug3310 {
2393 my $self = shift;
2394 my $tmpdir = $self->{tmpdir};
2395
2396 my $config_file = "$tmpdir/config.conf";
2397 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
2398 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
2399
2400 my $log_file = File::Spec->rel2abs('tests.log');
2401
2402 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
2403 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
2404
2405 my $user = 'proftpd';
2406 my $passwd = 'test';
2407 my $home_dir = File::Spec->rel2abs($tmpdir);
2408 my $uid = 500;
2409 my $gid = 500;
2410
2411 # Make sure that, if we're running as root, that the home directory has
2412 # permissions/privs set for the account we create
2413 if ($< == 0) {
2414 unless (chmod(0755, $home_dir)) {
2415 die("Can't set perms on $home_dir to 0755: $!");
2416 }
2417
2418 unless (chown($uid, $gid, $home_dir)) {
2419 die("Can't set owner of $home_dir to $uid/$gid: $!");
2420 }
2421 }
2422
2423 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2424 '/bin/bash');
2425 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
2426
2427 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
2428 mkpath($sub_dir);
2429
2430 my $ftpaccess_file = File::Spec->rel2abs("$sub_dir/.ftpaccess");
2431 if (open(my $fh, "> $ftpaccess_file")) {
2432 print $fh "UseSendfile 25%\n";
2433 unless (close($fh)) {
2434 die("Can't write $ftpaccess_file: $!");
2435 }
2436
2437 } else {
2438 die("Can't open $ftpaccess_file: $!");
2439 }
2440
2441 my $config = {
2442 PidFile => $pid_file,
2443 ScoreboardFile => $scoreboard_file,
2444 SystemLog => $log_file,
2445
2446 # We explicitly use DebugLevel 10 here, to get the sendfile log
2447 # message emitted by mod_xfer.
2448 DebugLevel => 10,
2449
2450 AuthUserFile => $auth_user_file,
2451 AuthGroupFile => $auth_group_file,
2452 AuthOrder => 'mod_auth_file.c',
2453
2454 AllowOverride => 'on',
2455
2456 IfModules => {
2457 'mod_delay.c' => {
2458 DelayEngine => 'off',
2459 },
2460 },
2461 };
2462
2463 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2464
2465 if (open(my $fh, ">> $config_file")) {
2466 print $fh <<EOC;
2467 <Directory ~>
2468 UseSendfile off
2469 </Directory>
2470 EOC
2471
2472 unless (close($fh)) {
2473 die("Can't write $config_file: $!");
2474 }
2475
2476 } else {
2477 die("Can't open $config_file: $!");
2478 }
2479
2480 my $path_without_sendfile = File::Spec->rel2abs($config_file);
2481
2482 # We copy the config file here, *after* it has been written out.
2483 my $path_with_sendfile = File::Spec->rel2abs("$sub_dir/test.dat");
2484 unless (copy($config_file, $path_with_sendfile)) {
2485 die("Can't copy '$config_file' to '$path_with_sendfile': $!");
2486 }
2487
2488 # Open pipes, for use between the parent and child processes. Specifically,
2489 # the child will indicate when it's done with its test by writing a message
2490 # to the parent.
2491 my ($rfh, $wfh);
2492 unless (pipe($rfh, $wfh)) {
2493 die("Can't open pipe: $!");
2494 }
2495
2496 my $ex;
2497
2498 # Fork child
2499 $self->handle_sigchld();
2500 defined(my $pid = fork()) or die("Can't fork: $!");
2501 if ($pid) {
2502 eval {
2503 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2504 $client->login($user, $passwd);
2505 $client->type('binary');
2506
2507 my ($buf, $conn, $expected, $resp_code, $resp_msg);
2508
2509 $conn = $client->retr_raw($path_without_sendfile);
2510 unless ($conn) {
2511 die("RETR failed: " . $client->response_code() . " " .
2512 $client->response_msg());
2513 }
2514
2515 while ($conn->read($buf, 8192, 30)) {
2516 }
2517 eval { $conn->close() };
2518
2519 $resp_code = $client->response_code();
2520 $resp_msg = $client->response_msg();
2521 $self->assert_transfer_ok($resp_code, $resp_msg);
2522
2523 $conn = $client->retr_raw($path_with_sendfile);
2524 unless ($conn) {
2525 die("RETR failed: " . $client->response_code() . " " .
2526 $client->response_msg());
2527 }
2528
2529 while ($conn->read($buf, 8192, 30)) {
2530 }
2531 eval { $conn->close() };
2532
2533 $resp_code = $client->response_code();
2534 $resp_msg = $client->response_msg();
2535 $self->assert_transfer_ok($resp_code, $resp_msg);
2536
2537 $client->quit();
2538 };
2539
2540 if ($@) {
2541 $ex = $@;
2542 }
2543
2544 $wfh->print("done\n");
2545 $wfh->flush();
2546
2547 } else {
2548 eval { server_wait($config_file, $rfh) };
2549 if ($@) {
2550 warn($@);
2551 exit 1;
2552 }
2553
2554 exit 0;
2555 }
2556
2557 # Stop server
2558 server_stop($pid_file);
2559
2560 $self->assert_child_ok($pid);
2561
2562 if (open(my $fh, "< $log_file")) {
2563 my $with_sendfile_ok = 0;
2564 my $with_sendfile_pct_ok = 0;
2565 my $without_sendfile_ok = 0;
2566
2567 my $expected_with_sendfile = 'using sendfile capability for transmitting data';
2568 my $expected_with_sendfile_pct = 'using sendfile with configured UseSendfile percentage';
2569 my $expected_without_sendfile = 'declining use of sendfile due to UseSendfile configuration';
2570
2571 while (my $line = <$fh>) {
2572 chomp($line);
2573
2574 # Look for the "without sendfile" log message first; only if that
2575 # is found will we look for the "with sendfile" log message.
2576 if ($with_sendfile_ok == 0 &&
2577 $line =~ /$expected_without_sendfile/) {
2578 $without_sendfile_ok = 1;
2579 }
2580
2581 if ($without_sendfile_ok == 1 &&
2582 $line =~ /$expected_with_sendfile/) {
2583 $with_sendfile_ok = 1;
2584 }
2585
2586 if ($with_sendfile_ok == 1 &&
2587 $line =~ /$expected_with_sendfile_pct/) {
2588 $with_sendfile_pct_ok = 1;
2589 last;
2590 }
2591 }
2592
2593 close($fh);
2594
2595 $self->assert($without_sendfile_ok,
2596 test_msg("Expected log message '$expected_without_sendfile' did not appear in SystemLog"));
2597
2598 $self->assert($with_sendfile_ok,
2599 test_msg("Expected log message '$expected_with_sendfile' did not appear in SystemLog"));
2600
2601 $self->assert($with_sendfile_pct_ok,
2602 test_msg("Expected log message '$expected_with_sendfile_pct' did not appear in SystemLog"));
2603
2604 } else {
2605 die("Can't read $log_file: $!");
2606 }
2607
2608 if ($ex) {
2609 die($ex);
2610 }
2611
2612 unlink($log_file);
2613 }
2614
2615 1;