Upgrade to Devel::PPPort 3.18_01
[p5sagit/p5-mst-13.2.git] / ext / Socket / t / socketpair.t
CommitLineData
02fc2eee 1#!./perl -w
2
b4023995 3my $child;
26bf1728 4my $can_fork;
47520729 5my $has_perlio;
b4023995 6
02fc2eee 7BEGIN {
8 chdir 't' if -d 't';
9 @INC = '../lib';
10 require Config; import Config;
2b09c5a8 11 $can_fork = $Config{'d_fork'} || $Config{'d_pseudofork'};
26bf1728 12
b5d2fea7 13 if ($^O eq "hpux" or $Config{'extensions'} !~ /\bSocket\b/ &&
02fc2eee 14 !(($^O eq 'VMS') && $Config{d_socket})) {
15 print "1..0\n";
16 exit 0;
2b09c5a8 17 }
850d6073 18}
19
20{
21 # This was in the BEGIN block, but since Test::More 0.47 added support to
22 # detect forking, we don't need to fork before Test::More initialises.
b4023995 23
24 # Too many things in this test will hang forever if something is wrong,
25 # so we need a self destruct timer. And IO can hang despite an alarm.
26
26bf1728 27 if( $can_fork) {
b4023995 28 my $parent = $$;
29 $child = fork;
30 die "Fork failed" unless defined $child;
31 if (!$child) {
32 $SIG{INT} = sub {exit 0}; # You have 60 seconds. Your time starts now.
33 my $must_finish_by = time + 60;
34 my $remaining;
208b703c 35 while (($remaining = $must_finish_by - time) > 0) {
b4023995 36 sleep $remaining;
37 }
38 warn "Something unexpectedly hung during testing";
39 kill "INT", $parent or die "Kill failed: $!";
40 exit 1;
41 }
02fc2eee 42 }
47520729 43 unless ($has_perlio = find PerlIO::Layer 'perlio') {
44 print <<EOF;
45# Since you don't have perlio you might get failures with UTF-8 locales.
46EOF
47 }
02fc2eee 48}
b4023995 49
02fc2eee 50use Socket;
51use Test::More;
52use strict;
53use warnings;
5ec8c883 54use Errno;
02fc2eee 55
56my $skip_reason;
57
58if( !$Config{d_alarm} ) {
59 plan skip_all => "alarm() not implemented on this platform";
26bf1728 60} elsif( !$can_fork ) {
b4023995 61 plan skip_all => "fork() not implemented on this platform";
02fc2eee 62} else {
63 # This should fail but not die if there is real socketpair
64 eval {socketpair LEFT, RIGHT, -1, -1, -1};
a5f25d7a 65 if ($@ =~ /^Unsupported socket function "socketpair" called/ ||
66 $! =~ /^The operation requested is not supported./) { # Stratus VOS
02fc2eee 67 plan skip_all => 'No socketpair (real or emulated)';
68 } else {
69 eval {AF_UNIX};
70 if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) {
71 plan skip_all => 'No AF_UNIX';
72 } else {
cd0506f1 73 plan tests => 45;
02fc2eee 74 }
75 }
76}
77
b4023995 78# But we'll install an alarm handler in case any of the races below fail.
79$SIG{ALRM} = sub {die "Unexpected alarm during testing"};
02fc2eee 80
81ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC),
82 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)")
c5f49a01 83 or print "# \$\! = $!\n";
02fc2eee 84
47520729 85if ($has_perlio) {
86 binmode(LEFT, ":bytes");
87 binmode(RIGHT, ":bytes");
88}
b018c34d 89
02fc2eee 90my @left = ("hello ", "world\n");
91my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
92
93foreach (@left) {
94 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
95 is (syswrite (LEFT, $_), length $_, "syswrite to left");
96}
97foreach (@right) {
98 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
99 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
100}
101
102# stream socket, so our writes will become joined:
103my ($buffer, $expect);
104$expect = join '', @right;
22954800 105undef $buffer;
02fc2eee 106is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
107is ($buffer, $expect, "content what we expected?");
108$expect = join '', @left;
22954800 109undef $buffer;
02fc2eee 110is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
111is ($buffer, $expect, "content what we expected?");
112
c5f49a01 113ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing");
b4023995 114# This will hang forever if eof is buggy, and alarm doesn't interrupt system
115# Calls. Hence the child process minder.
381c1bae 116SKIP: {
44dd0177 117 skip "SCO Unixware / OSR have a bug with shutdown",2 if $^O =~ /^(?:svr|sco)/;
c5f49a01 118 local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" };
e03d5f0a 119 local $TODO = "Known problems with unix sockets on $^O"
8bfa5a13 120 if $^O eq 'hpux' || $^O eq 'super-ux';
c5f49a01 121 alarm 3;
cd0506f1 122 $! = 0;
c5f49a01 123 ok (eof RIGHT, "right is at EOF");
8bfa5a13 124 local $TODO = "Known problems with unix sockets on $^O"
125 if $^O eq 'unicos' || $^O eq 'unicosmk';
505fdf5d 126 is ($!, '', 'and $! should report no error');
c5f49a01 127 alarm 60;
128}
129
b5d2fea7 130my $err = $!;
c5f49a01 131$SIG{PIPE} = 'IGNORE';
132{
133 local $SIG{ALRM}
134 = sub { warn "syswrite to left didn't fail within 3 seconds" };
135 alarm 3;
b5d2fea7 136 # Split the system call from the is() - is() does IO so
137 # (say) a flush may do a seek which on a pipe may disturb errno
138 my $ans = syswrite (LEFT, "void");
139 $err = $!;
140 is ($ans, undef, "syswrite to shutdown left should fail");
c5f49a01 141 alarm 60;
142}
26bf1728 143{
b5d2fea7 144 # This may need skipping on some OSes - restoring value saved above
145 # should help
146 $! = $err;
5ec8c883 147 ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN')
b5d2fea7 148 or printf "\$\!=%d(%s)\n", $err, $err;
c5f49a01 149}
02fc2eee 150
151my @gripping = (chr 255, chr 127);
152foreach (@gripping) {
153 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
154}
155
156ok (!eof LEFT, "left is not at EOF");
157
158$expect = join '', @gripping;
22954800 159undef $buffer;
02fc2eee 160is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
161is ($buffer, $expect, "content what we expected?");
162
163ok (close LEFT, "close left");
164ok (close RIGHT, "close right");
165
26bf1728 166
02fc2eee 167# And now datagrams
168# I suspect we also need a self destruct time-bomb for these, as I don't see any
169# guarantee that the stack won't drop a UDP packet, even if it is for localhost.
170
26bf1728 171SKIP: {
a64c954a 172 skip "No usable SOCK_DGRAM for socketpair", 24 if ($^O =~ /^(MSWin32|os2)\z/);
e5e77ef9 173 local $TODO = "socketpair not supported on $^O" if $^O eq 'nto';
26bf1728 174
02fc2eee 175ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
176 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
c5f49a01 177 or print "# \$\! = $!\n";
02fc2eee 178
47520729 179if ($has_perlio) {
180 binmode(LEFT, ":bytes");
181 binmode(RIGHT, ":bytes");
182}
b018c34d 183
02fc2eee 184foreach (@left) {
185 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
186 is (syswrite (LEFT, $_), length $_, "syswrite to left");
187}
188foreach (@right) {
189 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
190 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
191}
192
193# stream socket, so our writes will become joined:
194my ($total);
195$total = join '', @right;
196foreach $expect (@right) {
22954800 197 undef $buffer;
02fc2eee 198 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
199 is ($buffer, $expect, "content what we expected?");
200}
201$total = join '', @left;
202foreach $expect (@left) {
22954800 203 undef $buffer;
02fc2eee 204 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
205 is ($buffer, $expect, "content what we expected?");
206}
207
208ok (shutdown(LEFT, 1), "shutdown left for writing");
10b9e826 209
02fc2eee 210# eof uses buffering. eof is indicated by a sysread of zero.
211# but for a datagram socket there's no way it can know nothing will ever be
212# sent
10b9e826 213SKIP: {
214 skip "$^O does length 0 udp reads", 2 if ($^O eq 'os390');
215
02fc2eee 216 my $alarmed = 0;
217 local $SIG{ALRM} = sub { $alarmed = 1; };
218 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
219 alarm 3;
22954800 220 undef $buffer;
02fc2eee 221 is (sysread (RIGHT, $buffer, 1), undef,
222 "read on right should be interrupted");
223 is ($alarmed, 1, "alarm should have fired");
224}
10b9e826 225
02fc2eee 226alarm 30;
227
228#ok (eof RIGHT, "right is at EOF");
229
230foreach (@gripping) {
231 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
232}
233
234$total = join '', @gripping;
235foreach $expect (@gripping) {
22954800 236 undef $buffer;
02fc2eee 237 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
238 is ($buffer, $expect, "content what we expected?");
239}
240
241ok (close LEFT, "close left");
242ok (close RIGHT, "close right");
b4023995 243
26bf1728 244} # end of DGRAM SKIP
245
b4023995 246kill "INT", $child or warn "Failed to kill child process $child: $!";
247exit 0;