Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::RLimitOpenFiles;
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 rlimitopenfiles_max => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 rlimitopenfiles_session_max => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 rlimitopenfiles_session_min => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 rlimitopenfiles_daemon_max => {
34 order => ++$order,
35 test_class => [qw(bug forking)],
36 },
37
38 rlimitopenfiles_daemon_min => {
39 order => ++$order,
40 test_class => [qw(bug forking)],
41 },
42
43 };
44
45 sub new {
46 return shift()->SUPER::new(@_);
47 }
48
49 sub list_tests {
50 return testsuite_get_runnable_tests($TESTS);
51 }
52
53 sub rlimitopenfiles_max {
54 my $self = shift;
55 my $tmpdir = $self->{tmpdir};
56
57 my $config_file = "$tmpdir/config.conf";
58 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
59 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
60
61 my $log_file = test_get_logfile();
62
63 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
64 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
65
66 my $user = 'proftpd';
67 my $passwd = 'test';
68 my $group = 'ftpd';
69 my $home_dir = File::Spec->rel2abs($tmpdir);
70 my $uid = 500;
71 my $gid = 500;
72
73 # Make sure that, if we're running as root, that the home directory has
74 # permissions/privs set for the account we create
75 if ($< == 0) {
76 unless (chmod(0755, $home_dir)) {
77 die("Can't set perms on $home_dir to 0755: $!");
78 }
79
80 unless (chown($uid, $gid, $home_dir)) {
81 die("Can't set owner of $home_dir to $uid/$gid: $!");
82 }
83 }
84
85 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
86 '/bin/bash');
87 auth_group_write($auth_group_file, $group, $gid, $user);
88
89 my $config = {
90 PidFile => $pid_file,
91 ScoreboardFile => $scoreboard_file,
92 SystemLog => $log_file,
93
94 AuthUserFile => $auth_user_file,
95 AuthGroupFile => $auth_group_file,
96 AuthOrder => 'mod_auth_file.c',
97
98 RLimitOpenFiles => 'max',
99
100 IfModules => {
101 'mod_delay.c' => {
102 DelayEngine => 'off',
103 },
104 },
105 };
106
107 my ($port, $config_user, $config_group) = config_write($config_file, $config);
108
109 # Open pipes, for use between the parent and child processes. Specifically,
110 # the child will indicate when it's done with its test by writing a message
111 # to the parent.
112 my ($rfh, $wfh);
113 unless (pipe($rfh, $wfh)) {
114 die("Can't open pipe: $!");
115 }
116
117 my $ex;
118
119 # Fork child
120 $self->handle_sigchld();
121 defined(my $pid = fork()) or die("Can't fork: $!");
122 if ($pid) {
123 eval {
124 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
125 $client->login($user, $passwd);
126 $client->quit();
127 };
128
129 if ($@) {
130 $ex = $@;
131 }
132
133 $wfh->print("done\n");
134 $wfh->flush();
135
136 } else {
137 eval { server_wait($config_file, $rfh) };
138 if ($@) {
139 warn($@);
140 exit 1;
141 }
142
143 exit 0;
144 }
145
146 # Stop server
147 server_stop($pid_file);
148
149 $self->assert_child_ok($pid);
150
151 if ($ex) {
152 test_append_logfile($log_file, $ex);
153 unlink($log_file);
154
155 die($ex);
156 }
157
158 unlink($log_file);
159 }
160
161 sub rlimitopenfiles_session_max {
162 my $self = shift;
163 my $tmpdir = $self->{tmpdir};
164
165 my $config_file = "$tmpdir/config.conf";
166 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
167 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
168
169 my $log_file = test_get_logfile();
170
171 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
172 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
173
174 my $user = 'proftpd';
175 my $passwd = 'test';
176 my $group = 'ftpd';
177 my $home_dir = File::Spec->rel2abs($tmpdir);
178 my $uid = 500;
179 my $gid = 500;
180
181 # Make sure that, if we're running as root, that the home directory has
182 # permissions/privs set for the account we create
183 if ($< == 0) {
184 unless (chmod(0755, $home_dir)) {
185 die("Can't set perms on $home_dir to 0755: $!");
186 }
187
188 unless (chown($uid, $gid, $home_dir)) {
189 die("Can't set owner of $home_dir to $uid/$gid: $!");
190 }
191 }
192
193 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
194 '/bin/bash');
195 auth_group_write($auth_group_file, $group, $gid, $user);
196
197 my $config = {
198 PidFile => $pid_file,
199 ScoreboardFile => $scoreboard_file,
200 SystemLog => $log_file,
201
202 AuthUserFile => $auth_user_file,
203 AuthGroupFile => $auth_group_file,
204 AuthOrder => 'mod_auth_file.c',
205
206 RLimitOpenFiles => 'session max',
207
208 IfModules => {
209 'mod_delay.c' => {
210 DelayEngine => 'off',
211 },
212 },
213 };
214
215 my ($port, $config_user, $config_group) = config_write($config_file, $config);
216
217 # Open pipes, for use between the parent and child processes. Specifically,
218 # the child will indicate when it's done with its test by writing a message
219 # to the parent.
220 my ($rfh, $wfh);
221 unless (pipe($rfh, $wfh)) {
222 die("Can't open pipe: $!");
223 }
224
225 my $ex;
226
227 # Fork child
228 $self->handle_sigchld();
229 defined(my $pid = fork()) or die("Can't fork: $!");
230 if ($pid) {
231 eval {
232 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
233 $client->login($user, $passwd);
234 $client->quit();
235 };
236
237 if ($@) {
238 $ex = $@;
239 }
240
241 $wfh->print("done\n");
242 $wfh->flush();
243
244 } else {
245 eval { server_wait($config_file, $rfh) };
246 if ($@) {
247 warn($@);
248 exit 1;
249 }
250
251 exit 0;
252 }
253
254 # Stop server
255 server_stop($pid_file);
256
257 $self->assert_child_ok($pid);
258
259 if ($ex) {
260 test_append_logfile($log_file, $ex);
261 unlink($log_file);
262
263 die($ex);
264 }
265
266 unlink($log_file);
267 }
268
269 sub rlimitopenfiles_session_min {
270 my $self = shift;
271 my $tmpdir = $self->{tmpdir};
272
273 my $config_file = "$tmpdir/config.conf";
274 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
275 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
276
277 my $log_file = test_get_logfile();
278
279 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
280 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
281
282 my $user = 'proftpd';
283 my $passwd = 'test';
284 my $group = 'ftpd';
285 my $home_dir = File::Spec->rel2abs($tmpdir);
286 my $uid = 500;
287 my $gid = 500;
288
289 # Make sure that, if we're running as root, that the home directory has
290 # permissions/privs set for the account we create
291 if ($< == 0) {
292 unless (chmod(0755, $home_dir)) {
293 die("Can't set perms on $home_dir to 0755: $!");
294 }
295
296 unless (chown($uid, $gid, $home_dir)) {
297 die("Can't set owner of $home_dir to $uid/$gid: $!");
298 }
299 }
300
301 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
302 '/bin/bash');
303 auth_group_write($auth_group_file, $group, $gid, $user);
304
305 my $config = {
306 PidFile => $pid_file,
307 ScoreboardFile => $scoreboard_file,
308 SystemLog => $log_file,
309
310 AuthUserFile => $auth_user_file,
311 AuthGroupFile => $auth_group_file,
312 AuthOrder => 'mod_auth_file.c',
313
314 RLimitOpenFiles => 'session 6',
315
316 IfModules => {
317 'mod_delay.c' => {
318 DelayEngine => 'off',
319 },
320 },
321 };
322
323 my ($port, $config_user, $config_group) = config_write($config_file, $config);
324
325 # Open pipes, for use between the parent and child processes. Specifically,
326 # the child will indicate when it's done with its test by writing a message
327 # to the parent.
328 my ($rfh, $wfh);
329 unless (pipe($rfh, $wfh)) {
330 die("Can't open pipe: $!");
331 }
332
333 my $ex;
334
335 # Fork child
336 $self->handle_sigchld();
337 defined(my $pid = fork()) or die("Can't fork: $!");
338 if ($pid) {
339 eval {
340 # We allotted enough fds to allow client to connect, but NOT to login
341 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
342 eval { $client->login($user, $passwd) };
343 unless ($@) {
344 die("Login succeeded unexpectedly");
345 }
346
347 $client->quit();
348 };
349
350 if ($@) {
351 $ex = $@;
352 }
353
354 $wfh->print("done\n");
355 $wfh->flush();
356
357 } else {
358 eval { server_wait($config_file, $rfh) };
359 if ($@) {
360 warn($@);
361 exit 1;
362 }
363
364 exit 0;
365 }
366
367 # Stop server
368 server_stop($pid_file);
369
370 $self->assert_child_ok($pid);
371
372 if ($ex) {
373 test_append_logfile($log_file, $ex);
374 unlink($log_file);
375
376 die($ex);
377 }
378
379 unlink($log_file);
380 }
381
382 sub rlimitopenfiles_daemon_max {
383 my $self = shift;
384 my $tmpdir = $self->{tmpdir};
385
386 my $config_file = "$tmpdir/config.conf";
387 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
388 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
389
390 my $log_file = test_get_logfile();
391
392 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
393 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
394
395 my $user = 'proftpd';
396 my $passwd = 'test';
397 my $group = 'ftpd';
398 my $home_dir = File::Spec->rel2abs($tmpdir);
399 my $uid = 500;
400 my $gid = 500;
401
402 # Make sure that, if we're running as root, that the home directory has
403 # permissions/privs set for the account we create
404 if ($< == 0) {
405 unless (chmod(0755, $home_dir)) {
406 die("Can't set perms on $home_dir to 0755: $!");
407 }
408
409 unless (chown($uid, $gid, $home_dir)) {
410 die("Can't set owner of $home_dir to $uid/$gid: $!");
411 }
412 }
413
414 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
415 '/bin/bash');
416 auth_group_write($auth_group_file, $group, $gid, $user);
417
418 my $config = {
419 PidFile => $pid_file,
420 ScoreboardFile => $scoreboard_file,
421 SystemLog => $log_file,
422
423 AuthUserFile => $auth_user_file,
424 AuthGroupFile => $auth_group_file,
425 AuthOrder => 'mod_auth_file.c',
426
427 RLimitOpenFiles => 'daemon max',
428
429 IfModules => {
430 'mod_delay.c' => {
431 DelayEngine => 'off',
432 },
433 },
434 };
435
436 my ($port, $config_user, $config_group) = config_write($config_file, $config);
437
438 # Open pipes, for use between the parent and child processes. Specifically,
439 # the child will indicate when it's done with its test by writing a message
440 # to the parent.
441 my ($rfh, $wfh);
442 unless (pipe($rfh, $wfh)) {
443 die("Can't open pipe: $!");
444 }
445
446 my $ex;
447
448 # Fork child
449 $self->handle_sigchld();
450 defined(my $pid = fork()) or die("Can't fork: $!");
451 if ($pid) {
452 eval {
453 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
454 $client->login($user, $passwd);
455 $client->quit();
456 };
457
458 if ($@) {
459 $ex = $@;
460 }
461
462 $wfh->print("done\n");
463 $wfh->flush();
464
465 } else {
466 eval { server_wait($config_file, $rfh) };
467 if ($@) {
468 warn($@);
469 exit 1;
470 }
471
472 exit 0;
473 }
474
475 # Stop server
476 server_stop($pid_file);
477
478 $self->assert_child_ok($pid);
479
480 if ($ex) {
481 test_append_logfile($log_file, $ex);
482 unlink($log_file);
483
484 die($ex);
485 }
486
487 unlink($log_file);
488 }
489
490 sub rlimitopenfiles_daemon_min {
491 my $self = shift;
492 my $tmpdir = $self->{tmpdir};
493
494 my $config_file = "$tmpdir/config.conf";
495 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
496 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
497
498 my $log_file = test_get_logfile();
499
500 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
501 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
502
503 my $user = 'proftpd';
504 my $passwd = 'test';
505 my $group = 'ftpd';
506 my $home_dir = File::Spec->rel2abs($tmpdir);
507 my $uid = 500;
508 my $gid = 500;
509
510 # Make sure that, if we're running as root, that the home directory has
511 # permissions/privs set for the account we create
512 if ($< == 0) {
513 unless (chmod(0755, $home_dir)) {
514 die("Can't set perms on $home_dir to 0755: $!");
515 }
516
517 unless (chown($uid, $gid, $home_dir)) {
518 die("Can't set owner of $home_dir to $uid/$gid: $!");
519 }
520 }
521
522 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
523 '/bin/bash');
524 auth_group_write($auth_group_file, $group, $gid, $user);
525
526 my $config = {
527 PidFile => $pid_file,
528 ScoreboardFile => $scoreboard_file,
529 SystemLog => $log_file,
530
531 AuthUserFile => $auth_user_file,
532 AuthGroupFile => $auth_group_file,
533 AuthOrder => 'mod_auth_file.c',
534
535 RLimitOpenFiles => 'daemon 3',
536
537 IfModules => {
538 'mod_delay.c' => {
539 DelayEngine => 'off',
540 },
541 },
542 };
543
544 my ($port, $config_user, $config_group) = config_write($config_file, $config);
545
546 # Open pipes, for use between the parent and child processes. Specifically,
547 # the child will indicate when it's done with its test by writing a message
548 # to the parent.
549 my ($rfh, $wfh);
550 unless (pipe($rfh, $wfh)) {
551 die("Can't open pipe: $!");
552 }
553
554 my $ex;
555
556 # Fork child
557 $self->handle_sigchld();
558 defined(my $pid = fork()) or die("Can't fork: $!");
559 if ($pid) {
560 eval {
561 # We allowed just enough fds for the server to start up, but not
562 # much else.
563 eval { ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1) };
564 unless ($@) {
565 die("Connecting to server succeeded unexpectedly");
566 }
567 };
568
569 if ($@) {
570 $ex = $@;
571 }
572
573 $wfh->print("done\n");
574 $wfh->flush();
575
576 } else {
577 eval { server_wait($config_file, $rfh) };
578 if ($@) {
579 warn($@);
580 exit 1;
581 }
582
583 exit 0;
584 }
585
586 # Stop server
587 server_stop($pid_file);
588
589 $self->assert_child_ok($pid);
590
591 if ($ex) {
592 test_append_logfile($log_file, $ex);
593 unlink($log_file);
594
595 die($ex);
596 }
597
598 # unlink($log_file);
599 }
600
601 1;