| 1 |
package ProFTPD::TestSuite::Child; |
| 2 |
|
| 3 |
use base qw(Test::Unit::TestCase); |
| 4 |
use strict; |
| 5 |
|
| 6 |
use Carp; |
| 7 |
use File::Path qw(mkpath rmtree); |
| 8 |
use POSIX qw(:sys_wait_h); |
| 9 |
|
| 10 |
use ProFTPD::TestSuite::Utils qw(:testsuite); |
| 11 |
|
| 12 |
my $processes = {}; |
| 13 |
|
| 14 |
sub sig_chld { |
| 15 |
my $child; |
| 16 |
|
| 17 |
while (($child = waitpid(-1, 0)) > 0) { |
| 18 |
$processes->{$child} = ($? >> 8); |
| 19 |
} |
| 20 |
|
| 21 |
$SIG{CHLD} = \&sig_chld; |
| 22 |
} |
| 23 |
|
| 24 |
sub handle_sigchld { |
| 25 |
my $self = shift; |
| 26 |
$SIG{CHLD} = \&sig_chld; |
| 27 |
} |
| 28 |
|
| 29 |
sub assert_child_ok { |
| 30 |
my $self = shift; |
| 31 |
my $pid = shift; |
| 32 |
|
| 33 |
my ($pkg, $file, $lineno, $func, @rest) = caller(1); |
| 34 |
|
| 35 |
$self->assert($processes->{$pid} == 0, |
| 36 |
"Child test process $pid failed in $func (line $lineno) [see above for possible errors]"); |
| 37 |
} |
| 38 |
|
| 39 |
sub assert_transfer_ok { |
| 40 |
my $self = shift; |
| 41 |
my $resp_code = shift; |
| 42 |
my $resp_msg = shift; |
| 43 |
my $aborted = shift; |
| 44 |
$aborted = 0 unless $aborted; |
| 45 |
|
| 46 |
if ($resp_code == 226) { |
| 47 |
my $expected = "Transfer complete"; |
| 48 |
if ($aborted) { |
| 49 |
$expected = "Abort successful"; |
| 50 |
} |
| 51 |
|
| 52 |
if ($expected ne $resp_msg) { |
| 53 |
croak("Expected response message '$expected', got '$resp_msg'"); |
| 54 |
} |
| 55 |
|
| 56 |
} elsif ($resp_code == 150) { |
| 57 |
my $expected = "Opening .*? mode data connection"; |
| 58 |
if ($resp_msg !~ /$expected/) { |
| 59 |
croak("Expected response message '$expected', got '$resp_msg'"); |
| 60 |
} |
| 61 |
|
| 62 |
} else { |
| 63 |
croak("Expected response code 150 or 226, got $resp_code"); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
sub set_up { |
| 68 |
my $self = shift; |
| 69 |
|
| 70 |
# Create temporary scratch dir |
| 71 |
$self->{tmpdir} = testsuite_get_tmp_dir(); |
| 72 |
} |
| 73 |
|
| 74 |
sub tear_down { |
| 75 |
my $self = shift; |
| 76 |
|
| 77 |
# Remove temporary scratch dir |
| 78 |
if ($self->{tmpdir} && |
| 79 |
!$ENV{KEEP_TMPFILES}) { |
| 80 |
eval { rmtree($self->{tmpdir}) }; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
1; |