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