Don't use t/TEST to test a "read only file", repeatedly chmod-ing it a-w.
[p5sagit/p5-mst-13.2.git] / ext / POSIX / t / posix.t
CommitLineData
a0d0e21e 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
a0d0e21e 6 require Config; import Config;
fa6b8193 7 if ($^O ne 'VMS' and $Config{'extensions'} !~ /\bPOSIX\b/) {
c764b42b 8 print "1..0\n";
a0d0e21e 9 exit 0;
10 }
11}
c07a80fd 12
768fd157 13BEGIN { require "./test.pl"; }
2ae48df0 14plan(tests => 66);
e6c299c8 15
212caf55 16use POSIX qw(fcntl_h signal_h limits_h _exit getcwd open read strftime write
17 errno);
e6c299c8 18use strict 'subs';
a0d0e21e 19
c07a80fd 20$| = 1;
a0d0e21e 21
e6c299c8 22$Is_W32 = $^O eq 'MSWin32';
23$Is_Dos = $^O eq 'dos';
24$Is_MPE = $^O eq 'mpeix';
25$Is_MacOS = $^O eq 'MacOS';
26$Is_VMS = $^O eq 'VMS';
27$Is_OS2 = $^O eq 'os2';
28$Is_UWin = $^O eq 'uwin';
29$Is_OS390 = $^O eq 'os390';
6dead956 30
6a164b5b 31my $vms_unix_rpt = 0;
32my $vms_efs = 0;
33my $unix_mode = 1;
34
35if ($Is_VMS) {
36 $unix_mode = 0;
37 if (eval 'require VMS::Feature') {
38 $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
39 $vms_efs = VMS::Feature::current("efs_charset");
40 } else {
41 my $unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
42 my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
43 $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i;
44 $vms_efs = $efs_charset =~ /^[ET1]/i;
45 }
46
47 # Traditional VMS mode only if VMS is not in UNIX compatible mode.
48 $unix_mode = ($vms_efs && $vms_unix_rpt);
49
50}
51
52
e6c299c8 53ok( $testfd = open("TEST", O_RDONLY, 0), 'O_RDONLY with open' );
7814eec4 54read($testfd, $buffer, 4) if $testfd > 2;
55is( $buffer, "#!./", ' with read' );
c07a80fd 56
7814eec4 57TODO:
58{
59 local $TODO = "read to array element not working";
60
61 read($testfd, $buffer[1], 5) if $testfd > 2;
62 is( $buffer[1], "perl\n", ' read to array element' );
63}
64
65write(1,"ok 4\nnot ok 4\n", 5);
e6c299c8 66next_test();
67
68SKIP: {
69 skip("no pipe() support on DOS", 2) if $Is_Dos;
a0d0e21e 70
10de532f 71 @fds = POSIX::pipe();
e6c299c8 72 ok( $fds[0] > $testfd, 'POSIX::pipe' );
73
10de532f 74 CORE::open($reader = \*READER, "<&=".$fds[0]);
75 CORE::open($writer = \*WRITER, ">&=".$fds[1]);
7814eec4 76 print $writer "ok 6\n";
10de532f 77 close $writer;
78 print <$reader>;
79 close $reader;
e6c299c8 80 next_test();
6bbf1b34 81}
a0d0e21e 82
e6c299c8 83SKIP: {
84 skip("no sigaction support on win32/dos", 6) if $Is_W32 || $Is_Dos;
85
86 my $sigset = new POSIX::SigSet 1, 3;
87 $sigset->delset(1);
88 ok(! $sigset->ismember(1), 'POSIX::SigSet->delset' );
89 ok( $sigset->ismember(3), 'POSIX::SigSet->ismember' );
be4e88b6 90
e6c299c8 91 SKIP: {
92 skip("no kill() support on Mac OS", 4) if $Is_MacOS;
93
be4e88b6 94 my $sigint_called = 0;
95
e6c299c8 96 my $mask = new POSIX::SigSet &SIGINT;
97 my $action = new POSIX::SigAction 'main::SigHUP', $mask, 0;
10de532f 98 sigaction(&SIGHUP, $action);
99 $SIG{'INT'} = 'SigINT';
7eb03357 100
101 # At least OpenBSD/i386 3.3 is okay, as is NetBSD 1.5.
102 # But not NetBSD 1.6 & 1.6.1: the test makes perl crash.
103 # So the kill() must not be done with this config in order to
104 # finish the test.
105 # For others (darwin & freebsd), let the test fail without crashing.
106 my $todo = $^O eq 'netbsd' && $Config{osvers}=~/^1\.6/;
e6b15316 107 my $why_todo = "# TODO $^O $Config{osvers} seems to lose blocked signals";
22f20764 108 if (!$todo) {
109 kill 'HUP', $$;
110 } else {
111 print "not ok 9 - sigaction SIGHUP ",$why_todo,"\n";
112 print "not ok 10 - sig mask delayed SIGINT ",$why_todo,"\n";
113 }
10de532f 114 sleep 1;
be4e88b6 115
7eb03357 116 $todo = 1 if ($^O eq 'freebsd')
117 || ($^O eq 'darwin' && $Config{osvers} lt '6.6');
22f20764 118 printf "%s 11 - masked SIGINT received %s\n",
7eb03357 119 $sigint_called ? "ok" : "not ok",
22f20764 120 $todo ? $why_todo : '';
be4e88b6 121
7814eec4 122 print "ok 12 - signal masks successful\n";
10de532f 123
124 sub SigHUP {
7814eec4 125 print "ok 9 - sigaction SIGHUP\n";
10de532f 126 kill 'INT', $$;
127 sleep 2;
7814eec4 128 print "ok 10 - sig mask delayed SIGINT\n";
10de532f 129 }
130
131 sub SigINT {
be4e88b6 132 $sigint_called++;
10de532f 133 }
e6c299c8 134
135 # The order of the above tests is very important, so
136 # we use literal prints and hard coded numbers.
137 next_test() for 1..4;
d536870a 138 }
6dead956 139}
a0d0e21e 140
e6c299c8 141SKIP: {
142 skip("_POSIX_OPEN_MAX is inaccurate on MPE", 1) if $Is_MPE;
143 skip("_POSIX_OPEN_MAX undefined ($fds[1])", 1) unless &_POSIX_OPEN_MAX;
144
e85e3e79 145 ok( &_POSIX_OPEN_MAX >= 16, "The minimum allowed values according to susv2" );
4e0f6e8c 146
c9ff6e92 147}
a0d0e21e 148
d536870a 149my $pat;
150if ($Is_MacOS) {
151 $pat = qr/:t:$/;
e6c299c8 152}
6a164b5b 153elsif ( $unix_mode ) {
154 $pat = qr#[\\/]t$#i;
e6c299c8 155}
156else {
6a164b5b 157 $pat = qr/\.T]/i;
d536870a 158}
e6c299c8 159like( getcwd(), qr/$pat/, 'getcwd' );
a0d0e21e 160
a89d8a78 161# Check string conversion functions.
162
e6c299c8 163SKIP: {
164 skip("strtod() not present", 1) unless $Config{d_strtod};
165
ff68c719 166 $lc = &POSIX::setlocale(&POSIX::LC_NUMERIC, 'C') if $Config{d_setlocale};
e6c299c8 167
168 # we're just checking that strtod works, not how accurate it is
a89d8a78 169 ($n, $x) = &POSIX::strtod('3.14159_OR_SO');
e6c299c8 170 ok((abs("3.14159" - $n) < 1e-6) && ($x == 6), 'strtod works');
171
ff68c719 172 &POSIX::setlocale(&POSIX::LC_NUMERIC, $lc) if $Config{d_setlocale};
e6c299c8 173}
174
175SKIP: {
176 skip("strtol() not present", 2) unless $Config{d_strtol};
a89d8a78 177
a89d8a78 178 ($n, $x) = &POSIX::strtol('21_PENGUINS');
e6c299c8 179 is($n, 21, 'strtol() number');
180 is($x, 9, ' unparsed chars');
181}
182
183SKIP: {
184 skip("strtoul() not present", 2) unless $Config{d_strtoul};
a89d8a78 185
a89d8a78 186 ($n, $x) = &POSIX::strtoul('88_TEARS');
e6c299c8 187 is($n, 88, 'strtoul() number');
188 is($x, 6, ' unparsed chars');
189}
a89d8a78 190
a0d0e21e 191# Pick up whether we're really able to dynamically load everything.
e6c299c8 192ok( &POSIX::acos(1.0) == 0.0, 'dynamic loading' );
a0d0e21e 193
84ef74c4 194# This can coredump if struct tm has a timezone field and we
195# didn't detect it. If this fails, try adding
196# -DSTRUCT_TM_HASZONE to your cflags when compiling ext/POSIX/POSIX.c.
197# See ext/POSIX/hints/sunos_4.pl and ext/POSIX/hints/linux.pl
abbe0d1a 198print POSIX::strftime("ok 21 # %H:%M, on %m/%d/%y\n", localtime());
e6c299c8 199next_test();
84ef74c4 200
33c0e3ec 201# If that worked, validate the mini_mktime() routine's normalisation of
202# input fields to strftime().
203sub try_strftime {
33c0e3ec 204 my $expect = shift;
205 my $got = POSIX::strftime("%a %b %d %H:%M:%S %Y %j", @_);
61a515a6 206 is($got, $expect, "validating mini_mktime() and strftime(): $expect");
33c0e3ec 207}
208
209$lc = &POSIX::setlocale(&POSIX::LC_TIME, 'C') if $Config{d_setlocale};
e6c299c8 210try_strftime("Wed Feb 28 00:00:00 1996 059", 0,0,0, 28,1,96);
53059177 211SKIP: {
abbe0d1a 212 skip("VC++ 8 and Vista's CRTs regard 60 seconds as an invalid parameter", 1)
6fa15125 213 if ($Is_W32 and (($Config{cc} eq 'cl' and
abbe0d1a 214 $Config{ccversion} =~ /^(\d+)/ and $1 >= 14) or
6fa15125 215 (Win32::GetOSVersion())[1] >= 6));
53059177 216
217 try_strftime("Thu Feb 29 00:00:60 1996 060", 60,0,-24, 30,1,96);
218}
e6c299c8 219try_strftime("Fri Mar 01 00:00:00 1996 061", 0,0,-24, 31,1,96);
220try_strftime("Sun Feb 28 00:00:00 1999 059", 0,0,0, 28,1,99);
221try_strftime("Mon Mar 01 00:00:00 1999 060", 0,0,24, 28,1,99);
222try_strftime("Mon Feb 28 00:00:00 2000 059", 0,0,0, 28,1,100);
223try_strftime("Tue Feb 29 00:00:00 2000 060", 0,0,0, 0,2,100);
224try_strftime("Wed Mar 01 00:00:00 2000 061", 0,0,0, 1,2,100);
225try_strftime("Fri Mar 31 00:00:00 2000 091", 0,0,0, 31,2,100);
33c0e3ec 226&POSIX::setlocale(&POSIX::LC_TIME, $lc) if $Config{d_setlocale};
227
212caf55 228{
229 for my $test (0, 1) {
230 $! = 0;
231 # POSIX::errno is autoloaded.
232 # Autoloading requires many system calls.
233 # errno() looks at $! to generate its result.
234 # Autoloading should not munge the value.
235 my $foo = $!;
236 my $errno = POSIX::errno();
e6c299c8 237
e6c299c8 238 # Force numeric context.
239 is( $errno + 0, $foo + 0, 'autoloading and errno() mix' );
212caf55 240 }
241}
242
d4742b2c 243SKIP: {
244 skip("no kill() support on Mac OS", 1) if $Is_MacOS;
245 is (eval "kill 0", 0, "check we have CORE::kill")
246 or print "\$\@ is " . _qq($@) . "\n";
247}
248
249# Check that we can import the POSIX kill routine
250POSIX->import ('kill');
251my $result = eval "kill 0";
252is ($result, undef, "we should now have POSIX::kill");
253# Check usage.
254like ($@, qr/^Usage: POSIX::kill\(pid, sig\)/, "check its usage message");
255
256# Check unimplemented.
257$result = eval {POSIX::offsetof};
258is ($result, undef, "offsetof should fail");
259like ($@, qr/^Unimplemented: POSIX::offsetof\(\) is C-specific/,
260 "check its unimplemented message");
261
262# Check reimplemented.
263$result = eval {POSIX::fgets};
264is ($result, undef, "fgets should fail");
265like ($@, qr/^Use method IO::Handle::gets\(\) instead/,
266 "check its redef message");
267
4b3c6531 268# Simplistic tests for the isXXX() functions (bug #16799)
269ok( POSIX::isalnum('1'), 'isalnum' );
270ok(!POSIX::isalnum('*'), 'isalnum' );
271ok( POSIX::isalpha('f'), 'isalpha' );
272ok(!POSIX::isalpha('7'), 'isalpha' );
273ok( POSIX::iscntrl("\cA"),'iscntrl' );
274ok(!POSIX::iscntrl("A"), 'iscntrl' );
275ok( POSIX::isdigit('1'), 'isdigit' );
276ok(!POSIX::isdigit('z'), 'isdigit' );
277ok( POSIX::isgraph('@'), 'isgraph' );
278ok(!POSIX::isgraph(' '), 'isgraph' );
279ok( POSIX::islower('l'), 'islower' );
280ok(!POSIX::islower('L'), 'islower' );
281ok( POSIX::isupper('U'), 'isupper' );
282ok(!POSIX::isupper('u'), 'isupper' );
283ok( POSIX::isprint('$'), 'isprint' );
284ok(!POSIX::isprint("\n"), 'isprint' );
285ok( POSIX::ispunct('%'), 'ispunct' );
286ok(!POSIX::ispunct('u'), 'ispunct' );
287ok( POSIX::isspace("\t"), 'isspace' );
288ok(!POSIX::isspace('_'), 'isspace' );
289ok( POSIX::isxdigit('f'), 'isxdigit' );
290ok(!POSIX::isxdigit('g'), 'isxdigit' );
117206bb 291# metaphysical question : what should be returned for an empty string ?
292# anyway this shouldn't segfault (bug #24554)
293ok( POSIX::isalnum(''), 'isalnum empty string' );
294ok( POSIX::isalnum(undef),'isalnum undef' );
767bb2e0 295# those functions should stringify their arguments
296ok(!POSIX::isalpha([]), 'isalpha []' );
297ok( POSIX::isprint([]), 'isprint []' );
2ae48df0 298
299eval { use strict; POSIX->import("S_ISBLK"); my $x = S_ISBLK };
300unlike( $@, qr/Can't use string .* as a symbol ref/, "Can import autoloaded constants" );
4b3c6531 301
404d038e 302# Check that output is not flushed by _exit. This test should be last
303# in the file, and is not counted in the total number of tests.
304if ($^O eq 'vos') {
305 print "# TODO - hit VOS bug posix-885 - _exit flushes output buffers.\n";
306} else {
307 $| = 0;
308 # The following line assumes buffered output, which may be not true:
309 print '@#!*$@(!@#$' unless ($Is_MacOS || $Is_OS2 || $Is_UWin || $Is_OS390 ||
e6c299c8 310 $Is_VMS ||
601f2d16 311 (defined $ENV{PERLIO} &&
312 $ENV{PERLIO} eq 'unix' &&
313 $Config::Config{useperlio}));
404d038e 314 _exit(0);
315}