9 require Config; import Config;
10 $can_fork = $Config{d_fork} || ($^O eq 'MSWin32' && $Config{useithreads});
12 if ($^O eq "hpux" or $Config{'extensions'} !~ /\bSocket\b/ &&
13 !(($^O eq 'VMS') && $Config{d_socket})) {
18 # Too many things in this test will hang forever if something is wrong,
19 # so we need a self destruct timer. And IO can hang despite an alarm.
21 # This is convoluted, but we must fork before Test::More, else child's
22 # Test::More thinks that it ran no tests, and prints a message to that
27 die "Fork failed" unless defined $child;
29 $SIG{INT} = sub {exit 0}; # You have 60 seconds. Your time starts now.
30 my $must_finish_by = time + 60;
32 while ($remaining = time - $must_finish_by) {
35 warn "Something unexpectedly hung during testing";
36 kill "INT", $parent or die "Kill failed: $!";
50 if( !$Config{d_alarm} ) {
51 plan skip_all => "alarm() not implemented on this platform";
52 } elsif( !$can_fork ) {
53 plan skip_all => "fork() not implemented on this platform";
55 # This should fail but not die if there is real socketpair
56 eval {socketpair LEFT, RIGHT, -1, -1, -1};
57 if ($@ =~ /^Unsupported socket function "socketpair" called/) {
58 plan skip_all => 'No socketpair (real or emulated)';
61 if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) {
62 plan skip_all => 'No AF_UNIX';
69 # But we'll install an alarm handler in case any of the races below fail.
70 $SIG{ALRM} = sub {die "Unexpected alarm during testing"};
72 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC),
73 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)")
74 or print "# \$\! = $!\n";
76 my @left = ("hello ", "world\n");
77 my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
80 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
81 is (syswrite (LEFT, $_), length $_, "syswrite to left");
84 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
85 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
88 # stream socket, so our writes will become joined:
89 my ($buffer, $expect);
90 $expect = join '', @right;
92 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
93 is ($buffer, $expect, "content what we expected?");
94 $expect = join '', @left;
96 is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
97 is ($buffer, $expect, "content what we expected?");
99 ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing");
100 # This will hang forever if eof is buggy, and alarm doesn't interrupt system
101 # Calls. Hence the child process minder.
103 local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" };
104 local $TODO = "Known problems with unix sockets on $^O"
105 if $^O eq 'hpux' || $^O eq 'unicosmk';
108 ok (eof RIGHT, "right is at EOF");
109 is ($!, '', 'and $! should report no error');
114 $SIG{PIPE} = 'IGNORE';
117 = sub { warn "syswrite to left didn't fail within 3 seconds" };
119 # Split the system call from the is() - is() does IO so
120 # (say) a flush may do a seek which on a pipe may disturb errno
121 my $ans = syswrite (LEFT, "void");
123 is ($ans, undef, "syswrite to shutdown left should fail");
127 # This may need skipping on some OSes - restoring value saved above
130 ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN')
131 or printf "\$\!=%d(%s)\n", $err, $err;
134 my @gripping = (chr 255, chr 127);
135 foreach (@gripping) {
136 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
139 ok (!eof LEFT, "left is not at EOF");
141 $expect = join '', @gripping;
143 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
144 is ($buffer, $expect, "content what we expected?");
146 ok (close LEFT, "close left");
147 ok (close RIGHT, "close right");
151 # I suspect we also need a self destruct time-bomb for these, as I don't see any
152 # guarantee that the stack won't drop a UDP packet, even if it is for localhost.
155 skip "No usable SOCK_DGRAM for socketpair", 24 if ($^O =~ /^(MSWin32|os2)\z/);
158 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
159 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
160 or print "# \$\! = $!\n";
163 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
164 is (syswrite (LEFT, $_), length $_, "syswrite to left");
167 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
168 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
171 # stream socket, so our writes will become joined:
173 $total = join '', @right;
174 foreach $expect (@right) {
176 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
177 is ($buffer, $expect, "content what we expected?");
179 $total = join '', @left;
180 foreach $expect (@left) {
182 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
183 is ($buffer, $expect, "content what we expected?");
186 ok (shutdown(LEFT, 1), "shutdown left for writing");
188 # eof uses buffering. eof is indicated by a sysread of zero.
189 # but for a datagram socket there's no way it can know nothing will ever be
192 skip "$^O does length 0 udp reads", 2 if ($^O eq 'os390');
195 local $SIG{ALRM} = sub { $alarmed = 1; };
196 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
199 is (sysread (RIGHT, $buffer, 1), undef,
200 "read on right should be interrupted");
201 is ($alarmed, 1, "alarm should have fired");
206 #ok (eof RIGHT, "right is at EOF");
208 foreach (@gripping) {
209 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
212 $total = join '', @gripping;
213 foreach $expect (@gripping) {
215 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
216 is ($buffer, $expect, "content what we expected?");
219 ok (close LEFT, "close left");
220 ok (close RIGHT, "close right");
222 } # end of DGRAM SKIP
224 kill "INT", $child or warn "Failed to kill child process $child: $!";