Commit | Line | Data |
---|---|---|
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 | ||
6 | BEGIN { | |
7 | chdir 't' if -d 't'; | |
20822f61 | 8 | @INC = '../lib'; |
fbb0b3b3 | 9 | require './test.pl'; |
42e55ab1 JH |
10 | } |
11 | ||
fea55e48 | 12 | plan(tests => 48 + 27*14); |
42e55ab1 | 13 | |
fbb0b3b3 RGS |
14 | ok( -d 'op' ); |
15 | ok( -f 'TEST' ); | |
16 | ok( !-f 'op' ); | |
17 | ok( !-d 'TEST' ); | |
18 | ok( -r 'TEST' ); | |
42e55ab1 | 19 | |
e687eccd NC |
20 | # Make a read only file. This happens to be empty, so we also use it later. |
21 | my $ro_empty_file = tempfile(); | |
d4188802 NC |
22 | |
23 | { | |
e687eccd | 24 | open my $fh, '>', $ro_empty_file or die "open $fh: $!"; |
d4188802 NC |
25 | close $fh or die "close $fh: $!"; |
26 | } | |
27 | ||
e687eccd | 28 | chmod 0555, $ro_empty_file or die "chmod 0555, '$ro_empty_file' failed: $!"; |
846f25a3 | 29 | |
fbb0b3b3 | 30 | SKIP: { |
dcefe67b NC |
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; | |
fbb0b3b3 | 40 | } |
42e55ab1 | 41 | |
e687eccd | 42 | ok( !-w $ro_empty_file ); |
42e55ab1 | 43 | |
dcefe67b NC |
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 | } | |
fd1e013e | 50 | |
64f0b68d | 51 | # these would fail for the euid 1 |
fd1e013e | 52 | # (unless we have unpacked the source code as uid 1...) |
fbb0b3b3 | 53 | ok( -r 'op' ); |
64f0b68d | 54 | ok( -w 'op' ); |
fbb0b3b3 RGS |
55 | ok( -x 'op' ); # 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 | |
42e55ab1 | 60 | |
fbb0b3b3 RGS |
61 | ok( defined( -f -d 'TEST' ) && ! -f -d _ ); |
62 | ok( !defined( -e 'zoo' ) ); | |
63 | ok( !defined( -e -d 'zoo' ) ); | |
64 | ok( !defined( -f -e 'zoo' ) ); | |
65 | ok( -f -e 'TEST' ); | |
66 | ok( -e -f 'TEST' ); | |
67 | ok( defined(-d -e 'TEST') ); | |
68 | ok( defined(-e -d 'TEST') ); | |
69 | ok( ! -f -d 'op' ); | |
70 | ok( -x -d -x 'op' ); | |
fea55e48 NC |
71 | my ($size) = (stat 'TEST')[7]; |
72 | cmp_ok($size, '>', 1, 'TEST is longer than 1 byte'); | |
73 | is( (-s -f 'TEST'), $size, "-s returns real size" ); | |
fbb0b3b3 | 74 | ok( -f -s 'TEST' == 1 ); |
7294df96 | 75 | |
e7d3eb55 | 76 | # now with an empty file |
e687eccd NC |
77 | ok( -f $ro_empty_file ); |
78 | is( -s $ro_empty_file, 0 ); | |
79 | is( -f -s $ro_empty_file, 0 ); | |
80 | is( -s -f $ro_empty_file, 0 ); | |
e7d3eb55 | 81 | |
1f26655e FC |
82 | # stacked -l |
83 | eval { -l -e "TEST" }; | |
84 | like $@, 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. | |
8f67f7cf | 94 | # t/TEST can be a symlink under -Dmksymlinks, so use our temporary file. |
1f26655e FC |
95 | SKIP: { |
96 | use Perl::OSType 'os_type'; | |
61602f40 | 97 | if (os_type ne 'Unix') { skip "Not Unix", 2 } |
1f26655e | 98 | chomp(my $ln = `which ln`); |
61602f40 | 99 | if ( ! -e $ln ) { skip "No ln" , 2 } |
8f67f7cf NC |
100 | lstat $ro_empty_file; |
101 | `ln -s $ro_empty_file 1`; | |
1f26655e FC |
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 | |
8f67f7cf | 108 | system 'ln', '-s', $ro_empty_file, \*foo; |
433644ee FC |
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'; | |
117 | ok( -f _ ); | |
118 | sub _ { "this is not a file name" } | |
119 | ok( -f _ ); | |
d89f1457 BM |
120 | |
121 | my $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 | ||
156 | my $ft = bless [], "OverFtest"; | |
c1d4704a | 157 | my $ftstr = qq($ft); |
f6aa8023 BM |
158 | my $str = bless [], "OverString"; |
159 | my $both = bless [], "OverBoth"; | |
160 | my $neither = bless [], "OverNeither"; | |
c1d4704a | 161 | my $nstr = qq($neither); |
d89f1457 | 162 | |
d0c91b6a BM |
163 | open my $gv, "<", "TEST"; |
164 | bless $gv, "OverString"; | |
165 | open my $io, "<", "TEST"; | |
166 | $io = *{$io}{IO}; | |
167 | bless $io, "OverString"; | |
168 | ||
822146ea RGS |
169 | my $fcntl_not_available; |
170 | eval { require Fcntl } or $fcntl_not_available = 1; | |
fa7f7498 | 171 | |
d89f1457 | 172 | for 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 BM |
180 | my ($exp, $is) = (1, "is"); |
181 | if ( | |
822146ea | 182 | !$fcntl_not_available and ( |
fa7f7498 BM |
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 BM |
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 |
231 | stat "test.pl"; | |
232 | eval { use warnings FATAL => io; -l cradd }; | |
233 | ok !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 |
249 | my $Perl = which_perl(); |
250 | ||
5731662b | 251 | SKIP: { |
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 | |
310 | is runperl(prog => '-T _', switches => ['-w'], stderr => 1), "", | |
311 | 'no uninit warnings from -T with no preceding stat'; | |
7b7309af | 312 | |
ad2d99e3 | 313 | SKIP: { |
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 | } |