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