This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Croak on unimplemented already at import time
[perl5.git] / ext / POSIX / t / posix.t
CommitLineData
a0d0e21e
LW
1#!./perl
2
3BEGIN {
a0d0e21e 4 require Config; import Config;
fa6b8193 5 if ($^O ne 'VMS' and $Config{'extensions'} !~ /\bPOSIX\b/) {
c764b42b 6 print "1..0\n";
a0d0e21e
LW
7 exit 0;
8 }
12144562 9 unshift @INC, "../../t";
ef9d5242 10 require 'loc_tools.pl';
a0d0e21e 11}
c07a80fd 12
f914a2ba 13use Test::More tests => 93;
e6c299c8 14
212caf55 15use POSIX qw(fcntl_h signal_h limits_h _exit getcwd open read strftime write
1a917639 16 errno localeconv dup dup2 lseek access);
e6c299c8 17use strict 'subs';
a0d0e21e 18
849ddec1
NC
19sub next_test {
20 my $builder = Test::More->builder;
21 $builder->current_test($builder->current_test() + 1);
22}
23
c07a80fd 24$| = 1;
a0d0e21e 25
e6c299c8
JH
26$Is_W32 = $^O eq 'MSWin32';
27$Is_Dos = $^O eq 'dos';
e6c299c8
JH
28$Is_MacOS = $^O eq 'MacOS';
29$Is_VMS = $^O eq 'VMS';
30$Is_OS2 = $^O eq 'os2';
31$Is_UWin = $^O eq 'uwin';
32$Is_OS390 = $^O eq 'os390';
6dead956 33
6a164b5b
JM
34my $vms_unix_rpt = 0;
35my $vms_efs = 0;
36my $unix_mode = 1;
37
38if ($Is_VMS) {
39 $unix_mode = 0;
40 if (eval 'require VMS::Feature') {
41 $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
42 $vms_efs = VMS::Feature::current("efs_charset");
43 } else {
44 my $unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
45 my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
46 $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i;
47 $vms_efs = $efs_charset =~ /^[ET1]/i;
48 }
49
50 # Traditional VMS mode only if VMS is not in UNIX compatible mode.
51 $unix_mode = ($vms_efs && $vms_unix_rpt);
52
53}
54
a572b909
NC
55my $testfd = open("Makefile.PL", O_RDONLY, 0);
56like($testfd, qr/\A\d+\z/, 'O_RDONLY with open');
7814eec4 57read($testfd, $buffer, 4) if $testfd > 2;
2adbc9b6 58is( $buffer, "# Ex", ' with read' );
c07a80fd 59
7814eec4
PM
60TODO:
61{
62 local $TODO = "read to array element not working";
63
64 read($testfd, $buffer[1], 5) if $testfd > 2;
65 is( $buffer[1], "perl\n", ' read to array element' );
66}
67
a572b909
NC
68my $test = next_test();
69write(1,"ok $test\nnot ok $test\n", 5);
e6c299c8
JH
70
71SKIP: {
72 skip("no pipe() support on DOS", 2) if $Is_Dos;
a0d0e21e 73
10de532f 74 @fds = POSIX::pipe();
a572b909 75 cmp_ok($fds[0], '>', $testfd, 'POSIX::pipe');
e6c299c8 76
10de532f
JH
77 CORE::open($reader = \*READER, "<&=".$fds[0]);
78 CORE::open($writer = \*WRITER, ">&=".$fds[1]);
a572b909
NC
79 my $test = next_test();
80 print $writer "ok $test\n";
10de532f
JH
81 close $writer;
82 print <$reader>;
83 close $reader;
6bbf1b34 84}
a0d0e21e 85
e6c299c8
JH
86SKIP: {
87 skip("no sigaction support on win32/dos", 6) if $Is_W32 || $Is_Dos;
88
89 my $sigset = new POSIX::SigSet 1, 3;
90 $sigset->delset(1);
91 ok(! $sigset->ismember(1), 'POSIX::SigSet->delset' );
92 ok( $sigset->ismember(3), 'POSIX::SigSet->ismember' );
be4e88b6 93
e6c299c8
JH
94 SKIP: {
95 skip("no kill() support on Mac OS", 4) if $Is_MacOS;
96
be4e88b6
MS
97 my $sigint_called = 0;
98
e6c299c8
JH
99 my $mask = new POSIX::SigSet &SIGINT;
100 my $action = new POSIX::SigAction 'main::SigHUP', $mask, 0;
10de532f
JH
101 sigaction(&SIGHUP, $action);
102 $SIG{'INT'} = 'SigINT';
7eb03357
AB
103
104 # At least OpenBSD/i386 3.3 is okay, as is NetBSD 1.5.
105 # But not NetBSD 1.6 & 1.6.1: the test makes perl crash.
106 # So the kill() must not be done with this config in order to
107 # finish the test.
108 # For others (darwin & freebsd), let the test fail without crashing.
d188ff5f 109 # the test passes at least from freebsd 8.1
7eb03357 110 my $todo = $^O eq 'netbsd' && $Config{osvers}=~/^1\.6/;
e6b15316 111 my $why_todo = "# TODO $^O $Config{osvers} seems to lose blocked signals";
22f20764
AB
112 if (!$todo) {
113 kill 'HUP', $$;
114 } else {
115 print "not ok 9 - sigaction SIGHUP ",$why_todo,"\n";
116 print "not ok 10 - sig mask delayed SIGINT ",$why_todo,"\n";
117 }
10de532f 118 sleep 1;
be4e88b6 119
d188ff5f 120 $todo = 1 if ($^O eq 'freebsd' && $Config{osvers} < 8)
78385c2e 121 || ($^O eq 'darwin' && $Config{osvers} < '6.6');
22f20764 122 printf "%s 11 - masked SIGINT received %s\n",
7eb03357 123 $sigint_called ? "ok" : "not ok",
22f20764 124 $todo ? $why_todo : '';
be4e88b6 125
7814eec4 126 print "ok 12 - signal masks successful\n";
10de532f
JH
127
128 sub SigHUP {
7814eec4 129 print "ok 9 - sigaction SIGHUP\n";
10de532f
JH
130 kill 'INT', $$;
131 sleep 2;
7814eec4 132 print "ok 10 - sig mask delayed SIGINT\n";
10de532f
JH
133 }
134
135 sub SigINT {
be4e88b6 136 $sigint_called++;
10de532f 137 }
e6c299c8
JH
138
139 # The order of the above tests is very important, so
140 # we use literal prints and hard coded numbers.
141 next_test() for 1..4;
d536870a 142 }
6dead956 143}
a0d0e21e 144
e6c299c8 145SKIP: {
e6c299c8
JH
146 skip("_POSIX_OPEN_MAX undefined ($fds[1])", 1) unless &_POSIX_OPEN_MAX;
147
a572b909
NC
148 cmp_ok(&_POSIX_OPEN_MAX, '>=', 16,
149 "The minimum allowed values according to susv2" );
4e0f6e8c 150
c9ff6e92 151}
a0d0e21e 152
d536870a 153my $pat;
2adbc9b6
NC
154if ( $unix_mode ) {
155 $pat = qr#[\\/]POSIX$#i;
e6c299c8
JH
156}
157else {
2adbc9b6 158 $pat = qr/\.POSIX]/i;
d536870a 159}
e6c299c8 160like( getcwd(), qr/$pat/, 'getcwd' );
a0d0e21e 161
a89d8a78
DH
162# Check string conversion functions.
163
e6c299c8 164SKIP: {
a572b909 165 skip("strtod() not present", 2) unless $Config{d_strtod};
e6c299c8 166
629eeaee 167 if (locales_enabled('LC_NUMERIC')) {
3f1b8f9e
KW
168 $lc = &POSIX::setlocale(&POSIX::LC_NUMERIC);
169 &POSIX::setlocale(&POSIX::LC_NUMERIC, 'C');
170 }
e6c299c8
JH
171
172 # we're just checking that strtod works, not how accurate it is
a89d8a78 173 ($n, $x) = &POSIX::strtod('3.14159_OR_SO');
a572b909
NC
174 cmp_ok(abs("3.14159" - $n), '<', 1e-6, 'strtod works');
175 is($x, 6, 'strtod works');
e6c299c8 176
629eeaee 177 &POSIX::setlocale(&POSIX::LC_NUMERIC, $lc) if locales_enabled('LC_NUMERIC');
e6c299c8
JH
178}
179
180SKIP: {
0ff7b9da
JH
181 skip("strtold() not present", 2) unless $Config{d_strtold};
182
629eeaee 183 if (locales_enabled('LC_NUMERIC')) {
0ff7b9da
JH
184 $lc = &POSIX::setlocale(&POSIX::LC_NUMERIC);
185 &POSIX::setlocale(&POSIX::LC_NUMERIC, 'C');
186 }
187
188 # we're just checking that strtold works, not how accurate it is
189 ($n, $x) = &POSIX::strtod('2.718_ISH');
190 cmp_ok(abs("2.718" - $n), '<', 1e-6, 'strtold works');
191 is($x, 4, 'strtold works');
192
629eeaee 193 &POSIX::setlocale(&POSIX::LC_NUMERIC, $lc) if locales_enabled('LC_NUMERIC');
0ff7b9da
JH
194}
195
196SKIP: {
e6c299c8 197 skip("strtol() not present", 2) unless $Config{d_strtol};
a89d8a78 198
a89d8a78 199 ($n, $x) = &POSIX::strtol('21_PENGUINS');
e6c299c8
JH
200 is($n, 21, 'strtol() number');
201 is($x, 9, ' unparsed chars');
202}
203
204SKIP: {
205 skip("strtoul() not present", 2) unless $Config{d_strtoul};
a89d8a78 206
a89d8a78 207 ($n, $x) = &POSIX::strtoul('88_TEARS');
e6c299c8
JH
208 is($n, 88, 'strtoul() number');
209 is($x, 6, ' unparsed chars');
210}
a89d8a78 211
a0d0e21e 212# Pick up whether we're really able to dynamically load everything.
a572b909 213cmp_ok(&POSIX::acos(1.0), '==', 0.0, 'dynamic loading');
a0d0e21e 214
84ef74c4
AD
215# This can coredump if struct tm has a timezone field and we
216# didn't detect it. If this fails, try adding
217# -DSTRUCT_TM_HASZONE to your cflags when compiling ext/POSIX/POSIX.c.
218# See ext/POSIX/hints/sunos_4.pl and ext/POSIX/hints/linux.pl
a572b909
NC
219$test = next_test();
220print POSIX::strftime("ok $test # %H:%M, on %m/%d/%y\n", localtime());
84ef74c4 221
33c0e3ec
SB
222# If that worked, validate the mini_mktime() routine's normalisation of
223# input fields to strftime().
224sub try_strftime {
33c0e3ec
SB
225 my $expect = shift;
226 my $got = POSIX::strftime("%a %b %d %H:%M:%S %Y %j", @_);
61a515a6 227 is($got, $expect, "validating mini_mktime() and strftime(): $expect");
33c0e3ec
SB
228}
229
629eeaee 230if (locales_enabled('LC_TIME')) {
3f1b8f9e
KW
231 $lc = &POSIX::setlocale(&POSIX::LC_TIME);
232 &POSIX::setlocale(&POSIX::LC_TIME, 'C');
233}
234
e6c299c8 235try_strftime("Wed Feb 28 00:00:00 1996 059", 0,0,0, 28,1,96);
53059177 236SKIP: {
abbe0d1a 237 skip("VC++ 8 and Vista's CRTs regard 60 seconds as an invalid parameter", 1)
a48cc4c4
DD
238 if ($Is_W32
239 and (($Config{cc} eq 'cl' and
240 $Config{ccversion} =~ /^(\d+)/ and $1 >= 14)
241 or ($Config{cc} eq 'icl' and
242 `cl --version 2>&1` =~ /^.*Version\s+([\d.]+)/ and $1 >= 14)
243 or (Win32::GetOSVersion())[1] >= 6));
53059177
SH
244
245 try_strftime("Thu Feb 29 00:00:60 1996 060", 60,0,-24, 30,1,96);
246}
e6c299c8
JH
247try_strftime("Fri Mar 01 00:00:00 1996 061", 0,0,-24, 31,1,96);
248try_strftime("Sun Feb 28 00:00:00 1999 059", 0,0,0, 28,1,99);
249try_strftime("Mon Mar 01 00:00:00 1999 060", 0,0,24, 28,1,99);
250try_strftime("Mon Feb 28 00:00:00 2000 059", 0,0,0, 28,1,100);
251try_strftime("Tue Feb 29 00:00:00 2000 060", 0,0,0, 0,2,100);
252try_strftime("Wed Mar 01 00:00:00 2000 061", 0,0,0, 1,2,100);
253try_strftime("Fri Mar 31 00:00:00 2000 091", 0,0,0, 31,2,100);
a64f08cb
TC
254
255{ # rt 72232
256
257 # Std C/POSIX allows day/month to be negative and requires that
258 # wday/yday be adjusted as needed
259 # previously mini_mktime() would allow yday to dominate if mday and
260 # month were both non-positive
261 # check that yday doesn't dominate
262 try_strftime("Thu Dec 30 00:00:00 1999 364", 0,0,0, -1,0,100);
263 try_strftime("Thu Dec 30 00:00:00 1999 364", 0,0,0, -1,0,100,-1,10);
264 # it would also allow a positive wday to override the calculated value
265 # check that wday is recalculated too
266 try_strftime("Thu Dec 30 00:00:00 1999 364", 0,0,0, -1,0,100,0,10);
267}
268
629eeaee 269&POSIX::setlocale(&POSIX::LC_TIME, $lc) if locales_enabled('LC_TIME');
33c0e3ec 270
212caf55
TS
271{
272 for my $test (0, 1) {
273 $! = 0;
274 # POSIX::errno is autoloaded.
275 # Autoloading requires many system calls.
276 # errno() looks at $! to generate its result.
277 # Autoloading should not munge the value.
278 my $foo = $!;
279 my $errno = POSIX::errno();
e6c299c8 280
e6c299c8
JH
281 # Force numeric context.
282 is( $errno + 0, $foo + 0, 'autoloading and errno() mix' );
212caf55
TS
283 }
284}
285
d4742b2c
NC
286SKIP: {
287 skip("no kill() support on Mac OS", 1) if $Is_MacOS;
288 is (eval "kill 0", 0, "check we have CORE::kill")
289 or print "\$\@ is " . _qq($@) . "\n";
290}
291
292# Check that we can import the POSIX kill routine
293POSIX->import ('kill');
294my $result = eval "kill 0";
295is ($result, undef, "we should now have POSIX::kill");
296# Check usage.
297like ($@, qr/^Usage: POSIX::kill\(pid, sig\)/, "check its usage message");
298
299# Check unimplemented.
300$result = eval {POSIX::offsetof};
301is ($result, undef, "offsetof should fail");
19fc2965 302like ($@, qr/^Unimplemented: POSIX::offsetof\(\): C-specific/,
d4742b2c
NC
303 "check its unimplemented message");
304
305# Check reimplemented.
306$result = eval {POSIX::fgets};
307is ($result, undef, "fgets should fail");
19fc2965 308like ($@, qr/^Unimplemented: POSIX::fgets\(\): Use method IO::Handle::gets\(\) instead/,
d4742b2c
NC
309 "check its redef message");
310
2ae48df0
RGS
311eval { use strict; POSIX->import("S_ISBLK"); my $x = S_ISBLK };
312unlike( $@, qr/Can't use string .* as a symbol ref/, "Can import autoloaded constants" );
4a948f3f
NC
313
314SKIP: {
ef9d5242
KW
315 skip("locales not available", 26) unless locales_enabled(qw(NUMERIC MONETARY));
316 skip("localeconv() not available", 26) unless $Config{d_locconv};
4a948f3f
NC
317 my $conv = localeconv;
318 is(ref $conv, 'HASH', 'localconv returns a hash reference');
319
320 foreach (qw(decimal_point thousands_sep grouping int_curr_symbol
321 currency_symbol mon_decimal_point mon_thousands_sep
322 mon_grouping positive_sign negative_sign)) {
323 SKIP: {
324 skip("localeconv has no result for $_", 1)
325 unless exists $conv->{$_};
326 unlike(delete $conv->{$_}, qr/\A\z/,
327 "localeconv returned a non-empty string for $_");
328 }
329 }
330
b15c1b56
AF
331 my @lconv = qw(
332 int_frac_digits frac_digits
333 p_cs_precedes p_sep_by_space
334 n_cs_precedes n_sep_by_space
335 p_sign_posn n_sign_posn
336 );
337
338 SKIP: {
339 skip('No HAS_LC_MONETARY_2008', 6) unless $Config{d_lc_monetary_2008};
340
341 push @lconv, qw(
342 int_p_cs_precedes int_p_sep_by_space
343 int_n_cs_precedes int_n_sep_by_space
344 int_p_sign_posn int_n_sign_posn
345 );
346 }
347
348 foreach (@lconv) {
4a948f3f
NC
349 SKIP: {
350 skip("localeconv has no result for $_", 1)
351 unless exists $conv->{$_};
352 like(delete $conv->{$_}, qr/\A-?\d+\z/,
353 "localeconv returned an integer for $_");
354 }
355 }
356 is_deeply([%$conv], [], 'no unexpected keys returned by localeconv');
357}
358
792480b6
NC
359my $fd1 = open("Makefile.PL", O_RDONLY, 0);
360like($fd1, qr/\A\d+\z/, 'O_RDONLY with open');
361cmp_ok($fd1, '>', $testfd);
362my $fd2 = dup($fd1);
363like($fd2, qr/\A\d+\z/, 'dup');
364cmp_ok($fd2, '>', $fd1);
365is(POSIX::close($fd1), '0 but true', 'close');
366is(POSIX::close($testfd), '0 but true', 'close');
367$! = 0;
368undef $buffer;
369is(read($fd1, $buffer, 4), undef, 'read on closed file handle fails');
370cmp_ok($!, '==', POSIX::EBADF);
371undef $buffer;
372read($fd2, $buffer, 4) if $fd2 > 2;
373is($buffer, "# Ex", 'read');
374# The descriptor $testfd was using is now free, and is lower than that which
375# $fd1 was using. Hence if dup2() behaves as dup(), we'll know :-)
376{
792480b6
NC
377 $testfd = dup2($fd2, $fd1);
378 is($testfd, $fd1, 'dup2');
379 undef $buffer;
380 read($testfd, $buffer, 4) if $testfd > 2;
381 is($buffer, 'pect', 'read');
382 is(lseek($testfd, 0, 0), 0, 'lseek back');
383 # The two should share file position:
384 undef $buffer;
385 read($fd2, $buffer, 4) if $fd2 > 2;
386 is($buffer, "# Ex", 'read');
387}
388
389# The FreeBSD man page warns:
390# The access() system call is a potential security hole due to race
391# conditions and should never be used.
392is(access('Makefile.PL', POSIX::F_OK), '0 but true', 'access');
393is(access('Makefile.PL', POSIX::R_OK), '0 but true', 'access');
394$! = 0;
395is(access('no such file', POSIX::F_OK), undef, 'access on missing file');
396cmp_ok($!, '==', POSIX::ENOENT);
397is(access('Makefile.PL/nonsense', POSIX::F_OK), undef,
398 'access on not-a-directory');
399SKIP: {
400 skip("$^O is insufficiently POSIX", 1)
e5da71f2 401 if $Is_W32 || $Is_VMS;
792480b6
NC
402 cmp_ok($!, '==', POSIX::ENOTDIR);
403}
404
f914a2ba
JH
405{ # tmpnam() has been removed as unsafe
406 my $x = eval { POSIX::tmpnam() };
407 is($x, undef, 'tmpnam has been removed');
408 like($@, qr/use File::Temp/, 'tmpnam advises File::Temp');
cae71c5d
TC
409}
410
404d038e
PG
411# Check that output is not flushed by _exit. This test should be last
412# in the file, and is not counted in the total number of tests.
413if ($^O eq 'vos') {
414 print "# TODO - hit VOS bug posix-885 - _exit flushes output buffers.\n";
415} else {
416 $| = 0;
417 # The following line assumes buffered output, which may be not true:
418 print '@#!*$@(!@#$' unless ($Is_MacOS || $Is_OS2 || $Is_UWin || $Is_OS390 ||
e6c299c8 419 $Is_VMS ||
601f2d16
SB
420 (defined $ENV{PERLIO} &&
421 $ENV{PERLIO} eq 'unix' &&
422 $Config::Config{useperlio}));
404d038e
PG
423 _exit(0);
424}