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