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