This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
skip failing leak test under -Dmad
[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 plan(tests => 49 + 27*14);
13
14 is(-d 'op', 1);
15 is(-f 'TEST', 1);
16 isnt(-f 'op', 1);
17 isnt(-d 'TEST', 1);
18 is(-r 'TEST', 1);
19
20 # Make a read only file. This happens to be empty, so we also use it later.
21 my $ro_empty_file = tempfile();
22
23 {
24     open my $fh, '>', $ro_empty_file or die "open $fh: $!";
25     close $fh or die "close $fh: $!";
26 }
27
28 chmod 0555, $ro_empty_file or die "chmod 0555, '$ro_empty_file' failed: $!";
29
30 SKIP: {
31     my $restore_root;
32     if ($> == 0) {
33         # root can read and write anything, so switch uid (may not be
34         # implemented)
35         eval '$> = 1';
36
37         skip("Can't drop root privs to test read-only files") if $> == 0;
38         note("Dropped root privs to test read-only files. \$> == $>");
39         ++$restore_root;
40     }
41
42     isnt(-w $ro_empty_file, 1);
43
44     if ($restore_root) {
45         # If the previous assignment to $> worked, so should this:
46         $> = 0;
47         note("Restored root privs after testing read-only files. \$> == $>");
48     }
49 }
50
51 # these would fail for the euid 1
52 # (unless we have unpacked the source code as uid 1...)
53 is(-r 'op', 1);
54 is(-w 'op', 1);
55 is(-x 'op', 1); # Hohum.  Are directories -x everywhere?
56
57 is( "@{[grep -r, qw(foo io noo op zoo)]}", "io op" );
58
59 # Test stackability of filetest operators
60
61 is(defined( -f -d 'TEST' ), 1);
62 isnt(-f -d _, 1);
63 isnt(defined( -e 'zoo' ), 1);
64 isnt(defined( -e -d 'zoo' ), 1);
65 isnt(defined( -f -e 'zoo' ), 1);
66 is(-f -e 'TEST', 1);
67 is(-e -f 'TEST', 1);
68 is(defined(-d -e 'TEST'), 1);
69 is(defined(-e -d 'TEST'), 1);
70 isnt( -f -d 'op', 1);
71 is(-x -d -x 'op', 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" );
75 is(-f -s 'TEST', 1);
76
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 );
82
83 # stacked -l
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';
87 {
88  local $^W = 1;
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';
92 }
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.
96 SKIP: {
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');
104  unlink 1;
105
106  # Since we already have our skip block set up, we might as well put this
107  # test here, too:
108  # -l always treats a non-bareword argument as a file name
109  system 'ln', '-s', $ro_empty_file, \*foo;
110  local $^W = 1;
111  is(-l \*foo, 1, '-l \*foo is a file name');
112  unlink \*foo;
113 }
114
115 # test that _ is a bareword after filetest operators
116
117 -f 'TEST';
118 is(-f _, 1);
119 sub _ { "this is not a file name" }
120 is(-f _, 1);
121
122 my $over;
123 {
124     package OverFtest;
125
126     use overload 
127         fallback => 1,
128         -X => sub { 
129             $over = [qq($_[0]), $_[1]];
130             "-$_[1]"; 
131         };
132 }
133 {
134     package OverString;
135
136     # No fallback. -X should fall back to string overload even without
137     # it.
138     use overload q/""/ => sub { $over = 1; "TEST" };
139 }
140 {
141     package OverBoth;
142
143     use overload
144         q/""/   => sub { "TEST" },
145         -X      => sub { "-$_[1]" };
146 }
147 {
148     package OverNeither;
149
150     # Need fallback. Previous versions of perl required 'fallback' to do
151     # -X operations on an object with no "" overload.
152     use overload 
153         '+' => sub { 1 },
154         fallback => 1;
155 }
156
157 my $ft = bless [], "OverFtest";
158 my $ftstr = qq($ft);
159 my $str = bless [], "OverString";
160 my $both = bless [], "OverBoth";
161 my $neither = bless [], "OverNeither";
162 my $nstr = qq($neither);
163
164 open my $gv, "<", "TEST";
165 bless $gv, "OverString";
166 open my $io, "<", "TEST";
167 $io = *{$io}{IO};
168 bless $io, "OverString";
169
170 my $fcntl_not_available;
171 eval { require Fcntl } or $fcntl_not_available = 1;
172
173 for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
174     $over = [];
175     my $rv = eval "-$op \$ft";
176     isnt( $rv, undef,               "overloaded -$op succeeds" )
177         or diag( $@ );
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");
181
182     my ($exp, $is) = (1, "is");
183     if (
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() }
188         )
189     ) {
190         ($exp, $is) = (0, "not");
191     }
192
193     $over = 0;
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" );
198
199     ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
200
201     $over = 0;
202     eval "-$op \$gv";
203     is( $over,      $exp,   "string overload $is called for -$op on GLOB" );
204
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");
208
209     $over = 0;
210     eval "-$op \$io";
211     is( $over,      $exp,   "string overload $is called for -$op on IO");
212
213     $rv = eval "-$op \$both";
214     is( $rv,        "-$op",         "correct -$op on string/-X overload" );
215
216     $rv = eval "-$op \$neither";
217     is($@, "",                      "-$op succeeds with random overloading");
218     is( $rv, eval "-$op \$nstr",    "correct -$op with random overloading" );
219
220     is( eval "-r -$op \$ft", "-r",      "stacked overloaded -$op" );
221     is( eval "-$op -r \$ft", "-$op",    "overloaded stacked -$op" );
222 }
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 }
229
230 # -l and fatal warnings
231 stat "test.pl";
232 eval { use warnings FATAL => io; -l cradd };
233 isnt(stat _, 1,
234      'fatal warnings do not prevent -l HANDLE from setting stat status');
235
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 }
246
247 # -T and -B
248
249 my $Perl = which_perl();
250
251 SKIP: {
252     skip "no -T on filehandles", 8 unless eval { -T STDERR; 1 };
253
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';
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, '
264         . "q\0$Perl\0; print -B _",
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';
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     is(-T _, 1, '-T _ works after stat $ioref');
282
283     # and after -r $ioref
284     -r *$fh{IO};
285     is(-T _, 1, '-T _ works after -r $ioref');
286
287     # -T _ on closed filehandle should still reset stat info
288     stat $fh;
289     close $fh;
290     -T _;
291     isnt(stat _, 1, '-T _ on closed filehandle resets stat info');
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';
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';
308 }
309
310 is runperl(prog => '-T _', switches => ['-w'], stderr => 1), "",
311   'no uninit warnings from -T with no preceding stat';
312
313 SKIP: {
314     my $rand_file_name = 'filetest-' . rand =~ y/.//dr;
315     if (-e $rand_file_name) { skip "File $rand_file_name exists", 1 }
316     stat 'test.pl';
317     -T $rand_file_name;
318     isnt(stat _, 1, '-T "nonexistent" resets stat success status');
319 }
320
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