Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::RLimitCPU;
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 rlimitcpu_max => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 rlimitcpu_session_max => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 rlimitcpu_session_min => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 rlimitcpu_daemon_max => {
34 order => ++$order,
35 test_class => [qw(forking)],
36 },
37
38 rlimitcpu_daemon_min => {
39 order => ++$order,
40 test_class => [qw(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 rlimitcpu_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 RLimitCPU => '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 rlimitcpu_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 RLimitCPU => '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 rlimitcpu_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 RLimitCPU => 'session 0',
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 eval { ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1) };
341 unless ($@) {
342 die("Connecting to server succeeded unexpectedly");
343 }
344 };
345
346 if ($@) {
347 $ex = $@;
348 }
349
350 $wfh->print("done\n");
351 $wfh->flush();
352
353 } else {
354 eval { server_wait($config_file, $rfh) };
355 if ($@) {
356 warn($@);
357 exit 1;
358 }
359
360 exit 0;
361 }
362
363 if ($ex) {
364 test_append_logfile($log_file, $ex);
365 unlink($log_file);
366
367 die($ex);
368 }
369
370 unlink($log_file);
371 }
372
373 sub rlimitcpu_daemon_max {
374 my $self = shift;
375 my $tmpdir = $self->{tmpdir};
376
377 my $config_file = "$tmpdir/config.conf";
378 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
379 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
380
381 my $log_file = test_get_logfile();
382
383 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
384 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
385
386 my $user = 'proftpd';
387 my $passwd = 'test';
388 my $group = 'ftpd';
389 my $home_dir = File::Spec->rel2abs($tmpdir);
390 my $uid = 500;
391 my $gid = 500;
392
393 # Make sure that, if we're running as root, that the home directory has
394 # permissions/privs set for the account we create
395 if ($< == 0) {
396 unless (chmod(0755, $home_dir)) {
397 die("Can't set perms on $home_dir to 0755: $!");
398 }
399
400 unless (chown($uid, $gid, $home_dir)) {
401 die("Can't set owner of $home_dir to $uid/$gid: $!");
402 }
403 }
404
405 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
406 '/bin/bash');
407 auth_group_write($auth_group_file, $group, $gid, $user);
408
409 my $config = {
410 PidFile => $pid_file,
411 ScoreboardFile => $scoreboard_file,
412 SystemLog => $log_file,
413
414 AuthUserFile => $auth_user_file,
415 AuthGroupFile => $auth_group_file,
416 AuthOrder => 'mod_auth_file.c',
417
418 RLimitCPU => 'daemon max',
419
420 IfModules => {
421 'mod_delay.c' => {
422 DelayEngine => 'off',
423 },
424 },
425 };
426
427 my ($port, $config_user, $config_group) = config_write($config_file, $config);
428
429 # Open pipes, for use between the parent and child processes. Specifically,
430 # the child will indicate when it's done with its test by writing a message
431 # to the parent.
432 my ($rfh, $wfh);
433 unless (pipe($rfh, $wfh)) {
434 die("Can't open pipe: $!");
435 }
436
437 my $ex;
438
439 # Fork child
440 $self->handle_sigchld();
441 defined(my $pid = fork()) or die("Can't fork: $!");
442 if ($pid) {
443 eval {
444 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
445 $client->login($user, $passwd);
446 $client->quit();
447 };
448
449 if ($@) {
450 $ex = $@;
451 }
452
453 $wfh->print("done\n");
454 $wfh->flush();
455
456 } else {
457 eval { server_wait($config_file, $rfh) };
458 if ($@) {
459 warn($@);
460 exit 1;
461 }
462
463 exit 0;
464 }
465
466 # Stop server
467 server_stop($pid_file);
468
469 $self->assert_child_ok($pid);
470
471 if ($ex) {
472 test_append_logfile($log_file, $ex);
473 unlink($log_file);
474
475 die($ex);
476 }
477
478 unlink($log_file);
479 }
480
481 sub rlimitcpu_daemon_min {
482 my $self = shift;
483 my $tmpdir = $self->{tmpdir};
484
485 my $config_file = "$tmpdir/config.conf";
486 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
487 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
488
489 my $log_file = test_get_logfile();
490
491 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
492 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
493
494 my $user = 'proftpd';
495 my $passwd = 'test';
496 my $group = 'ftpd';
497 my $home_dir = File::Spec->rel2abs($tmpdir);
498 my $uid = 500;
499 my $gid = 500;
500
501 # Make sure that, if we're running as root, that the home directory has
502 # permissions/privs set for the account we create
503 if ($< == 0) {
504 unless (chmod(0755, $home_dir)) {
505 die("Can't set perms on $home_dir to 0755: $!");
506 }
507
508 unless (chown($uid, $gid, $home_dir)) {
509 die("Can't set owner of $home_dir to $uid/$gid: $!");
510 }
511 }
512
513 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
514 '/bin/bash');
515 auth_group_write($auth_group_file, $group, $gid, $user);
516
517 my $config = {
518 PidFile => $pid_file,
519 ScoreboardFile => $scoreboard_file,
520 SystemLog => $log_file,
521
522 AuthUserFile => $auth_user_file,
523 AuthGroupFile => $auth_group_file,
524 AuthOrder => 'mod_auth_file.c',
525
526 RLimitCPU => 'daemon 0',
527
528 IfModules => {
529 'mod_delay.c' => {
530 DelayEngine => 'off',
531 },
532 },
533 };
534
535 my ($port, $config_user, $config_group) = config_write($config_file, $config);
536
537 my $ex;
538
539 eval { server_start($config_file, $pid_file) };
540 unless ($@) {
541 # Stop server
542 server_stop($pid_file);
543
544 $ex = "Server started up successfully unexpectedly";
545 }
546
547 if ($ex) {
548 test_append_logfile($log_file, $ex);
549 unlink($log_file);
550
551 die($ex);
552 }
553
554 unlink($log_file);
555 }
556
557 1;