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';
24 $Is_Dosish = $Is_Dos || $Is_OS2 || $Is_MSWin32 || $Is_NetWare || $Is_Cygwin;
26 my($DEV, $INO, $MODE, $NLINK, $UID, $GID, $RDEV, $SIZE,
27 $ATIME, $MTIME, $CTIME, $BLKSIZE, $BLOCKS) = (0..12);
29 my $Curdir = File::Spec->curdir;
32 my $tmpfile = 'Op_stat.tmp';
33 my $tmpfile_link = $tmpfile.'2';
37 open(FOO, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
40 open(FOO, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
42 my($nlink, $mtime, $ctime) = (stat(FOO))[$NLINK, $MTIME, $CTIME];
43 is($nlink, 1, 'nlink on regular file');
46 skip "mtime and ctime not reliable", 2
47 if $Is_MSWin32 or $Is_NetWare or $Is_Cygwin or $Is_Dos;
49 ok( $mtime, 'mtime' );
50 is( $mtime, $ctime, 'mtime == ctime' );
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;
58 print FOO "Now is the time for all good men to come to.\n";
61 sleep 2 unless $funky_FAT_timestamps;
66 my $lnk_result = eval { link $tmpfile, $tmpfile_link };
67 skip "link() unimplemented", 6 if $@ =~ /unimplemented/;
69 is( $@, '', 'link() implemented' );
70 ok( $lnk_result, 'linked tmp testfile' );
71 ok( chmod(0644, $tmpfile), 'chmoded tmp testfile' );
73 my($nlink, $mtime, $ctime) = (stat($tmpfile))[$NLINK, $MTIME, $CTIME];
76 skip "No link count", 1 if $Config{dont_use_nlink};
77 is($nlink, 2, 'Link count on hard linked file' );
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
90 if( !ok($mtime, 'hard link mtime') ||
91 !isnt($mtime, $ctime, 'hard link ctime != mtime') ) {
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
102 # truncate and touch $tmpfile.
103 open(F, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
106 ok(-z $tmpfile, '-z on empty file');
107 ok(! -s $tmpfile, ' and -s');
109 open(F, ">$tmpfile") || BAILOUT("Can't open temp test file: $!");
113 ok(! -z $tmpfile, '-z on non-empty file');
114 ok(-s $tmpfile, ' and -s');
117 # Strip all access rights from the file.
118 ok( chmod(0000, $tmpfile), 'chmod 0000' );
121 skip "-r, -w and -x have different meanings on VMS", 3 if $Is_VMS;
124 # Going to try to switch away from root. Might not work.
127 skip "Can't test -r or -w meaningfully if you're superuser", 2
131 skip "Can't test -r meaningfully?", 1 if $Is_Dos || $Is_Cygwin;
132 ok(!-r $tmpfile, " -r");
135 ok(!-w $tmpfile, " -w");
137 # switch uid back (may not be implemented)
138 eval { $> = $olduid; };
141 ok(! -x $tmpfile, ' -x');
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 "# $@" ;
151 ok(chmod(0700,$tmpfile), 'chmod 0700');
152 ok(-r $tmpfile, ' -r');
153 ok(-w $tmpfile, ' -w');
156 skip "-x simply determins if a file ends in an executable suffix", 1
159 ok(-x $tmpfile, ' -x');
162 ok( -f $tmpfile, ' -f');
163 ok(! -d $tmpfile, ' !-d');
166 ok( -d $Curdir, '-d cwd' );
167 ok(! -f $Curdir, '!-f cwd' );
171 unlink($tmpfile_link);
172 my $symlink_rslt = eval { symlink $tmpfile, $tmpfile_link };
173 skip "symlink not implemented", 3 if $@ =~ /unimplemented/;
175 is( $@, '', 'symlink() implemented' );
176 ok( $symlink_rslt, 'symlink() ok' );
177 ok(-l $tmpfile_link, '-l');
180 ok(-o $tmpfile, '-o');
182 ok(-e $tmpfile, '-e');
184 unlink($tmpfile_link);
185 ok(! -e $tmpfile_link, ' -e on unlinked file');
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/';
193 my $LS = $Config{d_readlink} ? "ls -lL" : "ls -l";
194 my $CMD = "$LS /dev";
197 skip "$CMD failed", 3 if $DEV eq '';
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") };
205 if ($DEV !~ /\nb.* (\S+)\n/) { $skip->("block special") }
206 elsif (-b "/dev/$1") { $ok-> ("block special") }
207 else { $fail->("block special", "-b") }
209 if ($DEV !~ /\nc.* (\S+)\n/) { $skip->("character special") }
210 elsif (-c "/dev/$1") { $ok-> ("character special") }
211 else { $fail->("character special", "-c") }
213 if ($DEV !~ /\ns.* (\S+)\n/) { $skip->("socket") }
214 elsif (-S "/dev/$1") { $ok-> ("socket") }
215 else { $fail->("socket", "-S") }
218 ok(! -c $Curdir, '!-c cwd');
219 ok(! -S $Curdir, '!-S cwd');
220 ok(! -b $Curdir, '!-b cwd');
224 skip "No setuid", 3 if $Is_MPE or $Is_Amiga or $Is_Dosish or $Is_Cygwin;
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;
235 opendir BIN, $bin or die "Can't opendir $bin: $!";
236 while (defined($_ = readdir BIN)) {
240 last if $uid && $uid < $cnt;
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") )
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).
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.
263 skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST};
265 my $TTY = $^O eq 'rhapsody' ? "/dev/ttyp0" : "/dev/tty";
268 skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare;
269 skip "No TTY to test -t with", 2 unless -e $TTY;
272 warn "Can't open $TTY--run t/TEST outside of make.\n";
274 ok(-c TTY, 'tty is -c');
277 ok(! -t TTY, '!-t on closed TTY filehandle');
278 ok(-t, '-t on STDIN');
281 my $Null = File::Spec->devnull;
283 skip "No null device to test with", 1 unless -e $Null;
285 open(NULL, $Null) or BAIL_OUT("Can't open $Null: $!");
286 ok(! -t NULL, 'null device is not a TTY');
291 # These aren't strictly "stat" calls, but so what?
293 ok(-T 'op/stat.t', '-T');
294 ok(! -B 'op/stat.t', '!-B');
299 open(FOO,'op/stat.t');
302 skip "-T/B on filehandle not implemented", 15 if $@ =~ /not implemented/;
304 is( $@, '', '-T on filehandle causes no errors' );
307 ok(! -B FOO, ' !-B');
310 ok(/perl/, 'after readline');
311 ok(-T FOO, ' still -T');
312 ok(! -B FOO, ' still -B');
315 open(FOO,'op/stat.t');
317 ok(/perl/, 'reopened and after readline');
318 ok(-T FOO, ' still -T');
319 ok(! -B FOO, ' still !-B');
321 ok(seek(FOO,0,0), 'after seek');
322 ok(-T FOO, ' still -T');
323 ok(! -B FOO, ' still !-B');
325 # It's documented this way in perlfunc *shrug*
327 ok(eof FOO, 'at EOF');
328 ok(-T FOO, ' still -T');
329 ok(-B FOO, ' now -B');
335 skip "No null device to test with", 2 unless -e $Null;
337 ok(-T $Null, 'null device is -T');
338 ok(-B $Null, ' and -B');
342 # and now, a few parsing tests:
344 ok(-f, 'bare -f uses $_');
347 unlink $tmpfile or print "# unlink failed: $!\n";
349 # bug id 20011101.069
351 is(scalar @r, 13, 'stat returns full 13 elements');