Commit | Line | Data |
---|---|---|
8d063cd8 LW |
1 | #!./perl |
2 | ||
ea368a7c CS |
3 | BEGIN { |
4 | chdir 't' if -d 't'; | |
20822f61 | 5 | @INC = '../lib'; |
cc25fa79 | 6 | require './test.pl'; |
ea368a7c CS |
7 | } |
8 | ||
9 | use Config; | |
de5a37b2 | 10 | use File::Spec; |
ea368a7c | 11 | |
a7ec4029 | 12 | plan tests => 69; |
8d063cd8 | 13 | |
cc25fa79 MS |
14 | $Is_Amiga = $^O eq 'amigaos'; |
15 | $Is_Cygwin = $^O eq 'cygwin'; | |
16 | $Is_Dos = $^O eq 'dos'; | |
17 | $Is_MPE = $^O eq 'mpeix'; | |
68dc0745 | 18 | $Is_MSWin32 = $^O eq 'MSWin32'; |
2986a63f | 19 | $Is_NetWare = $^O eq 'NetWare'; |
cc25fa79 MS |
20 | $Is_OS2 = $^O eq 'os2'; |
21 | $Is_Solaris = $^O eq 'solaris'; | |
de5a37b2 | 22 | $Is_VMS = $^O eq 'VMS'; |
cc25fa79 MS |
23 | |
24 | $Is_Dosish = $Is_Dos || $Is_OS2 || $Is_MSWin32 || $Is_NetWare || $Is_Cygwin; | |
cc25fa79 MS |
25 | |
26 | my($DEV, $INO, $MODE, $NLINK, $UID, $GID, $RDEV, $SIZE, | |
27 | $ATIME, $MTIME, $CTIME, $BLKSIZE, $BLOCKS) = (0..12); | |
d48672a2 | 28 | |
de5a37b2 MS |
29 | my $Curdir = File::Spec->curdir; |
30 | ||
4435c477 | 31 | |
de5a37b2 | 32 | my $tmpfile = 'Op_stat.tmp'; |
cc25fa79 | 33 | my $tmpfile_link = $tmpfile.'2'; |
4435c477 | 34 | |
8d220878 | 35 | |
cc25fa79 MS |
36 | unlink $tmpfile; |
37 | open(FOO, ">$tmpfile") || BAILOUT("Can't open temp test file: $!"); | |
de5a37b2 | 38 | close FOO; |
8d220878 | 39 | |
de5a37b2 | 40 | open(FOO, ">$tmpfile") || BAILOUT("Can't open temp test file: $!"); |
4435c477 | 41 | |
cc25fa79 MS |
42 | my($nlink, $mtime, $ctime) = (stat(FOO))[$NLINK, $MTIME, $CTIME]; |
43 | is($nlink, 1, 'nlink on regular file'); | |
8d220878 | 44 | |
cc25fa79 MS |
45 | SKIP: { |
46 | skip "mtime and ctime not reliable", 2 | |
47 | if $Is_MSWin32 or $Is_NetWare or $Is_Cygwin or $Is_Dos; | |
48 | ||
49 | ok( $mtime, 'mtime' ); | |
50 | is( $mtime, $ctime, 'mtime == ctime' ); | |
4435c477 | 51 | } |
8d063cd8 | 52 | |
cc25fa79 MS |
53 | |
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; | |
57 | ||
58 | print FOO "Now is the time for all good men to come to.\n"; | |
59 | close(FOO); | |
60 | ||
61 | sleep 2 unless $funky_FAT_timestamps; | |
62 | ||
63 | ||
64 | SKIP: { | |
65 | unlink $tmpfile_link; | |
de5a37b2 MS |
66 | my $lnk_result = eval { link $tmpfile, $tmpfile_link }; |
67 | skip "link() unimplemented", 6 if $@ =~ /unimplemented/; | |
cc25fa79 | 68 | |
de5a37b2 MS |
69 | is( $@, '', 'link() implemented' ); |
70 | ok( $lnk_result, 'linked tmp testfile' ); | |
cc25fa79 MS |
71 | ok( chmod(0644, $tmpfile), 'chmoded tmp testfile' ); |
72 | ||
73 | my($nlink, $mtime, $ctime) = (stat($tmpfile))[$NLINK, $MTIME, $CTIME]; | |
74 | ||
75 | SKIP: { | |
76 | skip "No link count", 1 if $Config{dont_use_nlink}; | |
77 | is($nlink, 2, 'Link count on hard linked file' ); | |
78 | } | |
79 | ||
80 | SKIP: { | |
de5a37b2 | 81 | my $cwd = File::Spec->rel2abs($Curdir); |
cc25fa79 MS |
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 | |
88 | if $Is_Amiga; | |
89 | ||
90 | if( !ok($mtime, 'hard link mtime') || | |
91 | !isnt($mtime, $ctime, 'hard link ctime != mtime') ) { | |
92 | print <<DIAG; | |
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 | |
95 | # cause this failure. | |
96 | DIAG | |
97 | } | |
98 | } | |
99 | ||
3fe9a6f1 | 100 | } |
8d063cd8 | 101 | |
cc25fa79 MS |
102 | # truncate and touch $tmpfile. |
103 | open(F, ">$tmpfile") || BAILOUT("Can't open temp test file: $!"); | |
104 | close F; | |
105 | ||
106 | ok(-z $tmpfile, '-z on empty file'); | |
107 | ok(! -s $tmpfile, ' and -s'); | |
108 | ||
109 | open(F, ">$tmpfile") || BAILOUT("Can't open temp test file: $!"); | |
110 | print F "hi\n"; | |
111 | close F; | |
112 | ||
113 | ok(! -z $tmpfile, '-z on non-empty file'); | |
114 | ok(-s $tmpfile, ' and -s'); | |
115 | ||
116 | ||
117 | # Strip all access rights from the file. | |
118 | ok( chmod(0000, $tmpfile), 'chmod 0000' ); | |
119 | ||
120 | SKIP: { | |
de5a37b2 | 121 | skip "-r, -w and -x have different meanings on VMS", 3 if $Is_VMS; |
cc25fa79 MS |
122 | |
123 | SKIP: { | |
de5a37b2 MS |
124 | # Going to try to switch away from root. Might not work. |
125 | my $olduid = $>; | |
126 | eval { $> = 1; }; | |
127 | skip "Can't test -r or -w meaningfully if you're superuser", 2 | |
128 | if $> == 0; | |
129 | ||
130 | SKIP: { | |
131 | skip "Can't test -r meaningfully?", 1 if $Is_Dos || $Is_Cygwin; | |
132 | ok(!-r $tmpfile, " -r"); | |
133 | } | |
cc25fa79 | 134 | |
de5a37b2 | 135 | ok(!-w $tmpfile, " -w"); |
cc25fa79 | 136 | |
de5a37b2 MS |
137 | # switch uid back (may not be implemented) |
138 | eval { $> = $olduid; }; | |
139 | } | |
140 | ||
141 | ok(! -x $tmpfile, ' -x'); | |
a245ea2d | 142 | } |
cc25fa79 | 143 | |
de5a37b2 | 144 | |
cc25fa79 MS |
145 | |
146 | ||
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 "# $@" ; | |
150 | ||
151 | ok(chmod(0700,$tmpfile), 'chmod 0700'); | |
152 | ok(-r $tmpfile, ' -r'); | |
153 | ok(-w $tmpfile, ' -w'); | |
154 | ||
155 | SKIP: { | |
156 | skip "-x simply determins if a file ends in an executable suffix", 1 | |
157 | if $Is_Dosish; | |
158 | ||
159 | ok(-x $tmpfile, ' -x'); | |
6eb13c3b | 160 | } |
cc25fa79 MS |
161 | |
162 | ok( -f $tmpfile, ' -f'); | |
163 | ok(! -d $tmpfile, ' !-d'); | |
164 | ||
165 | # Is this portable? | |
de5a37b2 MS |
166 | ok( -d $Curdir, '-d cwd' ); |
167 | ok(! -f $Curdir, '!-f cwd' ); | |
168 | ||
cc25fa79 MS |
169 | |
170 | SKIP: { | |
de5a37b2 MS |
171 | unlink($tmpfile_link); |
172 | my $symlink_rslt = eval { symlink $tmpfile, $tmpfile_link }; | |
173 | skip "symlink not implemented", 3 if $@ =~ /unimplemented/; | |
cc25fa79 | 174 | |
de5a37b2 MS |
175 | is( $@, '', 'symlink() implemented' ); |
176 | ok( $symlink_rslt, 'symlink() ok' ); | |
177 | ok(-l $tmpfile_link, '-l'); | |
6eb13c3b | 178 | } |
cc25fa79 MS |
179 | |
180 | ok(-o $tmpfile, '-o'); | |
181 | ||
182 | ok(-e $tmpfile, '-e'); | |
de5a37b2 MS |
183 | |
184 | unlink($tmpfile_link); | |
cc25fa79 MS |
185 | ok(! -e $tmpfile_link, ' -e on unlinked file'); |
186 | ||
187 | SKIP: { | |
a7ec4029 | 188 | skip "No character, socket or block special files", 3 |
cc25fa79 | 189 | if $Is_MSWin32 || $Is_NetWare || $Is_Dos; |
a7ec4029 | 190 | skip "/dev/ isn't available to test against", 3 |
de5a37b2 MS |
191 | unless -d '/dev/' && -r '/dev/' && -x '/dev/'; |
192 | ||
a7ec4029 JH |
193 | my $LS = $Config{d_readlink} ? "ls -lL" : "ls -l"; |
194 | my $CMD = "$LS /dev"; | |
195 | my $DEV = qx($CMD); | |
196 | ||
197 | skip "$CMD failed", 3 if $DEV eq ''; | |
198 | ||
199 | my $type; | |
200 | ||
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") }; | |
204 | ||
205 | if ($DEV !~ /\nb.* (\S+)\n/) { $skip->("block special") } | |
206 | elsif (-b "/dev/$1") { $ok-> ("block special") } | |
207 | else { $fail->("block special", "-b") } | |
208 | ||
209 | if ($DEV !~ /\nc.* (\S+)\n/) { $skip->("character special") } | |
210 | elsif (-c "/dev/$1") { $ok-> ("character special") } | |
211 | else { $fail->("character special", "-c") } | |
212 | ||
213 | if ($DEV !~ /\ns.* (\S+)\n/) { $skip->("socket") } | |
214 | elsif (-S "/dev/$1") { $ok-> ("socket") } | |
215 | else { $fail->("socket", "-S") } | |
378cc40b LW |
216 | } |
217 | ||
de5a37b2 MS |
218 | ok(! -c $Curdir, '!-c cwd'); |
219 | ok(! -S $Curdir, '!-S cwd'); | |
220 | ok(! -b $Curdir, '!-b cwd'); | |
cc25fa79 | 221 | |
cc25fa79 MS |
222 | |
223 | SKIP: { | |
de5a37b2 | 224 | skip "No setuid", 3 if $Is_MPE or $Is_Amiga or $Is_Dosish or $Is_Cygwin; |
b971f6e4 | 225 | |
cc25fa79 MS |
226 | my($cnt, $uid); |
227 | $cnt = $uid = 0; | |
228 | ||
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); | |
de5a37b2 | 232 | skip "Can't find a setuid file to test with", 3 unless @bin; |
cc25fa79 MS |
233 | |
234 | for my $bin (@bin) { | |
235 | opendir BIN, $bin or die "Can't opendir $bin: $!"; | |
236 | while (defined($_ = readdir BIN)) { | |
237 | $_ = "$bin/$_"; | |
238 | $cnt++; | |
239 | $uid++ if -u; | |
240 | last if $uid && $uid < $cnt; | |
241 | } | |
242 | } | |
243 | closedir BIN; | |
244 | ||
de5a37b2 MS |
245 | if( !isnt($cnt, 0, 'found some programs') || |
246 | !isnt($uid, 0, 'found some setuid programs') || | |
cc25fa79 MS |
247 | !ok($uid < $cnt, " they're not all setuid") ) |
248 | { | |
249 | print <<DIAG; | |
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). | |
253 | # @bin | |
254 | DIAG | |
218d7377 | 255 | } |
378cc40b | 256 | } |
b8440792 | 257 | |
378cc40b | 258 | |
3e3baf6d TB |
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. | |
cc25fa79 MS |
262 | SKIP: { |
263 | skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST}; | |
264 | ||
265 | my $TTY = $^O eq 'rhapsody' ? "/dev/ttyp0" : "/dev/tty"; | |
266 | ||
267 | SKIP: { | |
268 | skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare; | |
269 | skip "No TTY to test -t with", 2 unless -e $TTY; | |
270 | ||
271 | open(TTY, $TTY) || | |
272 | warn "Can't open $TTY--run t/TEST outside of make.\n"; | |
273 | ok(-t TTY, '-t'); | |
274 | ok(-c TTY, 'tty is -c'); | |
275 | close(TTY); | |
3e3baf6d | 276 | } |
cc25fa79 MS |
277 | ok(! -t TTY, '!-t on closed TTY filehandle'); |
278 | ok(-t, '-t on STDIN'); | |
68dc0745 | 279 | } |
cc25fa79 | 280 | |
de5a37b2 | 281 | my $Null = File::Spec->devnull; |
cc25fa79 | 282 | SKIP: { |
de5a37b2 | 283 | skip "No null device to test with", 1 unless -e $Null; |
cc25fa79 | 284 | |
de5a37b2 MS |
285 | open(NULL, $Null) or BAIL_OUT("Can't open $Null: $!"); |
286 | ok(! -t NULL, 'null device is not a TTY'); | |
cc25fa79 | 287 | close(NULL); |
378cc40b | 288 | } |
cc25fa79 | 289 | |
378cc40b LW |
290 | |
291 | # These aren't strictly "stat" calls, but so what? | |
292 | ||
cc25fa79 MS |
293 | ok(-T 'op/stat.t', '-T'); |
294 | ok(! -B 'op/stat.t', '!-B'); | |
378cc40b | 295 | |
cc25fa79 MS |
296 | ok(-B $^X, '-B'); |
297 | ok(! -T $^X, '!-T'); | |
378cc40b | 298 | |
f0fcb552 | 299 | open(FOO,'op/stat.t'); |
cc25fa79 MS |
300 | SKIP: { |
301 | eval { -T FOO; }; | |
de5a37b2 | 302 | skip "-T/B on filehandle not implemented", 15 if $@ =~ /not implemented/; |
cc25fa79 MS |
303 | |
304 | is( $@, '', '-T on filehandle causes no errors' ); | |
305 | ||
306 | ok(-T FOO, ' -T'); | |
307 | ok(! -B FOO, ' !-B'); | |
308 | ||
f0fcb552 | 309 | $_ = <FOO>; |
cc25fa79 MS |
310 | ok(/perl/, 'after readline'); |
311 | ok(-T FOO, ' still -T'); | |
312 | ok(! -B FOO, ' still -B'); | |
f0fcb552 LW |
313 | close(FOO); |
314 | ||
315 | open(FOO,'op/stat.t'); | |
316 | $_ = <FOO>; | |
cc25fa79 MS |
317 | ok(/perl/, 'reopened and after readline'); |
318 | ok(-T FOO, ' still -T'); | |
319 | ok(! -B FOO, ' still !-B'); | |
320 | ||
321 | ok(seek(FOO,0,0), 'after seek'); | |
de5a37b2 MS |
322 | ok(-T FOO, ' still -T'); |
323 | ok(! -B FOO, ' still !-B'); | |
324 | ||
325 | # It's documented this way in perlfunc *shrug* | |
326 | () = <FOO>; | |
327 | ok(eof FOO, 'at EOF'); | |
328 | ok(-T FOO, ' still -T'); | |
329 | ok(-B FOO, ' now -B'); | |
f0fcb552 LW |
330 | } |
331 | close(FOO); | |
378cc40b | 332 | |
cc25fa79 | 333 | |
de5a37b2 MS |
334 | SKIP: { |
335 | skip "No null device to test with", 2 unless -e $Null; | |
336 | ||
337 | ok(-T $Null, 'null device is -T'); | |
338 | ok(-B $Null, ' and -B'); | |
339 | } | |
cc25fa79 | 340 | |
108ed793 SM |
341 | |
342 | # and now, a few parsing tests: | |
cc25fa79 MS |
343 | $_ = $tmpfile; |
344 | ok(-f, 'bare -f uses $_'); | |
345 | ok(-f(), ' -f() "'); | |
108ed793 | 346 | |
cc25fa79 | 347 | unlink $tmpfile or print "# unlink failed: $!\n"; |
58d95175 RG |
348 | |
349 | # bug id 20011101.069 | |
350 | my @r = \stat("."); | |
cc25fa79 MS |
351 | is(scalar @r, 13, 'stat returns full 13 elements'); |
352 |