3 # There are few filetest operators that are portable enough to test.
4 # See pod/perlport.pod for details.
13 plan(tests => 34 + 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 # File test ops should not call get-magic on the topmost SV on the stack if
241 # it belongs to another op.
244 sub oon::TIESCALAR{bless[],'oon'}
248 is $w, 1, 'file test does not call FETCH on stack item not its own';