5 require './test.pl'; # for which_perl() etc
14 if(eval {require File::Spec; 1}) {
15 $Null = File::Spec->devnull;
16 $Curdir = File::Spec->curdir;
18 die $@ unless is_miniperl();
20 diag("miniperl failed to load File::Spec, error is:\n$@");
21 diag("\ncontinuing, assuming '.' for current directory. Some tests will be skipped.");
24 if ($^O eq 'MSWin32') {
25 # under minitest, buildcustomize sets this to 1, which means
26 # nlinks isn't populated properly, allow nlinks tests to pass
27 ${^WIN32_SLOPPY_STAT} = 0;
32 my $Perl = which_perl();
34 $ENV{LC_ALL} = 'C'; # Forge English error messages.
35 $ENV{LANGUAGE} = 'C'; # Ditto in GNU.
37 my $Is_Amiga = $^O eq 'amigaos';
38 my $Is_Cygwin = $^O eq 'cygwin';
39 my $Is_Darwin = $^O eq 'darwin';
40 my $Is_Dos = $^O eq 'dos';
41 my $Is_MSWin32 = $^O eq 'MSWin32';
42 my $Is_NetWare = $^O eq 'NetWare';
43 my $Is_OS2 = $^O eq 'os2';
44 my $Is_Solaris = $^O eq 'solaris';
45 my $Is_VMS = $^O eq 'VMS';
46 my $Is_MPRAS = $^O =~ /svr4/ && -f '/etc/.relid';
47 my $Is_Android = $^O =~ /android/;
48 my $Is_Dfly = $^O eq 'dragonfly';
50 my $Is_Dosish = $Is_Dos || $Is_OS2 || $Is_MSWin32 || $Is_NetWare;
52 my $ufs_no_ctime = ($Is_Dfly || $Is_Darwin) && (() = `df -t ufs . 2>/dev/null`) == 2;
54 my $Is_linux_container = is_linux_container();
56 if ($Is_Cygwin && !is_miniperl) {
61 my($DEV, $INO, $MODE, $NLINK, $UID, $GID, $RDEV, $SIZE,
62 $ATIME, $MTIME, $CTIME, $BLKSIZE, $BLOCKS) = (0..12);
64 my $tmpfile = tempfile();
65 my $tmpfile_link = tempfile();
69 open(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
72 open(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
74 my($nlink, $mtime, $ctime) = (stat(FOO))[$NLINK, $MTIME, $CTIME];
76 # The clock on a network filesystem might be different from the
78 my $Filesystem_Time_Offset = abs($mtime - time);
80 #nlink should if link support configured in Perl.
82 skip "No link count - Hard link support not built in.", 1
83 unless $Config{d_link};
85 is($nlink, 1, 'nlink on regular file');
89 skip "mtime and ctime not reliable", 2
90 if $Is_MSWin32 or $Is_NetWare or $Is_Cygwin or $Is_Dos or $Is_Darwin;
92 ok( $mtime, 'mtime' );
93 is( $mtime, $ctime, 'mtime == ctime' );
97 # Cygwin seems to have a 3 second granularity on its timestamps.
98 my $funky_FAT_timestamps = $Is_Cygwin;
99 sleep 3 if $funky_FAT_timestamps;
101 print FOO "Now is the time for all good men to come to.\n";
107 my $inaccurate_atime = 0;
108 if (defined &Win32::IsWinNT && Win32::IsWinNT()) {
109 if (Win32::FsType() ne 'NTFS') {
111 $inaccurate_atime = 1;
116 skip "No link on this filesystem", 6 unless $has_link;
117 unlink $tmpfile_link;
118 my $lnk_result = eval { link $tmpfile, $tmpfile_link };
119 skip "link() unimplemented", 6 if $@ =~ /unimplemented/;
121 is( $@, '', 'link() implemented' );
122 ok( $lnk_result, 'linked tmp testfile' );
123 ok( chmod(0644, $tmpfile), 'chmoded tmp testfile' );
125 my($nlink, $mtime, $ctime) = (stat($tmpfile))[$NLINK, $MTIME, $CTIME];
128 skip "No link count", 1 if $Config{dont_use_nlink};
129 skip "Cygwin9X fakes hard links by copying", 1
130 if $Config{myuname} =~ /^cygwin_(?:9\d|me)\b/i;
132 is($nlink, 2, 'Link count on hard linked file' );
136 skip_if_miniperl("File::Spec not built for minitest", 2);
137 my $cwd = File::Spec->rel2abs($Curdir);
138 skip "Solaris tmpfs has different mtime/ctime link semantics", 2
139 if $Is_Solaris and $cwd =~ m#^/tmp# and
140 $mtime && $mtime == $ctime;
141 skip "AFS has different mtime/ctime link semantics", 2
142 if $cwd =~ m#$Config{'afsroot'}/#;
143 skip "AmigaOS has different mtime/ctime link semantics", 2
145 # Win32 could pass $mtime test but as FAT and NTFS have
146 # no ctime concept $ctime is ALWAYS == $mtime
147 # expect netware to be the same ...
148 skip "No ctime concept on this OS", 2
149 if $Is_MSWin32 || $ufs_no_ctime;
150 my $ok_mtime = ok($mtime, 'hard link mtime');
152 # https://bugs.dragonflybsd.org/issues/3251
153 # this might be hammer/hammer2 specific
154 $TODO = "DragonFly BSD doesn't touch ctime on link()/chmod"
155 if $^O eq "dragonfly" && $Config{myuname} =~ /5\.8/;
157 !isnt($mtime, $ctime, 'hard link ctime != mtime') ) {
159 # Check if you are on a tmpfs of some sort. Building in /tmp sometimes
160 # has this problem. Building on the ClearCase VOBS filesystem may also
161 # cause this failure.
163 # Some UFS implementations don't have a ctime concept, and thus are
164 # expected to fail this test.
171 # truncate and touch $tmpfile.
172 open(F, ">$tmpfile") || DIE("Can't open temp test file: $!");
173 ok(-z \*F, '-z on empty filehandle');
174 ok(! -s \*F, ' and -s');
177 ok(-z $tmpfile, '-z on empty file');
178 ok(! -s $tmpfile, ' and -s');
180 open(F, ">$tmpfile") || DIE("Can't open temp test file: $!");
184 open(F, "<$tmpfile") || DIE("Can't open temp test file: $!");
185 ok(!-z *F, '-z on empty filehandle');
186 ok( -s *F, ' and -s');
189 ok(! -z $tmpfile, '-z on non-empty file');
190 ok(-s $tmpfile, ' and -s');
193 # Strip all access rights from the file.
194 ok( chmod(0000, $tmpfile), 'chmod 0000' );
197 skip "-r, -w and -x have different meanings on VMS", 3 if $Is_VMS;
200 # Going to try to switch away from root. Might not work.
203 skip "Can't test -r or -w meaningfully if you're superuser", 2
204 if ($Is_Cygwin ? _ingroup(544, 1) : $> == 0);
207 skip "Can't test -r meaningfully?", 1 if $Is_Dos;
208 ok(!-r $tmpfile, " -r");
211 ok(!-w $tmpfile, " -w");
213 # switch uid back (may not be implemented)
214 eval { $> = $olduid; };
217 ok(! -x $tmpfile, ' -x');
222 ok(chmod(0700,$tmpfile), 'chmod 0700');
223 ok(-r $tmpfile, ' -r');
224 ok(-w $tmpfile, ' -w');
227 skip "-x simply determines if a file ends in an executable suffix", 1
230 ok(-x $tmpfile, ' -x');
233 ok( -f $tmpfile, ' -f');
234 ok(! -d $tmpfile, ' !-d');
237 ok( -d '.', '-d cwd' );
238 ok(! -f '.', '!-f cwd' );
242 unlink($tmpfile_link);
243 my $symlink_rslt = eval { symlink $tmpfile, $tmpfile_link };
244 skip "symlink not implemented", 3 if $@ =~ /unimplemented/;
246 is( $@, '', 'symlink() implemented' );
247 ok( $symlink_rslt, 'symlink() ok' );
248 ok(-l $tmpfile_link, '-l');
251 ok(-o $tmpfile, '-o');
253 ok(-e $tmpfile, '-e');
255 unlink($tmpfile_link);
256 ok(! -e $tmpfile_link, ' -e on unlinked file');
259 skip "No character, socket or block special files", 6
260 if $Is_MSWin32 || $Is_NetWare || $Is_Dos;
261 skip "/dev isn't available to test against", 6
262 unless -d '/dev' && -r '/dev' && -x '/dev';
263 skip "Skipping: unexpected ls output in MP-RAS", 6
266 # VMS problem: If GNV or other UNIX like tool is installed, then
267 # sometimes Perl will find /bin/ls, and will try to run it.
268 # But since Perl on VMS does not know to run it under Bash, it will
269 # try to run the DCL verb LS. And if the VMS product Language
270 # Sensitive Editor is installed, or some other LS verb, that will
271 # be run instead. So do not do this until we can teach Perl
272 # when to use BASH on VMS.
273 skip "ls command not available to Perl in OpenVMS right now.", 6
276 delete $ENV{CLICOLOR_FORCE};
277 my $LS = $Config{d_readlink} && !$Is_Android ? "ls -lL" : "ls -l";
278 my $CMD = "$LS /dev 2>/dev/null";
281 skip "$CMD failed", 6 if $DEV eq '';
283 my @DEV = do { my $dev; opendir($dev, "/dev") ? readdir($dev) : () };
285 skip "opendir failed: $!", 6 if @DEV == 0;
287 # /dev/stdout might be either character special or a named pipe,
288 # or a symlink, or a socket, depending on which OS and how are
289 # you running the test, so let's censor that one away.
290 # Similar remarks hold for stderr.
291 $DEV =~ s{^[cpls].+?\sstdout$}{}m;
292 @DEV = grep { $_ ne 'stdout' } @DEV;
293 $DEV =~ s{^[cpls].+?\sstderr$}{}m;
294 @DEV = grep { $_ ne 'stderr' } @DEV;
296 # /dev/printer is also naughty: in IRIX it shows up as
297 # Srwx-----, not srwx------.
298 $DEV =~ s{^.+?\sprinter$}{}m;
299 @DEV = grep { $_ ne 'printer' } @DEV;
301 # If running as root, we will see .files in the ls result,
302 # and readdir() will see them always. Potential for conflict,
303 # so let's weed them out.
304 $DEV =~ s{^.+?\s\..+?$}{}m;
305 @DEV = grep { ! m{^\..+$} } @DEV;
307 # sometimes files cannot be stat'd on cygwin, making inspecting pointless
308 # remove them from both @DEV and $DEV
309 @DEV = grep $DEV =~ s/^.\?{9}.*\s$_(?: -> .*)?$//m ? () : $_, @DEV
312 # Irix ls -l marks sockets with 'S' while 's' is a 'XENIX semaphore'.
314 $DEV =~ s{^S(.+?)}{s$1}mg;
318 my @c1 = eval qq[\$DEV =~ /^$_[0].*/mg];
319 my @c2 = eval qq[grep { $_[1] "/dev/\$_" } \@DEV];
322 diag "= before", $DEV, "-", @DEV, "= after", @c1, "-", @c2, "="
323 unless is($c1, $c2, "ls and $_[1] agreeing on /dev ($c1 $c2)");
332 ok(! -b $Curdir, '!-b cwd');
333 ok(! -c $Curdir, '!-c cwd');
334 ok(! -S $Curdir, '!-S cwd');
342 # Find a set of directories that's very likely to have setuid files
343 # but not likely to be *all* setuid files.
344 my @bin = grep {-d && -r && -x} qw(/sbin /usr/sbin /bin /usr/bin);
345 skip "Can't find a setuid file to test with", 3 unless @bin;
348 opendir BIN, $bin or die "Can't opendir $bin: $!";
349 while (defined($_ = readdir BIN)) {
353 last if $uid && $uid < $cnt;
358 skip "No setuid programs", 3 if $uid == 0;
360 isnt($cnt, 0, 'found some programs');
361 isnt($uid, 0, ' found some setuid programs');
362 ok($uid < $cnt, " they're not all setuid");
366 # To assist in automated testing when a controlling terminal (/dev/tty)
367 # may not be available (at, cron rsh etc), the PERL_SKIP_TTY_TEST env var
368 # can be set to skip the tests that need a tty.
370 skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST};
371 skip "Skipping TTY tests on linux containers", 4 if $Is_linux_container;
373 my $TTY = "/dev/tty";
376 skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare;
377 skip "No TTY to test -t with", 2 unless -e $TTY;
380 warn "Can't open $TTY--run t/TEST outside of make.\n";
382 ok(-c TTY, 'tty is -c');
385 ok(! -t TTY, '!-t on closed TTY filehandle');
388 local our $TODO = 'STDIN not a tty when output is to pipe' if $Is_VMS;
389 ok(-t, '-t on STDIN');
394 skip "No null device to test with", 1 unless -e $Null;
395 skip "We know Win32 thinks '$Null' is a TTY", 1 if $Is_MSWin32;
397 open(NULL, $Null) or DIE("Can't open $Null: $!");
398 ok(! -t NULL, 'null device is not a TTY');
403 # These aren't strictly "stat" calls, but so what?
404 my $statfile = './op/stat.t';
405 ok( -T $statfile, '-T');
406 ok(! -B $statfile, '!-B');
408 ok(! -T $Perl, '!-T');
413 skip "-T/B on filehandle not implemented", 15 if $@ =~ /not implemented/;
415 is( $@, '', '-T on filehandle causes no errors' );
418 ok(! -B FOO, ' !-B');
421 like($_, qr/perl/, 'after readline');
422 ok(-T FOO, ' still -T');
423 ok(! -B FOO, ' still -B');
428 like($_, qr/perl/, 'reopened and after readline');
429 ok(-T FOO, ' still -T');
430 ok(! -B FOO, ' still !-B');
432 ok(seek(FOO,0,0), 'after seek');
433 ok(-T FOO, ' still -T');
434 ok(! -B FOO, ' still !-B');
436 # It's documented this way in perlfunc *shrug*
438 ok(eof FOO, 'at EOF');
439 ok(-T FOO, ' still -T');
440 ok(-B FOO, ' now -B');
446 skip "No null device to test with", 2 unless -e $Null;
448 ok(-T $Null, 'null device is -T');
449 ok(-B $Null, ' and -B');
453 # and now, a few parsing tests:
455 ok(-f, 'bare -f uses $_');
458 unlink $tmpfile or print "# unlink failed: $!\n";
460 # bug id 20011101.069 (#7861)
461 my @r = \stat($Curdir);
462 is(scalar @r, 13, 'stat returns full 13 elements');
466 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
467 'lstat _ croaks after stat' );
469 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
470 'lstat *_ croaks after stat' );
472 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
473 'lstat \*_ croaks after stat' );
475 like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
476 '-l _ croaks after stat' );
480 is( "$@", "", "lstat _ ok after lstat" );
482 is( "$@", "", "-l _ ok after lstat" );
484 eval { lstat "test.pl" };
486 open my $fh, "test.pl";
490 like $@, qr/^The stat preceding lstat\(\) wasn't an lstat at /,
491 'stat $ioref resets stat type';
494 open(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
495 my @statbuf = stat FOO;
498 my @lstatbuf = lstat *FOO{IO};
499 is "@lstatbuf", "@statbuf", 'lstat $ioref reverts to regular fstat';
501 unlink $tmpfile or print "# unlink failed: $!\n";
505 skip "No lstat", 2 unless $Config{d_lstat} && $Config{d_symlink};
507 # bug id 20020124.004 (#8334)
508 my $linkname = 'stat-' . rand =~ y/.//dr;
510 $target =~ s/;\d+\z// if $Is_VMS; # symlinks don't like version numbers
511 unless (symlink $target, $linkname) {
512 if ($^O eq "MSWin32") {
513 # likely we don't have permission
514 skip "symlink failed: $!", 2;
516 die "# Can't symlink $0: $!";
521 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
522 'lstat croaks after -T _' );
524 like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
525 '-l _ croaks after -T _' );
526 unlink $linkname or print "# unlink $linkname failed: $!\n";
530 skip "Too much clock skew between system and filesystem", 5
531 if ($Filesystem_Time_Offset > 5);
533 sleep($Filesystem_Time_Offset+1);
534 my $f = 'tstamp.tmp';
536 ok (open(S, "> $f"), 'can create tmp file');
539 print "# time=$^T, stat=(@a)\n";
540 my @b = (-M _, -A _, -C _);
541 print "# -MAC=(@b)\n";
542 ok( (-M _) < 0, 'negative -M works');
545 skip "Access timestamps inaccurate", 1 if $inaccurate_atime;
546 ok( (-A _) < 0, 'negative -A works');
548 ok( (-C _) < 0, 'negative -C works');
549 ok(unlink($f), 'unlink tmp file');
554 ok(open(F, ">", $tmpfile), 'can create temp file');
556 chmod 0077, $tmpfile;
557 my @a = stat($tmpfile);
561 is($s1, $s2, q(-T _ doesn't break the statbuffer));
563 my $root_uid = $Is_Cygwin ? 18 : 0;
564 skip "No lstat", 1 unless $Config{d_lstat};
565 skip "uid=0", 1 if $< == $root_uid or $> == $root_uid;
566 skip "Can't check if admin user in miniperl", 1
567 if $^O =~ /^(cygwin|MSWin32|msys)$/ && is_miniperl();
568 skip "Readable by group/other means readable by me on $^O", 1 if $^O eq 'VMS'
569 or ($^O =~ /^(cygwin|MSWin32|msys)$/ and Win32::IsAdminUser());
573 q(-T _ doesn't break lstat for unreadable file));
579 skip "No dirfd()", 4 unless $Config{d_dirfd} || $Config{d_dir_dd_fd};
580 ok(opendir(DIR, "."), 'Can open "." dir') || diag "Can't open '.': $!";
581 ok(stat(DIR), "stat() on dirhandle works");
582 ok(-d -r _ , "chained -x's on dirhandle");
583 ok(-d DIR, "-d on a dirhandle works");
584 closedir DIR or die $!;
588 # RT #8244: *FILE{IO} does not behave like *FILE for stat() and -X() operators
589 ok(open(F, ">", $tmpfile), 'can create temp file');
590 my @thwap = stat *F{IO};
591 ok(@thwap, "stat(*F{IO}) works");
592 ok( -f *F{IO} , "single file tests work with *F{IO}");
596 #PVIO's hold dirhandle information, so let's test them too.
599 skip "No dirfd()", 4 unless $Config{d_dirfd} || $Config{d_dir_dd_fd};
600 ok(opendir(DIR, "."), 'Can open "." dir') || diag "Can't open '.': $!";
601 ok(stat(*DIR{IO}), "stat() on *DIR{IO} works");
602 ok(-d _ , "The special file handle _ is set correctly");
603 ok(-d -r *DIR{IO} , "chained -x's on *DIR{IO}");
604 closedir DIR or die $!;
611 local $SIG{__WARN__} = sub { warn shift; ++$w };
612 stat 'prepeinamehyparcheiarcheiometoonomaavto';
614 is $w, undef, 'no unopened warning from stat _';
619 # Inappropriate stacking of l?stat with filetests should either work or
620 # give a syntax error, they shouldn't crash.
622 ok(1, 'can "stat -t" without crashing');
624 ok(1, 'can "lstat -t" without crashing');
627 # [perl #126064] stat stat stack busting
628 is join("-", 1,2,3,(stat stat stat),4,5,6), "1-2-3-4-5-6",
629 'stat inside stat gets scalar context';
631 # [perl #126162] stat an array should not work
635 skip "There is a file named '2', which invalidates this test", 2 if -e '2';
637 my $Errno_loaded = eval { require Errno };
638 my @statarg = ($statfile, $statfile);
639 no warnings 'syntax';
641 'stat on an array of valid paths should warn and should not return any data';
643 skip "Errno not available", 1
644 unless $Errno_loaded;
645 is $error, &Errno::ENOENT,
646 'stat on an array of valid paths should return ENOENT';
649 # [perl #131895] stat() doesn't fail on filenames containing \0 / NUL
651 no warnings 'syscalls';
652 ok !stat("TEST\0-"), 'stat on filename with \0';
655 my $link = "stat_t_$$\_TEST.symlink";
656 my $can_symlink = eval { symlink "TEST", $link };
657 skip "cannot symlink", 1 unless $can_symlink;
658 no warnings 'syscalls';
659 ok !lstat("$link\0-"), 'lstat on filename with \0';
664 chmod 0666, $tmpfile;
669 my ($gid, $eff) = @_;
671 $^O eq "VMS" and return $_[0] == $);
673 my ($egid, @supp) = split " ", $);
674 my ($rgid) = split " ", $(;
676 $gid == ($eff ? $egid : $rgid) and return 1;
677 grep $gid == $_, @supp and return 1;