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