| 1 | package ProFTPD::Tests::Config::MaxStoreFileSize; |
|---|
| 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 | maxstorefilesize_ok => { |
|---|
| 19 | order => ++$order, |
|---|
| 20 | test_class => [qw(forking)], |
|---|
| 21 | }, |
|---|
| 22 | |
|---|
| 23 | maxstorefilesize_exceeded => { |
|---|
| 24 | order => ++$order, |
|---|
| 25 | test_class => [qw(forking)], |
|---|
| 26 | }, |
|---|
| 27 | |
|---|
| 28 | maxstorefilesize_appe_bug3649 => { |
|---|
| 29 | order => ++$order, |
|---|
| 30 | test_class => [qw(bug forking)], |
|---|
| 31 | }, |
|---|
| 32 | |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | sub new { |
|---|
| 36 | return shift()->SUPER::new(@_); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | sub list_tests { |
|---|
| 40 | return testsuite_get_runnable_tests($TESTS); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | sub maxstorefilesize_ok { |
|---|
| 44 | my $self = shift; |
|---|
| 45 | my $tmpdir = $self->{tmpdir}; |
|---|
| 46 | |
|---|
| 47 | my $config_file = "$tmpdir/config.conf"; |
|---|
| 48 | my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid"); |
|---|
| 49 | my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard"); |
|---|
| 50 | |
|---|
| 51 | my $log_file = File::Spec->rel2abs('tests.log'); |
|---|
| 52 | |
|---|
| 53 | my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd"); |
|---|
| 54 | my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group"); |
|---|
| 55 | |
|---|
| 56 | my $user = 'proftpd'; |
|---|
| 57 | my $passwd = 'test'; |
|---|
| 58 | my $home_dir = File::Spec->rel2abs($tmpdir); |
|---|
| 59 | my $uid = 500; |
|---|
| 60 | my $gid = 500; |
|---|
| 61 | |
|---|
| 62 | # Make sure that, if we're running as root, that the home directory has |
|---|
| 63 | # permissions/privs set for the account we create |
|---|
| 64 | if ($< == 0) { |
|---|
| 65 | unless (chmod(0755, $home_dir)) { |
|---|
| 66 | die("Can't set perms on $home_dir to 0755: $!"); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | unless (chown($uid, $gid, $home_dir)) { |
|---|
| 70 | die("Can't set owner of $home_dir to $uid/$gid: $!"); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
|---|
| 75 | '/bin/bash'); |
|---|
| 76 | auth_group_write($auth_group_file, 'ftpd', $gid, $user); |
|---|
| 77 | |
|---|
| 78 | my $dst_file = File::Spec->rel2abs("$tmpdir/foo"); |
|---|
| 79 | |
|---|
| 80 | my $config = { |
|---|
| 81 | PidFile => $pid_file, |
|---|
| 82 | ScoreboardFile => $scoreboard_file, |
|---|
| 83 | SystemLog => $log_file, |
|---|
| 84 | |
|---|
| 85 | AuthUserFile => $auth_user_file, |
|---|
| 86 | AuthGroupFile => $auth_group_file, |
|---|
| 87 | |
|---|
| 88 | MaxStoreFileSize => '100 B', |
|---|
| 89 | |
|---|
| 90 | IfModules => { |
|---|
| 91 | 'mod_delay.c' => { |
|---|
| 92 | DelayEngine => 'off', |
|---|
| 93 | }, |
|---|
| 94 | }, |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | my ($port, $config_user, $config_group) = config_write($config_file, $config); |
|---|
| 98 | |
|---|
| 99 | # Open pipes, for use between the parent and child processes. Specifically, |
|---|
| 100 | # the child will indicate when it's done with its test by writing a message |
|---|
| 101 | # to the parent. |
|---|
| 102 | my ($rfh, $wfh); |
|---|
| 103 | unless (pipe($rfh, $wfh)) { |
|---|
| 104 | die("Can't open pipe: $!"); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | my $ex; |
|---|
| 108 | |
|---|
| 109 | # Fork child |
|---|
| 110 | $self->handle_sigchld(); |
|---|
| 111 | defined(my $pid = fork()) or die("Can't fork: $!"); |
|---|
| 112 | if ($pid) { |
|---|
| 113 | eval { |
|---|
| 114 | my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
|---|
| 115 | |
|---|
| 116 | $client->login($user, $passwd); |
|---|
| 117 | |
|---|
| 118 | my $conn = $client->stor_raw('foo'); |
|---|
| 119 | unless ($conn) { |
|---|
| 120 | die("STOR failed: " . $client->response_code() . " " . |
|---|
| 121 | $client->response_msg()); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | my $buf = "Hello, World!\n"; |
|---|
| 125 | $conn->write($buf, length($buf)); |
|---|
| 126 | eval { $conn->close() }; |
|---|
| 127 | |
|---|
| 128 | my $expected; |
|---|
| 129 | |
|---|
| 130 | $expected = 14; |
|---|
| 131 | my $filelen = -s $dst_file; |
|---|
| 132 | $self->assert($expected == $filelen, |
|---|
| 133 | test_msg("Expected $expected, got $filelen")); |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | if ($@) { |
|---|
| 137 | $ex = $@; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | $wfh->print("done\n"); |
|---|
| 141 | $wfh->flush(); |
|---|
| 142 | |
|---|
| 143 | } else { |
|---|
| 144 | eval { server_wait($config_file, $rfh) }; |
|---|
| 145 | if ($@) { |
|---|
| 146 | warn($@); |
|---|
| 147 | exit 1; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | exit 0; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | # Stop server |
|---|
| 154 | server_stop($pid_file); |
|---|
| 155 | |
|---|
| 156 | $self->assert_child_ok($pid); |
|---|
| 157 | |
|---|
| 158 | if ($ex) { |
|---|
| 159 | die($ex); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | unlink($log_file); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | sub maxstorefilesize_exceeded { |
|---|
| 166 | my $self = shift; |
|---|
| 167 | my $tmpdir = $self->{tmpdir}; |
|---|
| 168 | |
|---|
| 169 | my $config_file = "$tmpdir/config.conf"; |
|---|
| 170 | my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid"); |
|---|
| 171 | my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard"); |
|---|
| 172 | |
|---|
| 173 | my $log_file = File::Spec->rel2abs('tests.log'); |
|---|
| 174 | |
|---|
| 175 | my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd"); |
|---|
| 176 | my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group"); |
|---|
| 177 | |
|---|
| 178 | my $user = 'proftpd'; |
|---|
| 179 | my $passwd = 'test'; |
|---|
| 180 | my $home_dir = File::Spec->rel2abs($tmpdir); |
|---|
| 181 | my $uid = 500; |
|---|
| 182 | my $gid = 500; |
|---|
| 183 | |
|---|
| 184 | # Make sure that, if we're running as root, that the home directory has |
|---|
| 185 | # permissions/privs set for the account we create |
|---|
| 186 | if ($< == 0) { |
|---|
| 187 | unless (chmod(0755, $home_dir)) { |
|---|
| 188 | die("Can't set perms on $home_dir to 0755: $!"); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | unless (chown($uid, $gid, $home_dir)) { |
|---|
| 192 | die("Can't set owner of $home_dir to $uid/$gid: $!"); |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
|---|
| 197 | '/bin/bash'); |
|---|
| 198 | auth_group_write($auth_group_file, 'ftpd', $gid, $user); |
|---|
| 199 | |
|---|
| 200 | my $dst_file = File::Spec->rel2abs("$tmpdir/foo"); |
|---|
| 201 | |
|---|
| 202 | my $config = { |
|---|
| 203 | PidFile => $pid_file, |
|---|
| 204 | ScoreboardFile => $scoreboard_file, |
|---|
| 205 | SystemLog => $log_file, |
|---|
| 206 | |
|---|
| 207 | AuthUserFile => $auth_user_file, |
|---|
| 208 | AuthGroupFile => $auth_group_file, |
|---|
| 209 | |
|---|
| 210 | MaxStoreFileSize => '4 B', |
|---|
| 211 | |
|---|
| 212 | IfModules => { |
|---|
| 213 | 'mod_delay.c' => { |
|---|
| 214 | DelayEngine => 'off', |
|---|
| 215 | }, |
|---|
| 216 | }, |
|---|
| 217 | }; |
|---|
| 218 | |
|---|
| 219 | my ($port, $config_user, $config_group) = config_write($config_file, $config); |
|---|
| 220 | |
|---|
| 221 | # Open pipes, for use between the parent and child processes. Specifically, |
|---|
| 222 | # the child will indicate when it's done with its test by writing a message |
|---|
| 223 | # to the parent. |
|---|
| 224 | my ($rfh, $wfh); |
|---|
| 225 | unless (pipe($rfh, $wfh)) { |
|---|
| 226 | die("Can't open pipe: $!"); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | my $ex; |
|---|
| 230 | |
|---|
| 231 | # Fork child |
|---|
| 232 | $self->handle_sigchld(); |
|---|
| 233 | defined(my $pid = fork()) or die("Can't fork: $!"); |
|---|
| 234 | if ($pid) { |
|---|
| 235 | eval { |
|---|
| 236 | my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
|---|
| 237 | |
|---|
| 238 | $client->login($user, $passwd); |
|---|
| 239 | |
|---|
| 240 | my ($resp_code, $resp_msg); |
|---|
| 241 | |
|---|
| 242 | my $conn = $client->stor_raw('foo'); |
|---|
| 243 | unless ($conn) { |
|---|
| 244 | die("STOR failed: " . $client->response_code() . " " . |
|---|
| 245 | $client->response_msg()); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | my $buf = "Hello, World!\n"; |
|---|
| 249 | $conn->write($buf, length($buf)); |
|---|
| 250 | eval { $conn->close() }; |
|---|
| 251 | |
|---|
| 252 | $resp_code = $client->response_code(); |
|---|
| 253 | $resp_msg = $client->response_msg(); |
|---|
| 254 | |
|---|
| 255 | my $expected; |
|---|
| 256 | |
|---|
| 257 | $expected = 552; |
|---|
| 258 | $self->assert($expected == $resp_code, |
|---|
| 259 | test_msg("Expected $expected, got $resp_code")); |
|---|
| 260 | |
|---|
| 261 | $expected = 'Transfer aborted. ((Disc|Disk) quota exceeded|File too large)'; |
|---|
| 262 | $self->assert(qr/$expected/, $resp_msg, |
|---|
| 263 | test_msg("Expected '$expected', got '$resp_msg'")); |
|---|
| 264 | }; |
|---|
| 265 | |
|---|
| 266 | if ($@) { |
|---|
| 267 | $ex = $@; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | $wfh->print("done\n"); |
|---|
| 271 | $wfh->flush(); |
|---|
| 272 | |
|---|
| 273 | } else { |
|---|
| 274 | eval { server_wait($config_file, $rfh) }; |
|---|
| 275 | if ($@) { |
|---|
| 276 | warn($@); |
|---|
| 277 | exit 1; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | exit 0; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | # Stop server |
|---|
| 284 | server_stop($pid_file); |
|---|
| 285 | |
|---|
| 286 | $self->assert_child_ok($pid); |
|---|
| 287 | |
|---|
| 288 | if ($ex) { |
|---|
| 289 | die($ex); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | unlink($log_file); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | sub maxstorefilesize_appe_bug3649 { |
|---|
| 296 | my $self = shift; |
|---|
| 297 | my $tmpdir = $self->{tmpdir}; |
|---|
| 298 | |
|---|
| 299 | my $config_file = "$tmpdir/config.conf"; |
|---|
| 300 | my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid"); |
|---|
| 301 | my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard"); |
|---|
| 302 | |
|---|
| 303 | my $log_file = File::Spec->rel2abs('tests.log'); |
|---|
| 304 | |
|---|
| 305 | my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd"); |
|---|
| 306 | my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group"); |
|---|
| 307 | |
|---|
| 308 | my $user = 'proftpd'; |
|---|
| 309 | my $passwd = 'test'; |
|---|
| 310 | my $group = 'ftpd'; |
|---|
| 311 | my $home_dir = File::Spec->rel2abs($tmpdir); |
|---|
| 312 | my $uid = 500; |
|---|
| 313 | my $gid = 500; |
|---|
| 314 | |
|---|
| 315 | # Make sure that, if we're running as root, that the home directory has |
|---|
| 316 | # permissions/privs set for the account we create |
|---|
| 317 | if ($< == 0) { |
|---|
| 318 | unless (chmod(0755, $home_dir)) { |
|---|
| 319 | die("Can't set perms on $home_dir to 0755: $!"); |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | unless (chown($uid, $gid, $home_dir)) { |
|---|
| 323 | die("Can't set owner of $home_dir to $uid/$gid: $!"); |
|---|
| 324 | } |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
|---|
| 328 | '/bin/bash'); |
|---|
| 329 | auth_group_write($auth_group_file, $group, $gid, $user); |
|---|
| 330 | |
|---|
| 331 | my $dst_file = File::Spec->rel2abs("$tmpdir/foo.txt"); |
|---|
| 332 | if (open(my $fh, "> $dst_file")) { |
|---|
| 333 | print $fh "ABC" x 32; |
|---|
| 334 | unless (close($fh)) { |
|---|
| 335 | die("Can't write $dst_file: $!"); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | } else { |
|---|
| 339 | die("Can't open $dst_file: $!"); |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | my $max_sz = 128; |
|---|
| 343 | |
|---|
| 344 | my $config = { |
|---|
| 345 | PidFile => $pid_file, |
|---|
| 346 | ScoreboardFile => $scoreboard_file, |
|---|
| 347 | SystemLog => $log_file, |
|---|
| 348 | |
|---|
| 349 | AuthUserFile => $auth_user_file, |
|---|
| 350 | AuthGroupFile => $auth_group_file, |
|---|
| 351 | |
|---|
| 352 | AllowOverwrite => 'on', |
|---|
| 353 | AllowStoreRestart => 'on', |
|---|
| 354 | MaxStoreFileSize => "$max_sz B", |
|---|
| 355 | |
|---|
| 356 | IfModules => { |
|---|
| 357 | 'mod_delay.c' => { |
|---|
| 358 | DelayEngine => 'off', |
|---|
| 359 | }, |
|---|
| 360 | }, |
|---|
| 361 | }; |
|---|
| 362 | |
|---|
| 363 | my ($port, $config_user, $config_group) = config_write($config_file, $config); |
|---|
| 364 | |
|---|
| 365 | # Open pipes, for use between the parent and child processes. Specifically, |
|---|
| 366 | # the child will indicate when it's done with its test by writing a message |
|---|
| 367 | # to the parent. |
|---|
| 368 | my ($rfh, $wfh); |
|---|
| 369 | unless (pipe($rfh, $wfh)) { |
|---|
| 370 | die("Can't open pipe: $!"); |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | my $ex; |
|---|
| 374 | |
|---|
| 375 | # Fork child |
|---|
| 376 | $self->handle_sigchld(); |
|---|
| 377 | defined(my $pid = fork()) or die("Can't fork: $!"); |
|---|
| 378 | if ($pid) { |
|---|
| 379 | eval { |
|---|
| 380 | my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
|---|
| 381 | $client->login($user, $passwd); |
|---|
| 382 | |
|---|
| 383 | my ($resp_code, $resp_msg); |
|---|
| 384 | |
|---|
| 385 | my $conn = $client->appe_raw('foo.txt'); |
|---|
| 386 | unless ($conn) { |
|---|
| 387 | die("APPE failed: " . $client->response_code() . " " . |
|---|
| 388 | $client->response_msg()); |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | my $buf = "DEF" x 32; |
|---|
| 392 | $conn->write($buf, length($buf), 25); |
|---|
| 393 | eval { $conn->close() }; |
|---|
| 394 | |
|---|
| 395 | $resp_code = $client->response_code(); |
|---|
| 396 | $resp_msg = $client->response_msg(); |
|---|
| 397 | |
|---|
| 398 | my $expected; |
|---|
| 399 | |
|---|
| 400 | $expected = 552; |
|---|
| 401 | $self->assert($expected == $resp_code, |
|---|
| 402 | test_msg("Expected $expected, got $resp_code")); |
|---|
| 403 | |
|---|
| 404 | $expected = 'Transfer aborted. ((Disc|Disk) quota exceeded|File too large)'; |
|---|
| 405 | $self->assert(qr/$expected/, $resp_msg, |
|---|
| 406 | test_msg("Expected '$expected', got '$resp_msg'")); |
|---|
| 407 | |
|---|
| 408 | my $sz = -s $dst_file; |
|---|
| 409 | $self->assert($sz <= $max_sz, |
|---|
| 410 | test_msg("Expected <= $max_sz, got $sz")); |
|---|
| 411 | }; |
|---|
| 412 | |
|---|
| 413 | if ($@) { |
|---|
| 414 | $ex = $@; |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | $wfh->print("done\n"); |
|---|
| 418 | $wfh->flush(); |
|---|
| 419 | |
|---|
| 420 | } else { |
|---|
| 421 | eval { server_wait($config_file, $rfh) }; |
|---|
| 422 | if ($@) { |
|---|
| 423 | warn($@); |
|---|
| 424 | exit 1; |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | exit 0; |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | # Stop server |
|---|
| 431 | server_stop($pid_file); |
|---|
| 432 | |
|---|
| 433 | $self->assert_child_ok($pid); |
|---|
| 434 | |
|---|
| 435 | if ($ex) { |
|---|
| 436 | die($ex); |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | unlink($log_file); |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | 1; |
|---|