3 # There are few filetest operators that are portable enough to test.
4 # See pod/perlport.pod for details.
13 plan(tests => 47 + 27*14);
21 # Make a read only file
22 my $ro_file = tempfile();
25 open my $fh, '>', $ro_file or die "open $fh: $!";
26 close $fh or die "close $fh: $!";
29 chmod 0555, $ro_file or die "chmod 0555, '$ro_file' failed: $!";
31 $oldeuid = $>; # root can read and write anything
32 eval '$> = 1'; # so switch uid (may not be implemented)
34 print "# oldeuid = $oldeuid, euid = $>\n";
37 if (!$Config{d_seteuid}) {
45 # Scripts are not -x everywhere so cannot test that.
47 eval '$> = $oldeuid'; # switch uid back (may not be implemented)
49 # this would fail for the euid 1
50 # (unless we have unpacked the source code as uid 1...)
53 # this would fail for the euid 1
54 # (unless we have unpacked the source code as uid 1...)
56 if ($Config{d_seteuid}) {
63 ok( -x 'op' ); # Hohum. Are directories -x everywhere?
65 is( "@{[grep -r, qw(foo io noo op zoo)]}", "io op" );
67 # Test stackability of filetest operators
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' ) );
75 ok( defined(-d -e 'TEST') );
76 ok( defined(-e -d 'TEST') );
79 ok( (-s -f 'TEST' > 1), "-s returns real size" );
80 ok( -f -s 'TEST' == 1 );
82 # now with an empty file
83 my $tempfile = tempfile();
84 open my $fh, ">", $tempfile;
87 is( -s $tempfile, 0 );
88 is( -f -s $tempfile, 0 );
89 is( -s -f $tempfile, 0 );
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';
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';
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.
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 }
112 ok ! -l -e _, 'stacked -l uses previous stat, not previous retval';
115 # Since we already have our skip block set up, we might as well put this
117 # -l always treats a non-bareword argument as a file name
118 system qw "ln -s TEST", \*foo;
120 ok -l \*foo, '-l \*foo is a file name';
124 # test that _ is a bareword after filetest operators
128 sub _ { "this is not a file name" }
138 $over = [qq($_[0]), $_[1]];
145 # No fallback. -X should fall back to string overload even without
147 use overload q/""/ => sub { $over = 1; "TEST" };
153 q/""/ => sub { "TEST" },
154 -X => sub { "-$_[1]" };
159 # Need fallback. Previous versions of perl required 'fallback' to do
160 # -X operations on an object with no "" overload.
166 my $ft = bless [], "OverFtest";
168 my $str = bless [], "OverString";
169 my $both = bless [], "OverBoth";
170 my $neither = bless [], "OverNeither";
171 my $nstr = qq($neither);
173 open my $gv, "<", "TEST";
174 bless $gv, "OverString";
175 open my $io, "<", "TEST";
177 bless $io, "OverString";
179 my $fcntl_not_available;
180 eval { require Fcntl } or $fcntl_not_available = 1;
182 for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
184 ok( my $rv = eval "-$op \$ft", "overloaded -$op succeeds" )
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");
190 my ($exp, $is) = (1, "is");
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() }
198 ($exp, $is) = (0, "not");
202 $rv = eval "-$op \$str";
203 ok( !$@, "-$op succeeds with string overloading" )
205 is( $rv, eval "-$op 'TEST'", "correct -$op on string overload" );
206 is( $over, $exp, "string overload $is called for -$op" );
208 ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
212 is( $over, $exp, "string overload $is called for -$op on GLOB" );
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");
220 is( $over, $exp, "string overload $is called for -$op on IO");
222 $rv = eval "-$op \$both";
223 is( $rv, "-$op", "correct -$op on string/-X overload" );
225 $rv = eval "-$op \$neither";
226 ok( !$@, "-$op succeeds with random overloading" )
228 is( $rv, eval "-$op \$nstr", "correct -$op with random overloading" );
230 is( eval "-r -$op \$ft", "-r", "stacked overloaded -$op" );
231 is( eval "-$op -r \$ft", "-$op", "overloaded stacked -$op" );
234 # -l stack corruption: this bug occurred from 5.8 to 5.14
236 push my @foo, "bar", -l baz;
237 is $foo[0], "bar", '-l bareword does not corrupt the stack';
240 # -l and fatal warnings
242 eval { use warnings FATAL => io; -l cradd };
244 'fatal warnings do not prevent -l HANDLE from setting stat status';
246 # File test ops should not call get-magic on the topmost SV on the stack if
247 # it belongs to another op.
250 sub oon::TIESCALAR{bless[],'oon'}
254 is $w, 1, 'file test does not call FETCH on stack item not its own';
259 my $Perl = which_perl();
262 skip "no -T on filehandles", 8 unless eval { -T STDERR; 1 };
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';
271 # statgv should be cleared when freed
273 'open my $fh, "test.pl"; -r $fh; undef $fh; open my $fh2, '
274 . "q\0$Perl\0; print -B _",
276 { switches => ['-l'] },
277 'PL_statgv should not point to freed-and-reused SV';
279 # or coerced into a non-glob
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 _",
284 { switches => ['-l'] },
285 'PL_statgv should not point to coerced-freed-and-reused GV';
287 # -T _ should work after stat $ioref
288 open my $fh, 'test.pl';
289 stat $Perl; # a binary file
291 ok -T _, '-T _ works after stat $ioref';
293 # and after -r $ioref
295 ok -T _, '-T _ works after -r $ioref';
297 # -T _ on closed filehandle should still reset stat info
301 ok !stat _, '-T _ on closed filehandle resets stat info';
306 like $@, qr/^The stat preceding lstat\(\) wasn't an lstat at /,
307 '-T on closed handle resets last stat type';
309 # Fatal warnings should not affect the setting of errno.
314 eval { use warnings FATAL => unopened; -T cradd };
317 'fatal warnings do not affect errno after -T BADHADNLE';
320 is runperl(prog => '-T _', switches => ['-w'], stderr => 1), "",
321 'no uninit warnings from -T with no preceding stat';
324 my $rand_file_name = 'filetest-' . rand =~ y/.//cdr;
325 if (-e $rand_file_name) { skip "File $rand_file_name exists", 1 }
328 ok !stat _, '-T "nonexistent" resets stat success status';
331 # Unsuccessful filetests on filehandles should leave stat buffers in the
332 # same state whether fatal warnings are on or off.
337 my $failed_stat1 = stat _;
340 eval { use warnings FATAL => unopened; -r *phlon };
341 my $failed_stat2 = stat _;
343 is $failed_stat2, $failed_stat1,
344 'failed -r($gv_without_io) with and w/out fatal warnings';
347 -r cength; # at compile time autovivifies IO, but with no fp
348 $failed_stat1 = stat _;
351 eval { use warnings FATAL => unopened; -r cength };
352 $failed_stat2 = stat _;
354 is $failed_stat2, $failed_stat1,
355 'failed -r($gv_with_io_but_no_fp) with and w/out fatal warnings';