This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove tmon.out in make clean
[perl5.git] / t / lib / filefind.t
CommitLineData
7e47e6ff 1#!./perl -T
81793b90
GS
2
3
4my %Expect;
5my $symlink_exists = eval { symlink("",""); 1 };
7e47e6ff
JH
6my $warn_msg;
7my $cwd;
8my $cwd_untainted;
1a3850a5
GA
9
10BEGIN {
11 chdir 't' if -d 't';
9f826d6a 12 unshift @INC => '../lib';
7e47e6ff
JH
13
14 for (keys %ENV) { # untaint ENV
9f826d6a 15 ($ENV{$_}) = $ENV{$_} =~ /(.*)/;
7e47e6ff
JH
16 }
17
18 $SIG{'__WARN__'} = sub { $warn_msg = $_[0]; warn "# Warn: $_[0]"; }
1a3850a5
GA
19}
20
950bcf42 21if ( $symlink_exists ) { print "1..193\n"; }
7e47e6ff 22else { print "1..75\n"; }
1a3850a5
GA
23
24use File::Find;
7e47e6ff 25use Cwd;
1a3850a5 26
24e8cdb8
NIS
27# Remove insecure directories from PATH
28my @path;
29my $sep = ($^O eq 'MSWin32') ? ';' : ':';
30foreach my $dir (split(/$sep/,$ENV{'PATH'}))
31 {
32 push(@path,$dir) unless -w $dir;
33 }
34$ENV{'PATH'} = join($sep,@path);
35
c80f55d1
JH
36cleanup();
37
7e47e6ff 38if ($^O eq 'MacOS') {
24e8cdb8 39 find({wanted => sub { print "ok 1\n" if $_ eq 'filefind.t'; }, untaint => 1}, ':');
7e47e6ff
JH
40 finddepth({wanted => sub { print "ok 2\n" if $_ eq 'filefind.t'; }, untaint => 1}, ':');
41} else {
42 find({wanted => sub { print "ok 1\n" if $_ eq 'filefind.t'; }, untaint => 1,
43 untaint_pattern => qr|^(.+)$|}, '.');
24e8cdb8 44 finddepth({wanted => sub { print "ok 2\n" if $_ eq 'filefind.t'; },
7e47e6ff
JH
45 untaint => 1, untaint_pattern => qr|^(.+)$|}, '.');
46}
81793b90 47
81793b90 48my $case = 2;
5eb85357 49my $FastFileTests_OK = 0;
81793b90 50
c80f55d1 51sub cleanup {
7e47e6ff
JH
52 if ($^O eq 'MacOS') {
53 if (-d ':for_find') {
54 chdir(':for_find');
55 }
56 if (-d ':fa') {
57 unlink ':fa:fa_ord',':fa:fsl',':fa:faa:faa_ord',
58 ':fa:fab:fab_ord',':fa:fab:faba:faba_ord',
59 ':fb:fb_ord',':fb:fba:fba_ord';
60 rmdir ':fa:faa';
61 rmdir ':fa:fab:faba';
62 rmdir ':fa:fab';
63 rmdir ':fa';
64 rmdir ':fb:fba';
65 rmdir ':fb';
66 chdir '::';
67 rmdir ':for_find';
68 }
69 } else {
70 if (-d 'for_find') {
71 chdir('for_find');
72 }
73 if (-d 'fa') {
74 unlink 'fa/fa_ord','fa/fsl','fa/faa/faa_ord',
75 'fa/fab/fab_ord','fa/fab/faba/faba_ord',
76 'fb/fb_ord','fb/fba/fba_ord';
77 rmdir 'fa/faa';
78 rmdir 'fa/fab/faba';
79 rmdir 'fa/fab';
80 rmdir 'fa';
81 rmdir 'fb/fba';
82 rmdir 'fb';
83 chdir '..';
84 rmdir 'for_find';
85 }
c80f55d1
JH
86 }
87}
88
81793b90 89END {
c80f55d1 90 cleanup();
81793b90
GS
91}
92
93sub Check($) {
94 $case++;
95 if ($_[0]) { print "ok $case\n"; }
96 else { print "not ok $case\n"; }
97}
98
99sub CheckDie($) {
100 $case++;
101 if ($_[0]) { print "ok $case\n"; }
102 else { print "not ok $case\n $!\n"; exit 0; }
103}
104
105sub touch {
106 CheckDie( open(my $T,'>',$_[0]) );
107}
108
109sub MkDir($$) {
110 CheckDie( mkdir($_[0],$_[1]) );
111}
112
113sub wanted {
24e8cdb8 114 print "# '$_' => 1\n";
11611c0f 115 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
81793b90 116 Check( $Expect{$_} );
5eb85357 117 if ( $FastFileTests_OK ) {
24e8cdb8 118 delete $Expect{$_}
5eb85357
GS
119 unless ( $Expect_Dir{$_} && ! -d _ );
120 } else {
24e8cdb8 121 delete $Expect{$_}
5eb85357
GS
122 unless ( $Expect_Dir{$_} && ! -d $_ );
123 }
10fbe481 124 $File::Find::prune=1 if $_ eq 'faba';
7e47e6ff 125
81793b90
GS
126}
127
57907763
GS
128sub dn_wanted {
129 my $n = $File::Find::name;
11611c0f 130 $n =~ s#\.$## if ($^O eq 'VMS' && $n ne '.');
57907763
GS
131 print "# '$n' => 1\n";
132 my $i = rindex($n,'/');
133 my $OK = exists($Expect{$n});
7e47e6ff
JH
134 unless ($^O eq 'MacOS') {
135 if ( $OK ) {
24e8cdb8 136 $OK= exists($Expect{substr($n,0,$i)}) if $i >= 0;
7e47e6ff 137 }
57907763
GS
138 }
139 Check($OK);
140 delete $Expect{$n};
141}
142
143sub d_wanted {
144 print "# '$_' => 1\n";
11611c0f 145 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
57907763
GS
146 my $i = rindex($_,'/');
147 my $OK = exists($Expect{$_});
7e47e6ff
JH
148 unless ($^O eq 'MacOS') {
149 if ( $OK ) {
150 $OK= exists($Expect{substr($_,0,$i)}) if $i >= 0;
151 }
57907763
GS
152 }
153 Check($OK);
154 delete $Expect{$_};
155}
156
7e47e6ff
JH
157sub simple_wanted {
158 print "# \$File::Find::dir => '$File::Find::dir'\n";
159 print "# \$_ => '$_'\n";
160}
161
162sub noop_wanted {}
78eac027 163
7e47e6ff
JH
164sub my_preprocess {
165 @files = @_;
166 print "# --PREPROCESS--\n";
167 print "# \$File::Find::dir => '$File::Find::dir' \n";
168 foreach $file (@files) {
169 print "# $file \n";
170 delete $Expect{$File::Find::dir}->{$file};
171 }
172 print "# --END PREPROCESS--\n";
173 Check(scalar(keys %{$Expect{$File::Find::dir}}) == 0);
174 if (scalar(keys %{$Expect{$File::Find::dir}}) == 0) {
175 delete $Expect{$File::Find::dir}
176 }
177 return @files;
178}
179
180sub my_postprocess {
181 print "# POSTPROCESS: \$File::Find::dir => '$File::Find::dir' \n";
182 delete $Expect{$File::Find::dir};
183}
184
185
186if ($^O eq 'MacOS') {
187
188 MkDir( 'for_find',0770 );
189 CheckDie(chdir(for_find));
190
191 $cwd = cwd(); # save cwd
192 ( $cwd_untainted ) = $cwd =~ m|^(.+)$|; # untaint it
193
194 MkDir( 'fa',0770 );
195 MkDir( 'fb',0770 );
196 touch(':fb:fb_ord');
197 MkDir( ':fb:fba',0770 );
198 touch(':fb:fba:fba_ord');
199 CheckDie( symlink(':fb',':fa:fsl') ) if $symlink_exists;
200 touch(':fa:fa_ord');
201
202 MkDir( ':fa:faa',0770 );
203 touch(':fa:faa:faa_ord');
204 MkDir( ':fa:fab',0770 );
205 touch(':fa:fab:fab_ord');
206 MkDir( ':fa:fab:faba',0770 );
207 touch(':fa:fab:faba:faba_ord');
208
209 %Expect = (':' => 1, 'fsl' => 1, 'fa_ord' => 1, 'fab' => 1, 'fab_ord' => 1,
210 'faba' => 1, 'faa' => 1, 'faa_ord' => 1);
211 delete $Expect{'fsl'} unless $symlink_exists;
24e8cdb8 212 %Expect_Dir = (':' => 1, 'fa' => 1, 'faa' => 1, 'fab' => 1, 'faba' => 1,
7e47e6ff
JH
213 'fb' => 1, 'fba' => 1);
214 delete @Expect_Dir{'fb','fba'} unless $symlink_exists;
24e8cdb8 215 File::Find::find( {wanted => \&wanted, untaint => 1},':fa' );
7e47e6ff
JH
216 Check( scalar(keys %Expect) == 0 );
217
9f826d6a
BM
218 print "# check re-entancy\n";
219 %Expect = (':' => 1, 'fsl' => 1, 'fa_ord' => 1, 'fab' => 1, 'fab_ord' => 1,
220 'faba' => 1, 'faa' => 1, 'faa_ord' => 1);
221 delete $Expect{'fsl'} unless $symlink_exists;
222 %Expect_Dir = (':' => 1, 'fa' => 1, 'faa' => 1, 'fab' => 1, 'faba' => 1,
223 'fb' => 1, 'fba' => 1);
224 delete @Expect_Dir{'fb','fba'} unless $symlink_exists;
225 File::Find::find( {wanted => sub {
226 wanted();
227 File::Find::find( {wanted => sub {} , untaint => 1 },':' );
228 }, untaint => 1 }, ':fa' );
229 Check( scalar(keys %Expect) == 0 );
230
7e47e6ff
JH
231 %Expect=(':fa' => 1, ':fa:fsl' => 1, ':fa:fa_ord' => 1, ':fa:fab' => 1,
232 ':fa:fab:fab_ord' => 1, ':fa:fab:faba' => 1,
233 ':fa:fab:faba:faba_ord' => 1, ':fa:faa' => 1, ':fa:faa:faa_ord' => 1);
234 delete $Expect{':fa:fsl'} unless $symlink_exists;
24e8cdb8 235 %Expect_Dir = (':fa' => 1, ':fa:faa' => 1, ':fa:fab' => 1, ':fa:fab:faba' => 1,
7e47e6ff
JH
236 ':fb' => 1, ':fb:fba' => 1);
237 delete @Expect_Dir{':fb',':fb:fba'} unless $symlink_exists;
238 File::Find::find( {wanted => \&wanted, no_chdir => 1, untaint => 1},':fa' );
239 Check( scalar(keys %Expect) == 0 );
240
241 %Expect=(':' => 1, ':fa' => 1, ':fa:fsl' => 1, ':fa:fa_ord' => 1, ':fa:fab' => 1,
242 ':fa:fab:fab_ord' => 1, ':fa:fab:faba' => 1,
243 ':fa:fab:faba:faba_ord' => 1, ':fa:faa' => 1, ':fa:faa:faa_ord' => 1,
244 ':fb' => 1, ':fb:fba' => 1, ':fb:fba:fba_ord' => 1, ':fb:fb_ord' => 1);
245 delete $Expect{':fa:fsl'} unless $symlink_exists;
24e8cdb8 246 %Expect_Dir = (':' => 1, ':fa' => 1, ':fa:faa' => 1, ':fa:fab' => 1, ':fa:fab:faba' => 1,
7e47e6ff
JH
247 ':fb' => 1, ':fb:fba' => 1);
248 delete @Expect_Dir{':fb',':fb:fba'} unless $symlink_exists;
249 File::Find::finddepth( {wanted => \&dn_wanted, untaint => 1 },':' );
250 Check( scalar(keys %Expect) == 0 );
251
252 %Expect=(':' => 1, ':fa' => 1, ':fa:fsl' => 1, ':fa:fa_ord' => 1, ':fa:fab' => 1,
253 ':fa:fab:fab_ord' => 1, ':fa:fab:faba' => 1,
254 ':fa:fab:faba:faba_ord' => 1, ':fa:faa' => 1, ':fa:faa:faa_ord' => 1,
255 ':fb' => 1, ':fb:fba' => 1, ':fb:fba:fba_ord' => 1, ':fb:fb_ord' => 1);
256 delete $Expect{':fa:fsl'} unless $symlink_exists;
24e8cdb8 257 %Expect_Dir = (':' => 1, ':fa' => 1, ':fa:faa' => 1, ':fa:fab' => 1, ':fa:fab:faba' => 1,
7e47e6ff
JH
258 ':fb' => 1, ':fb:fba' => 1);
259 delete @Expect_Dir{':fb',':fb:fba'} unless $symlink_exists;
260 File::Find::finddepth( {wanted => \&d_wanted, no_chdir => 1, untaint => 1 },':' );
261 Check( scalar(keys %Expect) == 0 );
262
263 # untaint, preprocess and postprocess tests below added by Thomas Wegner, 17-05-2001
264
265 print "# check untainting (no follow)\n";
266 # don't untaint at all
267 undef $@;
268 eval {File::Find::find( {wanted => \&simple_wanted},':fa' );};
269 print "# Died: $@";
270 Check( $@ =~ m|Insecure dependency| );
271 chdir($cwd_untainted);
272
273 undef $@;
274 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
24e8cdb8 275 untaint_pattern => qr|^(NO_MATCH)$|},':fa' );};
7e47e6ff
JH
276 print "# Died: $@";
277 Check( $@ =~ m|is still tainted| );
278 chdir($cwd_untainted);
279
280 print "# check untaint_skip (no follow)\n";
281 undef $@;
24e8cdb8 282 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1, untaint_skip => 1,
7e47e6ff
JH
283 untaint_pattern => qr|^(NO_MATCH)$|}, ':fa' );};
284 print "# Died: $@";
285 Check( $@ =~ m|insecure cwd| );
286 chdir($cwd_untainted);
287
288 print "# check preprocess\n";
289 %Expect=(
24e8cdb8 290 ':' => {fa => 1, fb => 1},
7e47e6ff
JH
291 ':fa:' => {faa => 1, fab => 1, fa_ord => 1},
292 ':fa:faa:' => {faa_ord => 1},
293 ':fa:fab:' => {faba => 1, fab_ord => 1},
24e8cdb8 294 ':fa:fab:faba:' => {faba_ord => 1},
7e47e6ff
JH
295 ':fb:' => {fba => 1, fb_ord => 1},
296 ':fb:fba:' => {fba_ord => 1}
297 );
298 File::Find::find( {wanted => \&noop_wanted, untaint => 1, preprocess => \&my_preprocess}, ':' );
299 Check( scalar(keys %Expect) == 0 );
300
301 print "# check postprocess\n";
302 %Expect=(':' => 1, ':fa:' => 1, ':fa:faa:' => 1, ':fa:fab:' => 1, ':fa:fab:faba:' => 1, ':fb:' => 1,
303 ':fb:fba:' => 1 );
304 File::Find::find( {wanted => \&noop_wanted, untaint => 1, postprocess => \&my_postprocess}, ':' );
305 Check( scalar(keys %Expect) == 0 );
306
307 # Verify that File::Find::find will call wanted even if the topdir of
308 # is a symlink to a directory, and it shouldn't follow the link
309 # unless follow is set, which it isn't in this case
310 %Expect = ('fsl' => 1);
311 %Expect_Dir = ();
312 File::Find::find( {wanted => \&wanted, untaint => 1},':fa:fsl' );
313 Check( scalar(keys %Expect) == 0 );
314
315 if ( $symlink_exists ) {
316 $FastFileTests_OK= 1;
317 %Expect=(':' => 1, 'fa_ord' => 1, 'fsl' => 1, 'fb_ord' => 1, 'fba' => 1,
318 'fba_ord' => 1, 'fab' => 1, 'fab_ord' => 1, 'faba' => 1, 'faa' => 1,
319 'faa_ord' => 1);
24e8cdb8
NIS
320 %Expect_Dir = (':' => 1, 'fa' => 1, 'faa' => 1, 'fab' => 1, 'faba' => 1,
321 'fb' => 1, 'fba' => 1);
7e47e6ff 322 File::Find::find( {wanted => \&wanted, follow_fast => 1, untaint => 1},':fa' );
24e8cdb8 323 Check( scalar(keys %Expect) == 0 );
7e47e6ff
JH
324
325 %Expect=(':fa' => 1, ':fa:fa_ord' => 1, ':fa:fsl' => 1, ':fa:fsl:fb_ord' => 1,
326 ':fa:fsl:fba' => 1, ':fa:fsl:fba:fba_ord' => 1, ':fa:fab' => 1,
327 ':fa:fab:fab_ord' => 1, ':fa:fab:faba' => 1, ':fa:fab:faba:faba_ord' => 1,
328 ':fa:faa' => 1, ':fa:faa:faa_ord' => 1);
24e8cdb8
NIS
329 %Expect_Dir = (':fa' => 1, ':fa:faa' => 1, ':fa:fab' => 1, ':fa:fab:faba' => 1,
330 ':fb' => 1, ':fb:fba' => 1);
7e47e6ff
JH
331 File::Find::find( {wanted => \&wanted, follow_fast => 1, no_chdir => 1, untaint => 1 },':fa' );
332 Check( scalar(keys %Expect) == 0 );
333
334 %Expect=(':fa' => 1, ':fa:fa_ord' => 1, ':fa:fsl' => 1, ':fa:fsl:fb_ord' => 1,
335 ':fa:fsl:fba' => 1, ':fa:fsl:fba:fba_ord' => 1, ':fa:fab' => 1,
336 ':fa:fab:fab_ord' => 1, ':fa:fab:faba' => 1, ':fa:fab:faba:faba_ord' => 1,
337 ':fa:faa' => 1, ':fa:faa:faa_ord' => 1);
24e8cdb8 338 %Expect_Dir = (':fa' => 1, ':fa:faa' => 1, ':fa:fab' => 1, ':fa:fab:faba' => 1,
7e47e6ff
JH
339 ':fb' => 1, ':fb:fba' => 1);
340 File::Find::finddepth( {wanted => \&dn_wanted, follow_fast => 1, untaint => 1 },':fa' );
341 Check( scalar(keys %Expect) == 0 );
342
343 %Expect=(':fa' => 1, ':fa:fa_ord' => 1, ':fa:fsl' => 1, ':fa:fsl:fb_ord' => 1,
344 ':fa:fsl:fba' => 1, ':fa:fsl:fba:fba_ord' => 1, ':fa:fab' => 1,
345 ':fa:fab:fab_ord' => 1, ':fa:fab:faba' => 1, ':fa:fab:faba:faba_ord' => 1,
346 ':fa:faa' => 1, ':fa:faa:faa_ord' => 1);
24e8cdb8 347 %Expect_Dir = (':fa' => 1, ':fa:faa' => 1, ':fa:fab' => 1, ':fa:fab:faba' => 1,
7e47e6ff
JH
348 ':fb' => 1, ':fb:fba' => 1);
349 File::Find::finddepth( {wanted => \&d_wanted, follow_fast => 1, no_chdir => 1, untaint => 1 },':fa' );
24e8cdb8 350 Check( scalar(keys %Expect) == 0 );
7e47e6ff
JH
351
352 # tests below added by Thomas Wegner, 17-05-2001
353
354 print "# check dangling symbolic links\n";
355 MkDir( 'dangling_dir',0770 );
356 CheckDie( symlink('dangling_dir','dangling_dir_sl') );
357 rmdir 'dangling_dir';
358 touch('dangling_file');
359 CheckDie( symlink('dangling_file',':fa:dangling_file_sl') );
360 unlink 'dangling_file';
361
362 %Expect=(':' => 1, 'fa_ord' => 1, 'fsl' => 1, 'fb_ord' => 1, 'fba' => 1,
363 'fba_ord' => 1, 'fab' => 1, 'fab_ord' => 1, 'faba' => 1, 'faba_ord' => 1,
364 'faa' => 1, 'faa_ord' => 1);
24e8cdb8 365 %Expect_Dir = (':' => 1, 'fa' => 1, 'faa' => 1, 'fab' => 1, 'faba' => 1,
7e47e6ff
JH
366 'fb' => 1, 'fba' => 1);
367 undef $warn_msg;
368 File::Find::find( {wanted => \&d_wanted, follow => 1, untaint => 1 }, 'dangling_dir_sl', ':fa' );
24e8cdb8 369 Check( $warn_msg =~ m|dangling_dir_sl is a dangling symbolic link| );
7e47e6ff
JH
370 unlink ':fa:dangling_file_sl', 'dangling_dir_sl';
371
372 print "# check recursion\n";
373 CheckDie( symlink(':fa:faa',':fa:faa:faa_sl') );
374 undef $@;
375 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1, no_chdir => 1, untaint => 1 },':fa' ); };
376 print "# Died: $@";
24e8cdb8
NIS
377 Check( $@ =~ m|:for_find:fa:faa:faa_sl is a recursive symbolic link| );
378 unlink ':fa:faa:faa_sl';
7e47e6ff
JH
379
380 print "# check follow_skip (file)\n";
381 CheckDie( symlink(':fa:fa_ord',':fa:fa_ord_sl') ); # symlink to a file
382 undef $@;
24e8cdb8 383 eval {File::Find::finddepth( {wanted => \&simple_wanted, follow => 1,follow_skip => 0,
7e47e6ff
JH
384 no_chdir => 1, untaint => 1 },':fa' );};
385 print "# Died: $@";
386 Check( $@ =~ m|:for_find:fa:fa_ord encountered a second time| );
387
388 %Expect=(':fa' => 1, ':fa:fa_ord' => 1, ':fa:fsl' => 1, ':fa:fsl:fb_ord' => 1,
389 ':fa:fsl:fba' => 1, ':fa:fsl:fba:fba_ord' => 1, ':fa:fab' => 1,
390 ':fa:fab:fab_ord' => 1, ':fa:fab:faba' => 1, ':fa:fab:faba:faba_ord' => 1,
391 ':fa:faa' => 1, ':fa:faa:faa_ord' => 1);
24e8cdb8
NIS
392 %Expect_Dir = (':fa' => 1, ':fa:faa' => 1, ':fa:fab' => 1, ':fa:fab:faba' => 1,
393 ':fb' => 1, ':fb:fba' => 1);
7e47e6ff
JH
394 File::Find::finddepth( {wanted => \&wanted, follow => 1, follow_skip => 1, no_chdir => 1,
395 untaint => 1 },':fa' );
396 Check( scalar(keys %Expect) == 0 );
397 unlink ':fa:fa_ord_sl';
398
399 print "# check follow_skip (directory)\n";
400 CheckDie( symlink(':fa:faa',':fa:faa_sl') ); # symlink to a directory
401 undef $@;
24e8cdb8 402 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1, follow_skip => 0,
7e47e6ff
JH
403 no_chdir => 1, untaint => 1 },':fa' );};
404 print "# Died: $@";
405 Check( $@ =~ m|:for_find:fa:faa: encountered a second time| );
406
407 undef $@;
24e8cdb8 408 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1, follow_skip => 1,
7e47e6ff
JH
409 no_chdir => 1, untaint => 1 },':fa' );};
410 print "# Died: $@";
24e8cdb8 411 Check( $@ =~ m|:for_find:fa:faa: encountered a second time| );
7e47e6ff
JH
412
413 %Expect=(':fa' => 1, ':fa:fa_ord' => 1, ':fa:fsl' => 1, ':fa:fsl:fb_ord' => 1,
414 ':fa:fsl:fba' => 1, ':fa:fsl:fba:fba_ord' => 1, ':fa:fab' => 1,
415 ':fa:fab:fab_ord' => 1, ':fa:fab:faba' => 1, ':fa:fab:faba:faba_ord' => 1,
416 ':fa:faa' => 1, ':fa:faa:faa_ord' => 1);
24e8cdb8
NIS
417 %Expect_Dir = (':fa' => 1, ':fa:faa' => 1, ':fa:fab' => 1, ':fa:fab:faba' => 1,
418 ':fb' => 1, ':fb:fba' => 1);
419 File::Find::find( {wanted => \&wanted, follow => 1, follow_skip => 2, no_chdir => 1,
7e47e6ff
JH
420 untaint => 1},':fa' );
421 Check( scalar(keys %Expect) == 0 );
422 unlink ':fa:faa_sl';
423
424 print "# check untainting (follow)\n";
425 # don't untaint at all
426 undef $@;
427 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1},':fa' );};
428 print "# Died: $@";
429 Check( $@ =~ m|Insecure dependency| );
430 chdir($cwd_untainted);
431
24e8cdb8 432 undef $@;
7e47e6ff
JH
433 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1, untaint => 1,
434 untaint_pattern => qr|^(NO_MATCH)$|},':fa' );};
435 print "# Died: $@";
436 Check( $@ =~ m|is still tainted| );
437 chdir($cwd_untainted);
438
439 print "# check untaint_skip (follow)\n";
440 undef $@;
24e8cdb8 441 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1, untaint_skip => 1,
7e47e6ff
JH
442 untaint_pattern => qr|^(NO_MATCH)$|}, ':fa' );};
443 print "# Died: $@";
444 Check( $@ =~ m|insecure cwd| );
445 chdir($cwd_untainted);
446
447 }
448
449} else {
450
451 MkDir( 'for_find',0770 );
452 CheckDie(chdir(for_find));
453
454 $cwd = cwd(); # save cwd
455 ( $cwd_untainted ) = $cwd =~ m|^(.+)$|; # untaint it
456
457 MkDir( 'fa',0770 );
458 MkDir( 'fb',0770 );
459 touch('fb/fb_ord');
460 MkDir( 'fb/fba',0770 );
461 touch('fb/fba/fba_ord');
462 CheckDie( symlink('../fb','fa/fsl') ) if $symlink_exists;
463 touch('fa/fa_ord');
464
465 MkDir( 'fa/faa',0770 );
466 touch('fa/faa/faa_ord');
467 MkDir( 'fa/fab',0770 );
468 touch('fa/fab/fab_ord');
469 MkDir( 'fa/fab/faba',0770 );
470 touch('fa/fab/faba/faba_ord');
471
472 %Expect = ('.' => 1, 'fsl' => 1, 'fa_ord' => 1, 'fab' => 1, 'fab_ord' => 1,
473 'faba' => 1, 'faa' => 1, 'faa_ord' => 1);
474 delete $Expect{'fsl'} unless $symlink_exists;
24e8cdb8 475 %Expect_Dir = ('fa' => 1, 'faa' => 1, 'fab' => 1, 'faba' => 1,
7e47e6ff
JH
476 'fb' => 1, 'fba' => 1);
477 delete @Expect_Dir{'fb','fba'} unless $symlink_exists;
478 File::Find::find( {wanted => \&wanted, untaint => 1, untaint_pattern => qr|^(.+)$|},'fa' );
479 Check( scalar(keys %Expect) == 0 );
480
9f826d6a
BM
481 print "# check re-entancy\n";
482 %Expect = ('.' => 1, 'fsl' => 1, 'fa_ord' => 1, 'fab' => 1, 'fab_ord' => 1,
483 'faba' => 1, 'faa' => 1, 'faa_ord' => 1);
484 delete $Expect{'fsl'} unless $symlink_exists;
485 %Expect_Dir = ('fa' => 1, 'faa' => 1, 'fab' => 1, 'faba' => 1,
486 'fb' => 1, 'fba' => 1);
487 delete @Expect_Dir{'fb','fba'} unless $symlink_exists;
488 File::Find::find( {wanted => sub {
489 wanted();
490 File::Find::find( {wanted => sub {} , untaint => 1, untaint_pattern => qr|^(.+)$|},'.' );
491 }, untaint => 1, untaint_pattern => qr|^(.+)$|},'fa' );
492 Check( scalar(keys %Expect) == 0 );
493
7e47e6ff
JH
494 %Expect=('fa' => 1, 'fa/fsl' => 1, 'fa/fa_ord' => 1, 'fa/fab' => 1,
495 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1,
496 'fa/fab/faba/faba_ord' => 1, 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
497 delete $Expect{'fa/fsl'} unless $symlink_exists;
24e8cdb8 498 %Expect_Dir = ('fa' => 1, 'fa/faa' => 1, '/fa/fab' => 1, 'fa/fab/faba' => 1,
7e47e6ff
JH
499 'fb' => 1, 'fb/fba' => 1);
500 delete @Expect_Dir{'fb','fb/fba'} unless $symlink_exists;
501 File::Find::find( {wanted => \&wanted, no_chdir => 1, untaint => 1, untaint_pattern => qr|^(.+)$|},'fa' );
502 Check( scalar(keys %Expect) == 0 );
503
504 %Expect=('.' => 1, './fa' => 1, './fa/fsl' => 1, './fa/fa_ord' => 1, './fa/fab' => 1,
505 './fa/fab/fab_ord' => 1, './fa/fab/faba' => 1,
506 './fa/fab/faba/faba_ord' => 1, './fa/faa' => 1, './fa/faa/faa_ord' => 1,
507 './fb' => 1, './fb/fba' => 1, './fb/fba/fba_ord' => 1, './fb/fb_ord' => 1);
508 delete $Expect{'./fa/fsl'} unless $symlink_exists;
24e8cdb8 509 %Expect_Dir = ('./fa' => 1, './fa/faa' => 1, '/fa/fab' => 1, './fa/fab/faba' => 1,
7e47e6ff
JH
510 './fb' => 1, './fb/fba' => 1);
511 delete @Expect_Dir{'./fb','./fb/fba'} unless $symlink_exists;
512 File::Find::finddepth( {wanted => \&dn_wanted , untaint => 1, untaint_pattern => qr|^(.+)$|},'.' );
513 Check( scalar(keys %Expect) == 0 );
514
515 %Expect=('.' => 1, './fa' => 1, './fa/fsl' => 1, './fa/fa_ord' => 1, './fa/fab' => 1,
516 './fa/fab/fab_ord' => 1, './fa/fab/faba' => 1,
517 './fa/fab/faba/faba_ord' => 1, './fa/faa' => 1, './fa/faa/faa_ord' => 1,
518 './fb' => 1, './fb/fba' => 1, './fb/fba/fba_ord' => 1, './fb/fb_ord' => 1);
519 delete $Expect{'./fa/fsl'} unless $symlink_exists;
24e8cdb8 520 %Expect_Dir = ('./fa' => 1, './fa/faa' => 1, '/fa/fab' => 1, './fa/fab/faba' => 1,
7e47e6ff
JH
521 './fb' => 1, './fb/fba' => 1);
522 delete @Expect_Dir{'./fb','./fb/fba'} unless $symlink_exists;
523 File::Find::finddepth( {wanted => \&d_wanted, no_chdir => 1, untaint => 1, untaint_pattern => qr|^(.+)$| },'.' );
524 Check( scalar(keys %Expect) == 0 );
525
526 # untaint, preprocess and postprocess tests below added by Thomas Wegner, 17-05-2001
527
528 print "# check untainting (no follow)\n";
529 # don't untaint at all
530 undef $@;
531 eval {File::Find::find( {wanted => \&simple_wanted},'fa' );};
532 print "# Died: $@";
533 Check( $@ =~ m|Insecure dependency| );
534 chdir($cwd_untainted);
535
536 undef $@;
537 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
24e8cdb8 538 untaint_pattern => qr|^(NO_MATCH)$|},'fa' );};
7e47e6ff
JH
539 print "# Died: $@";
540 Check( $@ =~ m|is still tainted| );
541 chdir($cwd_untainted);
542
543 print "# check untaint_skip (no follow)\n";
544 undef $@;
24e8cdb8 545 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1, untaint_skip => 1,
7e47e6ff
JH
546 untaint_pattern => qr|^(NO_MATCH)$|}, 'fa' );};
547 print "# Died: $@";
548 Check( $@ =~ m|insecure cwd| );
549 chdir($cwd_untainted);
550
551 print "# check preprocess\n";
552 %Expect=(
24e8cdb8 553 '.' => {fa => 1, fb => 1},
7e47e6ff
JH
554 './fa' => {faa => 1, fab => 1, fa_ord => 1},
555 './fa/faa' => {faa_ord => 1},
556 './fa/fab' => {faba => 1, fab_ord => 1},
24e8cdb8 557 './fa/fab/faba' => {faba_ord => 1},
7e47e6ff
JH
558 './fb' => {fba => 1, fb_ord => 1},
559 './fb/fba' => {fba_ord => 1}
560 );
561
24e8cdb8 562 File::Find::find( {wanted => \&noop_wanted, preprocess => \&my_preprocess, untaint => 1,
7e47e6ff
JH
563 untaint_pattern => qr|^(.+)$|}, '.' );
564 Check( scalar(keys %Expect) == 0 );
565
566 print "# check postprocess\n";
567 %Expect=('.' => 1, './fa' => 1, './fa/faa' => 1, './fa/fab' => 1, './fa/fab/faba' => 1, './fb' => 1,
568 './fb/fba' => 1 );
24e8cdb8 569 File::Find::find( {wanted => \&noop_wanted, postprocess => \&my_postprocess, untaint => 1,
7e47e6ff
JH
570 untaint_pattern => qr|^(.+)$|}, '.' );
571 Check( scalar(keys %Expect) == 0 );
572
573 # Verify that File::Find::find will call wanted even if the topdir of
574 # is a symlink to a directory, and it shouldn't follow the link
575 # unless follow is set, which it isn't in this case
576 %Expect = ('fsl' => 1);
577 %Expect_Dir = ();
578 File::Find::find( {wanted => \&wanted, untaint => 1},'fa/fsl' );
579 Check( scalar(keys %Expect) == 0 );
580
581 if ( $symlink_exists ) {
582 $FastFileTests_OK= 1;
583 %Expect=('.' => 1, 'fa_ord' => 1, 'fsl' => 1, 'fb_ord' => 1, 'fba' => 1,
584 'fba_ord' => 1, 'fab' => 1, 'fab_ord' => 1, 'faba' => 1, 'faa' => 1,
585 'faa_ord' => 1);
24e8cdb8 586 %Expect_Dir = ('fa' => 1, 'fa/faa' => 1, '/fa/fab' => 1, 'fa/fab/faba' => 1,
7e47e6ff
JH
587 'fb' => 1, 'fb/fba' => 1);
588 File::Find::find( {wanted => \&wanted, follow_fast => 1, untaint => 1, untaint_pattern => qr|^(.+)$|},'fa' );
589 Check( scalar(keys %Expect) == 0 );
590
591 %Expect=('fa' => 1, 'fa/fa_ord' => 1, 'fa/fsl' => 1, 'fa/fsl/fb_ord' => 1,
592 'fa/fsl/fba' => 1, 'fa/fsl/fba/fba_ord' => 1, 'fa/fab' => 1,
593 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1, 'fa/fab/faba/faba_ord' => 1,
594 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
24e8cdb8 595 %Expect_Dir = ('fa' => 1, 'fa/faa' => 1, '/fa/fab' => 1, 'fa/fab/faba' => 1,
7e47e6ff 596 'fb' => 1, 'fb/fba' => 1);
24e8cdb8 597 File::Find::find( {wanted => \&wanted, follow_fast => 1, no_chdir => 1, untaint => 1,
7e47e6ff
JH
598 untaint_pattern => qr|^(.+)$|},'fa' );
599 Check( scalar(keys %Expect) == 0 );
600
601 %Expect=('fa' => 1, 'fa/fa_ord' => 1, 'fa/fsl' => 1, 'fa/fsl/fb_ord' => 1,
602 'fa/fsl/fba' => 1, 'fa/fsl/fba/fba_ord' => 1, 'fa/fab' => 1,
603 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1, 'fa/fab/faba/faba_ord' => 1,
604 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
24e8cdb8
NIS
605 %Expect_Dir = ('fa' => 1, 'fa/faa' => 1, '/fa/fab' => 1, 'fa/fab/faba' => 1,
606 'fb' => 1, 'fb/fba' => 1);
607 File::Find::finddepth( {wanted => \&dn_wanted, follow_fast => 1, untaint => 1,
7e47e6ff
JH
608 untaint_pattern => qr|^(.+)$|},'fa' );
609 Check( scalar(keys %Expect) == 0 );
610
611 %Expect=('fa' => 1, 'fa/fa_ord' => 1, 'fa/fsl' => 1, 'fa/fsl/fb_ord' => 1,
612 'fa/fsl/fba' => 1, 'fa/fsl/fba/fba_ord' => 1, 'fa/fab' => 1,
613 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1, 'fa/fab/faba/faba_ord' => 1,
614 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
24e8cdb8 615 %Expect_Dir = ('fa' => 1, 'fa/faa' => 1, '/fa/fab' => 1, 'fa/fab/faba' => 1,
7e47e6ff 616 'fb' => 1, 'fb/fba' => 1);
24e8cdb8 617 File::Find::finddepth( {wanted => \&d_wanted, follow_fast => 1, no_chdir => 1,
7e47e6ff
JH
618 untaint => 1, untaint_pattern => qr|^(.+)$|},'fa' );
619 Check( scalar(keys %Expect) == 0 );
620
621 # tests below added by Thomas Wegner, 17-05-2001
622
623 print "# check dangling symbolic links\n";
624 MkDir( 'dangling_dir',0770 );
625 CheckDie( symlink('dangling_dir','dangling_dir_sl') );
626 rmdir 'dangling_dir';
627 touch('dangling_file');
628 CheckDie( symlink('../dangling_file','fa/dangling_file_sl') );
629 unlink 'dangling_file';
630
631 %Expect=('.' => 1, 'fa_ord' => 1, 'fsl' => 1, 'fb_ord' => 1, 'fba' => 1,
632 'fba_ord' => 1, 'fab' => 1, 'fab_ord' => 1, 'faba' => 1, 'faba_ord' => 1,
633 'faa' => 1, 'faa_ord' => 1);
24e8cdb8 634 %Expect_Dir = ('fa' => 1, 'fa/faa' => 1, 'fa/fab' => 1, 'fa/fab/faba' => 1,
7e47e6ff
JH
635 'fb' => 1, 'fb/fba' => 1);
636 undef $warn_msg;
24e8cdb8 637 File::Find::find( {wanted => \&d_wanted, follow => 1, untaint => 1,
7e47e6ff 638 untaint_pattern => qr|^(.+)$|}, 'dangling_dir_sl', 'fa' );
24e8cdb8 639 Check( $warn_msg =~ m|dangling_dir_sl is a dangling symbolic link| );
7e47e6ff
JH
640 unlink 'fa/dangling_file_sl', 'dangling_dir_sl';
641
642 print "# check recursion\n";
643 CheckDie( symlink('../faa','fa/faa/faa_sl') );
644 undef $@;
24e8cdb8 645 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1, no_chdir => 1,
7e47e6ff
JH
646 untaint => 1, untaint_pattern => qr|^(.+)$|},'fa' ); };
647 print "# Died: $@";
24e8cdb8
NIS
648 Check( $@ =~ m|for_find/fa/faa/faa_sl is a recursive symbolic link| );
649 unlink 'fa/faa/faa_sl';
650
7e47e6ff
JH
651 print "# check follow_skip (file)\n";
652 CheckDie( symlink('./fa_ord','fa/fa_ord_sl') ); # symlink to a file
653 undef $@;
24e8cdb8 654 eval {File::Find::finddepth( {wanted => \&simple_wanted, follow => 1, follow_skip => 0, no_chdir => 1,
7e47e6ff
JH
655 untaint => 1, untaint_pattern => qr|^(.+)$|},'fa' );};
656 print "# Died: $@";
657 Check( $@ =~ m|for_find/fa/fa_ord encountered a second time| );
24e8cdb8 658
7e47e6ff
JH
659 %Expect=('fa' => 1, 'fa/fa_ord' => 1, 'fa/fsl' => 1, 'fa/fsl/fb_ord' => 1,
660 'fa/fsl/fba' => 1, 'fa/fsl/fba/fba_ord' => 1, 'fa/fab' => 1,
661 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1, 'fa/fab/faba/faba_ord' => 1,
662 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
24e8cdb8
NIS
663 %Expect_Dir = ('fa' => 1, 'fa/faa' => 1, '/fa/fab' => 1, 'fa/fab/faba' => 1,
664 'fb' => 1, 'fb/fba' => 1);
665 File::Find::finddepth( {wanted => \&wanted, follow => 1, follow_skip => 1, no_chdir => 1,
7e47e6ff
JH
666 untaint => 1, untaint_pattern => qr|^(.+)$|},'fa' );
667 Check( scalar(keys %Expect) == 0 );
668 unlink 'fa/fa_ord_sl';
669
670 print "# check follow_skip (directory)\n";
671 CheckDie( symlink('./faa','fa/faa_sl') ); # symlink to a directory
672 undef $@;
673 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1, follow_skip => 0, no_chdir => 1,
674 untaint => 1, untaint_pattern => qr|^(.+)$|},'fa' );};
675 print "# Died: $@";
676 Check( $@ =~ m|for_find/fa/faa encountered a second time| );
677
678 undef $@;
679 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1, follow_skip => 1, no_chdir => 1,
680 untaint => 1, untaint_pattern => qr|^(.+)$|},'fa' );};
681 print "# Died: $@";
682 Check( $@ =~ m|for_find/fa/faa encountered a second time| );
683
684 %Expect=('fa' => 1, 'fa/fa_ord' => 1, 'fa/fsl' => 1, 'fa/fsl/fb_ord' => 1,
685 'fa/fsl/fba' => 1, 'fa/fsl/fba/fba_ord' => 1, 'fa/fab' => 1,
686 'fa/fab/fab_ord' => 1, 'fa/fab/faba' => 1, 'fa/fab/faba/faba_ord' => 1,
687 'fa/faa' => 1, 'fa/faa/faa_ord' => 1);
24e8cdb8
NIS
688 %Expect_Dir = ('fa' => 1, 'fa/faa' => 1, '/fa/fab' => 1, 'fa/fab/faba' => 1,
689 'fb' => 1, 'fb/fba' => 1);
7e47e6ff
JH
690 File::Find::find( {wanted => \&wanted, follow => 1, follow_skip => 2, no_chdir => 1,
691 untaint => 1, untaint_pattern => qr|^(.+)$|},'fa' );
692 Check( scalar(keys %Expect) == 0 );
693 unlink 'fa/faa_sl';
694
695 print "# check untainting (follow)\n";
696 # don't untaint at all
697 undef $@;
698 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1},'fa' );};
699 print "# Died: $@";
700 Check( $@ =~ m|Insecure dependency| );
701 chdir($cwd_untainted);
702
24e8cdb8 703 undef $@;
7e47e6ff
JH
704 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1, untaint => 1,
705 untaint_pattern => qr|^(NO_MATCH)$|},'fa' );};
706 print "# Died: $@";
707 Check( $@ =~ m|is still tainted| );
708 chdir($cwd_untainted);
709
710 print "# check untaint_skip (follow)\n";
711 undef $@;
24e8cdb8 712 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1, untaint_skip => 1,
7e47e6ff
JH
713 untaint_pattern => qr|^(NO_MATCH)$|}, 'fa' );};
714 print "# Died: $@";
715 Check( $@ =~ m|insecure cwd| );
716 chdir($cwd_untainted);
717
718 }
81793b90
GS
719}
720
721print "# of cases: $case\n";