| 1 |
package ProFTPD::Tests::Config::StoreUniquePrefix; |
| 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 |
storeuniqueprefix_ok => { |
| 19 |
order => ++$order, |
| 20 |
test_class => [qw(forking)], |
| 21 |
}, |
| 22 |
}; |
| 23 |
|
| 24 |
sub new { |
| 25 |
return shift()->SUPER::new(@_); |
| 26 |
} |
| 27 |
|
| 28 |
sub list_tests { |
| 29 |
return testsuite_get_runnable_tests($TESTS); |
| 30 |
} |
| 31 |
|
| 32 |
sub storeuniqueprefix_ok { |
| 33 |
my $self = shift; |
| 34 |
my $tmpdir = $self->{tmpdir}; |
| 35 |
|
| 36 |
my $config_file = "$tmpdir/config.conf"; |
| 37 |
my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid"); |
| 38 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard"); |
| 39 |
|
| 40 |
my $log_file = File::Spec->rel2abs('tests.log'); |
| 41 |
|
| 42 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd"); |
| 43 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group"); |
| 44 |
|
| 45 |
my $user = 'proftpd'; |
| 46 |
my $passwd = 'test'; |
| 47 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 48 |
my $uid = 500; |
| 49 |
my $gid = 500; |
| 50 |
|
| 51 |
# Make sure that, if we're running as root, that the home directory has |
| 52 |
# permissions/privs set for the account we create |
| 53 |
if ($< == 0) { |
| 54 |
unless (chmod(0755, $home_dir)) { |
| 55 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 56 |
} |
| 57 |
|
| 58 |
unless (chown($uid, $gid, $home_dir)) { |
| 59 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 64 |
'/bin/bash'); |
| 65 |
auth_group_write($auth_group_file, 'ftpd', $gid, $user); |
| 66 |
|
| 67 |
my $prefix = 'test-uniq-prefix'; |
| 68 |
|
| 69 |
my $config = { |
| 70 |
PidFile => $pid_file, |
| 71 |
ScoreboardFile => $scoreboard_file, |
| 72 |
SystemLog => $log_file, |
| 73 |
|
| 74 |
AuthUserFile => $auth_user_file, |
| 75 |
AuthGroupFile => $auth_group_file, |
| 76 |
AuthOrder => 'mod_auth_file.c', |
| 77 |
|
| 78 |
DefaultChdir => '~', |
| 79 |
AllowOverwrite => 'on', |
| 80 |
StoreUniquePrefix => $prefix, |
| 81 |
|
| 82 |
IfModules => { |
| 83 |
'mod_delay.c' => { |
| 84 |
DelayEngine => 'off', |
| 85 |
}, |
| 86 |
}, |
| 87 |
}; |
| 88 |
|
| 89 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 90 |
|
| 91 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 92 |
# the child will indicate when it's done with its test by writing a message |
| 93 |
# to the parent. |
| 94 |
my ($rfh, $wfh); |
| 95 |
unless (pipe($rfh, $wfh)) { |
| 96 |
die("Can't open pipe: $!"); |
| 97 |
} |
| 98 |
|
| 99 |
my $ex; |
| 100 |
|
| 101 |
# Fork child |
| 102 |
$self->handle_sigchld(); |
| 103 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 104 |
if ($pid) { |
| 105 |
eval { |
| 106 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 107 |
$client->login($user, $passwd); |
| 108 |
|
| 109 |
my $conn = $client->stou_raw('test.txt'); |
| 110 |
unless ($conn) { |
| 111 |
die("Failed to STOU test.txt: " . $client->response_code() . " " . |
| 112 |
$client->response_msg()); |
| 113 |
} |
| 114 |
|
| 115 |
my $buf = "Hello, World!\n"; |
| 116 |
$conn->write($buf, length($buf)); |
| 117 |
$conn->close(); |
| 118 |
|
| 119 |
my $resp_code = $client->response_code(); |
| 120 |
my $resp_msg = $client->response_msg(); |
| 121 |
|
| 122 |
my $expected; |
| 123 |
|
| 124 |
$expected = 226; |
| 125 |
$self->assert($expected == $resp_code, |
| 126 |
test_msg("Expected $expected, got $resp_code")); |
| 127 |
|
| 128 |
$expected = "Transfer complete"; |
| 129 |
$self->assert($expected eq $resp_msg, |
| 130 |
test_msg("Expected '$expected', got '$resp_msg'")); |
| 131 |
|
| 132 |
my $uniq_file = $client->response_uniq(); |
| 133 |
|
| 134 |
$client->quit(); |
| 135 |
|
| 136 |
$expected = "^$prefix"; |
| 137 |
$self->assert(qr/$expected/, $uniq_file, |
| 138 |
test_msg("Expected '$expected', got '$uniq_file'")); |
| 139 |
|
| 140 |
unless (-f "$home_dir/$uniq_file") { |
| 141 |
die("File $home_dir/uniq_file does not exist as expected"); |
| 142 |
} |
| 143 |
}; |
| 144 |
|
| 145 |
if ($@) { |
| 146 |
$ex = $@; |
| 147 |
} |
| 148 |
|
| 149 |
$wfh->print("done\n"); |
| 150 |
$wfh->flush(); |
| 151 |
|
| 152 |
} else { |
| 153 |
eval { server_wait($config_file, $rfh) }; |
| 154 |
if ($@) { |
| 155 |
warn($@); |
| 156 |
exit 1; |
| 157 |
} |
| 158 |
|
| 159 |
exit 0; |
| 160 |
} |
| 161 |
|
| 162 |
# Stop server |
| 163 |
server_stop($pid_file); |
| 164 |
|
| 165 |
$self->assert_child_ok($pid); |
| 166 |
|
| 167 |
if ($ex) { |
| 168 |
die($ex); |
| 169 |
} |
| 170 |
|
| 171 |
unlink($log_file); |
| 172 |
} |
| 173 |
|
| 174 |
1; |