source: trunk/essentials/dev-lang/perl/t/op/stat.t

Last change on this file was 3217, checked in by bird, 18 years ago

skip this on OS/2.

File size: 12.6 KB
Line 
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl'; # for which_perl() etc
7}
8
9use Config;
10use File::Spec;
11
12plan tests => 86;
13
14my $Perl = which_perl();
15
16$Is_Amiga = $^O eq 'amigaos';
17$Is_Cygwin = $^O eq 'cygwin';
18$Is_Darwin = $^O eq 'darwin';
19$Is_Dos = $^O eq 'dos';
20$Is_MacOS = $^O eq 'MacOS';
21$Is_MPE = $^O eq 'mpeix';
22$Is_MSWin32 = $^O eq 'MSWin32';
23$Is_NetWare = $^O eq 'NetWare';
24$Is_OS2 = $^O eq 'os2';
25$Is_Solaris = $^O eq 'solaris';
26$Is_VMS = $^O eq 'VMS';
27$Is_DGUX = $^O eq 'dgux';
28$Is_MPRAS = $^O =~ /svr4/ && -f '/etc/.relid';
29$Is_Rhapsody= $^O eq 'rhapsody';
30
31$Is_Dosish = $Is_Dos || $Is_OS2 || $Is_MSWin32 || $Is_NetWare || $Is_Cygwin;
32
33$Is_UFS = $Is_Darwin && (() = `df -t ufs .`) == 2;
34
35my($DEV, $INO, $MODE, $NLINK, $UID, $GID, $RDEV, $SIZE,
36 $ATIME, $MTIME, $CTIME, $BLKSIZE, $BLOCKS) = (0..12);
37
38my $Curdir = File::Spec->curdir;
39
40
41my $tmpfile = 'Op_stat.tmp';
42my $tmpfile_link = $tmpfile.'2';
43
44
451 while unlink $tmpfile;
46open(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
47close FOO;
48
49open(FOO, ">$tmpfile") || DIE("Can't open temp test file: $!");
50
51my($nlink, $mtime, $ctime) = (stat(FOO))[$NLINK, $MTIME, $CTIME];
52SKIP: {
53 skip "No link count", 1 if $Is_VMS;
54
55 is($nlink, 1, 'nlink on regular file');
56}
57
58SKIP: {
59 skip "mtime and ctime not reliable", 2
60 if $Is_MSWin32 or $Is_NetWare or $Is_Cygwin or $Is_Dos or $Is_MacOS;
61
62 ok( $mtime, 'mtime' );
63 is( $mtime, $ctime, 'mtime == ctime' );
64}
65
66
67# Cygwin seems to have a 3 second granularity on its timestamps.
68my $funky_FAT_timestamps = $Is_Cygwin;
69sleep 3 if $funky_FAT_timestamps;
70
71print FOO "Now is the time for all good men to come to.\n";
72close(FOO);
73
74sleep 2;
75
76
77SKIP: {
78 unlink $tmpfile_link;
79 my $lnk_result = eval { link $tmpfile, $tmpfile_link };
80 skip "link() unimplemented", 6 if $@ =~ /unimplemented/;
81
82 is( $@, '', 'link() implemented' );
83 ok( $lnk_result, 'linked tmp testfile' );
84 ok( chmod(0644, $tmpfile), 'chmoded tmp testfile' );
85
86 my($nlink, $mtime, $ctime) = (stat($tmpfile))[$NLINK, $MTIME, $CTIME];
87
88 SKIP: {
89 skip "No link count", 1 if $Config{dont_use_nlink};
90 skip "Cygwin9X fakes hard links by copying", 1
91 if $Config{myuname} =~ /^cygwin_(?:9\d|me)\b/i;
92
93 is($nlink, 2, 'Link count on hard linked file' );
94 }
95
96 SKIP: {
97 my $cwd = File::Spec->rel2abs($Curdir);
98 skip "Solaris tmpfs has different mtime/ctime link semantics", 2
99 if $Is_Solaris and $cwd =~ m#^/tmp# and
100 $mtime && $mtime == $ctime;
101 skip "AFS has different mtime/ctime link semantics", 2
102 if $cwd =~ m#$Config{'afsroot'}/#;
103 skip "AmigaOS has different mtime/ctime link semantics", 2
104 if $Is_Amiga;
105 # Win32 could pass $mtime test but as FAT and NTFS have
106 # no ctime concept $ctime is ALWAYS == $mtime
107 # expect netware to be the same ...
108 skip "No ctime concept on this OS", 2
109 if $Is_MSWin32 ||
110 ($Is_Darwin && $Is_UFS);
111
112 if( !ok($mtime, 'hard link mtime') ||
113 !isnt($mtime, $ctime, 'hard link ctime != mtime') ) {
114 print STDERR <<DIAG;
115# Check if you are on a tmpfs of some sort. Building in /tmp sometimes
116# has this problem. Building on the ClearCase VOBS filesystem may also
117# cause this failure.
118#
119# Darwin's UFS doesn't have a ctime concept, and thus is expected to fail
120# this test.
121DIAG
122 }
123 }
124
125}
126
127# truncate and touch $tmpfile.
128open(F, ">$tmpfile") || DIE("Can't open temp test file: $!");
129ok(-z \*F, '-z on empty filehandle');
130ok(! -s \*F, ' and -s');
131close F;
132
133ok(-z $tmpfile, '-z on empty file');
134ok(! -s $tmpfile, ' and -s');
135
136open(F, ">$tmpfile") || DIE("Can't open temp test file: $!");
137print F "hi\n";
138close F;
139
140open(F, "<$tmpfile") || DIE("Can't open temp test file: $!");
141ok(!-z *F, '-z on empty filehandle');
142ok( -s *F, ' and -s');
143close F;
144
145ok(! -z $tmpfile, '-z on non-empty file');
146ok(-s $tmpfile, ' and -s');
147
148
149# Strip all access rights from the file.
150ok( chmod(0000, $tmpfile), 'chmod 0000' );
151
152SKIP: {
153 skip "-r, -w and -x have different meanings on VMS", 3 if $Is_VMS;
154
155 SKIP: {
156 # Going to try to switch away from root. Might not work.
157 my $olduid = $>;
158 eval { $> = 1; };
159 skip "Can't test -r or -w meaningfully if you're superuser", 2
160 if $> == 0;
161
162 SKIP: {
163 skip "Can't test -r meaningfully?", 1 if $Is_Dos || $Is_Cygwin;
164 ok(!-r $tmpfile, " -r");
165 }
166
167 ok(!-w $tmpfile, " -w");
168
169 # switch uid back (may not be implemented)
170 eval { $> = $olduid; };
171 }
172
173 ok(! -x $tmpfile, ' -x');
174}
175
176
177
178ok(chmod(0700,$tmpfile), 'chmod 0700');
179ok(-r $tmpfile, ' -r');
180ok(-w $tmpfile, ' -w');
181
182SKIP: {
183 skip "-x simply determines if a file ends in an executable suffix", 1
184 if $Is_Dosish || $Is_MacOS;
185
186 ok(-x $tmpfile, ' -x');
187}
188
189ok( -f $tmpfile, ' -f');
190ok(! -d $tmpfile, ' !-d');
191
192# Is this portable?
193ok( -d $Curdir, '-d cwd' );
194ok(! -f $Curdir, '!-f cwd' );
195
196
197SKIP: {
198 unlink($tmpfile_link);
199 my $symlink_rslt = eval { symlink $tmpfile, $tmpfile_link };
200 skip "symlink not implemented", 3 if $@ =~ /unimplemented/;
201
202 is( $@, '', 'symlink() implemented' );
203 ok( $symlink_rslt, 'symlink() ok' );
204 ok(-l $tmpfile_link, '-l');
205}
206
207ok(-o $tmpfile, '-o');
208
209ok(-e $tmpfile, '-e');
210
211unlink($tmpfile_link);
212ok(! -e $tmpfile_link, ' -e on unlinked file');
213
214SKIP: {
215 skip "No character, socket or block special files", 6
216 if $Is_MSWin32 || $Is_NetWare || $Is_Dos;
217 skip "/dev isn't available to test against", 6
218 unless -d '/dev' && -r '/dev' && -x '/dev';
219 skip "Skipping: unexpected ls output in MP-RAS", 6
220 if $Is_MPRAS;
221
222 my $LS = $Config{d_readlink} ? "ls -lL" : "ls -l";
223 my $CMD = "$LS /dev 2>/dev/null";
224 my $DEV = qx($CMD);
225
226 skip "$CMD failed", 6 if $DEV eq '';
227
228 my @DEV = do { my $dev; opendir($dev, "/dev") ? readdir($dev) : () };
229
230 skip "opendir failed: $!", 6 if @DEV == 0;
231
232 # /dev/stdout might be either character special or a named pipe,
233 # or a symlink, or a socket, depending on which OS and how are
234 # you running the test, so let's censor that one away.
235 # Similar remarks hold for stderr.
236 $DEV =~ s{^[cpls].+?\sstdout$}{}m;
237 @DEV = grep { $_ ne 'stdout' } @DEV;
238 $DEV =~ s{^[cpls].+?\sstderr$}{}m;
239 @DEV = grep { $_ ne 'stderr' } @DEV;
240
241 # /dev/printer is also naughty: in IRIX it shows up as
242 # Srwx-----, not srwx------.
243 $DEV =~ s{^.+?\sprinter$}{}m;
244 @DEV = grep { $_ ne 'printer' } @DEV;
245
246 # If running as root, we will see .files in the ls result,
247 # and readdir() will see them always. Potential for conflict,
248 # so let's weed them out.
249 $DEV =~ s{^.+?\s\..+?$}{}m;
250 @DEV = grep { ! m{^\..+$} } @DEV;
251
252 # Irix ls -l marks sockets with 'S' while 's' is a 'XENIX semaphore'.
253 if ($^O eq 'irix') {
254 $DEV =~ s{^S(.+?)}{s$1}mg;
255 }
256
257 my $try = sub {
258 my @c1 = eval qq[\$DEV =~ /^$_[0].*/mg];
259 my @c2 = eval qq[grep { $_[1] "/dev/\$_" } \@DEV];
260 my $c1 = scalar @c1;
261 my $c2 = scalar @c2;
262 is($c1, $c2, "ls and $_[1] agreeing on /dev ($c1 $c2)");
263 };
264
265SKIP: {
266 skip("DG/UX ls -L broken", 3) if $Is_DGUX;
267
268 $try->('b', '-b');
269 $try->('c', '-c');
270 $try->('s', '-S');
271
272}
273
274ok(! -b $Curdir, '!-b cwd');
275ok(! -c $Curdir, '!-c cwd');
276ok(! -S $Curdir, '!-S cwd');
277
278}
279
280SKIP: {
281 my($cnt, $uid);
282 $cnt = $uid = 0;
283
284 # Find a set of directories that's very likely to have setuid files
285 # but not likely to be *all* setuid files.
286 my @bin = grep {-d && -r && -x} qw(/sbin /usr/sbin /bin /usr/bin);
287 skip "Can't find a setuid file to test with", 3 unless @bin;
288
289 for my $bin (@bin) {
290 opendir BIN, $bin or die "Can't opendir $bin: $!";
291 while (defined($_ = readdir BIN)) {
292 $_ = "$bin/$_";
293 $cnt++;
294 $uid++ if -u;
295 last if $uid && $uid < $cnt;
296 }
297 }
298 closedir BIN;
299
300 skip "No setuid programs", 3 if $uid == 0;
301
302 isnt($cnt, 0, 'found some programs');
303 isnt($uid, 0, ' found some setuid programs');
304 ok($uid < $cnt, " they're not all setuid");
305}
306
307
308# To assist in automated testing when a controlling terminal (/dev/tty)
309# may not be available (at, cron rsh etc), the PERL_SKIP_TTY_TEST env var
310# can be set to skip the tests that need a tty.
311SKIP: {
312 skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST};
313
314 my $TTY = $Is_Rhapsody ? "/dev/ttyp0" : "/dev/tty";
315
316 SKIP: {
317 skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare;
318 skip "No TTY to test -t with", 2 unless -e $TTY;
319
320 open(TTY, $TTY) ||
321 warn "Can't open $TTY--run t/TEST outside of make.\n";
322 ok(-t TTY, '-t');
323 ok(-c TTY, 'tty is -c');
324 close(TTY);
325 }
326 ok(! -t TTY, '!-t on closed TTY filehandle');
327
328 {
329 local $TODO = 'STDIN not a tty when output is to pipe' if $Is_VMS;
330 ok(-t, '-t on STDIN');
331 }
332}
333
334my $Null = File::Spec->devnull;
335SKIP: {
336 skip "No null device to test with", 1 unless -e $Null;
337 skip "We know Win32 thinks '$Null' is a TTY", 1 if $Is_MSWin32;
338 skip "We know kNIX thinks '$Null' is a TTY", 1 if $Is_OS2; # FIXME
339
340 open(NULL, $Null) or DIE("Can't open $Null: $!");
341 ok(! -t NULL, 'null device is not a TTY');
342 close(NULL);
343}
344
345
346# These aren't strictly "stat" calls, but so what?
347my $statfile = File::Spec->catfile($Curdir, 'op', 'stat.t');
348ok( -T $statfile, '-T');
349ok(! -B $statfile, '!-B');
350
351SKIP: {
352 skip("DG/UX", 1) if $Is_DGUX;
353ok(-B $Perl, '-B');
354}
355
356ok(! -T $Perl, '!-T');
357
358open(FOO,$statfile);
359SKIP: {
360 eval { -T FOO; };
361 skip "-T/B on filehandle not implemented", 15 if $@ =~ /not implemented/;
362
363 is( $@, '', '-T on filehandle causes no errors' );
364
365 ok(-T FOO, ' -T');
366 ok(! -B FOO, ' !-B');
367
368 $_ = <FOO>;
369 like($_, qr/perl/, 'after readline');
370 ok(-T FOO, ' still -T');
371 ok(! -B FOO, ' still -B');
372 close(FOO);
373
374 open(FOO,$statfile);
375 $_ = <FOO>;
376 like($_, qr/perl/, 'reopened and after readline');
377 ok(-T FOO, ' still -T');
378 ok(! -B FOO, ' still !-B');
379
380 ok(seek(FOO,0,0), 'after seek');
381 ok(-T FOO, ' still -T');
382 ok(! -B FOO, ' still !-B');
383
384 # It's documented this way in perlfunc *shrug*
385 () = <FOO>;
386 ok(eof FOO, 'at EOF');
387 ok(-T FOO, ' still -T');
388 ok(-B FOO, ' now -B');
389}
390close(FOO);
391
392
393SKIP: {
394 skip "No null device to test with", 2 unless -e $Null;
395
396 ok(-T $Null, 'null device is -T');
397 ok(-B $Null, ' and -B');
398}
399
400
401# and now, a few parsing tests:
402$_ = $tmpfile;
403ok(-f, 'bare -f uses $_');
404ok(-f(), ' -f() "');
405
406unlink $tmpfile or print "# unlink failed: $!\n";
407
408# bug id 20011101.069
409my @r = \stat($Curdir);
410is(scalar @r, 13, 'stat returns full 13 elements');
411
412stat $0;
413eval { lstat _ };
414like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
415 'lstat _ croaks after stat' );
416eval { -l _ };
417like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
418 '-l _ croaks after stat' );
419
420lstat $0;
421eval { lstat _ };
422is( "$@", "", "lstat _ ok after lstat" );
423eval { -l _ };
424is( "$@", "", "-l _ ok after lstat" );
425
426SKIP: {
427 skip "No lstat", 2 unless $Config{d_lstat};
428
429 # bug id 20020124.004
430 # If we have d_lstat, we should have symlink()
431 my $linkname = 'dolzero';
432 symlink $0, $linkname or die "# Can't symlink $0: $!";
433 lstat $linkname;
434 -T _;
435 eval { lstat _ };
436 like( $@, qr/^The stat preceding lstat\(\) wasn't an lstat/,
437 'lstat croaks after -T _' );
438 eval { -l _ };
439 like( $@, qr/^The stat preceding -l _ wasn't an lstat/,
440 '-l _ croaks after -T _' );
441 unlink $linkname or print "# unlink $linkname failed: $!\n";
442}
443
444print "# Zzz...\n";
445sleep(3);
446my $f = 'tstamp.tmp';
447unlink $f;
448ok (open(S, "> $f"), 'can create tmp file');
449close S or die;
450my @a = stat $f;
451print "# time=$^T, stat=(@a)\n";
452my @b = (-M _, -A _, -C _);
453print "# -MAC=(@b)\n";
454ok( (-M _) < 0, 'negative -M works');
455ok( (-A _) < 0, 'negative -A works');
456ok( (-C _) < 0, 'negative -C works');
457ok(unlink($f), 'unlink tmp file');
458
459{
460 ok(open(F, ">", $tmpfile), 'can create temp file');
461 close F;
462 chmod 0077, $tmpfile;
463 my @a = stat($tmpfile);
464 my $s1 = -s _;
465 -T _;
466 my $s2 = -s _;
467 is($s1, $s2, q(-T _ doesn't break the statbuffer));
468 unlink $tmpfile;
469}
470
471END {
472 1 while unlink $tmpfile;
473}
Note: See TracBrowser for help on using the repository browser.