dslinux/user/perl/ext/POSIX/t is.t posix.t sigaction.t taint.t waitpid.t
cayenne
dslinux_cayenne at user.in-berlin.de
Mon Dec 4 17:59:39 CET 2006
Update of /cvsroot/dslinux/dslinux/user/perl/ext/POSIX/t
In directory antilope:/tmp/cvs-serv17422/ext/POSIX/t
Added Files:
is.t posix.t sigaction.t taint.t waitpid.t
Log Message:
Adding fresh perl source to HEAD to branch from
--- NEW FILE: taint.t ---
#!./perl -Tw
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require Config; import Config;
if ($^O ne 'VMS' and $Config{'extensions'} !~ /\bPOSIX\b/) {
print "1..0\n";
exit 0;
}
}
use Test::More tests => 7;
use Scalar::Util qw/tainted/;
use POSIX qw(fcntl_h open read mkfifo);
use strict ;
$| = 1;
my $buffer;
my @buffer;
my $testfd;
# Sources of taint:
# The empty tainted value, for tainting strings
my $TAINT = substr($^X, 0, 0);
my $file = 'TEST';
eval { mkfifo($TAINT. $file, 0) };
like($@, qr/^Insecure dependency/, 'mkfifo with tainted data');
eval { $testfd = open($TAINT. $file, O_WRONLY, 0) };
like($@, qr/^Insecure dependency/, 'open with tainted data');
eval { $testfd = open($file, O_RDONLY, 0) };
is($@, "", 'open with untainted data');
read($testfd, $buffer, 2) if $testfd > 2;
is( $buffer, "#!", ' read' );
ok(tainted($buffer), ' scalar tainted');
TODO: {
local $TODO = "POSIX::read won't taint an array element";
read($testfd, $buffer[1], 2) if $testfd > 2;
is( $buffer[1], "./", ' read' );
ok(tainted($buffer[1]), ' array element tainted');
}
--- NEW FILE: is.t ---
#!./perl -w
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require Config; import Config;
if ($^O ne 'VMS' and $Config{'extensions'} !~ /\bPOSIX\b/) {
print "1..0\n";
exit 0;
}
}
use POSIX;
use strict ;
# E.g. \t might or might not be isprint() depending on the locale,
# so let's reset to the default.
setlocale(LC_ALL, 'C') if $Config{d_setlocale};
$| = 1;
# List of characters (and strings) to feed to the is<xxx> functions.
#
# The left-hand side (key) is a character or string.
# The right-hand side (value) is a list of character classes to which
# this string belongs. This is a *complete* list: any classes not
# listed, are expected to return '0' for the given string.
my %classes =
(
'a' => [ qw(print graph alnum alpha lower xdigit) ],
'A' => [ qw(print graph alnum alpha upper xdigit) ],
'z' => [ qw(print graph alnum alpha lower) ],
'Z' => [ qw(print graph alnum alpha upper) ],
'0' => [ qw(print graph alnum digit xdigit) ],
'9' => [ qw(print graph alnum digit xdigit) ],
'.' => [ qw(print graph punct) ],
'?' => [ qw(print graph punct) ],
' ' => [ qw(print space) ],
"\t" => [ qw(cntrl space) ],
"\001" => [ qw(cntrl) ],
# Multi-character strings. These are logically ANDed, so the
# presence of different types of chars in one string will
# reduce the list on the right.
'abc' => [ qw(print graph alnum alpha lower xdigit) ],
'az' => [ qw(print graph alnum alpha lower) ],
'aZ' => [ qw(print graph alnum alpha) ],
'abc ' => [ qw(print) ],
'012aF' => [ qw(print graph alnum xdigit) ],
" \t" => [ qw(space) ],
"abcde\001" => [],
# An empty string. Always true (al least in old days) [bug #24554]
'' => [ qw(print graph alnum alpha lower upper digit xdigit
punct cntrl space) ],
);
# Pass 1: convert the above arrays to hashes. While doing so, obtain
# a complete list of all the 'is<xxx>' functions. At least, the ones
# listed above.
my %functions;
foreach my $s (keys %classes) {
$classes{$s} = { map {
$functions{"is$_"}++; # Keep track of all the 'is<xxx>' functions
"is$_" => 1; # Our return value: is<xxx>($s) should pass.
} @{$classes{$s}} };
}
# Expected number of tests is one each for every combination of a
# known is<xxx> function and string listed above.
require './test.pl';
plan(tests => keys(%classes) * keys(%functions));
#
# Main test loop: Run all POSIX::is<xxx> tests on each string defined above.
# Only the character classes listed for that string should return 1. We
# always run all functions on every string, and expect to get 0 for the
# character classes not listed in the given string's hash value.
#
foreach my $s (sort keys %classes) {
foreach my $f (sort keys %functions) {
my $expected = exists $classes{$s}->{$f};
my $actual = eval "POSIX::$f( \$s )";
ok( $actual == $expected, "$f('$s') == $actual");
}
}
--- NEW FILE: sigaction.t ---
#!./perl
BEGIN {
chdir 't' if -d 't';
unshift @INC, '../lib';
}
BEGIN{
# Don't do anything if POSIX is missing, or sigaction missing.
use Config;
eval 'use POSIX';
if($@ || $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'dos' ||
$^O eq 'MacOS' || ($^O eq 'VMS' && !$Config{'d_sigaction'})) {
print "1..0\n";
exit 0;
}
}
use strict;
use vars qw/$bad7 $ok10 $bad18 $ok/;
$^W=1;
print "1..25\n";
sub IGNORE {
$bad7=1;
}
sub DEFAULT {
$bad18=1;
}
sub foo {
$ok=1;
}
my $newaction=POSIX::SigAction->new('::foo', new POSIX::SigSet(SIGUSR1), 0);
my $oldaction=POSIX::SigAction->new('::bar', new POSIX::SigSet(), 0);
{
my $bad;
local($SIG{__WARN__})=sub { $bad=1; };
sigaction(SIGHUP, $newaction, $oldaction);
if($bad) { print "not ok 1\n" } else { print "ok 1\n"}
}
if($oldaction->{HANDLER} eq 'DEFAULT' ||
$oldaction->{HANDLER} eq 'IGNORE')
{ print "ok 2\n" } else { print "not ok 2 # ", $oldaction->{HANDLER}, "\n"}
print $SIG{HUP} eq '::foo' ? "ok 3\n" : "not ok 3\n";
sigaction(SIGHUP, $newaction, $oldaction);
if($oldaction->{HANDLER} eq '::foo')
{ print "ok 4\n" } else { print "not ok 4\n"}
if($oldaction->{MASK}->ismember(SIGUSR1))
{ print "ok 5\n" } else { print "not ok 5\n"}
if($oldaction->{FLAGS}) {
if ($^O eq 'linux' || $^O eq 'unicos') {
print "ok 6 # Skip: sigaction() thinks different in $^O\n";
} else {
print "not ok 6\n";
}
} else {
print "ok 6\n";
}
$newaction=POSIX::SigAction->new('IGNORE');
sigaction(SIGHUP, $newaction);
kill 'HUP', $$;
print $bad7 ? "not ok 7\n" : "ok 7\n";
print $SIG{HUP} eq 'IGNORE' ? "ok 8\n" : "not ok 8\n";
sigaction(SIGHUP, POSIX::SigAction->new('DEFAULT'));
print $SIG{HUP} eq 'DEFAULT' ? "ok 9\n" : "not ok 9\n";
$newaction=POSIX::SigAction->new(sub { $ok10=1; });
sigaction(SIGHUP, $newaction);
{
local($^W)=0;
kill 'HUP', $$;
}
print $ok10 ? "ok 10\n" : "not ok 10\n";
print ref($SIG{HUP}) eq 'CODE' ? "ok 11\n" : "not ok 11\n";
sigaction(SIGHUP, POSIX::SigAction->new('::foo'));
# Make sure the signal mask gets restored after sigaction croak()s.
eval {
my $act=POSIX::SigAction->new('::foo');
delete $act->{HANDLER};
sigaction(SIGINT, $act);
};
kill 'HUP', $$;
print $ok ? "ok 12\n" : "not ok 12\n";
undef $ok;
# Make sure the signal mask gets restored after sigaction returns early.
my $x=defined sigaction(SIGKILL, $newaction, $oldaction);
kill 'HUP', $$;
print !$x && $ok ? "ok 13\n" : "not ok 13\n";
$SIG{HUP}=sub {};
sigaction(SIGHUP, $newaction, $oldaction);
print ref($oldaction->{HANDLER}) eq 'CODE' ? "ok 14\n" : "not ok 14\n";
eval {
sigaction(SIGHUP, undef, $oldaction);
};
print $@ ? "not ok 15\n" : "ok 15\n";
eval {
sigaction(SIGHUP, 0, $oldaction);
};
print $@ ? "not ok 16\n" : "ok 16\n";
eval {
sigaction(SIGHUP, bless({},'Class'), $oldaction);
};
print $@ ? "ok 17\n" : "not ok 17\n";
if ($^O eq 'VMS') {
print "ok 18 # Skip: SIGCONT not trappable in $^O\n";
} else {
$newaction=POSIX::SigAction->new(sub { $ok10=1; });
if (eval { SIGCONT; 1 }) {
sigaction(SIGCONT, POSIX::SigAction->new('DEFAULT'));
{
local($^W)=0;
kill 'CONT', $$;
}
}
print $bad18 ? "not ok 18\n" : "ok 18\n";
}
{
local $SIG{__WARN__} = sub { }; # Just suffer silently.
my $hup20;
my $hup21;
sub hup20 { $hup20++ }
sub hup21 { $hup21++ }
sigaction("FOOBAR", $newaction);
print "ok 19\n"; # no coredump, still alive
$newaction = POSIX::SigAction->new("hup20");
sigaction("SIGHUP", $newaction);
kill "HUP", $$;
print $hup20 == 1 ? "ok 20\n" : "not ok 20\n";
$newaction = POSIX::SigAction->new("hup21");
sigaction("HUP", $newaction);
kill "HUP", $$;
print $hup21 == 1 ? "ok 21\n" : "not ok 21\n";
}
# "safe" attribute.
# for this one, use the accessor instead of the attribute
# standard signal handling via %SIG is safe
$SIG{HUP} = \&foo;
$oldaction = POSIX::SigAction->new;
sigaction(SIGHUP, undef, $oldaction);
print $oldaction->safe ? "ok 22\n" : "not ok 22\n";
# SigAction handling is not safe ...
sigaction(SIGHUP, POSIX::SigAction->new(\&foo));
sigaction(SIGHUP, undef, $oldaction);
print $oldaction->safe ? "not ok 23\n" : "ok 23\n";
# ... unless we say so!
$newaction = POSIX::SigAction->new(\&foo);
$newaction->safe(1);
sigaction(SIGHUP, $newaction);
sigaction(SIGHUP, undef, $oldaction);
print $oldaction->safe ? "ok 24\n" : "not ok 24\n";
# And safe signal delivery must work
$ok = 0;
kill 'HUP', $$;
print $ok ? "ok 25\n" : "not ok 25\n";
--- NEW FILE: waitpid.t ---
BEGIN {
chdir 't' if -d 't';
unshift @INC, '../lib';
}
BEGIN {
use Config;
unless ($Config{d_fork}) {
print "1..0 # Skip: no fork\n";
exit 0;
}
eval 'use POSIX qw(sys_wait_h)';
if ($@) {
print "1..0 # Skip: no POSIX sys_wait_h\n";
exit 0;
}
eval 'use Time::HiRes qw(time)';
if ($@) {
print "1..0 # Skip: no Time::HiRes\n";
exit 0;
}
}
use warnings;
use strict;
$| = 1;
print "1..1\n";
sub NEG1_PROHIBITED () { 0x01 }
sub NEG1_REQUIRED () { 0x02 }
my $count = 0;
my $max_count = 9;
my $state = NEG1_PROHIBITED;
my $child_pid = fork();
# Parent receives a nonzero child PID.
if ($child_pid) {
my $ok = 1;
while ($count++ < $max_count) {
my $begin_time = time();
my $ret = waitpid( -1, WNOHANG );
my $elapsed_time = time() - $begin_time;
printf( "# waitpid(-1,WNOHANG) returned %d after %.2f seconds\n",
$ret, $elapsed_time );
if ($elapsed_time > 0.5) {
printf( "# %.2f seconds in non-blocking waitpid is too long!\n",
$elapsed_time );
$ok = 0;
last;
}
if ($state & NEG1_PROHIBITED) {
if ($ret == -1) {
print "# waitpid should not have returned -1 here!\n";
$ok = 0;
last;
}
elsif ($ret == $child_pid) {
$state = NEG1_REQUIRED;
}
}
elsif ($state & NEG1_REQUIRED) {
unless ($ret == -1) {
print "# waitpid should have returned -1 here\n";
$ok = 0;
}
last;
}
sleep(1);
}
print $ok ? "ok 1\n" : "not ok 1\n";
exit(0); # parent
} else {
# Child receives a zero PID and can request parent's PID with
# getppid().
sleep(3);
exit(0);
}
--- NEW FILE: posix.t ---
#!./perl
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require Config; import Config;
if ($^O ne 'VMS' and $Config{'extensions'} !~ /\bPOSIX\b/) {
print "1..0\n";
exit 0;
}
}
require "./test.pl";
plan(tests => 65);
use POSIX qw(fcntl_h signal_h limits_h _exit getcwd open read strftime write
errno);
use strict 'subs';
$| = 1;
$Is_W32 = $^O eq 'MSWin32';
$Is_Dos = $^O eq 'dos';
$Is_MPE = $^O eq 'mpeix';
$Is_MacOS = $^O eq 'MacOS';
$Is_VMS = $^O eq 'VMS';
$Is_OS2 = $^O eq 'os2';
$Is_UWin = $^O eq 'uwin';
$Is_OS390 = $^O eq 'os390';
ok( $testfd = open("TEST", O_RDONLY, 0), 'O_RDONLY with open' );
read($testfd, $buffer, 4) if $testfd > 2;
is( $buffer, "#!./", ' with read' );
TODO:
{
local $TODO = "read to array element not working";
read($testfd, $buffer[1], 5) if $testfd > 2;
is( $buffer[1], "perl\n", ' read to array element' );
}
write(1,"ok 4\nnot ok 4\n", 5);
next_test();
SKIP: {
skip("no pipe() support on DOS", 2) if $Is_Dos;
@fds = POSIX::pipe();
ok( $fds[0] > $testfd, 'POSIX::pipe' );
CORE::open($reader = \*READER, "<&=".$fds[0]);
CORE::open($writer = \*WRITER, ">&=".$fds[1]);
print $writer "ok 6\n";
close $writer;
print <$reader>;
close $reader;
next_test();
}
SKIP: {
skip("no sigaction support on win32/dos", 6) if $Is_W32 || $Is_Dos;
my $sigset = new POSIX::SigSet 1, 3;
$sigset->delset(1);
ok(! $sigset->ismember(1), 'POSIX::SigSet->delset' );
ok( $sigset->ismember(3), 'POSIX::SigSet->ismember' );
SKIP: {
skip("no kill() support on Mac OS", 4) if $Is_MacOS;
my $sigint_called = 0;
my $mask = new POSIX::SigSet &SIGINT;
my $action = new POSIX::SigAction 'main::SigHUP', $mask, 0;
sigaction(&SIGHUP, $action);
$SIG{'INT'} = 'SigINT';
# At least OpenBSD/i386 3.3 is okay, as is NetBSD 1.5.
# But not NetBSD 1.6 & 1.6.1: the test makes perl crash.
# So the kill() must not be done with this config in order to
# finish the test.
# For others (darwin & freebsd), let the test fail without crashing.
my $todo = $^O eq 'netbsd' && $Config{osvers}=~/^1\.6/;
my $why_todo = "# TODO $^O $Config{osvers} seems to loose blocked signals";
if (!$todo) {
kill 'HUP', $$;
} else {
print "not ok 9 - sigaction SIGHUP ",$why_todo,"\n";
print "not ok 10 - sig mask delayed SIGINT ",$why_todo,"\n";
}
sleep 1;
$todo = 1 if ($^O eq 'freebsd')
|| ($^O eq 'darwin' && $Config{osvers} lt '6.6');
printf "%s 11 - masked SIGINT received %s\n",
$sigint_called ? "ok" : "not ok",
$todo ? $why_todo : '';
print "ok 12 - signal masks successful\n";
sub SigHUP {
print "ok 9 - sigaction SIGHUP\n";
kill 'INT', $$;
sleep 2;
print "ok 10 - sig mask delayed SIGINT\n";
}
sub SigINT {
$sigint_called++;
}
# The order of the above tests is very important, so
# we use literal prints and hard coded numbers.
next_test() for 1..4;
}
}
SKIP: {
skip("_POSIX_OPEN_MAX is inaccurate on MPE", 1) if $Is_MPE;
skip("_POSIX_OPEN_MAX undefined ($fds[1])", 1) unless &_POSIX_OPEN_MAX;
ok( &_POSIX_OPEN_MAX >= 16, "The minimum allowed values according to susv2" );
}
my $pat;
if ($Is_MacOS) {
$pat = qr/:t:$/;
}
elsif ( $Is_VMS ) {
$pat = qr/\.T]/i;
}
else {
$pat = qr#[\\/]t$#i;
}
like( getcwd(), qr/$pat/, 'getcwd' );
# Check string conversion functions.
SKIP: {
skip("strtod() not present", 1) unless $Config{d_strtod};
$lc = &POSIX::setlocale(&POSIX::LC_NUMERIC, 'C') if $Config{d_setlocale};
# we're just checking that strtod works, not how accurate it is
($n, $x) = &POSIX::strtod('3.14159_OR_SO');
ok((abs("3.14159" - $n) < 1e-6) && ($x == 6), 'strtod works');
&POSIX::setlocale(&POSIX::LC_NUMERIC, $lc) if $Config{d_setlocale};
}
SKIP: {
skip("strtol() not present", 2) unless $Config{d_strtol};
($n, $x) = &POSIX::strtol('21_PENGUINS');
is($n, 21, 'strtol() number');
is($x, 9, ' unparsed chars');
}
SKIP: {
skip("strtoul() not present", 2) unless $Config{d_strtoul};
($n, $x) = &POSIX::strtoul('88_TEARS');
is($n, 88, 'strtoul() number');
is($x, 6, ' unparsed chars');
}
# Pick up whether we're really able to dynamically load everything.
ok( &POSIX::acos(1.0) == 0.0, 'dynamic loading' );
# This can coredump if struct tm has a timezone field and we
# didn't detect it. If this fails, try adding
# -DSTRUCT_TM_HASZONE to your cflags when compiling ext/POSIX/POSIX.c.
# See ext/POSIX/hints/sunos_4.pl and ext/POSIX/hints/linux.pl
print POSIX::strftime("ok 21 # %H:%M, on %D\n", localtime());
next_test();
# If that worked, validate the mini_mktime() routine's normalisation of
# input fields to strftime().
sub try_strftime {
my $expect = shift;
my $got = POSIX::strftime("%a %b %d %H:%M:%S %Y %j", @_);
is($got, $expect, "validating mini_mktime() and strftime(): $expect");
}
$lc = &POSIX::setlocale(&POSIX::LC_TIME, 'C') if $Config{d_setlocale};
try_strftime("Wed Feb 28 00:00:00 1996 059", 0,0,0, 28,1,96);
try_strftime("Thu Feb 29 00:00:60 1996 060", 60,0,-24, 30,1,96);
try_strftime("Fri Mar 01 00:00:00 1996 061", 0,0,-24, 31,1,96);
try_strftime("Sun Feb 28 00:00:00 1999 059", 0,0,0, 28,1,99);
try_strftime("Mon Mar 01 00:00:00 1999 060", 0,0,24, 28,1,99);
try_strftime("Mon Feb 28 00:00:00 2000 059", 0,0,0, 28,1,100);
try_strftime("Tue Feb 29 00:00:00 2000 060", 0,0,0, 0,2,100);
try_strftime("Wed Mar 01 00:00:00 2000 061", 0,0,0, 1,2,100);
try_strftime("Fri Mar 31 00:00:00 2000 091", 0,0,0, 31,2,100);
&POSIX::setlocale(&POSIX::LC_TIME, $lc) if $Config{d_setlocale};
{
for my $test (0, 1) {
$! = 0;
# POSIX::errno is autoloaded.
# Autoloading requires many system calls.
# errno() looks at $! to generate its result.
# Autoloading should not munge the value.
my $foo = $!;
my $errno = POSIX::errno();
# Force numeric context.
is( $errno + 0, $foo + 0, 'autoloading and errno() mix' );
}
}
SKIP: {
skip("no kill() support on Mac OS", 1) if $Is_MacOS;
is (eval "kill 0", 0, "check we have CORE::kill")
or print "\$\@ is " . _qq($@) . "\n";
}
# Check that we can import the POSIX kill routine
POSIX->import ('kill');
my $result = eval "kill 0";
is ($result, undef, "we should now have POSIX::kill");
# Check usage.
like ($@, qr/^Usage: POSIX::kill\(pid, sig\)/, "check its usage message");
# Check unimplemented.
$result = eval {POSIX::offsetof};
is ($result, undef, "offsetof should fail");
like ($@, qr/^Unimplemented: POSIX::offsetof\(\) is C-specific/,
"check its unimplemented message");
# Check reimplemented.
$result = eval {POSIX::fgets};
is ($result, undef, "fgets should fail");
like ($@, qr/^Use method IO::Handle::gets\(\) instead/,
"check its redef message");
# Simplistic tests for the isXXX() functions (bug #16799)
ok( POSIX::isalnum('1'), 'isalnum' );
ok(!POSIX::isalnum('*'), 'isalnum' );
ok( POSIX::isalpha('f'), 'isalpha' );
ok(!POSIX::isalpha('7'), 'isalpha' );
ok( POSIX::iscntrl("\cA"),'iscntrl' );
ok(!POSIX::iscntrl("A"), 'iscntrl' );
ok( POSIX::isdigit('1'), 'isdigit' );
ok(!POSIX::isdigit('z'), 'isdigit' );
ok( POSIX::isgraph('@'), 'isgraph' );
ok(!POSIX::isgraph(' '), 'isgraph' );
ok( POSIX::islower('l'), 'islower' );
ok(!POSIX::islower('L'), 'islower' );
ok( POSIX::isupper('U'), 'isupper' );
ok(!POSIX::isupper('u'), 'isupper' );
ok( POSIX::isprint('$'), 'isprint' );
ok(!POSIX::isprint("\n"), 'isprint' );
ok( POSIX::ispunct('%'), 'ispunct' );
ok(!POSIX::ispunct('u'), 'ispunct' );
ok( POSIX::isspace("\t"), 'isspace' );
ok(!POSIX::isspace('_'), 'isspace' );
ok( POSIX::isxdigit('f'), 'isxdigit' );
ok(!POSIX::isxdigit('g'), 'isxdigit' );
# metaphysical question : what should be returned for an empty string ?
# anyway this shouldn't segfault (bug #24554)
ok( POSIX::isalnum(''), 'isalnum empty string' );
ok( POSIX::isalnum(undef),'isalnum undef' );
# those functions should stringify their arguments
ok(!POSIX::isalpha([]), 'isalpha []' );
ok( POSIX::isprint([]), 'isprint []' );
# Check that output is not flushed by _exit. This test should be last
# in the file, and is not counted in the total number of tests.
if ($^O eq 'vos') {
print "# TODO - hit VOS bug posix-885 - _exit flushes output buffers.\n";
} else {
$| = 0;
# The following line assumes buffered output, which may be not true:
print '@#!*$@(!@#$' unless ($Is_MacOS || $Is_OS2 || $Is_UWin || $Is_OS390 ||
$Is_VMS ||
(defined $ENV{PERLIO} &&
$ENV{PERLIO} eq 'unix' &&
$Config::Config{useperlio}));
_exit(0);
}
More information about the dslinux-commit
mailing list