| 1 |
package ProFTPD::Tests::SMTP; |
| 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 |
smtp_session => { |
| 19 |
order => ++$order, |
| 20 |
test_class => [qw(bug forking)], |
| 21 |
}, |
| 22 |
|
| 23 |
}; |
| 24 |
|
| 25 |
sub new { |
| 26 |
return shift()->SUPER::new(@_); |
| 27 |
} |
| 28 |
|
| 29 |
sub list_tests { |
| 30 |
# Check for the required Perl modules: |
| 31 |
# |
| 32 |
# Net-SMTP |
| 33 |
|
| 34 |
my $required = [qw( |
| 35 |
Net::SMTP |
| 36 |
)]; |
| 37 |
|
| 38 |
foreach my $req (@$required) { |
| 39 |
eval "use $req"; |
| 40 |
if ($@) { |
| 41 |
print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n"; |
| 42 |
|
| 43 |
if ($ENV{TEST_VERBOSE}) { |
| 44 |
print STDERR "Unable to load $req: $@\n"; |
| 45 |
} |
| 46 |
|
| 47 |
return qw(testsuite_empty_test); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return testsuite_get_runnable_tests($TESTS); |
| 52 |
} |
| 53 |
|
| 54 |
sub smtp_session { |
| 55 |
my $self = shift; |
| 56 |
my $tmpdir = $self->{tmpdir}; |
| 57 |
|
| 58 |
my $config_file = "$tmpdir/smtp.conf"; |
| 59 |
my $pid_file = File::Spec->rel2abs("$tmpdir/smtp.pid"); |
| 60 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/smtp.scoreboard"); |
| 61 |
|
| 62 |
my $log_file = test_get_logfile(); |
| 63 |
|
| 64 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/smtp.passwd"); |
| 65 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/smtp.group"); |
| 66 |
|
| 67 |
my $user = 'proftpd'; |
| 68 |
my $passwd = 'test'; |
| 69 |
my $group = 'ftpd'; |
| 70 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 71 |
my $uid = 500; |
| 72 |
my $gid = 500; |
| 73 |
|
| 74 |
# Make sure that, if we're running as root, that the home directory has |
| 75 |
# permissions/privs set for the account we create |
| 76 |
if ($< == 0) { |
| 77 |
unless (chmod(0755, $home_dir)) { |
| 78 |
die("Can't set perms on $home_dir to 0755: $!"); |
| 79 |
} |
| 80 |
|
| 81 |
unless (chown($uid, $gid, $home_dir)) { |
| 82 |
die("Can't set owner of $home_dir to $uid/$gid: $!"); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 87 |
'/bin/bash'); |
| 88 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 89 |
|
| 90 |
my $config = { |
| 91 |
PidFile => $pid_file, |
| 92 |
ScoreboardFile => $scoreboard_file, |
| 93 |
SystemLog => $log_file, |
| 94 |
|
| 95 |
AuthUserFile => $auth_user_file, |
| 96 |
AuthGroupFile => $auth_group_file, |
| 97 |
AuthOrder => 'mod_auth_file.c', |
| 98 |
|
| 99 |
IfModules => { |
| 100 |
'mod_delay.c' => { |
| 101 |
DelayEngine => 'off', |
| 102 |
}, |
| 103 |
}, |
| 104 |
}; |
| 105 |
|
| 106 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 107 |
|
| 108 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 109 |
# the child will indicate when it's done with its test by writing a message |
| 110 |
# to the parent. |
| 111 |
my ($rfh, $wfh); |
| 112 |
unless (pipe($rfh, $wfh)) { |
| 113 |
die("Can't open pipe: $!"); |
| 114 |
} |
| 115 |
|
| 116 |
require Net::SMTP; |
| 117 |
|
| 118 |
my $ex; |
| 119 |
|
| 120 |
# Fork child |
| 121 |
$self->handle_sigchld(); |
| 122 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 123 |
if ($pid) { |
| 124 |
eval { |
| 125 |
sleep(1); |
| 126 |
|
| 127 |
# To reproduce Bug#4143, we only need to connect to the server, |
| 128 |
# then issue an SMTP command. |
| 129 |
|
| 130 |
my $client = Net::SMTP->new('127.0.0.1', |
| 131 |
Port => $port, |
| 132 |
Timeout => 10, |
| 133 |
Debug => $ENV{TEST_VERBOSE}, |
| 134 |
); |
| 135 |
|
| 136 |
if ($client) { |
| 137 |
die("Unexpectedly succeeded in connecting to SMTP server at 127.0.0.1:$port"); |
| 138 |
} |
| 139 |
|
| 140 |
}; |
| 141 |
|
| 142 |
if ($@) { |
| 143 |
$ex = $@; |
| 144 |
} |
| 145 |
|
| 146 |
$wfh->print("done\n"); |
| 147 |
$wfh->flush(); |
| 148 |
|
| 149 |
} else { |
| 150 |
eval { server_wait($config_file, $rfh) }; |
| 151 |
if ($@) { |
| 152 |
warn($@); |
| 153 |
exit 1; |
| 154 |
} |
| 155 |
|
| 156 |
exit 0; |
| 157 |
} |
| 158 |
|
| 159 |
# Stop server |
| 160 |
server_stop($pid_file); |
| 161 |
|
| 162 |
$self->assert_child_ok($pid); |
| 163 |
|
| 164 |
if ($ex) { |
| 165 |
test_append_logfile($log_file, $ex); |
| 166 |
unlink($log_file); |
| 167 |
|
| 168 |
die($ex); |
| 169 |
} |
| 170 |
|
| 171 |
unlink($log_file); |
| 172 |
} |
| 173 |
|
| 174 |
1; |