| 1 |
package ProFTPD::Tests::Config::MaxClientsPerClass; |
| 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 |
maxclientsperclass_one => { |
| 19 |
order => ++$order, |
| 20 |
test_class => [qw(forking)], |
| 21 |
}, |
| 22 |
|
| 23 |
maxclientsperclass_one_multi_conns => { |
| 24 |
order => ++$order, |
| 25 |
test_class => [qw(forking)], |
| 26 |
}, |
| 27 |
|
| 28 |
}; |
| 29 |
|
| 30 |
sub new { |
| 31 |
return shift()->SUPER::new(@_); |
| 32 |
} |
| 33 |
|
| 34 |
sub list_tests { |
| 35 |
return testsuite_get_runnable_tests($TESTS); |
| 36 |
} |
| 37 |
|
| 38 |
sub maxclientsperclass_one { |
| 39 |
my $self = shift; |
| 40 |
my $tmpdir = $self->{tmpdir}; |
| 41 |
|
| 42 |
my $config_file = "$tmpdir/config.conf"; |
| 43 |
my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid"); |
| 44 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard"); |
| 45 |
|
| 46 |
my $log_file = test_get_logfile(); |
| 47 |
|
| 48 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd"); |
| 49 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group"); |
| 50 |
|
| 51 |
my $user = 'proftpd'; |
| 52 |
my $passwd = 'test'; |
| 53 |
my $group = 'ftpd'; |
| 54 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 55 |
my $uid = 500; |
| 56 |
my $gid = 500; |
| 57 |
|
| 58 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 59 |
'/bin/bash'); |
| 60 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 61 |
|
| 62 |
my $class_name = "test"; |
| 63 |
my $max_clients = 1; |
| 64 |
|
| 65 |
my $config = { |
| 66 |
PidFile => $pid_file, |
| 67 |
ScoreboardFile => $scoreboard_file, |
| 68 |
SystemLog => $log_file, |
| 69 |
|
| 70 |
AuthUserFile => $auth_user_file, |
| 71 |
AuthGroupFile => $auth_group_file, |
| 72 |
AuthOrder => 'mod_auth_file.c', |
| 73 |
|
| 74 |
MaxClientsPerClass => "$class_name $max_clients", |
| 75 |
|
| 76 |
IfModules => { |
| 77 |
'mod_delay.c' => { |
| 78 |
DelayEngine => 'off', |
| 79 |
}, |
| 80 |
}, |
| 81 |
}; |
| 82 |
|
| 83 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 84 |
|
| 85 |
if (open(my $fh, ">> $config_file")) { |
| 86 |
print $fh <<EOC; |
| 87 |
<Class $class_name> |
| 88 |
From all |
| 89 |
</Class> |
| 90 |
EOC |
| 91 |
|
| 92 |
unless (close($fh)) { |
| 93 |
die("Can't write $config_file: $!"); |
| 94 |
} |
| 95 |
|
| 96 |
} else { |
| 97 |
die("Can't open $config_file: $!"); |
| 98 |
} |
| 99 |
|
| 100 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 101 |
# the child will indicate when it's done with its test by writing a message |
| 102 |
# to the parent. |
| 103 |
my ($rfh, $wfh); |
| 104 |
unless (pipe($rfh, $wfh)) { |
| 105 |
die("Can't open pipe: $!"); |
| 106 |
} |
| 107 |
|
| 108 |
my $ex; |
| 109 |
|
| 110 |
# Fork child |
| 111 |
$self->handle_sigchld(); |
| 112 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 113 |
if ($pid) { |
| 114 |
eval { |
| 115 |
# First client should be able to connect and log in... |
| 116 |
my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 117 |
$client1->login($user, $passwd); |
| 118 |
|
| 119 |
# ...but the second client should be able to connect, but not login. |
| 120 |
my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 121 |
|
| 122 |
eval { $client2->login($user, $passwd) }; |
| 123 |
unless ($@) { |
| 124 |
die("Login succeeded unexpectedly"); |
| 125 |
} |
| 126 |
|
| 127 |
$client1->quit(); |
| 128 |
}; |
| 129 |
|
| 130 |
if ($@) { |
| 131 |
$ex = $@; |
| 132 |
} |
| 133 |
|
| 134 |
$wfh->print("done\n"); |
| 135 |
$wfh->flush(); |
| 136 |
|
| 137 |
} else { |
| 138 |
eval { server_wait($config_file, $rfh) }; |
| 139 |
if ($@) { |
| 140 |
warn($@); |
| 141 |
exit 1; |
| 142 |
} |
| 143 |
|
| 144 |
exit 0; |
| 145 |
} |
| 146 |
|
| 147 |
# Stop server |
| 148 |
server_stop($pid_file); |
| 149 |
|
| 150 |
$self->assert_child_ok($pid); |
| 151 |
|
| 152 |
if ($ex) { |
| 153 |
test_append_logfile($log_file, $ex); |
| 154 |
unlink($log_file); |
| 155 |
|
| 156 |
die($ex); |
| 157 |
} |
| 158 |
|
| 159 |
unlink($log_file); |
| 160 |
} |
| 161 |
|
| 162 |
sub maxclientsperclass_one_multi_conns { |
| 163 |
my $self = shift; |
| 164 |
my $tmpdir = $self->{tmpdir}; |
| 165 |
|
| 166 |
my $config_file = "$tmpdir/config.conf"; |
| 167 |
my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid"); |
| 168 |
my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard"); |
| 169 |
|
| 170 |
my $log_file = test_get_logfile(); |
| 171 |
|
| 172 |
my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd"); |
| 173 |
my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group"); |
| 174 |
|
| 175 |
my $user = 'proftpd'; |
| 176 |
my $passwd = 'test'; |
| 177 |
my $group = 'ftpd'; |
| 178 |
my $home_dir = File::Spec->rel2abs($tmpdir); |
| 179 |
my $uid = 500; |
| 180 |
my $gid = 500; |
| 181 |
|
| 182 |
auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
| 183 |
'/bin/bash'); |
| 184 |
auth_group_write($auth_group_file, $group, $gid, $user); |
| 185 |
|
| 186 |
my $class_name = "test"; |
| 187 |
my $max_clients = 1; |
| 188 |
|
| 189 |
my $config = { |
| 190 |
PidFile => $pid_file, |
| 191 |
ScoreboardFile => $scoreboard_file, |
| 192 |
SystemLog => $log_file, |
| 193 |
|
| 194 |
AuthUserFile => $auth_user_file, |
| 195 |
AuthGroupFile => $auth_group_file, |
| 196 |
AuthOrder => 'mod_auth_file.c', |
| 197 |
|
| 198 |
MaxClientsPerClass => "$class_name $max_clients", |
| 199 |
|
| 200 |
IfModules => { |
| 201 |
'mod_delay.c' => { |
| 202 |
DelayEngine => 'off', |
| 203 |
}, |
| 204 |
}, |
| 205 |
}; |
| 206 |
|
| 207 |
my ($port, $config_user, $config_group) = config_write($config_file, $config); |
| 208 |
|
| 209 |
if (open(my $fh, ">> $config_file")) { |
| 210 |
print $fh <<EOC; |
| 211 |
<Class $class_name> |
| 212 |
From all |
| 213 |
</Class> |
| 214 |
EOC |
| 215 |
|
| 216 |
unless (close($fh)) { |
| 217 |
die("Can't write $config_file: $!"); |
| 218 |
} |
| 219 |
|
| 220 |
} else { |
| 221 |
die("Can't open $config_file: $!"); |
| 222 |
} |
| 223 |
|
| 224 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 225 |
# the child will indicate when it's done with its test by writing a message |
| 226 |
# to the parent. |
| 227 |
my ($rfh, $wfh); |
| 228 |
unless (pipe($rfh, $wfh)) { |
| 229 |
die("Can't open pipe: $!"); |
| 230 |
} |
| 231 |
|
| 232 |
my $ex; |
| 233 |
|
| 234 |
# Fork child |
| 235 |
$self->handle_sigchld(); |
| 236 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 237 |
if ($pid) { |
| 238 |
eval { |
| 239 |
# First client should be able to connect and log in... |
| 240 |
my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 241 |
$client1->login($user, $passwd); |
| 242 |
|
| 243 |
# ...but the second client should be able to connect, but not login. |
| 244 |
my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 245 |
|
| 246 |
eval { $client2->login($user, $passwd) }; |
| 247 |
unless ($@) { |
| 248 |
die("Login succeeded unexpectedly"); |
| 249 |
} |
| 250 |
|
| 251 |
# Even though we can't log in, we should be able to connect quite |
| 252 |
# a few more times |
| 253 |
|
| 254 |
my $clients = []; |
| 255 |
for (my $i = 0; $i < 10; $i++) { |
| 256 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 257 |
push(@$clients, $client); |
| 258 |
} |
| 259 |
|
| 260 |
$client1->quit(); |
| 261 |
}; |
| 262 |
|
| 263 |
if ($@) { |
| 264 |
$ex = $@; |
| 265 |
} |
| 266 |
|
| 267 |
$wfh->print("done\n"); |
| 268 |
$wfh->flush(); |
| 269 |
|
| 270 |
} else { |
| 271 |
eval { server_wait($config_file, $rfh) }; |
| 272 |
if ($@) { |
| 273 |
warn($@); |
| 274 |
exit 1; |
| 275 |
} |
| 276 |
|
| 277 |
exit 0; |
| 278 |
} |
| 279 |
|
| 280 |
# Stop server |
| 281 |
server_stop($pid_file); |
| 282 |
|
| 283 |
$self->assert_child_ok($pid); |
| 284 |
|
| 285 |
if ($ex) { |
| 286 |
test_append_logfile($log_file, $ex); |
| 287 |
unlink($log_file); |
| 288 |
|
| 289 |
die($ex); |
| 290 |
} |
| 291 |
|
| 292 |
unlink($log_file); |
| 293 |
} |
| 294 |
|
| 295 |
1; |