Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Commands::PASS;
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 pass_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 pass_fails_no_user => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 pass_fails_no_passwd => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 pass_nonascii_ok => {
34 order => ++$order,
35 test_class => [qw(forking)],
36 },
37
38 pass_spaces_ok => {
39 order => ++$order,
40 test_class => [qw(forking)],
41 },
42
43 };
44
45 sub new {
46 return shift()->SUPER::new(@_);
47 }
48
49 sub list_tests {
50 return testsuite_get_runnable_tests($TESTS);
51 }
52
53 sub pass_ok {
54 my $self = shift;
55 my $tmpdir = $self->{tmpdir};
56
57 my $config_file = "$tmpdir/cmds.conf";
58 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
59 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
60
61 my $log_file = test_get_logfile();
62
63 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
64 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
65
66 my $user = 'proftpd';
67 my $passwd = 'test';
68 my $group = 'ftpd';
69 my $home_dir = File::Spec->rel2abs($tmpdir);
70 my $uid = 500;
71 my $gid = 500;
72
73 # Make sure that, if we're running as root, that the home directory has
74 # permissions/privs set for the account we create
75 if ($< == 0) {
76 unless (chmod(0755, $home_dir)) {
77 die("Can't set perms on $home_dir to 0755: $!");
78 }
79
80 unless (chown($uid, $gid, $home_dir)) {
81 die("Can't set owner of $home_dir to $uid/$gid: $!");
82 }
83 }
84
85 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
86 '/bin/bash');
87 auth_group_write($auth_group_file, $group, $gid, $user);
88
89 my $config = {
90 PidFile => $pid_file,
91 ScoreboardFile => $scoreboard_file,
92 SystemLog => $log_file,
93
94 AuthUserFile => $auth_user_file,
95 AuthGroupFile => $auth_group_file,
96 AuthOrder => 'mod_auth_file.c',
97
98 IfModules => {
99 'mod_delay.c' => {
100 DelayEngine => 'off',
101 },
102 },
103 };
104
105 my ($port, $config_user, $config_group) = config_write($config_file, $config);
106
107 # Open pipes, for use between the parent and child processes. Specifically,
108 # the child will indicate when it's done with its test by writing a message
109 # to the parent.
110 my ($rfh, $wfh);
111 unless (pipe($rfh, $wfh)) {
112 die("Can't open pipe: $!");
113 }
114
115 my $ex;
116
117 # Fork child
118 $self->handle_sigchld();
119 defined(my $pid = fork()) or die("Can't fork: $!");
120 if ($pid) {
121 eval {
122 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
123 $client->user($user);
124 $client->pass($passwd);
125
126 my ($resp_code, $resp_msg);
127 $resp_code = $client->response_code();
128 $resp_msg = $client->response_msg();
129
130 my $expected;
131
132 $expected = 230;
133 $self->assert($expected == $resp_code,
134 test_msg("Expected $expected, got $resp_code"));
135
136 $expected = "User $user logged in";
137 $self->assert($expected eq $resp_msg,
138 test_msg("Expected '$expected', got '$resp_msg'"));
139 };
140
141 if ($@) {
142 $ex = $@;
143 }
144
145 $wfh->print("done\n");
146 $wfh->flush();
147
148 } else {
149 eval { server_wait($config_file, $rfh) };
150 if ($@) {
151 warn($@);
152 exit 1;
153 }
154
155 exit 0;
156 }
157
158 # Stop server
159 server_stop($pid_file);
160
161 $self->assert_child_ok($pid);
162
163 if ($ex) {
164 test_append_logfile($log_file, $ex);
165 unlink($log_file);
166
167 die($ex);
168 }
169
170 unlink($log_file);
171 }
172
173 sub pass_fails_no_user {
174 my $self = shift;
175 my $tmpdir = $self->{tmpdir};
176
177 my $config_file = "$tmpdir/cmds.conf";
178 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
179 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
180
181 my $log_file = test_get_logfile();
182
183 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
184 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
185
186 my $user = 'proftpd';
187 my $passwd = 'test';
188 my $group = 'ftpd';
189 my $home_dir = File::Spec->rel2abs($tmpdir);
190 my $uid = 500;
191 my $gid = 500;
192
193 # Make sure that, if we're running as root, that the home directory has
194 # permissions/privs set for the account we create
195 if ($< == 0) {
196 unless (chmod(0755, $home_dir)) {
197 die("Can't set perms on $home_dir to 0755: $!");
198 }
199
200 unless (chown($uid, $gid, $home_dir)) {
201 die("Can't set owner of $home_dir to $uid/$gid: $!");
202 }
203 }
204
205 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
206 '/bin/bash');
207 auth_group_write($auth_group_file, $group, $gid, $user);
208
209 my $config = {
210 PidFile => $pid_file,
211 ScoreboardFile => $scoreboard_file,
212 SystemLog => $log_file,
213
214 AuthUserFile => $auth_user_file,
215 AuthGroupFile => $auth_group_file,
216 AuthOrder => 'mod_auth_file.c',
217
218 IfModules => {
219 'mod_delay.c' => {
220 DelayEngine => 'off',
221 },
222 },
223 };
224
225 my ($port, $config_user, $config_group) = config_write($config_file, $config);
226
227 # Open pipes, for use between the parent and child processes. Specifically,
228 # the child will indicate when it's done with its test by writing a message
229 # to the parent.
230 my ($rfh, $wfh);
231 unless (pipe($rfh, $wfh)) {
232 die("Can't open pipe: $!");
233 }
234
235 my $ex;
236
237 # Fork child
238 $self->handle_sigchld();
239 defined(my $pid = fork()) or die("Can't fork: $!");
240 if ($pid) {
241 eval {
242 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
243
244 my ($resp_code, $resp_msg);
245 eval { ($resp_code, $resp_msg) = $client->pass($passwd) };
246 unless ($@) {
247 die("PASS succeeded unexpectedly ($resp_code $resp_msg)");
248
249 } else {
250 $resp_code = $client->response_code();
251 $resp_msg = $client->response_msg();
252 }
253
254 my $expected;
255
256 $expected = 503;
257 $self->assert($expected == $resp_code,
258 test_msg("Expected $expected, got $resp_code"));
259
260 $expected = "Login with USER first";
261 $self->assert($expected eq $resp_msg,
262 test_msg("Expected '$expected', got '$resp_msg'"));
263 };
264
265 if ($@) {
266 $ex = $@;
267 }
268
269 $wfh->print("done\n");
270 $wfh->flush();
271
272 } else {
273 eval { server_wait($config_file, $rfh) };
274 if ($@) {
275 warn($@);
276 exit 1;
277 }
278
279 exit 0;
280 }
281
282 # Stop server
283 server_stop($pid_file);
284
285 $self->assert_child_ok($pid);
286
287 if ($ex) {
288 test_append_logfile($log_file, $ex);
289 unlink($log_file);
290
291 die($ex);
292 }
293
294 unlink($log_file);
295 }
296
297 sub pass_fails_no_passwd {
298 my $self = shift;
299 my $tmpdir = $self->{tmpdir};
300
301 my $config_file = "$tmpdir/cmds.conf";
302 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
303 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
304
305 my $log_file = test_get_logfile();
306
307 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
308 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
309
310 my $user = 'proftpd';
311 my $passwd = 'test';
312 my $group = 'ftpd';
313 my $home_dir = File::Spec->rel2abs($tmpdir);
314 my $uid = 500;
315 my $gid = 500;
316
317 # Make sure that, if we're running as root, that the home directory has
318 # permissions/privs set for the account we create
319 if ($< == 0) {
320 unless (chmod(0755, $home_dir)) {
321 die("Can't set perms on $home_dir to 0755: $!");
322 }
323
324 unless (chown($uid, $gid, $home_dir)) {
325 die("Can't set owner of $home_dir to $uid/$gid: $!");
326 }
327 }
328
329 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
330 '/bin/bash');
331 auth_group_write($auth_group_file, $group, $gid, $user);
332
333 my $config = {
334 PidFile => $pid_file,
335 ScoreboardFile => $scoreboard_file,
336 SystemLog => $log_file,
337
338 AuthUserFile => $auth_user_file,
339 AuthGroupFile => $auth_group_file,
340 AuthOrder => 'mod_auth_file.c',
341
342 IfModules => {
343 'mod_delay.c' => {
344 DelayEngine => 'off',
345 },
346 },
347 };
348
349 my ($port, $config_user, $config_group) = config_write($config_file, $config);
350
351 # Open pipes, for use between the parent and child processes. Specifically,
352 # the child will indicate when it's done with its test by writing a message
353 # to the parent.
354 my ($rfh, $wfh);
355 unless (pipe($rfh, $wfh)) {
356 die("Can't open pipe: $!");
357 }
358
359 my $ex;
360
361 # Fork child
362 $self->handle_sigchld();
363 defined(my $pid = fork()) or die("Can't fork: $!");
364 if ($pid) {
365 eval {
366 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
367
368 $client->user($user);
369
370 my ($resp_code, $resp_msg);
371 eval { ($resp_code, $resp_msg) = $client->pass() };
372 unless ($@) {
373 die("PASS succeeded unexpectedly ($resp_code $resp_msg)");
374
375 } else {
376 $resp_code = $client->response_code();
377 $resp_msg = $client->response_msg();
378 }
379
380 my $expected;
381
382 $expected = 530;
383 $self->assert($expected == $resp_code,
384 test_msg("Expected $expected, got $resp_code"));
385
386 $expected = "Login incorrect.";
387 $self->assert($expected eq $resp_msg,
388 test_msg("Expected '$expected', got '$resp_msg'"));
389 };
390
391 if ($@) {
392 $ex = $@;
393 }
394
395 $wfh->print("done\n");
396 $wfh->flush();
397
398 } else {
399 eval { server_wait($config_file, $rfh) };
400 if ($@) {
401 warn($@);
402 exit 1;
403 }
404
405 exit 0;
406 }
407
408 # Stop server
409 server_stop($pid_file);
410
411 $self->assert_child_ok($pid);
412
413 if ($ex) {
414 test_append_logfile($log_file, $ex);
415 unlink($log_file);
416
417 die($ex);
418 }
419
420 unlink($log_file);
421 }
422
423 sub pass_nonascii_ok {
424 my $self = shift;
425 my $tmpdir = $self->{tmpdir};
426
427 my $config_file = "$tmpdir/cmds.conf";
428 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
429 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
430
431 my $log_file = test_get_logfile();
432
433 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
434 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
435
436 # This testcase was prompted by the following forums post:
437 #
438 # http://forums.proftpd.org/smf/index.php/topic,8605.0.html
439
440 my $user = 'proftpd';
441 my $passwd = 't£st';
442 my $group = 'ftpd';
443 my $home_dir = File::Spec->rel2abs($tmpdir);
444 my $uid = 500;
445 my $gid = 500;
446
447 # Make sure that, if we're running as root, that the home directory has
448 # permissions/privs set for the account we create
449 if ($< == 0) {
450 unless (chmod(0755, $home_dir)) {
451 die("Can't set perms on $home_dir to 0755: $!");
452 }
453
454 unless (chown($uid, $gid, $home_dir)) {
455 die("Can't set owner of $home_dir to $uid/$gid: $!");
456 }
457 }
458
459 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
460 '/bin/bash');
461 auth_group_write($auth_group_file, $group, $gid, $user);
462
463 my $config = {
464 PidFile => $pid_file,
465 ScoreboardFile => $scoreboard_file,
466 SystemLog => $log_file,
467
468 AuthUserFile => $auth_user_file,
469 AuthGroupFile => $auth_group_file,
470 AuthOrder => 'mod_auth_file.c',
471
472 IfModules => {
473 'mod_delay.c' => {
474 DelayEngine => 'off',
475 },
476 },
477 };
478
479 my ($port, $config_user, $config_group) = config_write($config_file, $config);
480
481 # Open pipes, for use between the parent and child processes. Specifically,
482 # the child will indicate when it's done with its test by writing a message
483 # to the parent.
484 my ($rfh, $wfh);
485 unless (pipe($rfh, $wfh)) {
486 die("Can't open pipe: $!");
487 }
488
489 my $ex;
490
491 # Fork child
492 $self->handle_sigchld();
493 defined(my $pid = fork()) or die("Can't fork: $!");
494 if ($pid) {
495 eval {
496 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
497 $client->user($user);
498 $client->pass($passwd);
499
500 my $resp_code = $client->response_code();
501 my $resp_msg = $client->response_msg();
502
503 my $expected;
504
505 $expected = 230;
506 $self->assert($expected == $resp_code,
507 test_msg("Expected $expected, got $resp_code"));
508
509 $expected = "User $user logged in";
510 $self->assert($expected eq $resp_msg,
511 test_msg("Expected '$expected', got '$resp_msg'"));
512 };
513
514 if ($@) {
515 $ex = $@;
516 }
517
518 $wfh->print("done\n");
519 $wfh->flush();
520
521 } else {
522 eval { server_wait($config_file, $rfh) };
523 if ($@) {
524 warn($@);
525 exit 1;
526 }
527
528 exit 0;
529 }
530
531 # Stop server
532 server_stop($pid_file);
533
534 $self->assert_child_ok($pid);
535
536 if ($ex) {
537 test_append_logfile($log_file, $ex);
538 unlink($log_file);
539
540 die($ex);
541 }
542
543 unlink($log_file);
544 }
545
546 sub pass_spaces_bugXXXX {
547 my $self = shift;
548 my $tmpdir = $self->{tmpdir};
549
550 my $config_file = "$tmpdir/cmds.conf";
551 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
552 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
553
554 my $log_file = test_get_logfile();
555
556 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
557 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
558
559 # This testcase was prompted by the following forums post:
560 #
561 # http://forums.proftpd.org/smf/index.php/topic,8605.0.html
562
563 my $user = 'proftpd';
564 my $passwd = 'test password with spaces';
565 my $group = 'ftpd';
566 my $home_dir = File::Spec->rel2abs($tmpdir);
567 my $uid = 500;
568 my $gid = 500;
569
570 # Make sure that, if we're running as root, that the home directory has
571 # permissions/privs set for the account we create
572 if ($< == 0) {
573 unless (chmod(0755, $home_dir)) {
574 die("Can't set perms on $home_dir to 0755: $!");
575 }
576
577 unless (chown($uid, $gid, $home_dir)) {
578 die("Can't set owner of $home_dir to $uid/$gid: $!");
579 }
580 }
581
582 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
583 '/bin/bash');
584 auth_group_write($auth_group_file, $group, $gid, $user);
585
586 my $config = {
587 PidFile => $pid_file,
588 ScoreboardFile => $scoreboard_file,
589 SystemLog => $log_file,
590
591 AuthUserFile => $auth_user_file,
592 AuthGroupFile => $auth_group_file,
593 AuthOrder => 'mod_auth_file.c',
594
595 IfModules => {
596 'mod_delay.c' => {
597 DelayEngine => 'off',
598 },
599 },
600 };
601
602 my ($port, $config_user, $config_group) = config_write($config_file, $config);
603
604 # Open pipes, for use between the parent and child processes. Specifically,
605 # the child will indicate when it's done with its test by writing a message
606 # to the parent.
607 my ($rfh, $wfh);
608 unless (pipe($rfh, $wfh)) {
609 die("Can't open pipe: $!");
610 }
611
612 my $ex;
613
614 # Fork child
615 $self->handle_sigchld();
616 defined(my $pid = fork()) or die("Can't fork: $!");
617 if ($pid) {
618 eval {
619 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
620 $client->user($user);
621 $client->pass($passwd);
622
623 my $resp_code = $client->response_code();
624 my $resp_msg = $client->response_msg();
625
626 my $expected;
627
628 $expected = 230;
629 $self->assert($expected == $resp_code,
630 test_msg("Expected $expected, got $resp_code"));
631
632 $expected = "User $user logged in";
633 $self->assert($expected eq $resp_msg,
634 test_msg("Expected '$expected', got '$resp_msg'"));
635 };
636
637 if ($@) {
638 $ex = $@;
639 }
640
641 $wfh->print("done\n");
642 $wfh->flush();
643
644 } else {
645 eval { server_wait($config_file, $rfh) };
646 if ($@) {
647 warn($@);
648 exit 1;
649 }
650
651 exit 0;
652 }
653
654 # Stop server
655 server_stop($pid_file);
656
657 $self->assert_child_ok($pid);
658
659 if ($ex) {
660 test_append_logfile($log_file, $ex);
661 unlink($log_file);
662
663 die($ex);
664 }
665
666 unlink($log_file);
667 }
668
669 sub pass_spaces_ok {
670 my $self = shift;
671 my $tmpdir = $self->{tmpdir};
672
673 my $config_file = "$tmpdir/cmds.conf";
674 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
675 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
676
677 my $log_file = test_get_logfile();
678
679 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
680 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
681
682 my $user = 'proftpd';
683 my $passwd = 'test passphrase with words';
684 my $group = 'ftpd';
685 my $home_dir = File::Spec->rel2abs($tmpdir);
686 my $uid = 500;
687 my $gid = 500;
688
689 # Make sure that, if we're running as root, that the home directory has
690 # permissions/privs set for the account we create
691 if ($< == 0) {
692 unless (chmod(0755, $home_dir)) {
693 die("Can't set perms on $home_dir to 0755: $!");
694 }
695
696 unless (chown($uid, $gid, $home_dir)) {
697 die("Can't set owner of $home_dir to $uid/$gid: $!");
698 }
699 }
700
701 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
702 '/bin/bash');
703 auth_group_write($auth_group_file, $group, $gid, $user);
704
705 my $config = {
706 PidFile => $pid_file,
707 ScoreboardFile => $scoreboard_file,
708 SystemLog => $log_file,
709
710 AuthUserFile => $auth_user_file,
711 AuthGroupFile => $auth_group_file,
712 AuthOrder => 'mod_auth_file.c',
713
714 IfModules => {
715 'mod_delay.c' => {
716 DelayEngine => 'off',
717 },
718 },
719 };
720
721 my ($port, $config_user, $config_group) = config_write($config_file, $config);
722
723 # Open pipes, for use between the parent and child processes. Specifically,
724 # the child will indicate when it's done with its test by writing a message
725 # to the parent.
726 my ($rfh, $wfh);
727 unless (pipe($rfh, $wfh)) {
728 die("Can't open pipe: $!");
729 }
730
731 my $ex;
732
733 # Fork child
734 $self->handle_sigchld();
735 defined(my $pid = fork()) or die("Can't fork: $!");
736 if ($pid) {
737 eval {
738 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
739 $client->user($user);
740 $client->pass($passwd);
741
742 my ($resp_code, $resp_msg);
743 $resp_code = $client->response_code();
744 $resp_msg = $client->response_msg();
745
746 my $expected;
747
748 $expected = 230;
749 $self->assert($expected == $resp_code,
750 test_msg("Expected $expected, got $resp_code"));
751
752 $expected = "User $user logged in";
753 $self->assert($expected eq $resp_msg,
754 test_msg("Expected '$expected', got '$resp_msg'"));
755 };
756
757 if ($@) {
758 $ex = $@;
759 }
760
761 $wfh->print("done\n");
762 $wfh->flush();
763
764 } else {
765 eval { server_wait($config_file, $rfh) };
766 if ($@) {
767 warn($@);
768 exit 1;
769 }
770
771 exit 0;
772 }
773
774 # Stop server
775 server_stop($pid_file);
776
777 $self->assert_child_ok($pid);
778
779 if ($ex) {
780 test_append_logfile($log_file, $ex);
781 unlink($log_file);
782
783 die($ex);
784 }
785
786 unlink($log_file);
787 }
788
789 1;