Retract #13475 until Arthur gets back to the mine.
[p5sagit/p5-mst-13.2.git] / t / op / stat.t
CommitLineData
8d063cd8 1#!./perl
2
ea368a7c 3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
cc25fa79 6 require './test.pl';
ea368a7c 7}
8
9use Config;
de5a37b2 10use File::Spec;
ea368a7c 11
de5a37b2 12plan tests => 73;
8d063cd8 13
cc25fa79 14$Is_Amiga = $^O eq 'amigaos';
15$Is_Cygwin = $^O eq 'cygwin';
16$Is_Dos = $^O eq 'dos';
17$Is_MPE = $^O eq 'mpeix';
68dc0745 18$Is_MSWin32 = $^O eq 'MSWin32';
2986a63f 19$Is_NetWare = $^O eq 'NetWare';
cc25fa79 20$Is_OS2 = $^O eq 'os2';
21$Is_Solaris = $^O eq 'solaris';
de5a37b2 22$Is_VMS = $^O eq 'VMS';
cc25fa79 23
24$Is_Dosish = $Is_Dos || $Is_OS2 || $Is_MSWin32 || $Is_NetWare || $Is_Cygwin;
cc25fa79 25
26my($DEV, $INO, $MODE, $NLINK, $UID, $GID, $RDEV, $SIZE,
27 $ATIME, $MTIME, $CTIME, $BLKSIZE, $BLOCKS) = (0..12);
d48672a2 28
de5a37b2 29my $Curdir = File::Spec->curdir;
30
4435c477 31
de5a37b2 32my $tmpfile = 'Op_stat.tmp';
cc25fa79 33my $tmpfile_link = $tmpfile.'2';
4435c477 34
8d220878 35
cc25fa79 36unlink $tmpfile;
37open(FOO, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
de5a37b2 38close FOO;
8d220878 39
de5a37b2 40open(FOO, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
4435c477 41
cc25fa79 42my($nlink, $mtime, $ctime) = (stat(FOO))[$NLINK, $MTIME, $CTIME];
43is($nlink, 1, 'nlink on regular file');
8d220878 44
cc25fa79 45SKIP: {
46 skip "mtime and ctime not reliable", 2
47 if $Is_MSWin32 or $Is_NetWare or $Is_Cygwin or $Is_Dos;
48
49 ok( $mtime, 'mtime' );
50 is( $mtime, $ctime, 'mtime == ctime' );
4435c477 51}
8d063cd8 52
cc25fa79 53
54# Cygwin seems to have a 3 second granularity on its timestamps.
55my $funky_FAT_timestamps = $Is_Cygwin;
56sleep 3 if $funky_FAT_timestamps;
57
58print FOO "Now is the time for all good men to come to.\n";
59close(FOO);
60
61sleep 2 unless $funky_FAT_timestamps;
62
63
64SKIP: {
65 unlink $tmpfile_link;
de5a37b2 66 my $lnk_result = eval { link $tmpfile, $tmpfile_link };
67 skip "link() unimplemented", 6 if $@ =~ /unimplemented/;
cc25fa79 68
de5a37b2 69 is( $@, '', 'link() implemented' );
70 ok( $lnk_result, 'linked tmp testfile' );
cc25fa79 71 ok( chmod(0644, $tmpfile), 'chmoded tmp testfile' );
72
73 my($nlink, $mtime, $ctime) = (stat($tmpfile))[$NLINK, $MTIME, $CTIME];
74
75 SKIP: {
76 skip "No link count", 1 if $Config{dont_use_nlink};
77 is($nlink, 2, 'Link count on hard linked file' );
78 }
79
80 SKIP: {
de5a37b2 81 my $cwd = File::Spec->rel2abs($Curdir);
cc25fa79 82 skip "Solaris tmpfs has different mtime/ctime link semantics", 2
83 if $Is_Solaris and $cwd =~ m#^/tmp# and
84 $mtime && $mtime == $ctime;
85 skip "AFS has different mtime/ctime link semantics", 2
86 if $cwd =~ m#$Config{'afsroot'}/#;
87 skip "AmigaOS has different mtime/ctime link semantics", 2
88 if $Is_Amiga;
89
90 if( !ok($mtime, 'hard link mtime') ||
91 !isnt($mtime, $ctime, 'hard link ctime != mtime') ) {
92 print <<DIAG;
93# Check if you are on a tmpfs of some sort. Building in /tmp sometimes
94# has this problem. Also building on the ClearCase VOBS filesystem may
95# cause this failure.
96DIAG
97 }
98 }
99
3fe9a6f1 100}
8d063cd8 101
cc25fa79 102# truncate and touch $tmpfile.
103open(F, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
104close F;
105
106ok(-z $tmpfile, '-z on empty file');
107ok(! -s $tmpfile, ' and -s');
108
109open(F, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
110print F "hi\n";
111close F;
112
113ok(! -z $tmpfile, '-z on non-empty file');
114ok(-s $tmpfile, ' and -s');
115
116
117# Strip all access rights from the file.
118ok( chmod(0000, $tmpfile), 'chmod 0000' );
119
120SKIP: {
de5a37b2 121 skip "-r, -w and -x have different meanings on VMS", 3 if $Is_VMS;
cc25fa79 122
123 SKIP: {
de5a37b2 124 # Going to try to switch away from root. Might not work.
125 my $olduid = $>;
126 eval { $> = 1; };
127 skip "Can't test -r or -w meaningfully if you're superuser", 2
128 if $> == 0;
129
130 SKIP: {
131 skip "Can't test -r meaningfully?", 1 if $Is_Dos || $Is_Cygwin;
132 ok(!-r $tmpfile, " -r");
133 }
cc25fa79 134
de5a37b2 135 ok(!-w $tmpfile, " -w");
cc25fa79 136
de5a37b2 137 # switch uid back (may not be implemented)
138 eval { $> = $olduid; };
139 }
140
141 ok(! -x $tmpfile, ' -x');
a245ea2d 142}
cc25fa79 143
de5a37b2 144
cc25fa79 145
146
147# in ms windows, $tmpfile inherits owner uid from directory
148# not sure about os/2, but chown is harmless anyway
149eval { chown $>,$tmpfile; 1 } or print "# $@" ;
150
151ok(chmod(0700,$tmpfile), 'chmod 0700');
152ok(-r $tmpfile, ' -r');
153ok(-w $tmpfile, ' -w');
154
155SKIP: {
156 skip "-x simply determins if a file ends in an executable suffix", 1
157 if $Is_Dosish;
158
159 ok(-x $tmpfile, ' -x');
6eb13c3b 160}
cc25fa79 161
162ok( -f $tmpfile, ' -f');
163ok(! -d $tmpfile, ' !-d');
164
165# Is this portable?
de5a37b2 166ok( -d $Curdir, '-d cwd' );
167ok(! -f $Curdir, '!-f cwd' );
168
cc25fa79 169
170SKIP: {
de5a37b2 171 unlink($tmpfile_link);
172 my $symlink_rslt = eval { symlink $tmpfile, $tmpfile_link };
173 skip "symlink not implemented", 3 if $@ =~ /unimplemented/;
cc25fa79 174
de5a37b2 175 is( $@, '', 'symlink() implemented' );
176 ok( $symlink_rslt, 'symlink() ok' );
177 ok(-l $tmpfile_link, '-l');
6eb13c3b 178}
cc25fa79 179
180ok(-o $tmpfile, '-o');
181
182ok(-e $tmpfile, '-e');
de5a37b2 183
184unlink($tmpfile_link);
cc25fa79 185ok(! -e $tmpfile_link, ' -e on unlinked file');
186
187SKIP: {
de5a37b2 188 skip "No character, socket or block special files", 7
cc25fa79 189 if $Is_MSWin32 || $Is_NetWare || $Is_Dos;
de5a37b2 190 skip "/dev/ isn't available to test against", 7
191 unless -d '/dev/' && -r '/dev/' && -x '/dev/';
192
193 opendir DEV, "/dev/" or BAILOUT("Can't open /dev/: $!");
194 my($cnt, $char, $sock, $block);
195 $cnt = $char = $sock = $block = 0;
196 foreach (readdir DEV) {
197 my $file = "/dev/$_";
198 $cnt++;
199 $char++ if -c $file;
200 $sock++ if -S $file;
201 $block++ if -b $file;
202 }
cc25fa79 203
de5a37b2 204 isnt( $cnt, 0, 'Found some files in /dev/ to test against' );
205 isnt( $char, 0, ' and some character special files' );
206 isnt( $sock, 0, ' and some socket files' );
207 isnt( $block, 0, ' and some block special files' );
208 ok( $char < $cnt, " they're not all character special" );
209 ok( $sock < $cnt, " they're not all sockets" );
210 ok( $block < $cnt, " they're not all block special" );
378cc40b 211}
212
de5a37b2 213ok(! -c $Curdir, '!-c cwd');
214ok(! -S $Curdir, '!-S cwd');
215ok(! -b $Curdir, '!-b cwd');
cc25fa79 216
cc25fa79 217
218SKIP: {
de5a37b2 219 skip "No setuid", 3 if $Is_MPE or $Is_Amiga or $Is_Dosish or $Is_Cygwin;
b971f6e4 220
cc25fa79 221 my($cnt, $uid);
222 $cnt = $uid = 0;
223
224 # Find a set of directories that's very likely to have setuid files
225 # but not likely to be *all* setuid files.
226 my @bin = grep {-d && -r && -x} qw(/sbin /usr/sbin /bin /usr/bin);
de5a37b2 227 skip "Can't find a setuid file to test with", 3 unless @bin;
cc25fa79 228
229 for my $bin (@bin) {
230 opendir BIN, $bin or die "Can't opendir $bin: $!";
231 while (defined($_ = readdir BIN)) {
232 $_ = "$bin/$_";
233 $cnt++;
234 $uid++ if -u;
235 last if $uid && $uid < $cnt;
236 }
237 }
238 closedir BIN;
239
de5a37b2 240 if( !isnt($cnt, 0, 'found some programs') ||
241 !isnt($uid, 0, 'found some setuid programs') ||
cc25fa79 242 !ok($uid < $cnt, " they're not all setuid") )
243 {
244 print <<DIAG;
245# The above two tests assume that at least one of these directories
246# are readable, executable and contain at least one setuid file
247# (but aren't all setuid).
248# @bin
249DIAG
218d7377 250 }
378cc40b 251}
b8440792 252
378cc40b 253
3e3baf6d 254# To assist in automated testing when a controlling terminal (/dev/tty)
255# may not be available (at, cron rsh etc), the PERL_SKIP_TTY_TEST env var
256# can be set to skip the tests that need a tty.
cc25fa79 257SKIP: {
258 skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST};
259
260 my $TTY = $^O eq 'rhapsody' ? "/dev/ttyp0" : "/dev/tty";
261
262 SKIP: {
263 skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare;
264 skip "No TTY to test -t with", 2 unless -e $TTY;
265
266 open(TTY, $TTY) ||
267 warn "Can't open $TTY--run t/TEST outside of make.\n";
268 ok(-t TTY, '-t');
269 ok(-c TTY, 'tty is -c');
270 close(TTY);
3e3baf6d 271 }
cc25fa79 272 ok(! -t TTY, '!-t on closed TTY filehandle');
273 ok(-t, '-t on STDIN');
68dc0745 274}
cc25fa79 275
de5a37b2 276my $Null = File::Spec->devnull;
cc25fa79 277SKIP: {
de5a37b2 278 skip "No null device to test with", 1 unless -e $Null;
cc25fa79 279
de5a37b2 280 open(NULL, $Null) or BAIL_OUT("Can't open $Null: $!");
281 ok(! -t NULL, 'null device is not a TTY');
cc25fa79 282 close(NULL);
378cc40b 283}
cc25fa79 284
378cc40b 285
286# These aren't strictly "stat" calls, but so what?
287
cc25fa79 288ok(-T 'op/stat.t', '-T');
289ok(! -B 'op/stat.t', '!-B');
378cc40b 290
cc25fa79 291ok(-B $^X, '-B');
292ok(! -T $^X, '!-T');
378cc40b 293
f0fcb552 294open(FOO,'op/stat.t');
cc25fa79 295SKIP: {
296 eval { -T FOO; };
de5a37b2 297 skip "-T/B on filehandle not implemented", 15 if $@ =~ /not implemented/;
cc25fa79 298
299 is( $@, '', '-T on filehandle causes no errors' );
300
301 ok(-T FOO, ' -T');
302 ok(! -B FOO, ' !-B');
303
f0fcb552 304 $_ = <FOO>;
cc25fa79 305 ok(/perl/, 'after readline');
306 ok(-T FOO, ' still -T');
307 ok(! -B FOO, ' still -B');
f0fcb552 308 close(FOO);
309
310 open(FOO,'op/stat.t');
311 $_ = <FOO>;
cc25fa79 312 ok(/perl/, 'reopened and after readline');
313 ok(-T FOO, ' still -T');
314 ok(! -B FOO, ' still !-B');
315
316 ok(seek(FOO,0,0), 'after seek');
de5a37b2 317 ok(-T FOO, ' still -T');
318 ok(! -B FOO, ' still !-B');
319
320 # It's documented this way in perlfunc *shrug*
321 () = <FOO>;
322 ok(eof FOO, 'at EOF');
323 ok(-T FOO, ' still -T');
324 ok(-B FOO, ' now -B');
f0fcb552 325}
326close(FOO);
378cc40b 327
cc25fa79 328
de5a37b2 329SKIP: {
330 skip "No null device to test with", 2 unless -e $Null;
331
332 ok(-T $Null, 'null device is -T');
333 ok(-B $Null, ' and -B');
334}
cc25fa79 335
108ed793 336
337# and now, a few parsing tests:
cc25fa79 338$_ = $tmpfile;
339ok(-f, 'bare -f uses $_');
340ok(-f(), ' -f() "');
108ed793 341
cc25fa79 342unlink $tmpfile or print "# unlink failed: $!\n";
58d95175 343
344# bug id 20011101.069
345my @r = \stat(".");
cc25fa79 346is(scalar @r, 13, 'stat returns full 13 elements');
347