Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Modules::mod_statcache;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use Cwd;
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 statcache_file => {
21 order => ++$order,
22 test_class => [qw(forking)],
23 },
24
25 statcache_file_chrooted => {
26 order => ++$order,
27 test_class => [qw(forking rootprivs)],
28 },
29
30 statcache_file_tilde => {
31 order => ++$order,
32 test_class => [qw(forking)],
33 },
34
35 statcache_dir => {
36 order => ++$order,
37 test_class => [qw(forking)],
38 },
39
40 statcache_dir_chrooted => {
41 order => ++$order,
42 test_class => [qw(forking rootprivs)],
43 },
44
45 statcache_rel_symlink_file => {
46 order => ++$order,
47 test_class => [qw(forking)],
48 },
49
50 statcache_rel_symlink_file_chrooted => {
51 order => ++$order,
52 test_class => [qw(forking rootprivs)],
53 },
54
55 statcache_rel_symlink_dir => {
56 order => ++$order,
57 test_class => [qw(forking)],
58 },
59
60 statcache_rel_symlink_dir_chrooted => {
61 order => ++$order,
62 test_class => [qw(forking rootprivs)],
63 },
64
65 statcache_config_max_age => {
66 order => ++$order,
67 test_class => [qw(forking)],
68 },
69
70 statcache_config_capacity => {
71 order => ++$order,
72 test_class => [qw(forking)],
73 },
74
75 };
76
77 sub new {
78 return shift()->SUPER::new(@_);
79 }
80
81 sub list_tests {
82 return testsuite_get_runnable_tests($TESTS);
83 }
84
85 sub statcache_file {
86 my $self = shift;
87 my $tmpdir = $self->{tmpdir};
88 my $setup = test_setup($tmpdir, 'statcache');
89
90 my $test_file = File::Spec->rel2abs("$setup->{home_dir}/test.txt");
91 if (open(my $fh, "> $test_file")) {
92 print $fh "Hello, World!\n";
93 unless (close($fh)) {
94 die("Can't write $test_file: $!");
95 }
96
97 } else {
98 die("Can't open $test_file: $!");
99 }
100
101 my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab");
102
103 my $config = {
104 PidFile => $setup->{pid_file},
105 ScoreboardFile => $setup->{scoreboard_file},
106 SystemLog => $setup->{log_file},
107 TraceLog => $setup->{log_file},
108 Trace => 'fsio:10 statcache:20',
109
110 AuthUserFile => $setup->{auth_user_file},
111 AuthGroupFile => $setup->{auth_group_file},
112 AuthOrder => 'mod_auth_file.c',
113
114 IfModules => {
115 'mod_statcache.c' => {
116 StatCacheEngine => 'on',
117 StatCacheTable => $statcache_tab,
118 },
119
120 'mod_delay.c' => {
121 DelayEngine => 'off',
122 },
123 },
124 };
125
126 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
127 $config);
128
129 # Open pipes, for use between the parent and child processes. Specifically,
130 # the child will indicate when it's done with its test by writing a message
131 # to the parent.
132 my ($rfh, $wfh);
133 unless (pipe($rfh, $wfh)) {
134 die("Can't open pipe: $!");
135 }
136
137 my $ex;
138
139 # Fork child
140 $self->handle_sigchld();
141 defined(my $pid = fork()) or die("Can't fork: $!");
142 if ($pid) {
143 eval {
144 sleep(1);
145 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
146 $client->login($setup->{user}, $setup->{passwd});
147 my ($resp_code, $resp_msg) = $client->mlst('test.txt');
148
149 my $expected = 250;
150 $self->assert($expected == $resp_code,
151 test_msg("Expected response code $expected, got $resp_code"));
152
153 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.txt$';
154 $self->assert(qr/$expected/, $resp_msg,
155 test_msg("Expected response message '$expected', got '$resp_msg'"));
156
157 # Do the MLST again; we'll check the logs to see if mod_statcache
158 # did its job.
159 $resp_code = $resp_msg = undef;
160 ($resp_code, $resp_msg) = $client->mlst('test.txt');
161
162 $expected = 250;
163 $self->assert($expected == $resp_code,
164 test_msg("Expected response code $expected, got $resp_code"));
165
166 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.txt$';
167 $self->assert(qr/$expected/, $resp_msg,
168 test_msg("Expected response message '$expected', got '$resp_msg'"));
169
170 $client->quit();
171
172 # Now connect again, do another MLST, and see if we're still using
173 # the cached entry.
174 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
175 $client->login($setup->{user}, $setup->{passwd});
176 ($resp_code, $resp_msg) = $client->mlst('test.txt');
177
178 $expected = 250;
179 $self->assert($expected == $resp_code,
180 test_msg("Expected response code $expected, got $resp_code"));
181
182 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX\.group=\d+;UNIX\.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.txt$';
183 $self->assert(qr/$expected/, $resp_msg,
184 test_msg("Expected response message '$expected', got '$resp_msg'"));
185
186 $client->quit();
187 };
188 if ($@) {
189 $ex = $@;
190 }
191
192 $wfh->print("done\n");
193 $wfh->flush();
194
195 } else {
196 eval { server_wait($setup->{config_file}, $rfh) };
197 if ($@) {
198 warn($@);
199 exit 1;
200 }
201
202 exit 0;
203 }
204
205 # Stop server
206 server_stop($setup->{pid_file});
207 $self->assert_child_ok($pid);
208
209 eval {
210 if (open(my $fh, "< $setup->{log_file}")) {
211 my $adding_entry = 0;
212 my $cached_stat = 0;
213 my $cached_lstat = 0;
214
215 if ($^O eq 'darwin') {
216 # MacOSX-specific hack
217 $test_file = '/private' . $test_file;
218 }
219
220 while (my $line = <$fh>) {
221 chomp($line);
222
223 if ($ENV{TEST_VERBOSE}) {
224 print STDERR "# line: $line\n";
225 }
226
227 if ($line =~ /<statcache:9>/) {
228 if ($line =~ /adding entry.*?type file/) {
229 $adding_entry++;
230 next;
231 }
232 }
233
234 if ($line =~ /<statcache:11>/) {
235 if ($cached_stat == 0 &&
236 $line =~ /using cached stat.*?path '$test_file'/) {
237 $cached_stat++;
238 next;
239 }
240
241 if ($cached_lstat == 0 &&
242 $line =~ /using cached lstat.*?path '$test_file'/) {
243 $cached_lstat++;
244 next;
245 }
246 }
247
248 if ($adding_entry >= 2 &&
249 $cached_stat == 1 &&
250 $cached_lstat == 1) {
251 last;
252 }
253 }
254
255 close($fh);
256
257 $self->assert($adding_entry >= 2 &&
258 $cached_stat == 1 &&
259 $cached_lstat == 1,
260 test_msg("Did not see expected 'statcache' TraceLog messages"));
261
262 } else {
263 die("Can't read $setup->{log_file}: $!");
264 }
265 };
266 if ($@) {
267 $ex = $@;
268 }
269
270 test_cleanup($setup->{log_file}, $ex);
271 }
272
273 sub statcache_file_chrooted {
274 my $self = shift;
275 my $tmpdir = $self->{tmpdir};
276
277 my $config_file = "$tmpdir/statcache.conf";
278 my $pid_file = File::Spec->rel2abs("$tmpdir/statcache.pid");
279 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/statcache.scoreboard");
280
281 my $log_file = test_get_logfile();
282
283 my $auth_user_file = File::Spec->rel2abs("$tmpdir/statcache.passwd");
284 my $auth_group_file = File::Spec->rel2abs("$tmpdir/statcache.group");
285
286 my $user = 'proftpd';
287 my $passwd = 'test';
288 my $group = 'ftpd';
289 my $home_dir = File::Spec->rel2abs($tmpdir);
290 my $uid = 500;
291 my $gid = 500;
292
293 # Make sure that, if we're running as root, that the home directory has
294 # permissions/privs set for the account we create
295 if ($< == 0) {
296 unless (chmod(0755, $home_dir)) {
297 die("Can't set perms on $home_dir to 0755: $!");
298 }
299
300 unless (chown($uid, $gid, $home_dir)) {
301 die("Can't set owner of $home_dir to $uid/$gid: $!");
302 }
303 }
304
305 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
306 '/bin/bash');
307 auth_group_write($auth_group_file, $group, $gid, $user);
308
309 my $test_file = File::Spec->rel2abs("$home_dir/test.txt");
310 if (open(my $fh, "> $test_file")) {
311 print $fh "Hello, World!\n";
312 unless (close($fh)) {
313 die("Can't write $test_file: $!");
314 }
315
316 } else {
317 die("Can't open $test_file: $!");
318 }
319
320 my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab");
321
322 my $config = {
323 PidFile => $pid_file,
324 ScoreboardFile => $scoreboard_file,
325 SystemLog => $log_file,
326 TraceLog => $log_file,
327 Trace => 'fsio:10 statcache:20',
328
329 AuthUserFile => $auth_user_file,
330 AuthGroupFile => $auth_group_file,
331 AuthOrder => 'mod_auth_file.c',
332
333 DefaultRoot => '~',
334
335 IfModules => {
336 'mod_statcache.c' => {
337 StatCacheEngine => 'on',
338 StatCacheTable => $statcache_tab,
339 },
340
341 'mod_delay.c' => {
342 DelayEngine => 'off',
343 },
344 },
345 };
346
347 my ($port, $config_user, $config_group) = config_write($config_file, $config);
348
349 # Open pipes, for use between the parent and child processes. Specifically,
350 # the child will indicate when it's done with its test by writing a message
351 # to the parent.
352 my ($rfh, $wfh);
353 unless (pipe($rfh, $wfh)) {
354 die("Can't open pipe: $!");
355 }
356
357 my $ex;
358
359 # Fork child
360 $self->handle_sigchld();
361 defined(my $pid = fork()) or die("Can't fork: $!");
362 if ($pid) {
363 eval {
364 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
365 $client->login($user, $passwd);
366 my ($resp_code, $resp_msg) = $client->mlst('test.txt');
367
368 my $expected;
369
370 $expected = 250;
371 $self->assert($expected == $resp_code,
372 test_msg("Expected response code $expected, got $resp_code"));
373
374 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/test\.txt$';
375 $self->assert(qr/$expected/, $resp_msg,
376 test_msg("Expected response message '$expected', got '$resp_msg'"));
377
378 # Do the MLST again; we'll check the logs to see if mod_statcache
379 # did its job.
380 $resp_code = $resp_msg = undef;
381 ($resp_code, $resp_msg) = $client->mlst('test.txt');
382
383 $expected = 250;
384 $self->assert($expected == $resp_code,
385 test_msg("Expected response code $expected, got $resp_code"));
386
387 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/test\.txt$';
388 $self->assert(qr/$expected/, $resp_msg,
389 test_msg("Expected response message '$expected', got '$resp_msg'"));
390
391 $client->quit();
392
393 # Now connect again, do another MLST, and see if we're still using
394 # the cached entry.
395 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
396 $client->login($user, $passwd);
397 ($resp_code, $resp_msg) = $client->mlst('test.txt');
398
399 $expected = 250;
400 $self->assert($expected == $resp_code,
401 test_msg("Expected response code $expected, got $resp_code"));
402
403 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/test\.txt$';
404 $self->assert(qr/$expected/, $resp_msg,
405 test_msg("Expected response message '$expected', got '$resp_msg'"));
406
407 $client->quit();
408 };
409
410 if ($@) {
411 $ex = $@;
412 }
413
414 $wfh->print("done\n");
415 $wfh->flush();
416
417 } else {
418 eval { server_wait($config_file, $rfh) };
419 if ($@) {
420 warn($@);
421 exit 1;
422 }
423
424 exit 0;
425 }
426
427 # Stop server
428 server_stop($pid_file);
429
430 $self->assert_child_ok($pid);
431
432 eval {
433 if (open(my $fh, "< $log_file")) {
434 my $adding_entry = 0;
435 my $cached_stat = 0;
436 my $cached_lstat = 0;
437
438 # Since we're chrooted, the expected path is now different.
439 $test_file = '/test.txt';
440
441 while (my $line = <$fh>) {
442 if ($line =~ /<statcache:9>/) {
443 if ($line =~ /adding entry.*?type file/) {
444 $adding_entry++;
445 next;
446 }
447 }
448
449 if ($line =~ /<statcache:11>/) {
450 if ($cached_stat == 0 &&
451 $line =~ /using cached stat.*?path '$test_file'/) {
452 $cached_stat++;
453 next;
454 }
455
456 if ($cached_lstat == 0 &&
457 $line =~ /using cached lstat.*?path '$test_file'/) {
458 $cached_lstat++;
459 next;
460 }
461 }
462
463 if ($adding_entry >= 2 &&
464 $cached_stat == 1 &&
465 $cached_lstat == 1) {
466 last;
467 }
468 }
469
470 close($fh);
471
472 $self->assert($adding_entry >= 2 &&
473 $cached_stat == 1 &&
474 $cached_lstat == 1,
475 test_msg("Did not see expected 'statcache' TraceLog messages"));
476
477 } else {
478 die("Can't read $log_file: $!");
479 }
480 };
481 if ($@) {
482 $ex = $@;
483 }
484
485 if ($ex) {
486 test_append_logfile($log_file, $ex);
487 unlink($log_file);
488
489 die($ex);
490 }
491
492 unlink($log_file);
493 }
494
495 sub statcache_file_tilde {
496 my $self = shift;
497 my $tmpdir = $self->{tmpdir};
498
499 my $config_file = "$tmpdir/statcache.conf";
500 my $pid_file = File::Spec->rel2abs("$tmpdir/statcache.pid");
501 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/statcache.scoreboard");
502
503 my $log_file = test_get_logfile();
504
505 my $auth_user_file = File::Spec->rel2abs("$tmpdir/statcache.passwd");
506 my $auth_group_file = File::Spec->rel2abs("$tmpdir/statcache.group");
507
508 my $user = 'proftpd';
509 my $passwd = 'test';
510 my $group = 'ftpd';
511 my $home_dir = File::Spec->rel2abs($tmpdir);
512 my $uid = 500;
513 my $gid = 500;
514
515 # Make sure that, if we're running as root, that the home directory has
516 # permissions/privs set for the account we create
517 if ($< == 0) {
518 unless (chmod(0755, $home_dir)) {
519 die("Can't set perms on $home_dir to 0755: $!");
520 }
521
522 unless (chown($uid, $gid, $home_dir)) {
523 die("Can't set owner of $home_dir to $uid/$gid: $!");
524 }
525 }
526
527 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
528 '/bin/bash');
529 auth_group_write($auth_group_file, $group, $gid, $user);
530
531 my $test_file = File::Spec->rel2abs("$home_dir/~test.txt");
532 if (open(my $fh, "> $test_file")) {
533 print $fh "Hello, World!\n";
534 unless (close($fh)) {
535 die("Can't write $test_file: $!");
536 }
537
538 } else {
539 die("Can't open $test_file: $!");
540 }
541
542 my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab");
543
544 my $config = {
545 PidFile => $pid_file,
546 ScoreboardFile => $scoreboard_file,
547 SystemLog => $log_file,
548 TraceLog => $log_file,
549 Trace => 'fsio:10 statcache:20',
550
551 AuthUserFile => $auth_user_file,
552 AuthGroupFile => $auth_group_file,
553 AuthOrder => 'mod_auth_file.c',
554
555 IfModules => {
556 'mod_statcache.c' => {
557 StatCacheEngine => 'on',
558 StatCacheTable => $statcache_tab,
559 },
560
561 'mod_delay.c' => {
562 DelayEngine => 'off',
563 },
564 },
565 };
566
567 my ($port, $config_user, $config_group) = config_write($config_file, $config);
568
569 # Open pipes, for use between the parent and child processes. Specifically,
570 # the child will indicate when it's done with its test by writing a message
571 # to the parent.
572 my ($rfh, $wfh);
573 unless (pipe($rfh, $wfh)) {
574 die("Can't open pipe: $!");
575 }
576
577 my $ex;
578
579 # Fork child
580 $self->handle_sigchld();
581 defined(my $pid = fork()) or die("Can't fork: $!");
582 if ($pid) {
583 eval {
584 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
585 $client->login($user, $passwd);
586 my ($resp_code, $resp_msg) = $client->mlst('~test.txt');
587
588 my $expected;
589
590 $expected = 250;
591 $self->assert($expected == $resp_code,
592 test_msg("Expected response code $expected, got $resp_code"));
593
594 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/~test\.txt$';
595 $self->assert(qr/$expected/, $resp_msg,
596 test_msg("Expected response message '$expected', got '$resp_msg'"));
597
598 # Do the MLST again; we'll check the logs to see if mod_statcache
599 # did its job.
600 $resp_code = $resp_msg = undef;
601 ($resp_code, $resp_msg) = $client->mlst('~test.txt');
602
603 $expected = 250;
604 $self->assert($expected == $resp_code,
605 test_msg("Expected response code $expected, got $resp_code"));
606
607 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/~test\.txt$';
608 $self->assert(qr/$expected/, $resp_msg,
609 test_msg("Expected response message '$expected', got '$resp_msg'"));
610
611 $client->quit();
612
613 # Now connect again, do another MLST, and see if we're still using
614 # the cached entry.
615 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
616 $client->login($user, $passwd);
617 ($resp_code, $resp_msg) = $client->mlst('~test.txt');
618
619 $expected = 250;
620 $self->assert($expected == $resp_code,
621 test_msg("Expected response code $expected, got $resp_code"));
622
623 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/~test\.txt$';
624 $self->assert(qr/$expected/, $resp_msg,
625 test_msg("Expected response message '$expected', got '$resp_msg'"));
626
627 $client->quit();
628 };
629
630 if ($@) {
631 $ex = $@;
632 }
633
634 $wfh->print("done\n");
635 $wfh->flush();
636
637 } else {
638 eval { server_wait($config_file, $rfh) };
639 if ($@) {
640 warn($@);
641 exit 1;
642 }
643
644 exit 0;
645 }
646
647 # Stop server
648 server_stop($pid_file);
649
650 $self->assert_child_ok($pid);
651
652 eval {
653 if (open(my $fh, "< $log_file")) {
654 my $adding_entry = 0;
655 my $cached_stat = 0;
656 my $cached_lstat = 0;
657
658 if ($^O eq 'darwin') {
659 # MacOSX-specific hack
660 $test_file = '/private' . $test_file;
661 }
662
663 while (my $line = <$fh>) {
664 if ($line =~ /<statcache:9>/) {
665 if ($line =~ /adding entry.*?type file/) {
666 $adding_entry++;
667 next;
668 }
669 }
670
671 if ($line =~ /<statcache:11>/) {
672 if ($cached_stat == 0 &&
673 $line =~ /using cached stat.*?path '$test_file'/) {
674 $cached_stat++;
675 next;
676 }
677
678 if ($cached_lstat == 0 &&
679 $line =~ /using cached lstat.*?path '$test_file'/) {
680 $cached_lstat++;
681 next;
682 }
683 }
684
685 if ($adding_entry >= 2 &&
686 $cached_stat == 1 &&
687 $cached_lstat == 1) {
688 last;
689 }
690 }
691
692 close($fh);
693
694 $self->assert($adding_entry >= 2 &&
695 $cached_stat == 1 &&
696 $cached_lstat == 1,
697 test_msg("Did not see expected 'statcache' TraceLog messages"));
698
699 } else {
700 die("Can't read $log_file: $!");
701 }
702 };
703 if ($@) {
704 $ex = $@;
705 }
706
707 if ($ex) {
708 test_append_logfile($log_file, $ex);
709 unlink($log_file);
710
711 die($ex);
712 }
713
714 unlink($log_file);
715 }
716
717 sub statcache_dir {
718 my $self = shift;
719 my $tmpdir = $self->{tmpdir};
720
721 my $config_file = "$tmpdir/statcache.conf";
722 my $pid_file = File::Spec->rel2abs("$tmpdir/statcache.pid");
723 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/statcache.scoreboard");
724
725 my $log_file = test_get_logfile();
726
727 my $auth_user_file = File::Spec->rel2abs("$tmpdir/statcache.passwd");
728 my $auth_group_file = File::Spec->rel2abs("$tmpdir/statcache.group");
729
730 my $user = 'proftpd';
731 my $passwd = 'test';
732 my $group = 'ftpd';
733 my $home_dir = File::Spec->rel2abs($tmpdir);
734 my $uid = 500;
735 my $gid = 500;
736
737 my $test_dir = File::Spec->rel2abs("$home_dir/test.d");
738 mkpath($test_dir);
739
740 # Make sure that, if we're running as root, that the home directory has
741 # permissions/privs set for the account we create
742 if ($< == 0) {
743 unless (chmod(0755, $home_dir, $test_dir)) {
744 die("Can't set perms on $home_dir to 0755: $!");
745 }
746
747 unless (chown($uid, $gid, $home_dir, $test_dir)) {
748 die("Can't set owner of $home_dir to $uid/$gid: $!");
749 }
750 }
751
752 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
753 '/bin/bash');
754 auth_group_write($auth_group_file, $group, $gid, $user);
755
756 my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab");
757
758 my $config = {
759 PidFile => $pid_file,
760 ScoreboardFile => $scoreboard_file,
761 SystemLog => $log_file,
762 TraceLog => $log_file,
763 Trace => 'fsio:10 statcache:20',
764
765 AuthUserFile => $auth_user_file,
766 AuthGroupFile => $auth_group_file,
767 AuthOrder => 'mod_auth_file.c',
768
769 IfModules => {
770 'mod_statcache.c' => {
771 StatCacheEngine => 'on',
772 StatCacheTable => $statcache_tab,
773 },
774
775 'mod_delay.c' => {
776 DelayEngine => 'off',
777 },
778 },
779 };
780
781 my ($port, $config_user, $config_group) = config_write($config_file, $config);
782
783 # Open pipes, for use between the parent and child processes. Specifically,
784 # the child will indicate when it's done with its test by writing a message
785 # to the parent.
786 my ($rfh, $wfh);
787 unless (pipe($rfh, $wfh)) {
788 die("Can't open pipe: $!");
789 }
790
791 my $ex;
792
793 # Fork child
794 $self->handle_sigchld();
795 defined(my $pid = fork()) or die("Can't fork: $!");
796 if ($pid) {
797 eval {
798 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
799 $client->login($user, $passwd);
800 my ($resp_code, $resp_msg) = $client->mlst('test.d');
801
802 my $expected;
803
804 $expected = 250;
805 $self->assert($expected == $resp_code,
806 test_msg("Expected response code $expected, got $resp_code"));
807
808 $expected = 'modify=\d+;perm=flcdmpe;type=dir;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.d$';
809 $self->assert(qr/$expected/, $resp_msg,
810 test_msg("Expected response message '$expected', got '$resp_msg'"));
811
812 # Do the MLST again; we'll check the logs to see if mod_statcache
813 # did its job.
814 $resp_code = $resp_msg = undef;
815 ($resp_code, $resp_msg) = $client->mlst('test.d');
816
817 $expected = 250;
818 $self->assert($expected == $resp_code,
819 test_msg("Expected response code $expected, got $resp_code"));
820
821 $expected = 'modify=\d+;perm=flcdmpe;type=dir;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.d$';
822 $self->assert(qr/$expected/, $resp_msg,
823 test_msg("Expected response message '$expected', got '$resp_msg'"));
824
825 $client->quit();
826
827 # Now connect again, do another MLST, and see if we're still using
828 # the cached entry.
829 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
830 $client->login($user, $passwd);
831 ($resp_code, $resp_msg) = $client->mlst('test.d');
832
833 $expected = 250;
834 $self->assert($expected == $resp_code,
835 test_msg("Expected response code $expected, got $resp_code"));
836
837 $expected = 'modify=\d+;perm=flcdmpe;type=dir;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.d$';
838 $self->assert(qr/$expected/, $resp_msg,
839 test_msg("Expected response message '$expected', got '$resp_msg'"));
840
841 $client->quit();
842 };
843
844 if ($@) {
845 $ex = $@;
846 }
847
848 $wfh->print("done\n");
849 $wfh->flush();
850
851 } else {
852 eval { server_wait($config_file, $rfh) };
853 if ($@) {
854 warn($@);
855 exit 1;
856 }
857
858 exit 0;
859 }
860
861 # Stop server
862 server_stop($pid_file);
863
864 $self->assert_child_ok($pid);
865
866 eval {
867 if (open(my $fh, "< $log_file")) {
868 my $adding_entry = 0;
869 my $cached_stat = 0;
870 my $cached_lstat = 0;
871
872 if ($^O eq 'darwin') {
873 # MacOSX-specific hack
874 $test_dir = '/private' . $test_dir;
875 }
876
877 while (my $line = <$fh>) {
878 if ($line =~ /<statcache:9>/) {
879 if ($line =~ /adding entry.*?type dir/) {
880 $adding_entry++;
881 next;
882 }
883 }
884
885 if ($line =~ /<statcache:11>/) {
886 if ($cached_stat == 0 &&
887 $line =~ /using cached stat.*?path '$test_dir'/) {
888 $cached_stat++;
889 next;
890 }
891
892 if ($cached_lstat == 0 &&
893 $line =~ /using cached lstat.*?path '$test_dir'/) {
894 $cached_lstat++;
895 next;
896 }
897 }
898
899 if ($adding_entry >= 2 &&
900 $cached_stat == 1 &&
901 $cached_lstat == 1) {
902 last;
903 }
904 }
905
906 close($fh);
907
908 $self->assert($adding_entry >= 2 &&
909 $cached_stat == 1 &&
910 $cached_lstat == 1,
911 test_msg("Did not see expected 'statcache' TraceLog messages"));
912
913 } else {
914 die("Can't read $log_file: $!");
915 }
916 };
917 if ($@) {
918 $ex = $@;
919 }
920
921 if ($ex) {
922 test_append_logfile($log_file, $ex);
923 unlink($log_file);
924
925 die($ex);
926 }
927
928 unlink($log_file);
929 }
930
931 sub statcache_dir_chrooted {
932 my $self = shift;
933 my $tmpdir = $self->{tmpdir};
934
935 my $config_file = "$tmpdir/statcache.conf";
936 my $pid_file = File::Spec->rel2abs("$tmpdir/statcache.pid");
937 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/statcache.scoreboard");
938
939 my $log_file = test_get_logfile();
940
941 my $auth_user_file = File::Spec->rel2abs("$tmpdir/statcache.passwd");
942 my $auth_group_file = File::Spec->rel2abs("$tmpdir/statcache.group");
943
944 my $user = 'proftpd';
945 my $passwd = 'test';
946 my $group = 'ftpd';
947 my $home_dir = File::Spec->rel2abs($tmpdir);
948 my $uid = 500;
949 my $gid = 500;
950
951 my $test_dir = File::Spec->rel2abs("$home_dir/test.d");
952 mkpath($test_dir);
953
954 # Make sure that, if we're running as root, that the home directory has
955 # permissions/privs set for the account we create
956 if ($< == 0) {
957 unless (chmod(0755, $home_dir, $test_dir)) {
958 die("Can't set perms on $home_dir to 0755: $!");
959 }
960
961 unless (chown($uid, $gid, $home_dir, $test_dir)) {
962 die("Can't set owner of $home_dir to $uid/$gid: $!");
963 }
964 }
965
966 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
967 '/bin/bash');
968 auth_group_write($auth_group_file, $group, $gid, $user);
969
970 my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab");
971
972 my $config = {
973 PidFile => $pid_file,
974 ScoreboardFile => $scoreboard_file,
975 SystemLog => $log_file,
976 TraceLog => $log_file,
977 Trace => 'fsio:10 statcache:20',
978
979 AuthUserFile => $auth_user_file,
980 AuthGroupFile => $auth_group_file,
981 AuthOrder => 'mod_auth_file.c',
982
983 DefaultRoot => '~',
984
985 IfModules => {
986 'mod_statcache.c' => {
987 StatCacheEngine => 'on',
988 StatCacheTable => $statcache_tab,
989 },
990
991 'mod_delay.c' => {
992 DelayEngine => 'off',
993 },
994 },
995 };
996
997 my ($port, $config_user, $config_group) = config_write($config_file, $config);
998
999 # Open pipes, for use between the parent and child processes. Specifically,
1000 # the child will indicate when it's done with its test by writing a message
1001 # to the parent.
1002 my ($rfh, $wfh);
1003 unless (pipe($rfh, $wfh)) {
1004 die("Can't open pipe: $!");
1005 }
1006
1007 my $ex;
1008
1009 # Fork child
1010 $self->handle_sigchld();
1011 defined(my $pid = fork()) or die("Can't fork: $!");
1012 if ($pid) {
1013 eval {
1014 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1015 $client->login($user, $passwd);
1016 my ($resp_code, $resp_msg) = $client->mlst('test.d');
1017
1018 my $expected;
1019
1020 $expected = 250;
1021 $self->assert($expected == $resp_code,
1022 test_msg("Expected response code $expected, got $resp_code"));
1023
1024 $expected = 'modify=\d+;perm=flcdmpe;type=dir;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/test\.d$';
1025 $self->assert(qr/$expected/, $resp_msg,
1026 test_msg("Expected response message '$expected', got '$resp_msg'"));
1027
1028 # Do the MLST again; we'll check the logs to see if mod_statcache
1029 # did its job.
1030 $resp_code = $resp_msg = undef;
1031 ($resp_code, $resp_msg) = $client->mlst('test.d');
1032
1033 $expected = 250;
1034 $self->assert($expected == $resp_code,
1035 test_msg("Expected response code $expected, got $resp_code"));
1036
1037 $expected = 'modify=\d+;perm=flcdmpe;type=dir;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/test\.d$';
1038 $self->assert(qr/$expected/, $resp_msg,
1039 test_msg("Expected response message '$expected', got '$resp_msg'"));
1040
1041 $client->quit();
1042
1043 # Now connect again, do another MLST, and see if we're still using
1044 # the cached entry.
1045 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1046 $client->login($user, $passwd);
1047 ($resp_code, $resp_msg) = $client->mlst('test.d');
1048
1049 $expected = 250;
1050 $self->assert($expected == $resp_code,
1051 test_msg("Expected response code $expected, got $resp_code"));
1052
1053 $expected = 'modify=\d+;perm=flcdmpe;type=dir;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/test\.d$';
1054 $self->assert(qr/$expected/, $resp_msg,
1055 test_msg("Expected response message '$expected', got '$resp_msg'"));
1056
1057 $client->quit();
1058 };
1059
1060 if ($@) {
1061 $ex = $@;
1062 }
1063
1064 $wfh->print("done\n");
1065 $wfh->flush();
1066
1067 } else {
1068 eval { server_wait($config_file, $rfh) };
1069 if ($@) {
1070 warn($@);
1071 exit 1;
1072 }
1073
1074 exit 0;
1075 }
1076
1077 # Stop server
1078 server_stop($pid_file);
1079
1080 $self->assert_child_ok($pid);
1081
1082 eval {
1083 if (open(my $fh, "< $log_file")) {
1084 my $adding_entry = 0;
1085 my $cached_stat = 0;
1086 my $cached_lstat = 0;
1087
1088 # Since we're chrooted, the expected path is now different.
1089 $test_dir = '/test.d';
1090
1091 while (my $line = <$fh>) {
1092 if ($line =~ /<statcache:9>/) {
1093 if ($line =~ /adding entry.*?type dir/) {
1094 $adding_entry++;
1095 next;
1096 }
1097 }
1098
1099 if ($line =~ /<statcache:11>/) {
1100 if ($cached_stat == 0 &&
1101 $line =~ /using cached stat.*?path '$test_dir'/) {
1102 $cached_stat++;
1103 next;
1104 }
1105
1106 if ($cached_lstat == 0 &&
1107 $line =~ /using cached lstat.*?path '$test_dir'/) {
1108 $cached_lstat++;
1109 next;
1110 }
1111 }
1112
1113 if ($adding_entry >= 2 &&
1114 $cached_stat == 1 &&
1115 $cached_lstat == 1) {
1116 last;
1117 }
1118 }
1119
1120 close($fh);
1121
1122 $self->assert($adding_entry >= 2 &&
1123 $cached_stat == 1 &&
1124 $cached_lstat == 1,
1125 test_msg("Did not see expected 'statcache' TraceLog messages"));
1126
1127 } else {
1128 die("Can't read $log_file: $!");
1129 }
1130 };
1131 if ($@) {
1132 $ex = $@;
1133 }
1134
1135 if ($ex) {
1136 test_append_logfile($log_file, $ex);
1137 unlink($log_file);
1138
1139 die($ex);
1140 }
1141
1142 unlink($log_file);
1143 }
1144
1145 sub statcache_rel_symlink_file {
1146 my $self = shift;
1147 my $tmpdir = $self->{tmpdir};
1148
1149 my $config_file = "$tmpdir/statcache.conf";
1150 my $pid_file = File::Spec->rel2abs("$tmpdir/statcache.pid");
1151 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/statcache.scoreboard");
1152
1153 my $log_file = test_get_logfile();
1154
1155 my $auth_user_file = File::Spec->rel2abs("$tmpdir/statcache.passwd");
1156 my $auth_group_file = File::Spec->rel2abs("$tmpdir/statcache.group");
1157
1158 my $user = 'proftpd';
1159 my $passwd = 'test';
1160 my $group = 'ftpd';
1161 my $home_dir = File::Spec->rel2abs($tmpdir);
1162 my $uid = 500;
1163 my $gid = 500;
1164
1165 # Make sure that, if we're running as root, that the home directory has
1166 # permissions/privs set for the account we create
1167 if ($< == 0) {
1168 unless (chmod(0755, $home_dir)) {
1169 die("Can't set perms on $home_dir to 0755: $!");
1170 }
1171
1172 unless (chown($uid, $gid, $home_dir)) {
1173 die("Can't set owner of $home_dir to $uid/$gid: $!");
1174 }
1175 }
1176
1177 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1178 '/bin/bash');
1179 auth_group_write($auth_group_file, $group, $gid, $user);
1180
1181 my $test_file = File::Spec->rel2abs("$home_dir/test.txt");
1182 if (open(my $fh, "> $test_file")) {
1183 print $fh "Hello, World!\n";
1184 unless (close($fh)) {
1185 die("Can't write $test_file: $!");
1186 }
1187
1188 } else {
1189 die("Can't open $test_file: $!");
1190 }
1191
1192 # Change to the directory in order to create a relative path in the
1193 # symlink we need.
1194
1195 my $cwd = getcwd();
1196 unless (chdir("$home_dir")) {
1197 die("Can't chdir to $home_dir: $!");
1198 }
1199
1200 unless (symlink('test.txt', 'test.lnk')) {
1201 die("Can't symlink 'test.txt' to 'test.lnk': $!");
1202 }
1203
1204 unless (chdir($cwd)) {
1205 die("Can't chdir to $cwd: $!");
1206 }
1207
1208 my $test_symlink = File::Spec->rel2abs("$home_dir/test.lnk");
1209
1210 my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab");
1211
1212 my $config = {
1213 PidFile => $pid_file,
1214 ScoreboardFile => $scoreboard_file,
1215 SystemLog => $log_file,
1216 TraceLog => $log_file,
1217 Trace => 'fsio:10 statcache:20',
1218
1219 AuthUserFile => $auth_user_file,
1220 AuthGroupFile => $auth_group_file,
1221 AuthOrder => 'mod_auth_file.c',
1222
1223 IfModules => {
1224 'mod_statcache.c' => {
1225 StatCacheEngine => 'on',
1226 StatCacheTable => $statcache_tab,
1227 },
1228
1229 'mod_delay.c' => {
1230 DelayEngine => 'off',
1231 },
1232 },
1233 };
1234
1235 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1236
1237 # Open pipes, for use between the parent and child processes. Specifically,
1238 # the child will indicate when it's done with its test by writing a message
1239 # to the parent.
1240 my ($rfh, $wfh);
1241 unless (pipe($rfh, $wfh)) {
1242 die("Can't open pipe: $!");
1243 }
1244
1245 my $ex;
1246
1247 # Fork child
1248 $self->handle_sigchld();
1249 defined(my $pid = fork()) or die("Can't fork: $!");
1250 if ($pid) {
1251 eval {
1252 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1253 $client->login($user, $passwd);
1254 my ($resp_code, $resp_msg) = $client->mlst('test.lnk');
1255
1256 my $expected;
1257
1258 $expected = 250;
1259 $self->assert($expected == $resp_code,
1260 test_msg("Expected response code $expected, got $resp_code"));
1261
1262 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.lnk$';
1263 $self->assert(qr/$expected/, $resp_msg,
1264 test_msg("Expected response message '$expected', got '$resp_msg'"));
1265
1266 # Do the MLST again; we'll check the logs to see if mod_statcache
1267 # did its job.
1268 $resp_code = $resp_msg = undef;
1269 ($resp_code, $resp_msg) = $client->mlst('test.lnk');
1270
1271 $expected = 250;
1272 $self->assert($expected == $resp_code,
1273 test_msg("Expected response code $expected, got $resp_code"));
1274
1275 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.lnk$';
1276 $self->assert(qr/$expected/, $resp_msg,
1277 test_msg("Expected response message '$expected', got '$resp_msg'"));
1278
1279 $client->quit();
1280
1281 # Now connect again, do another MLST, and see if we're still using
1282 # the cached entry.
1283 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1284 $client->login($user, $passwd);
1285 ($resp_code, $resp_msg) = $client->mlst('test.lnk');
1286
1287 $expected = 250;
1288 $self->assert($expected == $resp_code,
1289 test_msg("Expected response code $expected, got $resp_code"));
1290
1291 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.lnk$';
1292 $self->assert(qr/$expected/, $resp_msg,
1293 test_msg("Expected response message '$expected', got '$resp_msg'"));
1294
1295 $client->quit();
1296 };
1297
1298 if ($@) {
1299 $ex = $@;
1300 }
1301
1302 $wfh->print("done\n");
1303 $wfh->flush();
1304
1305 } else {
1306 eval { server_wait($config_file, $rfh) };
1307 if ($@) {
1308 warn($@);
1309 exit 1;
1310 }
1311
1312 exit 0;
1313 }
1314
1315 # Stop server
1316 server_stop($pid_file);
1317
1318 $self->assert_child_ok($pid);
1319
1320 eval {
1321 if (open(my $fh, "< $log_file")) {
1322 my $adding_entry = 0;
1323 my $cached_stat = 0;
1324 my $cached_lstat = 0;
1325
1326 if ($^O eq 'darwin') {
1327 # MacOSX-specific hack
1328 $test_symlink = '/private' . $test_symlink;
1329 }
1330
1331 while (my $line = <$fh>) {
1332 if ($line =~ /<statcache:9>/) {
1333 if ($line =~ /adding entry.*?type file/) {
1334 $adding_entry++;
1335 next;
1336 }
1337
1338 if ($line =~ /adding entry.*?type symlink/) {
1339 $adding_entry++;
1340 next;
1341 }
1342 }
1343
1344 if ($line =~ /<statcache:11>/) {
1345 if ($cached_stat == 0 &&
1346 $line =~ /using cached stat.*?path '$test_symlink'/) {
1347 $cached_stat++;
1348 next;
1349 }
1350
1351 if ($cached_lstat == 0 &&
1352 $line =~ /using cached lstat.*?path '$test_symlink'/) {
1353 $cached_lstat++;
1354 next;
1355 }
1356 }
1357
1358 if ($adding_entry >= 2 &&
1359 $cached_stat == 1 &&
1360 $cached_lstat == 1) {
1361 last;
1362 }
1363 }
1364
1365 close($fh);
1366
1367 $self->assert($adding_entry >= 2 &&
1368 $cached_stat == 1 &&
1369 $cached_lstat == 1,
1370 test_msg("Did not see expected 'statcache' TraceLog messages"));
1371
1372 } else {
1373 die("Can't read $log_file: $!");
1374 }
1375 };
1376 if ($@) {
1377 $ex = $@;
1378 }
1379
1380 if ($ex) {
1381 test_append_logfile($log_file, $ex);
1382 unlink($log_file);
1383
1384 die($ex);
1385 }
1386
1387 unlink($log_file);
1388 }
1389
1390 sub statcache_rel_symlink_file_chrooted {
1391 my $self = shift;
1392 my $tmpdir = $self->{tmpdir};
1393
1394 my $config_file = "$tmpdir/statcache.conf";
1395 my $pid_file = File::Spec->rel2abs("$tmpdir/statcache.pid");
1396 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/statcache.scoreboard");
1397
1398 my $log_file = test_get_logfile();
1399
1400 my $auth_user_file = File::Spec->rel2abs("$tmpdir/statcache.passwd");
1401 my $auth_group_file = File::Spec->rel2abs("$tmpdir/statcache.group");
1402
1403 my $user = 'proftpd';
1404 my $passwd = 'test';
1405 my $group = 'ftpd';
1406 my $home_dir = File::Spec->rel2abs($tmpdir);
1407 my $uid = 500;
1408 my $gid = 500;
1409
1410 # Make sure that, if we're running as root, that the home directory has
1411 # permissions/privs set for the account we create
1412 if ($< == 0) {
1413 unless (chmod(0755, $home_dir)) {
1414 die("Can't set perms on $home_dir to 0755: $!");
1415 }
1416
1417 unless (chown($uid, $gid, $home_dir)) {
1418 die("Can't set owner of $home_dir to $uid/$gid: $!");
1419 }
1420 }
1421
1422 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1423 '/bin/bash');
1424 auth_group_write($auth_group_file, $group, $gid, $user);
1425
1426 my $test_file = File::Spec->rel2abs("$home_dir/test.txt");
1427 if (open(my $fh, "> $test_file")) {
1428 print $fh "Hello, World!\n";
1429 unless (close($fh)) {
1430 die("Can't write $test_file: $!");
1431 }
1432
1433 } else {
1434 die("Can't open $test_file: $!");
1435 }
1436
1437 # Change to the directory in order to create a relative path in the
1438 # symlink we need.
1439
1440 my $cwd = getcwd();
1441 unless (chdir("$home_dir")) {
1442 die("Can't chdir to $home_dir: $!");
1443 }
1444
1445 unless (symlink('test.txt', 'test.lnk')) {
1446 die("Can't symlink 'test.txt' to 'test.lnk': $!");
1447 }
1448
1449 unless (chdir($cwd)) {
1450 die("Can't chdir to $cwd: $!");
1451 }
1452
1453 my $test_symlink = File::Spec->rel2abs("$home_dir/test.lnk");
1454
1455 my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab");
1456
1457 my $config = {
1458 PidFile => $pid_file,
1459 ScoreboardFile => $scoreboard_file,
1460 SystemLog => $log_file,
1461 TraceLog => $log_file,
1462 Trace => 'fsio:10 statcache:20',
1463
1464 AuthUserFile => $auth_user_file,
1465 AuthGroupFile => $auth_group_file,
1466 AuthOrder => 'mod_auth_file.c',
1467
1468 DefaultRoot => '~',
1469
1470 IfModules => {
1471 'mod_statcache.c' => {
1472 StatCacheEngine => 'on',
1473 StatCacheTable => $statcache_tab,
1474 },
1475
1476 'mod_delay.c' => {
1477 DelayEngine => 'off',
1478 },
1479 },
1480 };
1481
1482 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1483
1484 # Open pipes, for use between the parent and child processes. Specifically,
1485 # the child will indicate when it's done with its test by writing a message
1486 # to the parent.
1487 my ($rfh, $wfh);
1488 unless (pipe($rfh, $wfh)) {
1489 die("Can't open pipe: $!");
1490 }
1491
1492 my $ex;
1493
1494 # Fork child
1495 $self->handle_sigchld();
1496 defined(my $pid = fork()) or die("Can't fork: $!");
1497 if ($pid) {
1498 eval {
1499 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1500 $client->login($user, $passwd);
1501 my ($resp_code, $resp_msg) = $client->mlst('test.lnk');
1502
1503 my $expected;
1504
1505 $expected = 250;
1506 $self->assert($expected == $resp_code,
1507 test_msg("Expected response code $expected, got $resp_code"));
1508
1509 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/test\.lnk$';
1510 $self->assert(qr/$expected/, $resp_msg,
1511 test_msg("Expected response message '$expected', got '$resp_msg'"));
1512
1513 # Do the MLST again; we'll check the logs to see if mod_statcache
1514 # did its job.
1515 $resp_code = $resp_msg = undef;
1516 ($resp_code, $resp_msg) = $client->mlst('test.lnk');
1517
1518 $expected = 250;
1519 $self->assert($expected == $resp_code,
1520 test_msg("Expected response code $expected, got $resp_code"));
1521
1522 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/test\.lnk$';
1523 $self->assert(qr/$expected/, $resp_msg,
1524 test_msg("Expected response message '$expected', got '$resp_msg'"));
1525
1526 $client->quit();
1527
1528 # Now connect again, do another MLST, and see if we're still using
1529 # the cached entry.
1530 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1531 $client->login($user, $passwd);
1532 ($resp_code, $resp_msg) = $client->mlst('test.lnk');
1533
1534 $expected = 250;
1535 $self->assert($expected == $resp_code,
1536 test_msg("Expected response code $expected, got $resp_code"));
1537
1538 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/test\.lnk$';
1539 $self->assert(qr/$expected/, $resp_msg,
1540 test_msg("Expected response message '$expected', got '$resp_msg'"));
1541
1542 $client->quit();
1543 };
1544
1545 if ($@) {
1546 $ex = $@;
1547 }
1548
1549 $wfh->print("done\n");
1550 $wfh->flush();
1551
1552 } else {
1553 eval { server_wait($config_file, $rfh) };
1554 if ($@) {
1555 warn($@);
1556 exit 1;
1557 }
1558
1559 exit 0;
1560 }
1561
1562 # Stop server
1563 server_stop($pid_file);
1564
1565 $self->assert_child_ok($pid);
1566
1567 eval {
1568 if (open(my $fh, "< $log_file")) {
1569 my $adding_entry = 0;
1570 my $cached_stat = 0;
1571 my $cached_lstat = 0;
1572
1573 # Since we're chrooted, the expected path is now different.
1574 $test_symlink = '/test.lnk';
1575
1576 while (my $line = <$fh>) {
1577 if ($line =~ /<statcache:9>/) {
1578 if ($line =~ /adding entry.*?type file/) {
1579 $adding_entry++;
1580 next;
1581 }
1582
1583 if ($line =~ /adding entry.*?type symlink/) {
1584 $adding_entry++;
1585 next;
1586 }
1587 }
1588
1589 if ($line =~ /<statcache:11>/) {
1590 if ($cached_stat == 0 &&
1591 $line =~ /using cached stat.*?path '$test_symlink'/) {
1592 $cached_stat++;
1593 next;
1594 }
1595
1596 if ($cached_lstat == 0 &&
1597 $line =~ /using cached lstat.*?path '$test_symlink'/) {
1598 $cached_lstat++;
1599 next;
1600 }
1601 }
1602
1603 if ($adding_entry >= 2 &&
1604 $cached_stat == 1 &&
1605 $cached_lstat == 1) {
1606 last;
1607 }
1608 }
1609
1610 close($fh);
1611
1612 $self->assert($adding_entry >= 2 &&
1613 $cached_stat == 1 &&
1614 $cached_lstat == 1,
1615 test_msg("Did not see expected 'statcache' TraceLog messages"));
1616
1617 } else {
1618 die("Can't read $log_file: $!");
1619 }
1620 };
1621 if ($@) {
1622 $ex = $@;
1623 }
1624
1625 if ($ex) {
1626 test_append_logfile($log_file, $ex);
1627 unlink($log_file);
1628
1629 die($ex);
1630 }
1631
1632 unlink($log_file);
1633 }
1634
1635 sub statcache_rel_symlink_dir {
1636 my $self = shift;
1637 my $tmpdir = $self->{tmpdir};
1638
1639 my $config_file = "$tmpdir/statcache.conf";
1640 my $pid_file = File::Spec->rel2abs("$tmpdir/statcache.pid");
1641 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/statcache.scoreboard");
1642
1643 my $log_file = test_get_logfile();
1644
1645 my $auth_user_file = File::Spec->rel2abs("$tmpdir/statcache.passwd");
1646 my $auth_group_file = File::Spec->rel2abs("$tmpdir/statcache.group");
1647
1648 my $user = 'proftpd';
1649 my $passwd = 'test';
1650 my $group = 'ftpd';
1651 my $home_dir = File::Spec->rel2abs($tmpdir);
1652 my $uid = 500;
1653 my $gid = 500;
1654
1655 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
1656 mkpath($test_dir);
1657
1658 # Make sure that, if we're running as root, that the home directory has
1659 # permissions/privs set for the account we create
1660 if ($< == 0) {
1661 unless (chmod(0755, $home_dir, $test_dir)) {
1662 die("Can't set perms on $home_dir to 0755: $!");
1663 }
1664
1665 unless (chown($uid, $gid, $home_dir, $test_dir)) {
1666 die("Can't set owner of $home_dir to $uid/$gid: $!");
1667 }
1668 }
1669
1670 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1671 '/bin/bash');
1672 auth_group_write($auth_group_file, $group, $gid, $user);
1673
1674 # Change to the directory in order to create a relative path in the
1675 # symlink we need.
1676
1677 my $cwd = getcwd();
1678 unless (chdir("$home_dir")) {
1679 die("Can't chdir to $home_dir: $!");
1680 }
1681
1682 unless (symlink('test.d', 'test.lnk')) {
1683 die("Can't symlink 'test.d' to 'test.lnk': $!");
1684 }
1685
1686 unless (chdir($cwd)) {
1687 die("Can't chdir to $cwd: $!");
1688 }
1689
1690 my $test_symlink = File::Spec->rel2abs("$home_dir/test.lnk");
1691
1692 my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab");
1693
1694 my $config = {
1695 PidFile => $pid_file,
1696 ScoreboardFile => $scoreboard_file,
1697 SystemLog => $log_file,
1698 TraceLog => $log_file,
1699 Trace => 'fsio:10 statcache:20',
1700
1701 AuthUserFile => $auth_user_file,
1702 AuthGroupFile => $auth_group_file,
1703 AuthOrder => 'mod_auth_file.c',
1704
1705 IfModules => {
1706 'mod_statcache.c' => {
1707 StatCacheEngine => 'on',
1708 StatCacheTable => $statcache_tab,
1709 },
1710
1711 'mod_delay.c' => {
1712 DelayEngine => 'off',
1713 },
1714 },
1715 };
1716
1717 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1718
1719 # Open pipes, for use between the parent and child processes. Specifically,
1720 # the child will indicate when it's done with its test by writing a message
1721 # to the parent.
1722 my ($rfh, $wfh);
1723 unless (pipe($rfh, $wfh)) {
1724 die("Can't open pipe: $!");
1725 }
1726
1727 my $ex;
1728
1729 # Fork child
1730 $self->handle_sigchld();
1731 defined(my $pid = fork()) or die("Can't fork: $!");
1732 if ($pid) {
1733 eval {
1734 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1735 $client->login($user, $passwd);
1736 my ($resp_code, $resp_msg) = $client->mlst('test.lnk');
1737
1738 my $expected;
1739
1740 $expected = 250;
1741 $self->assert($expected == $resp_code,
1742 test_msg("Expected response code $expected, got $resp_code"));
1743
1744 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=dir;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.lnk$';
1745 $self->assert(qr/$expected/, $resp_msg,
1746 test_msg("Expected response message '$expected', got '$resp_msg'"));
1747
1748 # Do the MLST again; we'll check the logs to see if mod_statcache
1749 # did its job.
1750 $resp_code = $resp_msg = undef;
1751 ($resp_code, $resp_msg) = $client->mlst('test.lnk');
1752
1753 $expected = 250;
1754 $self->assert($expected == $resp_code,
1755 test_msg("Expected response code $expected, got $resp_code"));
1756
1757 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=dir;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.lnk$';
1758 $self->assert(qr/$expected/, $resp_msg,
1759 test_msg("Expected response message '$expected', got '$resp_msg'"));
1760
1761 $client->quit();
1762
1763 # Now connect again, do another MLST, and see if we're still using
1764 # the cached entry.
1765 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1766 $client->login($user, $passwd);
1767 ($resp_code, $resp_msg) = $client->mlst('test.lnk');
1768
1769 $expected = 250;
1770 $self->assert($expected == $resp_code,
1771 test_msg("Expected response code $expected, got $resp_code"));
1772
1773 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=dir;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.lnk$';
1774 $self->assert(qr/$expected/, $resp_msg,
1775 test_msg("Expected response message '$expected', got '$resp_msg'"));
1776
1777 $client->quit();
1778 };
1779
1780 if ($@) {
1781 $ex = $@;
1782 }
1783
1784 $wfh->print("done\n");
1785 $wfh->flush();
1786
1787 } else {
1788 eval { server_wait($config_file, $rfh) };
1789 if ($@) {
1790 warn($@);
1791 exit 1;
1792 }
1793
1794 exit 0;
1795 }
1796
1797 # Stop server
1798 server_stop($pid_file);
1799
1800 $self->assert_child_ok($pid);
1801
1802 eval {
1803 if (open(my $fh, "< $log_file")) {
1804 my $adding_entry = 0;
1805 my $cached_stat = 0;
1806 my $cached_lstat = 0;
1807
1808 if ($^O eq 'darwin') {
1809 # MacOSX-specific hack
1810 $test_symlink = '/private' . $test_symlink;
1811 }
1812
1813 while (my $line = <$fh>) {
1814 if ($line =~ /<statcache:9>/) {
1815 if ($line =~ /adding entry.*?type dir/) {
1816 $adding_entry++;
1817 next;
1818 }
1819
1820 if ($line =~ /adding entry.*?type symlink/) {
1821 $adding_entry++;
1822 next;
1823 }
1824 }
1825
1826 if ($line =~ /<statcache:11>/) {
1827 if ($cached_stat == 0 &&
1828 $line =~ /using cached stat.*?path '$test_symlink'/) {
1829 $cached_stat++;
1830 next;
1831 }
1832
1833 if ($cached_lstat == 0 &&
1834 $line =~ /using cached lstat.*?path '$test_symlink'/) {
1835 $cached_lstat++;
1836 next;
1837 }
1838 }
1839
1840 if ($adding_entry >= 2 &&
1841 $cached_stat == 1 &&
1842 $cached_lstat == 1) {
1843 last;
1844 }
1845 }
1846
1847 close($fh);
1848
1849 $self->assert($adding_entry >= 2 &&
1850 $cached_stat == 1 &&
1851 $cached_lstat == 1,
1852 test_msg("Did not see expected 'statcache' TraceLog messages"));
1853
1854 } else {
1855 die("Can't read $log_file: $!");
1856 }
1857 };
1858 if ($@) {
1859 $ex = $@;
1860 }
1861
1862 if ($ex) {
1863 test_append_logfile($log_file, $ex);
1864 unlink($log_file);
1865
1866 die($ex);
1867 }
1868
1869 unlink($log_file);
1870 }
1871
1872 sub statcache_rel_symlink_dir_chrooted {
1873 my $self = shift;
1874 my $tmpdir = $self->{tmpdir};
1875
1876 my $config_file = "$tmpdir/statcache.conf";
1877 my $pid_file = File::Spec->rel2abs("$tmpdir/statcache.pid");
1878 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/statcache.scoreboard");
1879
1880 my $log_file = test_get_logfile();
1881
1882 my $auth_user_file = File::Spec->rel2abs("$tmpdir/statcache.passwd");
1883 my $auth_group_file = File::Spec->rel2abs("$tmpdir/statcache.group");
1884
1885 my $user = 'proftpd';
1886 my $passwd = 'test';
1887 my $group = 'ftpd';
1888 my $home_dir = File::Spec->rel2abs($tmpdir);
1889 my $uid = 500;
1890 my $gid = 500;
1891
1892 my $test_dir = File::Spec->rel2abs("$tmpdir/test.d");
1893 mkpath($test_dir);
1894
1895 # Make sure that, if we're running as root, that the home directory has
1896 # permissions/privs set for the account we create
1897 if ($< == 0) {
1898 unless (chmod(0755, $home_dir, $test_dir)) {
1899 die("Can't set perms on $home_dir to 0755: $!");
1900 }
1901
1902 unless (chown($uid, $gid, $home_dir, $test_dir)) {
1903 die("Can't set owner of $home_dir to $uid/$gid: $!");
1904 }
1905 }
1906
1907 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
1908 '/bin/bash');
1909 auth_group_write($auth_group_file, $group, $gid, $user);
1910
1911 # Change to the directory in order to create a relative path in the
1912 # symlink we need.
1913
1914 my $cwd = getcwd();
1915 unless (chdir("$home_dir")) {
1916 die("Can't chdir to $home_dir: $!");
1917 }
1918
1919 unless (symlink('test.d', 'test.lnk')) {
1920 die("Can't symlink 'test.d' to 'test.lnk': $!");
1921 }
1922
1923 unless (chdir($cwd)) {
1924 die("Can't chdir to $cwd: $!");
1925 }
1926
1927 my $test_symlink = File::Spec->rel2abs("$home_dir/test.lnk");
1928
1929 my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab");
1930
1931 my $config = {
1932 PidFile => $pid_file,
1933 ScoreboardFile => $scoreboard_file,
1934 SystemLog => $log_file,
1935 TraceLog => $log_file,
1936 Trace => 'fsio:10 statcache:20',
1937
1938 AuthUserFile => $auth_user_file,
1939 AuthGroupFile => $auth_group_file,
1940 AuthOrder => 'mod_auth_file.c',
1941
1942 DefaultRoot => '~',
1943
1944 IfModules => {
1945 'mod_statcache.c' => {
1946 StatCacheEngine => 'on',
1947 StatCacheTable => $statcache_tab,
1948 },
1949
1950 'mod_delay.c' => {
1951 DelayEngine => 'off',
1952 },
1953 },
1954 };
1955
1956 my ($port, $config_user, $config_group) = config_write($config_file, $config);
1957
1958 # Open pipes, for use between the parent and child processes. Specifically,
1959 # the child will indicate when it's done with its test by writing a message
1960 # to the parent.
1961 my ($rfh, $wfh);
1962 unless (pipe($rfh, $wfh)) {
1963 die("Can't open pipe: $!");
1964 }
1965
1966 my $ex;
1967
1968 # Fork child
1969 $self->handle_sigchld();
1970 defined(my $pid = fork()) or die("Can't fork: $!");
1971 if ($pid) {
1972 eval {
1973 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1974 $client->login($user, $passwd);
1975 my ($resp_code, $resp_msg) = $client->mlst('test.lnk');
1976
1977 my $expected;
1978
1979 $expected = 250;
1980 $self->assert($expected == $resp_code,
1981 test_msg("Expected response code $expected, got $resp_code"));
1982
1983 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=dir;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/test\.lnk$';
1984 $self->assert(qr/$expected/, $resp_msg,
1985 test_msg("Expected response message '$expected', got '$resp_msg'"));
1986
1987 # Do the MLST again; we'll check the logs to see if mod_statcache
1988 # did its job.
1989 $resp_code = $resp_msg = undef;
1990 ($resp_code, $resp_msg) = $client->mlst('test.lnk');
1991
1992 $expected = 250;
1993 $self->assert($expected == $resp_code,
1994 test_msg("Expected response code $expected, got $resp_code"));
1995
1996 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=dir;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername\=S+; \/test\.lnk$';
1997 $self->assert(qr/$expected/, $resp_msg,
1998 test_msg("Expected response message '$expected', got '$resp_msg'"));
1999
2000 $client->quit();
2001
2002 # Now connect again, do another MLST, and see if we're still using
2003 # the cached entry.
2004 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2005 $client->login($user, $passwd);
2006 ($resp_code, $resp_msg) = $client->mlst('test.lnk');
2007
2008 $expected = 250;
2009 $self->assert($expected == $resp_code,
2010 test_msg("Expected response code $expected, got $resp_code"));
2011
2012 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=dir;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/test\.lnk$';
2013 $self->assert(qr/$expected/, $resp_msg,
2014 test_msg("Expected response message '$expected', got '$resp_msg'"));
2015
2016 $client->quit();
2017 };
2018
2019 if ($@) {
2020 $ex = $@;
2021 }
2022
2023 $wfh->print("done\n");
2024 $wfh->flush();
2025
2026 } else {
2027 eval { server_wait($config_file, $rfh) };
2028 if ($@) {
2029 warn($@);
2030 exit 1;
2031 }
2032
2033 exit 0;
2034 }
2035
2036 # Stop server
2037 server_stop($pid_file);
2038
2039 $self->assert_child_ok($pid);
2040
2041 eval {
2042 if (open(my $fh, "< $log_file")) {
2043 my $adding_entry = 0;
2044 my $cached_stat = 0;
2045 my $cached_lstat = 0;
2046
2047 # Since we're chrooted, the expected path is now different.
2048 $test_symlink = '/test.lnk';
2049
2050 while (my $line = <$fh>) {
2051 if ($line =~ /<statcache:9>/) {
2052 if ($line =~ /adding entry.*?type dir/) {
2053 $adding_entry++;
2054 next;
2055 }
2056
2057 if ($line =~ /adding entry.*?type symlink/) {
2058 $adding_entry++;
2059 next;
2060 }
2061 }
2062
2063 if ($line =~ /<statcache:11>/) {
2064 if ($cached_stat == 0 &&
2065 $line =~ /using cached stat.*?path '$test_symlink'/) {
2066 $cached_stat++;
2067 next;
2068 }
2069
2070 if ($cached_lstat == 0 &&
2071 $line =~ /using cached lstat.*?path '$test_symlink'/) {
2072 $cached_lstat++;
2073 next;
2074 }
2075 }
2076
2077 if ($adding_entry >= 2 &&
2078 $cached_stat == 1 &&
2079 $cached_lstat == 1) {
2080 last;
2081 }
2082 }
2083
2084 close($fh);
2085
2086 $self->assert($adding_entry >= 2 &&
2087 $cached_stat == 1 &&
2088 $cached_lstat == 1,
2089 test_msg("Did not see expected 'statcache' TraceLog messages"));
2090
2091 } else {
2092 die("Can't read $log_file: $!");
2093 }
2094 };
2095 if ($@) {
2096 $ex = $@;
2097 }
2098
2099 if ($ex) {
2100 test_append_logfile($log_file, $ex);
2101 unlink($log_file);
2102
2103 die($ex);
2104 }
2105
2106 unlink($log_file);
2107 }
2108
2109 sub statcache_config_max_age {
2110 my $self = shift;
2111 my $tmpdir = $self->{tmpdir};
2112
2113 my $config_file = "$tmpdir/statcache.conf";
2114 my $pid_file = File::Spec->rel2abs("$tmpdir/statcache.pid");
2115 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/statcache.scoreboard");
2116
2117 my $log_file = test_get_logfile();
2118
2119 my $auth_user_file = File::Spec->rel2abs("$tmpdir/statcache.passwd");
2120 my $auth_group_file = File::Spec->rel2abs("$tmpdir/statcache.group");
2121
2122 my $user = 'proftpd';
2123 my $passwd = 'test';
2124 my $group = 'ftpd';
2125 my $home_dir = File::Spec->rel2abs($tmpdir);
2126 my $uid = 500;
2127 my $gid = 500;
2128
2129 # Make sure that, if we're running as root, that the home directory has
2130 # permissions/privs set for the account we create
2131 if ($< == 0) {
2132 unless (chmod(0755, $home_dir)) {
2133 die("Can't set perms on $home_dir to 0755: $!");
2134 }
2135
2136 unless (chown($uid, $gid, $home_dir)) {
2137 die("Can't set owner of $home_dir to $uid/$gid: $!");
2138 }
2139 }
2140
2141 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2142 '/bin/bash');
2143 auth_group_write($auth_group_file, $group, $gid, $user);
2144
2145 my $test_file = File::Spec->rel2abs("$home_dir/test.txt");
2146 if (open(my $fh, "> $test_file")) {
2147 print $fh "Hello, World!\n";
2148 unless (close($fh)) {
2149 die("Can't write $test_file: $!");
2150 }
2151
2152 } else {
2153 die("Can't open $test_file: $!");
2154 }
2155
2156 my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab");
2157 my $max_age = 3;
2158 my $timeout = 30;
2159
2160 my $config = {
2161 PidFile => $pid_file,
2162 ScoreboardFile => $scoreboard_file,
2163 SystemLog => $log_file,
2164 TraceLog => $log_file,
2165 Trace => 'fsio:10 statcache:20',
2166
2167 AuthUserFile => $auth_user_file,
2168 AuthGroupFile => $auth_group_file,
2169 AuthOrder => 'mod_auth_file.c',
2170
2171 IfModules => {
2172 'mod_statcache.c' => {
2173 StatCacheEngine => 'on',
2174 StatCacheTable => $statcache_tab,
2175 StatCacheMaxAge => $max_age,
2176 },
2177
2178 'mod_delay.c' => {
2179 DelayEngine => 'off',
2180 },
2181 },
2182 };
2183
2184 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2185
2186 # Open pipes, for use between the parent and child processes. Specifically,
2187 # the child will indicate when it's done with its test by writing a message
2188 # to the parent.
2189 my ($rfh, $wfh);
2190 unless (pipe($rfh, $wfh)) {
2191 die("Can't open pipe: $!");
2192 }
2193
2194 my $ex;
2195
2196 # Fork child
2197 $self->handle_sigchld();
2198 defined(my $pid = fork()) or die("Can't fork: $!");
2199 if ($pid) {
2200 eval {
2201 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2202 $client->login($user, $passwd);
2203 my ($resp_code, $resp_msg) = $client->mlst('test.txt');
2204
2205 my $expected;
2206
2207 $expected = 250;
2208 $self->assert($expected == $resp_code,
2209 test_msg("Expected response code $expected, got $resp_code"));
2210
2211 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.txt$';
2212 $self->assert(qr/$expected/, $resp_msg,
2213 test_msg("Expected response message '$expected', got '$resp_msg'"));
2214
2215 # Do the MLST again; we'll check the logs to see if mod_statcache
2216 # did its job.
2217 $resp_code = $resp_msg = undef;
2218 ($resp_code, $resp_msg) = $client->mlst('test.txt');
2219
2220 $expected = 250;
2221 $self->assert($expected == $resp_code,
2222 test_msg("Expected response code $expected, got $resp_code"));
2223
2224 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.txt$';
2225 $self->assert(qr/$expected/, $resp_msg,
2226 test_msg("Expected response message '$expected', got '$resp_msg'"));
2227
2228 $client->quit();
2229
2230 # Now connect again, do another MLST, and see if we're still using
2231 # the cached entry. But give enough time for the entry to expire.
2232 sleep($max_age + 1);
2233
2234 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2235 $client->login($user, $passwd);
2236 ($resp_code, $resp_msg) = $client->mlst('test.txt');
2237
2238 $expected = 250;
2239 $self->assert($expected == $resp_code,
2240 test_msg("Expected response code $expected, got $resp_code"));
2241
2242 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.txt$';
2243 $self->assert(qr/$expected/, $resp_msg,
2244 test_msg("Expected response message '$expected', got '$resp_msg'"));
2245
2246 $client->quit();
2247 };
2248
2249 if ($@) {
2250 $ex = $@;
2251 }
2252
2253 $wfh->print("done\n");
2254 $wfh->flush();
2255
2256 } else {
2257 eval { server_wait($config_file, $rfh, $timeout) };
2258 if ($@) {
2259 warn($@);
2260 exit 1;
2261 }
2262
2263 exit 0;
2264 }
2265
2266 # Stop server
2267 server_stop($pid_file);
2268
2269 $self->assert_child_ok($pid);
2270
2271 eval {
2272 if (open(my $fh, "< $log_file")) {
2273 my $adding_entry = 0;
2274 my $expired_entry = 0;
2275 my $cached_stat = 0;
2276 my $cached_lstat = 0;
2277
2278 if ($^O eq 'darwin') {
2279 # MacOSX-specific hack
2280 $test_file = '/private' . $test_file;
2281 }
2282
2283 while (my $line = <$fh>) {
2284 chomp($line);
2285 if ($line =~ /<statcache:9>/) {
2286 if ($line =~ /adding entry.*?type file/) {
2287 $adding_entry++;
2288 next;
2289 }
2290 }
2291
2292 if ($line =~ /<statcache:11>/) {
2293 if ($cached_stat == 0 &&
2294 $line =~ /using cached stat.*?path '$test_file'/) {
2295 $cached_stat++;
2296 next;
2297 }
2298
2299 if ($cached_lstat == 0 &&
2300 $line =~ /using cached lstat.*?path '$test_file'/) {
2301 $cached_lstat++;
2302 next;
2303 }
2304 }
2305
2306 if ($line =~ /<statcache:17>/) {
2307 if ($line =~ /expired cache entry.*?path '$test_file'/) {
2308 $expired_entry++;
2309 next;
2310 }
2311 }
2312
2313 if ($adding_entry >= 2 &&
2314 $expired_entry >= 1 &&
2315 $cached_stat == 1 &&
2316 $cached_lstat == 1) {
2317 last;
2318 }
2319 }
2320
2321 close($fh);
2322
2323 $self->assert($adding_entry >= 2 &&
2324 $expired_entry >= 1 &&
2325 $cached_stat == 1 &&
2326 $cached_lstat == 1,
2327 test_msg("Did not see expected 'statcache' TraceLog messages"));
2328
2329 } else {
2330 die("Can't read $log_file: $!");
2331 }
2332 };
2333 if ($@) {
2334 $ex = $@;
2335 }
2336
2337 if ($ex) {
2338 test_append_logfile($log_file, $ex);
2339 unlink($log_file);
2340
2341 die($ex);
2342 }
2343
2344 unlink($log_file);
2345 }
2346
2347 sub statcache_config_capacity {
2348 my $self = shift;
2349 my $tmpdir = $self->{tmpdir};
2350
2351 my $config_file = "$tmpdir/statcache.conf";
2352 my $pid_file = File::Spec->rel2abs("$tmpdir/statcache.pid");
2353 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/statcache.scoreboard");
2354
2355 my $log_file = test_get_logfile();
2356
2357 my $auth_user_file = File::Spec->rel2abs("$tmpdir/statcache.passwd");
2358 my $auth_group_file = File::Spec->rel2abs("$tmpdir/statcache.group");
2359
2360 my $user = 'proftpd';
2361 my $passwd = 'test';
2362 my $group = 'ftpd';
2363 my $home_dir = File::Spec->rel2abs($tmpdir);
2364 my $uid = 500;
2365 my $gid = 500;
2366
2367 # Make sure that, if we're running as root, that the home directory has
2368 # permissions/privs set for the account we create
2369 if ($< == 0) {
2370 unless (chmod(0755, $home_dir)) {
2371 die("Can't set perms on $home_dir to 0755: $!");
2372 }
2373
2374 unless (chown($uid, $gid, $home_dir)) {
2375 die("Can't set owner of $home_dir to $uid/$gid: $!");
2376 }
2377 }
2378
2379 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
2380 '/bin/bash');
2381 auth_group_write($auth_group_file, $group, $gid, $user);
2382
2383 my $test_file = File::Spec->rel2abs("$home_dir/test.txt");
2384 if (open(my $fh, "> $test_file")) {
2385 print $fh "Hello, World!\n";
2386 unless (close($fh)) {
2387 die("Can't write $test_file: $!");
2388 }
2389
2390 } else {
2391 die("Can't open $test_file: $!");
2392 }
2393
2394 my $statcache_tab = File::Spec->rel2abs("$tmpdir/statcache.tab");
2395 my $capacity = 10000;
2396 my $max_age = 5;
2397 my $timeout = 300;
2398
2399 my $config = {
2400 PidFile => $pid_file,
2401 ScoreboardFile => $scoreboard_file,
2402 SystemLog => $log_file,
2403 TraceLog => $log_file,
2404 Trace => 'fsio:10 statcache:20',
2405
2406 AuthUserFile => $auth_user_file,
2407 AuthGroupFile => $auth_group_file,
2408 AuthOrder => 'mod_auth_file.c',
2409
2410 IfModules => {
2411 'mod_statcache.c' => {
2412 StatCacheEngine => 'on',
2413 StatCacheTable => $statcache_tab,
2414 StatCacheCapacity => $capacity,
2415 },
2416
2417 'mod_delay.c' => {
2418 DelayEngine => 'off',
2419 },
2420 },
2421 };
2422
2423 my ($port, $config_user, $config_group) = config_write($config_file, $config);
2424
2425 # Open pipes, for use between the parent and child processes. Specifically,
2426 # the child will indicate when it's done with its test by writing a message
2427 # to the parent.
2428 my ($rfh, $wfh);
2429 unless (pipe($rfh, $wfh)) {
2430 die("Can't open pipe: $!");
2431 }
2432
2433 my $ex;
2434
2435 # Fork child
2436 $self->handle_sigchld();
2437 defined(my $pid = fork()) or die("Can't fork: $!");
2438 if ($pid) {
2439 eval {
2440 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2441 $client->login($user, $passwd);
2442 my ($resp_code, $resp_msg) = $client->mlst('test.txt');
2443
2444 my $expected;
2445
2446 $expected = 250;
2447 $self->assert($expected == $resp_code,
2448 test_msg("Expected response code $expected, got $resp_code"));
2449
2450 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.txt$';
2451 $self->assert(qr/$expected/, $resp_msg,
2452 test_msg("Expected response message '$expected', got '$resp_msg'"));
2453
2454 # Do the MLST again; we'll check the logs to see if mod_statcache
2455 # did its job.
2456 $resp_code = $resp_msg = undef;
2457 ($resp_code, $resp_msg) = $client->mlst('test.txt');
2458
2459 $expected = 250;
2460 $self->assert($expected == $resp_code,
2461 test_msg("Expected response code $expected, got $resp_code"));
2462
2463 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.txt$';
2464 $self->assert(qr/$expected/, $resp_msg,
2465 test_msg("Expected response message '$expected', got '$resp_msg'"));
2466
2467 $client->quit();
2468
2469 # Now connect again, do another MLST, and see if we're still using
2470 # the cached entry. But give enough time for the entry to expire.
2471 sleep($max_age + 1);
2472
2473 $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
2474 $client->login($user, $passwd);
2475 ($resp_code, $resp_msg) = $client->mlst('test.txt');
2476
2477 $expected = 250;
2478 $self->assert($expected == $resp_code,
2479 test_msg("Expected response code $expected, got $resp_code"));
2480
2481 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX.group=\d+;UNIX.groupname=\S+;UNIX.mode=\d+;UNIX.owner=\d+;UNIX.ownername=\S+; \/.*\/test\.txt$';
2482 $self->assert(qr/$expected/, $resp_msg,
2483 test_msg("Expected response message '$expected', got '$resp_msg'"));
2484
2485 $client->quit();
2486 };
2487
2488 if ($@) {
2489 $ex = $@;
2490 }
2491
2492 $wfh->print("done\n");
2493 $wfh->flush();
2494
2495 } else {
2496 eval { server_wait($config_file, $rfh, $timeout) };
2497 if ($@) {
2498 warn($@);
2499 exit 1;
2500 }
2501
2502 exit 0;
2503 }
2504
2505 # Stop server
2506 server_stop($pid_file);
2507
2508 $self->assert_child_ok($pid);
2509
2510 eval {
2511 if (open(my $fh, "< $log_file")) {
2512 my $adding_entry = 0;
2513 my $expired_entry = 0;
2514 my $cached_stat = 0;
2515 my $cached_lstat = 0;
2516
2517 if ($^O eq 'darwin') {
2518 # MacOSX-specific hack
2519 $test_file = '/private' . $test_file;
2520 }
2521
2522 while (my $line = <$fh>) {
2523 if ($line =~ /<statcache:9>/) {
2524 if ($line =~ /adding entry.*?type file/) {
2525 $adding_entry++;
2526 next;
2527 }
2528 }
2529
2530 if ($line =~ /<statcache:11>/) {
2531 if ($cached_stat == 0 &&
2532 $line =~ /using cached stat.*?path '$test_file'/) {
2533 $cached_stat++;
2534 next;
2535 }
2536
2537 if ($cached_lstat == 0 &&
2538 $line =~ /using cached lstat.*?path '$test_file'/) {
2539 $cached_lstat++;
2540 next;
2541 }
2542 }
2543
2544 if ($line =~ /<statcache:17>/) {
2545 if ($line =~ /expired cache entry.*?path '$test_file'/) {
2546 $expired_entry++;
2547 next;
2548 }
2549 }
2550
2551 if ($adding_entry >= 2 &&
2552 $expired_entry >= 1 &&
2553 $cached_stat == 1 &&
2554 $cached_lstat == 1) {
2555 last;
2556 }
2557 }
2558
2559 close($fh);
2560
2561 $self->assert($adding_entry >= 2 &&
2562 $expired_entry >= 1 &&
2563 $cached_stat == 1 &&
2564 $cached_lstat == 1,
2565 test_msg("Did not see expected 'statcache' TraceLog messages"));
2566
2567 } else {
2568 die("Can't read $log_file: $!");
2569 }
2570 };
2571 if ($@) {
2572 $ex = $@;
2573 }
2574
2575 if ($ex) {
2576 test_append_logfile($log_file, $ex);
2577 unlink($log_file);
2578
2579 die($ex);
2580 }
2581
2582 unlink($log_file);
2583 }
2584
2585 1;