3 # There are few filetest operators that are portable enough to test.
4 # See pod/perlport.pod for details.
12 plan(tests => 49 + 27*14);
20 # Make a read only file. This happens to be empty, so we also use it later.
21 my $ro_empty_file = tempfile();
24 open my $fh, '>', $ro_empty_file or die "open $fh: $!";
25 close $fh or die "close $fh: $!";
28 chmod 0555, $ro_empty_file or die "chmod 0555, '$ro_empty_file' failed: $!";
33 # root can read and write anything, so switch uid (may not be
37 skip("Can't drop root privs to test read-only files") if $> == 0;
38 note("Dropped root privs to test read-only files. \$> == $>");
42 isnt(-w $ro_empty_file, 1);
45 # If the previous assignment to $> worked, so should this:
47 note("Restored root privs after testing read-only files. \$> == $>");
51 # these would fail for the euid 1
52 # (unless we have unpacked the source code as uid 1...)
55 is(-x 'op', 1); # Hohum. Are directories -x everywhere?
57 is( "@{[grep -r, qw(foo io noo op zoo)]}", "io op" );
59 # Test stackability of filetest operators
61 is(defined( -f -d 'TEST' ), 1);
63 isnt(defined( -e 'zoo' ), 1);
64 isnt(defined( -e -d 'zoo' ), 1);
65 isnt(defined( -f -e 'zoo' ), 1);
68 is(defined(-d -e 'TEST'), 1);
69 is(defined(-e -d 'TEST'), 1);
72 my ($size) = (stat 'TEST')[7];
73 cmp_ok($size, '>', 1, 'TEST is longer than 1 byte');
74 is( (-s -f 'TEST'), $size, "-s returns real size" );
77 # now with an empty file
78 is(-f $ro_empty_file, 1);
79 is( -s $ro_empty_file, 0 );
80 is( -f -s $ro_empty_file, 0 );
81 is( -s -f $ro_empty_file, 0 );
84 eval { -l -e "TEST" };
85 like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
86 'stacked -l non-lstat error with warnings off';
89 eval { -l -e "TEST" };
90 like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
91 'stacked -l non-lstat error with warnings on';
93 # Make sure -l is using the previous stat buffer, and not using the previ-
94 # ous op’s return value as a file name.
95 # t/TEST can be a symlink under -Dmksymlinks, so use our temporary file.
97 use Perl::OSType 'os_type';
98 if (os_type ne 'Unix') { skip "Not Unix", 2 }
99 chomp(my $ln = `which ln`);
100 if ( ! -e $ln ) { skip "No ln" , 2 }
101 lstat $ro_empty_file;
102 `ln -s $ro_empty_file 1`;
103 isnt(-l -e _, 1, 'stacked -l uses previous stat, not previous retval');
106 # Since we already have our skip block set up, we might as well put this
108 # -l always treats a non-bareword argument as a file name
109 system 'ln', '-s', $ro_empty_file, \*foo;
111 is(-l \*foo, 1, '-l \*foo is a file name');
115 # test that _ is a bareword after filetest operators
119 sub _ { "this is not a file name" }
129 $over = [qq($_[0]), $_[1]];
136 # No fallback. -X should fall back to string overload even without
138 use overload q/""/ => sub { $over = 1; "TEST" };
144 q/""/ => sub { "TEST" },
145 -X => sub { "-$_[1]" };
150 # Need fallback. Previous versions of perl required 'fallback' to do
151 # -X operations on an object with no "" overload.
157 my $ft = bless [], "OverFtest";
159 my $str = bless [], "OverString";
160 my $both = bless [], "OverBoth";
161 my $neither = bless [], "OverNeither";
162 my $nstr = qq($neither);
164 open my $gv, "<", "TEST";
165 bless $gv, "OverString";
166 open my $io, "<", "TEST";
168 bless $io, "OverString";
170 my $fcntl_not_available;
171 eval { require Fcntl } or $fcntl_not_available = 1;
173 for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
175 my $rv = eval "-$op \$ft";
176 isnt( $rv, undef, "overloaded -$op succeeds" )
178 is( $over->[0], $ftstr, "correct object for overloaded -$op" );
179 is( $over->[1], $op, "correct op for overloaded -$op" );
180 is( $rv, "-$op", "correct return value for overloaded -$op");
182 my ($exp, $is) = (1, "is");
184 !$fcntl_not_available and (
185 $op eq "u" and not eval { Fcntl::S_ISUID() } or
186 $op eq "g" and not eval { Fcntl::S_ISGID() } or
187 $op eq "k" and not eval { Fcntl::S_ISVTX() }
190 ($exp, $is) = (0, "not");
194 $rv = eval "-$op \$str";
195 is($@, "", "-$op succeeds with string overloading");
196 is( $rv, eval "-$op 'TEST'", "correct -$op on string overload" );
197 is( $over, $exp, "string overload $is called for -$op" );
199 ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
203 is( $over, $exp, "string overload $is called for -$op on GLOB" );
205 # IO refs always get string overload called. This might be a bug.
206 $op eq "t" || $op eq "T" || $op eq "B"
207 and ($exp, $is) = (1, "is");
211 is( $over, $exp, "string overload $is called for -$op on IO");
213 $rv = eval "-$op \$both";
214 is( $rv, "-$op", "correct -$op on string/-X overload" );
216 $rv = eval "-$op \$neither";
217 is($@, "", "-$op succeeds with random overloading");
218 is( $rv, eval "-$op \$nstr", "correct -$op with random overloading" );
220 is( eval "-r -$op \$ft", "-r", "stacked overloaded -$op" );
221 is( eval "-$op -r \$ft", "-$op", "overloaded stacked -$op" );
224 # -l stack corruption: this bug occurred from 5.8 to 5.14
226 push my @foo, "bar", -l baz;
227 is $foo[0], "bar", '-l bareword does not corrupt the stack';
230 # -l and fatal warnings
232 eval { use warnings FATAL => io; -l cradd };
234 'fatal warnings do not prevent -l HANDLE from setting stat status');
236 # File test ops should not call get-magic on the topmost SV on the stack if
237 # it belongs to another op.
240 sub oon::TIESCALAR{bless[],'oon'}
244 is $w, 1, 'file test does not call FETCH on stack item not its own';
249 my $Perl = which_perl();
252 skip "no -T on filehandles", 8 unless eval { -T STDERR; 1 };
254 # Test that -T HANDLE sets the last stat type
255 -l "perl.c"; # last stat type is now lstat
256 -T STDERR; # should set it to stat, since -T does a stat
257 eval { -l _ }; # should die, because the last stat type is not lstat
258 like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
259 '-T HANDLE sets the stat type';
261 # statgv should be cleared when freed
263 'open my $fh, "test.pl"; -r $fh; undef $fh; open my $fh2, '
264 . "q\0$Perl\0; print -B _",
266 { switches => ['-l'] },
267 'PL_statgv should not point to freed-and-reused SV';
269 # or coerced into a non-glob
271 'open Fh, "test.pl"; -r($h{i} = *Fh); $h{i} = 3; undef %h;'
272 . 'open my $fh2, ' . "q\0" . which_perl() . "\0; print -B _",
274 { switches => ['-l'] },
275 'PL_statgv should not point to coerced-freed-and-reused GV';
277 # -T _ should work after stat $ioref
278 open my $fh, 'test.pl';
279 stat $Perl; # a binary file
281 is(-T _, 1, '-T _ works after stat $ioref');
283 # and after -r $ioref
285 is(-T _, 1, '-T _ works after -r $ioref');
287 # -T _ on closed filehandle should still reset stat info
291 isnt(stat _, 1, '-T _ on closed filehandle resets stat info');
296 like $@, qr/^The stat preceding lstat\(\) wasn't an lstat at /,
297 '-T on closed handle resets last stat type';
299 # Fatal warnings should not affect the setting of errno.
304 eval { use warnings FATAL => unopened; -T cradd };
307 'fatal warnings do not affect errno after -T BADHADNLE';
310 is runperl(prog => '-T _', switches => ['-w'], stderr => 1), "",
311 'no uninit warnings from -T with no preceding stat';
314 my $rand_file_name = 'filetest-' . rand =~ y/.//dr;
315 if (-e $rand_file_name) { skip "File $rand_file_name exists", 1 }
318 isnt(stat _, 1, '-T "nonexistent" resets stat success status');
321 # Unsuccessful filetests on filehandles should leave stat buffers in the
322 # same state whether fatal warnings are on or off.
327 my $failed_stat1 = stat _;
330 eval { use warnings FATAL => unopened; -r *phlon };
331 my $failed_stat2 = stat _;
333 is $failed_stat2, $failed_stat1,
334 'failed -r($gv_without_io) with and w/out fatal warnings';
337 -r cength; # at compile time autovivifies IO, but with no fp
338 $failed_stat1 = stat _;
341 eval { use warnings FATAL => unopened; -r cength };
342 $failed_stat2 = stat _;
344 is $failed_stat2, $failed_stat1,
345 'failed -r($gv_with_io_but_no_fp) with and w/out fatal warnings';