| 1 |
package ProFTPD::Tests::Config::TransferRate; |
| 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 |
use Time::HiRes qw(gettimeofday tv_interval); |
| 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 |
transferrate_retr_ok => { |
| 20 |
order => ++$order, |
| 21 |
test_class => [qw(forking)], |
| 22 |
}, |
| 23 |
|
| 24 |
transferrate_stor_ok => { |
| 25 |
order => ++$order, |
| 26 |
test_class => [qw(forking)], |
| 27 |
}, |
| 28 |
|
| 29 |
# XXX Need tests for the free bytes parts |
| 30 |
|
| 31 |
}; |
| 32 |
|
| 33 |
sub new { |
| 34 |
return shift()->SUPER::new(@_); |
| 35 |
} |
| 36 |
|
| 37 |
sub list_tests { |
| 38 |
return testsuite_get_runnable_tests($TESTS); |
| 39 |
} |
| 40 |
|
| 41 |
sub transferrate_retr_ok { |
| 42 |
my $self = shift; |
| 43 |
my $tmpdir = $self->{tmpdir}; |
| 44 |
|
| 45 |
my $config_file = "$tmpdir/config.conf"; |
| 46 |
my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid"); |
| 47 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard"); |
| 48 |
|
| 49 |
my $log_file = File::Spec->rel2abs('tests.log'); |
| 50 |
|
| 51 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd"); |
| 52 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group"); |
| 53 |
|
| 54 |
my $user = 'proftpd'; |
| 55 |
my $passwd = 'test'; |
| 56 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 57 |
my $uid = 500; |
| 58 |
my $gid = 500; |
| 59 |
|
| 60 |
# Make sure that, if we're running as root, that the home directory has |
| 61 |
# permissions/privs set for the account we create |
| 62 |
if ($< == 0) { |
| 63 |
unless (chmod(0755, $home_dir)) { |
| 64 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 65 |
} |
| 66 |
|
| 67 |
unless (chown($uid, $gid, $home_dir)) { |
| 68 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 73 |
'/bin/bash'); |
| 74 |
auth_group_write($auth_group_file, 'ftpd', $gid, $user); |
| 75 |
|
| 76 |
my $test_file = File::Spec->rel2abs("$tmpdir/test.txt"); |
| 77 |
if (open(my $fh, "> $test_file")) { |
| 78 |
print $fh "ABCDefgh" x 1024, "\n"; |
| 79 |
|
| 80 |
unless (close($fh)) { |
| 81 |
die("Can't write $test_file: $!"); |
| 82 |
} |
| 83 |
|
| 84 |
} else { |
| 85 |
die("Can't open $test_file: $!"); |
| 86 |
} |
| 87 |
|
| 88 |
my $timeout_idle = 20; |
| 89 |
|
| 90 |
my $config = { |
| 91 |
PidFile => $pid_file, |
| 92 |
ScoreboardFile => $scoreboard_file, |
| 93 |
SystemLog => $log_file, |
| 94 |
TraceLog => $log_file, |
| 95 |
Trace => 'scoreboard:20', |
| 96 |
|
| 97 |
AuthUserFile => $auth_user_file, |
| 98 |
AuthGroupFile => $auth_group_file, |
| 99 |
AuthOrder => 'mod_auth_file.c', |
| 100 |
|
| 101 |
TimeoutIdle => $timeout_idle, |
| 102 |
|
| 103 |
# 1 KB/sec |
| 104 |
TransferRate => 'RETR 1', |
| 105 |
|
| 106 |
IfModules => { |
| 107 |
'mod_delay.c' => { |
| 108 |
DelayEngine => 'off', |
| 109 |
}, |
| 110 |
}, |
| 111 |
}; |
| 112 |
|
| 113 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 114 |
|
| 115 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 116 |
# the child will indicate when it's done with its test by writing a message |
| 117 |
# to the parent. |
| 118 |
my ($rfh, $wfh); |
| 119 |
unless (pipe($rfh, $wfh)) { |
| 120 |
die("Can't open pipe: $!"); |
| 121 |
} |
| 122 |
|
| 123 |
my $ex; |
| 124 |
|
| 125 |
# Fork child |
| 126 |
$self->handle_sigchld(); |
| 127 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 128 |
if ($pid) { |
| 129 |
eval { |
| 130 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 131 |
$client->login($user, $passwd); |
| 132 |
|
| 133 |
my $conn = $client->retr_raw('test.txt'); |
| 134 |
unless ($conn) { |
| 135 |
die("RETR failed: " . $client->response_code() . " " . |
| 136 |
$client->response_msg()); |
| 137 |
} |
| 138 |
|
| 139 |
my $buf = ''; |
| 140 |
my $tmp; |
| 141 |
|
| 142 |
my $xfer_start = [gettimeofday()]; |
| 143 |
|
| 144 |
while ($conn->read($tmp, 8192, 30)) { |
| 145 |
$buf .= $tmp; |
| 146 |
} |
| 147 |
$conn->close(); |
| 148 |
|
| 149 |
my $xfer_elapsed = tv_interval($xfer_start); |
| 150 |
|
| 151 |
my $resp_code = $client->response_code(); |
| 152 |
my $resp_msg = $client->response_msg(); |
| 153 |
|
| 154 |
$client->quit(); |
| 155 |
|
| 156 |
my $expected; |
| 157 |
|
| 158 |
$expected = 226; |
| 159 |
$self->assert($expected == $resp_code, |
| 160 |
test_msg("Expected $expected, got $resp_code")); |
| 161 |
|
| 162 |
$expected = 'Transfer complete'; |
| 163 |
$self->assert($expected eq $resp_msg, |
| 164 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 165 |
|
| 166 |
$expected = 8193; |
| 167 |
my $buflen = length($buf); |
| 168 |
$self->assert($expected == $buflen, |
| 169 |
test_msg("Expected $expected, got $buflen")); |
| 170 |
|
| 171 |
# We configured a TransferRate of 1 KB/sec, and retrieved 8 KB; |
| 172 |
# thus make sure that the transfer time is more than 8 secs. |
| 173 |
$self->assert($xfer_elapsed > 8, |
| 174 |
test_msg("Expected > 8 secs, got $xfer_elapsed")); |
| 175 |
}; |
| 176 |
|
| 177 |
if ($@) { |
| 178 |
$ex = $@; |
| 179 |
} |
| 180 |
|
| 181 |
$wfh->print("done\n"); |
| 182 |
$wfh->flush(); |
| 183 |
|
| 184 |
} else { |
| 185 |
eval { server_wait($config_file, $rfh, $timeout_idle + 3) }; |
| 186 |
if ($@) { |
| 187 |
warn($@); |
| 188 |
exit 1; |
| 189 |
} |
| 190 |
|
| 191 |
exit 0; |
| 192 |
} |
| 193 |
|
| 194 |
# Stop server |
| 195 |
server_stop($pid_file); |
| 196 |
|
| 197 |
$self->assert_child_ok($pid); |
| 198 |
|
| 199 |
if ($ex) { |
| 200 |
die($ex); |
| 201 |
} |
| 202 |
|
| 203 |
unlink($log_file); |
| 204 |
} |
| 205 |
|
| 206 |
sub transferrate_stor_ok { |
| 207 |
my $self = shift; |
| 208 |
my $tmpdir = $self->{tmpdir}; |
| 209 |
|
| 210 |
my $config_file = "$tmpdir/config.conf"; |
| 211 |
my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid"); |
| 212 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard"); |
| 213 |
|
| 214 |
my $log_file = File::Spec->rel2abs('tests.log'); |
| 215 |
|
| 216 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd"); |
| 217 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group"); |
| 218 |
|
| 219 |
my $user = 'proftpd'; |
| 220 |
my $passwd = 'test'; |
| 221 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 222 |
my $uid = 500; |
| 223 |
my $gid = 500; |
| 224 |
|
| 225 |
# Make sure that, if we're running as root, that the home directory has |
| 226 |
# permissions/privs set for the account we create |
| 227 |
if ($< == 0) { |
| 228 |
unless (chmod(0755, $home_dir)) { |
| 229 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 230 |
} |
| 231 |
|
| 232 |
unless (chown($uid, $gid, $home_dir)) { |
| 233 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 238 |
'/bin/bash'); |
| 239 |
auth_group_write($auth_group_file, 'ftpd', $gid, $user); |
| 240 |
|
| 241 |
my $test_file = File::Spec->rel2abs("$tmpdir/test.txt"); |
| 242 |
|
| 243 |
my $timeout_idle = 20; |
| 244 |
|
| 245 |
my $config = { |
| 246 |
PidFile => $pid_file, |
| 247 |
ScoreboardFile => $scoreboard_file, |
| 248 |
SystemLog => $log_file, |
| 249 |
TraceLog => $log_file, |
| 250 |
Trace => 'scoreboard:20', |
| 251 |
|
| 252 |
AuthUserFile => $auth_user_file, |
| 253 |
AuthGroupFile => $auth_group_file, |
| 254 |
AuthOrder => 'mod_auth_file.c', |
| 255 |
|
| 256 |
TimeoutIdle => $timeout_idle, |
| 257 |
|
| 258 |
# 1 KB/sec |
| 259 |
TransferRate => 'STOR 1', |
| 260 |
|
| 261 |
IfModules => { |
| 262 |
'mod_delay.c' => { |
| 263 |
DelayEngine => 'off', |
| 264 |
}, |
| 265 |
}, |
| 266 |
}; |
| 267 |
|
| 268 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 269 |
|
| 270 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 271 |
# the child will indicate when it's done with its test by writing a message |
| 272 |
# to the parent. |
| 273 |
my ($rfh, $wfh); |
| 274 |
unless (pipe($rfh, $wfh)) { |
| 275 |
die("Can't open pipe: $!"); |
| 276 |
} |
| 277 |
|
| 278 |
my $ex; |
| 279 |
|
| 280 |
# Fork child |
| 281 |
$self->handle_sigchld(); |
| 282 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 283 |
if ($pid) { |
| 284 |
eval { |
| 285 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 286 |
$client->login($user, $passwd); |
| 287 |
$client->type('binary'); |
| 288 |
|
| 289 |
my $conn = $client->stor_raw('test.txt'); |
| 290 |
unless ($conn) { |
| 291 |
die("STOR failed: " . $client->response_code() . " " . |
| 292 |
$client->response_msg()); |
| 293 |
} |
| 294 |
|
| 295 |
my $buf = ('ABCDefgh' x 1024) . "\n"; |
| 296 |
my $buflen = length($buf); |
| 297 |
|
| 298 |
my $xfer_start = [gettimeofday()]; |
| 299 |
|
| 300 |
$conn->write($buf, $buflen, 30); |
| 301 |
$conn->close(); |
| 302 |
|
| 303 |
my $xfer_elapsed = tv_interval($xfer_start); |
| 304 |
|
| 305 |
my $resp_code = $client->response_code(); |
| 306 |
my $resp_msg = $client->response_msg(); |
| 307 |
|
| 308 |
$client->quit(); |
| 309 |
|
| 310 |
my $expected; |
| 311 |
|
| 312 |
$expected = 226; |
| 313 |
$self->assert($expected == $resp_code, |
| 314 |
test_msg("Expected $expected, got $resp_code")); |
| 315 |
|
| 316 |
$expected = 'Transfer complete'; |
| 317 |
$self->assert($expected eq $resp_msg, |
| 318 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 319 |
|
| 320 |
$expected = 8193; |
| 321 |
my $filesz = (stat($test_file))[7]; |
| 322 |
$self->assert($expected == $filesz, |
| 323 |
test_msg("Expected $expected, got $filesz")); |
| 324 |
|
| 325 |
# We configured a TransferRate of 1 KB/sec, and stored 8 KB; |
| 326 |
# thus make sure that the transfer time is more than 8 secs. |
| 327 |
$self->assert($xfer_elapsed > 8, |
| 328 |
test_msg("Expected > 8 secs, got $xfer_elapsed")); |
| 329 |
}; |
| 330 |
|
| 331 |
if ($@) { |
| 332 |
$ex = $@; |
| 333 |
} |
| 334 |
|
| 335 |
$wfh->print("done\n"); |
| 336 |
$wfh->flush(); |
| 337 |
|
| 338 |
} else { |
| 339 |
eval { server_wait($config_file, $rfh, $timeout_idle + 3) }; |
| 340 |
if ($@) { |
| 341 |
warn($@); |
| 342 |
exit 1; |
| 343 |
} |
| 344 |
|
| 345 |
exit 0; |
| 346 |
} |
| 347 |
|
| 348 |
# Stop server |
| 349 |
server_stop($pid_file); |
| 350 |
|
| 351 |
$self->assert_child_ok($pid); |
| 352 |
|
| 353 |
if ($ex) { |
| 354 |
die($ex); |
| 355 |
} |
| 356 |
|
| 357 |
unlink($log_file); |
| 358 |
} |
| 359 |
|
| 360 |
1; |