This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix File::stat's -x and -X for root for directories and executable files.
[perl5.git] / lib / File / stat.t
CommitLineData
15b7a6a8 1#!./perl
f7a45afb
JH
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
d219c4fb
NC
8use strict;
9use warnings;
2f173a71 10use Test::More;
61a7d4fd 11use Config qw( %Config );
81a201f4 12use File::Temp qw( tempfile tempdir );
c69df70f 13
ff4803d9 14use File::stat;
f7a45afb 15
17c017a5 16my (undef, $file) = tempfile();
2f173a71 17
cf23dc19
NC
18{
19 my @stat = CORE::stat $file;
20 my $stat = File::stat::stat($file);
21 isa_ok($stat, 'File::stat', 'should build a stat object');
ff4803d9 22 is_deeply($stat, \@stat, '... and matches the builtin');
cf23dc19
NC
23
24 my $i = 0;
25 foreach ([dev => 'device number'],
26 [ino => 'inode number'],
27 [mode => 'file mode'],
28 [nlink => 'number of links'],
29 [uid => 'owner uid'],
30 [gid => 'group id'],
31 [rdev => 'device identifier'],
32 [size => 'file size'],
33 [atime => 'last access time'],
34 [mtime => 'last modify time'],
35 [ctime => 'change time'],
36 [blksize => 'IO block size'],
37 [blocks => 'number of blocks']) {
38 my ($meth, $desc) = @$_;
39 # On OS/2 (fake) ino is not constant, it is incremented each time
40 SKIP: {
41 skip('inode number is not constant on OS/2', 1)
42 if $i == 1 && $^O eq 'os2';
43 is($stat->$meth, $stat[$i], "$desc in position $i");
db8b1169 44 }
cf23dc19
NC
45 ++$i;
46 }
ff4803d9
NC
47
48 my $stat2 = stat $file;
49 isa_ok($stat2, 'File::stat',
50 'File::stat exports stat, overriding the builtin');
51 is_deeply($stat2, $stat, '... and matches the direct call');
cf23dc19 52}
db8b1169 53
cf23dc19
NC
54sub test_X_ops {
55 my ($file, $desc_tail) = @_;
56 my @stat = CORE::stat $file;
57 my $stat = File::stat::stat($file);
58 isa_ok($stat, 'File::stat', 'should build a stat object');
59
60 for my $op (split //, "rwxoRWXOezsfdlpSbcugkMCA") {
61 for my $access ('', 'use filetest "access";') {
62 my ($warnings, $awarn, $vwarn, $rv);
63 my $desc = $access
64 ? "for -$op under use filetest 'access' $desc_tail"
65 : "for -$op $desc_tail";
66 {
67 local $SIG{__WARN__} = sub {
68 my $w = shift;
69 if ($w =~ /^File::stat ignores VMS ACLs/) {
70 ++$vwarn;
71 } elsif ($w =~ /^File::stat ignores use filetest 'access'/) {
72 ++$awarn;
73 } else {
74 $warnings .= $w;
75 }
76 };
77 $rv = eval "$access; -$op \$stat";
78 }
79 is($@, '', "Overload succeeds $desc");
80
81 if ($^O eq "VMS" && $op =~ /[rwxRWX]/) {
82 is($vwarn, 1, "warning about VMS ACLs $desc");
83 } else {
84 is($rv, eval "-$op \$file", "correct overload $desc")
85 unless $access;
86 is($vwarn, undef, "no warnings about VMS ACLs $desc");
87 }
88
89 # 111640 - File::stat bogus index check in overload
90 if ($access && $op =~ /[rwxRXW]/) {
91 # these should all warn with filetest access
92 is($awarn, 1,
93 "produced the right warning $desc");
94 } else {
95 # -d and others shouldn't warn
96 is($awarn, undef, "should be no warning $desc")
97 }
98
99 is($warnings, undef, "no other warnings seen $desc");
db8b1169 100 }
cd22a09c
BM
101 }
102}
103
81a201f4
NC
104foreach ([file => $file],
105 [dir => tempdir(CLEANUP => 1)]) {
106 my ($what, $pathname) = @$_;
107 test_X_ops($pathname, "for $what $pathname");
cf23dc19 108
b050b2f8
NC
109 my $mode = 01000;
110 while ($mode) {
111 $mode >>= 1;
112 my $mode_oct = sprintf "0%03o", $mode;
81a201f4
NC
113 chmod $mode, $pathname or die "Can't chmod $mode_oct $pathname: $!";
114 test_X_ops($pathname, "for $what with mode=$mode_oct");
b050b2f8 115 }
81a201f4
NC
116 chmod 0600, $pathname
117 or die "Can't restore permissions on $pathname to 0600";
b050b2f8
NC
118}
119
3c1f5943 120SKIP: {
cf23dc19 121 -e $^X && -x $^X or skip "$^X is not present and executable", 4;
3c1f5943
TR
122 $^O eq "VMS" and skip "File::stat ignores VMS ACLs", 4;
123
cf23dc19 124 test_X_ops($^X, "for $^X");
3c1f5943
TR
125}
126
127
cf23dc19
NC
128my $stat = File::stat::stat($file);
129isa_ok($stat, 'File::stat', 'should build a stat object');
130
cd22a09c
BM
131for (split //, "tTB") {
132 eval "-$_ \$stat";
133 like( $@, qr/\Q-$_ is not implemented/, "-$_ overload fails" );
134}
135
83716b1e 136SKIP: {
137 local *STAT;
61a7d4fd 138 skip("Could not open file: $!", 2) unless open(STAT, $file);
cf23dc19
NC
139 isa_ok(File::stat::stat('STAT'), 'File::stat',
140 '... should be able to find filehandle');
83716b1e 141
142 package foo;
143 local *STAT = *main::STAT;
cf23dc19
NC
144 my $stat2 = File::stat::stat('STAT');
145 main::isa_ok($stat2, 'File::stat',
146 '... and filehandle in another package');
83716b1e 147 close STAT;
148
659293e7
PG
149# VOS open() updates atime; ignore this error (posix-975).
150 my $stat3 = $stat2;
151 if ($^O eq 'vos') {
152 $$stat3[8] = $$stat[8];
153 }
154
378bd967 155 main::skip("Win32: different stat-info on filehandle", 1) if $^O eq 'MSWin32';
afdf87ad 156 main::skip("dos: inode number is fake on dos", 1) if $^O eq 'dos';
378bd967 157
d51a9dd5
JH
158 main::skip("OS/2: inode number is not constant on os/2", 1) if $^O eq 'os2';
159
cf23dc19 160 main::is_deeply($stat, $stat3, '... and must match normal stat');
83716b1e 161}
162
e0b1616f
TC
163SKIP:
164{ # RT #111638
165 skip "We can't check for FIFOs", 2 unless defined &Fcntl::S_ISFIFO;
166 skip "No pipes", 2 unless defined $Config{d_pipe};
167 pipe my ($rh, $wh)
168 or skip "Couldn't create a pipe: $!", 2;
169 skip "Built-in -p doesn't detect a pipe", 2 unless -p $rh;
170
171 my $pstat = File::stat::stat($rh);
172 ok(!-p($stat), "-p should be false on a file");
e0b1616f
TC
173 ok(-p($pstat), "check -p detects a pipe");
174}
175
f7a45afb 176# Testing pretty much anything else is unportable.
c239c67c
NC
177
178done_testing;
179
180# Local variables:
181# cperl-indent-level: 4
182# indent-tabs-mode: nil
183# End:
184#
185# ex: set ts=8 sts=4 sw=4 et: