Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_deflate.pm

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