Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_shaper.pm

raw
1 package ProFTPD::Tests::Modules::mod_shaper;
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 shaper_sighup => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 shaper_queue_dos => {
24 order => ++$order,
25 test_class => [qw(bug forking)],
26 },
27
28 # Enable this once Bug#3928 is fixed
29 # shaper_resumed_download_bug3928 => {
30 # order => ++$order,
31 # test_class => [qw(bug forking)],
32 # }
33
34 shaper_sighup_shaperlog_bug4077 => {
35 order => ++$order,
36 test_class => [qw(bug forking os_linux)],
37 },
38
39 };
40
41 sub new {
42 return shift()->SUPER::new(@_);
43 }
44
45 sub list_tests {
46 return testsuite_get_runnable_tests($TESTS);
47 }
48
49 sub get_server_pid {
50 my $pid_file = shift;
51
52 my $pid;
53 if (open(my $fh, "< $pid_file")) {
54 $pid = <$fh>;
55 chomp($pid);
56 close($fh);
57
58 } else {
59 croak("Can't read $pid_file: $!");
60 }
61
62 return $pid;
63 }
64
65 sub server_open_fds {
66 my $pid_file = shift;
67
68 my $pid = get_server_pid($pid_file);
69
70 my $proc_dir = "/proc/$pid/fd";
71 if (opendir(my $dirh, $proc_dir)) {
72 my $count = 0;
73
74 # Only count entries whose names are numbers
75 while (my $dent = readdir($dirh)) {
76 if ($dent =~ /^\d+$/) {
77 $count++;
78 }
79 }
80
81 closedir($dirh);
82 return $count;
83
84 } else {
85 croak("Can't open directory '$proc_dir': $!");
86 }
87 }
88
89 sub shaper_sighup {
90 my $self = shift;
91 my $tmpdir = $self->{tmpdir};
92
93 my $config_file = "$tmpdir/shaper.conf";
94 my $pid_file = File::Spec->rel2abs("$tmpdir/shaper.pid");
95 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/shaper.scoreboard");
96
97 my $log_file = test_get_logfile();
98
99 my $shaper_tab = File::Spec->rel2abs("$tmpdir/shaper.tab");
100
101 my $auth_user_file = File::Spec->rel2abs("$tmpdir/shaper.passwd");
102 my $auth_group_file = File::Spec->rel2abs("$tmpdir/shaper.group");
103
104 my $user = 'proftpd';
105 my $passwd = 'test';
106 my $group = 'ftpd';
107 my $home_dir = File::Spec->rel2abs($tmpdir);
108 my $uid = 500;
109 my $gid = 500;
110
111 # Make sure that, if we're running as root, that the home directory has
112 # permissions/privs set for the account we create
113 if ($< == 0) {
114 unless (chmod(0755, $home_dir)) {
115 die("Can't set perms on $home_dir to 0755: $!");
116 }
117
118 unless (chown($uid, $gid, $home_dir)) {
119 die("Can't set owner of $home_dir to $uid/$gid: $!");
120 }
121 }
122
123 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
124 '/bin/bash');
125 auth_group_write($auth_group_file, $group, $gid, $user);
126
127 my $config = {
128 PidFile => $pid_file,
129 ScoreboardFile => $scoreboard_file,
130 SystemLog => $log_file,
131
132 AuthUserFile => $auth_user_file,
133 AuthGroupFile => $auth_group_file,
134 AuthOrder => 'mod_auth_file.c',
135
136 IfModules => {
137 'mod_shaper.c' => {
138 ShaperEngine => 'on',
139 ShaperLog => $log_file,
140 ShaperTable => $shaper_tab,
141 ShaperAll => 'downrate 1500 uprate 1500',
142 },
143
144 'mod_delay.c' => {
145 DelayEngine => 'off',
146 },
147 },
148 };
149
150 my ($port, $config_user, $config_group) = config_write($config_file, $config);
151
152 # First, start the server.
153 server_start($config_file);
154
155 # Give it a second to start up, then send the SIGHUP signal
156 sleep(2);
157 server_restart($pid_file);
158
159 # Finally, stop the server
160 server_stop($pid_file);
161
162 unlink($log_file);
163 }
164
165 sub shaper_queue_dos {
166 my $self = shift;
167 my $tmpdir = $self->{tmpdir};
168
169 my $config_file = "$tmpdir/shaper.conf";
170 my $pid_file = File::Spec->rel2abs("$tmpdir/shaper.pid");
171 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/shaper.scoreboard");
172
173 my $log_file = test_get_logfile();
174
175 my $shaper_tab = File::Spec->rel2abs("$tmpdir/shaper.tab");
176
177 my $auth_user_file = File::Spec->rel2abs("$tmpdir/shaper.passwd");
178 my $auth_group_file = File::Spec->rel2abs("$tmpdir/shaper.group");
179
180 my $user = 'proftpd';
181 my $passwd = 'test';
182 my $group = 'ftpd';
183 my $home_dir = File::Spec->rel2abs($tmpdir);
184 my $uid = 500;
185 my $gid = 500;
186
187 # Make sure that, if we're running as root, that the home directory has
188 # permissions/privs set for the account we create
189 if ($< == 0) {
190 unless (chmod(0755, $home_dir)) {
191 die("Can't set perms on $home_dir to 0755: $!");
192 }
193
194 unless (chown($uid, $gid, $home_dir)) {
195 die("Can't set owner of $home_dir to $uid/$gid: $!");
196 }
197 }
198
199 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
200 '/bin/bash');
201 auth_group_write($auth_group_file, $group, $gid, $user);
202
203 my $test_count = 3000;
204 my $test_timeout = 180;
205
206 my $config = {
207 PidFile => $pid_file,
208 ScoreboardFile => $scoreboard_file,
209 SystemLog => $log_file,
210
211 AuthUserFile => $auth_user_file,
212 AuthGroupFile => $auth_group_file,
213 AuthOrder => 'mod_auth_file.c',
214
215 TimeoutIdle => $test_timeout + 10,
216
217 IfModules => {
218 'mod_shaper.c' => {
219 ShaperEngine => 'on',
220 ShaperLog => $log_file,
221 ShaperTable => $shaper_tab,
222 ShaperAll => 'downrate 1500 uprate 1500',
223 },
224
225 'mod_delay.c' => {
226 DelayEngine => 'off',
227 },
228 },
229 };
230
231 my ($port, $config_user, $config_group) = config_write($config_file, $config);
232
233 # Open pipes, for use between the parent and child processes. Specifically,
234 # the child will indicate when it's done with its test by writing a message
235 # to the parent.
236 my ($rfh, $wfh);
237 unless (pipe($rfh, $wfh)) {
238 die("Can't open pipe: $!");
239 }
240
241 my $ex;
242
243 # Fork child
244 $self->handle_sigchld();
245 defined(my $pid = fork()) or die("Can't fork: $!");
246 if ($pid) {
247 eval {
248 # XXX Test how many connections it takes to fill up the msg queue used
249 # by mod_shaper.
250
251 my $clients = [];
252 for (my $i = 0; $i < $test_count; $i++) {
253 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
254 $client->login($user, $passwd);
255 push(@$clients, $client);
256 }
257
258 foreach my $client (@$clients) {
259 $client->quit();
260 }
261
262 $clients = undef;
263 };
264
265 if ($@) {
266 $ex = $@;
267 }
268
269 $wfh->print("done\n");
270 $wfh->flush();
271
272 } else {
273 eval { server_wait($config_file, $rfh, $test_timeout) };
274 if ($@) {
275 warn($@);
276 exit 1;
277 }
278
279 exit 0;
280 }
281
282 # Stop server
283 server_stop($pid_file);
284
285 $self->assert_child_ok($pid);
286
287 if ($ex) {
288 test_append_logfile($log_file, $ex);
289 unlink($log_file);
290
291 die($ex);
292 }
293
294 unlink($log_file);
295 }
296
297 sub shaper_resumed_download_bug3928 {
298 my $self = shift;
299 my $tmpdir = $self->{tmpdir};
300
301 my $config_file = "$tmpdir/shaper.conf";
302 my $pid_file = File::Spec->rel2abs("$tmpdir/shaper.pid");
303 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/shaper.scoreboard");
304
305 my $log_file = test_get_logfile();
306
307 my $shaper_tab = File::Spec->rel2abs("$tmpdir/shaper.tab");
308
309 my $auth_user_file = File::Spec->rel2abs("$tmpdir/shaper.passwd");
310 my $auth_group_file = File::Spec->rel2abs("$tmpdir/shaper.group");
311
312 my $user = 'proftpd';
313 my $passwd = 'test';
314 my $group = 'ftpd';
315 my $home_dir = File::Spec->rel2abs($tmpdir);
316 my $uid = 500;
317 my $gid = 500;
318
319 # Make sure that, if we're running as root, that the home directory has
320 # permissions/privs set for the account we create
321 if ($< == 0) {
322 unless (chmod(0755, $home_dir)) {
323 die("Can't set perms on $home_dir to 0755: $!");
324 }
325
326 unless (chown($uid, $gid, $home_dir)) {
327 die("Can't set owner of $home_dir to $uid/$gid: $!");
328 }
329 }
330
331 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
332 '/bin/bash');
333 auth_group_write($auth_group_file, $group, $gid, $user);
334
335 my $test_file = File::Spec->rel2abs("$tmpdir/test.dat");
336 if (open(my $fh, "> $test_file")) {
337 print $fh 'AbCd' x 8192;
338 unless (close($fh)) {
339 die("Can't write $test_file: $!");
340 }
341
342 } else {
343 die("Can't open $test_file: $!");
344 }
345
346 my $test_timeout = 45;
347
348 my $config = {
349 PidFile => $pid_file,
350 ScoreboardFile => $scoreboard_file,
351 SystemLog => $log_file,
352
353 AuthUserFile => $auth_user_file,
354 AuthGroupFile => $auth_group_file,
355 AuthOrder => 'mod_auth_file.c',
356
357 AllowRetrieveRestart => 'on',
358 TimeoutIdle => $test_timeout + 10,
359
360 IfModules => {
361 'mod_shaper.c' => {
362 ShaperEngine => 'on',
363 ShaperLog => $log_file,
364 ShaperTable => $shaper_tab,
365 ShaperAll => 'downrate 1 uprate 1500',
366 },
367
368 'mod_delay.c' => {
369 DelayEngine => 'off',
370 },
371 },
372 };
373
374 my ($port, $config_user, $config_group) = config_write($config_file, $config);
375
376 # Open pipes, for use between the parent and child processes. Specifically,
377 # the child will indicate when it's done with its test by writing a message
378 # to the parent.
379 my ($rfh, $wfh);
380 unless (pipe($rfh, $wfh)) {
381 die("Can't open pipe: $!");
382 }
383
384 my $ex;
385
386 # Fork child
387 $self->handle_sigchld();
388 defined(my $pid = fork()) or die("Can't fork: $!");
389 if ($pid) {
390 eval {
391 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
392 $client->login($user, $passwd);
393 $client->type('binary');
394
395 # Resume downloading this large file at 8K
396 $client->rest(8192);
397
398 my $conn = $client->retr_raw('test.dat');
399 unless ($conn) {
400 die("RETR test.dat failed: " . $client->response_code() . " " .
401 $client->response_msg());
402 }
403
404 my ($buf, $tmp);
405 while ($conn->read($tmp, 32768, 15)) {
406 $buf .= $tmp;
407 }
408
409 eval { $conn->close() };
410
411 my $resp_code = $client->response_code();
412 my $resp_msg = $client->response_msg();
413 $self->assert_transfer_ok($resp_code, $resp_msg);
414
415 $client->quit();
416 };
417
418 if ($@) {
419 $ex = $@;
420 }
421
422 $wfh->print("done\n");
423 $wfh->flush();
424
425 } else {
426 eval { server_wait($config_file, $rfh, $test_timeout) };
427 if ($@) {
428 warn($@);
429 exit 1;
430 }
431
432 exit 0;
433 }
434
435 # Stop server
436 server_stop($pid_file);
437
438 $self->assert_child_ok($pid);
439
440 if ($ex) {
441 test_append_logfile($log_file, $ex);
442 unlink($log_file);
443
444 die($ex);
445 }
446
447 unlink($log_file);
448 }
449
450 sub shaper_sighup_shaperlog_bug4077 {
451 my $self = shift;
452 my $tmpdir = $self->{tmpdir};
453
454 my $config_file = "$tmpdir/shaper.conf";
455 my $pid_file = File::Spec->rel2abs("$tmpdir/shaper.pid");
456 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/shaper.scoreboard");
457
458 my $log_file = test_get_logfile();
459
460 my $shaper_tab = File::Spec->rel2abs("$tmpdir/shaper.tab");
461
462 my $auth_user_file = File::Spec->rel2abs("$tmpdir/shaper.passwd");
463 my $auth_group_file = File::Spec->rel2abs("$tmpdir/shaper.group");
464
465 my $user = 'proftpd';
466 my $passwd = 'test';
467 my $group = 'ftpd';
468 my $home_dir = File::Spec->rel2abs($tmpdir);
469 my $uid = 500;
470 my $gid = 500;
471
472 # Make sure that, if we're running as root, that the home directory has
473 # permissions/privs set for the account we create
474 if ($< == 0) {
475 unless (chmod(0755, $home_dir)) {
476 die("Can't set perms on $home_dir to 0755: $!");
477 }
478
479 unless (chown($uid, $gid, $home_dir)) {
480 die("Can't set owner of $home_dir to $uid/$gid: $!");
481 }
482 }
483
484 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
485 '/bin/bash');
486 auth_group_write($auth_group_file, $group, $gid, $user);
487
488 my $config = {
489 PidFile => $pid_file,
490 ScoreboardFile => $scoreboard_file,
491 SystemLog => $log_file,
492
493 AuthUserFile => $auth_user_file,
494 AuthGroupFile => $auth_group_file,
495 AuthOrder => 'mod_auth_file.c',
496
497 IfModules => {
498 'mod_shaper.c' => {
499 ShaperEngine => 'on',
500 ShaperLog => $log_file,
501 ShaperTable => $shaper_tab,
502 ShaperAll => 'downrate 1500 uprate 1500',
503 },
504
505 'mod_delay.c' => {
506 DelayEngine => 'off',
507 },
508 },
509 };
510
511 my ($port, $config_user, $config_group) = config_write($config_file, $config);
512
513 # First, start the server.
514 server_start($config_file);
515
516 # Give it a second to start up...
517 sleep(2);
518
519 my $orig_nfds = server_open_fds($pid_file);
520 if ($ENV{TEST_VERBOSE}) {
521 print STDERR "Found $orig_nfds open fds after server startup\n";
522 }
523
524 # Now modify the config, removing the ShaperLog...
525 delete($config->{IfModules}->{'mod_shaper.c'}->{ShaperLog});
526
527 unlink($config_file);
528 ($port, $config_user, $config_group) = config_write($config_file, $config);
529
530 # ...send SIGHUP...
531 server_restart($pid_file);
532 sleep(2);
533
534 # ...and get the open fd count again (should be 1 less).
535 my $restart_nfds = server_open_fds($pid_file);
536 if ($ENV{TEST_VERBOSE}) {
537 print STDERR "Found $restart_nfds open fds after server restart #1\n";
538 }
539
540 # Finally, stop the server
541 server_stop($pid_file);
542
543 my $expected_nfds = $orig_nfds-1;
544 $self->assert($expected_nfds == $restart_nfds,
545 test_msg("Expected $expected_nfds open fds, found $restart_nfds"));
546
547 unlink($log_file);
548 }
549
550 1;