Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Modules::mod_quotatab_file;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Copy;
8 use File::Path qw(mkpath);
9 use File::Spec;
10 use IO::Handle;
11
12 use ProFTPD::TestSuite::FTP;
13 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
14
15 $| = 1;
16
17 my $order = 0;
18
19 my $TESTS = {
20 quotatab_file_single_suppl_group => {
21 order => ++$order,
22 test_class => [qw(bug forking)],
23 },
24
25 quotatab_file_all_limit => {
26 order => ++$order,
27 test_class => [qw(forking)],
28 },
29
30 quotatab_file_bytes_download_zero => {
31 order => ++$order,
32 test_class => [qw(bug forking)],
33 },
34
35 };
36
37 sub new {
38 return shift()->SUPER::new(@_);
39 }
40
41 sub list_tests {
42 return testsuite_get_runnable_tests($TESTS);
43 }
44
45 sub set_up {
46 my $self = shift;
47 $self->SUPER::set_up(@_);
48
49 # Make copies of the original tables into our scratch directory
50 my ($src_file, $dst_file);
51
52 $src_file = File::Spec->rel2abs('t/etc/modules/mod_quotatab_file/ftpquota-group-limit.tab');
53 $dst_file = File::Spec->rel2abs("$self->{tmpdir}/ftpquota-group-limit.tab");
54
55 unless (copy($src_file, $dst_file)) {
56 die("Can't copy $src_file to $dst_file: $!");
57 }
58
59 $src_file = File::Spec->rel2abs('t/etc/modules/mod_quotatab_file/ftpquota-group-tally.tab');
60 $dst_file = File::Spec->rel2abs("$self->{tmpdir}/ftpquota-group-tally.tab");
61
62 unless (copy($src_file, $dst_file)) {
63 die("Can't copy $src_file to $dst_file: $!");
64 }
65
66 $src_file = File::Spec->rel2abs('t/etc/modules/mod_quotatab_file/ftpquota-all-limit.tab');
67 $dst_file = File::Spec->rel2abs("$self->{tmpdir}/ftpquota-all-limit.tab");
68
69 unless (copy($src_file, $dst_file)) {
70 die("Can't copy $src_file to $dst_file: $!");
71 }
72
73 $src_file = File::Spec->rel2abs('t/etc/modules/mod_quotatab_file/ftpquota-all-tally.tab');
74 $dst_file = File::Spec->rel2abs("$self->{tmpdir}/ftpquota-all-tally.tab");
75
76 unless (copy($src_file, $dst_file)) {
77 die("Can't copy $src_file to $dst_file: $!");
78 }
79 }
80
81 sub get_ftpquota_bin {
82 my $ftpquota_bin;
83
84 if ($ENV{PROFTPD_TEST_PATH}) {
85 $ftpquota_bin = File::Spec->rel2abs("$ENV{PROFTPD_TEST_PATH}/contrib/ftpquota");
86
87 } else {
88 $ftpquota_bin = File::Spec->rel2abs('../contrib/ftpquota');
89 }
90
91 return $ftpquota_bin;
92 }
93
94 sub get_tally {
95 my $tally_file = shift;
96 my $name = shift;
97 my $quota_type = shift;
98
99 my $ftpquota_bin = get_ftpquota_bin();
100
101 my $cmd;
102
103 if ($ENV{TEST_VERBOSE}) {
104 $cmd = "perl $ftpquota_bin --verbose --show-records --table-path=$tally_file --type=tally";
105
106 } else {
107 $cmd = "perl $ftpquota_bin --show-records --table-path=$tally_file --type=tally";
108 }
109
110 if ($ENV{TEST_VERBOSE}) {
111 print STDERR "Executing perl: $cmd\n";
112 }
113
114 my @res = `$cmd`;
115
116 if (scalar(@res) &&
117 $ENV{TEST_VERBOSE}) {
118 print STDERR "Output: ", join('', @res), "\n";
119 }
120
121 if ($? != 0) {
122 die("'$cmd' failed");
123 }
124
125 my ($bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used);
126
127 foreach my $line (@res) {
128 chomp($line);
129
130 if ($line =~ /Uploaded bytes:\s+(\S+)$/) {
131 $bytes_in_used = $1;
132
133 } elsif ($line =~ /Downloaded bytes:\s+(\S+)$/) {
134 $bytes_out_used = $1;
135
136 } elsif ($line =~ /Transferred bytes:\s+(\S+)$/) {
137 $bytes_xfer_used = $1;
138
139 } elsif ($line =~ /Uploaded files:\s+(\S+)$/) {
140 $files_in_used = $1;
141
142 } elsif ($line =~ /Downloaded files:\s+(\S+)$/) {
143 $files_out_used = $1;
144
145 } elsif ($line =~ /Transferred files:\s+(\S+)$/) {
146 $files_xfer_used = $1;
147 }
148 }
149
150 return ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used);
151 }
152
153 sub quotatab_file_single_suppl_group {
154 my $self = shift;
155 my $tmpdir = $self->{tmpdir};
156
157 my $config_file = "$tmpdir/quotatab.conf";
158 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
159 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
160
161 my $log_file = test_get_logfile();
162
163 my $auth_user_file = File::Spec->rel2abs("$tmpdir/quotatab.passwd");
164 my $auth_group_file = File::Spec->rel2abs("$tmpdir/quotatab.group");
165
166 my $user = 'proftpd';
167 my $passwd = 'test';
168 my $group = 'ftpd';
169 my $home_dir = File::Spec->rel2abs($tmpdir);
170 mkpath($home_dir);
171
172 my $uid = 500;
173 my $gid = 500;
174
175 # Make sure that, if we're running as root, that the home directories has
176 # permissions/privs set for the account we create
177 if ($< == 0) {
178 unless (chmod(0755, $home_dir)) {
179 die("Can't set perms on $home_dir to 0755: $!");
180 }
181
182 unless (chown($uid, $gid, $home_dir)) {
183 die("Can't set owner of $home_dir to $uid/$gid: $!");
184 }
185 }
186
187 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
188 '/bin/bash');
189 auth_group_write($auth_group_file, $group, $gid, $user);
190
191 auth_group_write($auth_group_file, 'test1', $gid+2, $user);
192
193 # Make sure that the group for whom there is a limit is NOT the user's
194 # primary group, but IS the user's only supplemental group.
195 auth_group_write($auth_group_file, 'test', $gid+1, $user);
196
197 my $limit_file = File::Spec->rel2abs("$tmpdir/ftpquota-group-limit.tab");
198 my $tally_file = File::Spec->rel2abs("$tmpdir/ftpquota-group-tally.tab");
199
200 my $config = {
201 PidFile => $pid_file,
202 ScoreboardFile => $scoreboard_file,
203 SystemLog => $log_file,
204
205 AuthUserFile => $auth_user_file,
206 AuthGroupFile => $auth_group_file,
207 AuthOrder => 'mod_auth_file.c',
208
209 DefaultChdir => '~',
210
211 IfModules => {
212 'mod_delay.c' => {
213 DelayEngine => 'off',
214 },
215
216 'mod_quotatab_file.c' => {
217 QuotaEngine => 'on',
218 QuotaLog => $log_file,
219 QuotaLimitTable => "file:$limit_file",
220 QuotaTallyTable => "file:$tally_file",
221 },
222 },
223 };
224
225 my ($port, $config_user, $config_group) = config_write($config_file, $config);
226
227 # Open pipes, for use between the parent and child processes. Specifically,
228 # the child will indicate when it's done with its test by writing a message
229 # to the parent.
230 my ($rfh, $wfh);
231 unless (pipe($rfh, $wfh)) {
232 die("Can't open pipe: $!");
233 }
234
235 my $ex;
236
237 # Fork child
238 $self->handle_sigchld();
239 defined(my $pid = fork()) or die("Can't fork: $!");
240 if ($pid) {
241 eval {
242 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
243
244 $client->login($user, $passwd);
245
246 my $conn = $client->stor_raw('test.txt');
247 unless ($conn) {
248 die("Failed to STOR test.txt: " . $client->response_code() . " " .
249 $client->response_msg());
250 }
251
252 my $buf = "Hello, World\n";
253 $conn->write($buf, length($buf), 25);
254 eval { $conn->close() };
255
256 my $resp_code = $client->response_code();
257 my $resp_msg = $client->response_msg();
258 $self->assert_transfer_ok($resp_code, $resp_msg);
259
260 $client->quit();
261 };
262
263 if ($@) {
264 $ex = $@;
265 }
266
267 $wfh->print("done\n");
268 $wfh->flush();
269
270 } else {
271 eval { server_wait($config_file, $rfh) };
272 if ($@) {
273 warn($@);
274 exit 1;
275 }
276
277 exit 0;
278 }
279
280 # Stop server
281 server_stop($pid_file);
282
283 $self->assert_child_ok($pid);
284
285 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($tally_file, 'test', 'group');
286
287 my $expected;
288
289 $expected = 'group';
290 $self->assert($expected eq $quota_type,
291 test_msg("Expected '$expected', got '$quota_type'"));
292
293 $expected = '^(13.0+|13)$';
294 $self->assert(qr/$expected/, $bytes_in_used,
295 test_msg("Expected $expected, got $bytes_in_used"));
296
297 $expected = '^(0.0+|0)$';
298 $self->assert(qr/$expected/, $bytes_out_used,
299 test_msg("Expected $expected, got $bytes_out_used"));
300
301 $expected = '^(0.0+|0)$';
302 $self->assert(qr/$expected/, $bytes_xfer_used,
303 test_msg("Expected $expected, got $bytes_xfer_used"));
304
305 $expected = 0;
306 $self->assert($expected == $files_in_used,
307 test_msg("Expected $expected, got $files_in_used"));
308
309 $expected = 0;
310 $self->assert($expected == $files_out_used,
311 test_msg("Expected $expected, got $files_out_used"));
312
313 $expected = 0;
314 $self->assert($expected == $files_xfer_used,
315 test_msg("Expected $expected, got $files_xfer_used"));
316
317 if ($ex) {
318 test_append_logfile($log_file, $ex);
319 unlink($log_file);
320
321 die($ex);
322 }
323
324 unlink($log_file);
325 }
326
327 sub quotatab_file_all_limit {
328 my $self = shift;
329 my $tmpdir = $self->{tmpdir};
330
331 my $config_file = "$tmpdir/quotatab.conf";
332 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
333 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
334
335 my $log_file = test_get_logfile();
336
337 my $auth_user_file = File::Spec->rel2abs("$tmpdir/quotatab.passwd");
338 my $auth_group_file = File::Spec->rel2abs("$tmpdir/quotatab.group");
339
340 my $user = 'proftpd';
341 my $passwd = 'test';
342 my $group = 'ftpd';
343 my $home_dir = File::Spec->rel2abs($tmpdir);
344 mkpath($home_dir);
345
346 my $uid = 500;
347 my $gid = 500;
348
349 # Make sure that, if we're running as root, that the home directories has
350 # permissions/privs set for the account we create
351 if ($< == 0) {
352 unless (chmod(0755, $home_dir)) {
353 die("Can't set perms on $home_dir to 0755: $!");
354 }
355
356 unless (chown($uid, $gid, $home_dir)) {
357 die("Can't set owner of $home_dir to $uid/$gid: $!");
358 }
359 }
360
361 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
362 '/bin/bash');
363 auth_group_write($auth_group_file, $group, $gid, $user);
364
365 my $limit_file = File::Spec->rel2abs("$tmpdir/ftpquota-all-limit.tab");
366 my $tally_file = File::Spec->rel2abs("$tmpdir/ftpquota-all-tally.tab");
367
368 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
369 if (open(my $fh, ">> $test_file")) {
370 print $fh "Hello, World!\n";
371 unless (close($fh)) {
372 die("Can't write $test_file: $!");
373 }
374
375 } else {
376 die("Can't open $test_file: $!");
377 }
378
379 my $config = {
380 PidFile => $pid_file,
381 ScoreboardFile => $scoreboard_file,
382 SystemLog => $log_file,
383
384 AuthUserFile => $auth_user_file,
385 AuthGroupFile => $auth_group_file,
386 AuthOrder => 'mod_auth_file.c',
387
388 DefaultChdir => '~',
389
390 IfModules => {
391 'mod_delay.c' => {
392 DelayEngine => 'off',
393 },
394
395 'mod_quotatab_file.c' => {
396 QuotaEngine => 'on',
397 QuotaLog => $log_file,
398 QuotaLimitTable => "file:$limit_file",
399 QuotaTallyTable => "file:$tally_file",
400 QuotaDisplayUnits => 'Mb',
401 },
402 },
403 };
404
405 my ($port, $config_user, $config_group) = config_write($config_file, $config);
406
407 # Open pipes, for use between the parent and child processes. Specifically,
408 # the child will indicate when it's done with its test by writing a message
409 # to the parent.
410 my ($rfh, $wfh);
411 unless (pipe($rfh, $wfh)) {
412 die("Can't open pipe: $!");
413 }
414
415 my $ex;
416
417 # Fork child
418 $self->handle_sigchld();
419 defined(my $pid = fork()) or die("Can't fork: $!");
420 if ($pid) {
421 eval {
422 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
423 $client->login($user, $passwd);
424
425 my $conn = $client->retr_raw('test.txt');
426 unless ($conn) {
427 die("Failed to RETR test.txt: " . $client->response_code() . " " .
428 $client->response_msg());
429 }
430
431 my $buf;
432 my $bufsz = 8192;
433
434 $conn->read($buf, $bufsz, 25);
435 eval { $conn->close() };
436
437 my $resp_code = $client->response_code();
438 my $resp_msg = $client->response_msg();
439 $self->assert_transfer_ok($resp_code, $resp_msg);
440
441 $client->quit();
442 };
443
444 if ($@) {
445 $ex = $@;
446 }
447
448 $wfh->print("done\n");
449 $wfh->flush();
450
451 } else {
452 eval { server_wait($config_file, $rfh) };
453 if ($@) {
454 warn($@);
455 exit 1;
456 }
457
458 exit 0;
459 }
460
461 # Stop server
462 server_stop($pid_file);
463
464 $self->assert_child_ok($pid);
465
466 if ($ex) {
467 test_append_logfile($log_file, $ex);
468 unlink($log_file);
469
470 die($ex);
471 }
472
473 unlink($log_file);
474 }
475
476 sub quotatab_file_bytes_download_zero {
477 my $self = shift;
478 my $tmpdir = $self->{tmpdir};
479
480 my $config_file = "$tmpdir/quotatab.conf";
481 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
482 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
483
484 my $log_file = test_get_logfile();
485
486 my $auth_user_file = File::Spec->rel2abs("$tmpdir/quotatab.passwd");
487 my $auth_group_file = File::Spec->rel2abs("$tmpdir/quotatab.group");
488
489 my $user = 'proftpd';
490 my $passwd = 'test';
491 my $group = 'ftpd';
492 my $home_dir = File::Spec->rel2abs($tmpdir);
493 mkpath($home_dir);
494
495 my $uid = 500;
496 my $gid = 500;
497
498 # Make sure that, if we're running as root, that the home directories has
499 # permissions/privs set for the account we create
500 if ($< == 0) {
501 unless (chmod(0755, $home_dir)) {
502 die("Can't set perms on $home_dir to 0755: $!");
503 }
504
505 unless (chown($uid, $gid, $home_dir)) {
506 die("Can't set owner of $home_dir to $uid/$gid: $!");
507 }
508 }
509
510 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
511 '/bin/bash');
512 auth_group_write($auth_group_file, $group, $gid, $user);
513
514 # Generate our limit file
515
516 my $limit_file = File::Spec->rel2abs("$tmpdir/ftpquota-limit-zero-bytes.tab");
517 my $tally_file = File::Spec->rel2abs("$tmpdir/ftpquota-tally-zero-bytes.tab");
518
519 my $ftpquota_bin = get_ftpquota_bin();
520 my $cmd;
521
522 # See:
523 # https://forums.proftpd.org/smf/index.php/topic,11242.0.html
524
525 # Create the limit table
526 if ($ENV{TEST_VERBOSE}) {
527 $cmd = "perl $ftpquota_bin --verbose --create-table --table-path=$limit_file --type=limit";
528
529 } else {
530 $cmd = "perl $ftpquota_bin --create-table --table-path=$limit_file --type=limit";
531 }
532
533 if ($ENV{TEST_VERBOSE}) {
534 print STDERR "Executing perl: $cmd\n";
535 }
536
537 my @res = `$cmd`;
538
539 if (scalar(@res) &&
540 $ENV{TEST_VERBOSE}) {
541 print STDERR "Output: ", join('', @res), "\n";
542 }
543
544 if ($? != 0) {
545 die("'$cmd' failed");
546 }
547
548 # Create the tally table
549 if ($ENV{TEST_VERBOSE}) {
550 $cmd = "perl $ftpquota_bin --verbose --create-table --table-path=$tally_file --type=tally";
551
552 } else {
553 $cmd = "perl $ftpquota_bin --create-table --table-path=$tally_file --type=tally";
554 }
555
556 if ($ENV{TEST_VERBOSE}) {
557 print STDERR "Executing perl: $cmd\n";
558 }
559
560 @res = `$cmd`;
561
562 if (scalar(@res) &&
563 $ENV{TEST_VERBOSE}) {
564 print STDERR "Output: ", join('', @res), "\n";
565 }
566
567 if ($? != 0) {
568 die("'$cmd' failed");
569 }
570
571 # Populate the limit table
572 if ($ENV{TEST_VERBOSE}) {
573 $cmd = "perl $ftpquota_bin --verbose --table-path=$limit_file --type=limit --add-record --name=$user -Q=user -L=hard --Bu=10485760 --Fd=0 --Bd=0";
574
575 } else {
576 $cmd = "perl $ftpquota_bin --table-path=$limit_file --type=limit --add-record --name=$user -Q=user -L=hard --Bu=10485760 --Fd=0 --Bd=0";
577 }
578
579 if ($ENV{TEST_VERBOSE}) {
580 print STDERR "Executing perl: $cmd\n";
581 }
582
583 @res = `$cmd`;
584
585 if (scalar(@res) &&
586 $ENV{TEST_VERBOSE}) {
587 print STDERR "Output: ", join('', @res), "\n";
588 }
589
590 if ($? != 0) {
591 die("'$cmd' failed");
592 }
593
594 if ($ENV{TEST_VERBOSE}) {
595 $cmd = "perl $ftpquota_bin --verbose --table-path=$limit_file --type=limit --show-record --name=$user";
596
597 } else {
598 $cmd = "perl $ftpquota_bin --table-path=$limit_file --type=limit --show-record --name=$user";
599 }
600
601 if ($ENV{TEST_VERBOSE}) {
602 print STDERR "Executing perl: $cmd\n";
603 }
604
605 @res = `$cmd`;
606
607 if (scalar(@res) &&
608 $ENV{TEST_VERBOSE}) {
609 print STDERR "Output: ", join('', @res), "\n";
610 }
611
612 if ($? != 0) {
613 die("'$cmd' failed");
614 }
615
616 my $config = {
617 PidFile => $pid_file,
618 ScoreboardFile => $scoreboard_file,
619 SystemLog => $log_file,
620
621 AuthUserFile => $auth_user_file,
622 AuthGroupFile => $auth_group_file,
623 AuthOrder => 'mod_auth_file.c',
624
625 DefaultChdir => '~',
626
627 IfModules => {
628 'mod_delay.c' => {
629 DelayEngine => 'off',
630 },
631
632 'mod_quotatab_file.c' => {
633 QuotaEngine => 'on',
634 QuotaLog => $log_file,
635 QuotaLimitTable => "file:$limit_file",
636 QuotaTallyTable => "file:$tally_file",
637 QuotaDisplayUnits => 'Mb',
638 },
639 },
640 };
641
642 my ($port, $config_user, $config_group) = config_write($config_file, $config);
643
644 # Open pipes, for use between the parent and child processes. Specifically,
645 # the child will indicate when it's done with its test by writing a message
646 # to the parent.
647 my ($rfh, $wfh);
648 unless (pipe($rfh, $wfh)) {
649 die("Can't open pipe: $!");
650 }
651
652 my $ex;
653
654 # Fork child
655 $self->handle_sigchld();
656 defined(my $pid = fork()) or die("Can't fork: $!");
657 if ($pid) {
658 eval {
659 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
660 $client->login($user, $passwd);
661
662 $client->site("QUOTA");
663 my $resp_code = $client->response_code();
664 my $resp_msgs = $client->response_msgs();
665
666 my $expected = 200;
667 $self->assert($expected == $resp_code,
668 test_msg("Expected response code $expected, got $resp_code"));
669
670 $expected = ' Downloaded Mb: unlimited';
671 $self->assert($expected eq $resp_msgs->[6],
672 test_msg("Expected response message '$expected', got '$resp_msgs->[6]'"));
673
674 $client->quit();
675 };
676 if ($@) {
677 $ex = $@;
678 }
679
680 $wfh->print("done\n");
681 $wfh->flush();
682
683 } else {
684 eval { server_wait($config_file, $rfh) };
685 if ($@) {
686 warn($@);
687 exit 1;
688 }
689
690 exit 0;
691 }
692
693 # Stop server
694 server_stop($pid_file);
695
696 $self->assert_child_ok($pid);
697
698 if ($ex) {
699 test_append_logfile($log_file, $ex);
700 unlink($log_file);
701
702 die($ex);
703 }
704
705 # unlink($log_file);
706 }
707
708 1;