| 1 |
package ProFTPD::Tests::Config::TransferOptions; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use File::Path qw(mkpath); |
| 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 |
transferoptions_ignore_ascii_download_bug4159 => { |
| 20 |
order => ++$order, |
| 21 |
test_class => [qw(bug forking)], |
| 22 |
}, |
| 23 |
|
| 24 |
transferoptions_ignore_ascii_upload_bug4159 => { |
| 25 |
order => ++$order, |
| 26 |
test_class => [qw(bug 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 transferoptions_ignore_ascii_download_bug4159 { |
| 40 |
my $self = shift; |
| 41 |
my $tmpdir = $self->{tmpdir}; |
| 42 |
my $setup = test_setup($tmpdir, 'cmds'); |
| 43 |
|
| 44 |
my $test_data = "Hello, World!\r\n"; |
| 45 |
my $test_file = File::Spec->rel2abs("$tmpdir/test.dat"); |
| 46 |
if (open(my $fh, "> $test_file")) { |
| 47 |
print $fh $test_data; |
| 48 |
unless (close($fh)) { |
| 49 |
die("Can't write $test_file: $!"); |
| 50 |
} |
| 51 |
|
| 52 |
} else { |
| 53 |
die("Can't open $test_file: $!"); |
| 54 |
} |
| 55 |
|
| 56 |
my $config = { |
| 57 |
PidFile => $setup->{pid_file}, |
| 58 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 59 |
SystemLog => $setup->{log_file}, |
| 60 |
|
| 61 |
AuthUserFile => $setup->{auth_user_file}, |
| 62 |
AuthGroupFile => $setup->{auth_group_file}, |
| 63 |
AuthOrder => 'mod_auth_file.c', |
| 64 |
|
| 65 |
DefaultTransferMode => 'binary', |
| 66 |
TransferOptions => 'IgnoreASCII', |
| 67 |
|
| 68 |
IfModules => { |
| 69 |
'mod_delay.c' => { |
| 70 |
DelayEngine => 'off', |
| 71 |
}, |
| 72 |
}, |
| 73 |
}; |
| 74 |
|
| 75 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 76 |
$config); |
| 77 |
|
| 78 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 79 |
# the child will indicate when it's done with its test by writing a message |
| 80 |
# to the parent. |
| 81 |
my ($rfh, $wfh); |
| 82 |
unless (pipe($rfh, $wfh)) { |
| 83 |
die("Can't open pipe: $!"); |
| 84 |
} |
| 85 |
|
| 86 |
my $ex; |
| 87 |
|
| 88 |
# Fork child |
| 89 |
$self->handle_sigchld(); |
| 90 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 91 |
if ($pid) { |
| 92 |
eval { |
| 93 |
# When run too quickly with the other tests, this test can fail. So |
| 94 |
# pause a little here. |
| 95 |
sleep(1); |
| 96 |
|
| 97 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1); |
| 98 |
$client->login($setup->{user}, $setup->{passwd}); |
| 99 |
|
| 100 |
# Tell the server we'll be doing an ASCII transfer, BUT configure |
| 101 |
# the Net::FTP internals to expect binary data, so that we can properly |
| 102 |
# compare the data we transfer. |
| 103 |
$client->type('A'); |
| 104 |
my $ftp = $client->{ftp}; |
| 105 |
${*$ftp}{net_ftp_type} = 'I'; |
| 106 |
|
| 107 |
my $conn = $client->retr_raw($test_file); |
| 108 |
unless ($conn) { |
| 109 |
die("Failed to RETR: " . $client->response_code() . " " . |
| 110 |
$client->response_msg()); |
| 111 |
} |
| 112 |
|
| 113 |
my $buf; |
| 114 |
$conn->read($buf, 8192, 30); |
| 115 |
eval { $conn->close() }; |
| 116 |
|
| 117 |
my ($resp_code, $resp_msg); |
| 118 |
$resp_code = $client->response_code(); |
| 119 |
$resp_msg = $client->response_msg(); |
| 120 |
$self->assert_transfer_ok($resp_code, $resp_msg); |
| 121 |
$client->quit(); |
| 122 |
|
| 123 |
# Make sure that proftpd did NOT perform ASCII translation, per our |
| 124 |
# configured TransferOption. |
| 125 |
my $buflen = length($buf); |
| 126 |
my $test_datalen = length($test_data); |
| 127 |
$self->assert($buf eq $test_data, |
| 128 |
test_msg("Downloaded data '$buf' ($buflen) did not match expected data '$test_data' ($test_datalen)")); |
| 129 |
}; |
| 130 |
|
| 131 |
if ($@) { |
| 132 |
$ex = $@; |
| 133 |
} |
| 134 |
|
| 135 |
$wfh->print("done\n"); |
| 136 |
$wfh->flush(); |
| 137 |
|
| 138 |
} else { |
| 139 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 140 |
if ($@) { |
| 141 |
warn($@); |
| 142 |
exit 1; |
| 143 |
} |
| 144 |
|
| 145 |
exit 0; |
| 146 |
} |
| 147 |
|
| 148 |
# Stop server |
| 149 |
server_stop($setup->{pid_file}); |
| 150 |
|
| 151 |
$self->assert_child_ok($pid); |
| 152 |
|
| 153 |
test_cleanup($setup->{log_file}, $ex); |
| 154 |
} |
| 155 |
|
| 156 |
sub transferoptions_ignore_ascii_upload_bug4159 { |
| 157 |
my $self = shift; |
| 158 |
my $tmpdir = $self->{tmpdir}; |
| 159 |
my $setup = test_setup($tmpdir, 'cmds'); |
| 160 |
|
| 161 |
my $test_data = "Hello, World!\r\n"; |
| 162 |
my $test_file = File::Spec->rel2abs("$tmpdir/test.dat"); |
| 163 |
|
| 164 |
my $config = { |
| 165 |
PidFile => $setup->{pid_file}, |
| 166 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 167 |
SystemLog => $setup->{log_file}, |
| 168 |
|
| 169 |
AuthUserFile => $setup->{auth_user_file}, |
| 170 |
AuthGroupFile => $setup->{auth_group_file}, |
| 171 |
AuthOrder => 'mod_auth_file.c', |
| 172 |
|
| 173 |
DefaultTransferMode => 'binary', |
| 174 |
TransferOptions => 'IgnoreASCII', |
| 175 |
|
| 176 |
IfModules => { |
| 177 |
'mod_delay.c' => { |
| 178 |
DelayEngine => 'off', |
| 179 |
}, |
| 180 |
}, |
| 181 |
}; |
| 182 |
|
| 183 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 184 |
$config); |
| 185 |
|
| 186 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 187 |
# the child will indicate when it's done with its test by writing a message |
| 188 |
# to the parent. |
| 189 |
my ($rfh, $wfh); |
| 190 |
unless (pipe($rfh, $wfh)) { |
| 191 |
die("Can't open pipe: $!"); |
| 192 |
} |
| 193 |
|
| 194 |
my $ex; |
| 195 |
|
| 196 |
# Fork child |
| 197 |
$self->handle_sigchld(); |
| 198 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 199 |
if ($pid) { |
| 200 |
eval { |
| 201 |
# When run too quickly with the other tests, this test can fail. So |
| 202 |
# pause a little here. |
| 203 |
sleep(1); |
| 204 |
|
| 205 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1); |
| 206 |
$client->login($setup->{user}, $setup->{passwd}); |
| 207 |
|
| 208 |
# Tell the server we'll be doing an ASCII transfer, BUT configure |
| 209 |
# the Net::FTP internals to expect binary data, so that we can properly |
| 210 |
# compare the data we transfer. |
| 211 |
$client->type('A'); |
| 212 |
my $ftp = $client->{ftp}; |
| 213 |
${*$ftp}{net_ftp_type} = 'I'; |
| 214 |
|
| 215 |
my $conn = $client->stor_raw($test_file); |
| 216 |
unless ($conn) { |
| 217 |
die("Failed to STOR: " . $client->response_code() . " " . |
| 218 |
$client->response_msg()); |
| 219 |
} |
| 220 |
|
| 221 |
$conn->write($test_data, length($test_data), 30); |
| 222 |
eval { $conn->close() }; |
| 223 |
|
| 224 |
my ($resp_code, $resp_msg); |
| 225 |
$resp_code = $client->response_code(); |
| 226 |
$resp_msg = $client->response_msg(); |
| 227 |
$self->assert_transfer_ok($resp_code, $resp_msg); |
| 228 |
$client->quit(); |
| 229 |
|
| 230 |
# Make sure that proftpd did NOT perform ASCII translation, per our |
| 231 |
# configured TransferOption. |
| 232 |
if (open(my $fh, "< $test_file")) { |
| 233 |
local $/; |
| 234 |
my $buf = <$fh>; |
| 235 |
close($fh); |
| 236 |
|
| 237 |
my $buflen = length($buf); |
| 238 |
my $test_datalen = length($test_data); |
| 239 |
$self->assert($buf eq $test_data, |
| 240 |
test_msg("Downloaded data '$buf' ($buflen) did not match expected data '$test_data' ($test_datalen)")); |
| 241 |
|
| 242 |
} else { |
| 243 |
die("Can't read $test_file: $!"); |
| 244 |
} |
| 245 |
|
| 246 |
}; |
| 247 |
|
| 248 |
if ($@) { |
| 249 |
$ex = $@; |
| 250 |
} |
| 251 |
|
| 252 |
$wfh->print("done\n"); |
| 253 |
$wfh->flush(); |
| 254 |
|
| 255 |
} else { |
| 256 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 257 |
if ($@) { |
| 258 |
warn($@); |
| 259 |
exit 1; |
| 260 |
} |
| 261 |
|
| 262 |
exit 0; |
| 263 |
} |
| 264 |
|
| 265 |
# Stop server |
| 266 |
server_stop($setup->{pid_file}); |
| 267 |
|
| 268 |
$self->assert_child_ok($pid); |
| 269 |
|
| 270 |
test_cleanup($setup->{log_file}, $ex); |
| 271 |
} |
| 272 |
|
| 273 |
1; |