| 1 |
package ProFTPD::Tests::Config::Directory; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(Test::Unit::TestCase ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use Data::Dumper; |
| 8 |
use File::Path qw(mkpath rmtree); |
| 9 |
use File::Spec; |
| 10 |
use IO::Handle; |
| 11 |
use Time::HiRes qw(gettimeofday tv_interval); |
| 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 |
dir_wide_layout => { |
| 22 |
order => ++$order, |
| 23 |
test_class => [qw(bug forking)], |
| 24 |
}, |
| 25 |
|
| 26 |
# dir_deep_layout |
| 27 |
# dir_wide_deep_layout |
| 28 |
# |
| 29 |
# I suspect that, due to the nature of the parser, the order in which |
| 30 |
# the <Directory> sections appear in the config can affect performance. |
| 31 |
# If the _last_ <Directory> section is for the most common path referenced, |
| 32 |
# it could mean longer traversal times (for each lookup) before the |
| 33 |
# match is made. |
| 34 |
}; |
| 35 |
|
| 36 |
sub new { |
| 37 |
return shift()->SUPER::new(@_); |
| 38 |
} |
| 39 |
|
| 40 |
sub list_tests { |
| 41 |
return testsuite_get_runnable_tests($TESTS); |
| 42 |
} |
| 43 |
|
| 44 |
sub set_up { |
| 45 |
my $self = shift; |
| 46 |
$self->{tmpdir} = testsuite_get_tmp_dir(); |
| 47 |
|
| 48 |
# Create temporary scratch dir |
| 49 |
eval { mkpath($self->{tmpdir}) }; |
| 50 |
if ($@) { |
| 51 |
my $abs_path = File::Spec->rel2abs($self->{tmpdir}); |
| 52 |
die("Can't create dir $abs_path: $@"); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
sub tear_down { |
| 57 |
my $self = shift; |
| 58 |
|
| 59 |
# Remove temporary scratch dir |
| 60 |
if ($self->{tmpdir}) { |
| 61 |
eval { rmtree($self->{tmpdir}) }; |
| 62 |
} |
| 63 |
|
| 64 |
undef $self; |
| 65 |
} |
| 66 |
|
| 67 |
my ($prev_name, $prev_namelen); |
| 68 |
|
| 69 |
sub get_name { |
| 70 |
my $name_len = shift; |
| 71 |
my $inc = shift; |
| 72 |
|
| 73 |
# If the requested name length has changed, start over |
| 74 |
if ($name_len != $prev_namelen) { |
| 75 |
$prev_name = undef; |
| 76 |
} |
| 77 |
|
| 78 |
if (defined($prev_name)) { |
| 79 |
# Split the name into its individual chars, in reverse order |
| 80 |
my @chars = reverse(split('', $prev_name)); |
| 81 |
|
| 82 |
# Increment the first char, then reassemble the name. We only want |
| 83 |
# ASCII characters (i.e. A-Za-z inclusive). So if the incremented first |
| 84 |
# char is outside the range, reset the first char to the range start, and |
| 85 |
# increment the next char. |
| 86 |
|
| 87 |
for (my $i = 0; $i < $prev_namelen; $i++) { |
| 88 |
my $char = $chars[$i]; |
| 89 |
my $val = ord($char); |
| 90 |
|
| 91 |
$char = chr(++$val); |
| 92 |
|
| 93 |
my $reset_char = 0; |
| 94 |
while ($char !~ /[A-Za-z]/o) { |
| 95 |
++$val; |
| 96 |
if ($val > 122) { |
| 97 |
# Too far; reset to 'A'. |
| 98 |
$val = 65; |
| 99 |
$reset_char = 1; |
| 100 |
} |
| 101 |
|
| 102 |
$char = chr($val); |
| 103 |
} |
| 104 |
|
| 105 |
$chars[$i] = $char; |
| 106 |
|
| 107 |
unless ($reset_char) { |
| 108 |
last; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
$prev_name = join('', reverse(@chars)); |
| 113 |
|
| 114 |
} else { |
| 115 |
$prev_name = "A" x $name_len; |
| 116 |
$prev_namelen = $name_len; |
| 117 |
} |
| 118 |
|
| 119 |
return $prev_name; |
| 120 |
} |
| 121 |
|
| 122 |
sub dir_wide_layout { |
| 123 |
my $self = shift; |
| 124 |
my $tmpdir = $self->{tmpdir}; |
| 125 |
|
| 126 |
my $config_file = "$tmpdir/dir.conf"; |
| 127 |
my $pid_file = File::Spec->rel2abs("$tmpdir/dir.pid"); |
| 128 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/dir.scoreboard"); |
| 129 |
|
| 130 |
my $log_file = File::Spec->rel2abs('tests.log'); |
| 131 |
|
| 132 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/dir.passwd"); |
| 133 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/dir.group"); |
| 134 |
|
| 135 |
my $user = 'proftpd'; |
| 136 |
my $passwd = 'test'; |
| 137 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 138 |
my $uid = 500; |
| 139 |
my $gid = 500; |
| 140 |
|
| 141 |
# Make sure that, if we're running as root, that the home directory has |
| 142 |
# permissions/privs set for the account we create |
| 143 |
if ($< == 0) { |
| 144 |
unless (chmod(0755, $home_dir)) { |
| 145 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 146 |
} |
| 147 |
|
| 148 |
unless (chown($uid, $gid, $home_dir)) { |
| 149 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 154 |
'/bin/bash'); |
| 155 |
auth_group_write($auth_group_file, 'ftpd', $gid, $user); |
| 156 |
|
| 157 |
my $config = { |
| 158 |
PidFile => $pid_file, |
| 159 |
ScoreboardFile => $scoreboard_file, |
| 160 |
SystemLog => $log_file, |
| 161 |
TraceLog => $log_file, |
| 162 |
Trace => 'directory:10', |
| 163 |
|
| 164 |
AuthUserFile => $auth_user_file, |
| 165 |
AuthGroupFile => $auth_group_file, |
| 166 |
AuthOrder => 'mod_auth_file.c', |
| 167 |
|
| 168 |
DefaultChdir => '~', |
| 169 |
|
| 170 |
Directory => { |
| 171 |
'/' => { |
| 172 |
Umask => '066 077', |
| 173 |
}, |
| 174 |
}, |
| 175 |
|
| 176 |
IfModules => { |
| 177 |
'mod_delay.c' => { |
| 178 |
DelayEngine => 'off', |
| 179 |
}, |
| 180 |
}, |
| 181 |
}; |
| 182 |
|
| 183 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 184 |
|
| 185 |
# Append our mess of many wide <Directory> sections to the config: |
| 186 |
# |
| 187 |
# <Directory /path/to/a> |
| 188 |
# Umask 066 077 |
| 189 |
# </Directory> |
| 190 |
# |
| 191 |
# <Directory/path/to/b> |
| 192 |
# Umask 066 077 |
| 193 |
# </Directory> |
| 194 |
|
| 195 |
my $target_dir; |
| 196 |
|
| 197 |
if (open(my $fh, ">> $config_file")) { |
| 198 |
my $width = 1000; |
| 199 |
my $namelen = 3; |
| 200 |
|
| 201 |
for (my $i = 0; $i < $width; $i++) { |
| 202 |
$target_dir = get_name($namelen, 1); |
| 203 |
my $dir = File::Spec->rel2abs("$tmpdir/$target_dir"); |
| 204 |
mkpath($dir); |
| 205 |
|
| 206 |
print $fh <<EOD; |
| 207 |
<Directory ~/$target_dir> |
| 208 |
Umask 066 077 |
| 209 |
</Directory> |
| 210 |
EOD |
| 211 |
} |
| 212 |
|
| 213 |
unless (close($fh)) { |
| 214 |
die("Can't write $config_file: $!"); |
| 215 |
} |
| 216 |
|
| 217 |
} else { |
| 218 |
die("Can't open $config_file: $!"); |
| 219 |
} |
| 220 |
|
| 221 |
# To test the worst-case scenario, the target directory (to which we will |
| 222 |
# write a file) should be the _last_ in the list. |
| 223 |
|
| 224 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 225 |
# the child will indicate when it's done with its test by writing a message |
| 226 |
# to the parent. |
| 227 |
my ($rfh, $wfh); |
| 228 |
unless (pipe($rfh, $wfh)) { |
| 229 |
die("Can't open pipe: $!"); |
| 230 |
} |
| 231 |
|
| 232 |
my $ex; |
| 233 |
|
| 234 |
# Fork child |
| 235 |
$self->handle_sigchld(); |
| 236 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 237 |
if ($pid) { |
| 238 |
eval { |
| 239 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 240 |
$client->login($user, $passwd); |
| 241 |
|
| 242 |
my ($resp_code, $resp_msg); |
| 243 |
|
| 244 |
my $start_time = [gettimeofday()]; |
| 245 |
|
| 246 |
my $conn = $client->stor_raw("$target_dir/test.txt"); |
| 247 |
unless ($conn) { |
| 248 |
die("Failed to STOR $target_dir/test.txt: " . $client->response_code() . |
| 249 |
" " . $client->response_msg()); |
| 250 |
} |
| 251 |
|
| 252 |
my $elapsed = tv_interval($start_time); |
| 253 |
print STDERR "Elapsed: ", Dumper($elapsed); |
| 254 |
|
| 255 |
$conn->close(); |
| 256 |
|
| 257 |
$resp_code = $client->response_code(); |
| 258 |
$resp_msg = $client->response_msg(); |
| 259 |
|
| 260 |
my $expected; |
| 261 |
|
| 262 |
$expected = 226; |
| 263 |
$self->assert($expected == $resp_code, |
| 264 |
test_msg("Expected '$expected', got '$resp_code'")); |
| 265 |
|
| 266 |
$expected = "Transfer complete"; |
| 267 |
$self->assert($expected eq $resp_msg, |
| 268 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 269 |
}; |
| 270 |
|
| 271 |
if ($@) { |
| 272 |
$ex = $@; |
| 273 |
} |
| 274 |
|
| 275 |
$wfh->print("done\n"); |
| 276 |
$wfh->flush(); |
| 277 |
|
| 278 |
} else { |
| 279 |
eval { server_wait($config_file, $rfh) }; |
| 280 |
if ($@) { |
| 281 |
warn($@); |
| 282 |
exit 1; |
| 283 |
} |
| 284 |
|
| 285 |
exit 0; |
| 286 |
} |
| 287 |
|
| 288 |
# Stop server |
| 289 |
server_stop($pid_file); |
| 290 |
|
| 291 |
$self->assert_child_ok($pid); |
| 292 |
|
| 293 |
if ($ex) { |
| 294 |
die($ex); |
| 295 |
} |
| 296 |
|
| 297 |
unlink($log_file); |
| 298 |
} |
| 299 |
|
| 300 |
1; |