Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Commands/RETR.pm

raw
1 package ProFTPD::Tests::Commands::RETR;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use Cwd;
8 use Digest::MD5;
9 use File::Path qw(mkpath);
10 use File::Spec;
11 use IO::Handle;
12
13 use ProFTPD::TestSuite::FTP;
14 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
15
16 $| = 1;
17
18 my $order = 0;
19
20 my $TESTS = {
21 retr_ok_raw_active_binary => {
22 order => ++$order,
23 test_class => [qw(forking)],
24 },
25
26 retr_ok_raw_active_multiple_downloads => {
27 order => ++$order,
28 test_class => [qw(forking)],
29 },
30
31 retr_ok_raw_passive => {
32 order => ++$order,
33 test_class => [qw(forking)],
34 },
35
36 retr_ok_file => {
37 order => ++$order,
38 test_class => [qw(forking)],
39 },
40
41 retr_ok_largefile_ascii => {
42 order => ++$order,
43 test_class => [qw(forking)],
44 },
45
46 retr_ok_largefile_binary => {
47 order => ++$order,
48 test_class => [qw(forking)],
49 },
50
51 retr_ok_ascii_file_bug4237 => {
52 order => ++$order,
53 test_class => [qw(bug forking)],
54 },
55
56 retr_ok_ascii_file_bug4277 => {
57 order => ++$order,
58 test_class => [qw(bug forking)],
59 },
60
61 retr_abs_symlink => {
62 order => ++$order,
63 test_class => [qw(forking)],
64 },
65
66 retr_abs_symlink_chrooted_bug4219 => {
67 order => ++$order,
68 test_class => [qw(bug forking rootprivs)],
69 },
70
71 retr_rel_symlink => {
72 order => ++$order,
73 test_class => [qw(forking)],
74 },
75
76 retr_rel_symlink_chrooted_bug4219 => {
77 order => ++$order,
78 test_class => [qw(bug forking rootprivs)],
79 },
80
81 retr_fails_not_reg => {
82 order => ++$order,
83 test_class => [qw(forking)],
84 },
85
86 retr_fails_login_required => {
87 order => ++$order,
88 test_class => [qw(forking)],
89 },
90
91 retr_fails_no_path => {
92 order => ++$order,
93 test_class => [qw(forking)],
94 },
95
96 retr_fails_enoent => {
97 order => ++$order,
98 test_class => [qw(forking)],
99 },
100
101 retr_fails_enoent_glob => {
102 order => ++$order,
103 test_class => [qw(forking)],
104 },
105
106 retr_fails_abs_symlink_enoent => {
107 order => ++$order,
108 test_class => [qw(forking)],
109 },
110
111 retr_fails_abs_symlink_enoent_chrooted_bug4219 => {
112 order => ++$order,
113 test_class => [qw(forking rootprivs)],
114 },
115
116 retr_fails_rel_symlink_enoent => {
117 order => ++$order,
118 test_class => [qw(forking)],
119 },
120
121 retr_fails_rel_symlink_enoent_chrooted_bug4219 => {
122 order => ++$order,
123 test_class => [qw(forking rootprivs)],
124 },
125
126 retr_fails_eperm => {
127 order => ++$order,
128 test_class => [qw(forking)],
129 },
130
131 retr_ok_dir_with_spaces => {
132 order => ++$order,
133 test_class => [qw(forking)],
134 },
135
136 retr_leading_whitespace => {
137 order => ++$order,
138 test_class => [qw(forking)],
139 },
140
141 retr_bug3496 => {
142 order => ++$order,
143 test_class => [qw(bug forking)],
144 },
145
146 retr_2nd_transfer_terminates_1st_transfer_bug4010 => {
147 order => ++$order,
148 test_class => [qw(bug forking)],
149 },
150
151 };
152
153 sub new {
154 return shift()->SUPER::new(@_);
155 }
156
157 sub list_tests {
158 return testsuite_get_runnable_tests($TESTS);
159 }
160
161 sub retr_ok_raw_active_binary {
162 my $self = shift;
163 my $tmpdir = $self->{tmpdir};
164 my $setup = test_setup($tmpdir, 'cmds');
165
166 my $test_file = File::Spec->rel2abs($setup->{config_file});
167
168 my $config = {
169 PidFile => $setup->{pid_file},
170 ScoreboardFile => $setup->{scoreboard_file},
171 SystemLog => $setup->{log_file},
172
173 AuthUserFile => $setup->{auth_user_file},
174 AuthGroupFile => $setup->{auth_group_file},
175 AuthOrder => 'mod_auth_file.c',
176
177 IfModules => {
178 'mod_delay.c' => {
179 DelayEngine => 'off',
180 },
181 },
182 };
183
184 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
185 $config);
186
187 # Calculate the MD5 checksum of this file, for comparison with the
188 # downloaded file.
189 my $test_file_md5;
190
191 my $md5_file = $test_file;
192 if ($^O eq 'darwin') {
193 # MacOSX-specific hack
194 $md5_file = '/private' . $test_file;
195 }
196
197 if (open(my $fh, "< $md5_file")) {
198 binmode($fh);
199
200 my $ctx = Digest::MD5->new();
201 $ctx->addfile($fh);
202 $test_file_md5 = $ctx->hexdigest();
203 close($fh);
204
205 } else {
206 die("Can't read $md5_file: $!");
207 }
208
209 # Open pipes, for use between the parent and child processes. Specifically,
210 # the child will indicate when it's done with its test by writing a message
211 # to the parent.
212 my ($rfh, $wfh);
213 unless (pipe($rfh, $wfh)) {
214 die("Can't open pipe: $!");
215 }
216
217 my $ex;
218
219 # Fork child
220 $self->handle_sigchld();
221 defined(my $pid = fork()) or die("Can't fork: $!");
222 if ($pid) {
223 eval {
224 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
225 $client->login($setup->{user}, $setup->{passwd});
226 $client->type('binary');
227
228 my $conn = $client->retr_raw($test_file);
229 unless ($conn) {
230 die("Failed to RETR: " . $client->response_code() . " " .
231 $client->response_msg());
232 }
233
234 my $ctx = Digest::MD5->new();
235 my $buf;
236 $conn->read($buf, 8192, 30);
237 eval { $conn->close() };
238
239 $ctx->add($buf);
240
241 my ($resp_code, $resp_msg);
242 $resp_code = $client->response_code();
243 $resp_msg = $client->response_msg();
244
245 $self->assert_transfer_ok($resp_code, $resp_msg);
246
247 my $md5 = $ctx->hexdigest();
248 $self->assert($md5 eq $test_file_md5,
249 test_msg("Expected MD5 $test_file_md5, got $md5"));
250 };
251 if ($@) {
252 $ex = $@;
253 }
254
255 $wfh->print("done\n");
256 $wfh->flush();
257
258 } else {
259 eval { server_wait($setup->{config_file}, $rfh) };
260 if ($@) {
261 warn($@);
262 exit 1;
263 }
264
265 exit 0;
266 }
267
268 # Stop server
269 server_stop($setup->{pid_file});
270 $self->assert_child_ok($pid);
271
272 test_cleanup($setup->{log_file}, $ex);
273 }
274
275 sub retr_ok_raw_active_multiple_downloads {
276 my $self = shift;
277 my $tmpdir = $self->{tmpdir};
278 my $setup = test_setup($tmpdir, 'cmds');
279
280 my $test_file = File::Spec->rel2abs($setup->{config_file});
281
282 my $config = {
283 PidFile => $setup->{pid_file},
284 ScoreboardFile => $setup->{scoreboard_file},
285 SystemLog => $setup->{log_file},
286 TraceLog => $setup->{log_file},
287 Trace => 'binding:20 data:20 netio:20',
288
289 AuthUserFile => $setup->{auth_user_file},
290 AuthGroupFile => $setup->{auth_group_file},
291 AuthOrder => 'mod_auth_file.c',
292
293 IfModules => {
294 'mod_delay.c' => {
295 DelayEngine => 'off',
296 },
297 },
298 };
299
300 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
301 $config);
302
303 # Open pipes, for use between the parent and child processes. Specifically,
304 # the child will indicate when it's done with its test by writing a message
305 # to the parent.
306 my ($rfh, $wfh);
307 unless (pipe($rfh, $wfh)) {
308 die("Can't open pipe: $!");
309 }
310
311 my $ex;
312
313 # Fork child
314 $self->handle_sigchld();
315 defined(my $pid = fork()) or die("Can't fork: $!");
316 if ($pid) {
317 eval {
318 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
319 $client->login($setup->{user}, $setup->{passwd});
320
321 for (my $i = 0; $i < 5; $i++) {
322 my $conn = $client->retr_raw($test_file);
323 unless ($conn) {
324 die("Failed to RETR: " . $client->response_code() . " " .
325 $client->response_msg());
326 }
327
328 my $buf;
329 $conn->read($buf, 8192, 30);
330 eval { $conn->close() };
331
332 my $resp_code = $client->response_code();
333 my $resp_msg = $client->response_msg();
334 $self->assert_transfer_ok($resp_code, $resp_msg);
335 }
336
337 $client->quit();
338 };
339 if ($@) {
340 $ex = $@;
341 }
342
343 $wfh->print("done\n");
344 $wfh->flush();
345
346 } else {
347 eval { server_wait($setup->{config_file}, $rfh) };
348 if ($@) {
349 warn($@);
350 exit 1;
351 }
352
353 exit 0;
354 }
355
356 # Stop server
357 server_stop($setup->{pid_file});
358 $self->assert_child_ok($pid);
359
360 test_cleanup($setup->{log_file}, $ex);
361 }
362
363 sub retr_ok_raw_passive {
364 my $self = shift;
365 my $tmpdir = $self->{tmpdir};
366 my $setup = test_setup($tmpdir, 'cmds');
367
368 my $test_file = File::Spec->rel2abs($setup->{config_file});
369
370 my $config = {
371 PidFile => $setup->{pid_file},
372 ScoreboardFile => $setup->{scoreboard_file},
373 SystemLog => $setup->{log_file},
374
375 AuthUserFile => $setup->{auth_user_file},
376 AuthGroupFile => $setup->{auth_group_file},
377 AuthOrder => 'mod_auth_file.c',
378
379 IfModules => {
380 'mod_delay.c' => {
381 DelayEngine => 'off',
382 },
383 },
384 };
385
386 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
387 $config);
388
389 # Open pipes, for use between the parent and child processes. Specifically,
390 # the child will indicate when it's done with its test by writing a message
391 # to the parent.
392 my ($rfh, $wfh);
393 unless (pipe($rfh, $wfh)) {
394 die("Can't open pipe: $!");
395 }
396
397 my $ex;
398
399 # Fork child
400 $self->handle_sigchld();
401 defined(my $pid = fork()) or die("Can't fork: $!");
402 if ($pid) {
403 eval {
404 # When run too quickly with the other tests, this test can fail. So
405 # pause a little here.
406 sleep(1);
407
408 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
409 $client->login($setup->{user}, $setup->{passwd});
410
411 my $conn = $client->retr_raw($test_file);
412 unless ($conn) {
413 die("Failed to RETR: " . $client->response_code() . " " .
414 $client->response_msg());
415 }
416
417 my $buf;
418 $conn->read($buf, 8192, 30);
419 eval { $conn->close() };
420
421 my ($resp_code, $resp_msg);
422 $resp_code = $client->response_code();
423 $resp_msg = $client->response_msg();
424
425 $self->assert_transfer_ok($resp_code, $resp_msg);
426 };
427
428 if ($@) {
429 $ex = $@;
430 }
431
432 $wfh->print("done\n");
433 $wfh->flush();
434
435 } else {
436 eval { server_wait($setup->{config_file}, $rfh) };
437 if ($@) {
438 warn($@);
439 exit 1;
440 }
441
442 exit 0;
443 }
444
445 # Stop server
446 server_stop($setup->{pid_file});
447
448 $self->assert_child_ok($pid);
449
450 test_cleanup($setup->{log_file}, $ex);
451 }
452
453 sub retr_ok_file {
454 my $self = shift;
455 my $tmpdir = $self->{tmpdir};
456 my $setup = test_setup($tmpdir, 'cmds');
457
458 my $test_file = File::Spec->rel2abs("$tmpdir/foo");
459 if (open(my $fh, "> $test_file")) {
460 print $fh "Foo!\n";
461
462 unless (close($fh)) {
463 die("Unable to write $test_file: $!");
464 }
465
466 } else {
467 die("Unable to open $test_file: $!");
468 }
469
470 my $config = {
471 PidFile => $setup->{pid_file},
472 ScoreboardFile => $setup->{scoreboard_file},
473 SystemLog => $setup->{log_file},
474
475 AuthUserFile => $setup->{auth_user_file},
476 AuthGroupFile => $setup->{auth_group_file},
477 AuthOrder => 'mod_auth_file.c',
478
479 IfModules => {
480 'mod_delay.c' => {
481 DelayEngine => 'off',
482 },
483 },
484 };
485
486 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
487 $config);
488
489 # Open pipes, for use between the parent and child processes. Specifically,
490 # the child will indicate when it's done with its test by writing a message
491 # to the parent.
492 my ($rfh, $wfh);
493 unless (pipe($rfh, $wfh)) {
494 die("Can't open pipe: $!");
495 }
496
497 my $ex;
498
499 # Fork child
500 $self->handle_sigchld();
501 defined(my $pid = fork()) or die("Can't fork: $!");
502 if ($pid) {
503 eval {
504 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
505 $client->login($setup->{user}, $setup->{passwd});
506
507 my $conn = $client->retr_raw($test_file);
508 unless ($conn) {
509 die("RETR failed: " . $client->response_code() . " " .
510 $client->response_msg());
511 }
512
513 my $buf;
514 $conn->read($buf, 8192, 30);
515 my $size = $conn->bytes_read();
516 eval { $conn->close() };
517
518 my $expected = 6;
519 $self->assert($expected == $size,
520 test_msg("Expected $expected, got $size"));
521 };
522
523 if ($@) {
524 $ex = $@;
525 }
526
527 $wfh->print("done\n");
528 $wfh->flush();
529
530 } else {
531 eval { server_wait($setup->{config_file}, $rfh) };
532 if ($@) {
533 warn($@);
534 exit 1;
535 }
536
537 exit 0;
538 }
539
540 # Stop server
541 server_stop($setup->{pid_file});
542 $self->assert_child_ok($pid);
543
544 test_cleanup($setup->{log_file}, $ex);
545 }
546
547 sub retr_ok_largefile_ascii {
548 my $self = shift;
549 my $tmpdir = $self->{tmpdir};
550 my $setup = test_setup($tmpdir, 'cmds');
551
552 my $test_file = File::Spec->rel2abs("$tmpdir/foo");
553 if (open(my $fh, "> $test_file")) {
554 print $fh "AbCd\r\nEfGh\r\n\r\n" x 20480;
555
556 unless (close($fh)) {
557 die("Unable to write $test_file: $!");
558 }
559
560 } else {
561 die("Unable to open $test_file: $!");
562 }
563
564 my $ctx = Digest::MD5->new();
565 $ctx->add("AbCd\nEfGh\n\n" x 20480);
566 my $test_file_md5 = $ctx->hexdigest();
567
568 my $config = {
569 PidFile => $setup->{pid_file},
570 ScoreboardFile => $setup->{scoreboard_file},
571 SystemLog => $setup->{log_file},
572
573 AuthUserFile => $setup->{auth_user_file},
574 AuthGroupFile => $setup->{auth_group_file},
575 AuthOrder => 'mod_auth_file.c',
576
577 IfModules => {
578 'mod_delay.c' => {
579 DelayEngine => 'off',
580 },
581 },
582 };
583
584 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
585 $config);
586
587 # Open pipes, for use between the parent and child processes. Specifically,
588 # the child will indicate when it's done with its test by writing a message
589 # to the parent.
590 my ($rfh, $wfh);
591 unless (pipe($rfh, $wfh)) {
592 die("Can't open pipe: $!");
593 }
594
595 my $ex;
596
597 # Fork child
598 $self->handle_sigchld();
599 defined(my $pid = fork()) or die("Can't fork: $!");
600 if ($pid) {
601 eval {
602 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
603 $client->login($setup->{user}, $setup->{passwd});
604 $client->type('ascii');
605
606 my $conn = $client->retr_raw($test_file);
607 unless ($conn) {
608 die("RETR failed: " . $client->response_code() . " " .
609 $client->response_msg());
610 }
611
612 my $ctx = Digest::MD5->new();
613 my $buf;
614 my $tmp;
615 while ($conn->read($tmp, 8192, 30)) {
616 $ctx->add($tmp);
617 $buf .= $tmp;
618 }
619 my $size = $conn->bytes_read();
620 eval { $conn->close() };
621
622 $client->quit();
623
624 my $expected = -s $test_file;
625 $self->assert($expected == $size,
626 test_msg("Expected file size $expected, got $size"));
627
628 my $md5 = $ctx->hexdigest();
629 $self->assert($md5 eq $test_file_md5,
630 test_msg("Expected file MD5 $test_file_md5, got $md5"));
631 };
632 if ($@) {
633 $ex = $@;
634 }
635
636 $wfh->print("done\n");
637 $wfh->flush();
638
639 } else {
640 eval { server_wait($setup->{config_file}, $rfh) };
641 if ($@) {
642 warn($@);
643 exit 1;
644 }
645
646 exit 0;
647 }
648
649 # Stop server
650 server_stop($setup->{pid_file});
651 $self->assert_child_ok($pid);
652
653 test_cleanup($setup->{log_file}, $ex);
654 }
655
656 sub retr_ok_largefile_binary {
657 my $self = shift;
658 my $tmpdir = $self->{tmpdir};
659 my $setup = test_setup($tmpdir, 'cmds');
660
661 my $test_file = File::Spec->rel2abs("$tmpdir/foo");
662 if (open(my $fh, "> $test_file")) {
663 print $fh "AbCd\r\nEfGh\r\n\r\n" x 20480;
664
665 unless (close($fh)) {
666 die("Unable to write $test_file: $!");
667 }
668
669 } else {
670 die("Unable to open $test_file: $!");
671 }
672
673 # Calculate the MD5 checksum of this file, for comparison with the
674 # downloaded file.
675 my $test_file_md5;
676
677 my $md5_file = $test_file;
678 if ($^O eq 'darwin') {
679 # MacOSX-specific hack
680 $md5_file = '/private' . $test_file;
681 }
682
683 if (open(my $fh, "< $md5_file")) {
684 binmode($fh);
685
686 my $ctx = Digest::MD5->new();
687 $ctx->addfile($fh);
688 $test_file_md5 = $ctx->hexdigest();
689 close($fh);
690
691 } else {
692 die("Can't read $md5_file: $!");
693 }
694
695 my $config = {
696 PidFile => $setup->{pid_file},
697 ScoreboardFile => $setup->{scoreboard_file},
698 SystemLog => $setup->{log_file},
699
700 AuthUserFile => $setup->{auth_user_file},
701 AuthGroupFile => $setup->{auth_group_file},
702 AuthOrder => 'mod_auth_file.c',
703
704 IfModules => {
705 'mod_delay.c' => {
706 DelayEngine => 'off',
707 },
708 },
709 };
710
711 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
712 $config);
713
714 # Open pipes, for use between the parent and child processes. Specifically,
715 # the child will indicate when it's done with its test by writing a message
716 # to the parent.
717 my ($rfh, $wfh);
718 unless (pipe($rfh, $wfh)) {
719 die("Can't open pipe: $!");
720 }
721
722 my $ex;
723
724 # Fork child
725 $self->handle_sigchld();
726 defined(my $pid = fork()) or die("Can't fork: $!");
727 if ($pid) {
728 eval {
729 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
730 $client->login($setup->{user}, $setup->{passwd});
731 $client->type('binary');
732
733 my $conn = $client->retr_raw($test_file);
734 unless ($conn) {
735 die("RETR failed: " . $client->response_code() . " " .
736 $client->response_msg());
737 }
738
739 my $ctx = Digest::MD5->new();
740 my $buf;
741 my $tmp;
742 while ($conn->read($tmp, 8192, 30)) {
743 $ctx->add($tmp);
744 $buf .= $tmp;
745 }
746 my $size = $conn->bytes_read();
747 eval { $conn->close() };
748
749 $client->quit();
750
751 my $expected = -s $test_file;
752 $self->assert($expected == $size,
753 test_msg("Expected file size $expected, got $size"));
754
755 my $md5 = $ctx->hexdigest();
756 $self->assert($md5 eq $test_file_md5,
757 test_msg("Expected file MD5 $test_file_md5, got $md5"));
758 };
759 if ($@) {
760 $ex = $@;
761 }
762
763 $wfh->print("done\n");
764 $wfh->flush();
765
766 } else {
767 eval { server_wait($setup->{config_file}, $rfh) };
768 if ($@) {
769 warn($@);
770 exit 1;
771 }
772
773 exit 0;
774 }
775
776 # Stop server
777 server_stop($setup->{pid_file});
778 $self->assert_child_ok($pid);
779
780 test_cleanup($setup->{log_file}, $ex);
781 }
782
783 sub retr_ok_ascii_file_bug4237 {
784 my $self = shift;
785 my $tmpdir = $self->{tmpdir};
786 my $setup = test_setup($tmpdir, 'cmds');
787
788 my $test_file = File::Spec->rel2abs("$tmpdir/test.dat");
789 if (open(my $fh, "> $test_file")) {
790 print $fh "\r" x 32768;
791
792 unless (close($fh)) {
793 die("Unable to write $test_file: $!");
794 }
795
796 } else {
797 die("Unable to open $test_file: $!");
798 }
799
800 # Calculate the MD5 checksum of these files, for comparison with the
801 # downloaded files.
802 #
803 # These are going to be "fun", since we are downloading these files in
804 # ASCII mode, which means that the data received by the client will not
805 # necessarily be the bytes on disk as written, due to ASCII translation.
806
807 my $test_file_md5;
808
809 my $md5_file = $test_file;
810 if ($^O eq 'darwin') {
811 # MacOSX-specific hack
812 $md5_file = '/private' . $test_file;
813 }
814
815 if (open(my $fh, "< $md5_file")) {
816 binmode($fh);
817
818 my $ctx = Digest::MD5->new();
819 $ctx->addfile($fh);
820 $test_file_md5 = $ctx->hexdigest();
821 close($fh);
822
823 } else {
824 die("Can't read $md5_file: $!");
825 }
826
827 my $test_file2 = File::Spec->rel2abs("$tmpdir/test2.dat");
828 if (open(my $fh, "> $test_file2")) {
829 print $fh "\n" x 32768;
830
831 unless (close($fh)) {
832 die("Unable to write $test_file2: $!");
833 }
834
835 } else {
836 die("Unable to open $test_file2: $!");
837 }
838
839 my $test_file2_md5;
840 $md5_file = $test_file2;
841 if ($^O eq 'darwin') {
842 # MacOSX-specific hack
843 $md5_file = '/private' . $test_file2;
844 }
845
846 if (open(my $fh, "< $md5_file")) {
847 binmode($fh);
848
849 my $ctx = Digest::MD5->new();
850 $ctx->addfile($fh);
851 $test_file2_md5 = $ctx->hexdigest();
852 close($fh);
853
854 } else {
855 die("Can't read $md5_file: $!");
856 }
857
858 my $test_file3 = File::Spec->rel2abs("$tmpdir/test3.dat");
859 if (open(my $fh, "> $test_file3")) {
860 print $fh "\r\n" x 32768;
861
862 unless (close($fh)) {
863 die("Unable to write $test_file3: $!");
864 }
865
866 } else {
867 die("Unable to open $test_file3: $!");
868 }
869
870 # Since File #3 is full of CRLF, and since we will be downloading these
871 # files in ASCII mode, which translates CRLF -> LF, and since File #2
872 # is already a file (of the same size!) full of only LF, we should then
873 # expect that the MD5 digest of data downloaded for File #3 to be the
874 # same as for File #2. Nice.
875 my $test_file3_md5 = $test_file2_md5;
876
877 my $config = {
878 PidFile => $setup->{pid_file},
879 ScoreboardFile => $setup->{scoreboard_file},
880 SystemLog => $setup->{log_file},
881
882 AuthUserFile => $setup->{auth_user_file},
883 AuthGroupFile => $setup->{auth_group_file},
884 AuthOrder => 'mod_auth_file.c',
885
886 IfModules => {
887 'mod_delay.c' => {
888 DelayEngine => 'off',
889 },
890 },
891 };
892
893 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
894 $config);
895
896 # Open pipes, for use between the parent and child processes. Specifically,
897 # the child will indicate when it's done with its test by writing a message
898 # to the parent.
899 my ($rfh, $wfh);
900 unless (pipe($rfh, $wfh)) {
901 die("Can't open pipe: $!");
902 }
903
904 my $ex;
905
906 # Fork child
907 $self->handle_sigchld();
908 defined(my $pid = fork()) or die("Can't fork: $!");
909 if ($pid) {
910 eval {
911 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
912 $client->login($setup->{user}, $setup->{passwd});
913 $client->type('ascii');
914
915 # Download a file of all CRs
916 my $conn = $client->retr_raw($test_file);
917 unless ($conn) {
918 die("RETR failed: " . $client->response_code() . " " .
919 $client->response_msg());
920 }
921
922 my $ctx = Digest::MD5->new();
923 my $buf = '';
924 my $tmp;
925 while ($conn->read($tmp, 8192, 25)) {
926 $ctx->add($tmp);
927 $buf .= $tmp;
928 }
929 eval { $conn->close() };
930
931 my $size = length($buf);
932 my $expected = -s $test_file;
933 $self->assert($expected == $size,
934 test_msg("Expected file #1 size $expected, got $size"));
935
936 my $md5 = $ctx->hexdigest();
937 $self->assert($md5 eq $test_file_md5,
938 test_msg("Expected file #1 MD5 $test_file_md5, got $md5"));
939
940 # Download a file of all LFs
941 $conn = $client->retr_raw($test_file2);
942 unless ($conn) {
943 die("RETR failed: " . $client->response_code() . " " .
944 $client->response_msg());
945 }
946
947 $ctx = Digest::MD5->new();
948 $buf = '';
949 while ($conn->read($tmp, 8192, 25)) {
950 $ctx->add($tmp);
951 $buf .= $tmp;
952 }
953 eval { $conn->close() };
954
955 $size = length($buf);
956 $expected = -s $test_file2;
957 $self->assert($expected == $size,
958 test_msg("Expected file #2 size $expected, got $size"));
959
960 $md5 = $ctx->hexdigest();
961 $self->assert($md5 eq $test_file2_md5,
962 test_msg("Expected file #2 MD5 $test_file2_md5, got $md5"));
963
964 # Download a file of all CRLF pairs
965 $conn = $client->retr_raw($test_file3);
966 unless ($conn) {
967 die("RETR failed: " . $client->response_code() . " " .
968 $client->response_msg());
969 }
970
971 $ctx = Digest::MD5->new();
972 $buf = '';
973 while ($conn->read($tmp, 8192, 25)) {
974 $ctx->add($tmp);
975 $buf .= $tmp;
976 }
977 eval { $conn->close() };
978
979 $size = length($buf);
980
981 # Note: the expected size here is half the length of the file. Why?
982 # The test3.dat file is comprised of 32K "\r\n" pairs. As such, the
983 # will SEND that data as is. And on the client end, since we are a Unix
984 # machine, they will be converted to just "\n" (32K of them) -- thus
985 # half the original file size.
986 $expected = (-s $test_file3) / 2;
987 $self->assert($expected == $size,
988 test_msg("Expected file #3 size $expected, got $size"));
989
990 $md5 = $ctx->hexdigest();
991 $self->assert($md5 eq $test_file3_md5,
992 test_msg("Expected file #3 MD5 $test_file3_md5, got $md5"));
993
994 $client->quit();
995 };
996 if ($@) {
997 $ex = $@;
998 }
999
1000 $wfh->print("done\n");
1001 $wfh->flush();
1002
1003 } else {
1004 eval { server_wait($setup->{config_file}, $rfh) };
1005 if ($@) {
1006 warn($@);
1007 exit 1;
1008 }
1009
1010 exit 0;
1011 }
1012
1013 # Stop server
1014 server_stop($setup->{pid_file});
1015 $self->assert_child_ok($pid);
1016
1017 test_cleanup($setup->{log_file}, $ex);
1018 }
1019
1020 sub retr_ok_ascii_file_bug4277 {
1021 my $self = shift;
1022 my $tmpdir = $self->{tmpdir};
1023 my $setup = test_setup($tmpdir, 'cmds');
1024
1025 # Use Digest::MD5 to verify the absence of corruption.
1026 use Digest::MD5;
1027
1028 my $test_file1 = File::Spec->rel2abs("$tmpdir/test.dat");
1029 if (open(my $fh, "> $test_file1")) {
1030 print $fh "\r" x 32768;
1031
1032 unless (close($fh)) {
1033 die("Unable to write $test_file1: $!");
1034 }
1035
1036 } else {
1037 die("Unable to open $test_file1: $!");
1038 }
1039
1040 my $test_file1_digest;
1041 if (open(my $fh, "< $test_file1")) {
1042 my $ctx = Digest::MD5->new();
1043 $ctx->addfile($fh);
1044 $test_file1_digest = lc($ctx->hexdigest);
1045 close($fh);
1046
1047 } else {
1048 die("Can't open $test_file1: $!");
1049 }
1050
1051 my $test_file2 = File::Spec->rel2abs("$tmpdir/test2.dat");
1052 if (open(my $fh, "> $test_file2")) {
1053 print $fh "\n" x 32768;
1054
1055 unless (close($fh)) {
1056 die("Unable to write $test_file2: $!");
1057 }
1058
1059 } else {
1060 die("Unable to open $test_file2: $!");
1061 }
1062
1063 my $test_file2_digest;
1064 if (open(my $fh, "< $test_file2")) {
1065 my $ctx = Digest::MD5->new();
1066 $ctx->addfile($fh);
1067 $test_file2_digest = lc($ctx->hexdigest);
1068 close($fh);
1069
1070 } else {
1071 die("Can't open $test_file2: $!");
1072 }
1073
1074 my $test_file3 = File::Spec->rel2abs("$tmpdir/test3.dat");
1075 if (open(my $fh, "> $test_file3")) {
1076 print $fh "\r\n" x 32768;
1077
1078 unless (close($fh)) {
1079 die("Unable to write $test_file3: $!");
1080 }
1081
1082 } else {
1083 die("Unable to open $test_file3: $!");
1084 }
1085
1086 # Since UNIX uses LF, not CRLF, the EXPECTED digest for $test_file3 will
1087 # be the same as for $test_file2.
1088 my $test_file3_digest = $test_file2_digest;
1089
1090 my $config = {
1091 PidFile => $setup->{pid_file},
1092 ScoreboardFile => $setup->{scoreboard_file},
1093 SystemLog => $setup->{log_file},
1094
1095 AuthUserFile => $setup->{auth_user_file},
1096 AuthGroupFile => $setup->{auth_group_file},
1097 AuthOrder => 'mod_auth_file.c',
1098
1099 IfModules => {
1100 'mod_delay.c' => {
1101 DelayEngine => 'off',
1102 },
1103 },
1104 };
1105
1106 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1107 $config);
1108
1109 # Open pipes, for use between the parent and child processes. Specifically,
1110 # the child will indicate when it's done with its test by writing a message
1111 # to the parent.
1112 my ($rfh, $wfh);
1113 unless (pipe($rfh, $wfh)) {
1114 die("Can't open pipe: $!");
1115 }
1116
1117 my $ex;
1118
1119 # Fork child
1120 $self->handle_sigchld();
1121 defined(my $pid = fork()) or die("Can't fork: $!");
1122 if ($pid) {
1123 eval {
1124 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1125 $client->login($setup->{user}, $setup->{passwd});
1126 $client->type('ascii');
1127
1128 # Download a file of all CRs
1129 my $conn = $client->retr_raw($test_file1);
1130 unless ($conn) {
1131 die("RETR failed: " . $client->response_code() . " " .
1132 $client->response_msg());
1133 }
1134
1135 my $buf = '';
1136 my $tmp;
1137 while ($conn->read($tmp, 8192, 25)) {
1138 $buf .= $tmp;
1139 }
1140 eval { $conn->close() };
1141
1142 my $size = length($buf);
1143 my $expected = -s $test_file1;
1144 $self->assert($expected == $size,
1145 test_msg("Expected size $expected, got $size"));
1146
1147 my $ctx = Digest::MD5->new();
1148 $ctx->add($buf);
1149 my $digest = lc($ctx->hexdigest);
1150 $self->assert($digest eq $test_file1_digest,
1151 test_msg("Expected MD5 $test_file1_digest, got $digest"));
1152
1153 # Download a file of all LFs
1154 $conn = $client->retr_raw($test_file2);
1155 unless ($conn) {
1156 die("RETR failed: " . $client->response_code() . " " .
1157 $client->response_msg());
1158 }
1159
1160 $buf = '';
1161 $tmp = '';
1162 while ($conn->read($tmp, 8192, 25)) {
1163 $buf .= $tmp;
1164 }
1165 eval { $conn->close() };
1166
1167 $size = length($buf);
1168 $expected = -s $test_file2;
1169 $self->assert($expected == $size,
1170 test_msg("Expected size $expected, got $size"));
1171
1172 $ctx = Digest::MD5->new();
1173 $ctx->add($buf);
1174 $digest = lc($ctx->hexdigest);
1175 $self->assert($digest eq $test_file2_digest,
1176 test_msg("Expected MD5 $test_file2_digest, got $digest"));
1177
1178 # Download a file of all CRLF pairs
1179 $conn = $client->retr_raw($test_file3);
1180 unless ($conn) {
1181 die("RETR failed: " . $client->response_code() . " " .
1182 $client->response_msg());
1183 }
1184
1185 $buf = '';
1186 $tmp = '';
1187 while ($conn->read($tmp, 8192, 25)) {
1188 $buf .= $tmp;
1189 }
1190 eval { $conn->close() };
1191
1192 $size = length($buf);
1193
1194 # Note: the expected size here is half the length of the file. Why?
1195 # The test3.dat file is comprised of 32K "\r\n" pairs. As such, the
1196 # will SEND that data as is. And on the client end, since we are a Unix
1197 # machine, they will be converted to just "\n" (32K of them) -- thus
1198 # half the original file size.
1199 $expected = (-s $test_file3) / 2;
1200 $self->assert($expected == $size,
1201 test_msg("Expected size $expected, got $size"));
1202
1203 $ctx = Digest::MD5->new();
1204 $ctx->add($buf);
1205 $digest = lc($ctx->hexdigest);
1206 $self->assert($digest eq $test_file3_digest,
1207 test_msg("Expected MD5 $test_file3_digest, got $digest"));
1208
1209 $client->quit();
1210 };
1211
1212 if ($@) {
1213 $ex = $@;
1214 }
1215
1216 $wfh->print("done\n");
1217 $wfh->flush();
1218
1219 } else {
1220 eval { server_wait($setup->{config_file}, $rfh) };
1221 if ($@) {
1222 warn($@);
1223 exit 1;
1224 }
1225
1226 exit 0;
1227 }
1228
1229 # Stop server
1230 server_stop($setup->{pid_file});
1231 $self->assert_child_ok($pid);
1232
1233 test_cleanup($setup->{log_file}, $ex);
1234 }
1235
1236 sub retr_abs_symlink {
1237 my $self = shift;
1238 my $tmpdir = $self->{tmpdir};
1239 my $setup = test_setup($tmpdir, 'cmds');
1240
1241 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
1242 mkpath($test_dir);
1243
1244 my $test_file = File::Spec->rel2abs("$test_dir/foo");
1245 if (open(my $fh, "> $test_file")) {
1246 print $fh "Foo!\n";
1247
1248 unless (close($fh)) {
1249 die("Unable to write $test_file: $!");
1250 }
1251
1252 } else {
1253 die("Unable to open $test_file: $!");
1254 }
1255
1256 my $test_symlink = File::Spec->rel2abs("$test_dir/test.lnk");
1257
1258 my $dst_path = $test_file;
1259 if ($^O eq 'darwin') {
1260 # MacOSX-specific hack
1261 $dst_path = '/private' . $dst_path;
1262 }
1263
1264 unless (symlink($dst_path, $test_symlink)) {
1265 die("Can't symlink '$test_symlink' to '$dst_path': $!");
1266 }
1267
1268 my $config = {
1269 PidFile => $setup->{pid_file},
1270 ScoreboardFile => $setup->{scoreboard_file},
1271 SystemLog => $setup->{log_file},
1272
1273 AuthUserFile => $setup->{auth_user_file},
1274 AuthGroupFile => $setup->{auth_group_file},
1275 AuthOrder => 'mod_auth_file.c',
1276
1277 IfModules => {
1278 'mod_delay.c' => {
1279 DelayEngine => 'off',
1280 },
1281 },
1282 };
1283
1284 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1285 $config);
1286
1287 # Open pipes, for use between the parent and child processes. Specifically,
1288 # the child will indicate when it's done with its test by writing a message
1289 # to the parent.
1290 my ($rfh, $wfh);
1291 unless (pipe($rfh, $wfh)) {
1292 die("Can't open pipe: $!");
1293 }
1294
1295 my $ex;
1296
1297 # Fork child
1298 $self->handle_sigchld();
1299 defined(my $pid = fork()) or die("Can't fork: $!");
1300 if ($pid) {
1301 eval {
1302 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1303 $client->login($setup->{user}, $setup->{passwd});
1304 $client->type('binary');
1305
1306 my $path = 'test.d/test.lnk';
1307 my $conn = $client->retr_raw($path);
1308 unless ($conn) {
1309 die("RETR failed: " . $client->response_code() . " " .
1310 $client->response_msg());
1311 }
1312
1313 my $buf;
1314 $conn->read($buf, 8192, 30);
1315 my $size = $conn->bytes_read();
1316 eval { $conn->close() };
1317
1318 my $resp_code = $client->response_code();
1319 my $resp_msg = $client->response_msg();
1320 $self->assert_transfer_ok($resp_code, $resp_msg);
1321 $client->quit();
1322
1323 my $expected = 5;
1324 $self->assert($expected == $size,
1325 test_msg("Expected size $expected, got $size"));
1326 };
1327
1328 if ($@) {
1329 $ex = $@;
1330 }
1331
1332 $wfh->print("done\n");
1333 $wfh->flush();
1334
1335 } else {
1336 eval { server_wait($setup->{config_file}, $rfh) };
1337 if ($@) {
1338 warn($@);
1339 exit 1;
1340 }
1341
1342 exit 0;
1343 }
1344
1345 # Stop server
1346 server_stop($setup->{pid_file});
1347
1348 $self->assert_child_ok($pid);
1349
1350 test_cleanup($setup->{log_file}, $ex);
1351 }
1352
1353 sub retr_abs_symlink_chrooted_bug4219 {
1354 my $self = shift;
1355 my $tmpdir = $self->{tmpdir};
1356 my $setup = test_setup($tmpdir, 'cmds');
1357
1358 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
1359 mkpath($test_dir);
1360
1361 my $test_file = File::Spec->rel2abs("$test_dir/foo");
1362 if (open(my $fh, "> $test_file")) {
1363 print $fh "Foo!\n";
1364
1365 unless (close($fh)) {
1366 die("Unable to write $test_file: $!");
1367 }
1368
1369 } else {
1370 die("Unable to open $test_file: $!");
1371 }
1372
1373 my $test_symlink = File::Spec->rel2abs("$test_dir/test.lnk");
1374
1375 my $dst_path = $test_file;
1376 if ($^O eq 'darwin') {
1377 # MacOSX-specific hack
1378 $dst_path = '/private' . $dst_path;
1379 }
1380
1381 unless (symlink($dst_path, $test_symlink)) {
1382 die("Can't symlink '$test_symlink' to '$dst_path': $!");
1383 }
1384
1385 my $config = {
1386 PidFile => $setup->{pid_file},
1387 ScoreboardFile => $setup->{scoreboard_file},
1388 SystemLog => $setup->{log_file},
1389
1390 AuthUserFile => $setup->{auth_user_file},
1391 AuthGroupFile => $setup->{auth_group_file},
1392 AuthOrder => 'mod_auth_file.c',
1393
1394 DefaultRoot => '~',
1395
1396 IfModules => {
1397 'mod_delay.c' => {
1398 DelayEngine => 'off',
1399 },
1400 },
1401 };
1402
1403 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1404 $config);
1405
1406 # Open pipes, for use between the parent and child processes. Specifically,
1407 # the child will indicate when it's done with its test by writing a message
1408 # to the parent.
1409 my ($rfh, $wfh);
1410 unless (pipe($rfh, $wfh)) {
1411 die("Can't open pipe: $!");
1412 }
1413
1414 my $ex;
1415
1416 # Fork child
1417 $self->handle_sigchld();
1418 defined(my $pid = fork()) or die("Can't fork: $!");
1419 if ($pid) {
1420 eval {
1421 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1422 $client->login($setup->{user}, $setup->{passwd});
1423 $client->type('binary');
1424
1425 my $path = 'test.d/test.lnk';
1426 my $conn = $client->retr_raw($path);
1427 unless ($conn) {
1428 die("RETR failed: " . $client->response_code() . " " .
1429 $client->response_msg());
1430 }
1431
1432 my $buf;
1433 $conn->read($buf, 8192, 30);
1434 my $size = $conn->bytes_read();
1435 eval { $conn->close() };
1436
1437 my $resp_code = $client->response_code();
1438 my $resp_msg = $client->response_msg();
1439 $self->assert_transfer_ok($resp_code, $resp_msg);
1440 $client->quit();
1441
1442 my $expected = 5;
1443 $self->assert($expected == $size,
1444 test_msg("Expected size $expected, got $size"));
1445 };
1446
1447 if ($@) {
1448 $ex = $@;
1449 }
1450
1451 $wfh->print("done\n");
1452 $wfh->flush();
1453
1454 } else {
1455 eval { server_wait($setup->{config_file}, $rfh) };
1456 if ($@) {
1457 warn($@);
1458 exit 1;
1459 }
1460
1461 exit 0;
1462 }
1463
1464 # Stop server
1465 server_stop($setup->{pid_file});
1466
1467 $self->assert_child_ok($pid);
1468
1469 test_cleanup($setup->{log_file}, $ex);
1470 }
1471
1472 sub retr_rel_symlink {
1473 my $self = shift;
1474 my $tmpdir = $self->{tmpdir};
1475 my $setup = test_setup($tmpdir, 'cmds');
1476
1477 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
1478 mkpath($test_dir);
1479
1480 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
1481 if (open(my $fh, "> $test_file")) {
1482 print $fh "Foo!\n";
1483
1484 unless (close($fh)) {
1485 die("Unable to write $test_file: $!");
1486 }
1487
1488 } else {
1489 die("Unable to open $test_file: $!");
1490 }
1491
1492 # Change to the test directory in order to create a relative path in the
1493 # symlink we need
1494
1495 my $cwd = getcwd();
1496 unless (chdir($test_dir)) {
1497 die("Can't chdir to $test_dir: $!");
1498 }
1499
1500 unless (symlink('./test.txt', './test.lnk')) {
1501 die("Can't symlink 'test.lnk' to './test.txt': $!");
1502 }
1503
1504 unless (chdir($cwd)) {
1505 die("Can't chdir to $cwd: $!");
1506 }
1507
1508 my $config = {
1509 PidFile => $setup->{pid_file},
1510 ScoreboardFile => $setup->{scoreboard_file},
1511 SystemLog => $setup->{log_file},
1512
1513 AuthUserFile => $setup->{auth_user_file},
1514 AuthGroupFile => $setup->{auth_group_file},
1515 AuthOrder => 'mod_auth_file.c',
1516
1517 IfModules => {
1518 'mod_delay.c' => {
1519 DelayEngine => 'off',
1520 },
1521 },
1522 };
1523
1524 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1525 $config);
1526
1527 # Open pipes, for use between the parent and child processes. Specifically,
1528 # the child will indicate when it's done with its test by writing a message
1529 # to the parent.
1530 my ($rfh, $wfh);
1531 unless (pipe($rfh, $wfh)) {
1532 die("Can't open pipe: $!");
1533 }
1534
1535 my $ex;
1536
1537 # Fork child
1538 $self->handle_sigchld();
1539 defined(my $pid = fork()) or die("Can't fork: $!");
1540 if ($pid) {
1541 eval {
1542 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1543 $client->login($setup->{user}, $setup->{passwd});
1544 $client->type('binary');
1545
1546 my $path = 'test.d/test.lnk';
1547 my $conn = $client->retr_raw($path);
1548 unless ($conn) {
1549 die("RETR failed: " . $client->response_code() . " " .
1550 $client->response_msg());
1551 }
1552
1553 my $buf;
1554 $conn->read($buf, 8192, 30);
1555 my $size = $conn->bytes_read();
1556 eval { $conn->close() };
1557
1558 my $resp_code = $client->response_code();
1559 my $resp_msg = $client->response_msg();
1560 $self->assert_transfer_ok($resp_code, $resp_msg);
1561 $client->quit();
1562
1563 my $expected = 5;
1564 $self->assert($expected == $size,
1565 test_msg("Expected size $expected, got $size"));
1566 };
1567
1568 if ($@) {
1569 $ex = $@;
1570 }
1571
1572 $wfh->print("done\n");
1573 $wfh->flush();
1574
1575 } else {
1576 eval { server_wait($setup->{config_file}, $rfh) };
1577 if ($@) {
1578 warn($@);
1579 exit 1;
1580 }
1581
1582 exit 0;
1583 }
1584
1585 # Stop server
1586 server_stop($setup->{pid_file});
1587
1588 $self->assert_child_ok($pid);
1589
1590 test_cleanup($setup->{log_file}, $ex);
1591 }
1592
1593 sub retr_rel_symlink_chrooted_bug4219 {
1594 my $self = shift;
1595 my $tmpdir = $self->{tmpdir};
1596 my $setup = test_setup($tmpdir, 'cmds');
1597
1598 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
1599 mkpath($test_dir);
1600
1601 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
1602 if (open(my $fh, "> $test_file")) {
1603 print $fh "Foo!\n";
1604
1605 unless (close($fh)) {
1606 die("Unable to write $test_file: $!");
1607 }
1608
1609 } else {
1610 die("Unable to open $test_file: $!");
1611 }
1612
1613 # Change to the test directory in order to create a relative path in the
1614 # symlink we need
1615
1616 my $cwd = getcwd();
1617 unless (chdir($test_dir)) {
1618 die("Can't chdir to $test_dir: $!");
1619 }
1620
1621 unless (symlink('./test.txt', './test.lnk')) {
1622 die("Can't symlink 'test.lnk' to './test.txt': $!");
1623 }
1624
1625 unless (chdir($cwd)) {
1626 die("Can't chdir to $cwd: $!");
1627 }
1628
1629 my $config = {
1630 PidFile => $setup->{pid_file},
1631 ScoreboardFile => $setup->{scoreboard_file},
1632 SystemLog => $setup->{log_file},
1633
1634 AuthUserFile => $setup->{auth_user_file},
1635 AuthGroupFile => $setup->{auth_group_file},
1636 AuthOrder => 'mod_auth_file.c',
1637
1638 DefaultRoot => '~',
1639
1640 IfModules => {
1641 'mod_delay.c' => {
1642 DelayEngine => 'off',
1643 },
1644 },
1645 };
1646
1647 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1648 $config);
1649
1650 # Open pipes, for use between the parent and child processes. Specifically,
1651 # the child will indicate when it's done with its test by writing a message
1652 # to the parent.
1653 my ($rfh, $wfh);
1654 unless (pipe($rfh, $wfh)) {
1655 die("Can't open pipe: $!");
1656 }
1657
1658 my $ex;
1659
1660 # Fork child
1661 $self->handle_sigchld();
1662 defined(my $pid = fork()) or die("Can't fork: $!");
1663 if ($pid) {
1664 eval {
1665 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1666 $client->login($setup->{user}, $setup->{passwd});
1667 $client->type('binary');
1668
1669 my $path = 'test.d/test.lnk';
1670 my $conn = $client->retr_raw($path);
1671 unless ($conn) {
1672 die("RETR failed: " . $client->response_code() . " " .
1673 $client->response_msg());
1674 }
1675
1676 my $buf;
1677 $conn->read($buf, 8192, 30);
1678 my $size = $conn->bytes_read();
1679 eval { $conn->close() };
1680
1681 my $resp_code = $client->response_code();
1682 my $resp_msg = $client->response_msg();
1683 $self->assert_transfer_ok($resp_code, $resp_msg);
1684 $client->quit();
1685
1686 my $expected = 5;
1687 $self->assert($expected == $size,
1688 test_msg("Expected size $expected, got $size"));
1689 };
1690
1691 if ($@) {
1692 $ex = $@;
1693 }
1694
1695 $wfh->print("done\n");
1696 $wfh->flush();
1697
1698 } else {
1699 eval { server_wait($setup->{config_file}, $rfh) };
1700 if ($@) {
1701 warn($@);
1702 exit 1;
1703 }
1704
1705 exit 0;
1706 }
1707
1708 # Stop server
1709 server_stop($setup->{pid_file});
1710
1711 $self->assert_child_ok($pid);
1712
1713 test_cleanup($setup->{log_file}, $ex);
1714 }
1715
1716 sub retr_fails_not_reg {
1717 my $self = shift;
1718 my $tmpdir = $self->{tmpdir};
1719 my $setup = test_setup($tmpdir, 'cmds');
1720
1721 my $config = {
1722 PidFile => $setup->{pid_file},
1723 ScoreboardFile => $setup->{scoreboard_file},
1724 SystemLog => $setup->{log_file},
1725
1726 AuthUserFile => $setup->{auth_user_file},
1727 AuthGroupFile => $setup->{auth_group_file},
1728 AuthOrder => 'mod_auth_file.c',
1729
1730 IfModules => {
1731 'mod_delay.c' => {
1732 DelayEngine => 'off',
1733 },
1734 },
1735 };
1736
1737 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1738 $config);
1739
1740 # Open pipes, for use between the parent and child processes. Specifically,
1741 # the child will indicate when it's done with its test by writing a message
1742 # to the parent.
1743 my ($rfh, $wfh);
1744 unless (pipe($rfh, $wfh)) {
1745 die("Can't open pipe: $!");
1746 }
1747
1748 my $ex;
1749
1750 # Fork child
1751 $self->handle_sigchld();
1752 defined(my $pid = fork()) or die("Can't fork: $!");
1753 if ($pid) {
1754 eval {
1755 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1756 $client->login($setup->{user}, $setup->{passwd});
1757
1758 my $conn = $client->retr_raw($setup->{home_dir});
1759 my $resp_code = $client->response_code();
1760 my $resp_msg = $client->response_msg();
1761
1762 if ($conn) {
1763 die("RETR succeeded unexpectedly ($resp_code $resp_msg)");
1764 }
1765
1766 my $expected;
1767
1768 $expected = 550;
1769 $self->assert($expected == $resp_code,
1770 test_msg("Expected response code $expected, got $resp_code"));
1771
1772 $expected = "$setup->{home_dir}: Not a regular file";
1773 $self->assert($expected eq $resp_msg,
1774 test_msg("Expected response message '$expected', got '$resp_msg'"));
1775 };
1776
1777 if ($@) {
1778 $ex = $@;
1779 }
1780
1781 $wfh->print("done\n");
1782 $wfh->flush();
1783
1784 } else {
1785 eval { server_wait($setup->{config_file}, $rfh) };
1786 if ($@) {
1787 warn($@);
1788 exit 1;
1789 }
1790
1791 exit 0;
1792 }
1793
1794 # Stop server
1795 server_stop($setup->{pid_file});
1796
1797 $self->assert_child_ok($pid);
1798
1799 test_cleanup($setup->{log_file}, $ex);
1800 }
1801
1802 sub retr_fails_login_required {
1803 my $self = shift;
1804 my $tmpdir = $self->{tmpdir};
1805 my $setup = test_setup($tmpdir, 'cmds');
1806
1807 my $config = {
1808 PidFile => $setup->{pid_file},
1809 ScoreboardFile => $setup->{scoreboard_file},
1810 SystemLog => $setup->{log_file},
1811
1812 IfModules => {
1813 'mod_delay.c' => {
1814 DelayEngine => 'off',
1815 },
1816 },
1817 };
1818
1819 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1820 $config);
1821
1822 # Open pipes, for use between the parent and child processes. Specifically,
1823 # the child will indicate when it's done with its test by writing a message
1824 # to the parent.
1825 my ($rfh, $wfh);
1826 unless (pipe($rfh, $wfh)) {
1827 die("Can't open pipe: $!");
1828 }
1829
1830 my $ex;
1831
1832 # Fork child
1833 $self->handle_sigchld();
1834 defined(my $pid = fork()) or die("Can't fork: $!");
1835 if ($pid) {
1836 eval {
1837 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1838
1839 my ($resp_code, $resp_msg);
1840 eval { ($resp_code, $resp_msg) = $client->retr() };
1841 unless ($@) {
1842 die("RETR succeeded unexpectedly ($resp_code $resp_msg)");
1843 }
1844
1845 $resp_code = $client->response_code();
1846 $resp_msg = $client->response_msg();
1847
1848 my $expected;
1849
1850 $expected = 530;
1851 $self->assert($expected == $resp_code,
1852 test_msg("Expected response code $expected, got $resp_code"));
1853
1854 $expected = "Please login with USER and PASS";
1855 $self->assert($expected eq $resp_msg,
1856 test_msg("Expected response message '$expected', got '$resp_msg'"));
1857 };
1858
1859 if ($@) {
1860 $ex = $@;
1861 }
1862
1863 $wfh->print("done\n");
1864 $wfh->flush();
1865
1866 } else {
1867 eval { server_wait($setup->{config_file}, $rfh) };
1868 if ($@) {
1869 warn($@);
1870 exit 1;
1871 }
1872
1873 exit 0;
1874 }
1875
1876 # Stop server
1877 server_stop($setup->{pid_file});
1878
1879 $self->assert_child_ok($pid);
1880
1881 test_cleanup($setup->{log_file}, $ex);
1882 }
1883
1884 sub retr_fails_no_path {
1885 my $self = shift;
1886 my $tmpdir = $self->{tmpdir};
1887 my $setup = test_setup($tmpdir, 'cmds');
1888
1889 my $config = {
1890 PidFile => $setup->{pid_file},
1891 ScoreboardFile => $setup->{scoreboard_file},
1892 SystemLog => $setup->{log_file},
1893
1894 AuthUserFile => $setup->{auth_user_file},
1895 AuthGroupFile => $setup->{auth_group_file},
1896 AuthOrder => 'mod_auth_file.c',
1897
1898 IfModules => {
1899 'mod_delay.c' => {
1900 DelayEngine => 'off',
1901 },
1902 },
1903 };
1904
1905 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1906 $config);
1907
1908 # Open pipes, for use between the parent and child processes. Specifically,
1909 # the child will indicate when it's done with its test by writing a message
1910 # to the parent.
1911 my ($rfh, $wfh);
1912 unless (pipe($rfh, $wfh)) {
1913 die("Can't open pipe: $!");
1914 }
1915
1916 my $ex;
1917
1918 # Fork child
1919 $self->handle_sigchld();
1920 defined(my $pid = fork()) or die("Can't fork: $!");
1921 if ($pid) {
1922 eval {
1923 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
1924 $client->login($setup->{user}, $setup->{passwd});
1925
1926 my $conn = $client->retr_raw();
1927
1928 my $resp_code = $client->response_code();
1929 my $resp_msg = $client->response_msg();
1930
1931 if ($conn) {
1932 die("RETR succeeded unexpectedly ($resp_code $resp_msg)");
1933 }
1934
1935 my $expected;
1936
1937 $expected = 500;
1938 $self->assert($expected == $resp_code,
1939 test_msg("Expected response code $expected, got $resp_code"));
1940
1941 $expected = "'RETR' not understood";
1942 $self->assert($expected eq $resp_msg,
1943 test_msg("Expected response message '$expected', got '$resp_msg'"));
1944 };
1945
1946 if ($@) {
1947 $ex = $@;
1948 }
1949
1950 print $wfh "done\n";
1951
1952 } else {
1953 eval { server_wait($setup->{config_file}, $rfh) };
1954 if ($@) {
1955 warn($@);
1956 exit 1;
1957 }
1958
1959 exit 0;
1960 }
1961
1962 # Stop server
1963 server_stop($setup->{pid_file});
1964
1965 $self->assert_child_ok($pid);
1966
1967 test_cleanup($setup->{log_file}, $ex);
1968 }
1969
1970 sub retr_fails_enoent {
1971 my $self = shift;
1972 my $tmpdir = $self->{tmpdir};
1973 my $setup = test_setup($tmpdir, 'cmds');
1974
1975 my $config = {
1976 PidFile => $setup->{pid_file},
1977 ScoreboardFile => $setup->{scoreboard_file},
1978 SystemLog => $setup->{log_file},
1979
1980 AuthUserFile => $setup->{auth_user_file},
1981 AuthGroupFile => $setup->{auth_group_file},
1982 AuthOrder => 'mod_auth_file.c',
1983
1984 IfModules => {
1985 'mod_delay.c' => {
1986 DelayEngine => 'off',
1987 },
1988 },
1989 };
1990
1991 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1992 $config);
1993
1994 # Open pipes, for use between the parent and child processes. Specifically,
1995 # the child will indicate when it's done with its test by writing a message
1996 # to the parent.
1997 my ($rfh, $wfh);
1998 unless (pipe($rfh, $wfh)) {
1999 die("Can't open pipe: $!");
2000 }
2001
2002 my $ex;
2003
2004 # Fork child
2005 $self->handle_sigchld();
2006 defined(my $pid = fork()) or die("Can't fork: $!");
2007 if ($pid) {
2008 eval {
2009 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
2010 $client->login($setup->{user}, $setup->{passwd});
2011
2012 $client->port();
2013
2014 my $test_file = 'foo/bar/baz';
2015
2016 my ($resp_code, $resp_msg);
2017 eval { ($resp_code, $resp_msg) = $client->retr($test_file) };
2018 unless ($@) {
2019 die("RETR succeeded unexpectedly ($resp_code $resp_msg)");
2020 }
2021
2022 $resp_code = $client->response_code();
2023 $resp_msg = $client->response_msg();
2024
2025 my $expected;
2026
2027 $expected = 550;
2028 $self->assert($expected == $resp_code,
2029 test_msg("Expected response code $expected, got $resp_code"));
2030
2031 $expected = "$test_file: No such file or directory";
2032 $self->assert($expected eq $resp_msg,
2033 test_msg("Expected response message '$expected', got '$resp_msg'"));
2034 };
2035
2036 if ($@) {
2037 $ex = $@;
2038 }
2039
2040 $wfh->print("done\n");
2041 $wfh->flush();
2042
2043 } else {
2044 eval { server_wait($setup->{config_file}, $rfh) };
2045 if ($@) {
2046 warn($@);
2047 exit 1;
2048 }
2049
2050 exit 0;
2051 }
2052
2053 # Stop server
2054 server_stop($setup->{pid_file});
2055
2056 $self->assert_child_ok($pid);
2057
2058 test_cleanup($setup->{log_file}, $ex);
2059 }
2060
2061 sub retr_fails_enoent_glob {
2062 my $self = shift;
2063 my $tmpdir = $self->{tmpdir};
2064 my $setup = test_setup($tmpdir, 'cmds');
2065
2066 my $config = {
2067 PidFile => $setup->{pid_file},
2068 ScoreboardFile => $setup->{scoreboard_file},
2069 SystemLog => $setup->{log_file},
2070
2071 AuthUserFile => $setup->{auth_user_file},
2072 AuthGroupFile => $setup->{auth_group_file},
2073 AuthOrder => 'mod_auth_file.c',
2074
2075 IfModules => {
2076 'mod_delay.c' => {
2077 DelayEngine => 'off',
2078 },
2079 },
2080 };
2081
2082 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2083 $config);
2084
2085 # Open pipes, for use between the parent and child processes. Specifically,
2086 # the child will indicate when it's done with its test by writing a message
2087 # to the parent.
2088 my ($rfh, $wfh);
2089 unless (pipe($rfh, $wfh)) {
2090 die("Can't open pipe: $!");
2091 }
2092
2093 my $ex;
2094
2095 # Fork child
2096 $self->handle_sigchld();
2097 defined(my $pid = fork()) or die("Can't fork: $!");
2098 if ($pid) {
2099 eval {
2100 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
2101 $client->login($setup->{user}, $setup->{passwd});
2102 $client->port();
2103
2104 my $test_glob = '*foo';
2105
2106 my ($resp_code, $resp_msg);
2107 eval { ($resp_code, $resp_msg) = $client->retr($test_glob) };
2108 unless ($@) {
2109 die("RETR succeeded unexpectedly ($resp_code $resp_msg)");
2110 }
2111
2112 $resp_code = $client->response_code();
2113 $resp_msg = $client->response_msg();
2114
2115 my $expected;
2116
2117 $expected = 550;
2118 $self->assert($expected == $resp_code,
2119 test_msg("Expected response code $expected, got $resp_code"));
2120
2121 $expected = "*foo: No such file or directory";
2122 $self->assert($expected eq $resp_msg,
2123 test_msg("Expected response message '$expected', got '$resp_msg'"));
2124 };
2125
2126 if ($@) {
2127 $ex = $@;
2128 }
2129
2130 $wfh->print("done\n");
2131 $wfh->flush();
2132
2133 } else {
2134 eval { server_wait($setup->{config_file}, $rfh) };
2135 if ($@) {
2136 warn($@);
2137 exit 1;
2138 }
2139
2140 exit 0;
2141 }
2142
2143 # Stop server
2144 server_stop($setup->{pid_file});
2145
2146 $self->assert_child_ok($pid);
2147
2148 test_cleanup($setup->{log_file}, $ex);
2149 }
2150
2151 sub retr_fails_abs_symlink_enoent {
2152 my $self = shift;
2153 my $tmpdir = $self->{tmpdir};
2154 my $setup = test_setup($tmpdir, 'cmds');
2155
2156 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
2157 mkpath($test_dir);
2158
2159 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
2160 my $test_symlink = File::Spec->rel2abs("$test_dir/test.lnk");
2161
2162 unless (symlink($test_file, $test_symlink)) {
2163 die("Can't symlink '$test_symlink' to '$test_file': $!");
2164 }
2165
2166 my $config = {
2167 PidFile => $setup->{pid_file},
2168 ScoreboardFile => $setup->{scoreboard_file},
2169 SystemLog => $setup->{log_file},
2170
2171 AuthUserFile => $setup->{auth_user_file},
2172 AuthGroupFile => $setup->{auth_group_file},
2173 AuthOrder => 'mod_auth_file.c',
2174
2175 IfModules => {
2176 'mod_delay.c' => {
2177 DelayEngine => 'off',
2178 },
2179 },
2180 };
2181
2182 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2183 $config);
2184
2185 # Open pipes, for use between the parent and child processes. Specifically,
2186 # the child will indicate when it's done with its test by writing a message
2187 # to the parent.
2188 my ($rfh, $wfh);
2189 unless (pipe($rfh, $wfh)) {
2190 die("Can't open pipe: $!");
2191 }
2192
2193 my $ex;
2194
2195 # Fork child
2196 $self->handle_sigchld();
2197 defined(my $pid = fork()) or die("Can't fork: $!");
2198 if ($pid) {
2199 eval {
2200 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
2201 $client->login($setup->{user}, $setup->{passwd});
2202
2203 my $path = 'test.d/test.lnk';
2204 my $conn = $client->retr_raw($path);
2205 if ($conn) {
2206 die("RETR succeeded unexpectedly");
2207 }
2208
2209 my $resp_code = $client->response_code();
2210 my $resp_msg = $client->response_msg();
2211 $client->quit();
2212
2213 my $expected = 550;
2214 $self->assert($expected == $resp_code,
2215 test_msg("Expected response code $expected, got $resp_code"));
2216
2217 $expected = "$path: No such file or directory";
2218 $self->assert($expected eq $resp_msg,
2219 test_msg("Expected response message '$expected', got '$resp_msg'"));
2220 };
2221
2222 if ($@) {
2223 $ex = $@;
2224 }
2225
2226 $wfh->print("done\n");
2227 $wfh->flush();
2228
2229 } else {
2230 eval { server_wait($setup->{config_file}, $rfh) };
2231 if ($@) {
2232 warn($@);
2233 exit 1;
2234 }
2235
2236 exit 0;
2237 }
2238
2239 # Stop server
2240 server_stop($setup->{pid_file});
2241
2242 $self->assert_child_ok($pid);
2243
2244 test_cleanup($setup->{log_file}, $ex);
2245 }
2246
2247 sub retr_fails_abs_symlink_enoent_chrooted_bug4219 {
2248 my $self = shift;
2249 my $tmpdir = $self->{tmpdir};
2250 my $setup = test_setup($tmpdir, 'cmds');
2251
2252 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
2253 mkpath($test_dir);
2254
2255 my $test_file = File::Spec->rel2abs("$test_dir/test.txt");
2256 my $test_symlink = File::Spec->rel2abs("$test_dir/test.lnk");
2257
2258 my $dst_path = $test_file;
2259 if ($^O eq 'darwin') {
2260 # MacOSX-specific hack
2261 $dst_path = '/private' . $dst_path;
2262 }
2263
2264 unless (symlink($dst_path, $test_symlink)) {
2265 die("Can't symlink '$test_symlink' to '$dst_path': $!");
2266 }
2267
2268 my $config = {
2269 PidFile => $setup->{pid_file},
2270 ScoreboardFile => $setup->{scoreboard_file},
2271 SystemLog => $setup->{log_file},
2272
2273 AuthUserFile => $setup->{auth_user_file},
2274 AuthGroupFile => $setup->{auth_group_file},
2275 AuthOrder => 'mod_auth_file.c',
2276
2277 DefaultRoot => '~',
2278
2279 IfModules => {
2280 'mod_delay.c' => {
2281 DelayEngine => 'off',
2282 },
2283 },
2284 };
2285
2286 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2287 $config);
2288
2289 # Open pipes, for use between the parent and child processes. Specifically,
2290 # the child will indicate when it's done with its test by writing a message
2291 # to the parent.
2292 my ($rfh, $wfh);
2293 unless (pipe($rfh, $wfh)) {
2294 die("Can't open pipe: $!");
2295 }
2296
2297 my $ex;
2298
2299 # Fork child
2300 $self->handle_sigchld();
2301 defined(my $pid = fork()) or die("Can't fork: $!");
2302 if ($pid) {
2303 eval {
2304 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
2305 $client->login($setup->{user}, $setup->{passwd});
2306
2307 my $path = 'test.d/test.lnk';
2308 my $conn = $client->retr_raw($path);
2309 if ($conn) {
2310 die("RETR succeeded unexpectedly");
2311 }
2312
2313 my $resp_code = $client->response_code();
2314 my $resp_msg = $client->response_msg();
2315 $client->quit();
2316
2317 my $expected = 550;
2318 $self->assert($expected == $resp_code,
2319 test_msg("Expected response code $expected, got $resp_code"));
2320
2321 $expected = "$path: No such file or directory";
2322 $self->assert($expected eq $resp_msg,
2323 test_msg("Expected response message '$expected', got '$resp_msg'"));
2324 };
2325
2326 if ($@) {
2327 $ex = $@;
2328 }
2329
2330 $wfh->print("done\n");
2331 $wfh->flush();
2332
2333 } else {
2334 eval { server_wait($setup->{config_file}, $rfh) };
2335 if ($@) {
2336 warn($@);
2337 exit 1;
2338 }
2339
2340 exit 0;
2341 }
2342
2343 # Stop server
2344 server_stop($setup->{pid_file});
2345
2346 $self->assert_child_ok($pid);
2347
2348 test_cleanup($setup->{log_file}, $ex);
2349 }
2350
2351 sub retr_fails_rel_symlink_enoent {
2352 my $self = shift;
2353 my $tmpdir = $self->{tmpdir};
2354 my $setup = test_setup($tmpdir, 'cmds');
2355
2356 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
2357 mkpath($test_dir);
2358
2359 # Change to the test directory in order to create a relative path in the
2360 # symlink we need
2361
2362 my $cwd = getcwd();
2363 unless (chdir($test_dir)) {
2364 die("Can't chdir to $test_dir: $!");
2365 }
2366
2367 unless (symlink('./test.txt', './test.lnk')) {
2368 die("Can't symlink 'test.lnk' to './test.txt': $!");
2369 }
2370
2371 unless (chdir($cwd)) {
2372 die("Can't chdir to $cwd: $!");
2373 }
2374
2375 my $config = {
2376 PidFile => $setup->{pid_file},
2377 ScoreboardFile => $setup->{scoreboard_file},
2378 SystemLog => $setup->{log_file},
2379
2380 AuthUserFile => $setup->{auth_user_file},
2381 AuthGroupFile => $setup->{auth_group_file},
2382 AuthOrder => 'mod_auth_file.c',
2383
2384 IfModules => {
2385 'mod_delay.c' => {
2386 DelayEngine => 'off',
2387 },
2388 },
2389 };
2390
2391 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2392 $config);
2393
2394 # Open pipes, for use between the parent and child processes. Specifically,
2395 # the child will indicate when it's done with its test by writing a message
2396 # to the parent.
2397 my ($rfh, $wfh);
2398 unless (pipe($rfh, $wfh)) {
2399 die("Can't open pipe: $!");
2400 }
2401
2402 my $ex;
2403
2404 # Fork child
2405 $self->handle_sigchld();
2406 defined(my $pid = fork()) or die("Can't fork: $!");
2407 if ($pid) {
2408 eval {
2409 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
2410 $client->login($setup->{user}, $setup->{passwd});
2411
2412 my $path = 'test.d/test.lnk';
2413 my $conn = $client->retr_raw($path);
2414 if ($conn) {
2415 die("RETR succeeded unexpectedly");
2416 }
2417
2418 my $resp_code = $client->response_code();
2419 my $resp_msg = $client->response_msg();
2420 $client->quit();
2421
2422 my $expected = 550;
2423 $self->assert($expected == $resp_code,
2424 test_msg("Expected response code $expected, got $resp_code"));
2425
2426 $expected = "$path: No such file or directory";
2427 $self->assert($expected eq $resp_msg,
2428 test_msg("Expected response message '$expected', got '$resp_msg'"));
2429 };
2430
2431 if ($@) {
2432 $ex = $@;
2433 }
2434
2435 $wfh->print("done\n");
2436 $wfh->flush();
2437
2438 } else {
2439 eval { server_wait($setup->{config_file}, $rfh) };
2440 if ($@) {
2441 warn($@);
2442 exit 1;
2443 }
2444
2445 exit 0;
2446 }
2447
2448 # Stop server
2449 server_stop($setup->{pid_file});
2450
2451 $self->assert_child_ok($pid);
2452
2453 test_cleanup($setup->{log_file}, $ex);
2454 }
2455
2456 sub retr_fails_rel_symlink_enoent_chrooted_bug4219 {
2457 my $self = shift;
2458 my $tmpdir = $self->{tmpdir};
2459 my $setup = test_setup($tmpdir, 'cmds');
2460
2461 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
2462 mkpath($test_dir);
2463
2464 # Change to the test directory in order to create a relative path in the
2465 # symlink we need
2466
2467 my $cwd = getcwd();
2468 unless (chdir($test_dir)) {
2469 die("Can't chdir to $test_dir: $!");
2470 }
2471
2472 unless (symlink('./test.txt', './test.lnk')) {
2473 die("Can't symlink 'test.lnk' to './test.txt': $!");
2474 }
2475
2476 unless (chdir($cwd)) {
2477 die("Can't chdir to $cwd: $!");
2478 }
2479
2480 my $config = {
2481 PidFile => $setup->{pid_file},
2482 ScoreboardFile => $setup->{scoreboard_file},
2483 SystemLog => $setup->{log_file},
2484
2485 AuthUserFile => $setup->{auth_user_file},
2486 AuthGroupFile => $setup->{auth_group_file},
2487 AuthOrder => 'mod_auth_file.c',
2488
2489 DefaultRoot => '~',
2490
2491 IfModules => {
2492 'mod_delay.c' => {
2493 DelayEngine => 'off',
2494 },
2495 },
2496 };
2497
2498 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2499 $config);
2500
2501 # Open pipes, for use between the parent and child processes. Specifically,
2502 # the child will indicate when it's done with its test by writing a message
2503 # to the parent.
2504 my ($rfh, $wfh);
2505 unless (pipe($rfh, $wfh)) {
2506 die("Can't open pipe: $!");
2507 }
2508
2509 my $ex;
2510
2511 # Fork child
2512 $self->handle_sigchld();
2513 defined(my $pid = fork()) or die("Can't fork: $!");
2514 if ($pid) {
2515 eval {
2516 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
2517 $client->login($setup->{user}, $setup->{passwd});
2518
2519 my $path = 'test.d/test.lnk';
2520 my $conn = $client->retr_raw($path);
2521 if ($conn) {
2522 die("RETR succeeded unexpectedly");
2523 }
2524
2525 my $resp_code = $client->response_code();
2526 my $resp_msg = $client->response_msg();
2527 $client->quit();
2528
2529 my $expected = 550;
2530 $self->assert($expected == $resp_code,
2531 test_msg("Expected response code $expected, got $resp_code"));
2532
2533 $expected = "$path: No such file or directory";
2534 $self->assert($expected eq $resp_msg,
2535 test_msg("Expected response message '$expected', got '$resp_msg'"));
2536 };
2537
2538 if ($@) {
2539 $ex = $@;
2540 }
2541
2542 $wfh->print("done\n");
2543 $wfh->flush();
2544
2545 } else {
2546 eval { server_wait($setup->{config_file}, $rfh) };
2547 if ($@) {
2548 warn($@);
2549 exit 1;
2550 }
2551
2552 exit 0;
2553 }
2554
2555 # Stop server
2556 server_stop($setup->{pid_file});
2557
2558 $self->assert_child_ok($pid);
2559
2560 test_cleanup($setup->{log_file}, $ex);
2561 }
2562
2563 sub retr_fails_eperm {
2564 my $self = shift;
2565 my $tmpdir = $self->{tmpdir};
2566 my $setup = test_setup($tmpdir, 'cmds');
2567
2568 my $sub_dir = File::Spec->rel2abs("$tmpdir/foo");
2569 mkpath($sub_dir);
2570
2571 my $test_file = File::Spec->rel2abs("$tmpdir/foo/bar");
2572 if (open(my $fh, "> $test_file")) {
2573 close($fh);
2574
2575 } else {
2576 die("Can't open $test_file: $!");
2577 }
2578
2579 my $config = {
2580 PidFile => $setup->{pid_file},
2581 ScoreboardFile => $setup->{scoreboard_file},
2582 SystemLog => $setup->{log_file},
2583
2584 AuthUserFile => $setup->{auth_user_file},
2585 AuthGroupFile => $setup->{auth_group_file},
2586 AuthOrder => 'mod_auth_file.c',
2587
2588 IfModules => {
2589 'mod_delay.c' => {
2590 DelayEngine => 'off',
2591 },
2592 },
2593 };
2594
2595 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2596 $config);
2597
2598 # Open pipes, for use between the parent and child processes. Specifically,
2599 # the child will indicate when it's done with its test by writing a message
2600 # to the parent.
2601 my ($rfh, $wfh);
2602 unless (pipe($rfh, $wfh)) {
2603 die("Can't open pipe: $!");
2604 }
2605
2606 my $ex;
2607
2608 # Fork child
2609 $self->handle_sigchld();
2610 defined(my $pid = fork()) or die("Can't fork: $!");
2611 if ($pid) {
2612 eval {
2613 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
2614 $client->login($setup->{user}, $setup->{passwd});
2615 $client->port();
2616
2617 chmod(0660, $sub_dir);
2618
2619 my ($resp_code, $resp_msg);
2620 eval { ($resp_code, $resp_msg) = $client->retr($test_file) };
2621 unless ($@) {
2622 die("RETR succeeded unexpectedly ($resp_code $resp_msg)");
2623 }
2624
2625 $resp_code = $client->response_code();
2626 $resp_msg = $client->response_msg();
2627
2628 my $expected;
2629
2630 $expected = 550;
2631 $self->assert($expected == $resp_code,
2632 test_msg("Expected response code $expected, got $resp_code"));
2633
2634 $expected = "$test_file: Permission denied";
2635 $self->assert($expected eq $resp_msg,
2636 test_msg("Expected response message '$expected', got '$resp_msg'"));
2637 };
2638
2639 if ($@) {
2640 $ex = $@;
2641 }
2642
2643 $wfh->print("done\n");
2644 $wfh->flush();
2645
2646 } else {
2647 eval { server_wait($setup->{config_file}, $rfh) };
2648 if ($@) {
2649 warn($@);
2650 exit 1;
2651 }
2652
2653 exit 0;
2654 }
2655
2656 # Stop server
2657 server_stop($setup->{pid_file});
2658
2659 $self->assert_child_ok($pid);
2660
2661 test_cleanup($setup->{log_file}, $ex);
2662 }
2663
2664 sub retr_ok_dir_with_spaces {
2665 my $self = shift;
2666 my $tmpdir = $self->{tmpdir};
2667 my $setup = test_setup($tmpdir, 'cmds');
2668
2669 my $sub_dir = File::Spec->rel2abs("$tmpdir/sub dir");
2670 mkpath($sub_dir);
2671
2672 if ($< == 0) {
2673 unless (chmod(0755, $sub_dir)) {
2674 die("Can't set perms on $sub_dir to 0755: $!");
2675 }
2676 }
2677
2678 my $test_file = File::Spec->rel2abs("$sub_dir/test.txt");
2679 if (open(my $fh, "> $test_file")) {
2680 print $fh "Hello, World!\n";
2681
2682 unless (close($fh)) {
2683 die("Can't write $test_file: $!");
2684 }
2685
2686 } else {
2687 die("Can't open $test_file: $!");
2688 }
2689
2690 my $config = {
2691 PidFile => $setup->{pid_file},
2692 ScoreboardFile => $setup->{scoreboard_file},
2693 SystemLog => $setup->{log_file},
2694
2695 AuthUserFile => $setup->{auth_user_file},
2696 AuthGroupFile => $setup->{auth_group_file},
2697 AuthOrder => 'mod_auth_file.c',
2698
2699 IfModules => {
2700 'mod_delay.c' => {
2701 DelayEngine => 'off',
2702 },
2703 },
2704 };
2705
2706 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2707 $config);
2708
2709 # Open pipes, for use between the parent and child processes. Specifically,
2710 # the child will indicate when it's done with its test by writing a message
2711 # to the parent.
2712 my ($rfh, $wfh);
2713 unless (pipe($rfh, $wfh)) {
2714 die("Can't open pipe: $!");
2715 }
2716
2717 my $ex;
2718
2719 # Fork child
2720 $self->handle_sigchld();
2721 defined(my $pid = fork()) or die("Can't fork: $!");
2722 if ($pid) {
2723 eval {
2724 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
2725 $client->login($setup->{user}, $setup->{passwd});
2726
2727 my $conn = $client->retr_raw('sub dir/test.txt');
2728 unless ($conn) {
2729 die("Failed to RETR: " . $client->response_code() . " " .
2730 $client->response_msg());
2731 }
2732
2733 my $buf;
2734 $conn->read($buf, 8192, 30);
2735 eval { $conn->close() };
2736
2737 my ($resp_code, $resp_msg);
2738 $resp_code = $client->response_code();
2739 $resp_msg = $client->response_msg();
2740
2741 $self->assert_transfer_ok($resp_code, $resp_msg);
2742 };
2743
2744 if ($@) {
2745 $ex = $@;
2746 }
2747
2748 $wfh->print("done\n");
2749 $wfh->flush();
2750
2751 } else {
2752 eval { server_wait($setup->{config_file}, $rfh) };
2753 if ($@) {
2754 warn($@);
2755 exit 1;
2756 }
2757
2758 exit 0;
2759 }
2760
2761 # Stop server
2762 server_stop($setup->{pid_file});
2763
2764 $self->assert_child_ok($pid);
2765
2766 test_cleanup($setup->{log_file}, $ex);
2767 }
2768
2769 sub retr_leading_whitespace {
2770 my $self = shift;
2771 my $tmpdir = $self->{tmpdir};
2772 my $setup = test_setup($tmpdir, 'cmds');
2773
2774 my $test_file = File::Spec->rel2abs("$tmpdir/ test.txt");
2775 if (open(my $fh, "> $test_file")) {
2776 print $fh "ABCD\n" x 32;
2777 unless (close($fh)) {
2778 die("Can't write $test_file: $!");
2779 }
2780
2781 } else {
2782 die("Can't open $test_file: $!");
2783 }
2784
2785 my $test_sz = (stat($test_file))[7];
2786
2787 my $config = {
2788 PidFile => $setup->{pid_file},
2789 ScoreboardFile => $setup->{scoreboard_file},
2790 SystemLog => $setup->{log_file},
2791
2792 AuthUserFile => $setup->{auth_user_file},
2793 AuthGroupFile => $setup->{auth_group_file},
2794 AuthOrder => 'mod_auth_file.c',
2795
2796 IfModules => {
2797 'mod_delay.c' => {
2798 DelayEngine => 'off',
2799 },
2800 },
2801 };
2802
2803 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2804 $config);
2805
2806 # Open pipes, for use between the parent and child processes. Specifically,
2807 # the child will indicate when it's done with its test by writing a message
2808 # to the parent.
2809 my ($rfh, $wfh);
2810 unless (pipe($rfh, $wfh)) {
2811 die("Can't open pipe: $!");
2812 }
2813
2814 my $ex;
2815
2816 # Fork child
2817 $self->handle_sigchld();
2818 defined(my $pid = fork()) or die("Can't fork: $!");
2819 if ($pid) {
2820 eval {
2821 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
2822 $client->login($setup->{user}, $setup->{passwd});
2823
2824 my $conn = $client->retr_raw(' test.txt');
2825 unless ($conn) {
2826 die("Failed to RETR: " . $client->response_code() . " " .
2827 $client->response_msg());
2828 }
2829
2830 my $buf;
2831 $conn->read($buf, 8192, 30);
2832 eval { $conn->close() };
2833
2834 my ($resp_code, $resp_msg);
2835 $resp_code = $client->response_code();
2836 $resp_msg = $client->response_msg();
2837
2838 $self->assert_transfer_ok($resp_code, $resp_msg);
2839
2840 my $buflen = length($buf);
2841 my $expected = $test_sz;
2842 $self->assert($expected == $buflen,
2843 test_msg("Expected download byte count $expected, got $buflen"));
2844 };
2845
2846 if ($@) {
2847 $ex = $@;
2848 }
2849
2850 $wfh->print("done\n");
2851 $wfh->flush();
2852
2853 } else {
2854 eval { server_wait($setup->{config_file}, $rfh) };
2855 if ($@) {
2856 warn($@);
2857 exit 1;
2858 }
2859
2860 exit 0;
2861 }
2862
2863 # Stop server
2864 server_stop($setup->{pid_file});
2865
2866 $self->assert_child_ok($pid);
2867
2868 test_cleanup($setup->{log_file}, $ex);
2869 }
2870
2871 sub retr_bug3496 {
2872 my $self = shift;
2873 my $tmpdir = $self->{tmpdir};
2874 my $setup = test_setup($tmpdir, 'cmds');
2875
2876 my $test_file = File::Spec->rel2abs("$tmpdir/largfile");
2877 if (open(my $fh, "> $test_file")) {
2878 my $str = "ABCDabcd" x 16;
2879
2880 for (my $i = 0; $i < 1024; $i++) {
2881 print $fh "$str\n";
2882 }
2883
2884 unless (close($fh)) {
2885 die("Can't write $test_file: $!");
2886 }
2887
2888 } else {
2889 die("Can't open $test_file: $!");
2890 }
2891
2892 my $config = {
2893 PidFile => $setup->{pid_file},
2894 ScoreboardFile => $setup->{scoreboard_file},
2895 SystemLog => $setup->{log_file},
2896 TraceLog => $setup->{log_file},
2897 Trace => 'DEFAULT:10',
2898
2899 AuthUserFile => $setup->{auth_user_file},
2900 AuthGroupFile => $setup->{auth_group_file},
2901 AuthOrder => 'mod_auth_file.c',
2902
2903 MaxInstances => 2,
2904
2905 IfModules => {
2906 'mod_delay.c' => {
2907 DelayEngine => 'off',
2908 },
2909 },
2910 };
2911
2912 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2913 $config);
2914
2915 # Open pipes, for use between the parent and child processes. Specifically,
2916 # the child will indicate when it's done with its test by writing a message
2917 # to the parent.
2918 my ($rfh, $wfh);
2919 unless (pipe($rfh, $wfh)) {
2920 die("Can't open pipe: $!");
2921 }
2922
2923 my $ex;
2924
2925 # Fork child
2926 $self->handle_sigchld();
2927 defined(my $pid = fork()) or die("Can't fork: $!");
2928 if ($pid) {
2929 eval {
2930 # When run too quickly with the other tests, this test can fail. So
2931 # pause a little here.
2932 sleep(3);
2933
2934 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 3);
2935 $client->login($setup->{user}, $setup->{passwd});
2936
2937 my $conn = $client->retr_raw($test_file);
2938 unless ($conn) {
2939 die("Failed to RETR: " . $client->response_code() . " " .
2940 $client->response_msg());
2941 }
2942
2943 # Close the _control_ connection immediately
2944 $client->{ftp}->close();
2945
2946 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1, 1);
2947 $client2->login($setup->{user}, $setup->{passwd});
2948 };
2949 if ($@) {
2950 $ex = $@;
2951 }
2952
2953 $wfh->print("done\n");
2954 $wfh->flush();
2955
2956 } else {
2957 eval { server_wait($setup->{config_file}, $rfh) };
2958 if ($@) {
2959 warn($@);
2960 exit 1;
2961 }
2962
2963 exit 0;
2964 }
2965
2966 # Stop server
2967 server_stop($setup->{pid_file});
2968 $self->assert_child_ok($pid);
2969
2970 test_cleanup($setup->{log_file}, $ex);
2971 }
2972
2973 sub retr_2nd_transfer_terminates_1st_transfer_bug4010 {
2974 my $self = shift;
2975 my $tmpdir = $self->{tmpdir};
2976 my $setup = test_setup($tmpdir, 'cmds');
2977
2978 my $test_file = File::Spec->rel2abs($setup->{config_file});
2979
2980 my $config = {
2981 PidFile => $setup->{pid_file},
2982 ScoreboardFile => $setup->{scoreboard_file},
2983 SystemLog => $setup->{log_file},
2984
2985 AuthUserFile => $setup->{auth_user_file},
2986 AuthGroupFile => $setup->{auth_group_file},
2987 AuthOrder => 'mod_auth_file.c',
2988
2989 IfModules => {
2990 'mod_delay.c' => {
2991 DelayEngine => 'off',
2992 },
2993 },
2994 };
2995
2996 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2997 $config);
2998
2999 # Open pipes, for use between the parent and child processes. Specifically,
3000 # the child will indicate when it's done with its test by writing a message
3001 # to the parent.
3002 my ($rfh, $wfh);
3003 unless (pipe($rfh, $wfh)) {
3004 die("Can't open pipe: $!");
3005 }
3006
3007 my $ex;
3008
3009 # Fork child
3010 $self->handle_sigchld();
3011 defined(my $pid = fork()) or die("Can't fork: $!");
3012 if ($pid) {
3013 eval {
3014 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
3015 $client1->login($setup->{user}, $setup->{passwd});
3016
3017 my $conn1 = $client1->retr_raw($test_file);
3018 unless ($conn1) {
3019 die("Failed to RETR: " . $client1->response_code() . " " .
3020 $client1->response_msg());
3021 }
3022
3023 my ($buf1, $tmpbuf);
3024 $conn1->read($tmpbuf, 1, 30);
3025 $buf1 .= $tmpbuf;
3026
3027 # Now, log in a second time with same user/passwd, do a directory
3028 # listing, then close the client.
3029 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
3030 $client2->login($setup->{user}, $setup->{passwd});
3031
3032 my $conn2 = $client2->list_raw('foo.bar.baz');
3033 unless ($conn2) {
3034 die("Failed to LIST: " . $client2->response_code() . " " .
3035 $client2->response_msg());
3036 }
3037
3038 my $buf2;
3039 $conn2->read($buf2, 8192, 30);
3040 eval { $conn2->close() };
3041
3042 my $resp_code = $client2->response_code();
3043 my $resp_msg = $client2->response_msg();
3044
3045 my $expected = 450;
3046 $self->assert($expected == $resp_code,
3047 test_msg("Expected response code $expected, got $resp_code"));
3048
3049 $conn1->read($tmpbuf, 8192, 30);
3050 $buf1 .= $tmpbuf;
3051
3052 eval { $conn1->close() };
3053
3054 $resp_code = $client1->response_code();
3055 $resp_msg = $client1->response_msg();
3056 $self->assert_transfer_ok($resp_code, $resp_msg);
3057 };
3058
3059 if ($@) {
3060 $ex = $@;
3061 }
3062
3063 $wfh->print("done\n");
3064 $wfh->flush();
3065
3066 } else {
3067 eval { server_wait($setup->{config_file}, $rfh) };
3068 if ($@) {
3069 warn($@);
3070 exit 1;
3071 }
3072
3073 exit 0;
3074 }
3075
3076 # Stop server
3077 server_stop($setup->{pid_file});
3078
3079 $self->assert_child_ok($pid);
3080
3081 test_cleanup($setup->{log_file}, $ex);
3082 }
3083
3084 1;