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