diff options
author | Paul \"LeoNerd\" Evans <leonerd@leonerd.org.uk> | 2011-10-24 19:02:43 +0100 |
---|---|---|
committer | Florian Ragwitz <rafl@debian.org> | 2011-12-01 15:23:14 +0100 |
commit | 44559db1c23f895802dafc1174080c9a8c0f1bce (patch) | |
tree | 93ec4ef674ec0ab5eed77b0d3b100917d0d1f6c0 /cpan/Socket/t | |
parent | 4c3d8e12eba407ea54003da3b559af75fc927817 (diff) | |
download | perl-44559db1c23f895802dafc1174080c9a8c0f1bce.tar.gz |
Prepare Socket.pm for dual-life
* Moved from ext/Socket to cpan/Socket
* Currently declared as PEVANS/Socket-1.95.tar.gz - which is what it
will eventually become
Diffstat (limited to 'cpan/Socket/t')
-rw-r--r-- | cpan/Socket/t/Socket.t | 184 | ||||
-rw-r--r-- | cpan/Socket/t/getaddrinfo.t | 125 | ||||
-rw-r--r-- | cpan/Socket/t/getnameinfo.t | 37 | ||||
-rw-r--r-- | cpan/Socket/t/ipv6_mreq.t | 26 | ||||
-rw-r--r-- | cpan/Socket/t/socketpair.t | 245 |
5 files changed, 617 insertions, 0 deletions
diff --git a/cpan/Socket/t/Socket.t b/cpan/Socket/t/Socket.t new file mode 100644 index 0000000000..f59c265c4c --- /dev/null +++ b/cpan/Socket/t/Socket.t @@ -0,0 +1,184 @@ +#!./perl + +BEGIN { + require Config; import Config; + if ($Config{'extensions'} !~ /\bSocket\b/ && + !(($^O eq 'VMS') && $Config{d_socket})) { + print "1..0\n"; + exit 0; + } + $has_alarm = $Config{d_alarm}; +} + +use Socket qw(:all); +use Test::More tests => 26; + +$has_echo = $^O ne 'MSWin32'; +$alarmed = 0; +sub arm { $alarmed = 0; alarm(shift) if $has_alarm } +sub alarmed { $alarmed = 1 } +$SIG{ALRM} = 'alarmed' if $has_alarm; + +SKIP: { + unless(socket(T, PF_INET, SOCK_STREAM, IPPROTO_TCP)) { + skip "No PF_INET", 3; + } + + pass "socket(PF_INET)"; + + arm(5); + my $host = $^O eq 'MacOS' || ($^O eq 'irix' && $Config{osvers} == 5) ? + '127.0.0.1' : 'localhost'; + my $localhost = inet_aton($host); + + SKIP: { + unless($has_echo && defined $localhost && connect(T,pack_sockaddr_in(7,$localhost))) { + skip "Unable to connect to localhost:7", 2; + } + + arm(0); + + pass "PF_INET echo localhost connected"; + + diag "Connected to " . + inet_ntoa((unpack_sockaddr_in(getpeername(T)))[1])."\n"; + + arm(5); + syswrite(T,"hello",5); + arm(0); + + arm(5); + $read = sysread(T,$buff,10); # Connection may be granted, then closed! + arm(0); + + while ($read > 0 && length($buff) < 5) { + # adjust for fact that TCP doesn't guarantee size of reads/writes + arm(5); + $read = sysread(T,$buff,10,length($buff)); + arm(0); + } + + is(($read == 0 || $buff eq "hello"), "PF_INET echo localhost reply"); + } +} + +SKIP: { + unless(socket(S, PF_INET, SOCK_STREAM, IPPROTO_TCP)) { + skip "No PF_INET", 3; + } + + pass "socket(PF_INET)"; + + SKIP: { + arm(5); + unless($has_echo && connect(S,pack_sockaddr_in(7,INADDR_LOOPBACK))) { + skip "Unable to connect to localhost:7", 2; + } + + arm(0); + + pass "PF_INET echo INADDR_LOOPBACK connected"; + + diag "Connected to " . + inet_ntoa((unpack_sockaddr_in(getpeername(S)))[1])."\n"; + + arm(5); + syswrite(S,"olleh",5); + arm(0); + + arm(5); + $read = sysread(S,$buff,10); # Connection may be granted, then closed! + arm(0); + + while ($read > 0 && length($buff) < 5) { + # adjust for fact that TCP doesn't guarantee size of reads/writes + arm(5); + $read = sysread(S,$buff,10,length($buff)); + arm(0); + } + + is(($read == 0 || $buff eq "olleh"), "PF_INET echo INADDR_LOOPBACK reply"); + } +} + +# warnings +{ + my $w = 0; + local $SIG{__WARN__} = sub { + ++ $w if $_[0] =~ /^6-ARG sockaddr_in call is deprecated/ ; + }; + + no warnings 'Socket'; + sockaddr_in(1,2,3,4,5,6) ; + is($w, 0, "sockaddr_in deprecated form doesn't warn without lexical warnings"); + + use warnings 'Socket'; + sockaddr_in(1,2,3,4,5,6) ; + is($w, 1, "sockaddr_in deprecated form warns with lexical warnings"); +} + +# Test that whatever we give into pack/unpack_sockaddr retains +# the value thru the entire chain. +is(inet_ntoa((unpack_sockaddr_in(pack_sockaddr_in(100,inet_aton("10.250.230.10"))))[1]), '10.250.230.10', + 'inet_aton->pack_sockaddr_in->unpack_sockaddr_in->inet_ntoa roundtrip'); + +is(inet_ntoa(inet_aton("10.20.30.40")), "10.20.30.40", 'inet_aton->inet_ntoa roundtrip'); +is(inet_ntoa(v10.20.30.40), "10.20.30.40", 'inet_ntoa from v-string'); + +{ + my ($port,$addr) = unpack_sockaddr_in(pack_sockaddr_in(100,v10.10.10.10)); + is($port, 100, 'pack_sockaddr_in->unpack_sockaddr_in port'); + is(inet_ntoa($addr), "10.10.10.10", 'pack_sockaddr_in->unpack_sockaddr_in addr'); +} + +{ + local $@; + eval { inet_ntoa(v10.20.30.400) }; + like($@, qr/^Wide character in Socket::inet_ntoa at/, 'inet_ntoa warns about wide characters'); +} + +is(sockaddr_family(pack_sockaddr_in(100,inet_aton("10.250.230.10"))), AF_INET, 'pack_sockaddr_in->sockaddr_family'); + +{ + local $@; + eval { sockaddr_family("") }; + like($@, qr/^Bad arg length for Socket::sockaddr_family, length is 0, should be at least \d+/, 'sockaddr_family warns about argument length'); +} + +SKIP: { + # see if we can handle abstract sockets + skip "Abstract AF_UNIX paths unsupported", 2 unless $^O eq "linux"; + + my $test_abstract_socket = chr(0) . '/org/perl/hello'. chr(0) . 'world'; + my $addr = sockaddr_un ($test_abstract_socket); + my ($path) = sockaddr_un ($addr); + is($path, $test_abstract_socket, 'sockaddr_un can handle abstract AF_UNIX paths'); + + # see if we calculate the address structure length correctly + is(length ($test_abstract_socket) + 2, length $addr, 'sockaddr_un abstract address length'); +} + +SKIP: { + skip "No inet_ntop", 3 unless $Config{d_inetntop} && $Config{d_inetaton}; + + is(inet_ntop(AF_INET, inet_pton(AF_INET, "10.20.30.40")), "10.20.30.40", 'inet_pton->inet_ntop AF_INET roundtrip'); + is(inet_ntop(AF_INET, inet_aton("10.20.30.40")), "10.20.30.40", 'inet_aton->inet_ntop AF_INET roundtrip'); + + SKIP: { + skip "No AF_INET6", 1 unless defined eval { AF_INET6() }; + is(lc inet_ntop(AF_INET6, inet_pton(AF_INET6, "2001:503:BA3E::2:30")), "2001:503:ba3e::2:30", 'inet_pton->inet_ntop AF_INET6 roundtrip'); + } +} + +SKIP: { + skip "No AF_INET6", 5 unless defined eval { AF_INET6() }; + + my $sin6 = pack_sockaddr_in6(0x1234, "0123456789abcdef", 0, 89); + + is(sockaddr_family($sin6), AF_INET6, 'sockaddr_family of pack_sockaddr_in6'); + + is((unpack_sockaddr_in6($sin6))[0], 0x1234, 'pack_sockaddr_in6->unpack_sockaddr_in6 port'); + is((unpack_sockaddr_in6($sin6))[1], "0123456789abcdef", 'pack_sockaddr_in6->unpack_sockaddr_in6 addr'); + is((unpack_sockaddr_in6($sin6))[2], 0, 'pack_sockaddr_in6->unpack_sockaddr_in6 scope_id'); + is((unpack_sockaddr_in6($sin6))[3], 89, 'pack_sockaddr_in6->unpack_sockaddr_in6 flowinfo'); +} diff --git a/cpan/Socket/t/getaddrinfo.t b/cpan/Socket/t/getaddrinfo.t new file mode 100644 index 0000000000..b85af38254 --- /dev/null +++ b/cpan/Socket/t/getaddrinfo.t @@ -0,0 +1,125 @@ +use strict; +use warnings; +use Test::More tests => 30; + +use Socket qw( + AI_NUMERICHOST AF_INET SOCK_STREAM IPPROTO_TCP + unpack_sockaddr_in inet_aton getaddrinfo +); + +my ( $err, @res ); + +( $err, @res ) = getaddrinfo( "127.0.0.1", "80", { socktype => SOCK_STREAM } ); +cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1/service=80/socktype=STREAM' ); +cmp_ok( $err, "eq", "", '$err eq "" for host=127.0.0.1/service=80/socktype=STREAM' ); +is( scalar @res, 1, + '@res has 1 result' ); + +is( $res[0]->{family}, AF_INET, + '$res[0] family is AF_INET' ); +is( $res[0]->{socktype}, SOCK_STREAM, + '$res[0] socktype is SOCK_STREAM' ); +ok( $res[0]->{protocol} == 0 || $res[0]->{protocol} == IPPROTO_TCP, + '$res[0] protocol is 0 or IPPROTO_TCP' ); +ok( defined $res[0]->{addr}, + '$res[0] addr is defined' ); +if (length $res[0]->{addr}) { + is_deeply( [ unpack_sockaddr_in $res[0]->{addr} ], + [ 80, inet_aton( "127.0.0.1" ) ], + '$res[0] addr is {"127.0.0.1", 80}' ); +} else { + fail( '$res[0] addr is empty: check $socksizetype' ); +} + +# Check actual IV integers work just as well as PV strings +( $err, @res ) = getaddrinfo( "127.0.0.1", 80, { socktype => SOCK_STREAM } ); +cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1/service=80/socktype=STREAM' ); +is_deeply( [ unpack_sockaddr_in $res[0]->{addr} ], + [ 80, inet_aton( "127.0.0.1" ) ], + '$res[0] addr is {"127.0.0.1", 80}' ); + +( $err, @res ) = getaddrinfo( "127.0.0.1", "" ); +cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1' ); +# Might get more than one; e.g. different socktypes +ok( scalar @res > 0, '@res has results' ); + +( $err, @res ) = getaddrinfo( "127.0.0.1", undef ); +cmp_ok( $err, "==", 0, '$err == 0 for host=127.0.0.1/service=undef' ); + +# Test GETMAGIC +{ + "127.0.0.1" =~ /(.+)/; + ( $err, @res ) = getaddrinfo($1, undef); + cmp_ok( $err, "==", 0, '$err == 0 for host=$1' ); + ok( scalar @res > 0, '@res has results' ); + is( (unpack_sockaddr_in $res[0]->{addr})[1], + inet_aton( "127.0.0.1" ), + '$res[0] addr is {"127.0.0.1", ??}' ); +} + +( $err, @res ) = getaddrinfo( "", "80", { family => AF_INET, socktype => SOCK_STREAM } ); +cmp_ok( $err, "==", 0, '$err == 0 for service=80/family=AF_INET/socktype=STREAM' ); +is( scalar @res, 1, '@res has 1 result' ); + +# Just pick the first one +is( $res[0]->{family}, AF_INET, + '$res[0] family is AF_INET' ); +is( $res[0]->{socktype}, SOCK_STREAM, + '$res[0] socktype is SOCK_STREAM' ); +ok( $res[0]->{protocol} == 0 || $res[0]->{protocol} == IPPROTO_TCP, + '$res[0] protocol is 0 or IPPROTO_TCP' ); + +# Now some tests of a few well-known internet hosts +my $goodhost = "cpan.perl.org"; + +SKIP: { + skip "Resolver has no answer for $goodhost", 2 unless gethostbyname( $goodhost ); + + ( $err, @res ) = getaddrinfo( "cpan.perl.org", "ftp", { socktype => SOCK_STREAM } ); + cmp_ok( $err, "==", 0, '$err == 0 for host=cpan.perl.org/service=ftp/socktype=STREAM' ); + # Might get more than one; e.g. different families + ok( scalar @res > 0, '@res has results' ); +} + +# Now something I hope doesn't exist - we put it in a known-missing TLD +my $missinghost = "TbK4jM2M0OS.lm57DWIyu4i"; + +# Some CPAN testing machines seem to have wildcard DNS servers that reply to +# any request. We'd better check for them + +SKIP: { + skip "Resolver has an answer for $missinghost", 1 if gethostbyname( $missinghost ); + + # Some OSes return $err == 0 but no results + ( $err, @res ) = getaddrinfo( $missinghost, "ftp", { socktype => SOCK_STREAM } ); + ok( $err != 0 || ( $err == 0 && @res == 0 ), + '$err != 0 or @res == 0 for host=TbK4jM2M0OS.lm57DWIyu4i/service=ftp/socktype=SOCK_STREAM' ); + if( @res ) { + # Diagnostic that might help + while( my $r = shift @res ) { + diag( "family=$r->{family} socktype=$r->{socktype} protocol=$r->{protocol} addr=[" . length( $r->{addr} ) . " bytes]" ); + diag( " addr=" . join( ", ", map { sprintf '0x%02x', ord $_ } split m//, $r->{addr} ) ); + } + } +} + +# Now check that names with AI_NUMERICHOST fail + +( $err, @res ) = getaddrinfo( "localhost", "ftp", { flags => AI_NUMERICHOST, socktype => SOCK_STREAM } ); +ok( $err != 0, '$err != 0 for host=localhost/service=ftp/flags=AI_NUMERICHOST/socktype=SOCK_STREAM' ); + +# Some sanity checking on the hints hash +ok( defined eval { getaddrinfo( "127.0.0.1", "80", undef ); 1 }, + 'getaddrinfo() with undef hints works' ); +ok( !defined eval { getaddrinfo( "127.0.0.1", "80", "hints" ); 1 }, + 'getaddrinfo() with string hints dies' ); +ok( !defined eval { getaddrinfo( "127.0.0.1", "80", [] ); 1 }, + 'getaddrinfo() with ARRAY hints dies' ); + +# Ensure it doesn't segfault if args are missing + +( $err, @res ) = getaddrinfo(); +ok( defined $err, '$err defined for getaddrinfo()' ); + +( $err, @res ) = getaddrinfo( "127.0.0.1" ); +ok( defined $err, '$err defined for getaddrinfo("127.0.0.1")' ); diff --git a/cpan/Socket/t/getnameinfo.t b/cpan/Socket/t/getnameinfo.t new file mode 100644 index 0000000000..803e8c0c57 --- /dev/null +++ b/cpan/Socket/t/getnameinfo.t @@ -0,0 +1,37 @@ +use strict; +use warnings; +use Test::More tests => 10; + +use Socket qw( + AF_INET NI_NUMERICHOST NI_NUMERICSERV + getnameinfo pack_sockaddr_in inet_aton +); + +my ( $err, $host, $service ); + +( $err, $host, $service ) = getnameinfo( pack_sockaddr_in( 80, inet_aton( "127.0.0.1" ) ), NI_NUMERICHOST|NI_NUMERICSERV ); +cmp_ok( $err, "==", 0, '$err == 0 for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICHOST|NI_NUMERICSERV' ); +cmp_ok( $err, "eq", "", '$err eq "" for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICHOST|NI_NUMERICSERV' ); + +is( $host, "127.0.0.1", '$host is 127.0.0.1 for NH/NS' ); +is( $service, "80", '$service is 80 for NH/NS' ); + +# Probably "localhost" but we'd better ask the system to be sure +my $expect_host = gethostbyaddr( inet_aton( "127.0.0.1" ), AF_INET ); +defined $expect_host or $expect_host = "127.0.0.1"; + +( $err, $host, $service ) = getnameinfo( pack_sockaddr_in( 80, inet_aton( "127.0.0.1" ) ), NI_NUMERICSERV ); +cmp_ok( $err, "==", 0, '$err == 0 for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICSERV' ); + +is( $host, $expect_host, "\$host is $expect_host for NS" ); +is( $service, "80", '$service is 80 for NS' ); + +# Probably "www" but we'd better ask the system to be sure +my $expect_service = getservbyport( 80, "tcp" ); +defined $expect_service or $expect_service = "80"; + +( $err, $host, $service ) = getnameinfo( pack_sockaddr_in( 80, inet_aton( "127.0.0.1" ) ), NI_NUMERICHOST ); +cmp_ok( $err, "==", 0, '$err == 0 for {family=AF_INET,port=80,sinaddr=127.0.0.1}/NI_NUMERICHOST' ); + +is( $host, "127.0.0.1", '$host is 127.0.0.1 for NH' ); +is( $service, $expect_service, "\$service is $expect_service for NH" ); diff --git a/cpan/Socket/t/ipv6_mreq.t b/cpan/Socket/t/ipv6_mreq.t new file mode 100644 index 0000000000..43fb8f804b --- /dev/null +++ b/cpan/Socket/t/ipv6_mreq.t @@ -0,0 +1,26 @@ +use strict; +use warnings; +use Test::More; + +use Socket qw( + pack_ipv6_mreq unpack_ipv6_mreq +); + +# Check that pack/unpack_ipv6_mreq either croak with "Not implemented", or +# roundtrip as identity + +my $packed; +eval { + $packed = pack_ipv6_mreq "ANADDRESSIN16CHR", 123; +}; +if( !defined $packed ) { + plan skip_all => "No pack_ipv6_mreq" if $@ =~ m/ not implemented /; + die $@; +} + +plan tests => 2; + +my @unpacked = unpack_ipv6_mreq $packed; + +is( $unpacked[0], "ANADDRESSIN16CHR", 'unpack_ipv6_mreq multiaddr' ); +is( $unpacked[1], 123, 'unpack_ipv6_mreq ifindex' ); diff --git a/cpan/Socket/t/socketpair.t b/cpan/Socket/t/socketpair.t new file mode 100644 index 0000000000..997628c3bd --- /dev/null +++ b/cpan/Socket/t/socketpair.t @@ -0,0 +1,245 @@ +#!./perl -w + +my $child; +my $can_fork; +my $has_perlio; + +BEGIN { + require Config; import Config; + $can_fork = $Config{'d_fork'} || $Config{'d_pseudofork'}; + + if ($^O eq "hpux" or $Config{'extensions'} !~ /\bSocket\b/ && + !(($^O eq 'VMS') && $Config{d_socket})) { + print "1..0\n"; + exit 0; + } +} + +{ + # This was in the BEGIN block, but since Test::More 0.47 added support to + # detect forking, we don't need to fork before Test::More initialises. + + # Too many things in this test will hang forever if something is wrong, + # so we need a self destruct timer. And IO can hang despite an alarm. + + if( $can_fork) { + my $parent = $$; + $child = fork; + die "Fork failed" unless defined $child; + if (!$child) { + $SIG{INT} = sub {exit 0}; # You have 60 seconds. Your time starts now. + my $must_finish_by = time + 60; + my $remaining; + while (($remaining = $must_finish_by - time) > 0) { + sleep $remaining; + } + warn "Something unexpectedly hung during testing"; + kill "INT", $parent or die "Kill failed: $!"; + exit 1; + } + } + unless ($has_perlio = find PerlIO::Layer 'perlio') { + print <<EOF; +# Since you don't have perlio you might get failures with UTF-8 locales. +EOF + } +} + +use Socket; +use Test::More; +use strict; +use warnings; +use Errno; + +my $skip_reason; + +if( !$Config{d_alarm} ) { + plan skip_all => "alarm() not implemented on this platform"; +} elsif( !$can_fork ) { + plan skip_all => "fork() not implemented on this platform"; +} else { + # This should fail but not die if there is real socketpair + eval {socketpair LEFT, RIGHT, -1, -1, -1}; + if ($@ =~ /^Unsupported socket function "socketpair" called/ || + $! =~ /^The operation requested is not supported./) { # Stratus VOS + plan skip_all => 'No socketpair (real or emulated)'; + } else { + eval {AF_UNIX}; + if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) { + plan skip_all => 'No AF_UNIX'; + } else { + plan tests => 45; + } + } +} + +# But we'll install an alarm handler in case any of the races below fail. +$SIG{ALRM} = sub {die "Unexpected alarm during testing"}; + +ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC), + "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)") + or print "# \$\! = $!\n"; + +if ($has_perlio) { + binmode(LEFT, ":bytes"); + binmode(RIGHT, ":bytes"); +} + +my @left = ("hello ", "world\n"); +my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here. + +foreach (@left) { + # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left"); + is (syswrite (LEFT, $_), length $_, "syswrite to left"); +} +foreach (@right) { + # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right"); + is (syswrite (RIGHT, $_), length $_, "syswrite to right"); +} + +# stream socket, so our writes will become joined: +my ($buffer, $expect); +$expect = join '', @right; +undef $buffer; +is (read (LEFT, $buffer, length $expect), length $expect, "read on left"); +is ($buffer, $expect, "content what we expected?"); +$expect = join '', @left; +undef $buffer; +is (read (RIGHT, $buffer, length $expect), length $expect, "read on right"); +is ($buffer, $expect, "content what we expected?"); + +ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing"); +# This will hang forever if eof is buggy, and alarm doesn't interrupt system +# Calls. Hence the child process minder. +SKIP: { + skip "SCO Unixware / OSR have a bug with shutdown",2 if $^O =~ /^(?:svr|sco)/; + local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" }; + local $TODO = "Known problems with unix sockets on $^O" + if $^O eq 'hpux' || $^O eq 'super-ux'; + alarm 3; + $! = 0; + ok (eof RIGHT, "right is at EOF"); + local $TODO = "Known problems with unix sockets on $^O" + if $^O eq 'unicos' || $^O eq 'unicosmk'; + is ($!, '', 'and $! should report no error'); + alarm 60; +} + +my $err = $!; +$SIG{PIPE} = 'IGNORE'; +{ + local $SIG{ALRM} + = sub { warn "syswrite to left didn't fail within 3 seconds" }; + alarm 3; + # Split the system call from the is() - is() does IO so + # (say) a flush may do a seek which on a pipe may disturb errno + my $ans = syswrite (LEFT, "void"); + $err = $!; + is ($ans, undef, "syswrite to shutdown left should fail"); + alarm 60; +} +{ + # This may need skipping on some OSes - restoring value saved above + # should help + $! = $err; + ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN') + or printf "\$\!=%d(%s)\n", $err, $err; +} + +my @gripping = (chr 255, chr 127); +foreach (@gripping) { + is (syswrite (RIGHT, $_), length $_, "syswrite to right"); +} + +ok (!eof LEFT, "left is not at EOF"); + +$expect = join '', @gripping; +undef $buffer; +is (read (LEFT, $buffer, length $expect), length $expect, "read on left"); +is ($buffer, $expect, "content what we expected?"); + +ok (close LEFT, "close left"); +ok (close RIGHT, "close right"); + + +# And now datagrams +# I suspect we also need a self destruct time-bomb for these, as I don't see any +# guarantee that the stack won't drop a UDP packet, even if it is for localhost. + +SKIP: { + skip "No usable SOCK_DGRAM for socketpair", 24 if ($^O =~ /^(MSWin32|os2)\z/); + local $TODO = "socketpair not supported on $^O" if $^O eq 'nto'; + +ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC), + "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)") + or print "# \$\! = $!\n"; + +if ($has_perlio) { + binmode(LEFT, ":bytes"); + binmode(RIGHT, ":bytes"); +} + +foreach (@left) { + # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left"); + is (syswrite (LEFT, $_), length $_, "syswrite to left"); +} +foreach (@right) { + # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right"); + is (syswrite (RIGHT, $_), length $_, "syswrite to right"); +} + +# stream socket, so our writes will become joined: +my ($total); +$total = join '', @right; +foreach $expect (@right) { + undef $buffer; + is (sysread (LEFT, $buffer, length $total), length $expect, "read on left"); + is ($buffer, $expect, "content what we expected?"); +} +$total = join '', @left; +foreach $expect (@left) { + undef $buffer; + is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right"); + is ($buffer, $expect, "content what we expected?"); +} + +ok (shutdown(LEFT, 1), "shutdown left for writing"); + +# eof uses buffering. eof is indicated by a sysread of zero. +# but for a datagram socket there's no way it can know nothing will ever be +# sent +SKIP: { + skip "$^O does length 0 udp reads", 2 if ($^O eq 'os390'); + + my $alarmed = 0; + local $SIG{ALRM} = sub { $alarmed = 1; }; + print "# Approximate forever as 3 seconds. Wait 'forever'...\n"; + alarm 3; + undef $buffer; + is (sysread (RIGHT, $buffer, 1), undef, + "read on right should be interrupted"); + is ($alarmed, 1, "alarm should have fired"); +} + +alarm 30; + +#ok (eof RIGHT, "right is at EOF"); + +foreach (@gripping) { + is (syswrite (RIGHT, $_), length $_, "syswrite to right"); +} + +$total = join '', @gripping; +foreach $expect (@gripping) { + undef $buffer; + is (sysread (LEFT, $buffer, length $total), length $expect, "read on left"); + is ($buffer, $expect, "content what we expected?"); +} + +ok (close LEFT, "close left"); +ok (close RIGHT, "close right"); + +} # end of DGRAM SKIP + +kill "INT", $child or warn "Failed to kill child process $child: $!"; +exit 0; |