| 1 |
package ProFTPD::Tests::Modules::mod_statcache::sftp; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use Cwd; |
| 8 |
use File::Path qw(mkpath); |
| 9 |
use File::Spec; |
| 10 |
use IO::Handle; |
| 11 |
use POSIX qw(:fcntl_h); |
| 12 |
|
| 13 |
use ProFTPD::TestSuite::FTP; |
| 14 |
use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite); |
| 15 |
|
| 16 |
$| = 1; |
| 17 |
|
| 18 |
my $order = 0; |
| 19 |
|
| 20 |
my $TESTS = { |
| 21 |
statcache_sftp_stat_file => { |
| 22 |
order => ++$order, |
| 23 |
test_class => [qw(forking sftp)], |
| 24 |
}, |
| 25 |
|
| 26 |
statcache_sftp_stat_dir => { |
| 27 |
order => ++$order, |
| 28 |
test_class => [qw(forking sftp)], |
| 29 |
}, |
| 30 |
|
| 31 |
statcache_sftp_upload_file => { |
| 32 |
order => ++$order, |
| 33 |
test_class => [qw(forking sftp)], |
| 34 |
}, |
| 35 |
|
| 36 |
}; |
| 37 |
|
| 38 |
sub new { |
| 39 |
return shift()->SUPER::new(@_); |
| 40 |
} |
| 41 |
|
| 42 |
sub list_tests { |
| 43 |
# Check for the required Perl modules: |
| 44 |
# |
| 45 |
# Net-SSH2 |
| 46 |
# Net-SSH2-SFTP |
| 47 |
|
| 48 |
my $required = [qw( |
| 49 |
Net::SSH2 |
| 50 |
Net::SSH2::SFTP |
| 51 |
)]; |
| 52 |
|
| 53 |
foreach my $req (@$required) { |
| 54 |
eval "use $req"; |
| 55 |
if ($@) { |
| 56 |
print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n"; |
| 57 |
|
| 58 |
if ($ENV{TEST_VERBOSE}) { |
| 59 |
print STDERR "Unable to load $req: $@\n"; |
| 60 |
} |
| 61 |
|
| 62 |
return qw(testsuite_empty_test); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return testsuite_get_runnable_tests($TESTS); |
| 67 |
} |
| 68 |
|
| 69 |
sub set_up { |
| 70 |
my $self = shift; |
| 71 |
$self->SUPER::set_up(@_); |
| 72 |
|
| 73 |
# Make sure that mod_sftp does not complain about permissions on the hostkey |
| 74 |
# files. |
| 75 |
|
| 76 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_statcache/ssh_host_rsa_key'); |
| 77 |
|
| 78 |
unless (chmod(0400, $rsa_host_key)) { |
| 79 |
die("Can't set perms on $rsa_host_key: $!"); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
sub statcache_sftp_stat_file { |
| 84 |
my $self = shift; |
| 85 |
my $tmpdir = $self->{tmpdir}; |
| 86 |
my $setup = test_setup($tmpdir, 'statcache'); |
| 87 |
|
| 88 |
my $test_file = File::Spec->rel2abs("$setup->{home_dir}/test.txt"); |
| 89 |
if (open(my $fh, "> $test_file")) { |
| 90 |
print $fh "Hello, World!\n"; |
| 91 |
unless (close($fh)) { |
| 92 |
die("Can't write $test_file: $!"); |
| 93 |
} |
| 94 |
|
| 95 |
} else { |
| 96 |
die("Can't open $test_file: $!"); |
| 97 |
} |
| 98 |
|
| 99 |
my $test_filemode = (stat($test_file))[2]; |
| 100 |
if ($ENV{TEST_VERBOSE}) { |
| 101 |
print STDERR "# $test_file mode: $test_filemode\n"; |
| 102 |
} |
| 103 |
|
| 104 |
my $test_filesize = (stat($test_file))[7]; |
| 105 |
|
| 106 |
my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab"); |
| 107 |
|
| 108 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_statcache/ssh_host_rsa_key'); |
| 109 |
|
| 110 |
my $config = { |
| 111 |
PidFile => $setup->{pid_file}, |
| 112 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 113 |
SystemLog => $setup->{log_file}, |
| 114 |
TraceLog => $setup->{log_file}, |
| 115 |
Trace => 'fsio:10 sftp:20 statcache:20', |
| 116 |
|
| 117 |
AuthUserFile => $setup->{auth_user_file}, |
| 118 |
AuthGroupFile => $setup->{auth_group_file}, |
| 119 |
AuthOrder => 'mod_auth_file.c', |
| 120 |
|
| 121 |
IfModules => { |
| 122 |
'mod_delay.c' => { |
| 123 |
DelayEngine => 'off', |
| 124 |
}, |
| 125 |
|
| 126 |
'mod_sftp.c' => { |
| 127 |
SFTPEngine => 'on', |
| 128 |
SFTPLog => $setup->{log_file}, |
| 129 |
SFTPHostKey => $rsa_host_key, |
| 130 |
}, |
| 131 |
|
| 132 |
'mod_statcache.c' => { |
| 133 |
StatCacheEngine => 'on', |
| 134 |
StatCacheTable => $statcache_tab, |
| 135 |
}, |
| 136 |
}, |
| 137 |
}; |
| 138 |
|
| 139 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 140 |
$config); |
| 141 |
|
| 142 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 143 |
# the child will indicate when it's done with its test by writing a message |
| 144 |
# to the parent. |
| 145 |
my ($rfh, $wfh); |
| 146 |
unless (pipe($rfh, $wfh)) { |
| 147 |
die("Can't open pipe: $!"); |
| 148 |
} |
| 149 |
|
| 150 |
require Net::SSH2; |
| 151 |
|
| 152 |
my $ex; |
| 153 |
|
| 154 |
# Fork child |
| 155 |
$self->handle_sigchld(); |
| 156 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 157 |
if ($pid) { |
| 158 |
eval { |
| 159 |
my $ssh2 = Net::SSH2->new(); |
| 160 |
|
| 161 |
sleep(1); |
| 162 |
|
| 163 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 164 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 165 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 166 |
} |
| 167 |
|
| 168 |
unless ($ssh2->auth_password($setup->{user}, $setup->{passwd})) { |
| 169 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 170 |
die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 171 |
} |
| 172 |
|
| 173 |
my $sftp = $ssh2->sftp(); |
| 174 |
unless ($sftp) { |
| 175 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 176 |
die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str"); |
| 177 |
} |
| 178 |
|
| 179 |
my $path = 'test.txt'; |
| 180 |
my $attrs = $sftp->stat($path, 1); |
| 181 |
unless ($attrs) { |
| 182 |
my ($err_code, $err_name) = $sftp->error(); |
| 183 |
die("STAT $path failed: [$err_name] ($err_code)"); |
| 184 |
} |
| 185 |
|
| 186 |
my $expected = $test_filesize; |
| 187 |
my $file_size = $attrs->{size}; |
| 188 |
$self->assert($expected == $file_size, |
| 189 |
test_msg("Expected file size '$expected', got '$file_size'")); |
| 190 |
|
| 191 |
$expected = $<; |
| 192 |
my $file_uid = $attrs->{uid}; |
| 193 |
$self->assert($expected == $file_uid, |
| 194 |
test_msg("Expected file UID '$expected', got '$file_uid'")); |
| 195 |
|
| 196 |
$expected = $(; |
| 197 |
my $file_gid = $attrs->{gid}; |
| 198 |
$self->assert($expected == $file_gid, |
| 199 |
test_msg("Expected file GID '$expected', got '$file_gid'")); |
| 200 |
|
| 201 |
$expected = $test_filemode; |
| 202 |
my $file_mode = $attrs->{mode}; |
| 203 |
$self->assert($expected == $file_mode, |
| 204 |
test_msg("Expected file mode '$expected', got '$file_mode'")); |
| 205 |
|
| 206 |
# Do the stat again; we'll check the logs to see if mod_statcache |
| 207 |
# did its job. |
| 208 |
$attrs = $sftp->stat($path, 1); |
| 209 |
unless ($attrs) { |
| 210 |
my ($err_code, $err_name) = $sftp->error(); |
| 211 |
die("STAT $path failed: [$err_name] ($err_code)"); |
| 212 |
} |
| 213 |
|
| 214 |
$expected = $test_filesize; |
| 215 |
$file_size = $attrs->{size}; |
| 216 |
$self->assert($expected == $file_size, |
| 217 |
test_msg("Expected file size '$expected', got '$file_size'")); |
| 218 |
|
| 219 |
$expected = $<; |
| 220 |
$file_uid = $attrs->{uid}; |
| 221 |
$self->assert($expected == $file_uid, |
| 222 |
test_msg("Expected file UID '$expected', got '$file_uid'")); |
| 223 |
|
| 224 |
$expected = $(; |
| 225 |
$file_gid = $attrs->{gid}; |
| 226 |
$self->assert($expected == $file_gid, |
| 227 |
test_msg("Expected file GID '$expected', got '$file_gid'")); |
| 228 |
|
| 229 |
$expected = $test_filemode; |
| 230 |
$file_mode = $attrs->{mode}; |
| 231 |
$self->assert($expected == $file_mode, |
| 232 |
test_msg("Expected file mode '$expected', got '$file_mode'")); |
| 233 |
|
| 234 |
$sftp = undef; |
| 235 |
$ssh2->disconnect(); |
| 236 |
|
| 237 |
# Now connect again, do another stat, and see if we're still using |
| 238 |
# the cached entry. |
| 239 |
$ssh2 = Net::SSH2->new(); |
| 240 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 241 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 242 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 243 |
} |
| 244 |
|
| 245 |
unless ($ssh2->auth_password($setup->{user}, $setup->{passwd})) { |
| 246 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 247 |
die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 248 |
} |
| 249 |
|
| 250 |
$sftp = $ssh2->sftp(); |
| 251 |
unless ($sftp) { |
| 252 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 253 |
die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str"); |
| 254 |
} |
| 255 |
|
| 256 |
$attrs = $sftp->stat($path, 1); |
| 257 |
unless ($attrs) { |
| 258 |
my ($err_code, $err_name) = $sftp->error(); |
| 259 |
die("STAT $path failed: [$err_name] ($err_code)"); |
| 260 |
} |
| 261 |
|
| 262 |
$expected = $test_filesize; |
| 263 |
$file_size = $attrs->{size}; |
| 264 |
$self->assert($expected == $file_size, |
| 265 |
test_msg("Expected file size '$expected', got '$file_size'")); |
| 266 |
|
| 267 |
$expected = $<; |
| 268 |
$file_uid = $attrs->{uid}; |
| 269 |
$self->assert($expected == $file_uid, |
| 270 |
test_msg("Expected file UID '$expected', got '$file_uid'")); |
| 271 |
|
| 272 |
$expected = $(; |
| 273 |
$file_gid = $attrs->{gid}; |
| 274 |
$self->assert($expected == $file_gid, |
| 275 |
test_msg("Expected file GID '$expected', got '$file_gid'")); |
| 276 |
|
| 277 |
$expected = $test_filemode; |
| 278 |
$file_mode = $attrs->{mode}; |
| 279 |
$self->assert($expected == $file_mode, |
| 280 |
test_msg("Expected file mode '$expected', got '$file_mode'")); |
| 281 |
|
| 282 |
$sftp = undef; |
| 283 |
$ssh2->disconnect(); |
| 284 |
}; |
| 285 |
if ($@) { |
| 286 |
$ex = $@; |
| 287 |
} |
| 288 |
|
| 289 |
$wfh->print("done\n"); |
| 290 |
$wfh->flush(); |
| 291 |
|
| 292 |
} else { |
| 293 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 294 |
if ($@) { |
| 295 |
warn($@); |
| 296 |
exit 1; |
| 297 |
} |
| 298 |
|
| 299 |
exit 0; |
| 300 |
} |
| 301 |
|
| 302 |
# Stop server |
| 303 |
server_stop($setup->{pid_file}); |
| 304 |
$self->assert_child_ok($pid); |
| 305 |
|
| 306 |
eval { |
| 307 |
if (open(my $fh, "< $setup->{log_file}")) { |
| 308 |
my $adding_entry = 0; |
| 309 |
my $cached_stat = 0; |
| 310 |
my $cached_lstat = 0; |
| 311 |
|
| 312 |
if ($^O eq 'darwin') { |
| 313 |
# MacOSX-specific hack |
| 314 |
$test_file = '/private' . $test_file; |
| 315 |
} |
| 316 |
|
| 317 |
while (my $line = <$fh>) { |
| 318 |
chomp($line); |
| 319 |
|
| 320 |
if ($ENV{TEST_VERBOSE}) { |
| 321 |
print STDERR "# line: $line\n"; |
| 322 |
} |
| 323 |
|
| 324 |
if ($line =~ /<statcache:9>/) { |
| 325 |
if ($line =~ /adding entry.*?type file/) { |
| 326 |
$adding_entry++; |
| 327 |
next; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
if ($line =~ /<statcache:11>/) { |
| 332 |
if ($cached_stat == 0 && |
| 333 |
$line =~ /using cached stat.*?path '$test_file'/) { |
| 334 |
$cached_stat++; |
| 335 |
next; |
| 336 |
} |
| 337 |
|
| 338 |
if ($cached_lstat == 0 && |
| 339 |
$line =~ /using cached lstat.*?path '$test_file'/) { |
| 340 |
$cached_lstat++; |
| 341 |
next; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
if ($adding_entry >= 2 && |
| 346 |
$cached_stat == 1 && |
| 347 |
$cached_lstat == 1) { |
| 348 |
last; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
close($fh); |
| 353 |
|
| 354 |
$self->assert($adding_entry >= 2 && |
| 355 |
$cached_stat == 1 && |
| 356 |
$cached_lstat == 1, |
| 357 |
test_msg("Did not see expected 'statcache' TraceLog messages")); |
| 358 |
|
| 359 |
} else { |
| 360 |
die("Can't read $setup->{log_file}: $!"); |
| 361 |
} |
| 362 |
}; |
| 363 |
if ($@) { |
| 364 |
$ex = $@; |
| 365 |
} |
| 366 |
|
| 367 |
test_cleanup($setup->{log_file}, $ex); |
| 368 |
} |
| 369 |
|
| 370 |
sub statcache_sftp_stat_dir { |
| 371 |
my $self = shift; |
| 372 |
my $tmpdir = $self->{tmpdir}; |
| 373 |
my $setup = test_setup($tmpdir, 'statcache'); |
| 374 |
|
| 375 |
my $test_dir = File::Spec->rel2abs("$setup->{home_dir}/test.d"); |
| 376 |
mkpath($test_dir); |
| 377 |
|
| 378 |
my $test_filemode = (stat($test_dir))[2]; |
| 379 |
if ($ENV{TEST_VERBOSE}) { |
| 380 |
print STDERR "# $test_dir mode: $test_filemode\n"; |
| 381 |
} |
| 382 |
|
| 383 |
my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab"); |
| 384 |
|
| 385 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_statcache/ssh_host_rsa_key'); |
| 386 |
|
| 387 |
my $config = { |
| 388 |
PidFile => $setup->{pid_file}, |
| 389 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 390 |
SystemLog => $setup->{log_file}, |
| 391 |
TraceLog => $setup->{log_file}, |
| 392 |
Trace => 'fsio:10 sftp:20 statcache:20', |
| 393 |
|
| 394 |
AuthUserFile => $setup->{auth_user_file}, |
| 395 |
AuthGroupFile => $setup->{auth_group_file}, |
| 396 |
AuthOrder => 'mod_auth_file.c', |
| 397 |
|
| 398 |
IfModules => { |
| 399 |
'mod_delay.c' => { |
| 400 |
DelayEngine => 'off', |
| 401 |
}, |
| 402 |
|
| 403 |
'mod_sftp.c' => { |
| 404 |
SFTPEngine => 'on', |
| 405 |
SFTPLog => $setup->{log_file}, |
| 406 |
SFTPHostKey => $rsa_host_key, |
| 407 |
}, |
| 408 |
|
| 409 |
'mod_statcache.c' => { |
| 410 |
StatCacheEngine => 'on', |
| 411 |
StatCacheTable => $statcache_tab, |
| 412 |
}, |
| 413 |
}, |
| 414 |
}; |
| 415 |
|
| 416 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 417 |
$config); |
| 418 |
|
| 419 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 420 |
# the child will indicate when it's done with its test by writing a message |
| 421 |
# to the parent. |
| 422 |
my ($rfh, $wfh); |
| 423 |
unless (pipe($rfh, $wfh)) { |
| 424 |
die("Can't open pipe: $!"); |
| 425 |
} |
| 426 |
|
| 427 |
require Net::SSH2; |
| 428 |
|
| 429 |
my $ex; |
| 430 |
|
| 431 |
# Fork child |
| 432 |
$self->handle_sigchld(); |
| 433 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 434 |
if ($pid) { |
| 435 |
eval { |
| 436 |
my $ssh2 = Net::SSH2->new(); |
| 437 |
|
| 438 |
sleep(1); |
| 439 |
|
| 440 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 441 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 442 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 443 |
} |
| 444 |
|
| 445 |
unless ($ssh2->auth_password($setup->{user}, $setup->{passwd})) { |
| 446 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 447 |
die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 448 |
} |
| 449 |
|
| 450 |
my $sftp = $ssh2->sftp(); |
| 451 |
unless ($sftp) { |
| 452 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 453 |
die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str"); |
| 454 |
} |
| 455 |
|
| 456 |
my $path = 'test.d'; |
| 457 |
my $attrs = $sftp->stat($path, 1); |
| 458 |
unless ($attrs) { |
| 459 |
my ($err_code, $err_name) = $sftp->error(); |
| 460 |
die("STAT $path failed: [$err_name] ($err_code)"); |
| 461 |
} |
| 462 |
|
| 463 |
my $expected = $<; |
| 464 |
my $file_uid = $attrs->{uid}; |
| 465 |
$self->assert($expected == $file_uid, |
| 466 |
test_msg("Expected file UID '$expected', got '$file_uid'")); |
| 467 |
|
| 468 |
$expected = $(; |
| 469 |
my $file_gid = $attrs->{gid}; |
| 470 |
$self->assert($expected == $file_gid, |
| 471 |
test_msg("Expected file GID '$expected', got '$file_gid'")); |
| 472 |
|
| 473 |
$expected = $test_filemode; |
| 474 |
my $file_mode = $attrs->{mode}; |
| 475 |
$self->assert($expected == $file_mode, |
| 476 |
test_msg("Expected file mode '$expected', got '$file_mode'")); |
| 477 |
|
| 478 |
# Do the stat again; we'll check the logs to see if mod_statcache |
| 479 |
# did its job. |
| 480 |
$attrs = $sftp->stat($path, 1); |
| 481 |
unless ($attrs) { |
| 482 |
my ($err_code, $err_name) = $sftp->error(); |
| 483 |
die("STAT $path failed: [$err_name] ($err_code)"); |
| 484 |
} |
| 485 |
|
| 486 |
$expected = $<; |
| 487 |
$file_uid = $attrs->{uid}; |
| 488 |
$self->assert($expected == $file_uid, |
| 489 |
test_msg("Expected file UID '$expected', got '$file_uid'")); |
| 490 |
|
| 491 |
$expected = $(; |
| 492 |
$file_gid = $attrs->{gid}; |
| 493 |
$self->assert($expected == $file_gid, |
| 494 |
test_msg("Expected file GID '$expected', got '$file_gid'")); |
| 495 |
|
| 496 |
$expected = $test_filemode; |
| 497 |
$file_mode = $attrs->{mode}; |
| 498 |
$self->assert($expected == $file_mode, |
| 499 |
test_msg("Expected file mode '$expected', got '$file_mode'")); |
| 500 |
|
| 501 |
$sftp = undef; |
| 502 |
$ssh2->disconnect(); |
| 503 |
|
| 504 |
# Now connect again, do another stat, and see if we're still using |
| 505 |
# the cached entry. |
| 506 |
$ssh2 = Net::SSH2->new(); |
| 507 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 508 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 509 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 510 |
} |
| 511 |
|
| 512 |
unless ($ssh2->auth_password($setup->{user}, $setup->{passwd})) { |
| 513 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 514 |
die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 515 |
} |
| 516 |
|
| 517 |
$sftp = $ssh2->sftp(); |
| 518 |
unless ($sftp) { |
| 519 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 520 |
die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str"); |
| 521 |
} |
| 522 |
|
| 523 |
$attrs = $sftp->stat($path, 1); |
| 524 |
unless ($attrs) { |
| 525 |
my ($err_code, $err_name) = $sftp->error(); |
| 526 |
die("STAT $path failed: [$err_name] ($err_code)"); |
| 527 |
} |
| 528 |
|
| 529 |
$expected = $<; |
| 530 |
$file_uid = $attrs->{uid}; |
| 531 |
$self->assert($expected == $file_uid, |
| 532 |
test_msg("Expected file UID '$expected', got '$file_uid'")); |
| 533 |
|
| 534 |
$expected = $(; |
| 535 |
$file_gid = $attrs->{gid}; |
| 536 |
$self->assert($expected == $file_gid, |
| 537 |
test_msg("Expected file GID '$expected', got '$file_gid'")); |
| 538 |
|
| 539 |
$expected = $test_filemode; |
| 540 |
$file_mode = $attrs->{mode}; |
| 541 |
$self->assert($expected == $file_mode, |
| 542 |
test_msg("Expected file mode '$expected', got '$file_mode'")); |
| 543 |
|
| 544 |
$sftp = undef; |
| 545 |
$ssh2->disconnect(); |
| 546 |
}; |
| 547 |
if ($@) { |
| 548 |
$ex = $@; |
| 549 |
} |
| 550 |
|
| 551 |
$wfh->print("done\n"); |
| 552 |
$wfh->flush(); |
| 553 |
|
| 554 |
} else { |
| 555 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 556 |
if ($@) { |
| 557 |
warn($@); |
| 558 |
exit 1; |
| 559 |
} |
| 560 |
|
| 561 |
exit 0; |
| 562 |
} |
| 563 |
|
| 564 |
# Stop server |
| 565 |
server_stop($setup->{pid_file}); |
| 566 |
$self->assert_child_ok($pid); |
| 567 |
|
| 568 |
eval { |
| 569 |
if (open(my $fh, "< $setup->{log_file}")) { |
| 570 |
my $adding_entry = 0; |
| 571 |
my $cached_stat = 0; |
| 572 |
my $cached_lstat = 0; |
| 573 |
|
| 574 |
if ($^O eq 'darwin') { |
| 575 |
# MacOSX-specific hack |
| 576 |
$test_dir = '/private' . $test_dir; |
| 577 |
} |
| 578 |
|
| 579 |
while (my $line = <$fh>) { |
| 580 |
chomp($line); |
| 581 |
|
| 582 |
if ($ENV{TEST_VERBOSE}) { |
| 583 |
print STDERR "# line: $line\n"; |
| 584 |
} |
| 585 |
|
| 586 |
if ($line =~ /<statcache:9>/) { |
| 587 |
if ($line =~ /adding entry.*?type dir/) { |
| 588 |
$adding_entry++; |
| 589 |
next; |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
if ($line =~ /<statcache:11>/) { |
| 594 |
if ($cached_stat == 0 && |
| 595 |
$line =~ /using cached stat.*?path '$test_dir'/) { |
| 596 |
$cached_stat++; |
| 597 |
next; |
| 598 |
} |
| 599 |
|
| 600 |
if ($cached_lstat == 0 && |
| 601 |
$line =~ /using cached lstat.*?path '$test_dir'/) { |
| 602 |
$cached_lstat++; |
| 603 |
next; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
if ($adding_entry >= 2 && |
| 608 |
$cached_stat == 1 && |
| 609 |
$cached_lstat == 1) { |
| 610 |
last; |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
close($fh); |
| 615 |
|
| 616 |
$self->assert($adding_entry >= 2 && |
| 617 |
$cached_stat == 1 && |
| 618 |
$cached_lstat == 1, |
| 619 |
test_msg("Did not see expected 'statcache' TraceLog messages")); |
| 620 |
|
| 621 |
} else { |
| 622 |
die("Can't read $setup->{log_file}: $!"); |
| 623 |
} |
| 624 |
}; |
| 625 |
if ($@) { |
| 626 |
$ex = $@; |
| 627 |
} |
| 628 |
|
| 629 |
test_cleanup($setup->{log_file}, $ex); |
| 630 |
} |
| 631 |
|
| 632 |
sub statcache_sftp_upload_file { |
| 633 |
my $self = shift; |
| 634 |
my $tmpdir = $self->{tmpdir}; |
| 635 |
my $setup = test_setup($tmpdir, 'statcache'); |
| 636 |
|
| 637 |
my $test_file = File::Spec->rel2abs("$setup->{home_dir}/test.txt"); |
| 638 |
my $test_filemode = 33188; |
| 639 |
my $test_filesize = 14; |
| 640 |
|
| 641 |
my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab"); |
| 642 |
|
| 643 |
my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_statcache/ssh_host_rsa_key'); |
| 644 |
|
| 645 |
my $config = { |
| 646 |
PidFile => $setup->{pid_file}, |
| 647 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 648 |
SystemLog => $setup->{log_file}, |
| 649 |
TraceLog => $setup->{log_file}, |
| 650 |
Trace => 'fsio:10 sftp:20 statcache:20', |
| 651 |
|
| 652 |
AuthUserFile => $setup->{auth_user_file}, |
| 653 |
AuthGroupFile => $setup->{auth_group_file}, |
| 654 |
AuthOrder => 'mod_auth_file.c', |
| 655 |
|
| 656 |
IfModules => { |
| 657 |
'mod_delay.c' => { |
| 658 |
DelayEngine => 'off', |
| 659 |
}, |
| 660 |
|
| 661 |
'mod_sftp.c' => { |
| 662 |
SFTPEngine => 'on', |
| 663 |
SFTPLog => $setup->{log_file}, |
| 664 |
SFTPHostKey => $rsa_host_key, |
| 665 |
}, |
| 666 |
|
| 667 |
'mod_statcache.c' => { |
| 668 |
StatCacheEngine => 'on', |
| 669 |
StatCacheTable => $statcache_tab, |
| 670 |
}, |
| 671 |
}, |
| 672 |
}; |
| 673 |
|
| 674 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 675 |
$config); |
| 676 |
|
| 677 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 678 |
# the child will indicate when it's done with its test by writing a message |
| 679 |
# to the parent. |
| 680 |
my ($rfh, $wfh); |
| 681 |
unless (pipe($rfh, $wfh)) { |
| 682 |
die("Can't open pipe: $!"); |
| 683 |
} |
| 684 |
|
| 685 |
require Net::SSH2; |
| 686 |
|
| 687 |
my $ex; |
| 688 |
|
| 689 |
# Fork child |
| 690 |
$self->handle_sigchld(); |
| 691 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 692 |
if ($pid) { |
| 693 |
eval { |
| 694 |
my $ssh2 = Net::SSH2->new(); |
| 695 |
|
| 696 |
sleep(1); |
| 697 |
|
| 698 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 699 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 700 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 701 |
} |
| 702 |
|
| 703 |
unless ($ssh2->auth_password($setup->{user}, $setup->{passwd})) { |
| 704 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 705 |
die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 706 |
} |
| 707 |
|
| 708 |
my $sftp = $ssh2->sftp(); |
| 709 |
unless ($sftp) { |
| 710 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 711 |
die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str"); |
| 712 |
} |
| 713 |
|
| 714 |
my $path = 'test.txt'; |
| 715 |
my $fh = $sftp->open($path, O_WRONLY|O_CREAT|O_TRUNC, 0644); |
| 716 |
unless ($fh) { |
| 717 |
my ($err_code, $err_name) = $sftp->error(); |
| 718 |
die("Can't open test.txt: [$err_name] ($err_code)"); |
| 719 |
} |
| 720 |
|
| 721 |
print $fh "Hello, World!\n"; |
| 722 |
|
| 723 |
# To issue the FXP_CLOSE, we have to explicitly destroy the filehandle |
| 724 |
$fh = undef; |
| 725 |
|
| 726 |
my $attrs = $sftp->stat($path, 1); |
| 727 |
unless ($attrs) { |
| 728 |
my ($err_code, $err_name) = $sftp->error(); |
| 729 |
die("STAT $path failed: [$err_name] ($err_code)"); |
| 730 |
} |
| 731 |
|
| 732 |
my $expected = $test_filesize; |
| 733 |
my $file_size = $attrs->{size}; |
| 734 |
$self->assert($expected == $file_size, |
| 735 |
test_msg("Expected file size '$expected', got '$file_size'")); |
| 736 |
|
| 737 |
$expected = $<; |
| 738 |
my $file_uid = $attrs->{uid}; |
| 739 |
$self->assert($expected == $file_uid, |
| 740 |
test_msg("Expected file UID '$expected', got '$file_uid'")); |
| 741 |
|
| 742 |
$expected = $(; |
| 743 |
my $file_gid = $attrs->{gid}; |
| 744 |
$self->assert($expected == $file_gid, |
| 745 |
test_msg("Expected file GID '$expected', got '$file_gid'")); |
| 746 |
|
| 747 |
$expected = $test_filemode; |
| 748 |
my $file_mode = $attrs->{mode}; |
| 749 |
$self->assert($expected == $file_mode, |
| 750 |
test_msg("Expected file mode '$expected', got '$file_mode'")); |
| 751 |
|
| 752 |
# Do the stat again; we'll check the logs to see if mod_statcache |
| 753 |
# did its job. |
| 754 |
$attrs = $sftp->stat($path, 1); |
| 755 |
unless ($attrs) { |
| 756 |
my ($err_code, $err_name) = $sftp->error(); |
| 757 |
die("STAT $path failed: [$err_name] ($err_code)"); |
| 758 |
} |
| 759 |
|
| 760 |
$expected = $test_filesize; |
| 761 |
$file_size = $attrs->{size}; |
| 762 |
$self->assert($expected == $file_size, |
| 763 |
test_msg("Expected file size '$expected', got '$file_size'")); |
| 764 |
|
| 765 |
$expected = $<; |
| 766 |
$file_uid = $attrs->{uid}; |
| 767 |
$self->assert($expected == $file_uid, |
| 768 |
test_msg("Expected file UID '$expected', got '$file_uid'")); |
| 769 |
|
| 770 |
$expected = $(; |
| 771 |
$file_gid = $attrs->{gid}; |
| 772 |
$self->assert($expected == $file_gid, |
| 773 |
test_msg("Expected file GID '$expected', got '$file_gid'")); |
| 774 |
|
| 775 |
$expected = $test_filemode; |
| 776 |
$file_mode = $attrs->{mode}; |
| 777 |
$self->assert($expected == $file_mode, |
| 778 |
test_msg("Expected file mode '$expected', got '$file_mode'")); |
| 779 |
|
| 780 |
$sftp = undef; |
| 781 |
$ssh2->disconnect(); |
| 782 |
|
| 783 |
# Now connect again, do another stat, and see if we're still using |
| 784 |
# the cached entry. |
| 785 |
$ssh2 = Net::SSH2->new(); |
| 786 |
unless ($ssh2->connect('127.0.0.1', $port)) { |
| 787 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 788 |
die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 789 |
} |
| 790 |
|
| 791 |
unless ($ssh2->auth_password($setup->{user}, $setup->{passwd})) { |
| 792 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 793 |
die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str"); |
| 794 |
} |
| 795 |
|
| 796 |
$sftp = $ssh2->sftp(); |
| 797 |
unless ($sftp) { |
| 798 |
my ($err_code, $err_name, $err_str) = $ssh2->error(); |
| 799 |
die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str"); |
| 800 |
} |
| 801 |
|
| 802 |
$attrs = $sftp->stat($path, 1); |
| 803 |
unless ($attrs) { |
| 804 |
my ($err_code, $err_name) = $sftp->error(); |
| 805 |
die("STAT $path failed: [$err_name] ($err_code)"); |
| 806 |
} |
| 807 |
|
| 808 |
$expected = $test_filesize; |
| 809 |
$file_size = $attrs->{size}; |
| 810 |
$self->assert($expected == $file_size, |
| 811 |
test_msg("Expected file size '$expected', got '$file_size'")); |
| 812 |
|
| 813 |
$expected = $<; |
| 814 |
$file_uid = $attrs->{uid}; |
| 815 |
$self->assert($expected == $file_uid, |
| 816 |
test_msg("Expected file UID '$expected', got '$file_uid'")); |
| 817 |
|
| 818 |
$expected = $(; |
| 819 |
$file_gid = $attrs->{gid}; |
| 820 |
$self->assert($expected == $file_gid, |
| 821 |
test_msg("Expected file GID '$expected', got '$file_gid'")); |
| 822 |
|
| 823 |
$expected = $test_filemode; |
| 824 |
$file_mode = $attrs->{mode}; |
| 825 |
$self->assert($expected == $file_mode, |
| 826 |
test_msg("Expected file mode '$expected', got '$file_mode'")); |
| 827 |
|
| 828 |
$sftp = undef; |
| 829 |
$ssh2->disconnect(); |
| 830 |
}; |
| 831 |
if ($@) { |
| 832 |
$ex = $@; |
| 833 |
} |
| 834 |
|
| 835 |
$wfh->print("done\n"); |
| 836 |
$wfh->flush(); |
| 837 |
|
| 838 |
} else { |
| 839 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 840 |
if ($@) { |
| 841 |
warn($@); |
| 842 |
exit 1; |
| 843 |
} |
| 844 |
|
| 845 |
exit 0; |
| 846 |
} |
| 847 |
|
| 848 |
# Stop server |
| 849 |
server_stop($setup->{pid_file}); |
| 850 |
$self->assert_child_ok($pid); |
| 851 |
|
| 852 |
eval { |
| 853 |
if (open(my $fh, "< $setup->{log_file}")) { |
| 854 |
my $adding_entry = 0; |
| 855 |
my $cached_stat = 0; |
| 856 |
my $cached_lstat = 0; |
| 857 |
|
| 858 |
if ($^O eq 'darwin') { |
| 859 |
# MacOSX-specific hack |
| 860 |
$test_file = '/private' . $test_file; |
| 861 |
} |
| 862 |
|
| 863 |
while (my $line = <$fh>) { |
| 864 |
chomp($line); |
| 865 |
|
| 866 |
if ($ENV{TEST_VERBOSE}) { |
| 867 |
print STDERR "# line: $line\n"; |
| 868 |
} |
| 869 |
|
| 870 |
if ($line =~ /<statcache:9>/) { |
| 871 |
if ($line =~ /adding entry.*?type file/) { |
| 872 |
$adding_entry++; |
| 873 |
next; |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
if ($line =~ /<statcache:11>/) { |
| 878 |
if ($cached_stat == 0 && |
| 879 |
$line =~ /using cached stat.*?path '$test_file'/) { |
| 880 |
$cached_stat++; |
| 881 |
next; |
| 882 |
} |
| 883 |
|
| 884 |
if ($cached_lstat == 0 && |
| 885 |
$line =~ /using cached lstat.*?path '$test_file'/) { |
| 886 |
$cached_lstat++; |
| 887 |
next; |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
if ($adding_entry >= 2 && |
| 892 |
$cached_stat == 1 && |
| 893 |
$cached_lstat == 1) { |
| 894 |
last; |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
close($fh); |
| 899 |
|
| 900 |
$self->assert($adding_entry >= 2 && |
| 901 |
$cached_stat == 1 && |
| 902 |
$cached_lstat == 1, |
| 903 |
test_msg("Did not see expected 'statcache' TraceLog messages")); |
| 904 |
|
| 905 |
} else { |
| 906 |
die("Can't read $setup->{log_file}: $!"); |
| 907 |
} |
| 908 |
}; |
| 909 |
if ($@) { |
| 910 |
$ex = $@; |
| 911 |
} |
| 912 |
|
| 913 |
test_cleanup($setup->{log_file}, $ex); |
| 914 |
} |
| 915 |
|
| 916 |
1; |