| 1 |
package ProFTPD::Tests::Config::MaxTransfersPerHost; |
| 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 |
maxtransfersperhost_retr => { |
| 19 |
order => ++$order, |
| 20 |
test_class => [qw(forking)], |
| 21 |
}, |
| 22 |
|
| 23 |
maxtransfersperhost_retr_custom_message => { |
| 24 |
order => ++$order, |
| 25 |
test_class => [qw(forking)], |
| 26 |
}, |
| 27 |
|
| 28 |
}; |
| 29 |
|
| 30 |
sub new { |
| 31 |
return shift()->SUPER::new(@_); |
| 32 |
} |
| 33 |
|
| 34 |
sub list_tests { |
| 35 |
return testsuite_get_runnable_tests($TESTS); |
| 36 |
} |
| 37 |
|
| 38 |
sub maxtransfersperhost_retr { |
| 39 |
my $self = shift; |
| 40 |
my $tmpdir = $self->{tmpdir}; |
| 41 |
my $setup = test_setup($tmpdir, 'config'); |
| 42 |
|
| 43 |
my $test_file = File::Spec->rel2abs($setup->{config_file}); |
| 44 |
my $max_transfers = 1; |
| 45 |
|
| 46 |
my $config = { |
| 47 |
PidFile => $setup->{pid_file}, |
| 48 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 49 |
SystemLog => $setup->{log_file}, |
| 50 |
TraceLog => $setup->{log_file}, |
| 51 |
Trace => 'xfer:30', |
| 52 |
|
| 53 |
AuthUserFile => $setup->{auth_user_file}, |
| 54 |
AuthGroupFile => $setup->{auth_group_file}, |
| 55 |
AuthOrder => 'mod_auth_file.c', |
| 56 |
|
| 57 |
MaxTransfersPerHost => "RETR $max_transfers", |
| 58 |
|
| 59 |
IfModules => { |
| 60 |
'mod_delay.c' => { |
| 61 |
DelayEngine => 'off', |
| 62 |
}, |
| 63 |
}, |
| 64 |
}; |
| 65 |
|
| 66 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 67 |
$config); |
| 68 |
|
| 69 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 70 |
# the child will indicate when it's done with its test by writing a message |
| 71 |
# to the parent. |
| 72 |
my ($rfh, $wfh); |
| 73 |
unless (pipe($rfh, $wfh)) { |
| 74 |
die("Can't open pipe: $!"); |
| 75 |
} |
| 76 |
|
| 77 |
my $ex; |
| 78 |
|
| 79 |
# Fork child |
| 80 |
$self->handle_sigchld(); |
| 81 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 82 |
if ($pid) { |
| 83 |
eval { |
| 84 |
# First client should be able to connect and log in... |
| 85 |
my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 86 |
$client1->login($setup->{user}, $setup->{passwd}); |
| 87 |
my $conn1 = $client1->retr_raw($test_file); |
| 88 |
unless ($conn1) { |
| 89 |
die("RETR failed: " . $client1->response_code() . " " . |
| 90 |
$client1->response_msg()); |
| 91 |
} |
| 92 |
|
| 93 |
my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 94 |
$client2->login($setup->{user}, $setup->{passwd}); |
| 95 |
my $conn2 = $client2->retr_raw($test_file); |
| 96 |
if ($conn2) { |
| 97 |
die("RETR succeeded unexpectedly"); |
| 98 |
} |
| 99 |
|
| 100 |
my $resp_code = $client2->response_code(); |
| 101 |
my $resp_msg = $client2->response_msg(); |
| 102 |
|
| 103 |
my $expected = 451; |
| 104 |
$self->assert($expected == $resp_code, |
| 105 |
test_msg("Expected response code $expected, got $resp_code")); |
| 106 |
|
| 107 |
$expected = "Sorry, the maximum number of data transfers ($max_transfers) from your host are currently being used."; |
| 108 |
$self->assert($expected eq $resp_msg, |
| 109 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 110 |
|
| 111 |
$client1->quit(); |
| 112 |
$client2->quit(); |
| 113 |
}; |
| 114 |
if ($@) { |
| 115 |
$ex = $@; |
| 116 |
} |
| 117 |
|
| 118 |
$wfh->print("done\n"); |
| 119 |
$wfh->flush(); |
| 120 |
|
| 121 |
} else { |
| 122 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 123 |
if ($@) { |
| 124 |
warn($@); |
| 125 |
exit 1; |
| 126 |
} |
| 127 |
|
| 128 |
exit 0; |
| 129 |
} |
| 130 |
|
| 131 |
# Stop server |
| 132 |
server_stop($setup->{pid_file}); |
| 133 |
|
| 134 |
$self->assert_child_ok($pid); |
| 135 |
test_cleanup($setup->{log_file}, $ex); |
| 136 |
} |
| 137 |
|
| 138 |
sub maxtransfersperhost_retr_custom_message { |
| 139 |
my $self = shift; |
| 140 |
my $tmpdir = $self->{tmpdir}; |
| 141 |
my $setup = test_setup($tmpdir, 'config'); |
| 142 |
|
| 143 |
my $test_file = File::Spec->rel2abs($setup->{config_file}); |
| 144 |
my $max_transfers = 1; |
| 145 |
|
| 146 |
my $config = { |
| 147 |
PidFile => $setup->{pid_file}, |
| 148 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 149 |
SystemLog => $setup->{log_file}, |
| 150 |
TraceLog => $setup->{log_file}, |
| 151 |
Trace => 'xfer:30', |
| 152 |
|
| 153 |
AuthUserFile => $setup->{auth_user_file}, |
| 154 |
AuthGroupFile => $setup->{auth_group_file}, |
| 155 |
AuthOrder => 'mod_auth_file.c', |
| 156 |
|
| 157 |
MaxTransfersPerHost => "RETR $max_transfers \"Too many transfers from your host\"", |
| 158 |
|
| 159 |
IfModules => { |
| 160 |
'mod_delay.c' => { |
| 161 |
DelayEngine => 'off', |
| 162 |
}, |
| 163 |
}, |
| 164 |
}; |
| 165 |
|
| 166 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 167 |
$config); |
| 168 |
|
| 169 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 170 |
# the child will indicate when it's done with its test by writing a message |
| 171 |
# to the parent. |
| 172 |
my ($rfh, $wfh); |
| 173 |
unless (pipe($rfh, $wfh)) { |
| 174 |
die("Can't open pipe: $!"); |
| 175 |
} |
| 176 |
|
| 177 |
my $ex; |
| 178 |
|
| 179 |
# Fork child |
| 180 |
$self->handle_sigchld(); |
| 181 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 182 |
if ($pid) { |
| 183 |
eval { |
| 184 |
# First client should be able to connect and log in... |
| 185 |
my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 186 |
$client1->login($setup->{user}, $setup->{passwd}); |
| 187 |
my $conn1 = $client1->retr_raw($test_file); |
| 188 |
unless ($conn1) { |
| 189 |
die("RETR failed: " . $client1->response_code() . " " . |
| 190 |
$client1->response_msg()); |
| 191 |
} |
| 192 |
|
| 193 |
my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 194 |
$client2->login($setup->{user}, $setup->{passwd}); |
| 195 |
my $conn2 = $client2->retr_raw($test_file); |
| 196 |
if ($conn2) { |
| 197 |
die("RETR succeeded unexpectedly"); |
| 198 |
} |
| 199 |
|
| 200 |
my $resp_code = $client2->response_code(); |
| 201 |
my $resp_msg = $client2->response_msg(); |
| 202 |
|
| 203 |
my $expected = 451; |
| 204 |
$self->assert($expected == $resp_code, |
| 205 |
test_msg("Expected response code $expected, got $resp_code")); |
| 206 |
|
| 207 |
$expected = 'Too many transfers from your host'; |
| 208 |
$self->assert($expected eq $resp_msg, |
| 209 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 210 |
|
| 211 |
$client1->quit(); |
| 212 |
$client2->quit(); |
| 213 |
}; |
| 214 |
if ($@) { |
| 215 |
$ex = $@; |
| 216 |
} |
| 217 |
|
| 218 |
$wfh->print("done\n"); |
| 219 |
$wfh->flush(); |
| 220 |
|
| 221 |
} else { |
| 222 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 223 |
if ($@) { |
| 224 |
warn($@); |
| 225 |
exit 1; |
| 226 |
} |
| 227 |
|
| 228 |
exit 0; |
| 229 |
} |
| 230 |
|
| 231 |
# Stop server |
| 232 |
server_stop($setup->{pid_file}); |
| 233 |
|
| 234 |
$self->assert_child_ok($pid); |
| 235 |
test_cleanup($setup->{log_file}, $ex); |
| 236 |
} |
| 237 |
|
| 238 |
1; |