Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Utils/ftpwho.pm

raw
1 package ProFTPD::Tests::Utils::ftpwho;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Path qw(mkpath rmtree);
8 use File::Spec;
9 use IO::Handle;
10
11 use ProFTPD::TestSuite::FTP;
12 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
13
14 $| = 1;
15
16 my $order = 0;
17
18 my $TESTS = {
19 ftpwho_ok => {
20 order => ++$order,
21 test_class => [qw(forking)],
22 },
23
24 ftpwho_verbose_ok => {
25 order => ++$order,
26 test_class => [qw(forking)],
27 },
28
29 ftpwho_bug3714 => {
30 order => ++$order,
31 test_class => [qw(bug forking)],
32 },
33
34 };
35
36 sub new {
37 return shift()->SUPER::new(@_);
38 }
39
40 sub list_tests {
41 return testsuite_get_runnable_tests($TESTS);
42 }
43
44 sub ftpwho_wait_alarm {
45 # We merely need to interrupt the current process by throwing an exception.
46 # Messy, but it gets the job done (it's necessary, at least, on a
47 # Mac OSX 10.4 machine).
48 die();
49 }
50
51 sub ftpwho {
52 my $scoreboard_file = shift;
53 my $ftpwho_opts = shift;
54 $ftpwho_opts = '' unless defined($ftpwho_opts);
55 my $output_file = shift;
56 my $ftpwho_wait_timeout = shift;
57 $ftpwho_wait_timeout = 1 unless defined($ftpwho_wait_timeout);
58
59 my $ftpwho_bin;
60 if ($ENV{PROFTPD_TEST_PATH}) {
61 $ftpwho_bin = "$ENV{PROFTPD_TEST_PATH}/../ftpwho";
62
63 } else {
64 $ftpwho_bin = '../ftpwho';
65 }
66
67 my $cmd = "$ftpwho_bin -f $scoreboard_file $ftpwho_opts >$output_file 2>/dev/null &";
68
69 if ($ENV{TEST_VERBOSE}) {
70 print STDERR "Executing ftpwho: $cmd\n";
71 }
72
73 eval {
74 $SIG{ALRM} = \&ftpwho_wait_alarm;
75 alarm($ftpwho_wait_timeout);
76 system($cmd);
77 alarm(0);
78 $SIG{ALRM} = 'DEFAULT';
79 };
80 }
81
82 sub ftpwho_ok {
83 my $self = shift;
84 my $tmpdir = $self->{tmpdir};
85
86 my $config_file = "$tmpdir/ftpwho.conf";
87 my $pid_file = File::Spec->rel2abs("$tmpdir/ftpwho.pid");
88 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ftpwho.scoreboard");
89
90 my $log_file = File::Spec->rel2abs('tests.log');
91
92 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
93 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
94
95 my $user = 'proftpd';
96 my $passwd = 'test';
97 my $group = 'ftpd';
98 my $home_dir = File::Spec->rel2abs($tmpdir);
99 my $uid = 500;
100 my $gid = 500;
101
102 # Make sure that, if we're running as root, that the home directory has
103 # permissions/privs set for the account we create
104 if ($< == 0) {
105 unless (chmod(0755, $home_dir)) {
106 die("Can't set perms on $home_dir to 0755: $!");
107 }
108
109 unless (chown($uid, $gid, $home_dir)) {
110 die("Can't set owner of $home_dir to $uid/$gid: $!");
111 }
112 }
113
114 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
115 '/bin/bash');
116 auth_group_write($auth_group_file, $group, $gid, $user);
117
118 my $ftpwho_file = File::Spec->rel2abs("$tmpdir/ftpwho.txt");
119
120 my $config = {
121 PidFile => $pid_file,
122 ScoreboardFile => $scoreboard_file,
123 SystemLog => $log_file,
124
125 AuthUserFile => $auth_user_file,
126 AuthGroupFile => $auth_group_file,
127 AuthOrder => 'mod_auth_file.c',
128
129 IfModules => {
130 'mod_delay.c' => {
131 DelayEngine => 'off',
132 },
133 },
134 };
135
136 my ($port, $config_user, $config_group) = config_write($config_file, $config);
137
138 # Open pipes, for use between the parent and child processes. Specifically,
139 # the child will indicate when it's done with its test by writing a message
140 # to the parent.
141 my ($rfh, $wfh);
142 unless (pipe($rfh, $wfh)) {
143 die("Can't open pipe: $!");
144 }
145
146 my $ex;
147
148 # Fork child
149 $self->handle_sigchld();
150 defined(my $pid = fork()) or die("Can't fork: $!");
151 if ($pid) {
152 eval {
153 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
154 $client->login($user, $passwd);
155
156 # Before we stop the server, use ftpwho to read the scoreboard
157 ftpwho($scoreboard_file, '', $ftpwho_file);
158
159 $client->quit();
160 };
161
162 if ($@) {
163 $ex = $@;
164 }
165
166 $wfh->print("done\n");
167 $wfh->flush();
168
169 } else {
170 eval { server_wait($config_file, $rfh) };
171 if ($@) {
172 warn($@);
173 exit 1;
174 }
175
176 exit 0;
177 }
178
179 # Stop server
180 server_stop($pid_file);
181
182 $self->assert_child_ok($pid);
183
184 if ($ex) {
185 die($ex);
186 }
187
188 my $lines = [];
189 if (open(my $fh, "< $ftpwho_file")) {
190 while (my $line = <$fh>) {
191 chomp($line);
192 push(@$lines, $line);
193 }
194
195 close($fh);
196
197 } else {
198 die("Can't read $ftpwho_file: $!");
199 }
200
201 my $expected = 3;
202
203 my $count = scalar(@$lines);
204 $self->assert($expected == $count,
205 test_msg("Expected $expected, got $count"));
206
207 my $ok = 0;
208 foreach my $line (@$lines) {
209 chomp($line);
210
211 if ($line =~ /^Service class\s+\-\s+(\d+) user/) {
212 my $user_count = $1;
213
214 $expected = 1;
215 $self->assert($expected eq $user_count,
216 test_msg("Expected '$expected', got '$user_count'"));
217
218 $ok = 1;
219 }
220 }
221
222 unless ($ok) {
223 die("Unexpected ftpwho output (expected user count not found)");
224 }
225
226 unlink($log_file);
227 }
228
229 sub ftpwho_verbose_ok {
230 my $self = shift;
231 my $tmpdir = $self->{tmpdir};
232
233 my $config_file = "$tmpdir/ftpwho.conf";
234 my $pid_file = File::Spec->rel2abs("$tmpdir/ftpwho.pid");
235 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ftpwho.scoreboard");
236
237 my $log_file = File::Spec->rel2abs('tests.log');
238
239 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
240 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
241
242 my $user = 'proftpd';
243 my $passwd = 'test';
244 my $group = 'ftpd';
245 my $home_dir = File::Spec->rel2abs($tmpdir);
246 my $uid = 500;
247 my $gid = 500;
248
249 # Make sure that, if we're running as root, that the home directory has
250 # permissions/privs set for the account we create
251 if ($< == 0) {
252 unless (chmod(0755, $home_dir)) {
253 die("Can't set perms on $home_dir to 0755: $!");
254 }
255
256 unless (chown($uid, $gid, $home_dir)) {
257 die("Can't set owner of $home_dir to $uid/$gid: $!");
258 }
259 }
260
261 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
262 '/bin/bash');
263 auth_group_write($auth_group_file, $group, $gid, $user);
264
265 my $ftpwho_file = File::Spec->rel2abs("$tmpdir/ftpwho.txt");
266
267 my $config = {
268 PidFile => $pid_file,
269 ScoreboardFile => $scoreboard_file,
270 SystemLog => $log_file,
271
272 AuthUserFile => $auth_user_file,
273 AuthGroupFile => $auth_group_file,
274 AuthOrder => 'mod_auth_file.c',
275
276 IfModules => {
277 'mod_delay.c' => {
278 DelayEngine => 'off',
279 },
280 },
281 };
282
283 my ($port, $config_user, $config_group) = config_write($config_file, $config);
284
285 # Open pipes, for use between the parent and child processes. Specifically,
286 # the child will indicate when it's done with its test by writing a message
287 # to the parent.
288 my ($rfh, $wfh);
289 unless (pipe($rfh, $wfh)) {
290 die("Can't open pipe: $!");
291 }
292
293 my $ex;
294
295 # Fork child
296 $self->handle_sigchld();
297 defined(my $pid = fork()) or die("Can't fork: $!");
298 if ($pid) {
299 eval {
300 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
301 $client->login($user, $passwd);
302
303 # Before we stop the server, use ftpwho to read the scoreboard
304 ftpwho($scoreboard_file, '-v', $ftpwho_file);
305
306 $client->quit();
307 };
308
309 if ($@) {
310 $ex = $@;
311 }
312
313 $wfh->print("done\n");
314 $wfh->flush();
315
316 } else {
317 eval { server_wait($config_file, $rfh) };
318 if ($@) {
319 warn($@);
320 exit 1;
321 }
322
323 exit 0;
324 }
325
326 # Stop server
327 server_stop($pid_file);
328
329 $self->assert_child_ok($pid);
330
331 if ($ex) {
332 die($ex);
333 }
334
335 my $lines = [];
336 if (open(my $fh, "< $ftpwho_file")) {
337 while (my $line = <$fh>) {
338 chomp($line);
339 push(@$lines, $line);
340 }
341
342 close($fh);
343
344 } else {
345 die("Can't read $ftpwho_file: $!");
346 }
347
348 my $expected = 8;
349
350 my $count = scalar(@$lines);
351 if ($ENV{TEST_VERBOSE}) {
352 if ($count != $expected) {
353 print STDERR "Lines:\n";
354 for (my $i = 0; $i < $count; $i++) {
355 print STDERR " $i: $lines->[$i]\n";
356 }
357 }
358 }
359
360 $self->assert($expected == $count,
361 test_msg("Expected $expected, got $count"));
362
363 my $ok = 0;
364 foreach my $line (@$lines) {
365 chomp($line);
366
367 if ($line =~ /^Service class\s+\-\s+(\d+) user/) {
368 my $user_count = $1;
369
370 $expected = 1;
371 $self->assert($expected eq $user_count,
372 test_msg("Expected '$expected', got '$user_count'"));
373
374 $ok = 1;
375 }
376 }
377
378 unless ($ok) {
379 die("Unexpected ftpwho output (expected user count not found)");
380 }
381
382 unlink($log_file);
383 }
384
385 sub ftpwho_bug3714 {
386 my $self = shift;
387 my $tmpdir = $self->{tmpdir};
388
389 my $config_file = "$tmpdir/ftpwho.conf";
390 my $pid_file = File::Spec->rel2abs("$tmpdir/ftpwho.pid");
391 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ftpwho.scoreboard");
392
393 my $log_file = File::Spec->rel2abs('tests.log');
394
395 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
396 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
397
398 my $user = 'proftpd';
399 my $passwd = 'test';
400 my $group = 'ftpd';
401 my $home_dir = File::Spec->rel2abs($tmpdir);
402 my $uid = 500;
403 my $gid = 500;
404
405 # Make sure that, if we're running as root, that the home directory has
406 # permissions/privs set for the account we create
407 if ($< == 0) {
408 unless (chmod(0755, $home_dir)) {
409 die("Can't set perms on $home_dir to 0755: $!");
410 }
411
412 unless (chown($uid, $gid, $home_dir)) {
413 die("Can't set owner of $home_dir to $uid/$gid: $!");
414 }
415 }
416
417 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
418 '/bin/bash');
419 auth_group_write($auth_group_file, $group, $gid, $user);
420
421 my $ftpwho_file = File::Spec->rel2abs("$tmpdir/ftpwho.txt");
422
423 my $config = {
424 PidFile => $pid_file,
425 ScoreboardFile => $scoreboard_file,
426 SystemLog => $log_file,
427
428 AuthUserFile => $auth_user_file,
429 AuthGroupFile => $auth_group_file,
430 AuthOrder => 'mod_auth_file.c',
431
432 IfModules => {
433 'mod_delay.c' => {
434 DelayEngine => 'off',
435 },
436 },
437 };
438
439 my ($port, $config_user, $config_group) = config_write($config_file, $config);
440
441 # Open pipes, for use between the parent and child processes. Specifically,
442 # the child will indicate when it's done with its test by writing a message
443 # to the parent.
444 my ($rfh, $wfh);
445 unless (pipe($rfh, $wfh)) {
446 die("Can't open pipe: $!");
447 }
448
449 my $ex;
450
451 # Fork child
452 $self->handle_sigchld();
453 defined(my $pid = fork()) or die("Can't fork: $!");
454 if ($pid) {
455 eval {
456 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
457 $client->login($user, $passwd);
458
459 # Start a download
460 my $conn = $client->retr_raw($config_file);
461 unless ($conn) {
462 die("RETR $config_file failed: " . $client->response_code() . " " .
463 $client->response_msg());
464 }
465
466 # Before we stop the server, use ftpwho to read the scoreboard
467 ftpwho($scoreboard_file, '', $ftpwho_file);
468
469 eval { $conn->close() };
470 $client->quit();
471 };
472
473 if ($@) {
474 $ex = $@;
475 }
476
477 $wfh->print("done\n");
478 $wfh->flush();
479
480 } else {
481 eval { server_wait($config_file, $rfh) };
482 if ($@) {
483 warn($@);
484 exit 1;
485 }
486
487 exit 0;
488 }
489
490 # Stop server
491 server_stop($pid_file);
492
493 $self->assert_child_ok($pid);
494
495 if ($ex) {
496 die($ex);
497 }
498
499 my $lines = [];
500 if (open(my $fh, "< $ftpwho_file")) {
501 while (my $line = <$fh>) {
502 chomp($line);
503 push(@$lines, $line);
504 }
505
506 close($fh);
507
508 } else {
509 die("Can't read $ftpwho_file: $!");
510 }
511
512 my $expected = 3;
513
514 my $count = scalar(@$lines);
515 $self->assert($expected == $count,
516 test_msg("Expected $expected, got $count"));
517
518 my $cmd_ok = 0;
519 my $user_ok = 0;
520 foreach my $line (@$lines) {
521 chomp($line);
522
523 if ($ENV{TEST_VERBOSE}) {
524 print STDERR "# line: $line\n";
525 }
526
527 if ($line =~ /RETR (\S+)$/) {
528 $cmd_ok = 1;
529 }
530
531 if ($line =~ /^Service class\s+\-\s+(\d+) user/) {
532 my $user_count = $1;
533
534 $expected = 1;
535 $self->assert($expected eq $user_count,
536 test_msg("Expected '$expected', got '$user_count'"));
537
538 $user_ok = 1;
539 }
540 }
541
542 $self->assert($cmd_ok,
543 test_msg("Unexpected ftpwho output (expected command not found)"));
544
545 $self->assert($user_ok,
546 test_msg("Unexpected ftpwho output (expected user count not found)"));
547
548 unlink($log_file);
549 }
550
551 1;