10 require Config; import Config;
11 $can_fork = $Config{'d_fork'} || $Config{'d_pseudofork'};
13 if ($^O eq "hpux" or $Config{'extensions'} !~ /\bSocket\b/ &&
14 !(($^O eq 'VMS') && $Config{d_socket})) {
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.
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.
30 die "Fork failed" unless defined $child;
32 $SIG{INT} = sub {exit 0}; # You have 60 seconds. Your time starts now.
33 my $must_finish_by = time + 60;
35 while (($remaining = $must_finish_by - time) > 0) {
38 warn "Something unexpectedly hung during testing";
39 kill "INT", $parent or die "Kill failed: $!";
43 unless ($has_perlio = find PerlIO::Layer 'perlio') {
45 # Since you don't have perlio you might get failures with UTF-8 locales.
58 if( !$Config{d_alarm} ) {
59 plan skip_all => "alarm() not implemented on this platform";
60 } elsif( !$can_fork ) {
61 plan skip_all => "fork() not implemented on this platform";
63 # This should fail but not die if there is real socketpair
64 eval {socketpair LEFT, RIGHT, -1, -1, -1};
65 if ($@ =~ /^Unsupported socket function "socketpair" called/ ||
66 $! =~ /^The operation requested is not supported./) { # Stratus VOS
67 plan skip_all => 'No socketpair (real or emulated)';
70 if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) {
71 plan skip_all => 'No AF_UNIX';
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"};
81 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC),
82 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)")
83 or print "# \$\! = $!\n";
86 binmode(LEFT, ":bytes");
87 binmode(RIGHT, ":bytes");
90 my @left = ("hello ", "world\n");
91 my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
94 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
95 is (syswrite (LEFT, $_), length $_, "syswrite to left");
98 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
99 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
102 # stream socket, so our writes will become joined:
103 my ($buffer, $expect);
104 $expect = join '', @right;
106 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
107 is ($buffer, $expect, "content what we expected?");
108 $expect = join '', @left;
110 is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
111 is ($buffer, $expect, "content what we expected?");
113 ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing");
114 # This will hang forever if eof is buggy, and alarm doesn't interrupt system
115 # Calls. Hence the child process minder.
117 skip "SCO Unixware / OSR have a bug with shutdown",2 if $^O =~ /^(?:svr|sco)/;
118 local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" };
119 local $TODO = "Known problems with unix sockets on $^O"
120 if $^O eq 'hpux' || $^O eq 'super-ux';
123 ok (eof RIGHT, "right is at EOF");
124 local $TODO = "Known problems with unix sockets on $^O"
125 if $^O eq 'unicos' || $^O eq 'unicosmk';
126 is ($!, '', 'and $! should report no error');
131 $SIG{PIPE} = 'IGNORE';
134 = sub { warn "syswrite to left didn't fail within 3 seconds" };
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");
140 is ($ans, undef, "syswrite to shutdown left should fail");
144 # This may need skipping on some OSes - restoring value saved above
147 ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN')
148 or printf "\$\!=%d(%s)\n", $err, $err;
151 my @gripping = (chr 255, chr 127);
152 foreach (@gripping) {
153 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
156 ok (!eof LEFT, "left is not at EOF");
158 $expect = join '', @gripping;
160 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
161 is ($buffer, $expect, "content what we expected?");
163 ok (close LEFT, "close left");
164 ok (close RIGHT, "close right");
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.
172 skip "No usable SOCK_DGRAM for socketpair", 24 if ($^O =~ /^(MSWin32|os2)\z/);
173 local $TODO = "socketpair not supported on $^O" if $^O eq 'nto';
175 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
176 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
177 or print "# \$\! = $!\n";
180 binmode(LEFT, ":bytes");
181 binmode(RIGHT, ":bytes");
185 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
186 is (syswrite (LEFT, $_), length $_, "syswrite to left");
189 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
190 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
193 # stream socket, so our writes will become joined:
195 $total = join '', @right;
196 foreach $expect (@right) {
198 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
199 is ($buffer, $expect, "content what we expected?");
201 $total = join '', @left;
202 foreach $expect (@left) {
204 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
205 is ($buffer, $expect, "content what we expected?");
208 ok (shutdown(LEFT, 1), "shutdown left for writing");
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
214 skip "$^O does length 0 udp reads", 2 if ($^O eq 'os390');
217 local $SIG{ALRM} = sub { $alarmed = 1; };
218 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
221 is (sysread (RIGHT, $buffer, 1), undef,
222 "read on right should be interrupted");
223 is ($alarmed, 1, "alarm should have fired");
228 #ok (eof RIGHT, "right is at EOF");
230 foreach (@gripping) {
231 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
234 $total = join '', @gripping;
235 foreach $expect (@gripping) {
237 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
238 is ($buffer, $expect, "content what we expected?");
241 ok (close LEFT, "close left");
242 ok (close RIGHT, "close right");
244 } # end of DGRAM SKIP
246 kill "INT", $child or warn "Failed to kill child process $child: $!";