| 1 |
package ProFTPD::Tests::Config::AllowStoreRestart; |
| 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 |
allowstorerestart_default => { |
| 19 |
order => ++$order, |
| 20 |
test_class => [qw(forking)], |
| 21 |
}, |
| 22 |
|
| 23 |
allowstorerestart_on => { |
| 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 allowstorerestart_default { |
| 39 |
my $self = shift; |
| 40 |
my $tmpdir = $self->{tmpdir}; |
| 41 |
my $setup = test_setup($tmpdir, 'config'); |
| 42 |
|
| 43 |
my $test_file = File::Spec->rel2abs("$tmpdir/test.txt"); |
| 44 |
if (open(my $fh, "> $test_file")) { |
| 45 |
print $fh "AbCdEfGh" x 8192; |
| 46 |
|
| 47 |
unless (close($fh)) { |
| 48 |
die("Can't write $test_file: $!"); |
| 49 |
} |
| 50 |
|
| 51 |
# Make sure that, if we're running as root, that the test file has |
| 52 |
# permissions/privs set for the account we create |
| 53 |
if ($< == 0) { |
| 54 |
unless (chown($setup->{uid}, $setup->{gid}, $test_file)) { |
| 55 |
die("Can't set owner of $test_file to $setup->{uid}/$setup->{gid}: $!"); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
} else { |
| 60 |
die("Can't open $test_file: $!"); |
| 61 |
} |
| 62 |
|
| 63 |
my $config = { |
| 64 |
PidFile => $setup->{pid_file}, |
| 65 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 66 |
SystemLog => $setup->{log_file}, |
| 67 |
|
| 68 |
AuthUserFile => $setup->{auth_user_file}, |
| 69 |
AuthGroupFile => $setup->{auth_group_file}, |
| 70 |
AuthOrder => 'mod_auth_file.c', |
| 71 |
|
| 72 |
AllowOverwrite => 'on', |
| 73 |
|
| 74 |
IfModules => { |
| 75 |
'mod_delay.c' => { |
| 76 |
DelayEngine => 'off', |
| 77 |
}, |
| 78 |
}, |
| 79 |
}; |
| 80 |
|
| 81 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 82 |
$config); |
| 83 |
|
| 84 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 85 |
# the child will indicate when it's done with its test by writing a message |
| 86 |
# to the parent. |
| 87 |
my ($rfh, $wfh); |
| 88 |
unless (pipe($rfh, $wfh)) { |
| 89 |
die("Can't open pipe: $!"); |
| 90 |
} |
| 91 |
|
| 92 |
my $ex; |
| 93 |
|
| 94 |
# Fork child |
| 95 |
$self->handle_sigchld(); |
| 96 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 97 |
if ($pid) { |
| 98 |
eval { |
| 99 |
# Allow for server startup |
| 100 |
sleep(1); |
| 101 |
|
| 102 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 103 |
$client->login($setup->{user}, $setup->{passwd}); |
| 104 |
|
| 105 |
$client->type('I'); |
| 106 |
$client->rest(1); |
| 107 |
|
| 108 |
my $conn = $client->stor_raw('test.txt'); |
| 109 |
if ($conn) { |
| 110 |
die("STOR test.txt succeeded unexpectedly"); |
| 111 |
} |
| 112 |
|
| 113 |
my $resp_code = $client->response_code(); |
| 114 |
my $resp_msg = $client->response_msg(); |
| 115 |
|
| 116 |
my $expected = 451; |
| 117 |
$self->assert($expected == $resp_code, |
| 118 |
test_msg("Expected response code $expected, got $resp_code")); |
| 119 |
|
| 120 |
$expected = "test.txt: Append/Restart not permitted, try again"; |
| 121 |
$self->assert($expected eq $resp_msg, |
| 122 |
test_msg("Expected response message '$expected', got '$resp_msg'")); |
| 123 |
|
| 124 |
$client->quit(); |
| 125 |
}; |
| 126 |
if ($@) { |
| 127 |
$ex = $@; |
| 128 |
} |
| 129 |
|
| 130 |
$wfh->print("done\n"); |
| 131 |
$wfh->flush(); |
| 132 |
|
| 133 |
} else { |
| 134 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 135 |
if ($@) { |
| 136 |
warn($@); |
| 137 |
exit 1; |
| 138 |
} |
| 139 |
|
| 140 |
exit 0; |
| 141 |
} |
| 142 |
|
| 143 |
# Stop server |
| 144 |
server_stop($setup->{pid_file}); |
| 145 |
$self->assert_child_ok($pid); |
| 146 |
|
| 147 |
test_cleanup($setup->{log_file}, $ex); |
| 148 |
} |
| 149 |
|
| 150 |
sub allowstorerestart_on { |
| 151 |
my $self = shift; |
| 152 |
my $tmpdir = $self->{tmpdir}; |
| 153 |
my $setup = test_setup($tmpdir, 'config'); |
| 154 |
|
| 155 |
my $test_file = File::Spec->rel2abs("$tmpdir/test.txt"); |
| 156 |
if (open(my $fh, "> $test_file")) { |
| 157 |
print $fh "AbCdEfGh" x 8192; |
| 158 |
|
| 159 |
unless (close($fh)) { |
| 160 |
die("Can't write $test_file: $!"); |
| 161 |
} |
| 162 |
|
| 163 |
# Make sure that, if we're running as root, that the test file has |
| 164 |
# permissions/privs set for the account we create |
| 165 |
if ($< == 0) { |
| 166 |
unless (chown($setup->{uid}, $setup->{gid}, $test_file)) { |
| 167 |
die("Can't set owner of $test_file to $setup->{uid}/$setup->{gid}: $!"); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
} else { |
| 172 |
die("Can't open $test_file: $!"); |
| 173 |
} |
| 174 |
|
| 175 |
my $config = { |
| 176 |
PidFile => $setup->{pid_file}, |
| 177 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 178 |
SystemLog => $setup->{log_file}, |
| 179 |
|
| 180 |
AuthUserFile => $setup->{auth_user_file}, |
| 181 |
AuthGroupFile => $setup->{auth_group_file}, |
| 182 |
AuthOrder => 'mod_auth_file.c', |
| 183 |
|
| 184 |
AllowOverwrite => 'on', |
| 185 |
AllowStoreRestart => 'on', |
| 186 |
|
| 187 |
IfModules => { |
| 188 |
'mod_delay.c' => { |
| 189 |
DelayEngine => 'off', |
| 190 |
}, |
| 191 |
}, |
| 192 |
}; |
| 193 |
|
| 194 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 195 |
$config); |
| 196 |
|
| 197 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 198 |
# the child will indicate when it's done with its test by writing a message |
| 199 |
# to the parent. |
| 200 |
my ($rfh, $wfh); |
| 201 |
unless (pipe($rfh, $wfh)) { |
| 202 |
die("Can't open pipe: $!"); |
| 203 |
} |
| 204 |
|
| 205 |
my $ex; |
| 206 |
|
| 207 |
# Fork child |
| 208 |
$self->handle_sigchld(); |
| 209 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 210 |
if ($pid) { |
| 211 |
eval { |
| 212 |
# Allow for server startup |
| 213 |
sleep(1); |
| 214 |
|
| 215 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 216 |
$client->login($setup->{user}, $setup->{passwd}); |
| 217 |
|
| 218 |
$client->type('I'); |
| 219 |
$client->rest(1); |
| 220 |
|
| 221 |
my $conn = $client->stor_raw('test.txt'); |
| 222 |
unless ($conn) { |
| 223 |
die("STOR test.txt failed: " . $client->response_code() . ' ' . |
| 224 |
$client->response_msg()); |
| 225 |
} |
| 226 |
|
| 227 |
my $resp_code = $client->response_code(); |
| 228 |
my $resp_msg = $client->response_msg(); |
| 229 |
|
| 230 |
my $expected = 150; |
| 231 |
$self->assert($expected == $resp_code, |
| 232 |
test_msg("Expected response code $expected, got $resp_code")); |
| 233 |
|
| 234 |
eval { $conn->close(5) }; |
| 235 |
$client->quit(); |
| 236 |
}; |
| 237 |
if ($@) { |
| 238 |
$ex = $@; |
| 239 |
} |
| 240 |
|
| 241 |
$wfh->print("done\n"); |
| 242 |
$wfh->flush(); |
| 243 |
|
| 244 |
} else { |
| 245 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 246 |
if ($@) { |
| 247 |
warn($@); |
| 248 |
exit 1; |
| 249 |
} |
| 250 |
|
| 251 |
exit 0; |
| 252 |
} |
| 253 |
|
| 254 |
# Stop server |
| 255 |
server_stop($setup->{pid_file}); |
| 256 |
$self->assert_child_ok($pid); |
| 257 |
|
| 258 |
test_cleanup($setup->{log_file}, $ex); |
| 259 |
} |
| 260 |
|
| 261 |
1; |