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.");
25 my $Perl = which_perl();
27 $ENV{LC_ALL} = 'C'; # Forge English error messages.
28 $ENV{LANGUAGE} = 'C'; # Ditto in GNU.
30 $Is_Amiga = $^O eq 'amigaos';
31 $Is_Cygwin = $^O eq 'cygwin';
32 $Is_Darwin = $^O eq 'darwin';
33 $Is_Dos = $^O eq 'dos';
34 $Is_MSWin32 = $^O eq 'MSWin32';
35 $Is_NetWare = $^O eq 'NetWare';
36 $Is_OS2 = $^O eq 'os2';
37 $Is_Solaris = $^O eq 'solaris';
38 $Is_VMS = $^O eq 'VMS';
39 $Is_MPRAS = $^O =~ /svr4/ && -f '/etc/.relid';
40 $Is_Android = $^O =~ /android/;
42 $Is_Dosish = $Is_Dos || $Is_OS2 || $Is_MSWin32 || $Is_NetWare;
44 $Is_UFS = $Is_Darwin && (() = `df -t ufs . 2>/dev/null`) == 2;
51 my($DEV, $INO, $MODE, $NLINK, $UID, $GID, $RDEV, $SIZE,
52 $ATIME, $MTIME, $CTIME, $BLKSIZE, $BLOCKS) = (0..12);
54 my $tmpfile = tempfile();
55 my $tmpfile_link = tempfile();
59 open(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
62 open(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
64 my($nlink, $mtime, $ctime) = (stat(FOO))[$NLINK, $MTIME, $CTIME];
66 # The clock on a network filesystem might be different from the
68 my $Filesystem_Time_Offset = abs($mtime - time);
70 #nlink should if link support configured in Perl.
72 skip "No link count - Hard link support not built in.", 1
73 unless $Config{d_link};
75 is($nlink, 1, 'nlink on regular file');
79 skip "mtime and ctime not reliable", 2
80 if $Is_MSWin32 or $Is_NetWare or $Is_Cygwin or $Is_Dos or $Is_Darwin;
82 ok( $mtime, 'mtime' );
83 is( $mtime, $ctime, 'mtime == ctime' );
87 # Cygwin seems to have a 3 second granularity on its timestamps.
88 my $funky_FAT_timestamps = $Is_Cygwin;
89 sleep 3 if $funky_FAT_timestamps;
91 print FOO "Now is the time for all good men to come to.\n";
97 my $inaccurate_atime = 0;
98 if (defined &Win32::IsWinNT && Win32::IsWinNT()) {
99 if (Win32::FsType() ne 'NTFS') {
101 $inaccurate_atime = 1;
106 skip "No link on this filesystem", 6 unless $has_link;
107 unlink $tmpfile_link;
108 my $lnk_result = eval { link $tmpfile, $tmpfile_link };
109 skip "link() unimplemented", 6 if $@ =~ /unimplemented/;
111 is( $@, '', 'link() implemented' );
112 ok( $lnk_result, 'linked tmp testfile' );
113 ok( chmod(0644, $tmpfile), 'chmoded tmp testfile' );
115 my($nlink, $mtime, $ctime) = (stat($tmpfile))[$NLINK, $MTIME, $CTIME];
118 skip "No link count", 1 if $Config{dont_use_nlink};
119 skip "Cygwin9X fakes hard links by copying", 1
120 if $Config{myuname} =~ /^cygwin_(?:9\d|me)\b/i;
122 is($nlink, 2, 'Link count on hard linked file' );
126 skip_if_miniperl("File::Spec not built for minitest", 2);
127 my $cwd = File::Spec->rel2abs($Curdir);
128 skip "Solaris tmpfs has different mtime/ctime link semantics", 2
129 if $Is_Solaris and $cwd =~ m#^/tmp# and
130 $mtime && $mtime == $ctime;
131 skip "AFS has different mtime/ctime link semantics", 2
132 if $cwd =~ m#$Config{'afsroot'}/#;
133 skip "AmigaOS has different mtime/ctime link semantics", 2
135 # Win32 could pass $mtime test but as FAT and NTFS have
136 # no ctime concept $ctime is ALWAYS == $mtime
137 # expect netware to be the same ...
138 skip "No ctime concept on this OS", 2
140 ($Is_Darwin && $Is_UFS);
142 if( !ok($mtime, 'hard link mtime') ||
143 !isnt($mtime, $ctime, 'hard link ctime != mtime') ) {
145 # Check if you are on a tmpfs of some sort. Building in /tmp sometimes
146 # has this problem. Building on the ClearCase VOBS filesystem may also
147 # cause this failure.
149 # Darwin's UFS doesn't have a ctime concept, and thus is expected to fail
157 # truncate and touch $tmpfile.
158 open(F, ">$tmpfile") || DIE("Can't open temp test file: $!");
159 ok(-z \*F, '-z on empty filehandle');
160 ok(! -s \*F, ' and -s');
163 ok(-z $tmpfile, '-z on empty file');
164 ok(! -s $tmpfile, ' and -s');
166 open(F, ">$tmpfile") || DIE("Can't open temp test file: $!");
170 open(F, "<$tmpfile") || DIE("Can't open temp test file: $!");
171 ok(!-z *F, '-z on empty filehandle');
172 ok( -s *F, ' and -s');
175 ok(! -z $tmpfile, '-z on non-empty file');
176 ok(-s $tmpfile, ' and -s');
179 # Strip all access rights from the file.
180 ok( chmod(0000, $tmpfile), 'chmod 0000' );
183 skip "-r, -w and -x have different meanings on VMS", 3 if $Is_VMS;
186 # Going to try to switch away from root. Might not work.
189 skip "Can't test -r or -w meaningfully if you're superuser", 2
190 if ($Is_Cygwin ? Win32::IsAdminUser : $> == 0);
193 skip "Can't test -r meaningfully?", 1 if $Is_Dos;
194 ok(!-r $tmpfile, " -r");
197 ok(!-w $tmpfile, " -w");
199 # switch uid back (may not be implemented)
200 eval { $> = $olduid; };
203 ok(! -x $tmpfile, ' -x');
208 ok(chmod(0700,$tmpfile), 'chmod 0700');
209 ok(-r $tmpfile, ' -r');
210 ok(-w $tmpfile, ' -w');
213 skip "-x simply determines if a file ends in an executable suffix", 1
216 ok(-x $tmpfile, ' -x');
219 ok( -f $tmpfile, ' -f');
220 ok(! -d $tmpfile, ' !-d');
223 ok( -d '.', '-d cwd' );
224 ok(! -f '.', '!-f cwd' );
228 unlink($tmpfile_link);
229 my $symlink_rslt = eval { symlink $tmpfile, $tmpfile_link };
230 skip "symlink not implemented", 3 if $@ =~ /unimplemented/;
232 is( $@, '', 'symlink() implemented' );
233 ok( $symlink_rslt, 'symlink() ok' );
234 ok(-l $tmpfile_link, '-l');
237 ok(-o $tmpfile, '-o');
239 ok(-e $tmpfile, '-e');
241 unlink($tmpfile_link);
242 ok(! -e $tmpfile_link, ' -e on unlinked file');
245 skip "No character, socket or block special files", 6
246 if $Is_MSWin32 || $Is_NetWare || $Is_Dos;
247 skip "/dev isn't available to test against", 6
248 unless -d '/dev' && -r '/dev' && -x '/dev';
249 skip "Skipping: unexpected ls output in MP-RAS", 6
252 # VMS problem: If GNV or other UNIX like tool is installed, then
253 # sometimes Perl will find /bin/ls, and will try to run it.
254 # But since Perl on VMS does not know to run it under Bash, it will
255 # try to run the DCL verb LS. And if the VMS product Language
256 # Sensitive Editor is installed, or some other LS verb, that will
257 # be run instead. So do not do this until we can teach Perl
258 # when to use BASH on VMS.
259 skip "ls command not available to Perl in OpenVMS right now.", 6
262 delete $ENV{CLICOLOR_FORCE};
263 my $LS = $Config{d_readlink} && !$Is_Android ? "ls -lL" : "ls -l";
264 my $CMD = "$LS /dev 2>/dev/null";
267 skip "$CMD failed", 6 if $DEV eq '';
269 my @DEV = do { my $dev; opendir($dev, "/dev") ? readdir($dev) : () };
271 skip "opendir failed: $!", 6 if @DEV == 0;
273 # /dev/stdout might be either character special or a named pipe,
274 # or a symlink, or a socket, depending on which OS and how are
275 # you running the test, so let's censor that one away.
276 # Similar remarks hold for stderr.
277 $DEV =~ s{^[cpls].+?\sstdout$}{}m;
278 @DEV = grep { $_ ne 'stdout' } @DEV;
279 $DEV =~ s{^[cpls].+?\sstderr$}{}m;
280 @DEV = grep { $_ ne 'stderr' } @DEV;
282 # /dev/printer is also naughty: in IRIX it shows up as
283 # Srwx-----, not srwx------.
284 $DEV =~ s{^.+?\sprinter$}{}m;
285 @DEV = grep { $_ ne 'printer' } @DEV;
287 # If running as root, we will see .files in the ls result,
288 # and readdir() will see them always. Potential for conflict,
289 # so let's weed them out.
290 $DEV =~ s{^.+?\s\..+?$}{}m;
291 @DEV = grep { ! m{^\..+$} } @DEV;
293 # Irix ls -l marks sockets with 'S' while 's' is a 'XENIX semaphore'.
295 $DEV =~ s{^S(.+?)}{s$1}mg;
299 my @c1 = eval qq[\$DEV =~ /^$_[0].*/mg];
300 my @c2 = eval qq[grep { $_[1] "/dev/\$_" } \@DEV];
303 is($c1, $c2, "ls and $_[1] agreeing on /dev ($c1 $c2)");
312 ok(! -b $Curdir, '!-b cwd');
313 ok(! -c $Curdir, '!-c cwd');
314 ok(! -S $Curdir, '!-S cwd');
322 # Find a set of directories that's very likely to have setuid files
323 # but not likely to be *all* setuid files.
324 my @bin = grep {-d && -r && -x} qw(/sbin /usr/sbin /bin /usr/bin);
325 skip "Can't find a setuid file to test with", 3 unless @bin;
328 opendir BIN, $bin or die "Can't opendir $bin: $!";
329 while (defined($_ = readdir BIN)) {
333 last if $uid && $uid < $cnt;
338 skip "No setuid programs", 3 if $uid == 0;
340 isnt($cnt, 0, 'found some programs');
341 isnt($uid, 0, ' found some setuid programs');
342 ok($uid < $cnt, " they're not all setuid");
346 # To assist in automated testing when a controlling terminal (/dev/tty)
347 # may not be available (at, cron rsh etc), the PERL_SKIP_TTY_TEST env var
348 # can be set to skip the tests that need a tty.
350 skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST};
352 my $TTY = "/dev/tty";
355 skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare;
356 skip "No TTY to test -t with", 2 unless -e $TTY;
359 warn "Can't open $TTY--run t/TEST outside of make.\n";
361 ok(-c TTY, 'tty is -c');
364 ok(! -t TTY, '!-t on closed TTY filehandle');
367 local $TODO = 'STDIN not a tty when output is to pipe' if $Is_VMS;
368 ok(-t, '-t on STDIN');
373 skip "No null device to test with", 1 unless -e $Null;
374 skip "We know Win32 thinks '$Null' is a TTY", 1 if $Is_MSWin32;
376 open(NULL, $Null) or DIE("Can't open $Null: $!");
377 ok(! -t NULL, 'null device is not a TTY');
382 # These aren't strictly "stat" calls, but so what?
383 my $statfile = './op/stat.t';
384 ok( -T $statfile, '-T');
385 ok(! -B $statfile, '!-B');
387 ok(! -T $Perl, '!-T');
392 skip "-T/B on filehandle not implemented", 15 if $@ =~ /not implemented/;
394 is( $@, '', '-T on filehandle causes no errors' );
397 ok(! -B FOO, ' !-B');
400 like($_, qr/perl/, 'after readline');
401 ok(-T FOO, ' still -T');
402 ok(! -B FOO, ' still -B');
407 like($_, qr/perl/, 'reopened and after readline');
408 ok(-T FOO, ' still -T');
409 ok(! -B FOO, ' still !-B');
411 ok(seek(FOO,0,0), 'after seek');
412 ok(-T FOO, ' still -T');
413 ok(! -B FOO, ' still !-B');
415 # It's documented this way in perlfunc *shrug*
417 ok(eof FOO, 'at EOF');
418 ok(-T FOO, ' still -T');
419 ok(-B FOO, ' now -B');
425 skip "No null device to test with", 2 unless -e $Null;
427 ok(-T $Null, 'null device is -T');
428 ok(-B $Null, ' and -B');
432 # and now, a few parsing tests:
434 ok(-f, 'bare -f uses $_');
437 unlink $tmpfile or print "# unlink failed: $!\n";
439 # bug id 20011101.069
440 my @r = \stat($Curdir);
441 is(scalar @r, 13, 'stat returns full 13 elements');
445 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
446 'lstat _ croaks after stat' );
448 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
449 'lstat *_ croaks after stat' );
451 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
452 'lstat \*_ croaks after stat' );
454 like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
455 '-l _ croaks after stat' );
459 is( "$@", "", "lstat _ ok after lstat" );
461 is( "$@", "", "-l _ ok after lstat" );
463 eval { lstat "test.pl" };
465 open my $fh, "test.pl";
469 like $@, qr/^The stat preceding lstat\(\) wasn't an lstat at /,
470 'stat $ioref resets stat type';
473 my @statbuf = stat STDOUT;
475 my @lstatbuf = lstat *STDOUT{IO};
476 is "@lstatbuf", "@statbuf", 'lstat $ioref reverts to regular fstat';
480 skip "No lstat", 2 unless $Config{d_lstat};
482 # bug id 20020124.004
483 # If we have d_lstat, we should have symlink()
484 my $linkname = 'stat-' . rand =~ y/.//dr;
486 $target =~ s/;\d+\z// if $Is_VMS; # symlinks don't like version numbers
487 symlink $target, $linkname or die "# Can't symlink $0: $!";
491 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
492 'lstat croaks after -T _' );
494 like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
495 '-l _ croaks after -T _' );
496 unlink $linkname or print "# unlink $linkname failed: $!\n";
500 skip "Too much clock skew between system and filesystem", 5
501 if ($Filesystem_Time_Offset > 5);
503 sleep($Filesystem_Time_Offset+1);
504 my $f = 'tstamp.tmp';
506 ok (open(S, "> $f"), 'can create tmp file');
509 print "# time=$^T, stat=(@a)\n";
510 my @b = (-M _, -A _, -C _);
511 print "# -MAC=(@b)\n";
512 ok( (-M _) < 0, 'negative -M works');
515 skip "Access timestamps inaccurate", 1 if $inaccurate_atime;
516 ok( (-A _) < 0, 'negative -A works');
518 ok( (-C _) < 0, 'negative -C works');
519 ok(unlink($f), 'unlink tmp file');
524 ok(open(F, ">", $tmpfile), 'can create temp file');
526 chmod 0077, $tmpfile;
527 my @a = stat($tmpfile);
531 is($s1, $s2, q(-T _ doesn't break the statbuffer));
533 skip "No lstat", 1 unless $Config{d_lstat};
534 skip "uid=0", 1 unless $<&&$>;
535 skip "Readable by group/other means readable by me", 1 if $^O eq 'VMS';
539 q(-T _ doesn't break lstat for unreadable file));
545 skip "No dirfd()", 9 unless $Config{d_dirfd} || $Config{d_dir_dd_fd};
546 ok(opendir(DIR, "."), 'Can open "." dir') || diag "Can't open '.': $!";
547 ok(stat(DIR), "stat() on dirhandle works");
548 ok(-d -r _ , "chained -x's on dirhandle");
549 ok(-d DIR, "-d on a dirhandle works");
551 # And now for the ambiguous bareword case
553 no warnings 'deprecated';
554 ok(open(DIR, "TEST"), 'Can open "TEST" dir')
555 || diag "Can't open 'TEST': $!";
557 my $size = (stat(DIR))[7];
558 ok(defined $size, "stat() on bareword works");
559 is($size, -s "TEST", "size returned by stat of bareword is for the file");
560 ok(-f _, "ambiguous bareword uses file handle, not dir handle");
562 closedir DIR or die $!;
567 # RT #8244: *FILE{IO} does not behave like *FILE for stat() and -X() operators
568 ok(open(F, ">", $tmpfile), 'can create temp file');
569 my @thwap = stat *F{IO};
570 ok(@thwap, "stat(*F{IO}) works");
571 ok( -f *F{IO} , "single file tests work with *F{IO}");
575 #PVIO's hold dirhandle information, so let's test them too.
578 skip "No dirfd()", 9 unless $Config{d_dirfd} || $Config{d_dir_dd_fd};
579 ok(opendir(DIR, "."), 'Can open "." dir') || diag "Can't open '.': $!";
580 ok(stat(*DIR{IO}), "stat() on *DIR{IO} works");
581 ok(-d _ , "The special file handle _ is set correctly");
582 ok(-d -r *DIR{IO} , "chained -x's on *DIR{IO}");
584 # And now for the ambiguous bareword case
586 no warnings 'deprecated';
587 ok(open(DIR, "TEST"), 'Can open "TEST" dir')
588 || diag "Can't open 'TEST': $!";
590 my $size = (stat(*DIR{IO}))[7];
591 ok(defined $size, "stat() on *THINGY{IO} works");
593 "size returned by stat of *THINGY{IO} is for the file");
594 ok(-f _, "ambiguous *THINGY{IO} uses file handle, not dir handle");
596 closedir DIR or die $!;
605 local $SIG{__WARN__} = sub { warn shift; ++$w };
606 stat 'prepeinamehyparcheiarcheiometoonomaavto';
608 is $w, undef, 'no unopened warning from stat _';
612 chmod 0666, $tmpfile;