6 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bSocket\b/ &&
8 !(($^O eq 'VMS') && $Config{d_socket})) {
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;
63 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
64 is ($buffer, $expect, "content what we expected?");
65 $expect = join '', @left;
66 is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
67 is ($buffer, $expect, "content what we expected?");
69 ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing");
70 # This will hang forever if eof is buggy.
72 local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" };
74 ok (eof RIGHT, "right is at EOF");
78 $SIG{PIPE} = 'IGNORE';
81 = sub { warn "syswrite to left didn't fail within 3 seconds" };
83 is (syswrite (LEFT, "void"), undef, "syswrite to shutdown left should fail");
87 # This may need skipping on some OSes
88 ok ($! == EPIPE, '$! should be EPIPE')
89 or printf "\$\!=%d(%s)\n", $!, $!;
92 my @gripping = (chr 255, chr 127);
94 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
97 ok (!eof LEFT, "left is not at EOF");
99 $expect = join '', @gripping;
100 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
101 is ($buffer, $expect, "content what we expected?");
103 ok (close LEFT, "close left");
104 ok (close RIGHT, "close right");
107 # I suspect we also need a self destruct time-bomb for these, as I don't see any
108 # guarantee that the stack won't drop a UDP packet, even if it is for localhost.
110 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
111 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
112 or print "# \$\! = $!\n";
115 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
116 is (syswrite (LEFT, $_), length $_, "syswrite to left");
119 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
120 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
123 # stream socket, so our writes will become joined:
125 $total = join '', @right;
126 foreach $expect (@right) {
127 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
128 is ($buffer, $expect, "content what we expected?");
130 $total = join '', @left;
131 foreach $expect (@left) {
132 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
133 is ($buffer, $expect, "content what we expected?");
136 ok (shutdown(LEFT, 1), "shutdown left for writing");
137 # eof uses buffering. eof is indicated by a sysread of zero.
138 # but for a datagram socket there's no way it can know nothing will ever be
142 local $SIG{ALRM} = sub { $alarmed = 1; };
143 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
145 is (sysread (RIGHT, $buffer, 1), undef,
146 "read on right should be interrupted");
147 is ($alarmed, 1, "alarm should have fired");
151 #ok (eof RIGHT, "right is at EOF");
153 foreach (@gripping) {
154 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
157 $total = join '', @gripping;
158 foreach $expect (@gripping) {
159 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
160 is ($buffer, $expect, "content what we expected?");
163 ok (close LEFT, "close left");
164 ok (close RIGHT, "close right");