8 require Config; import Config;
9 if ($Config{'extensions'} !~ /\bSocket\b/ &&
10 !(($^O eq 'VMS') && $Config{d_socket})) {
15 # Too many things in this test will hang forever if something is wrong,
16 # so we need a self destruct timer. And IO can hang despite an alarm.
18 # This is convoluted, but we must fork before Test::More, else child's
19 # Test::More thinks that it ran no tests, and prints a message to that
21 if( $Config{d_fork} ) {
24 die "Fork failed" unless defined $child;
26 $SIG{INT} = sub {exit 0}; # You have 60 seconds. Your time starts now.
27 my $must_finish_by = time + 60;
29 while ($remaining = time - $must_finish_by) {
32 warn "Something unexpectedly hung during testing";
33 kill "INT", $parent or die "Kill failed: $!";
47 if( !$Config{d_alarm} ) {
48 plan skip_all => "alarm() not implemented on this platform";
49 } elsif( !$Config{d_fork} ) {
50 plan skip_all => "fork() not implemented on this platform";
52 # This should fail but not die if there is real socketpair
53 eval {socketpair LEFT, RIGHT, -1, -1, -1};
54 if ($@ =~ /^Unsupported socket function "socketpair" called/) {
55 plan skip_all => 'No socketpair (real or emulated)';
58 if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) {
59 plan skip_all => 'No AF_UNIX';
66 # But we'll install an alarm handler in case any of the races below fail.
67 $SIG{ALRM} = sub {die "Unexpected alarm during testing"};
69 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC),
70 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)")
71 or print "# \$\! = $!\n";
73 my @left = ("hello ", "world\n");
74 my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
77 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
78 is (syswrite (LEFT, $_), length $_, "syswrite to left");
81 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
82 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
85 # stream socket, so our writes will become joined:
86 my ($buffer, $expect);
87 $expect = join '', @right;
89 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
90 is ($buffer, $expect, "content what we expected?");
91 $expect = join '', @left;
93 is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
94 is ($buffer, $expect, "content what we expected?");
96 ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing");
97 # This will hang forever if eof is buggy, and alarm doesn't interrupt system
98 # Calls. Hence the child process minder.
100 local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" };
101 local $TODO = "Known problems with unix sockets on $^O" if $^O eq 'hpux';
104 ok (eof RIGHT, "right is at EOF");
105 is ($!, '', 'and $! should report no error');
109 $SIG{PIPE} = 'IGNORE';
112 = sub { warn "syswrite to left didn't fail within 3 seconds" };
114 is (syswrite (LEFT, "void"), undef, "syswrite to shutdown left should fail");
118 # This may need skipping on some OSes
119 ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN')
120 or printf "\$\!=%d(%s)\n", $!, $!;
123 my @gripping = (chr 255, chr 127);
124 foreach (@gripping) {
125 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
128 ok (!eof LEFT, "left is not at EOF");
130 $expect = join '', @gripping;
132 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
133 is ($buffer, $expect, "content what we expected?");
135 ok (close LEFT, "close left");
136 ok (close RIGHT, "close right");
139 # I suspect we also need a self destruct time-bomb for these, as I don't see any
140 # guarantee that the stack won't drop a UDP packet, even if it is for localhost.
142 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
143 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
144 or print "# \$\! = $!\n";
147 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
148 is (syswrite (LEFT, $_), length $_, "syswrite to left");
151 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
152 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
155 # stream socket, so our writes will become joined:
157 $total = join '', @right;
158 foreach $expect (@right) {
160 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
161 is ($buffer, $expect, "content what we expected?");
163 $total = join '', @left;
164 foreach $expect (@left) {
166 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
167 is ($buffer, $expect, "content what we expected?");
170 ok (shutdown(LEFT, 1), "shutdown left for writing");
171 # eof uses buffering. eof is indicated by a sysread of zero.
172 # but for a datagram socket there's no way it can know nothing will ever be
176 local $SIG{ALRM} = sub { $alarmed = 1; };
177 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
180 is (sysread (RIGHT, $buffer, 1), undef,
181 "read on right should be interrupted");
182 is ($alarmed, 1, "alarm should have fired");
186 #ok (eof RIGHT, "right is at EOF");
188 foreach (@gripping) {
189 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
192 $total = join '', @gripping;
193 foreach $expect (@gripping) {
195 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
196 is ($buffer, $expect, "content what we expected?");
199 ok (close LEFT, "close left");
200 ok (close RIGHT, "close right");
202 kill "INT", $child or warn "Failed to kill child process $child: $!";