Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::DisplayFileTransfer;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Path qw(mkpath);
8 use File::Spec;
9 use IO::Handle;
10
11 use ProFTPD::TestSuite::FTP;
12 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
13
14 $| = 1;
15
16 my $order = 0;
17
18 my $TESTS = {
19 displayfilexfer_abs_path => {
20 order => ++$order,
21 test_class => [qw(forking)],
22 },
23
24 displayfilexfer_rel_path => {
25 order => ++$order,
26 test_class => [qw(forking)],
27 },
28
29 displayfilexfer_chrooted => {
30 order => ++$order,
31 test_class => [qw(forking rootprivs)],
32 },
33
34 displayfilexfer_multiline => {
35 order => ++$order,
36 test_class => [qw(forking)],
37 },
38
39 displayfilexfer_non_path => {
40 order => ++$order,
41 test_class => [qw(forking)],
42 },
43
44 # XXX Add other tests with the various Display variables
45 };
46
47 sub new {
48 return shift()->SUPER::new(@_);
49 }
50
51 sub list_tests {
52 return testsuite_get_runnable_tests($TESTS);
53 }
54
55 sub displayfilexfer_abs_path {
56 my $self = shift;
57 my $tmpdir = $self->{tmpdir};
58
59 my $config_file = "$tmpdir/config.conf";
60 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
61 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
62
63 my $log_file = File::Spec->rel2abs('tests.log');
64
65 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
66 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
67
68 my $user = 'proftpd';
69 my $passwd = 'test';
70 my $home_dir = File::Spec->rel2abs($tmpdir);
71
72 auth_user_write($auth_user_file, $user, $passwd, 500, 500, $home_dir,
73 '/bin/bash');
74 auth_group_write($auth_group_file, 'ftpd', 500, $user);
75
76 my $xfer_file = File::Spec->rel2abs("$tmpdir/xfer.txt");
77 if (open(my $fh, "> $xfer_file")) {
78 print $fh "Hello user!\n";
79
80 unless (close($fh)) {
81 die("Unable to write $xfer_file: $!");
82 }
83
84 } else {
85 die("Unable to open $xfer_file: $!");
86 }
87
88 my $config = {
89 PidFile => $pid_file,
90 ScoreboardFile => $scoreboard_file,
91 SystemLog => $log_file,
92
93 AuthUserFile => $auth_user_file,
94 AuthGroupFile => $auth_group_file,
95 AuthOrder => 'mod_auth_file.c',
96
97 DisplayFileTransfer => $xfer_file,
98
99 IfModules => {
100 'mod_delay.c' => {
101 DelayEngine => 'off',
102 },
103 },
104 };
105
106 my ($port, $config_user, $config_group) = config_write($config_file, $config);
107
108 # Open pipes, for use between the parent and child processes. Specifically,
109 # the child will indicate when it's done with its test by writing a message
110 # to the parent.
111 my ($rfh, $wfh);
112 unless (pipe($rfh, $wfh)) {
113 die("Can't open pipe: $!");
114 }
115
116 my $ex;
117
118 # Fork child
119 $self->handle_sigchld();
120 defined(my $pid = fork()) or die("Can't fork: $!");
121 if ($pid) {
122 eval {
123 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
124
125 $client->login($user, $passwd);
126
127 my $conn = $client->retr_raw('config.conf');
128 unless ($conn) {
129 die("RETR config.conf failed: " . $client->response_code() . " " .
130 $client->response_msg());
131 }
132
133 my $buf;
134 $conn->read($buf, 32768, 30);
135 $conn->close();
136
137 my ($resp_code, $resp_msg);
138
139 $resp_code = $client->response_code();
140 $resp_msg = $client->response_msg(1);
141
142 my $expected;
143
144 $expected = 226;
145 $self->assert($expected == $resp_code,
146 test_msg("Expected response code $expected, got $resp_code"));
147
148 $expected = "Hello user!";
149 $self->assert($expected eq $resp_msg,
150 test_msg("Expected response message '$expected', got '$resp_msg'"));
151 };
152
153 if ($@) {
154 $ex = $@;
155 }
156
157 $wfh->print("done\n");
158 $wfh->flush();
159
160 } else {
161 eval { server_wait($config_file, $rfh) };
162 if ($@) {
163 warn($@);
164 exit 1;
165 }
166
167 exit 0;
168 }
169
170 # Stop server
171 server_stop($pid_file);
172
173 $self->assert_child_ok($pid);
174
175 if ($ex) {
176 die($ex);
177 }
178
179 unlink($log_file);
180 }
181
182 sub displayfilexfer_rel_path {
183 my $self = shift;
184 my $tmpdir = $self->{tmpdir};
185
186 my $config_file = "$tmpdir/config.conf";
187 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
188 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
189
190 my $log_file = File::Spec->rel2abs('tests.log');
191
192 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
193 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
194
195 my $user = 'proftpd';
196 my $passwd = 'test';
197 my $home_dir = File::Spec->rel2abs($tmpdir);
198
199 auth_user_write($auth_user_file, $user, $passwd, 500, 500, $home_dir,
200 '/bin/bash');
201 auth_group_write($auth_group_file, 'ftpd', 500, $user);
202
203 my $xfer_file = 'xfer.txt';
204
205 my $xfer_path = File::Spec->rel2abs("$tmpdir/$xfer_file");
206 if (open(my $fh, "> $xfer_path")) {
207 print $fh "Hello user!\n";
208
209 unless (close($fh)) {
210 die("Unable to write $xfer_path: $!");
211 }
212
213 } else {
214 die("Unable to open $xfer_path: $!");
215 }
216
217 my $config = {
218 PidFile => $pid_file,
219 ScoreboardFile => $scoreboard_file,
220 SystemLog => $log_file,
221
222 AuthUserFile => $auth_user_file,
223 AuthGroupFile => $auth_group_file,
224 AuthOrder => 'mod_auth_file.c',
225
226 DisplayFileTransfer => $xfer_file,
227
228 IfModules => {
229 'mod_delay.c' => {
230 DelayEngine => 'off',
231 },
232 },
233 };
234
235 my ($port, $config_user, $config_group) = config_write($config_file, $config);
236
237 # Open pipes, for use between the parent and child processes. Specifically,
238 # the child will indicate when it's done with its test by writing a message
239 # to the parent.
240 my ($rfh, $wfh);
241 unless (pipe($rfh, $wfh)) {
242 die("Can't open pipe: $!");
243 }
244
245 my $ex;
246
247 # Fork child
248 $self->handle_sigchld();
249 defined(my $pid = fork()) or die("Can't fork: $!");
250 if ($pid) {
251 eval {
252 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
253 $client->login($user, $passwd);
254
255 my $conn = $client->retr_raw('config.conf');
256 unless ($conn) {
257 die("RETR config.conf failed: " . $client->response_code() . " " .
258 $client->response_msg());
259 }
260
261 my $buf;
262 $conn->read($buf, 32768, 30);
263 $conn->close();
264
265 my ($resp_code, $resp_msg);
266
267 $resp_code = $client->response_code();
268 $resp_msg = $client->response_msg(1);
269
270 my $expected;
271
272 $expected = 226;
273 $self->assert($expected == $resp_code,
274 test_msg("Expected response code $expected, got $resp_code"));
275
276 $expected = "Hello user!";
277 $self->assert($expected eq $resp_msg,
278 test_msg("Expected response message '$expected', got '$resp_msg'"));
279 };
280
281 if ($@) {
282 $ex = $@;
283 }
284
285 $wfh->print("done\n");
286 $wfh->flush();
287
288 } else {
289 eval { server_wait($config_file, $rfh) };
290 if ($@) {
291 warn($@);
292 exit 1;
293 }
294
295 exit 0;
296 }
297
298 # Stop server
299 server_stop($pid_file);
300
301 $self->assert_child_ok($pid);
302
303 if ($ex) {
304 die($ex);
305 }
306
307 unlink($log_file);
308 }
309
310 sub displayfilexfer_chrooted {
311 my $self = shift;
312 my $tmpdir = $self->{tmpdir};
313
314 my $config_file = "$tmpdir/config.conf";
315 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
316 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
317
318 my $log_file = File::Spec->rel2abs('tests.log');
319
320 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
321 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
322
323 my $user = 'proftpd';
324 my $passwd = 'test';
325
326 my $home_dir = File::Spec->rel2abs("$tmpdir/home");
327 mkpath($home_dir);
328
329 my $uid = 500;
330 my $gid = 500;
331
332 # Make sure that, if we're running as root, that the home directory has
333 # permissions/privs set for the account we create
334 if ($< == 0) {
335 unless (chmod(0755, $home_dir)) {
336 die("Can't set perms on $home_dir to 0755: $!");
337 }
338
339 unless (chown($uid, $gid, $home_dir)) {
340 die("Can't set owner of $home_dir to $uid/$gid: $!");
341 }
342 }
343
344 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
345 '/bin/bash');
346 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
347
348 my $xfer_file = File::Spec->rel2abs("$tmpdir/xfer.txt");
349 if (open(my $fh, "> $xfer_file")) {
350 print $fh "Hello user!\n";
351 unless (close($fh)) {
352 die("Can't write $xfer_file: $!");
353 }
354
355 } else {
356 die("Can't open $xfer_file: $!");
357 }
358
359 my $test_file = File::Spec->rel2abs("$home_dir/test.txt");
360 if (open(my $fh, "> $test_file")) {
361 close($fh);
362
363 } else {
364 die("Can't open $test_file: $!");
365 }
366
367 my $config = {
368 PidFile => $pid_file,
369 ScoreboardFile => $scoreboard_file,
370 SystemLog => $log_file,
371
372 AuthUserFile => $auth_user_file,
373 AuthGroupFile => $auth_group_file,
374 AuthOrder => 'mod_auth_file.c',
375
376 DefaultRoot => '~',
377 DisplayFileTransfer => $xfer_file,
378
379 IfModules => {
380 'mod_delay.c' => {
381 DelayEngine => 'off',
382 },
383 },
384 };
385
386 my ($port, $config_user, $config_group) = config_write($config_file, $config);
387
388 # Open pipes, for use between the parent and child processes. Specifically,
389 # the child will indicate when it's done with its test by writing a message
390 # to the parent.
391 my ($rfh, $wfh);
392 unless (pipe($rfh, $wfh)) {
393 die("Can't open pipe: $!");
394 }
395
396 my $ex;
397
398 # Fork child
399 $self->handle_sigchld();
400 defined(my $pid = fork()) or die("Can't fork: $!");
401 if ($pid) {
402 eval {
403 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
404 $client->login($user, $passwd);
405
406 my $conn = $client->retr_raw('test.txt');
407 unless ($conn) {
408 die("RETR test.txt failed: " . $client->response_code() . " " .
409 $client->response_msg());
410 }
411
412 my $buf;
413 $conn->read($buf, 32768, 30);
414 $conn->close();
415
416 my ($resp_code, $resp_msg);
417
418 $resp_code = $client->response_code();
419 $resp_msg = $client->response_msg(1);
420
421 my $expected;
422
423 $expected = 226;
424 $self->assert($expected == $resp_code,
425 test_msg("Expected response code $expected, got $resp_code"));
426
427 $expected = "Hello user!";
428 $self->assert($expected eq $resp_msg,
429 test_msg("Expected response message '$expected', got '$resp_msg'"));
430 };
431
432 if ($@) {
433 $ex = $@;
434 }
435
436 $wfh->print("done\n");
437 $wfh->flush();
438
439 } else {
440 eval { server_wait($config_file, $rfh) };
441 if ($@) {
442 warn($@);
443 exit 1;
444 }
445
446 exit 0;
447 }
448
449 # Stop server
450 server_stop($pid_file);
451
452 $self->assert_child_ok($pid);
453
454 if ($ex) {
455 die($ex);
456 }
457
458 unlink($log_file);
459 }
460
461 sub displayfilexfer_multiline {
462 my $self = shift;
463 my $tmpdir = $self->{tmpdir};
464
465 my $config_file = "$tmpdir/config.conf";
466 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
467 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
468
469 my $log_file = File::Spec->rel2abs('tests.log');
470
471 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
472 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
473
474 my $user = 'proftpd';
475 my $passwd = 'test';
476 my $home_dir = File::Spec->rel2abs($tmpdir);
477
478 auth_user_write($auth_user_file, $user, $passwd, 500, 500, $home_dir,
479 '/bin/bash');
480 auth_group_write($auth_group_file, 'ftpd', 500, $user);
481
482 my $xfer_file = File::Spec->rel2abs("$tmpdir/xfer.txt");
483 if (open(my $fh, "> $xfer_file")) {
484 print $fh "Thanks for transferring a file\nEnjoy the bits\nHello user!\n";
485
486 unless (close($fh)) {
487 die("Unable to write $xfer_file: $!");
488 }
489
490 } else {
491 die("Unable to open $xfer_file: $!");
492 }
493
494 my $config = {
495 PidFile => $pid_file,
496 ScoreboardFile => $scoreboard_file,
497 SystemLog => $log_file,
498
499 AuthUserFile => $auth_user_file,
500 AuthGroupFile => $auth_group_file,
501 AuthOrder => 'mod_auth_file.c',
502
503 DisplayFileTransfer => $xfer_file,
504
505 IfModules => {
506 'mod_delay.c' => {
507 DelayEngine => 'off',
508 },
509 },
510 };
511
512 my ($port, $config_user, $config_group) = config_write($config_file, $config);
513
514 # Open pipes, for use between the parent and child processes. Specifically,
515 # the child will indicate when it's done with its test by writing a message
516 # to the parent.
517 my ($rfh, $wfh);
518 unless (pipe($rfh, $wfh)) {
519 die("Can't open pipe: $!");
520 }
521
522 my $ex;
523
524 # Fork child
525 $self->handle_sigchld();
526 defined(my $pid = fork()) or die("Can't fork: $!");
527 if ($pid) {
528 eval {
529 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
530
531 $client->login($user, $passwd);
532
533 my $conn = $client->retr_raw('config.conf');
534 unless ($conn) {
535 die("RETR config.conf failed: " . $client->response_code() . " " .
536 $client->response_msg());
537 }
538
539 my $buf;
540 $conn->read($buf, 32768, 30);
541 $conn->close();
542
543 my ($resp_code, $resp_msg);
544
545 $resp_code = $client->response_code();
546 $resp_msg = $client->response_msg(3);
547
548 my $expected;
549
550 $expected = 226;
551 $self->assert($expected == $resp_code,
552 test_msg("Expected response code $expected, got $resp_code"));
553
554 $expected = " Hello user!";
555 $self->assert($expected eq $resp_msg,
556 test_msg("Expected response message '$expected', got '$resp_msg'"));
557 };
558
559 if ($@) {
560 $ex = $@;
561 }
562
563 $wfh->print("done\n");
564 $wfh->flush();
565
566 } else {
567 eval { server_wait($config_file, $rfh) };
568 if ($@) {
569 warn($@);
570 exit 1;
571 }
572
573 exit 0;
574 }
575
576 # Stop server
577 server_stop($pid_file);
578
579 $self->assert_child_ok($pid);
580
581 if ($ex) {
582 die($ex);
583 }
584
585 unlink($log_file);
586 }
587
588 sub displayfilexfer_non_path {
589 my $self = shift;
590 my $tmpdir = $self->{tmpdir};
591 my $setup = test_setup($tmpdir, 'config');
592
593 my $xfer_file = '"SUCCESS: Transfer file count:%t | Bytes transferred:%K"';
594
595 my $config = {
596 PidFile => $setup->{pid_file},
597 ScoreboardFile => $setup->{scoreboard_file},
598 SystemLog => $setup->{log_file},
599 TraceLog => $setup->{log_file},
600 Trace => 'response:10',
601
602 AuthUserFile => $setup->{auth_user_file},
603 AuthGroupFile => $setup->{auth_group_file},
604 AuthOrder => 'mod_auth_file.c',
605
606 DisplayFileTransfer => $xfer_file,
607
608 IfModules => {
609 'mod_delay.c' => {
610 DelayEngine => 'off',
611 },
612 },
613 };
614
615 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
616 $config);
617
618 # Open pipes, for use between the parent and child processes. Specifically,
619 # the child will indicate when it's done with its test by writing a message
620 # to the parent.
621 my ($rfh, $wfh);
622 unless (pipe($rfh, $wfh)) {
623 die("Can't open pipe: $!");
624 }
625
626 my $ex;
627
628 # Fork child
629 $self->handle_sigchld();
630 defined(my $pid = fork()) or die("Can't fork: $!");
631 if ($pid) {
632 eval {
633 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
634 $client->login($setup->{user}, $setup->{passwd});
635
636 my $conn = $client->retr_raw('config.conf');
637 unless ($conn) {
638 die("RETR config.conf failed: " . $client->response_code() . " " .
639 $client->response_msg());
640 }
641
642 my $buf;
643 $conn->read($buf, 32768, 5);
644 eval { $conn->close() };
645
646 my $resp_code = $client->response_code();
647 my $resp_msg = $client->response_msg();
648
649 my $expected = 226;
650 $self->assert($expected == $resp_code,
651 test_msg("Expected response code $expected, got $resp_code"));
652 };
653
654 if ($@) {
655 $ex = $@;
656 }
657
658 $wfh->print("done\n");
659 $wfh->flush();
660
661 } else {
662 eval { server_wait($setup->{config_file}, $rfh) };
663 if ($@) {
664 warn($@);
665 exit 1;
666 }
667
668 exit 0;
669 }
670
671 # Stop server
672 server_stop($setup->{pid_file});
673 $self->assert_child_ok($pid);
674
675 test_cleanup($setup->{log_file}, $ex);
676 }
677
678 1;