This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
bump Benchmark version
[perl5.git] / t / op / filetest.t
CommitLineData
42e55ab1
JH
1#!./perl
2
3# There are few filetest operators that are portable enough to test.
4# See pod/perlport.pod for details.
5
6BEGIN {
7 chdir 't' if -d 't';
20822f61 8 @INC = '../lib';
fbb0b3b3 9 require './test.pl';
42e55ab1
JH
10}
11
5840701a 12plan(tests => 53 + 27*14);
42e55ab1 13
8c5e482d
JK
14# Tests presume we are in t/op directory and that file 'TEST' is found
15# therein.
16is(-d 'op', 1, "-d: directory correctly identified");
17is(-f 'TEST', 1, "-f: plain file correctly identified");
18isnt(-f 'op', 1, "-f: directory is not a plain file");
19isnt(-d 'TEST', 1, "-d: plain file is not a directory");
20is(-r 'TEST', 1, "-r: file readable by effective uid/gid not found");
42e55ab1 21
e687eccd
NC
22# Make a read only file. This happens to be empty, so we also use it later.
23my $ro_empty_file = tempfile();
d4188802
NC
24
25{
e687eccd 26 open my $fh, '>', $ro_empty_file or die "open $fh: $!";
d4188802
NC
27 close $fh or die "close $fh: $!";
28}
29
e687eccd 30chmod 0555, $ro_empty_file or die "chmod 0555, '$ro_empty_file' failed: $!";
846f25a3 31
fbb0b3b3 32SKIP: {
dcefe67b
NC
33 my $restore_root;
34 if ($> == 0) {
35 # root can read and write anything, so switch uid (may not be
36 # implemented)
37 eval '$> = 1';
38
39 skip("Can't drop root privs to test read-only files") if $> == 0;
40 note("Dropped root privs to test read-only files. \$> == $>");
41 ++$restore_root;
fbb0b3b3 42 }
42e55ab1 43
8c5e482d 44 isnt(-w $ro_empty_file, 1, "-w: file writable by effective uid/gid");
42e55ab1 45
dcefe67b
NC
46 if ($restore_root) {
47 # If the previous assignment to $> worked, so should this:
48 $> = 0;
49 note("Restored root privs after testing read-only files. \$> == $>");
50 }
51}
fd1e013e 52
64f0b68d 53# these would fail for the euid 1
fd1e013e 54# (unless we have unpacked the source code as uid 1...)
8c5e482d
JK
55is(-r 'op', 1, "-r: directory readable by effective uid/gid");
56is(-w 'op', 1, "-w: directory writable by effective uid/gid");
57is(-x 'op', 1, "-x: executable by effective uid/gid"); # Hohum. Are directories -x everywhere?
fbb0b3b3 58
8c5e482d
JK
59is( "@{[grep -r, qw(foo io noo op zoo)]}", "io op",
60 "-r: found directories readable by effective uid/gid" );
fbb0b3b3
RGS
61
62# Test stackability of filetest operators
42e55ab1 63
8c5e482d
JK
64is(defined( -f -d 'TEST' ), 1, "-f and -d stackable: plain file found");
65isnt(-f -d _, 1, "-f and -d stackable: no plain file found");
66isnt(defined( -e 'zoo' ), 1, "-e: file does not exist");
67isnt(defined( -e -d 'zoo' ), 1, "-e and -d: neither file nor directory exists");
68isnt(defined( -f -e 'zoo' ), 1, "-f and -e: not a plain file and does not exist");
69is(-f -e 'TEST', 1, "-f and -e: plain file and exists");
70is(-e -f 'TEST', 1, "-e and -f: exists and is plain file");
71is(defined(-d -e 'TEST'), 1, "-d and -e: file at least exists");
72is(defined(-e -d 'TEST'), 1, "-e and -d: file at least exists");
73isnt( -f -d 'op', 1, "-f and -d: directory found but is not a plain file");
74is(-x -d -x 'op', 1, "-x, -d and -x again: directory exists and is executable");
fea55e48
NC
75my ($size) = (stat 'TEST')[7];
76cmp_ok($size, '>', 1, 'TEST is longer than 1 byte');
77is( (-s -f 'TEST'), $size, "-s returns real size" );
8c5e482d 78is(-f -s 'TEST', 1, "-f and -s: plain file with non-zero size");
7294df96 79
e7d3eb55 80# now with an empty file
8c5e482d
JK
81is(-f $ro_empty_file, 1, "-f: plain file found");
82is(-s $ro_empty_file, 0, "-s: file has 0 bytes");
83is(-f -s $ro_empty_file, 0, "-f and -s: plain file with 0 bytes");
84is(-s -f $ro_empty_file, 0, "-s and -f: file with 0 bytes is plain file");
e7d3eb55 85
1f26655e
FC
86# stacked -l
87eval { -l -e "TEST" };
88like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
89 'stacked -l non-lstat error with warnings off';
90{
91 local $^W = 1;
92 eval { -l -e "TEST" };
93 like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
94 'stacked -l non-lstat error with warnings on';
95}
96# Make sure -l is using the previous stat buffer, and not using the previ-
97# ous op’s return value as a file name.
8f67f7cf 98# t/TEST can be a symlink under -Dmksymlinks, so use our temporary file.
1f26655e
FC
99SKIP: {
100 use Perl::OSType 'os_type';
cd22fad3 101 if (os_type ne 'Unix') { skip "Not Unix", 3 }
19d2bdc1
BF
102 if ( $^O =~ /android/ ) {
103 # Even the most basic toolbox in android provides ln,
104 # but not which.
105 $ln = "ln";
106 }
107 else {
108 chomp(my $ln = `which ln`);
109 if ( ! -e $ln ) { skip "No ln" , 3 }
110 }
8f67f7cf
NC
111 lstat $ro_empty_file;
112 `ln -s $ro_empty_file 1`;
bce3b594 113 isnt(-l -e _, 1, 'stacked -l uses previous stat, not previous retval');
1f26655e 114 unlink 1;
433644ee
FC
115
116 # Since we already have our skip block set up, we might as well put this
117 # test here, too:
118 # -l always treats a non-bareword argument as a file name
8f67f7cf 119 system 'ln', '-s', $ro_empty_file, \*foo;
433644ee 120 local $^W = 1;
cd22fad3
RS
121 my @warnings;
122 local $SIG{__WARN__} = sub { push @warnings, @_ };
bce3b594 123 is(-l \*foo, 1, '-l \*foo is a file name');
cd22fad3 124 ok($warnings[0] =~ /-l on filehandle foo/, 'warning for -l $handle');
433644ee 125 unlink \*foo;
1f26655e 126}
5840701a
FC
127# More -l $handle warning tests
128{
129 local $^W = 1;
130 my @warnings;
131 local $SIG{__WARN__} = sub { push @warnings, @_ };
132 () = -l \*{"\x{3c6}oo"};
133 like($warnings[0], qr/-l on filehandle \x{3c6}oo/,
134 '-l $handle warning is utf8-clean');
135 () = -l *foo;
136 like($warnings[1], qr/-l on filehandle foo/,
137 '-l $handle warning occurs for globs, not just globrefs');
138 tell foo; # vivify the IO slot
139 () = -l *foo{IO};
140 # (element [3] because tell also warns)
141 like($warnings[3], qr/-l on filehandle at/,
142 '-l $handle warning occurs for iorefs as well');
143}
1f26655e 144
7294df96
RGS
145# test that _ is a bareword after filetest operators
146
147-f 'TEST';
8c5e482d 148is(-f _, 1, "_ is bareword after filetest operator");
7294df96 149sub _ { "this is not a file name" }
8c5e482d 150is(-f _, 1, "_ is bareword after filetest operator");
d89f1457
BM
151
152my $over;
153{
154 package OverFtest;
155
4d57d24f 156 use overload
c1d4704a 157 fallback => 1,
4d57d24f 158 -X => sub {
c1d4704a 159 $over = [qq($_[0]), $_[1]];
4d57d24f
BM
160 "-$_[1]";
161 };
d89f1457 162}
f6aa8023
BM
163{
164 package OverString;
165
166 # No fallback. -X should fall back to string overload even without
167 # it.
168 use overload q/""/ => sub { $over = 1; "TEST" };
169}
170{
171 package OverBoth;
172
173 use overload
174 q/""/ => sub { "TEST" },
175 -X => sub { "-$_[1]" };
176}
177{
178 package OverNeither;
179
4d57d24f 180 # Need fallback. Previous versions of perl required 'fallback' to do
f6aa8023
BM
181 # -X operations on an object with no "" overload.
182 use overload
183 '+' => sub { 1 },
184 fallback => 1;
185}
186
187my $ft = bless [], "OverFtest";
c1d4704a 188my $ftstr = qq($ft);
f6aa8023
BM
189my $str = bless [], "OverString";
190my $both = bless [], "OverBoth";
191my $neither = bless [], "OverNeither";
c1d4704a 192my $nstr = qq($neither);
d89f1457 193
d0c91b6a
BM
194open my $gv, "<", "TEST";
195bless $gv, "OverString";
196open my $io, "<", "TEST";
197$io = *{$io}{IO};
198bless $io, "OverString";
199
822146ea
RGS
200my $fcntl_not_available;
201eval { require Fcntl } or $fcntl_not_available = 1;
fa7f7498 202
d89f1457 203for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
d3ebc3eb 204 $over = [];
bce3b594
NC
205 my $rv = eval "-$op \$ft";
206 isnt( $rv, undef, "overloaded -$op succeeds" )
f6aa8023
BM
207 or diag( $@ );
208 is( $over->[0], $ftstr, "correct object for overloaded -$op" );
d89f1457
BM
209 is( $over->[1], $op, "correct op for overloaded -$op" );
210 is( $rv, "-$op", "correct return value for overloaded -$op");
f6aa8023 211
fa7f7498 212 my ($exp, $is) = (1, "is");
213 if (
822146ea 214 !$fcntl_not_available and (
fa7f7498 215 $op eq "u" and not eval { Fcntl::S_ISUID() } or
216 $op eq "g" and not eval { Fcntl::S_ISGID() } or
217 $op eq "k" and not eval { Fcntl::S_ISVTX() }
822146ea 218 )
fa7f7498 219 ) {
220 ($exp, $is) = (0, "not");
221 }
222
f6aa8023
BM
223 $over = 0;
224 $rv = eval "-$op \$str";
bce3b594 225 is($@, "", "-$op succeeds with string overloading");
f6aa8023 226 is( $rv, eval "-$op 'TEST'", "correct -$op on string overload" );
fa7f7498 227 is( $over, $exp, "string overload $is called for -$op" );
f6aa8023 228
fa7f7498 229 ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
d0c91b6a
BM
230
231 $over = 0;
232 eval "-$op \$gv";
233 is( $over, $exp, "string overload $is called for -$op on GLOB" );
234
52f7f5ab
BM
235 # IO refs always get string overload called. This might be a bug.
236 $op eq "t" || $op eq "T" || $op eq "B"
237 and ($exp, $is) = (1, "is");
238
d0c91b6a
BM
239 $over = 0;
240 eval "-$op \$io";
241 is( $over, $exp, "string overload $is called for -$op on IO");
242
f6aa8023
BM
243 $rv = eval "-$op \$both";
244 is( $rv, "-$op", "correct -$op on string/-X overload" );
245
246 $rv = eval "-$op \$neither";
bce3b594 247 is($@, "", "-$op succeeds with random overloading");
f6aa8023 248 is( $rv, eval "-$op \$nstr", "correct -$op with random overloading" );
f6aa8023 249
4d57d24f
BM
250 is( eval "-r -$op \$ft", "-r", "stacked overloaded -$op" );
251 is( eval "-$op -r \$ft", "-$op", "overloaded stacked -$op" );
252}
ca867162
FC
253
254# -l stack corruption: this bug occurred from 5.8 to 5.14
255{
256 push my @foo, "bar", -l baz;
257 is $foo[0], "bar", '-l bareword does not corrupt the stack';
258}
49498caf 259
31b139ba
FC
260# -l and fatal warnings
261stat "test.pl";
262eval { use warnings FATAL => io; -l cradd };
bce3b594
NC
263isnt(stat _, 1,
264 'fatal warnings do not prevent -l HANDLE from setting stat status');
31b139ba 265
49498caf
FC
266# File test ops should not call get-magic on the topmost SV on the stack if
267# it belongs to another op.
268{
269 my $w;
270 sub oon::TIESCALAR{bless[],'oon'}
271 sub oon::FETCH{$w++}
272 tie my $t, 'oon';
273 push my @a, $t, -t;
274 is $w, 1, 'file test does not call FETCH on stack item not its own';
275}
5731662b 276
97c226b8
FC
277# -T and -B
278
bd5f6c01
FC
279my $Perl = which_perl();
280
5731662b 281SKIP: {
2ad48547 282 skip "no -T on filehandles", 8 unless eval { -T STDERR; 1 };
97c226b8
FC
283
284 # Test that -T HANDLE sets the last stat type
5731662b
FC
285 -l "perl.c"; # last stat type is now lstat
286 -T STDERR; # should set it to stat, since -T does a stat
287 eval { -l _ }; # should die, because the last stat type is not lstat
288 like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
289 '-T HANDLE sets the stat type';
97c226b8
FC
290
291 # statgv should be cleared when freed
292 fresh_perl_is
293 'open my $fh, "test.pl"; -r $fh; undef $fh; open my $fh2, '
bd5f6c01 294 . "q\0$Perl\0; print -B _",
97c226b8
FC
295 '',
296 { switches => ['-l'] },
297 'PL_statgv should not point to freed-and-reused SV';
298
299 # or coerced into a non-glob
300 fresh_perl_is
301 'open Fh, "test.pl"; -r($h{i} = *Fh); $h{i} = 3; undef %h;'
302 . 'open my $fh2, ' . "q\0" . which_perl() . "\0; print -B _",
303 '',
304 { switches => ['-l'] },
305 'PL_statgv should not point to coerced-freed-and-reused GV';
bd5f6c01
FC
306
307 # -T _ should work after stat $ioref
308 open my $fh, 'test.pl';
309 stat $Perl; # a binary file
310 stat *$fh{IO};
bce3b594 311 is(-T _, 1, '-T _ works after stat $ioref');
bd5f6c01
FC
312
313 # and after -r $ioref
314 -r *$fh{IO};
bce3b594 315 is(-T _, 1, '-T _ works after -r $ioref');
eb4c377a
FC
316
317 # -T _ on closed filehandle should still reset stat info
318 stat $fh;
319 close $fh;
320 -T _;
bce3b594 321 isnt(stat _, 1, '-T _ on closed filehandle resets stat info');
21a64c3e
FC
322
323 lstat "test.pl";
324 -T $fh; # closed
325 eval { lstat _ };
326 like $@, qr/^The stat preceding lstat\(\) wasn't an lstat at /,
327 '-T on closed handle resets last stat type';
2ad48547
FC
328
329 # Fatal warnings should not affect the setting of errno.
330 $! = 7;
331 -T cradd;
332 my $errno = $!;
333 $! = 7;
334 eval { use warnings FATAL => unopened; -T cradd };
335 my $errno2 = $!;
336 is $errno2, $errno,
337 'fatal warnings do not affect errno after -T BADHADNLE';
5731662b 338}
de61bf2a
FC
339
340is runperl(prog => '-T _', switches => ['-w'], stderr => 1), "",
341 'no uninit warnings from -T with no preceding stat';
7b7309af 342
ad2d99e3 343SKIP: {
bee528ec 344 my $rand_file_name = 'filetest-' . rand =~ y/.//dr;
ad2d99e3
FC
345 if (-e $rand_file_name) { skip "File $rand_file_name exists", 1 }
346 stat 'test.pl';
347 -T $rand_file_name;
bce3b594 348 isnt(stat _, 1, '-T "nonexistent" resets stat success status');
ad2d99e3
FC
349}
350
7b7309af
FC
351# Unsuccessful filetests on filehandles should leave stat buffers in the
352# same state whether fatal warnings are on or off.
353{
354 stat "test.pl";
355 # This GV has no IO
356 -r *phlon;
357 my $failed_stat1 = stat _;
358
359 stat "test.pl";
360 eval { use warnings FATAL => unopened; -r *phlon };
361 my $failed_stat2 = stat _;
362
363 is $failed_stat2, $failed_stat1,
364 'failed -r($gv_without_io) with and w/out fatal warnings';
365
366 stat "test.pl";
367 -r cength; # at compile time autovivifies IO, but with no fp
368 $failed_stat1 = stat _;
369
370 stat "test.pl";
371 eval { use warnings FATAL => unopened; -r cength };
372 $failed_stat2 = stat _;
373
374 is $failed_stat2, $failed_stat1,
375 'failed -r($gv_with_io_but_no_fp) with and w/out fatal warnings';
376}