6 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bSocket\b/ &&
8 !(($^O eq 'VMS') && $Config{d_socket})) {
21 if( !$Config{d_alarm} ) {
22 plan skip_all => "alarm() not implemented on this platform";
24 # This should fail but not die if there is real socketpair
25 eval {socketpair LEFT, RIGHT, -1, -1, -1};
26 if ($@ =~ /^Unsupported socket function "socketpair" called/) {
27 plan skip_all => 'No socketpair (real or emulated)';
30 if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) {
31 plan skip_all => 'No AF_UNIX';
38 # Too many things in this test will hang forever if something is wrong, so
39 # we need a self destruct timer.
40 $SIG{ALRM} = sub {die "Something unexpectedly hung during testing"};
43 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC),
44 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)")
45 or print "# \$\! = $!";
47 my @left = ("hello ", "world\n");
48 my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
51 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
52 is (syswrite (LEFT, $_), length $_, "syswrite to left");
55 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
56 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
59 # stream socket, so our writes will become joined:
60 my ($buffer, $expect);
61 $expect = join '', @right;
62 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
63 is ($buffer, $expect, "content what we expected?");
64 $expect = join '', @left;
65 is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
66 is ($buffer, $expect, "content what we expected?");
68 ok (shutdown(LEFT, 1), "shutdown left for writing");
69 # This will hang forever if eof is buggy.
70 ok (eof RIGHT, "right is at EOF");
72 my @gripping = (chr 255, chr 127);
74 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
77 ok (!eof LEFT, "left is not at EOF");
79 $expect = join '', @gripping;
80 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
81 is ($buffer, $expect, "content what we expected?");
83 ok (close LEFT, "close left");
84 ok (close RIGHT, "close right");
87 # I suspect we also need a self destruct time-bomb for these, as I don't see any
88 # guarantee that the stack won't drop a UDP packet, even if it is for localhost.
90 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
91 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
92 or print "# \$\! = $!";
95 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
96 is (syswrite (LEFT, $_), length $_, "syswrite to left");
99 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
100 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
103 # stream socket, so our writes will become joined:
105 $total = join '', @right;
106 foreach $expect (@right) {
107 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
108 is ($buffer, $expect, "content what we expected?");
110 $total = join '', @left;
111 foreach $expect (@left) {
112 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
113 is ($buffer, $expect, "content what we expected?");
116 ok (shutdown(LEFT, 1), "shutdown left for writing");
117 # eof uses buffering. eof is indicated by a sysread of zero.
118 # but for a datagram socket there's no way it can know nothing will ever be
122 local $SIG{ALRM} = sub { $alarmed = 1; };
123 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
125 is (sysread (RIGHT, $buffer, 1), undef,
126 "read on right should be interrupted");
127 is ($alarmed, 1, "alarm should have fired");
131 #ok (eof RIGHT, "right is at EOF");
133 foreach (@gripping) {
134 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
137 $total = join '', @gripping;
138 foreach $expect (@gripping) {
139 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
140 is ($buffer, $expect, "content what we expected?");
143 ok (close LEFT, "close left");
144 ok (close RIGHT, "close right");