Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Commands::REST;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Spec;
8 use IO::Handle;
9
10 use ProFTPD::TestSuite::FTP;
11 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
12
13 $| = 1;
14
15 my $order = 0;
16
17 my $TESTS = {
18 rest_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 rest_fails_login_required => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 rest_fails_negative_offset => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 rest_fails_bad_offset => {
34 order => ++$order,
35 test_class => [qw(forking)],
36 },
37
38 rest_fails_offset_and_ascii => {
39 order => ++$order,
40 test_class => [qw(forking)],
41 },
42
43 rest_2gb_last_byte => {
44 order => ++$order,
45 test_class => [qw(forking slow)],
46 },
47
48 rest_4gb_last_byte => {
49 order => ++$order,
50 test_class => [qw(forking slow)],
51 },
52
53 };
54
55 sub new {
56 return shift()->SUPER::new(@_);
57 }
58
59 sub list_tests {
60 return testsuite_get_runnable_tests($TESTS);
61 }
62
63 sub rest_ok {
64 my $self = shift;
65 my $tmpdir = $self->{tmpdir};
66
67 my $config_file = "$tmpdir/cmds.conf";
68 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
69 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
70
71 my $log_file = test_get_logfile();
72
73 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
74 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
75
76 my $user = 'proftpd';
77 my $passwd = 'test';
78 my $home_dir = File::Spec->rel2abs($tmpdir);
79 my $uid = 500;
80 my $gid = 500;
81
82 # Make sure that, if we're running as root, that the home directory has
83 # permissions/privs set for the account we create
84 if ($< == 0) {
85 unless (chmod(0755, $home_dir)) {
86 die("Can't set perms on $home_dir to 0755: $!");
87 }
88
89 unless (chown($uid, $gid, $home_dir)) {
90 die("Can't set owner of $home_dir to $uid/$gid: $!");
91 }
92 }
93
94 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
95 '/bin/bash');
96 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
97
98 my $config = {
99 PidFile => $pid_file,
100 ScoreboardFile => $scoreboard_file,
101 SystemLog => $log_file,
102
103 AuthUserFile => $auth_user_file,
104 AuthGroupFile => $auth_group_file,
105 AuthOrder => 'mod_auth_file.c',
106
107 IfModules => {
108 'mod_delay.c' => {
109 DelayEngine => 'off',
110 },
111 },
112 };
113
114 my ($port, $config_user, $config_group) = config_write($config_file, $config);
115
116 # Open pipes, for use between the parent and child processes. Specifically,
117 # the child will indicate when it's done with its test by writing a message
118 # to the parent.
119 my ($rfh, $wfh);
120 unless (pipe($rfh, $wfh)) {
121 die("Can't open pipe: $!");
122 }
123
124 my $ex;
125
126 # Fork child
127 $self->handle_sigchld();
128 defined(my $pid = fork()) or die("Can't fork: $!");
129 if ($pid) {
130 eval {
131 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
132
133 $client->login($user, $passwd);
134
135 my ($resp_code, $resp_msg);
136 ($resp_code, $resp_msg) = $client->rest(0);
137
138 my $expected;
139
140 $expected = 350;
141 $self->assert($expected == $resp_code,
142 test_msg("Expected $expected, got $resp_code"));
143
144 $expected = "Restarting at 0. Send STORE or RETRIEVE to initiate transfer";
145 $self->assert($expected eq $resp_msg,
146 test_msg("Expected '$expected', got '$resp_msg'"));
147 };
148
149 if ($@) {
150 $ex = $@;
151 }
152
153 $wfh->print("done\n");
154 $wfh->flush();
155
156 } else {
157 eval { server_wait($config_file, $rfh) };
158 if ($@) {
159 warn($@);
160 exit 1;
161 }
162
163 exit 0;
164 }
165
166 # Stop server
167 server_stop($pid_file);
168
169 $self->assert_child_ok($pid);
170
171 if ($ex) {
172 test_append_logfile($log_file, $ex);
173 unlink($log_file);
174
175 die($ex);
176 }
177
178 unlink($log_file);
179 }
180
181 sub rest_fails_login_required {
182 my $self = shift;
183 my $tmpdir = $self->{tmpdir};
184
185 my $config_file = "$tmpdir/cmds.conf";
186 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
187 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
188
189 my $log_file = test_get_logfile();
190
191 my $config = {
192 PidFile => $pid_file,
193 ScoreboardFile => $scoreboard_file,
194 SystemLog => $log_file,
195
196 IfModules => {
197 'mod_delay.c' => {
198 DelayEngine => 'off',
199 },
200 },
201 };
202
203 my ($port, $config_user, $config_group) = config_write($config_file, $config);
204
205 # Open pipes, for use between the parent and child processes. Specifically,
206 # the child will indicate when it's done with its test by writing a message
207 # to the parent.
208 my ($rfh, $wfh);
209 unless (pipe($rfh, $wfh)) {
210 die("Can't open pipe: $!");
211 }
212
213 my $ex;
214
215 # Fork child
216 $self->handle_sigchld();
217 defined(my $pid = fork()) or die("Can't fork: $!");
218 if ($pid) {
219 eval {
220 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
221
222 my ($resp_code, $resp_msg);
223 eval { ($resp_code, $resp_msg) = $client->rest() };
224 unless ($@) {
225 die("REST succeeded unexpectedly");
226
227 } else {
228 $resp_code = $client->response_code();
229 $resp_msg = $client->response_msg();
230 }
231
232 my $expected;
233
234 $expected = 530;
235 $self->assert($expected == $resp_code,
236 test_msg("Expected $expected, got $resp_code"));
237
238 $expected = "Please login with USER and PASS";
239 $self->assert($expected eq $resp_msg,
240 test_msg("Expected '$expected', got '$resp_msg'"));
241 };
242
243 if ($@) {
244 $ex = $@;
245 }
246
247 $wfh->print("done\n");
248 $wfh->flush();
249
250 } else {
251 eval { server_wait($config_file, $rfh) };
252 if ($@) {
253 warn($@);
254 exit 1;
255 }
256
257 exit 0;
258 }
259
260 # Stop server
261 server_stop($pid_file);
262
263 $self->assert_child_ok($pid);
264
265 if ($ex) {
266 test_append_logfile($log_file, $ex);
267 unlink($log_file);
268
269 die($ex);
270 }
271
272 unlink($log_file);
273 }
274
275 sub rest_fails_negative_offset {
276 my $self = shift;
277 my $tmpdir = $self->{tmpdir};
278
279 my $config_file = "$tmpdir/cmds.conf";
280 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
281 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
282
283 my $log_file = test_get_logfile();
284
285 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
286 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
287
288 my $user = 'proftpd';
289 my $passwd = 'test';
290 my $home_dir = File::Spec->rel2abs($tmpdir);
291 my $uid = 500;
292 my $gid = 500;
293
294 # Make sure that, if we're running as root, that the home directory has
295 # permissions/privs set for the account we create
296 if ($< == 0) {
297 unless (chmod(0755, $home_dir)) {
298 die("Can't set perms on $home_dir to 0755: $!");
299 }
300
301 unless (chown($uid, $gid, $home_dir)) {
302 die("Can't set owner of $home_dir to $uid/$gid: $!");
303 }
304 }
305
306 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
307 '/bin/bash');
308 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
309
310 my $config = {
311 PidFile => $pid_file,
312 ScoreboardFile => $scoreboard_file,
313 SystemLog => $log_file,
314
315 AuthUserFile => $auth_user_file,
316 AuthGroupFile => $auth_group_file,
317 AuthOrder => 'mod_auth_file.c',
318
319 IfModules => {
320 'mod_delay.c' => {
321 DelayEngine => 'off',
322 },
323 },
324 };
325
326 my ($port, $config_user, $config_group) = config_write($config_file, $config);
327
328 # Open pipes, for use between the parent and child processes. Specifically,
329 # the child will indicate when it's done with its test by writing a message
330 # to the parent.
331 my ($rfh, $wfh);
332 unless (pipe($rfh, $wfh)) {
333 die("Can't open pipe: $!");
334 }
335
336 my $ex;
337
338 # Fork child
339 $self->handle_sigchld();
340 defined(my $pid = fork()) or die("Can't fork: $!");
341 if ($pid) {
342 eval {
343 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
344
345 $client->login($user, $passwd);
346
347 my ($resp_code, $resp_msg);
348 eval { ($resp_code, $resp_msg) = $client->rest(-1) };
349 unless ($@) {
350 die("REST succeeded unexpectedly");
351
352 } else {
353 $resp_code = $client->response_code();
354 $resp_msg = $client->response_msg();
355 }
356
357 my $expected;
358
359 $expected = 501;
360 $self->assert($expected == $resp_code,
361 test_msg("Expected $expected, got $resp_code"));
362
363 $expected = "REST requires a value greater than or equal to 0";
364 $self->assert($expected eq $resp_msg,
365 test_msg("Expected '$expected', got '$resp_msg'"));
366 };
367
368 if ($@) {
369 $ex = $@;
370 }
371
372 $wfh->print("done\n");
373 $wfh->flush();
374
375 } else {
376 eval { server_wait($config_file, $rfh) };
377 if ($@) {
378 warn($@);
379 exit 1;
380 }
381
382 exit 0;
383 }
384
385 # Stop server
386 server_stop($pid_file);
387
388 $self->assert_child_ok($pid);
389
390 if ($ex) {
391 test_append_logfile($log_file, $ex);
392 unlink($log_file);
393
394 die($ex);
395 }
396
397 unlink($log_file);
398 }
399
400 sub rest_fails_bad_offset {
401 my $self = shift;
402 my $tmpdir = $self->{tmpdir};
403
404 my $config_file = "$tmpdir/cmds.conf";
405 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
406 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
407
408 my $log_file = test_get_logfile();
409
410 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
411 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
412
413 my $user = 'proftpd';
414 my $passwd = 'test';
415 my $home_dir = File::Spec->rel2abs($tmpdir);
416 my $uid = 500;
417 my $gid = 500;
418
419 # Make sure that, if we're running as root, that the home directory has
420 # permissions/privs set for the account we create
421 if ($< == 0) {
422 unless (chmod(0755, $home_dir)) {
423 die("Can't set perms on $home_dir to 0755: $!");
424 }
425
426 unless (chown($uid, $gid, $home_dir)) {
427 die("Can't set owner of $home_dir to $uid/$gid: $!");
428 }
429 }
430
431 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
432 '/bin/bash');
433 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
434
435 my $config = {
436 PidFile => $pid_file,
437 ScoreboardFile => $scoreboard_file,
438 SystemLog => $log_file,
439
440 AuthUserFile => $auth_user_file,
441 AuthGroupFile => $auth_group_file,
442 AuthOrder => 'mod_auth_file.c',
443
444 IfModules => {
445 'mod_delay.c' => {
446 DelayEngine => 'off',
447 },
448 },
449 };
450
451 my ($port, $config_user, $config_group) = config_write($config_file, $config);
452
453 # Open pipes, for use between the parent and child processes. Specifically,
454 # the child will indicate when it's done with its test by writing a message
455 # to the parent.
456 my ($rfh, $wfh);
457 unless (pipe($rfh, $wfh)) {
458 die("Can't open pipe: $!");
459 }
460
461 my $ex;
462
463 # Fork child
464 $self->handle_sigchld();
465 defined(my $pid = fork()) or die("Can't fork: $!");
466 if ($pid) {
467 eval {
468 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
469
470 $client->login($user, $passwd);
471
472 my ($resp_code, $resp_msg);
473 eval { ($resp_code, $resp_msg) = $client->rest("100f") };
474 unless ($@) {
475 die("REST succeeded unexpectedly");
476
477 } else {
478 $resp_code = $client->response_code();
479 $resp_msg = $client->response_msg();
480 }
481
482 my $expected;
483
484 $expected = 501;
485 $self->assert($expected == $resp_code,
486 test_msg("Expected $expected, got $resp_code"));
487
488 $expected = "REST requires a value greater than or equal to 0";
489 $self->assert($expected eq $resp_msg,
490 test_msg("Expected '$expected', got '$resp_msg'"));
491 };
492
493 if ($@) {
494 $ex = $@;
495 }
496
497 $wfh->print("done\n");
498 $wfh->flush();
499
500 } else {
501 eval { server_wait($config_file, $rfh) };
502 if ($@) {
503 warn($@);
504 exit 1;
505 }
506
507 exit 0;
508 }
509
510 # Stop server
511 server_stop($pid_file);
512
513 $self->assert_child_ok($pid);
514
515 if ($ex) {
516 test_append_logfile($log_file, $ex);
517 unlink($log_file);
518
519 die($ex);
520 }
521
522 unlink($log_file);
523 }
524
525 sub rest_fails_offset_and_ascii {
526 my $self = shift;
527 my $tmpdir = $self->{tmpdir};
528
529 my $config_file = "$tmpdir/cmds.conf";
530 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
531 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
532
533 my $log_file = test_get_logfile();
534
535 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
536 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
537
538 my $user = 'proftpd';
539 my $passwd = 'test';
540 my $home_dir = File::Spec->rel2abs($tmpdir);
541 my $uid = 500;
542 my $gid = 500;
543
544 # Make sure that, if we're running as root, that the home directory has
545 # permissions/privs set for the account we create
546 if ($< == 0) {
547 unless (chmod(0755, $home_dir)) {
548 die("Can't set perms on $home_dir to 0755: $!");
549 }
550
551 unless (chown($uid, $gid, $home_dir)) {
552 die("Can't set owner of $home_dir to $uid/$gid: $!");
553 }
554 }
555
556 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
557 '/bin/bash');
558 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
559
560 my $config = {
561 PidFile => $pid_file,
562 ScoreboardFile => $scoreboard_file,
563 SystemLog => $log_file,
564
565 AuthUserFile => $auth_user_file,
566 AuthGroupFile => $auth_group_file,
567 AuthOrder => 'mod_auth_file.c',
568
569 IfModules => {
570 'mod_delay.c' => {
571 DelayEngine => 'off',
572 },
573 },
574 };
575
576 my ($port, $config_user, $config_group) = config_write($config_file, $config);
577
578 # Open pipes, for use between the parent and child processes. Specifically,
579 # the child will indicate when it's done with its test by writing a message
580 # to the parent.
581 my ($rfh, $wfh);
582 unless (pipe($rfh, $wfh)) {
583 die("Can't open pipe: $!");
584 }
585
586 my $ex;
587
588 # Fork child
589 $self->handle_sigchld();
590 defined(my $pid = fork()) or die("Can't fork: $!");
591 if ($pid) {
592 eval {
593 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
594
595 $client->login($user, $passwd);
596 $client->type('ascii');
597
598 my ($resp_code, $resp_msg);
599 eval { ($resp_code, $resp_msg) = $client->rest("100") };
600 unless ($@) {
601 die("REST succeeded unexpectedly");
602
603 } else {
604 $resp_code = $client->response_code();
605 $resp_msg = $client->response_msg();
606 }
607
608 my $expected;
609
610 $expected = 501;
611 $self->assert($expected == $resp_code,
612 test_msg("Expected $expected, got $resp_code"));
613
614 $expected = "REST: Resuming transfers not allowed in ASCII mode";
615 $self->assert($expected eq $resp_msg,
616 test_msg("Expected '$expected', got '$resp_msg'"));
617 };
618
619 if ($@) {
620 $ex = $@;
621 }
622
623 $wfh->print("done\n");
624 $wfh->flush();
625
626 } else {
627 eval { server_wait($config_file, $rfh) };
628 if ($@) {
629 warn($@);
630 exit 1;
631 }
632
633 exit 0;
634 }
635
636 # Stop server
637 server_stop($pid_file);
638
639 $self->assert_child_ok($pid);
640
641 if ($ex) {
642 test_append_logfile($log_file, $ex);
643 unlink($log_file);
644
645 die($ex);
646 }
647
648 unlink($log_file);
649 }
650
651 sub rest_2gb_last_byte {
652 my $self = shift;
653 my $tmpdir = $self->{tmpdir};
654
655 my $config_file = "$tmpdir/cmds.conf";
656 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
657 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
658
659 my $log_file = test_get_logfile();
660
661 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
662 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
663
664 my $user = 'proftpd';
665 my $passwd = 'test';
666 my $home_dir = File::Spec->rel2abs($tmpdir);
667 my $uid = 500;
668 my $gid = 500;
669
670 # Make sure that, if we're running as root, that the home directory has
671 # permissions/privs set for the account we create
672 if ($< == 0) {
673 unless (chmod(0755, $home_dir)) {
674 die("Can't set perms on $home_dir to 0755: $!");
675 }
676
677 unless (chown($uid, $gid, $home_dir)) {
678 die("Can't set owner of $home_dir to $uid/$gid: $!");
679 }
680 }
681
682 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
683 '/bin/bash');
684 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
685
686 # According to:
687 #
688 # http://trac.filezilla-project.org/ticket/4487
689 #
690 # FileZilla tests whether servers support resuming transfers over 2GB by
691 # (IMHO) seeking to just before the last byte of the file on the server, and
692 # expecting just a single byte.
693 #
694 # Make sure that proftpd does The Right Thing(tm) in a regression test for
695 # this use case.
696
697 # Create a file that is 2GB plus 24 bytes.
698 my $test_len = (2 ** 31) + 24;
699 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
700 if (open(my $fh, "> $test_file")) {
701
702 # Seek to the 2GB limit, then fill the rest with 'A'
703 unless (seek($fh, (2 ** 31), 0)) {
704 die("Can't seek to 2GB length: $!");
705 }
706
707 print $fh "A" x 24;
708
709 unless (close($fh)) {
710 die("Can't write $test_file: $!");
711 }
712
713 } else {
714 die("Can't open $test_file: $!");
715 }
716
717 my $config = {
718 PidFile => $pid_file,
719 ScoreboardFile => $scoreboard_file,
720 SystemLog => $log_file,
721
722 AuthUserFile => $auth_user_file,
723 AuthGroupFile => $auth_group_file,
724 AuthOrder => 'mod_auth_file.c',
725
726 IfModules => {
727 'mod_delay.c' => {
728 DelayEngine => 'off',
729 },
730 },
731 };
732
733 my ($port, $config_user, $config_group) = config_write($config_file, $config);
734
735 # Open pipes, for use between the parent and child processes. Specifically,
736 # the child will indicate when it's done with its test by writing a message
737 # to the parent.
738 my ($rfh, $wfh);
739 unless (pipe($rfh, $wfh)) {
740 die("Can't open pipe: $!");
741 }
742
743 my $ex;
744
745 # Fork child
746 $self->handle_sigchld();
747 defined(my $pid = fork()) or die("Can't fork: $!");
748 if ($pid) {
749 eval {
750 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
751
752 $client->login($user, $passwd);
753 $client->type('binary');
754
755 my ($resp_code, $resp_msg);
756
757 my $rest_len = $test_len - 1;
758 ($resp_code, $resp_msg) = $client->rest($rest_len);
759
760 my $expected;
761
762 $expected = 350;
763 $self->assert($expected == $resp_code,
764 test_msg("Expected $expected, got $resp_code"));
765
766 $expected = "Restarting at $rest_len. Send STORE or RETRIEVE to initiate transfer";
767 $self->assert($expected eq $resp_msg,
768 test_msg("Expected '$expected', got '$resp_msg'"));
769
770 # Now attempt a download; we expect to get just one byte.
771 my $conn = $client->retr_raw($test_file);
772 unless ($conn) {
773 die("RETR $test_file failed: " . $client->response_code() . " " .
774 $client->response_msg());
775 }
776
777 my $buf;
778 $conn->read($buf, 1024, 30);
779 $conn->close();
780
781 $resp_code = $client->response_code();
782 $resp_msg = $client->response_msg();
783
784 $expected = 226;
785 $self->assert($expected == $resp_code,
786 test_msg("Expected $expected, got $resp_code"));
787
788 $expected = "Transfer complete";
789 $self->assert($expected eq $resp_msg,
790 test_msg("Expected '$expected', got '$resp_msg'"));
791
792 my $buflen = length($buf);
793 $expected = 1;
794 $self->assert($expected == $buflen,
795 test_msg("Expected $expected, got $buflen"));
796 };
797
798 if ($@) {
799 $ex = $@;
800 }
801
802 $wfh->print("done\n");
803 $wfh->flush();
804
805 } else {
806 eval { server_wait($config_file, $rfh) };
807 if ($@) {
808 warn($@);
809 exit 1;
810 }
811
812 exit 0;
813 }
814
815 # Stop server
816 server_stop($pid_file);
817
818 $self->assert_child_ok($pid);
819
820 if ($ex) {
821 test_append_logfile($log_file, $ex);
822 unlink($log_file);
823
824 die($ex);
825 }
826
827 unlink($log_file);
828 }
829
830 sub rest_4gb_last_byte {
831 my $self = shift;
832 my $tmpdir = $self->{tmpdir};
833
834 my $config_file = "$tmpdir/cmds.conf";
835 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
836 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
837
838 my $log_file = test_get_logfile();
839
840 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
841 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
842
843 my $user = 'proftpd';
844 my $passwd = 'test';
845 my $home_dir = File::Spec->rel2abs($tmpdir);
846 my $uid = 500;
847 my $gid = 500;
848
849 # Make sure that, if we're running as root, that the home directory has
850 # permissions/privs set for the account we create
851 if ($< == 0) {
852 unless (chmod(0755, $home_dir)) {
853 die("Can't set perms on $home_dir to 0755: $!");
854 }
855
856 unless (chown($uid, $gid, $home_dir)) {
857 die("Can't set owner of $home_dir to $uid/$gid: $!");
858 }
859 }
860
861 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
862 '/bin/bash');
863 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
864
865 # According to:
866 #
867 # http://trac.filezilla-project.org/ticket/4487
868 #
869 # FileZilla tests whether servers support resuming transfers over 4GB by
870 # (IMHO) seeking to just before the last byte of the file on the server, and
871 # expecting just a single byte.
872 #
873 # Make sure that proftpd does The Right Thing(tm) in a regression test for
874 # this use case.
875
876 # Create a file that is 4GB plus 24 bytes.
877 my $test_len = (2 ** 32) + 24;
878 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
879 if (open(my $fh, "> $test_file")) {
880
881 # Seek to the $GB limit, then fill the rest with 'A'
882 unless (seek($fh, (2 ** 32), 0)) {
883 die("Can't seek to 2GB length: $!");
884 }
885
886 print $fh "A" x 24;
887
888 unless (close($fh)) {
889 die("Can't write $test_file: $!");
890 }
891
892 } else {
893 die("Can't open $test_file: $!");
894 }
895
896 my $config = {
897 PidFile => $pid_file,
898 ScoreboardFile => $scoreboard_file,
899 SystemLog => $log_file,
900
901 AuthUserFile => $auth_user_file,
902 AuthGroupFile => $auth_group_file,
903 AuthOrder => 'mod_auth_file.c',
904
905 IfModules => {
906 'mod_delay.c' => {
907 DelayEngine => 'off',
908 },
909 },
910 };
911
912 my ($port, $config_user, $config_group) = config_write($config_file, $config);
913
914 # Open pipes, for use between the parent and child processes. Specifically,
915 # the child will indicate when it's done with its test by writing a message
916 # to the parent.
917 my ($rfh, $wfh);
918 unless (pipe($rfh, $wfh)) {
919 die("Can't open pipe: $!");
920 }
921
922 my $ex;
923
924 # Fork child
925 $self->handle_sigchld();
926 defined(my $pid = fork()) or die("Can't fork: $!");
927 if ($pid) {
928 eval {
929 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
930
931 $client->login($user, $passwd);
932 $client->type('binary');
933
934 my ($resp_code, $resp_msg);
935
936 my $rest_len = $test_len - 1;
937 ($resp_code, $resp_msg) = $client->rest($rest_len);
938
939 my $expected;
940
941 $expected = 350;
942 $self->assert($expected == $resp_code,
943 test_msg("Expected $expected, got $resp_code"));
944
945 $expected = "Restarting at $rest_len. Send STORE or RETRIEVE to initiate transfer";
946 $self->assert($expected eq $resp_msg,
947 test_msg("Expected '$expected', got '$resp_msg'"));
948
949 # Now attempt a download; we expect to get just one byte.
950 my $conn = $client->retr_raw($test_file);
951 unless ($conn) {
952 die("RETR $test_file failed: " . $client->response_code() . " " .
953 $client->response_msg());
954 }
955
956 my $buf;
957 $conn->read($buf, 1024, 30);
958 $conn->close();
959
960 $resp_code = $client->response_code();
961 $resp_msg = $client->response_msg();
962
963 $expected = 226;
964 $self->assert($expected == $resp_code,
965 test_msg("Expected $expected, got $resp_code"));
966
967 $expected = "Transfer complete";
968 $self->assert($expected eq $resp_msg,
969 test_msg("Expected '$expected', got '$resp_msg'"));
970
971 my $buflen = length($buf);
972 $expected = 1;
973 $self->assert($expected == $buflen,
974 test_msg("Expected $expected, got $buflen"));
975 };
976
977 if ($@) {
978 $ex = $@;
979 }
980
981 $wfh->print("done\n");
982 $wfh->flush();
983
984 } else {
985 eval { server_wait($config_file, $rfh) };
986 if ($@) {
987 warn($@);
988 exit 1;
989 }
990
991 exit 0;
992 }
993
994 # Stop server
995 server_stop($pid_file);
996
997 $self->assert_child_ok($pid);
998
999 if ($ex) {
1000 test_append_logfile($log_file, $ex);
1001 unlink($log_file);
1002
1003 die($ex);
1004 }
1005
1006 unlink($log_file);
1007 }
1008
1009 1;