6 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bSocket\b/ &&
8 !(($^O eq 'VMS') && $Config{d_socket})) {
18 use Errno qw(EPIPE ESHUTDOWN);
22 if( !$Config{d_alarm} ) {
23 plan skip_all => "alarm() not implemented on this platform";
25 # This should fail but not die if there is real socketpair
26 eval {socketpair LEFT, RIGHT, -1, -1, -1};
27 if ($@ =~ /^Unsupported socket function "socketpair" called/) {
28 plan skip_all => 'No socketpair (real or emulated)';
31 if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) {
32 plan skip_all => 'No AF_UNIX';
39 # Too many things in this test will hang forever if something is wrong, so
40 # we need a self destruct timer.
41 $SIG{ALRM} = sub {die "Something unexpectedly hung during testing"};
44 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC),
45 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)")
46 or print "# \$\! = $!\n";
48 my @left = ("hello ", "world\n");
49 my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
52 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
53 is (syswrite (LEFT, $_), length $_, "syswrite to left");
56 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
57 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
60 # stream socket, so our writes will become joined:
61 my ($buffer, $expect);
62 $expect = join '', @right;
64 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
65 is ($buffer, $expect, "content what we expected?");
66 $expect = join '', @left;
68 is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
69 is ($buffer, $expect, "content what we expected?");
71 ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing");
72 # This will hang forever if eof is buggy.
74 local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" };
77 ok (eof RIGHT, "right is at EOF");
78 is ($!, '', 'and $! should report no error');
82 $SIG{PIPE} = 'IGNORE';
85 = sub { warn "syswrite to left didn't fail within 3 seconds" };
87 is (syswrite (LEFT, "void"), undef, "syswrite to shutdown left should fail");
91 # This may need skipping on some OSes
92 ok (($! == EPIPE or $! == ESHUTDOWN), '$! should be EPIPE or ESHUTDOWN')
93 or printf "\$\!=%d(%s)\n", $!, $!;
96 my @gripping = (chr 255, chr 127);
98 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
101 ok (!eof LEFT, "left is not at EOF");
103 $expect = join '', @gripping;
105 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
106 is ($buffer, $expect, "content what we expected?");
108 ok (close LEFT, "close left");
109 ok (close RIGHT, "close right");
112 # I suspect we also need a self destruct time-bomb for these, as I don't see any
113 # guarantee that the stack won't drop a UDP packet, even if it is for localhost.
115 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
116 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
117 or print "# \$\! = $!\n";
120 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
121 is (syswrite (LEFT, $_), length $_, "syswrite to left");
124 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
125 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
128 # stream socket, so our writes will become joined:
130 $total = join '', @right;
131 foreach $expect (@right) {
133 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
134 is ($buffer, $expect, "content what we expected?");
136 $total = join '', @left;
137 foreach $expect (@left) {
139 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
140 is ($buffer, $expect, "content what we expected?");
143 ok (shutdown(LEFT, 1), "shutdown left for writing");
144 # eof uses buffering. eof is indicated by a sysread of zero.
145 # but for a datagram socket there's no way it can know nothing will ever be
149 local $SIG{ALRM} = sub { $alarmed = 1; };
150 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
153 is (sysread (RIGHT, $buffer, 1), undef,
154 "read on right should be interrupted");
155 is ($alarmed, 1, "alarm should have fired");
159 #ok (eof RIGHT, "right is at EOF");
161 foreach (@gripping) {
162 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
165 $total = join '', @gripping;
166 foreach $expect (@gripping) {
168 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
169 is ($buffer, $expect, "content what we expected?");
172 ok (close LEFT, "close left");
173 ok (close RIGHT, "close right");