6 require './test.pl'; # for which_perl() etc
12 if(eval {require File::Spec; 1}) {
13 $Null = File::Spec->devnull;
14 $Curdir = File::Spec->curdir;
16 die $@ unless is_miniperl();
18 diag("miniperl failed to load File::Spec, error is:\n$@");
19 diag("\ncontinuing, assuming '.' for current directory. Some tests will be skipped.");
22 if ($^O eq 'MSWin32') {
23 # under minitest, buildcustomize sets this to 1, which means
24 # nlinks isn't populated properly, allow nlinks tests to pass
25 ${^WIN32_SLOPPY_STAT} = 0;
30 my $Perl = which_perl();
32 $ENV{LC_ALL} = 'C'; # Forge English error messages.
33 $ENV{LANGUAGE} = 'C'; # Ditto in GNU.
35 $Is_Amiga = $^O eq 'amigaos';
36 $Is_Cygwin = $^O eq 'cygwin';
37 $Is_Darwin = $^O eq 'darwin';
38 $Is_Dos = $^O eq 'dos';
39 $Is_MSWin32 = $^O eq 'MSWin32';
40 $Is_NetWare = $^O eq 'NetWare';
41 $Is_OS2 = $^O eq 'os2';
42 $Is_Solaris = $^O eq 'solaris';
43 $Is_VMS = $^O eq 'VMS';
44 $Is_MPRAS = $^O =~ /svr4/ && -f '/etc/.relid';
45 $Is_Android = $^O =~ /android/;
47 $Is_Dosish = $Is_Dos || $Is_OS2 || $Is_MSWin32 || $Is_NetWare;
49 $Is_UFS = $Is_Darwin && (() = `df -t ufs . 2>/dev/null`) == 2;
51 if ($Is_Cygwin && !is_miniperl) {
56 my($DEV, $INO, $MODE, $NLINK, $UID, $GID, $RDEV, $SIZE,
57 $ATIME, $MTIME, $CTIME, $BLKSIZE, $BLOCKS) = (0..12);
59 my $tmpfile = tempfile();
60 my $tmpfile_link = tempfile();
64 open(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
67 open(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
69 my($nlink, $mtime, $ctime) = (stat(FOO))[$NLINK, $MTIME, $CTIME];
71 # The clock on a network filesystem might be different from the
73 my $Filesystem_Time_Offset = abs($mtime - time);
75 #nlink should if link support configured in Perl.
77 skip "No link count - Hard link support not built in.", 1
78 unless $Config{d_link};
80 is($nlink, 1, 'nlink on regular file');
84 skip "mtime and ctime not reliable", 2
85 if $Is_MSWin32 or $Is_NetWare or $Is_Cygwin or $Is_Dos or $Is_Darwin;
87 ok( $mtime, 'mtime' );
88 is( $mtime, $ctime, 'mtime == ctime' );
92 # Cygwin seems to have a 3 second granularity on its timestamps.
93 my $funky_FAT_timestamps = $Is_Cygwin;
94 sleep 3 if $funky_FAT_timestamps;
96 print FOO "Now is the time for all good men to come to.\n";
102 my $inaccurate_atime = 0;
103 if (defined &Win32::IsWinNT && Win32::IsWinNT()) {
104 if (Win32::FsType() ne 'NTFS') {
106 $inaccurate_atime = 1;
111 skip "No link on this filesystem", 6 unless $has_link;
112 unlink $tmpfile_link;
113 my $lnk_result = eval { link $tmpfile, $tmpfile_link };
114 skip "link() unimplemented", 6 if $@ =~ /unimplemented/;
116 is( $@, '', 'link() implemented' );
117 ok( $lnk_result, 'linked tmp testfile' );
118 ok( chmod(0644, $tmpfile), 'chmoded tmp testfile' );
120 my($nlink, $mtime, $ctime) = (stat($tmpfile))[$NLINK, $MTIME, $CTIME];
123 skip "No link count", 1 if $Config{dont_use_nlink};
124 skip "Cygwin9X fakes hard links by copying", 1
125 if $Config{myuname} =~ /^cygwin_(?:9\d|me)\b/i;
127 is($nlink, 2, 'Link count on hard linked file' );
131 skip_if_miniperl("File::Spec not built for minitest", 2);
132 my $cwd = File::Spec->rel2abs($Curdir);
133 skip "Solaris tmpfs has different mtime/ctime link semantics", 2
134 if $Is_Solaris and $cwd =~ m#^/tmp# and
135 $mtime && $mtime == $ctime;
136 skip "AFS has different mtime/ctime link semantics", 2
137 if $cwd =~ m#$Config{'afsroot'}/#;
138 skip "AmigaOS has different mtime/ctime link semantics", 2
140 # Win32 could pass $mtime test but as FAT and NTFS have
141 # no ctime concept $ctime is ALWAYS == $mtime
142 # expect netware to be the same ...
143 skip "No ctime concept on this OS", 2
145 ($Is_Darwin && $Is_UFS);
147 if( !ok($mtime, 'hard link mtime') ||
148 !isnt($mtime, $ctime, 'hard link ctime != mtime') ) {
150 # Check if you are on a tmpfs of some sort. Building in /tmp sometimes
151 # has this problem. Building on the ClearCase VOBS filesystem may also
152 # cause this failure.
154 # Darwin's UFS doesn't have a ctime concept, and thus is expected to fail
162 # truncate and touch $tmpfile.
163 open(F, ">$tmpfile") || DIE("Can't open temp test file: $!");
164 ok(-z \*F, '-z on empty filehandle');
165 ok(! -s \*F, ' and -s');
168 ok(-z $tmpfile, '-z on empty file');
169 ok(! -s $tmpfile, ' and -s');
171 open(F, ">$tmpfile") || DIE("Can't open temp test file: $!");
175 open(F, "<$tmpfile") || DIE("Can't open temp test file: $!");
176 ok(!-z *F, '-z on empty filehandle');
177 ok( -s *F, ' and -s');
180 ok(! -z $tmpfile, '-z on non-empty file');
181 ok(-s $tmpfile, ' and -s');
184 # Strip all access rights from the file.
185 ok( chmod(0000, $tmpfile), 'chmod 0000' );
188 skip "-r, -w and -x have different meanings on VMS", 3 if $Is_VMS;
191 # Going to try to switch away from root. Might not work.
194 skip "Can't test if an admin user in miniperl", 2,
195 if $Is_Cygwin && is_miniperl();
196 skip "Can't test -r or -w meaningfully if you're superuser", 2
200 skip "Can't test -r meaningfully?", 1 if $Is_Dos;
201 ok(!-r $tmpfile, " -r");
204 ok(!-w $tmpfile, " -w");
206 # switch uid back (may not be implemented)
207 eval { $> = $olduid; };
210 ok(! -x $tmpfile, ' -x');
215 ok(chmod(0700,$tmpfile), 'chmod 0700');
216 ok(-r $tmpfile, ' -r');
217 ok(-w $tmpfile, ' -w');
220 skip "-x simply determines if a file ends in an executable suffix", 1
223 ok(-x $tmpfile, ' -x');
226 ok( -f $tmpfile, ' -f');
227 ok(! -d $tmpfile, ' !-d');
230 ok( -d '.', '-d cwd' );
231 ok(! -f '.', '!-f cwd' );
235 unlink($tmpfile_link);
236 my $symlink_rslt = eval { symlink $tmpfile, $tmpfile_link };
237 skip "symlink not implemented", 3 if $@ =~ /unimplemented/;
239 is( $@, '', 'symlink() implemented' );
240 ok( $symlink_rslt, 'symlink() ok' );
241 ok(-l $tmpfile_link, '-l');
244 ok(-o $tmpfile, '-o');
246 ok(-e $tmpfile, '-e');
248 unlink($tmpfile_link);
249 ok(! -e $tmpfile_link, ' -e on unlinked file');
252 skip "No character, socket or block special files", 6
253 if $Is_MSWin32 || $Is_NetWare || $Is_Dos;
254 skip "/dev isn't available to test against", 6
255 unless -d '/dev' && -r '/dev' && -x '/dev';
256 skip "Skipping: unexpected ls output in MP-RAS", 6
259 # VMS problem: If GNV or other UNIX like tool is installed, then
260 # sometimes Perl will find /bin/ls, and will try to run it.
261 # But since Perl on VMS does not know to run it under Bash, it will
262 # try to run the DCL verb LS. And if the VMS product Language
263 # Sensitive Editor is installed, or some other LS verb, that will
264 # be run instead. So do not do this until we can teach Perl
265 # when to use BASH on VMS.
266 skip "ls command not available to Perl in OpenVMS right now.", 6
269 delete $ENV{CLICOLOR_FORCE};
270 my $LS = $Config{d_readlink} && !$Is_Android ? "ls -lL" : "ls -l";
271 my $CMD = "$LS /dev 2>/dev/null";
274 skip "$CMD failed", 6 if $DEV eq '';
276 my @DEV = do { my $dev; opendir($dev, "/dev") ? readdir($dev) : () };
278 skip "opendir failed: $!", 6 if @DEV == 0;
280 # /dev/stdout might be either character special or a named pipe,
281 # or a symlink, or a socket, depending on which OS and how are
282 # you running the test, so let's censor that one away.
283 # Similar remarks hold for stderr.
284 $DEV =~ s{^[cpls].+?\sstdout$}{}m;
285 @DEV = grep { $_ ne 'stdout' } @DEV;
286 $DEV =~ s{^[cpls].+?\sstderr$}{}m;
287 @DEV = grep { $_ ne 'stderr' } @DEV;
289 # /dev/printer is also naughty: in IRIX it shows up as
290 # Srwx-----, not srwx------.
291 $DEV =~ s{^.+?\sprinter$}{}m;
292 @DEV = grep { $_ ne 'printer' } @DEV;
294 # If running as root, we will see .files in the ls result,
295 # and readdir() will see them always. Potential for conflict,
296 # so let's weed them out.
297 $DEV =~ s{^.+?\s\..+?$}{}m;
298 @DEV = grep { ! m{^\..+$} } @DEV;
300 # Irix ls -l marks sockets with 'S' while 's' is a 'XENIX semaphore'.
302 $DEV =~ s{^S(.+?)}{s$1}mg;
306 my @c1 = eval qq[\$DEV =~ /^$_[0].*/mg];
307 my @c2 = eval qq[grep { $_[1] "/dev/\$_" } \@DEV];
310 is($c1, $c2, "ls and $_[1] agreeing on /dev ($c1 $c2)");
319 ok(! -b $Curdir, '!-b cwd');
320 ok(! -c $Curdir, '!-c cwd');
321 ok(! -S $Curdir, '!-S cwd');
329 # Find a set of directories that's very likely to have setuid files
330 # but not likely to be *all* setuid files.
331 my @bin = grep {-d && -r && -x} qw(/sbin /usr/sbin /bin /usr/bin);
332 skip "Can't find a setuid file to test with", 3 unless @bin;
335 opendir BIN, $bin or die "Can't opendir $bin: $!";
336 while (defined($_ = readdir BIN)) {
340 last if $uid && $uid < $cnt;
345 skip "No setuid programs", 3 if $uid == 0;
347 isnt($cnt, 0, 'found some programs');
348 isnt($uid, 0, ' found some setuid programs');
349 ok($uid < $cnt, " they're not all setuid");
353 # To assist in automated testing when a controlling terminal (/dev/tty)
354 # may not be available (at, cron rsh etc), the PERL_SKIP_TTY_TEST env var
355 # can be set to skip the tests that need a tty.
357 skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST};
359 my $TTY = "/dev/tty";
362 skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare;
363 skip "No TTY to test -t with", 2 unless -e $TTY;
366 warn "Can't open $TTY--run t/TEST outside of make.\n";
368 ok(-c TTY, 'tty is -c');
371 ok(! -t TTY, '!-t on closed TTY filehandle');
374 local $TODO = 'STDIN not a tty when output is to pipe' if $Is_VMS;
375 ok(-t, '-t on STDIN');
380 skip "No null device to test with", 1 unless -e $Null;
381 skip "We know Win32 thinks '$Null' is a TTY", 1 if $Is_MSWin32;
383 open(NULL, $Null) or DIE("Can't open $Null: $!");
384 ok(! -t NULL, 'null device is not a TTY');
389 # These aren't strictly "stat" calls, but so what?
390 my $statfile = './op/stat.t';
391 ok( -T $statfile, '-T');
392 ok(! -B $statfile, '!-B');
394 ok(! -T $Perl, '!-T');
399 skip "-T/B on filehandle not implemented", 15 if $@ =~ /not implemented/;
401 is( $@, '', '-T on filehandle causes no errors' );
404 ok(! -B FOO, ' !-B');
407 like($_, qr/perl/, 'after readline');
408 ok(-T FOO, ' still -T');
409 ok(! -B FOO, ' still -B');
414 like($_, qr/perl/, 'reopened and after readline');
415 ok(-T FOO, ' still -T');
416 ok(! -B FOO, ' still !-B');
418 ok(seek(FOO,0,0), 'after seek');
419 ok(-T FOO, ' still -T');
420 ok(! -B FOO, ' still !-B');
422 # It's documented this way in perlfunc *shrug*
424 ok(eof FOO, 'at EOF');
425 ok(-T FOO, ' still -T');
426 ok(-B FOO, ' now -B');
432 skip "No null device to test with", 2 unless -e $Null;
434 ok(-T $Null, 'null device is -T');
435 ok(-B $Null, ' and -B');
439 # and now, a few parsing tests:
441 ok(-f, 'bare -f uses $_');
444 unlink $tmpfile or print "# unlink failed: $!\n";
446 # bug id 20011101.069
447 my @r = \stat($Curdir);
448 is(scalar @r, 13, 'stat returns full 13 elements');
452 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
453 'lstat _ croaks after stat' );
455 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
456 'lstat *_ croaks after stat' );
458 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
459 'lstat \*_ croaks after stat' );
461 like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
462 '-l _ croaks after stat' );
466 is( "$@", "", "lstat _ ok after lstat" );
468 is( "$@", "", "-l _ ok after lstat" );
470 eval { lstat "test.pl" };
472 open my $fh, "test.pl";
476 like $@, qr/^The stat preceding lstat\(\) wasn't an lstat at /,
477 'stat $ioref resets stat type';
480 open(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
481 my @statbuf = stat FOO;
483 my @lstatbuf = lstat *FOO{IO};
484 is "@lstatbuf", "@statbuf", 'lstat $ioref reverts to regular fstat';
486 unlink $tmpfile or print "# unlink failed: $!\n";
490 skip "No lstat", 2 unless $Config{d_lstat};
492 # bug id 20020124.004
493 # If we have d_lstat, we should have symlink()
494 my $linkname = 'stat-' . rand =~ y/.//dr;
496 $target =~ s/;\d+\z// if $Is_VMS; # symlinks don't like version numbers
497 symlink $target, $linkname or die "# Can't symlink $0: $!";
501 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
502 'lstat croaks after -T _' );
504 like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
505 '-l _ croaks after -T _' );
506 unlink $linkname or print "# unlink $linkname failed: $!\n";
510 skip "Too much clock skew between system and filesystem", 5
511 if ($Filesystem_Time_Offset > 5);
513 sleep($Filesystem_Time_Offset+1);
514 my $f = 'tstamp.tmp';
516 ok (open(S, "> $f"), 'can create tmp file');
519 print "# time=$^T, stat=(@a)\n";
520 my @b = (-M _, -A _, -C _);
521 print "# -MAC=(@b)\n";
522 ok( (-M _) < 0, 'negative -M works');
525 skip "Access timestamps inaccurate", 1 if $inaccurate_atime;
526 ok( (-A _) < 0, 'negative -A works');
528 ok( (-C _) < 0, 'negative -C works');
529 ok(unlink($f), 'unlink tmp file');
534 ok(open(F, ">", $tmpfile), 'can create temp file');
536 chmod 0077, $tmpfile;
537 my @a = stat($tmpfile);
541 is($s1, $s2, q(-T _ doesn't break the statbuffer));
543 my $root_uid = $Is_Cygwin ? 18 : 0;
544 skip "No lstat", 1 unless $Config{d_lstat};
545 skip "uid=0", 1 if $< == $root_uid or $> == $root_uid;
546 skip "Can't check if admin user in miniperl", 1
547 if $^O =~ /^(cygwin|MSWin32|msys)$/ && is_miniperl();
548 skip "Readable by group/other means readable by me on $^O", 1 if $^O eq 'VMS'
549 or ($^O =~ /^(cygwin|MSWin32|msys)$/ and Win32::IsAdminUser());
553 q(-T _ doesn't break lstat for unreadable file));
559 skip "No dirfd()", 9 unless $Config{d_dirfd} || $Config{d_dir_dd_fd};
560 ok(opendir(DIR, "."), 'Can open "." dir') || diag "Can't open '.': $!";
561 ok(stat(DIR), "stat() on dirhandle works");
562 ok(-d -r _ , "chained -x's on dirhandle");
563 ok(-d DIR, "-d on a dirhandle works");
565 # And now for the ambiguous bareword case
567 no warnings 'deprecated';
568 ok(open(DIR, "TEST"), 'Can open "TEST" dir')
569 || diag "Can't open 'TEST': $!";
571 my $size = (stat(DIR))[7];
572 ok(defined $size, "stat() on bareword works");
573 is($size, -s "TEST", "size returned by stat of bareword is for the file");
574 ok(-f _, "ambiguous bareword uses file handle, not dir handle");
576 closedir DIR or die $!;
581 # RT #8244: *FILE{IO} does not behave like *FILE for stat() and -X() operators
582 ok(open(F, ">", $tmpfile), 'can create temp file');
583 my @thwap = stat *F{IO};
584 ok(@thwap, "stat(*F{IO}) works");
585 ok( -f *F{IO} , "single file tests work with *F{IO}");
589 #PVIO's hold dirhandle information, so let's test them too.
592 skip "No dirfd()", 9 unless $Config{d_dirfd} || $Config{d_dir_dd_fd};
593 ok(opendir(DIR, "."), 'Can open "." dir') || diag "Can't open '.': $!";
594 ok(stat(*DIR{IO}), "stat() on *DIR{IO} works");
595 ok(-d _ , "The special file handle _ is set correctly");
596 ok(-d -r *DIR{IO} , "chained -x's on *DIR{IO}");
598 # And now for the ambiguous bareword case
600 no warnings 'deprecated';
601 ok(open(DIR, "TEST"), 'Can open "TEST" dir')
602 || diag "Can't open 'TEST': $!";
604 my $size = (stat(*DIR{IO}))[7];
605 ok(defined $size, "stat() on *THINGY{IO} works");
607 "size returned by stat of *THINGY{IO} is for the file");
608 ok(-f _, "ambiguous *THINGY{IO} uses file handle, not dir handle");
610 closedir DIR or die $!;
619 local $SIG{__WARN__} = sub { warn shift; ++$w };
620 stat 'prepeinamehyparcheiarcheiometoonomaavto';
622 is $w, undef, 'no unopened warning from stat _';
627 # Inappropriate stacking of l?stat with filetests should either work or
628 # give a syntax error, they shouldn't crash.
630 ok(1, 'can "stat -t" without crashing');
632 ok(1, 'can "lstat -t" without crashing');
635 # [perl #126064] stat stat stack busting
636 is join("-", 1,2,3,(stat stat stat),4,5,6), "1-2-3-4-5-6",
637 'stat inside stat gets scalar context';
639 # [perl #126162] stat an array should not work
640 my $Errno_loaded = eval { require Errno };
641 my $statfile = './op/stat.t';
642 my @statarg = ($statfile, $statfile);
644 'stat on an array of valid paths should warn and should not return any data';
648 skip "Errno not available", 1
649 unless $Errno_loaded;
650 is $error, &Errno::ENOENT,
651 'stat on an array of valid paths should return ENOENT';
655 chmod 0666, $tmpfile;