983b54d364af838a9cc87b03c93f8236aebe5601
[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 is($nlink, 1, 'nlink on regular file');
44
45 SKIP: {
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' );
51 }
52
53
54 # Cygwin seems to have a 3 second granularity on its timestamps.
55 my $funky_FAT_timestamps = $Is_Cygwin;
56 sleep 3 if $funky_FAT_timestamps;
57
58 print FOO "Now is the time for all good men to come to.\n";
59 close(FOO);
60
61 sleep 2 unless $funky_FAT_timestamps;
62
63
64 SKIP: {
65     unlink $tmpfile_link;
66     my $lnk_result = eval { link $tmpfile, $tmpfile_link };
67     skip "link() unimplemented", 6 if $@ =~ /unimplemented/;
68
69     is( $@, '',         'link() implemented' );
70     ok( $lnk_result,    'linked tmp testfile' );
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: {
81         my $cwd = File::Spec->rel2abs($Curdir);
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.
96 DIAG
97         }
98     }
99
100 }
101
102 # truncate and touch $tmpfile.
103 open(F, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
104 close F;
105
106 ok(-z $tmpfile,     '-z on empty file');
107 ok(! -s $tmpfile,   '   and -s');
108
109 open(F, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
110 print F "hi\n";
111 close F;
112
113 ok(! -z $tmpfile,   '-z on non-empty file');
114 ok(-s $tmpfile,     '   and -s');
115
116
117 # Strip all access rights from the file.
118 ok( chmod(0000, $tmpfile),     'chmod 0000' );
119
120 SKIP: {
121     skip "-r, -w and -x have different meanings on VMS", 3 if $Is_VMS;
122
123     SKIP: {
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         }
134
135         ok(!-w $tmpfile,    "   -w");
136
137         # switch uid back (may not be implemented)
138         eval { $> = $olduid; };
139     }
140
141     ok(! -x $tmpfile,   '   -x');
142 }
143
144
145
146
147 # in ms windows, $tmpfile inherits owner uid from directory
148 # not sure about os/2, but chown is harmless anyway
149 eval { chown $>,$tmpfile; 1 } or print "# $@" ;
150
151 ok(chmod(0700,$tmpfile),    'chmod 0700');
152 ok(-r $tmpfile,     '   -r');
153 ok(-w $tmpfile,     '   -w');
154
155 SKIP: {
156     skip "-x simply determins if a file ends in an executable suffix", 1
157       if $Is_Dosish;
158
159     ok(-x $tmpfile,     '   -x');
160 }
161
162 ok(  -f $tmpfile,   '   -f');
163 ok(! -d $tmpfile,   '   !-d');
164
165 # Is this portable?
166 ok(  -d $Curdir,          '-d cwd' );
167 ok(! -f $Curdir,          '!-f cwd' );
168
169
170 SKIP: {
171     unlink($tmpfile_link);
172     my $symlink_rslt = eval { symlink $tmpfile, $tmpfile_link };
173     skip "symlink not implemented", 3 if $@ =~ /unimplemented/;
174
175     is( $@, '',     'symlink() implemented' );
176     ok( $symlink_rslt,      'symlink() ok' );
177     ok(-l $tmpfile_link,    '-l');
178 }
179
180 ok(-o $tmpfile,     '-o');
181
182 ok(-e $tmpfile,     '-e');
183
184 unlink($tmpfile_link);
185 ok(! -e $tmpfile_link,  '   -e on unlinked file');
186
187 SKIP: {
188     skip "No character, socket or block special files", 3
189       if $Is_MSWin32 || $Is_NetWare || $Is_Dos;
190     skip "/dev/ isn't available to test against", 3
191       unless -d '/dev/' && -r '/dev/' && -x '/dev/';
192
193     my $LS  = $Config{d_readlink} ? "ls -lL" : "ls -l"; 
194     my $CMD = "$LS /dev";
195     my $DEV = qx($CMD);
196
197     skip "$CMD failed", 3 if $DEV eq '';
198
199     my $type;
200
201     my $skip = sub { ok(1, "Skip: no $_[0]s in /dev") };
202     my $ok   = sub { ok(1, "$_[0] /dev/$1") };
203     my $fail = sub { ok(0, "ls -l thinks /dev/$1 is $_[0] but $_[1] says not") };
204     
205     if ($DEV !~ /\nb.* (\S+)\n/) { $skip->("block special") }
206     elsif (-b "/dev/$1")         { $ok->  ("block special") }
207     else { $fail->("block special", "-b") }
208     
209     if ($DEV !~ /\nc.* (\S+)\n/) { $skip->("character special") }
210     elsif (-c "/dev/$1")         { $ok->  ("character special") }
211     else { $fail->("character special", "-c") }
212     
213     if ($DEV !~ /\ns.* (\S+)\n/) { $skip->("socket") }
214     elsif (-S "/dev/$1")         { $ok->  ("socket") }
215     else { $fail->("socket", "-S") }
216 }
217
218 ok(! -c $Curdir,    '!-c cwd');
219 ok(! -S $Curdir,    '!-S cwd');
220 ok(! -b $Curdir,    '!-b cwd');
221
222
223 SKIP: {
224     skip "No setuid", 3 if $Is_MPE or $Is_Amiga or $Is_Dosish or $Is_Cygwin;
225
226     my($cnt, $uid);
227     $cnt = $uid = 0;
228
229     # Find a set of directories that's very likely to have setuid files
230     # but not likely to be *all* setuid files.
231     my @bin = grep {-d && -r && -x} qw(/sbin /usr/sbin /bin /usr/bin);
232     skip "Can't find a setuid file to test with", 3 unless @bin;
233
234     for my $bin (@bin) {
235         opendir BIN, $bin or die "Can't opendir $bin: $!";
236         while (defined($_ = readdir BIN)) {
237             $_ = "$bin/$_";
238             $cnt++;
239             $uid++ if -u;
240             last if $uid && $uid < $cnt;
241         }
242     }
243     closedir BIN;
244
245     if( !isnt($cnt, 0,    'found some programs') ||
246         !isnt($uid, 0,    'found some setuid programs') ||
247         !ok($uid < $cnt,  "  they're not all setuid") )
248     {
249         print <<DIAG;
250 # The above two tests assume that at least one of these directories
251 # are readable, executable and contain at least one setuid file
252 # (but aren't all setuid).
253 #   @bin
254 DIAG
255     }
256 }
257
258
259 # To assist in automated testing when a controlling terminal (/dev/tty)
260 # may not be available (at, cron  rsh etc), the PERL_SKIP_TTY_TEST env var
261 # can be set to skip the tests that need a tty.
262 SKIP: {
263     skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST};
264
265     my $TTY = $^O eq 'rhapsody' ? "/dev/ttyp0" : "/dev/tty";
266
267     SKIP: {
268         skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare;
269         skip "No TTY to test -t with", 2 unless -e $TTY;
270
271         open(TTY, $TTY) || 
272           warn "Can't open $TTY--run t/TEST outside of make.\n";
273         ok(-t TTY,  '-t');
274         ok(-c TTY,  'tty is -c');
275         close(TTY);
276     }
277     ok(! -t TTY,    '!-t on closed TTY filehandle');
278     ok(-t,          '-t on STDIN');
279 }
280
281 my $Null = File::Spec->devnull;
282 SKIP: {
283     skip "No null device to test with", 1 unless -e $Null;
284
285     open(NULL, $Null) or BAIL_OUT("Can't open $Null: $!");
286     ok(! -t NULL,   'null device is not a TTY');
287     close(NULL);
288 }
289
290
291 # These aren't strictly "stat" calls, but so what?
292
293 ok(-T 'op/stat.t',      '-T');
294 ok(! -B 'op/stat.t',    '!-B');
295
296 ok(-B $^X,      '-B');
297 ok(! -T $^X,    '!-T');
298
299 open(FOO,'op/stat.t');
300 SKIP: {
301     eval { -T FOO; };
302     skip "-T/B on filehandle not implemented", 15 if $@ =~ /not implemented/;
303
304     is( $@, '',     '-T on filehandle causes no errors' );
305
306     ok(-T FOO,      '   -T');
307     ok(! -B FOO,    '   !-B');
308
309     $_ = <FOO>;
310     ok(/perl/,      'after readline');
311     ok(-T FOO,      '   still -T');
312     ok(! -B FOO,    '   still -B');
313     close(FOO);
314
315     open(FOO,'op/stat.t');
316     $_ = <FOO>;
317     ok(/perl/,      'reopened and after readline');
318     ok(-T FOO,      '   still -T');
319     ok(! -B FOO,    '   still !-B');
320
321     ok(seek(FOO,0,0),   'after seek');
322     ok(-T FOO,          '   still -T');
323     ok(! -B FOO,        '   still !-B');
324
325     # It's documented this way in perlfunc *shrug*
326     () = <FOO>;
327     ok(eof FOO,         'at EOF');
328     ok(-T FOO,          '   still -T');
329     ok(-B FOO,          '   now -B');
330 }
331 close(FOO);
332
333
334 SKIP: {
335     skip "No null device to test with", 2 unless -e $Null;
336
337     ok(-T $Null,  'null device is -T');
338     ok(-B $Null,  '    and -B');
339 }
340
341
342 # and now, a few parsing tests:
343 $_ = $tmpfile;
344 ok(-f,      'bare -f   uses $_');
345 ok(-f(),    '     -f() "');
346
347 unlink $tmpfile or print "# unlink failed: $!\n";
348
349 # bug id 20011101.069
350 my @r = \stat(".");
351 is(scalar @r, 13,   'stat returns full 13 elements');
352