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