Commit | Line | Data |
8d063cd8 |
1 | #!./perl |
2 | |
ea368a7c |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
20822f61 |
5 | @INC = '../lib'; |
cc25fa79 |
6 | require './test.pl'; |
ea368a7c |
7 | } |
8 | |
9 | use Config; |
10 | |
cc25fa79 |
11 | plan tests => 63; |
8d063cd8 |
12 | |
cc25fa79 |
13 | $Is_Amiga = $^O eq 'amigaos'; |
14 | $Is_Cygwin = $^O eq 'cygwin'; |
15 | $Is_Dos = $^O eq 'dos'; |
16 | $Is_MPE = $^O eq 'mpeix'; |
68dc0745 |
17 | $Is_MSWin32 = $^O eq 'MSWin32'; |
2986a63f |
18 | $Is_NetWare = $^O eq 'NetWare'; |
cc25fa79 |
19 | $Is_OS2 = $^O eq 'os2'; |
20 | $Is_Solaris = $^O eq 'solaris'; |
21 | |
22 | $Is_Dosish = $Is_Dos || $Is_OS2 || $Is_MSWin32 || $Is_NetWare || $Is_Cygwin; |
2986a63f |
23 | chop($cwd = (($Is_MSWin32 || $Is_NetWare) ? `cd` : `pwd`)); |
6eb13c3b |
24 | |
cc25fa79 |
25 | $Dev_list = `ls -l /dev` unless $Is_Dosish or $Is_Cygwin; |
26 | |
27 | my($DEV, $INO, $MODE, $NLINK, $UID, $GID, $RDEV, $SIZE, |
28 | $ATIME, $MTIME, $CTIME, $BLKSIZE, $BLOCKS) = (0..12); |
d48672a2 |
29 | |
4435c477 |
30 | |
cc25fa79 |
31 | my $tmpfile = 'Op.stat.tmp'; |
32 | my $tmpfile_link = $tmpfile.'2'; |
4435c477 |
33 | |
8d220878 |
34 | |
cc25fa79 |
35 | unlink $tmpfile; |
36 | open(FOO, ">$tmpfile") || BAILOUT("Can't open temp test file: $!"); |
8d220878 |
37 | |
cc25fa79 |
38 | # hack to make Apollo update link count: |
39 | $junk = `ls $tmpfile` unless ($Is_MSWin32 || $Is_NetWare || $Is_Dos); |
4435c477 |
40 | |
cc25fa79 |
41 | my($nlink, $mtime, $ctime) = (stat(FOO))[$NLINK, $MTIME, $CTIME]; |
42 | is($nlink, 1, 'nlink on regular file'); |
8d220878 |
43 | |
cc25fa79 |
44 | SKIP: { |
45 | skip "mtime and ctime not reliable", 2 |
46 | if $Is_MSWin32 or $Is_NetWare or $Is_Cygwin or $Is_Dos; |
47 | |
48 | ok( $mtime, 'mtime' ); |
49 | is( $mtime, $ctime, 'mtime == ctime' ); |
4435c477 |
50 | } |
8d063cd8 |
51 | |
cc25fa79 |
52 | |
53 | # Cygwin seems to have a 3 second granularity on its timestamps. |
54 | my $funky_FAT_timestamps = $Is_Cygwin; |
55 | sleep 3 if $funky_FAT_timestamps; |
56 | |
57 | print FOO "Now is the time for all good men to come to.\n"; |
58 | close(FOO); |
59 | |
60 | sleep 2 unless $funky_FAT_timestamps; |
61 | |
62 | |
63 | SKIP: { |
64 | unlink $tmpfile_link; |
65 | |
66 | skip "No hard links", 5 if $Is_Dosish || $Is_MPE; |
67 | |
68 | ok( link($tmpfile, $tmpfile_link), 'linked tmp testfile' ); |
69 | ok( chmod(0644, $tmpfile), 'chmoded tmp testfile' ); |
70 | |
71 | my($nlink, $mtime, $ctime) = (stat($tmpfile))[$NLINK, $MTIME, $CTIME]; |
72 | |
73 | SKIP: { |
74 | skip "No link count", 1 if $Config{dont_use_nlink}; |
75 | is($nlink, 2, 'Link count on hard linked file' ); |
76 | } |
77 | |
78 | SKIP: { |
79 | skip "Solaris tmpfs has different mtime/ctime link semantics", 2 |
80 | if $Is_Solaris and $cwd =~ m#^/tmp# and |
81 | $mtime && $mtime == $ctime; |
82 | skip "AFS has different mtime/ctime link semantics", 2 |
83 | if $cwd =~ m#$Config{'afsroot'}/#; |
84 | skip "AmigaOS has different mtime/ctime link semantics", 2 |
85 | if $Is_Amiga; |
86 | |
87 | if( !ok($mtime, 'hard link mtime') || |
88 | !isnt($mtime, $ctime, 'hard link ctime != mtime') ) { |
89 | print <<DIAG; |
90 | # Check if you are on a tmpfs of some sort. Building in /tmp sometimes |
91 | # has this problem. Also building on the ClearCase VOBS filesystem may |
92 | # cause this failure. |
93 | DIAG |
94 | } |
95 | } |
96 | |
3fe9a6f1 |
97 | } |
8d063cd8 |
98 | |
cc25fa79 |
99 | # truncate and touch $tmpfile. |
100 | open(F, ">$tmpfile") || BAILOUT("Can't open temp test file: $!"); |
101 | close F; |
102 | |
103 | ok(-z $tmpfile, '-z on empty file'); |
104 | ok(! -s $tmpfile, ' and -s'); |
105 | |
106 | open(F, ">$tmpfile") || BAILOUT("Can't open temp test file: $!"); |
107 | print F "hi\n"; |
108 | close F; |
109 | |
110 | ok(! -z $tmpfile, '-z on non-empty file'); |
111 | ok(-s $tmpfile, ' and -s'); |
112 | |
113 | |
114 | # Strip all access rights from the file. |
115 | ok( chmod(0000, $tmpfile), 'chmod 0000' ); |
116 | |
117 | SKIP: { |
118 | # Going to try to switch away from root. Might not work. |
119 | my $olduid = $>; |
120 | eval { $> = 1; }; |
121 | skip "Can't test -r or -w meaningfully if you're superuser", 2 if $> == 0; |
122 | |
123 | SKIP: { |
124 | skip "Can't test -r meaningfully?", 1 if $Is_Dos || $Is_Cygwin; |
125 | ok(!-r $tmpfile, " -r"); |
126 | } |
127 | |
128 | ok(!-w $tmpfile, " -w"); |
129 | |
130 | # switch uid back (may not be implemented) |
131 | eval { $> = $olduid; }; |
a245ea2d |
132 | } |
cc25fa79 |
133 | |
134 | ok(! -x $tmpfile, ' -x'); |
135 | |
136 | |
137 | # in ms windows, $tmpfile inherits owner uid from directory |
138 | # not sure about os/2, but chown is harmless anyway |
139 | eval { chown $>,$tmpfile; 1 } or print "# $@" ; |
140 | |
141 | ok(chmod(0700,$tmpfile), 'chmod 0700'); |
142 | ok(-r $tmpfile, ' -r'); |
143 | ok(-w $tmpfile, ' -w'); |
144 | |
145 | SKIP: { |
146 | skip "-x simply determins if a file ends in an executable suffix", 1 |
147 | if $Is_Dosish; |
148 | |
149 | ok(-x $tmpfile, ' -x'); |
6eb13c3b |
150 | } |
cc25fa79 |
151 | |
152 | ok( -f $tmpfile, ' -f'); |
153 | ok(! -d $tmpfile, ' !-d'); |
154 | |
155 | # Is this portable? |
156 | ok( -d '.', '-d cwd' ); |
157 | ok(! -f '.', '!-f cwd' ); |
158 | |
159 | SKIP: { |
160 | skip "Test uses unixisms", 1 if $Is_Dosish; |
161 | skip "perl not a symlink", 1 unless `ls -l perl` =~ /^l.*->/; |
162 | |
163 | ok(-l 'perl', '-l'); |
6eb13c3b |
164 | } |
cc25fa79 |
165 | |
166 | ok(-o $tmpfile, '-o'); |
167 | |
168 | ok(-e $tmpfile, '-e'); |
169 | ok(unlink($tmpfile_link), 'unlink'); |
170 | ok(! -e $tmpfile_link, ' -e on unlinked file'); |
171 | |
172 | SKIP: { |
173 | skip "No character special files", 1 |
174 | if $Is_MSWin32 || $Is_NetWare || $Is_Dos; |
175 | skip "No character special files to test against", 1 |
176 | if $Dev_list !~ /\nc.* (\S+)\n/; |
177 | |
178 | ok(-c "/dev/$1", '-c'); |
378cc40b |
179 | } |
cc25fa79 |
180 | ok(! -c '.', '!-c cwd'); |
378cc40b |
181 | |
cc25fa79 |
182 | SKIP: { |
183 | skip "No socket files", 1 if $Is_MSWin32 || $Is_NetWare || $Is_Dos; |
184 | skip "No socket files to test against", 1 |
185 | if $Dev_list !~ /\ns.* (\S+)\n/; |
186 | |
187 | ok(-S "/dev/$1", '-S'); |
378cc40b |
188 | } |
cc25fa79 |
189 | ok(! -S '.', '!-S cwd'); |
190 | |
191 | SKIP: { |
192 | skip "No block files", 1 if $Is_MSWin32 || $Is_NetWare || $Is_Dos; |
193 | skip "No block files to test against", 1 |
194 | if $Dev_list !~ /\nb.* (\S+)\n/; |
195 | |
196 | ok(-b "/dev/$1", '-b'); |
378cc40b |
197 | } |
198 | |
cc25fa79 |
199 | ok(! -b '.', '!-b cwd'); |
200 | |
201 | SKIP: { |
202 | skip "No setuid", 2 if $Is_MPE or $Is_Amiga or $Is_Dosish or $Is_Cygwin; |
b971f6e4 |
203 | |
cc25fa79 |
204 | my($cnt, $uid); |
205 | $cnt = $uid = 0; |
206 | |
207 | # Find a set of directories that's very likely to have setuid files |
208 | # but not likely to be *all* setuid files. |
209 | my @bin = grep {-d && -r && -x} qw(/sbin /usr/sbin /bin /usr/bin); |
210 | skip "Can't find a setuid file to test with", 2 unless @bin; |
211 | |
212 | for my $bin (@bin) { |
213 | opendir BIN, $bin or die "Can't opendir $bin: $!"; |
214 | while (defined($_ = readdir BIN)) { |
215 | $_ = "$bin/$_"; |
216 | $cnt++; |
217 | $uid++ if -u; |
218 | last if $uid && $uid < $cnt; |
219 | } |
220 | } |
221 | closedir BIN; |
222 | |
223 | if( !isnt($uid, 0, 'found some setuid programs') || |
224 | !ok($uid < $cnt, " they're not all setuid") ) |
225 | { |
226 | print <<DIAG; |
227 | # The above two tests assume that at least one of these directories |
228 | # are readable, executable and contain at least one setuid file |
229 | # (but aren't all setuid). |
230 | # @bin |
231 | DIAG |
218d7377 |
232 | } |
378cc40b |
233 | } |
b8440792 |
234 | |
378cc40b |
235 | |
3e3baf6d |
236 | # To assist in automated testing when a controlling terminal (/dev/tty) |
237 | # may not be available (at, cron rsh etc), the PERL_SKIP_TTY_TEST env var |
238 | # can be set to skip the tests that need a tty. |
cc25fa79 |
239 | SKIP: { |
240 | skip "These tests require a TTY", 4 if $ENV{PERL_SKIP_TTY_TEST}; |
241 | |
242 | my $TTY = $^O eq 'rhapsody' ? "/dev/ttyp0" : "/dev/tty"; |
243 | |
244 | SKIP: { |
245 | skip "Test uses unixisms", 2 if $Is_MSWin32 || $Is_NetWare; |
246 | skip "No TTY to test -t with", 2 unless -e $TTY; |
247 | |
248 | open(TTY, $TTY) || |
249 | warn "Can't open $TTY--run t/TEST outside of make.\n"; |
250 | ok(-t TTY, '-t'); |
251 | ok(-c TTY, 'tty is -c'); |
252 | close(TTY); |
3e3baf6d |
253 | } |
cc25fa79 |
254 | ok(! -t TTY, '!-t on closed TTY filehandle'); |
255 | ok(-t, '-t on STDIN'); |
68dc0745 |
256 | } |
cc25fa79 |
257 | |
258 | |
259 | SKIP: { |
260 | skip "No /dev/null to test with", 1 unless -e '/dev/null'; |
261 | |
262 | open(NULL,"/dev/null") or BAIL_OUT("Can't open /dev/null equivalent: $!"); |
263 | ok(! -t NULL, '/dev/null is not a TTY'); |
264 | close(NULL); |
378cc40b |
265 | } |
cc25fa79 |
266 | |
378cc40b |
267 | |
268 | # These aren't strictly "stat" calls, but so what? |
269 | |
cc25fa79 |
270 | ok(-T 'op/stat.t', '-T'); |
271 | ok(! -B 'op/stat.t', '!-B'); |
378cc40b |
272 | |
cc25fa79 |
273 | ok(-B $^X, '-B'); |
274 | ok(! -T $^X, '!-T'); |
378cc40b |
275 | |
f0fcb552 |
276 | open(FOO,'op/stat.t'); |
cc25fa79 |
277 | SKIP: { |
278 | eval { -T FOO; }; |
279 | skip "-T/B on filehandle not implemented", 12 if $@ =~ /not implemented/; |
280 | |
281 | is( $@, '', '-T on filehandle causes no errors' ); |
282 | |
283 | ok(-T FOO, ' -T'); |
284 | ok(! -B FOO, ' !-B'); |
285 | |
f0fcb552 |
286 | $_ = <FOO>; |
cc25fa79 |
287 | ok(/perl/, 'after readline'); |
288 | ok(-T FOO, ' still -T'); |
289 | ok(! -B FOO, ' still -B'); |
f0fcb552 |
290 | close(FOO); |
291 | |
292 | open(FOO,'op/stat.t'); |
293 | $_ = <FOO>; |
cc25fa79 |
294 | ok(/perl/, 'reopened and after readline'); |
295 | ok(-T FOO, ' still -T'); |
296 | ok(! -B FOO, ' still !-B'); |
297 | |
298 | ok(seek(FOO,0,0), 'after seek'); |
299 | ok(-T FOO, ' still -T'); |
300 | ok(! -B FOO, ' still !-B'); |
f0fcb552 |
301 | } |
302 | close(FOO); |
378cc40b |
303 | |
cc25fa79 |
304 | |
305 | ok(-T '/dev/null', '/dev/null is -T'); |
306 | ok(-B '/dev/null', ' and -B'); |
307 | |
108ed793 |
308 | |
309 | # and now, a few parsing tests: |
cc25fa79 |
310 | $_ = $tmpfile; |
311 | ok(-f, 'bare -f uses $_'); |
312 | ok(-f(), ' -f() "'); |
108ed793 |
313 | |
cc25fa79 |
314 | unlink $tmpfile or print "# unlink failed: $!\n"; |
58d95175 |
315 | |
316 | # bug id 20011101.069 |
317 | my @r = \stat("."); |
cc25fa79 |
318 | is(scalar @r, 13, 'stat returns full 13 elements'); |
319 | |