9 require Config; import Config;
10 $can_fork = $Config{'d_fork'}
11 || ($^O eq 'MSWin32' && $Config{useithreads}
12 && $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS\b/);
15 if ($^O eq "hpux" or $Config{'extensions'} !~ /\bSocket\b/ &&
16 !(($^O eq 'VMS') && $Config{d_socket})) {
21 # Too many things in this test will hang forever if something is wrong,
22 # so we need a self destruct timer. And IO can hang despite an alarm.
24 # This is convoluted, but we must fork before Test::More, else child's
25 # Test::More thinks that it ran no tests, and prints a message to that
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 = time - $must_finish_by) {
38 warn "Something unexpectedly hung during testing";
39 kill "INT", $parent or die "Kill failed: $!";
53 if( !$Config{d_alarm} ) {
54 plan skip_all => "alarm() not implemented on this platform";
55 } elsif( !$can_fork ) {
56 plan skip_all => "fork() not implemented on this platform";
58 # This should fail but not die if there is real socketpair
59 eval {socketpair LEFT, RIGHT, -1, -1, -1};
60 if ($@ =~ /^Unsupported socket function "socketpair" called/) {
61 plan skip_all => 'No socketpair (real or emulated)';
64 if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) {
65 plan skip_all => 'No AF_UNIX';
72 # But we'll install an alarm handler in case any of the races below fail.
73 $SIG{ALRM} = sub {die "Unexpected alarm during testing"};
75 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC),
76 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)")
77 or print "# \$\! = $!\n";
79 my @left = ("hello ", "world\n");
80 my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
83 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
84 is (syswrite (LEFT, $_), length $_, "syswrite to left");
87 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
88 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
91 # stream socket, so our writes will become joined:
92 my ($buffer, $expect);
93 $expect = join '', @right;
95 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
96 is ($buffer, $expect, "content what we expected?");
97 $expect = join '', @left;
99 is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
100 is ($buffer, $expect, "content what we expected?");
102 ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing");
103 # This will hang forever if eof is buggy, and alarm doesn't interrupt system
104 # Calls. Hence the child process minder.
106 local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" };
107 local $TODO = "Known problems with unix sockets on $^O"
108 if $^O eq 'hpux' || $^O eq 'unicosmk';
111 ok (eof RIGHT, "right is at EOF");
112 is ($!, '', 'and $! should report no error');
117 $SIG{PIPE} = 'IGNORE';
120 = sub { warn "syswrite to left didn't fail within 3 seconds" };
122 # Split the system call from the is() - is() does IO so
123 # (say) a flush may do a seek which on a pipe may disturb errno
124 my $ans = syswrite (LEFT, "void");
126 is ($ans, undef, "syswrite to shutdown left should fail");
130 # This may need skipping on some OSes - restoring value saved above
133 ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN')
134 or printf "\$\!=%d(%s)\n", $err, $err;
137 my @gripping = (chr 255, chr 127);
138 foreach (@gripping) {
139 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
142 ok (!eof LEFT, "left is not at EOF");
144 $expect = join '', @gripping;
146 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
147 is ($buffer, $expect, "content what we expected?");
149 ok (close LEFT, "close left");
150 ok (close RIGHT, "close right");
154 # I suspect we also need a self destruct time-bomb for these, as I don't see any
155 # guarantee that the stack won't drop a UDP packet, even if it is for localhost.
158 skip "No usable SOCK_DGRAM for socketpair", 24 if ($^O =~ /^(MSWin32|os2)\z/);
161 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
162 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
163 or print "# \$\! = $!\n";
166 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
167 is (syswrite (LEFT, $_), length $_, "syswrite to left");
170 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
171 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
174 # stream socket, so our writes will become joined:
176 $total = join '', @right;
177 foreach $expect (@right) {
179 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
180 is ($buffer, $expect, "content what we expected?");
182 $total = join '', @left;
183 foreach $expect (@left) {
185 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
186 is ($buffer, $expect, "content what we expected?");
189 ok (shutdown(LEFT, 1), "shutdown left for writing");
191 # eof uses buffering. eof is indicated by a sysread of zero.
192 # but for a datagram socket there's no way it can know nothing will ever be
195 skip "$^O does length 0 udp reads", 2 if ($^O eq 'os390');
198 local $SIG{ALRM} = sub { $alarmed = 1; };
199 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
202 is (sysread (RIGHT, $buffer, 1), undef,
203 "read on right should be interrupted");
204 is ($alarmed, 1, "alarm should have fired");
209 #ok (eof RIGHT, "right is at EOF");
211 foreach (@gripping) {
212 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
215 $total = join '', @gripping;
216 foreach $expect (@gripping) {
218 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
219 is ($buffer, $expect, "content what we expected?");
222 ok (close LEFT, "close left");
223 ok (close RIGHT, "close right");
225 } # end of DGRAM SKIP
227 kill "INT", $child or warn "Failed to kill child process $child: $!";