Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_quotatab/site_misc.pm

raw
1 package ProFTPD::Tests::Modules::mod_quotatab::site_misc;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Path qw(mkpath);
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 quotatab_site_mkdir_user_limit_no_directory_tally_bug3660 => {
20 order => ++$order,
21 test_class => [qw(bug forking mod_quotatab_sql mod_site_misc)],
22 },
23
24 quotatab_site_mkdir_user_limit_with_directory_tally_bug3660 => {
25 order => ++$order,
26 test_class => [qw(bug forking mod_quotatab_sql mod_site_misc)],
27 },
28
29 quotatab_site_mkdir_user_limit_bytes_in_exceeded_soft_limit_bug3660 => {
30 order => ++$order,
31 test_class => [qw(bug forking mod_quotatab_sql mod_site_misc)],
32 },
33
34 quotatab_site_mkdir_user_limit_bytes_in_exceeded_hard_limit1_bug3660 => {
35 order => ++$order,
36 test_class => [qw(bug forking mod_quotatab_sql mod_site_misc)],
37 },
38
39 quotatab_site_mkdir_user_limit_bytes_in_exceeded_hard_limit2_bug3660 => {
40 order => ++$order,
41 test_class => [qw(bug forking mod_quotatab_sql mod_site_misc)],
42 },
43
44 quotatab_site_mkdir_user_limit_files_in_reached_bug3660 => {
45 order => ++$order,
46 test_class => [qw(bug forking mod_quotatab_sql mod_site_misc)],
47 },
48
49 quotatab_site_mkdir_user_limit_files_in_exceeded_bug3660 => {
50 order => ++$order,
51 test_class => [qw(bug forking mod_quotatab_sql mod_site_misc)],
52 },
53
54 quotatab_site_rmdir_user_limit_no_directory_tally_bug3660 => {
55 order => ++$order,
56 test_class => [qw(bug forking mod_quotatab_sql mod_site_misc)],
57 },
58
59 quotatab_site_rmdir_user_limit_with_directory_tally_bug3660 => {
60 order => ++$order,
61 test_class => [qw(bug forking mod_quotatab_sql mod_site_misc)],
62 },
63
64 };
65
66 sub new {
67 return shift()->SUPER::new(@_);
68 }
69
70 sub list_tests {
71 return testsuite_get_runnable_tests($TESTS);
72 }
73
74 sub get_tally {
75 my $db_file = shift;
76 my $where = shift;
77
78 my $sql = "SELECT quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies";
79 if ($where) {
80 $sql .= " WHERE $where";
81 }
82
83 my $cmd = "sqlite3 $db_file \"$sql\"";
84
85 if ($ENV{TEST_VERBOSE}) {
86 print STDERR "Executing sqlite3: $cmd\n";
87 }
88
89 my $res = join('', `$cmd`);
90
91 # The default sqlite3 delimiter is '|'
92 return split(/\|/, $res);
93 }
94
95 sub quotatab_site_mkdir_user_limit_no_directory_tally_bug3660 {
96 my $self = shift;
97 my $tmpdir = $self->{tmpdir};
98
99 my $config_file = "$tmpdir/quotatab.conf";
100 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
101 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
102
103 my $log_file = File::Spec->rel2abs('tests.log');
104
105 my $user = 'proftpd';
106 my $passwd = 'test';
107 my $group = 'ftpd';
108 my $home_dir = File::Spec->rel2abs($tmpdir);
109 my $uid = 500;
110 my $gid = 500;
111
112 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
113
114 # Build up sqlite3 command to create users, groups tables and populate them
115 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
116
117 if (open(my $fh, "> $db_script")) {
118 print $fh <<EOS;
119 CREATE TABLE users (
120 userid TEXT,
121 passwd TEXT,
122 uid INTEGER,
123 gid INTEGER,
124 homedir TEXT,
125 shell TEXT,
126 lastdir TEXT
127 );
128 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
129
130 CREATE TABLE groups (
131 groupname TEXT,
132 gid INTEGER,
133 members TEXT
134 );
135 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
136
137 CREATE TABLE quotalimits (
138 name TEXT NOT NULL,
139 quota_type TEXT NOT NULL,
140 per_session TEXT NOT NULL,
141 limit_type TEXT NOT NULL,
142 bytes_in_avail REAL NOT NULL,
143 bytes_out_avail REAL NOT NULL,
144 bytes_xfer_avail REAL NOT NULL,
145 files_in_avail INTEGER NOT NULL,
146 files_out_avail INTEGER NOT NULL,
147 files_xfer_avail INTEGER NOT NULL
148 );
149 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 32, 0, 0, 2, 0, 0);
150
151 CREATE TABLE quotatallies (
152 name TEXT NOT NULL,
153 quota_type TEXT NOT NULL,
154 bytes_in_used REAL NOT NULL,
155 bytes_out_used REAL NOT NULL,
156 bytes_xfer_used REAL NOT NULL,
157 files_in_used INTEGER NOT NULL,
158 files_out_used INTEGER NOT NULL,
159 files_xfer_used INTEGER NOT NULL
160 );
161 EOS
162
163 unless (close($fh)) {
164 die("Can't write $db_script: $!");
165 }
166
167 } else {
168 die("Can't open $db_script: $!");
169 }
170
171 my $cmd = "sqlite3 $db_file < $db_script";
172
173 if ($ENV{TEST_VERBOSE}) {
174 print STDERR "Executing sqlite3: $cmd\n";
175 }
176
177 my @output = `$cmd`;
178 if (scalar(@output) &&
179 $ENV{TEST_VERBOSE}) {
180 print STDERR "Output: ", join('', @output), "\n";
181 }
182
183 my $test_dir = File::Spec->rel2abs("$tmpdir/foo/bar/baz");
184
185 my $config = {
186 PidFile => $pid_file,
187 ScoreboardFile => $scoreboard_file,
188 SystemLog => $log_file,
189
190 DefaultChdir => '~',
191
192 IfModules => {
193 'mod_delay.c' => {
194 DelayEngine => 'off',
195 },
196
197 'mod_quotatab_sql.c' => [
198 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
199 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
200 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
201 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
202
203 'QuotaEngine on',
204 "QuotaLog $log_file",
205 'QuotaLimitTable sql:/get-quota-limit',
206 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
207 ],
208
209 'mod_sql.c' => {
210 SQLAuthTypes => 'plaintext',
211 SQLBackend => 'sqlite3',
212 SQLConnectInfo => $db_file,
213 SQLLogFile => $log_file,
214 SQLMinID => '0',
215 },
216 },
217 };
218
219 my ($port, $config_user, $config_group) = config_write($config_file, $config);
220
221 # Open pipes, for use between the parent and child processes. Specifically,
222 # the child will indicate when it's done with its test by writing a message
223 # to the parent.
224 my ($rfh, $wfh);
225 unless (pipe($rfh, $wfh)) {
226 die("Can't open pipe: $!");
227 }
228
229 my $ex;
230
231 # Fork child
232 $self->handle_sigchld();
233 defined(my $pid = fork()) or die("Can't fork: $!");
234 if ($pid) {
235 eval {
236 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
237 $client->login($user, $passwd);
238
239 my ($resp_code, $resp_msg) = $client->site('mkdir', 'foo/bar/baz');
240
241 my $expected;
242
243 $expected = 200;
244 $self->assert($expected == $resp_code,
245 test_msg("Expected $expected, got $resp_code"));
246
247 $expected = "SITE mkdir command successful";
248 $self->assert($expected eq $resp_msg,
249 test_msg("Expected '$expected', got '$resp_msg'"));
250
251 $self->assert(-d $test_dir,
252 test_msg("Directory $test_dir does not exist as expected"));
253 };
254
255 if ($@) {
256 $ex = $@;
257 }
258
259 $wfh->print("done\n");
260 $wfh->flush();
261
262 } else {
263 eval { server_wait($config_file, $rfh) };
264 if ($@) {
265 warn($@);
266 exit 1;
267 }
268
269 exit 0;
270 }
271
272 # Stop server
273 server_stop($pid_file);
274
275 $self->assert_child_ok($pid);
276
277 if ($ex) {
278 die($ex);
279 }
280
281 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
282
283 my $expected;
284
285 $expected = 'user';
286 $self->assert($expected eq $quota_type,
287 test_msg("Expected '$expected', got '$quota_type'"));
288
289 $expected = '^(0.0|0)$';
290 $self->assert(qr/$expected/, $bytes_in_used,
291 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
292
293 $expected = '^(0.0|0)$';
294 $self->assert(qr/$expected/, $bytes_out_used,
295 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
296
297 $expected = '^(0.0|0)$';
298 $self->assert(qr/$expected/, $bytes_xfer_used,
299 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
300
301 $expected = 0;
302 $self->assert($expected == $files_in_used,
303 test_msg("Expected $expected files-in-used, got $files_in_used"));
304
305 $expected = 0;
306 $self->assert($expected == $files_out_used,
307 test_msg("Expected $expected files-out-used, got $files_out_used"));
308
309 $expected = 0;
310 $self->assert($expected == $files_xfer_used,
311 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
312
313 unlink($log_file);
314 }
315
316 sub quotatab_site_mkdir_user_limit_with_directory_tally_bug3660 {
317 my $self = shift;
318 my $tmpdir = $self->{tmpdir};
319
320 my $config_file = "$tmpdir/quotatab.conf";
321 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
322 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
323
324 my $log_file = File::Spec->rel2abs('tests.log');
325
326 my $user = 'proftpd';
327 my $passwd = 'test';
328 my $group = 'ftpd';
329 my $home_dir = File::Spec->rel2abs($tmpdir);
330 my $uid = 500;
331 my $gid = 500;
332
333 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
334
335 # Build up sqlite3 command to create users, groups tables and populate them
336 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
337
338 if (open(my $fh, "> $db_script")) {
339 print $fh <<EOS;
340 CREATE TABLE users (
341 userid TEXT,
342 passwd TEXT,
343 uid INTEGER,
344 gid INTEGER,
345 homedir TEXT,
346 shell TEXT,
347 lastdir TEXT
348 );
349 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
350
351 CREATE TABLE groups (
352 groupname TEXT,
353 gid INTEGER,
354 members TEXT
355 );
356 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
357
358 CREATE TABLE quotalimits (
359 name TEXT NOT NULL,
360 quota_type TEXT NOT NULL,
361 per_session TEXT NOT NULL,
362 limit_type TEXT NOT NULL,
363 bytes_in_avail REAL NOT NULL,
364 bytes_out_avail REAL NOT NULL,
365 bytes_xfer_avail REAL NOT NULL,
366 files_in_avail INTEGER NOT NULL,
367 files_out_avail INTEGER NOT NULL,
368 files_xfer_avail INTEGER NOT NULL
369 );
370 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 16384, 0, 0, 4, 0, 0);
371
372 CREATE TABLE quotatallies (
373 name TEXT NOT NULL,
374 quota_type TEXT NOT NULL,
375 bytes_in_used REAL NOT NULL,
376 bytes_out_used REAL NOT NULL,
377 bytes_xfer_used REAL NOT NULL,
378 files_in_used INTEGER NOT NULL,
379 files_out_used INTEGER NOT NULL,
380 files_xfer_used INTEGER NOT NULL
381 );
382 EOS
383
384 unless (close($fh)) {
385 die("Can't write $db_script: $!");
386 }
387
388 } else {
389 die("Can't open $db_script: $!");
390 }
391
392 my $cmd = "sqlite3 $db_file < $db_script";
393
394 if ($ENV{TEST_VERBOSE}) {
395 print STDERR "Executing sqlite3: $cmd\n";
396 }
397
398 my @output = `$cmd`;
399 if (scalar(@output) &&
400 $ENV{TEST_VERBOSE}) {
401 print STDERR "Output: ", join('', @output), "\n";
402 }
403
404 my $test_dir = File::Spec->rel2abs("$tmpdir/foo/bar/baz");
405
406 my $config = {
407 PidFile => $pid_file,
408 ScoreboardFile => $scoreboard_file,
409 SystemLog => $log_file,
410
411 DefaultChdir => '~',
412
413 IfModules => {
414 'mod_delay.c' => {
415 DelayEngine => 'off',
416 },
417
418 'mod_quotatab_sql.c' => [
419 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
420 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
421 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
422 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
423
424 'QuotaEngine on',
425 "QuotaLog $log_file",
426 "QuotaDirectoryTally on",
427
428 'QuotaLimitTable sql:/get-quota-limit',
429 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
430 ],
431
432 'mod_sql.c' => {
433 SQLAuthTypes => 'plaintext',
434 SQLBackend => 'sqlite3',
435 SQLConnectInfo => $db_file,
436 SQLLogFile => $log_file,
437 SQLMinID => '0',
438 },
439 },
440 };
441
442 my ($port, $config_user, $config_group) = config_write($config_file, $config);
443
444 # Open pipes, for use between the parent and child processes. Specifically,
445 # the child will indicate when it's done with its test by writing a message
446 # to the parent.
447 my ($rfh, $wfh);
448 unless (pipe($rfh, $wfh)) {
449 die("Can't open pipe: $!");
450 }
451
452 my $ex;
453
454 # Fork child
455 $self->handle_sigchld();
456 defined(my $pid = fork()) or die("Can't fork: $!");
457 if ($pid) {
458 eval {
459 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
460 $client->login($user, $passwd);
461
462 my ($resp_code, $resp_msg) = $client->site('mkdir', 'foo/bar/baz');
463
464 my $expected;
465
466 $expected = 200;
467 $self->assert($expected == $resp_code,
468 test_msg("Expected $expected, got $resp_code"));
469
470 $expected = "SITE mkdir command successful";
471 $self->assert($expected eq $resp_msg,
472 test_msg("Expected '$expected', got '$resp_msg'"));
473
474 $self->assert(-d $test_dir,
475 test_msg("Directory $test_dir does not exist as expected"));
476 };
477
478 if ($@) {
479 $ex = $@;
480 }
481
482 $wfh->print("done\n");
483 $wfh->flush();
484
485 } else {
486 eval { server_wait($config_file, $rfh) };
487 if ($@) {
488 warn($@);
489 exit 1;
490 }
491
492 exit 0;
493 }
494
495 # Stop server
496 server_stop($pid_file);
497
498 $self->assert_child_ok($pid);
499
500 if ($ex) {
501 die($ex);
502 }
503
504 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
505
506 my $expected;
507
508 $expected = 'user';
509 $self->assert($expected eq $quota_type,
510 test_msg("Expected '$expected', got '$quota_type'"));
511
512 $expected = '^(12288.0|12288)$';
513 $self->assert(qr/$expected/, $bytes_in_used,
514 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
515
516 $expected = '^(0.0|0)$';
517 $self->assert(qr/$expected/, $bytes_out_used,
518 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
519
520 $expected = '^(0.0|0)$';
521 $self->assert(qr/$expected/, $bytes_xfer_used,
522 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
523
524 $expected = 3;
525 $self->assert($expected == $files_in_used,
526 test_msg("Expected $expected files-in-used, got $files_in_used"));
527
528 $expected = 0;
529 $self->assert($expected == $files_out_used,
530 test_msg("Expected $expected files-out-used, got $files_out_used"));
531
532 $expected = 0;
533 $self->assert($expected == $files_xfer_used,
534 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
535
536 unlink($log_file);
537 }
538
539 sub quotatab_site_mkdir_user_limit_bytes_in_exceeded_soft_limit_bug3660 {
540 my $self = shift;
541 my $tmpdir = $self->{tmpdir};
542
543 my $config_file = "$tmpdir/quotatab.conf";
544 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
545 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
546
547 my $log_file = File::Spec->rel2abs('tests.log');
548
549 my $user = 'proftpd';
550 my $passwd = 'test';
551 my $group = 'ftpd';
552 my $home_dir = File::Spec->rel2abs($tmpdir);
553 my $uid = 500;
554 my $gid = 500;
555
556 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
557
558 # Build up sqlite3 command to create users, groups tables and populate them
559 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
560
561 if (open(my $fh, "> $db_script")) {
562 print $fh <<EOS;
563 CREATE TABLE users (
564 userid TEXT,
565 passwd TEXT,
566 uid INTEGER,
567 gid INTEGER,
568 homedir TEXT,
569 shell TEXT,
570 lastdir TEXT
571 );
572 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
573
574 CREATE TABLE groups (
575 groupname TEXT,
576 gid INTEGER,
577 members TEXT
578 );
579 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
580
581 CREATE TABLE quotalimits (
582 name TEXT NOT NULL,
583 quota_type TEXT NOT NULL,
584 per_session TEXT NOT NULL,
585 limit_type TEXT NOT NULL,
586 bytes_in_avail REAL NOT NULL,
587 bytes_out_avail REAL NOT NULL,
588 bytes_xfer_avail REAL NOT NULL,
589 files_in_avail INTEGER NOT NULL,
590 files_out_avail INTEGER NOT NULL,
591 files_xfer_avail INTEGER NOT NULL
592 );
593 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 10000, 0, 0, 4, 0, 0);
594
595 CREATE TABLE quotatallies (
596 name TEXT NOT NULL,
597 quota_type TEXT NOT NULL,
598 bytes_in_used REAL NOT NULL,
599 bytes_out_used REAL NOT NULL,
600 bytes_xfer_used REAL NOT NULL,
601 files_in_used INTEGER NOT NULL,
602 files_out_used INTEGER NOT NULL,
603 files_xfer_used INTEGER NOT NULL
604 );
605 EOS
606
607 unless (close($fh)) {
608 die("Can't write $db_script: $!");
609 }
610
611 } else {
612 die("Can't open $db_script: $!");
613 }
614
615 my $cmd = "sqlite3 $db_file < $db_script";
616
617 if ($ENV{TEST_VERBOSE}) {
618 print STDERR "Executing sqlite3: $cmd\n";
619 }
620
621 my @output = `$cmd`;
622 if (scalar(@output) &&
623 $ENV{TEST_VERBOSE}) {
624 print STDERR "Output: ", join('', @output), "\n";
625 }
626
627 my $test_dir = File::Spec->rel2abs("$tmpdir/foo/bar/baz");
628
629 my $config = {
630 PidFile => $pid_file,
631 ScoreboardFile => $scoreboard_file,
632 SystemLog => $log_file,
633
634 DefaultChdir => '~',
635
636 IfModules => {
637 'mod_delay.c' => {
638 DelayEngine => 'off',
639 },
640
641 'mod_quotatab_sql.c' => [
642 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
643 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
644 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
645 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
646
647 'QuotaEngine on',
648 "QuotaLog $log_file",
649 "QuotaDirectoryTally on",
650
651 'QuotaLimitTable sql:/get-quota-limit',
652 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
653 ],
654
655 'mod_sql.c' => {
656 SQLAuthTypes => 'plaintext',
657 SQLBackend => 'sqlite3',
658 SQLConnectInfo => $db_file,
659 SQLLogFile => $log_file,
660 SQLMinID => '0',
661 },
662 },
663 };
664
665 my ($port, $config_user, $config_group) = config_write($config_file, $config);
666
667 # Open pipes, for use between the parent and child processes. Specifically,
668 # the child will indicate when it's done with its test by writing a message
669 # to the parent.
670 my ($rfh, $wfh);
671 unless (pipe($rfh, $wfh)) {
672 die("Can't open pipe: $!");
673 }
674
675 my $ex;
676
677 # Fork child
678 $self->handle_sigchld();
679 defined(my $pid = fork()) or die("Can't fork: $!");
680 if ($pid) {
681 eval {
682 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
683 $client->login($user, $passwd);
684
685 my ($resp_code, $resp_msg) = $client->site('mkdir', 'foo/bar/baz');
686
687 my $expected;
688
689 $expected = 200;
690 $self->assert($expected == $resp_code,
691 test_msg("Expected $expected, got $resp_code"));
692
693 $expected = "SITE mkdir command successful";
694 $self->assert($expected eq $resp_msg,
695 test_msg("Expected '$expected', got '$resp_msg'"));
696
697 $self->assert(-d $test_dir,
698 test_msg("Directory $test_dir does not exist as expected"));
699 };
700
701 if ($@) {
702 $ex = $@;
703 }
704
705 $wfh->print("done\n");
706 $wfh->flush();
707
708 } else {
709 eval { server_wait($config_file, $rfh) };
710 if ($@) {
711 warn($@);
712 exit 1;
713 }
714
715 exit 0;
716 }
717
718 # Stop server
719 server_stop($pid_file);
720
721 $self->assert_child_ok($pid);
722
723 if ($ex) {
724 die($ex);
725 }
726
727 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
728
729 my $expected;
730
731 $expected = 'user';
732 $self->assert($expected eq $quota_type,
733 test_msg("Expected '$expected', got '$quota_type'"));
734
735 $expected = '^(12288.0|12288)$';
736 $self->assert(qr/$expected/, $bytes_in_used,
737 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
738
739 $expected = '^(0.0|0)$';
740 $self->assert(qr/$expected/, $bytes_out_used,
741 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
742
743 $expected = '^(0.0|0)$';
744 $self->assert(qr/$expected/, $bytes_xfer_used,
745 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
746
747 $expected = 3;
748 $self->assert($expected == $files_in_used,
749 test_msg("Expected $expected files-in-used, got $files_in_used"));
750
751 $expected = 0;
752 $self->assert($expected == $files_out_used,
753 test_msg("Expected $expected files-out-used, got $files_out_used"));
754
755 $expected = 0;
756 $self->assert($expected == $files_xfer_used,
757 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
758
759 unlink($log_file);
760 }
761
762 sub quotatab_site_mkdir_user_limit_bytes_in_exceeded_hard_limit1_bug3660 {
763 my $self = shift;
764 my $tmpdir = $self->{tmpdir};
765
766 my $config_file = "$tmpdir/quotatab.conf";
767 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
768 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
769
770 my $log_file = File::Spec->rel2abs('tests.log');
771
772 my $user = 'proftpd';
773 my $passwd = 'test';
774 my $group = 'ftpd';
775 my $home_dir = File::Spec->rel2abs($tmpdir);
776 my $uid = 500;
777 my $gid = 500;
778
779 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
780
781 # Build up sqlite3 command to create users, groups tables and populate them
782 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
783
784 if (open(my $fh, "> $db_script")) {
785 print $fh <<EOS;
786 CREATE TABLE users (
787 userid TEXT,
788 passwd TEXT,
789 uid INTEGER,
790 gid INTEGER,
791 homedir TEXT,
792 shell TEXT,
793 lastdir TEXT
794 );
795 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
796
797 CREATE TABLE groups (
798 groupname TEXT,
799 gid INTEGER,
800 members TEXT
801 );
802 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
803
804 CREATE TABLE quotalimits (
805 name TEXT NOT NULL,
806 quota_type TEXT NOT NULL,
807 per_session TEXT NOT NULL,
808 limit_type TEXT NOT NULL,
809 bytes_in_avail REAL NOT NULL,
810 bytes_out_avail REAL NOT NULL,
811 bytes_xfer_avail REAL NOT NULL,
812 files_in_avail INTEGER NOT NULL,
813 files_out_avail INTEGER NOT NULL,
814 files_xfer_avail INTEGER NOT NULL
815 );
816 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'hard', 10000, 0, 0, 4, 0, 0);
817
818 CREATE TABLE quotatallies (
819 name TEXT NOT NULL,
820 quota_type TEXT NOT NULL,
821 bytes_in_used REAL NOT NULL,
822 bytes_out_used REAL NOT NULL,
823 bytes_xfer_used REAL NOT NULL,
824 files_in_used INTEGER NOT NULL,
825 files_out_used INTEGER NOT NULL,
826 files_xfer_used INTEGER NOT NULL
827 );
828 EOS
829
830 unless (close($fh)) {
831 die("Can't write $db_script: $!");
832 }
833
834 } else {
835 die("Can't open $db_script: $!");
836 }
837
838 my $cmd = "sqlite3 $db_file < $db_script";
839
840 if ($ENV{TEST_VERBOSE}) {
841 print STDERR "Executing sqlite3: $cmd\n";
842 }
843
844 my @output = `$cmd`;
845 if (scalar(@output) &&
846 $ENV{TEST_VERBOSE}) {
847 print STDERR "Output: ", join('', @output), "\n";
848 }
849
850 my $test_dir = File::Spec->rel2abs("$tmpdir/foo/bar/baz");
851
852 my $config = {
853 PidFile => $pid_file,
854 ScoreboardFile => $scoreboard_file,
855 SystemLog => $log_file,
856
857 DefaultChdir => '~',
858
859 IfModules => {
860 'mod_delay.c' => {
861 DelayEngine => 'off',
862 },
863
864 'mod_quotatab_sql.c' => [
865 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
866 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
867 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
868 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
869
870 'QuotaEngine on',
871 "QuotaLog $log_file",
872 "QuotaDirectoryTally on",
873
874 'QuotaLimitTable sql:/get-quota-limit',
875 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
876 ],
877
878 'mod_sql.c' => {
879 SQLAuthTypes => 'plaintext',
880 SQLBackend => 'sqlite3',
881 SQLConnectInfo => $db_file,
882 SQLLogFile => $log_file,
883 SQLMinID => '0',
884 },
885 },
886 };
887
888 my ($port, $config_user, $config_group) = config_write($config_file, $config);
889
890 # Open pipes, for use between the parent and child processes. Specifically,
891 # the child will indicate when it's done with its test by writing a message
892 # to the parent.
893 my ($rfh, $wfh);
894 unless (pipe($rfh, $wfh)) {
895 die("Can't open pipe: $!");
896 }
897
898 my $ex;
899
900 # Fork child
901 $self->handle_sigchld();
902 defined(my $pid = fork()) or die("Can't fork: $!");
903 if ($pid) {
904 eval {
905 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
906 $client->login($user, $passwd);
907
908 my ($resp_code, $resp_msg) = $client->site('mkdir', 'foo/bar/baz');
909
910 my $expected;
911
912 $expected = 200;
913 $self->assert($expected == $resp_code,
914 test_msg("Expected $expected, got $resp_code"));
915
916 $expected = "SITE mkdir command successful";
917 $self->assert($expected eq $resp_msg,
918 test_msg("Expected '$expected', got '$resp_msg'"));
919
920 $self->assert(!-d $test_dir,
921 test_msg("Directory $test_dir does not exist as expected"));
922 };
923
924 if ($@) {
925 $ex = $@;
926 }
927
928 $wfh->print("done\n");
929 $wfh->flush();
930
931 } else {
932 eval { server_wait($config_file, $rfh) };
933 if ($@) {
934 warn($@);
935 exit 1;
936 }
937
938 exit 0;
939 }
940
941 # Stop server
942 server_stop($pid_file);
943
944 $self->assert_child_ok($pid);
945
946 if ($ex) {
947 die($ex);
948 }
949
950 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
951
952 my $expected;
953
954 $expected = 'user';
955 $self->assert($expected eq $quota_type,
956 test_msg("Expected '$expected', got '$quota_type'"));
957
958 $expected = '^(8192.0|8192)$';
959 $self->assert(qr/$expected/, $bytes_in_used,
960 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
961
962 $expected = '^(0.0|0)$';
963 $self->assert(qr/$expected/, $bytes_out_used,
964 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
965
966 $expected = '^(0.0|0)$';
967 $self->assert(qr/$expected/, $bytes_xfer_used,
968 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
969
970 $expected = 2;
971 $self->assert($expected == $files_in_used,
972 test_msg("Expected $expected files-in-used, got $files_in_used"));
973
974 $expected = 0;
975 $self->assert($expected == $files_out_used,
976 test_msg("Expected $expected files-out-used, got $files_out_used"));
977
978 $expected = 0;
979 $self->assert($expected == $files_xfer_used,
980 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
981
982 unlink($log_file);
983 }
984
985 sub quotatab_site_mkdir_user_limit_bytes_in_exceeded_hard_limit2_bug3660 {
986 my $self = shift;
987 my $tmpdir = $self->{tmpdir};
988
989 my $config_file = "$tmpdir/quotatab.conf";
990 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
991 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
992
993 my $log_file = File::Spec->rel2abs('tests.log');
994
995 my $user = 'proftpd';
996 my $passwd = 'test';
997 my $group = 'ftpd';
998 my $home_dir = File::Spec->rel2abs($tmpdir);
999 my $uid = 500;
1000 my $gid = 500;
1001
1002 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1003
1004 # Build up sqlite3 command to create users, groups tables and populate them
1005 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1006
1007 if (open(my $fh, "> $db_script")) {
1008 print $fh <<EOS;
1009 CREATE TABLE users (
1010 userid TEXT,
1011 passwd TEXT,
1012 uid INTEGER,
1013 gid INTEGER,
1014 homedir TEXT,
1015 shell TEXT,
1016 lastdir TEXT
1017 );
1018 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
1019
1020 CREATE TABLE groups (
1021 groupname TEXT,
1022 gid INTEGER,
1023 members TEXT
1024 );
1025 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
1026
1027 CREATE TABLE quotalimits (
1028 name TEXT NOT NULL,
1029 quota_type TEXT NOT NULL,
1030 per_session TEXT NOT NULL,
1031 limit_type TEXT NOT NULL,
1032 bytes_in_avail REAL NOT NULL,
1033 bytes_out_avail REAL NOT NULL,
1034 bytes_xfer_avail REAL NOT NULL,
1035 files_in_avail INTEGER NOT NULL,
1036 files_out_avail INTEGER NOT NULL,
1037 files_xfer_avail INTEGER NOT NULL
1038 );
1039 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'hard', 8000, 0, 0, 4, 0, 0);
1040
1041 CREATE TABLE quotatallies (
1042 name TEXT NOT NULL,
1043 quota_type TEXT NOT NULL,
1044 bytes_in_used REAL NOT NULL,
1045 bytes_out_used REAL NOT NULL,
1046 bytes_xfer_used REAL NOT NULL,
1047 files_in_used INTEGER NOT NULL,
1048 files_out_used INTEGER NOT NULL,
1049 files_xfer_used INTEGER NOT NULL
1050 );
1051 EOS
1052
1053 unless (close($fh)) {
1054 die("Can't write $db_script: $!");
1055 }
1056
1057 } else {
1058 die("Can't open $db_script: $!");
1059 }
1060
1061 my $cmd = "sqlite3 $db_file < $db_script";
1062
1063 if ($ENV{TEST_VERBOSE}) {
1064 print STDERR "Executing sqlite3: $cmd\n";
1065 }
1066
1067 my @output = `$cmd`;
1068 if (scalar(@output) &&
1069 $ENV{TEST_VERBOSE}) {
1070 print STDERR "Output: ", join('', @output), "\n";
1071 }
1072
1073 my $test_dir = File::Spec->rel2abs("$tmpdir/foo/bar/baz");
1074
1075 my $config = {
1076 PidFile => $pid_file,
1077 ScoreboardFile => $scoreboard_file,
1078 SystemLog => $log_file,
1079
1080 DefaultChdir => '~',
1081
1082 IfModules => {
1083 'mod_delay.c' => {
1084 DelayEngine => 'off',
1085 },
1086
1087 'mod_quotatab_sql.c' => [
1088 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1089 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1090 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
1091 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
1092
1093 'QuotaEngine on',
1094 "QuotaLog $log_file",
1095 "QuotaDirectoryTally on",
1096
1097 'QuotaLimitTable sql:/get-quota-limit',
1098 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
1099 ],
1100
1101 'mod_sql.c' => {
1102 SQLAuthTypes => 'plaintext',
1103 SQLBackend => 'sqlite3',
1104 SQLConnectInfo => $db_file,
1105 SQLLogFile => $log_file,
1106 SQLMinID => '0',
1107 },
1108 },
1109 };
1110
1111 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1112
1113 # Open pipes, for use between the parent and child processes. Specifically,
1114 # the child will indicate when it's done with its test by writing a message
1115 # to the parent.
1116 my ($rfh, $wfh);
1117 unless (pipe($rfh, $wfh)) {
1118 die("Can't open pipe: $!");
1119 }
1120
1121 my $ex;
1122
1123 # Fork child
1124 $self->handle_sigchld();
1125 defined(my $pid = fork()) or die("Can't fork: $!");
1126 if ($pid) {
1127 eval {
1128 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1129 $client->login($user, $passwd);
1130
1131 eval { $client->site('mkdir', 'foo/bar/baz') };
1132 unless ($@) {
1133 die("SITE MKDIR succeeded unexpectedly");
1134 }
1135
1136 my $resp_code = $client->response_code();
1137 my $resp_msg = $client->response_msg();
1138
1139 my $expected;
1140
1141 $expected = 550;
1142 $self->assert($expected == $resp_code,
1143 test_msg("Expected $expected, got $resp_code"));
1144
1145 # This is not the best error message, but it is accurate (from a
1146 # particular point of view).
1147 #
1148 # SITE MKDIR works by creating each directory component in the path.
1149 # In this test case, it creates foo/, which succeeds, then
1150 # creates foo/bar/, which succeeds -- but mod_quotatab's POST_CMD
1151 # handler sees that it exceeded the bytes limit, and removes the
1152 # foo/bar/ directory. Since that happens in a POST_CMD handler,
1153 # it can't fail that MKD command. So then SITE MKDIR tries to
1154 # create foo/bar/baz, and fails -- because foo/bar/ isn't there.
1155 # Hence the ENOENT error message.
1156
1157 $expected = "mkdir foo/bar/baz: No such file or directory";
1158 $self->assert($expected eq $resp_msg,
1159 test_msg("Expected '$expected', got '$resp_msg'"));
1160
1161 $self->assert(!-d $test_dir,
1162 test_msg("Directory $test_dir does not exist as expected"));
1163 };
1164
1165 if ($@) {
1166 $ex = $@;
1167 }
1168
1169 $wfh->print("done\n");
1170 $wfh->flush();
1171
1172 } else {
1173 eval { server_wait($config_file, $rfh) };
1174 if ($@) {
1175 warn($@);
1176 exit 1;
1177 }
1178
1179 exit 0;
1180 }
1181
1182 # Stop server
1183 server_stop($pid_file);
1184
1185 $self->assert_child_ok($pid);
1186
1187 if ($ex) {
1188 die($ex);
1189 }
1190
1191 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
1192
1193 my $expected;
1194
1195 $expected = 'user';
1196 $self->assert($expected eq $quota_type,
1197 test_msg("Expected '$expected', got '$quota_type'"));
1198
1199 $expected = '^(4096.0|4096)$';
1200 $self->assert(qr/$expected/, $bytes_in_used,
1201 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
1202
1203 $expected = '^(0.0|0)$';
1204 $self->assert(qr/$expected/, $bytes_out_used,
1205 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
1206
1207 $expected = '^(0.0|0)$';
1208 $self->assert(qr/$expected/, $bytes_xfer_used,
1209 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
1210
1211 $expected = 1;
1212 $self->assert($expected == $files_in_used,
1213 test_msg("Expected $expected files-in-used, got $files_in_used"));
1214
1215 $expected = 0;
1216 $self->assert($expected == $files_out_used,
1217 test_msg("Expected $expected files-out-used, got $files_out_used"));
1218
1219 $expected = 0;
1220 $self->assert($expected == $files_xfer_used,
1221 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
1222
1223 unlink($log_file);
1224 }
1225
1226 sub quotatab_site_mkdir_user_limit_files_in_reached_bug3660 {
1227 my $self = shift;
1228 my $tmpdir = $self->{tmpdir};
1229
1230 my $config_file = "$tmpdir/quotatab.conf";
1231 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
1232 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
1233
1234 my $log_file = File::Spec->rel2abs('tests.log');
1235
1236 my $user = 'proftpd';
1237 my $passwd = 'test';
1238 my $group = 'ftpd';
1239 my $home_dir = File::Spec->rel2abs($tmpdir);
1240 my $uid = 500;
1241 my $gid = 500;
1242
1243 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1244
1245 # Build up sqlite3 command to create users, groups tables and populate them
1246 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1247
1248 if (open(my $fh, "> $db_script")) {
1249 print $fh <<EOS;
1250 CREATE TABLE users (
1251 userid TEXT,
1252 passwd TEXT,
1253 uid INTEGER,
1254 gid INTEGER,
1255 homedir TEXT,
1256 shell TEXT,
1257 lastdir TEXT
1258 );
1259 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
1260
1261 CREATE TABLE groups (
1262 groupname TEXT,
1263 gid INTEGER,
1264 members TEXT
1265 );
1266 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
1267
1268 CREATE TABLE quotalimits (
1269 name TEXT NOT NULL,
1270 quota_type TEXT NOT NULL,
1271 per_session TEXT NOT NULL,
1272 limit_type TEXT NOT NULL,
1273 bytes_in_avail REAL NOT NULL,
1274 bytes_out_avail REAL NOT NULL,
1275 bytes_xfer_avail REAL NOT NULL,
1276 files_in_avail INTEGER NOT NULL,
1277 files_out_avail INTEGER NOT NULL,
1278 files_xfer_avail INTEGER NOT NULL
1279 );
1280 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'hard', 32768, 0, 0, 3, 0, 0);
1281
1282 CREATE TABLE quotatallies (
1283 name TEXT NOT NULL,
1284 quota_type TEXT NOT NULL,
1285 bytes_in_used REAL NOT NULL,
1286 bytes_out_used REAL NOT NULL,
1287 bytes_xfer_used REAL NOT NULL,
1288 files_in_used INTEGER NOT NULL,
1289 files_out_used INTEGER NOT NULL,
1290 files_xfer_used INTEGER NOT NULL
1291 );
1292 EOS
1293
1294 unless (close($fh)) {
1295 die("Can't write $db_script: $!");
1296 }
1297
1298 } else {
1299 die("Can't open $db_script: $!");
1300 }
1301
1302 my $cmd = "sqlite3 $db_file < $db_script";
1303
1304 if ($ENV{TEST_VERBOSE}) {
1305 print STDERR "Executing sqlite3: $cmd\n";
1306 }
1307
1308 my @output = `$cmd`;
1309 if (scalar(@output) &&
1310 $ENV{TEST_VERBOSE}) {
1311 print STDERR "Output: ", join('', @output), "\n";
1312 }
1313
1314 my $test_dir = File::Spec->rel2abs("$tmpdir/foo/bar/baz");
1315
1316 my $config = {
1317 PidFile => $pid_file,
1318 ScoreboardFile => $scoreboard_file,
1319 SystemLog => $log_file,
1320
1321 DefaultChdir => '~',
1322
1323 IfModules => {
1324 'mod_delay.c' => {
1325 DelayEngine => 'off',
1326 },
1327
1328 'mod_quotatab_sql.c' => [
1329 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1330 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1331 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
1332 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
1333
1334 'QuotaEngine on',
1335 "QuotaLog $log_file",
1336 "QuotaDirectoryTally on",
1337
1338 'QuotaLimitTable sql:/get-quota-limit',
1339 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
1340 ],
1341
1342 'mod_sql.c' => {
1343 SQLAuthTypes => 'plaintext',
1344 SQLBackend => 'sqlite3',
1345 SQLConnectInfo => $db_file,
1346 SQLLogFile => $log_file,
1347 SQLMinID => '0',
1348 },
1349 },
1350 };
1351
1352 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1353
1354 # Open pipes, for use between the parent and child processes. Specifically,
1355 # the child will indicate when it's done with its test by writing a message
1356 # to the parent.
1357 my ($rfh, $wfh);
1358 unless (pipe($rfh, $wfh)) {
1359 die("Can't open pipe: $!");
1360 }
1361
1362 my $ex;
1363
1364 # Fork child
1365 $self->handle_sigchld();
1366 defined(my $pid = fork()) or die("Can't fork: $!");
1367 if ($pid) {
1368 eval {
1369 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1370 $client->login($user, $passwd);
1371
1372 my ($resp_code, $resp_msg) = $client->site('mkdir', 'foo/bar/baz');
1373
1374 my $expected;
1375
1376 $expected = 200;
1377 $self->assert($expected == $resp_code,
1378 test_msg("Expected $expected, got $resp_code"));
1379
1380 $expected = "SITE mkdir command successful";
1381 $self->assert($expected eq $resp_msg,
1382 test_msg("Expected '$expected', got '$resp_msg'"));
1383
1384 $self->assert(-d $test_dir,
1385 test_msg("Directory $test_dir does not exist as expected"));
1386 };
1387
1388 if ($@) {
1389 $ex = $@;
1390 }
1391
1392 $wfh->print("done\n");
1393 $wfh->flush();
1394
1395 } else {
1396 eval { server_wait($config_file, $rfh) };
1397 if ($@) {
1398 warn($@);
1399 exit 1;
1400 }
1401
1402 exit 0;
1403 }
1404
1405 # Stop server
1406 server_stop($pid_file);
1407
1408 $self->assert_child_ok($pid);
1409
1410 if ($ex) {
1411 die($ex);
1412 }
1413
1414 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
1415
1416 my $expected;
1417
1418 $expected = 'user';
1419 $self->assert($expected eq $quota_type,
1420 test_msg("Expected '$expected', got '$quota_type'"));
1421
1422 $expected = '^(12288.0|12288)$';
1423 $self->assert(qr/$expected/, $bytes_in_used,
1424 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
1425
1426 $expected = '^(0.0|0)$';
1427 $self->assert(qr/$expected/, $bytes_out_used,
1428 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
1429
1430 $expected = '^(0.0|0)$';
1431 $self->assert(qr/$expected/, $bytes_xfer_used,
1432 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
1433
1434 $expected = 3;
1435 $self->assert($expected == $files_in_used,
1436 test_msg("Expected $expected files-in-used, got $files_in_used"));
1437
1438 $expected = 0;
1439 $self->assert($expected == $files_out_used,
1440 test_msg("Expected $expected files-out-used, got $files_out_used"));
1441
1442 $expected = 0;
1443 $self->assert($expected == $files_xfer_used,
1444 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
1445
1446 unlink($log_file);
1447 }
1448
1449 sub quotatab_site_mkdir_user_limit_files_in_exceeded_bug3660 {
1450 my $self = shift;
1451 my $tmpdir = $self->{tmpdir};
1452
1453 my $config_file = "$tmpdir/quotatab.conf";
1454 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
1455 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
1456
1457 my $log_file = File::Spec->rel2abs('tests.log');
1458
1459 my $user = 'proftpd';
1460 my $passwd = 'test';
1461 my $group = 'ftpd';
1462 my $home_dir = File::Spec->rel2abs($tmpdir);
1463 my $uid = 500;
1464 my $gid = 500;
1465
1466 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1467
1468 # Build up sqlite3 command to create users, groups tables and populate them
1469 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1470
1471 if (open(my $fh, "> $db_script")) {
1472 print $fh <<EOS;
1473 CREATE TABLE users (
1474 userid TEXT,
1475 passwd TEXT,
1476 uid INTEGER,
1477 gid INTEGER,
1478 homedir TEXT,
1479 shell TEXT,
1480 lastdir TEXT
1481 );
1482 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
1483
1484 CREATE TABLE groups (
1485 groupname TEXT,
1486 gid INTEGER,
1487 members TEXT
1488 );
1489 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
1490
1491 CREATE TABLE quotalimits (
1492 name TEXT NOT NULL,
1493 quota_type TEXT NOT NULL,
1494 per_session TEXT NOT NULL,
1495 limit_type TEXT NOT NULL,
1496 bytes_in_avail REAL NOT NULL,
1497 bytes_out_avail REAL NOT NULL,
1498 bytes_xfer_avail REAL NOT NULL,
1499 files_in_avail INTEGER NOT NULL,
1500 files_out_avail INTEGER NOT NULL,
1501 files_xfer_avail INTEGER NOT NULL
1502 );
1503 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'hard', 32768, 0, 0, 2, 0, 0);
1504
1505 CREATE TABLE quotatallies (
1506 name TEXT NOT NULL,
1507 quota_type TEXT NOT NULL,
1508 bytes_in_used REAL NOT NULL,
1509 bytes_out_used REAL NOT NULL,
1510 bytes_xfer_used REAL NOT NULL,
1511 files_in_used INTEGER NOT NULL,
1512 files_out_used INTEGER NOT NULL,
1513 files_xfer_used INTEGER NOT NULL
1514 );
1515 EOS
1516
1517 unless (close($fh)) {
1518 die("Can't write $db_script: $!");
1519 }
1520
1521 } else {
1522 die("Can't open $db_script: $!");
1523 }
1524
1525 my $cmd = "sqlite3 $db_file < $db_script";
1526
1527 if ($ENV{TEST_VERBOSE}) {
1528 print STDERR "Executing sqlite3: $cmd\n";
1529 }
1530
1531 my @output = `$cmd`;
1532 if (scalar(@output) &&
1533 $ENV{TEST_VERBOSE}) {
1534 print STDERR "Output: ", join('', @output), "\n";
1535 }
1536
1537 my $test_dir = File::Spec->rel2abs("$tmpdir/foo/bar/baz");
1538
1539 my $config = {
1540 PidFile => $pid_file,
1541 ScoreboardFile => $scoreboard_file,
1542 SystemLog => $log_file,
1543
1544 DefaultChdir => '~',
1545
1546 IfModules => {
1547 'mod_delay.c' => {
1548 DelayEngine => 'off',
1549 },
1550
1551 'mod_quotatab_sql.c' => [
1552 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1553 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1554 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
1555 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
1556
1557 'QuotaEngine on',
1558 "QuotaLog $log_file",
1559 "QuotaDirectoryTally on",
1560
1561 'QuotaLimitTable sql:/get-quota-limit',
1562 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
1563 ],
1564
1565 'mod_sql.c' => {
1566 SQLAuthTypes => 'plaintext',
1567 SQLBackend => 'sqlite3',
1568 SQLConnectInfo => $db_file,
1569 SQLLogFile => $log_file,
1570 SQLMinID => '0',
1571 },
1572 },
1573 };
1574
1575 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1576
1577 # Open pipes, for use between the parent and child processes. Specifically,
1578 # the child will indicate when it's done with its test by writing a message
1579 # to the parent.
1580 my ($rfh, $wfh);
1581 unless (pipe($rfh, $wfh)) {
1582 die("Can't open pipe: $!");
1583 }
1584
1585 my $ex;
1586
1587 # Fork child
1588 $self->handle_sigchld();
1589 defined(my $pid = fork()) or die("Can't fork: $!");
1590 if ($pid) {
1591 eval {
1592 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1593 $client->login($user, $passwd);
1594
1595 eval { $client->site('mkdir', 'foo/bar/baz') };
1596 unless ($@) {
1597 die("SITE MKDIR succeeded unexpectedly");
1598 }
1599
1600 my $resp_code = $client->response_code();
1601 my $resp_msg = $client->response_msg();
1602
1603 my $expected;
1604
1605 $expected = 550;
1606 $self->assert($expected == $resp_code,
1607 test_msg("Expected $expected, got $resp_code"));
1608
1609 $expected = "mkdir foo/bar/baz: Disk quota exceeded";
1610 $self->assert($expected eq $resp_msg,
1611 test_msg("Expected '$expected', got '$resp_msg'"));
1612
1613 $self->assert(!-d $test_dir,
1614 test_msg("Directory $test_dir does not exist as expected"));
1615 };
1616
1617 if ($@) {
1618 $ex = $@;
1619 }
1620
1621 $wfh->print("done\n");
1622 $wfh->flush();
1623
1624 } else {
1625 eval { server_wait($config_file, $rfh) };
1626 if ($@) {
1627 warn($@);
1628 exit 1;
1629 }
1630
1631 exit 0;
1632 }
1633
1634 # Stop server
1635 server_stop($pid_file);
1636
1637 $self->assert_child_ok($pid);
1638
1639 if ($ex) {
1640 die($ex);
1641 }
1642
1643 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
1644
1645 my $expected;
1646
1647 $expected = 'user';
1648 $self->assert($expected eq $quota_type,
1649 test_msg("Expected '$expected', got '$quota_type'"));
1650
1651 $expected = '^(8192.0|8192)$';
1652 $self->assert(qr/$expected/, $bytes_in_used,
1653 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
1654
1655 $expected = '^(0.0|0)$';
1656 $self->assert(qr/$expected/, $bytes_out_used,
1657 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
1658
1659 $expected = '^(0.0|0)$';
1660 $self->assert(qr/$expected/, $bytes_xfer_used,
1661 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
1662
1663 $expected = 2;
1664 $self->assert($expected == $files_in_used,
1665 test_msg("Expected $expected files-in-used, got $files_in_used"));
1666
1667 $expected = 0;
1668 $self->assert($expected == $files_out_used,
1669 test_msg("Expected $expected files-out-used, got $files_out_used"));
1670
1671 $expected = 0;
1672 $self->assert($expected == $files_xfer_used,
1673 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
1674
1675 unlink($log_file);
1676 }
1677
1678 sub quotatab_site_rmdir_user_limit_no_directory_tally_bug3660 {
1679 my $self = shift;
1680 my $tmpdir = $self->{tmpdir};
1681
1682 my $config_file = "$tmpdir/quotatab.conf";
1683 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
1684 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
1685
1686 my $log_file = File::Spec->rel2abs('tests.log');
1687
1688 my $user = 'proftpd';
1689 my $passwd = 'test';
1690 my $group = 'ftpd';
1691 my $home_dir = File::Spec->rel2abs($tmpdir);
1692 my $uid = 500;
1693 my $gid = 500;
1694
1695 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1696
1697 # Build up sqlite3 command to create users, groups tables and populate them
1698 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1699
1700 if (open(my $fh, "> $db_script")) {
1701 print $fh <<EOS;
1702 CREATE TABLE users (
1703 userid TEXT,
1704 passwd TEXT,
1705 uid INTEGER,
1706 gid INTEGER,
1707 homedir TEXT,
1708 shell TEXT,
1709 lastdir TEXT
1710 );
1711 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
1712
1713 CREATE TABLE groups (
1714 groupname TEXT,
1715 gid INTEGER,
1716 members TEXT
1717 );
1718 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
1719
1720 CREATE TABLE quotalimits (
1721 name TEXT NOT NULL,
1722 quota_type TEXT NOT NULL,
1723 per_session TEXT NOT NULL,
1724 limit_type TEXT NOT NULL,
1725 bytes_in_avail REAL NOT NULL,
1726 bytes_out_avail REAL NOT NULL,
1727 bytes_xfer_avail REAL NOT NULL,
1728 files_in_avail INTEGER NOT NULL,
1729 files_out_avail INTEGER NOT NULL,
1730 files_xfer_avail INTEGER NOT NULL
1731 );
1732 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 32, 0, 0, 2, 0, 0);
1733
1734 CREATE TABLE quotatallies (
1735 name TEXT NOT NULL,
1736 quota_type TEXT NOT NULL,
1737 bytes_in_used REAL NOT NULL,
1738 bytes_out_used REAL NOT NULL,
1739 bytes_xfer_used REAL NOT NULL,
1740 files_in_used INTEGER NOT NULL,
1741 files_out_used INTEGER NOT NULL,
1742 files_xfer_used INTEGER NOT NULL
1743 );
1744 EOS
1745
1746 unless (close($fh)) {
1747 die("Can't write $db_script: $!");
1748 }
1749
1750 } else {
1751 die("Can't open $db_script: $!");
1752 }
1753
1754 my $cmd = "sqlite3 $db_file < $db_script";
1755
1756 if ($ENV{TEST_VERBOSE}) {
1757 print STDERR "Executing sqlite3: $cmd\n";
1758 }
1759
1760 my @output = `$cmd`;
1761 if (scalar(@output) &&
1762 $ENV{TEST_VERBOSE}) {
1763 print STDERR "Output: ", join('', @output), "\n";
1764 }
1765
1766 my $test_dir = File::Spec->rel2abs("$tmpdir/foo/bar/baz");
1767 mkpath($test_dir);
1768
1769 my $config = {
1770 PidFile => $pid_file,
1771 ScoreboardFile => $scoreboard_file,
1772 SystemLog => $log_file,
1773
1774 DefaultChdir => '~',
1775
1776 IfModules => {
1777 'mod_delay.c' => {
1778 DelayEngine => 'off',
1779 },
1780
1781 'mod_quotatab_sql.c' => [
1782 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1783 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
1784 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
1785 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
1786
1787 'QuotaEngine on',
1788 "QuotaLog $log_file",
1789 'QuotaLimitTable sql:/get-quota-limit',
1790 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
1791 ],
1792
1793 'mod_sql.c' => {
1794 SQLAuthTypes => 'plaintext',
1795 SQLBackend => 'sqlite3',
1796 SQLConnectInfo => $db_file,
1797 SQLLogFile => $log_file,
1798 SQLMinID => '0',
1799 },
1800 },
1801 };
1802
1803 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1804
1805 # Open pipes, for use between the parent and child processes. Specifically,
1806 # the child will indicate when it's done with its test by writing a message
1807 # to the parent.
1808 my ($rfh, $wfh);
1809 unless (pipe($rfh, $wfh)) {
1810 die("Can't open pipe: $!");
1811 }
1812
1813 my $ex;
1814
1815 # Fork child
1816 $self->handle_sigchld();
1817 defined(my $pid = fork()) or die("Can't fork: $!");
1818 if ($pid) {
1819 eval {
1820 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1821 $client->login($user, $passwd);
1822
1823 my ($resp_code, $resp_msg) = $client->site('rmdir', 'foo/bar/baz');
1824
1825 my $expected;
1826
1827 $expected = 200;
1828 $self->assert($expected == $resp_code,
1829 test_msg("Expected $expected, got $resp_code"));
1830
1831 $expected = "SITE rmdir command successful";
1832 $self->assert($expected eq $resp_msg,
1833 test_msg("Expected '$expected', got '$resp_msg'"));
1834
1835 $self->assert(!-d $test_dir,
1836 test_msg("Directory $test_dir exists unexpectedly"));
1837 };
1838
1839 if ($@) {
1840 $ex = $@;
1841 }
1842
1843 $wfh->print("done\n");
1844 $wfh->flush();
1845
1846 } else {
1847 eval { server_wait($config_file, $rfh) };
1848 if ($@) {
1849 warn($@);
1850 exit 1;
1851 }
1852
1853 exit 0;
1854 }
1855
1856 # Stop server
1857 server_stop($pid_file);
1858
1859 $self->assert_child_ok($pid);
1860
1861 if ($ex) {
1862 die($ex);
1863 }
1864
1865 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
1866
1867 my $expected;
1868
1869 $expected = 'user';
1870 $self->assert($expected eq $quota_type,
1871 test_msg("Expected '$expected', got '$quota_type'"));
1872
1873 $expected = '^(0.0|0)$';
1874 $self->assert(qr/$expected/, $bytes_in_used,
1875 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
1876
1877 $expected = '^(0.0|0)$';
1878 $self->assert(qr/$expected/, $bytes_out_used,
1879 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
1880
1881 $expected = '^(0.0|0)$';
1882 $self->assert(qr/$expected/, $bytes_xfer_used,
1883 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
1884
1885 $expected = 0;
1886 $self->assert($expected == $files_in_used,
1887 test_msg("Expected $expected files-in-used, got $files_in_used"));
1888
1889 $expected = 0;
1890 $self->assert($expected == $files_out_used,
1891 test_msg("Expected $expected files-out-used, got $files_out_used"));
1892
1893 $expected = 0;
1894 $self->assert($expected == $files_xfer_used,
1895 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
1896
1897 unlink($log_file);
1898 }
1899
1900 sub quotatab_site_rmdir_user_limit_with_directory_tally_bug3660 {
1901 my $self = shift;
1902 my $tmpdir = $self->{tmpdir};
1903
1904 my $config_file = "$tmpdir/quotatab.conf";
1905 my $pid_file = File::Spec->rel2abs("$tmpdir/quotatab.pid");
1906 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/quotatab.scoreboard");
1907
1908 my $log_file = File::Spec->rel2abs('tests.log');
1909
1910 my $user = 'proftpd';
1911 my $passwd = 'test';
1912 my $group = 'ftpd';
1913 my $home_dir = File::Spec->rel2abs($tmpdir);
1914 my $uid = 500;
1915 my $gid = 500;
1916
1917 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1918
1919 # Build up sqlite3 command to create users, groups tables and populate them
1920 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
1921
1922 if (open(my $fh, "> $db_script")) {
1923 print $fh <<EOS;
1924 CREATE TABLE users (
1925 userid TEXT,
1926 passwd TEXT,
1927 uid INTEGER,
1928 gid INTEGER,
1929 homedir TEXT,
1930 shell TEXT,
1931 lastdir TEXT
1932 );
1933 INSERT INTO users (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
1934
1935 CREATE TABLE groups (
1936 groupname TEXT,
1937 gid INTEGER,
1938 members TEXT
1939 );
1940 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
1941
1942 CREATE TABLE quotalimits (
1943 name TEXT NOT NULL,
1944 quota_type TEXT NOT NULL,
1945 per_session TEXT NOT NULL,
1946 limit_type TEXT NOT NULL,
1947 bytes_in_avail REAL NOT NULL,
1948 bytes_out_avail REAL NOT NULL,
1949 bytes_xfer_avail REAL NOT NULL,
1950 files_in_avail INTEGER NOT NULL,
1951 files_out_avail INTEGER NOT NULL,
1952 files_xfer_avail INTEGER NOT NULL
1953 );
1954 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$user', 'user', 'false', 'soft', 16384, 0, 0, 4, 0, 0);
1955
1956 CREATE TABLE quotatallies (
1957 name TEXT NOT NULL,
1958 quota_type TEXT NOT NULL,
1959 bytes_in_used REAL NOT NULL,
1960 bytes_out_used REAL NOT NULL,
1961 bytes_xfer_used REAL NOT NULL,
1962 files_in_used INTEGER NOT NULL,
1963 files_out_used INTEGER NOT NULL,
1964 files_xfer_used INTEGER NOT NULL
1965 );
1966 INSERT INTO quotatallies (name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used) VALUES ('$user', 'user', 12288, 0, 0, 3, 0, 0);
1967 EOS
1968
1969 unless (close($fh)) {
1970 die("Can't write $db_script: $!");
1971 }
1972
1973 } else {
1974 die("Can't open $db_script: $!");
1975 }
1976
1977 my $cmd = "sqlite3 $db_file < $db_script";
1978
1979 if ($ENV{TEST_VERBOSE}) {
1980 print STDERR "Executing sqlite3: $cmd\n";
1981 }
1982
1983 my @output = `$cmd`;
1984 if (scalar(@output) &&
1985 $ENV{TEST_VERBOSE}) {
1986 print STDERR "Output: ", join('', @output), "\n";
1987 }
1988
1989 my $test_dir = File::Spec->rel2abs("$tmpdir/foo/bar/baz");
1990 mkpath($test_dir);
1991
1992 my $root_dir = File::Spec->rel2abs("$tmpdir/foo");
1993
1994 my $config = {
1995 PidFile => $pid_file,
1996 ScoreboardFile => $scoreboard_file,
1997 SystemLog => $log_file,
1998
1999 DefaultChdir => '~',
2000
2001 IfModules => {
2002 'mod_delay.c' => {
2003 DelayEngine => 'off',
2004 },
2005
2006 'mod_quotatab_sql.c' => [
2007 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
2008 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
2009 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
2010 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
2011
2012 'QuotaEngine on',
2013 "QuotaLog $log_file",
2014 'QuotaDirectoryTally on',
2015
2016 'QuotaLimitTable sql:/get-quota-limit',
2017 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
2018 ],
2019
2020 'mod_sql.c' => {
2021 SQLAuthTypes => 'plaintext',
2022 SQLBackend => 'sqlite3',
2023 SQLConnectInfo => $db_file,
2024 SQLLogFile => $log_file,
2025 SQLMinID => '0',
2026 },
2027 },
2028 };
2029
2030 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2031
2032 # Open pipes, for use between the parent and child processes. Specifically,
2033 # the child will indicate when it's done with its test by writing a message
2034 # to the parent.
2035 my ($rfh, $wfh);
2036 unless (pipe($rfh, $wfh)) {
2037 die("Can't open pipe: $!");
2038 }
2039
2040 my $ex;
2041
2042 # Fork child
2043 $self->handle_sigchld();
2044 defined(my $pid = fork()) or die("Can't fork: $!");
2045 if ($pid) {
2046 eval {
2047 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2048 $client->login($user, $passwd);
2049
2050 my ($resp_code, $resp_msg) = $client->site('rmdir', 'foo');
2051
2052 my $expected;
2053
2054 $expected = 200;
2055 $self->assert($expected == $resp_code,
2056 test_msg("Expected $expected, got $resp_code"));
2057
2058 $expected = "SITE rmdir command successful";
2059 $self->assert($expected eq $resp_msg,
2060 test_msg("Expected '$expected', got '$resp_msg'"));
2061
2062 $self->assert(!-d $root_dir,
2063 test_msg("Directory $root_dir exists unexpectedly"));
2064 };
2065
2066 if ($@) {
2067 $ex = $@;
2068 }
2069
2070 $wfh->print("done\n");
2071 $wfh->flush();
2072
2073 } else {
2074 eval { server_wait($config_file, $rfh) };
2075 if ($@) {
2076 warn($@);
2077 exit 1;
2078 }
2079
2080 exit 0;
2081 }
2082
2083 # Stop server
2084 server_stop($pid_file);
2085
2086 $self->assert_child_ok($pid);
2087
2088 if ($ex) {
2089 die($ex);
2090 }
2091
2092 my ($quota_type, $bytes_in_used, $bytes_out_used, $bytes_xfer_used, $files_in_used, $files_out_used, $files_xfer_used) = get_tally($db_file, "name = \'$user\'");
2093
2094 my $expected;
2095
2096 $expected = 'user';
2097 $self->assert($expected eq $quota_type,
2098 test_msg("Expected '$expected', got '$quota_type'"));
2099
2100 $expected = '^(0.0|0)$';
2101 $self->assert(qr/$expected/, $bytes_in_used,
2102 test_msg("Expected $expected bytes-in-used, got $bytes_in_used"));
2103
2104 $expected = '^(0.0|0)$';
2105 $self->assert(qr/$expected/, $bytes_out_used,
2106 test_msg("Expected $expected bytes-out-used, got $bytes_out_used"));
2107
2108 $expected = '^(0.0|0)$';
2109 $self->assert(qr/$expected/, $bytes_xfer_used,
2110 test_msg("Expected $expected bytes-xfer-used, got $bytes_xfer_used"));
2111
2112 $expected = 0;
2113 $self->assert($expected == $files_in_used,
2114 test_msg("Expected $expected files-in-used, got $files_in_used"));
2115
2116 $expected = 0;
2117 $self->assert($expected == $files_out_used,
2118 test_msg("Expected $expected files-out-used, got $files_out_used"));
2119
2120 $expected = 0;
2121 $self->assert($expected == $files_xfer_used,
2122 test_msg("Expected $expected files-xfer-used, got $files_xfer_used"));
2123
2124 unlink($log_file);
2125 }
2126
2127 1;