This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Skip 2 tests in t/op/filetest.t if t/TEST is a symlink.
[perl5.git] / t / op / filetest.t
old mode 100755 (executable)
new mode 100644 (file)
index 0fec3c1..d013cce
@@ -10,7 +10,7 @@ BEGIN {
 }
 
 use Config;
-plan(tests => 28 + 27*10);
+plan(tests => 34 + 27*14);
 
 ok( -d 'op' );
 ok( -f 'TEST' );
@@ -18,9 +18,15 @@ ok( !-f 'op' );
 ok( !-d 'TEST' );
 ok( -r 'TEST' );
 
-# make sure TEST is r-x
-eval { chmod 0555, 'TEST' or die "chmod 0555, 'TEST' failed: $!" };
-chomp ($bad_chmod = $@);
+# Make a read only file
+my $ro_file = tempfile();
+
+{
+    open my $fh, '>', $ro_file or die "open $fh: $!";
+    close $fh or die "close $fh: $!";
+}
+
+chmod 0555, $ro_file or die "chmod 0555, '$ro_file' failed: $!";
 
 $oldeuid = $>;         # root can read and write anything
 eval '$> = 1';         # so switch uid (may not be implemented)
@@ -31,14 +37,8 @@ SKIP: {
     if (!$Config{d_seteuid}) {
        skip('no seteuid');
     } 
-    elsif ($Config{config_args} =~/Dmksymlinks/) {
-       skip('we cannot chmod symlinks');
-    }
-    elsif ($bad_chmod) {
-       skip( $bad_chmod );
-    }
     else {
-       ok( !-w 'TEST' );
+       ok( !-w $ro_file );
     }
 }
 
@@ -87,7 +87,39 @@ ok( -f $tempfile );
 is( -s $tempfile, 0 );
 is( -f -s $tempfile, 0 );
 is( -s -f $tempfile, 0 );
-unlink $tempfile;
+unlink_all $tempfile;
+
+# stacked -l
+eval { -l -e "TEST" };
+like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
+  'stacked -l non-lstat error with warnings off';
+{
+ local $^W = 1;
+ eval { -l -e "TEST" };
+ like $@, qr/^The stat preceding -l _ wasn't an lstat at /,
+  'stacked -l non-lstat error with warnings on';
+}
+# Make sure -l is using the previous stat buffer, and not using the previ-
+# ous op’s return value as a file name.
+SKIP: {
+ use Perl::OSType 'os_type';
+ if (os_type ne 'Unix') { skip "Not Unix", 2 }
+ if (-l "TEST") { skip "TEST is a symlink", 2 }
+ chomp(my $ln = `which ln`);
+ if ( ! -e $ln ) { skip "No ln"   , 2 }
+ lstat "TEST";
+ `ln -s TEST 1`;
+ ok ! -l -e _, 'stacked -l uses previous stat, not previous retval';
+ unlink 1;
+
+ # Since we already have our skip block set up, we might as well put this
+ # test here, too:
+ # -l always treats a non-bareword argument as a file name
+ system qw "ln -s TEST", \*foo;
+ local $^W = 1;
+ ok -l \*foo, '-l \*foo is a file name';
+ unlink \*foo;
+}
 
 # test that _ is a bareword after filetest operators
 
@@ -100,10 +132,12 @@ my $over;
 {
     package OverFtest;
 
-    use overload -X => sub { 
-        $over = [overload::StrVal($_[0]), $_[1]];
-        "-$_[1]"; 
-    };
+    use overload 
+       fallback => 1,
+        -X => sub { 
+            $over = [qq($_[0]), $_[1]];
+            "-$_[1]"; 
+        };
 }
 {
     package OverString;
@@ -122,7 +156,7 @@ my $over;
 {
     package OverNeither;
 
-    # Need fallback. Previous veraions of perl required 'fallback' to do
+    # Need fallback. Previous versions of perl required 'fallback' to do
     # -X operations on an object with no "" overload.
     use overload 
         '+' => sub { 1 },
@@ -130,11 +164,20 @@ my $over;
 }
 
 my $ft = bless [], "OverFtest";
-my $ftstr = overload::StrVal($ft);
+my $ftstr = qq($ft);
 my $str = bless [], "OverString";
 my $both = bless [], "OverBoth";
 my $neither = bless [], "OverNeither";
-my $nstr = overload::StrVal($neither);
+my $nstr = qq($neither);
+
+open my $gv, "<", "TEST";
+bless $gv, "OverString";
+open my $io, "<", "TEST";
+$io = *{$io}{IO};
+bless $io, "OverString";
+
+my $fcntl_not_available;
+eval { require Fcntl } or $fcntl_not_available = 1;
 
 for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
     $over = [];
@@ -144,12 +187,37 @@ for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
     is( $over->[1], $op,            "correct op for overloaded -$op" );
     is( $rv,        "-$op",         "correct return value for overloaded -$op");
 
+    my ($exp, $is) = (1, "is");
+    if (
+       !$fcntl_not_available and (
+        $op eq "u" and not eval { Fcntl::S_ISUID() } or
+        $op eq "g" and not eval { Fcntl::S_ISGID() } or
+        $op eq "k" and not eval { Fcntl::S_ISVTX() }
+       )
+    ) {
+        ($exp, $is) = (0, "not");
+    }
+
     $over = 0;
     $rv = eval "-$op \$str";
     ok( !$@,                        "-$op succeeds with string overloading" )
         or diag( $@ );
     is( $rv, eval "-$op 'TEST'",    "correct -$op on string overload" );
-    is( $over,      1,              "string overload called for -$op" );
+    is( $over,      $exp,           "string overload $is called for -$op" );
+
+    ($exp, $is) = $op eq "l" ? (1, "is") : (0, "not");
+
+    $over = 0;
+    eval "-$op \$gv";
+    is( $over,      $exp,   "string overload $is called for -$op on GLOB" );
+
+    # IO refs always get string overload called. This might be a bug.
+    $op eq "t" || $op eq "T" || $op eq "B"
+        and ($exp, $is) = (1, "is");
+
+    $over = 0;
+    eval "-$op \$io";
+    is( $over,      $exp,   "string overload $is called for -$op on IO");
 
     $rv = eval "-$op \$both";
     is( $rv,        "-$op",         "correct -$op on string/-X overload" );
@@ -158,6 +226,24 @@ for my $op (split //, "rwxoRWXOezsfdlpSbctugkTMBAC") {
     ok( !$@,                        "-$op succeeds with random overloading" )
         or diag( $@ );
     is( $rv, eval "-$op \$nstr",    "correct -$op with random overloading" );
+
+    is( eval "-r -$op \$ft", "-r",      "stacked overloaded -$op" );
+    is( eval "-$op -r \$ft", "-$op",    "overloaded stacked -$op" );
 }
 
-is( -r -f $ft,  "-r",               "stacked overloaded -X" );
+# -l stack corruption: this bug occurred from 5.8 to 5.14
+{
+ push my @foo, "bar", -l baz;
+ is $foo[0], "bar", '-l bareword does not corrupt the stack';
+}
+
+# File test ops should not call get-magic on the topmost SV on the stack if
+# it belongs to another op.
+{
+  my $w;
+  sub oon::TIESCALAR{bless[],'oon'}
+  sub oon::FETCH{$w++}
+  tie my $t, 'oon';
+  push my @a, $t, -t;
+  is $w, 1, 'file test does not call FETCH on stack item not its own';
+}