Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Config/MaxClientsPerHost.pm

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