This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
because (sys)?read doesn't reset buffers
[perl5.git] / ext / Socket / socketpair.t
CommitLineData
02fc2eee
NC
1#!./perl -w
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bSocket\b/ &&
8 !(($^O eq 'VMS') && $Config{d_socket})) {
9 print "1..0\n";
10 exit 0;
11 }
12}
13
14use Socket;
15use Test::More;
16use strict;
17use warnings;
c5f49a01 18use Errno 'EPIPE';
02fc2eee
NC
19
20my $skip_reason;
21
22if( !$Config{d_alarm} ) {
23 plan skip_all => "alarm() not implemented on this platform";
24} else {
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)';
29 } else {
30 eval {AF_UNIX};
31 if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) {
32 plan skip_all => 'No AF_UNIX';
33 } else {
cd0506f1 34 plan tests => 45;
02fc2eee
NC
35 }
36 }
37}
38
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"};
42alarm(60);
43
44ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC),
45 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)")
c5f49a01 46 or print "# \$\! = $!\n";
02fc2eee
NC
47
48my @left = ("hello ", "world\n");
49my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
50
51foreach (@left) {
52 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
53 is (syswrite (LEFT, $_), length $_, "syswrite to left");
54}
55foreach (@right) {
56 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
57 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
58}
59
60# stream socket, so our writes will become joined:
61my ($buffer, $expect);
62$expect = join '', @right;
22954800 63undef $buffer;
02fc2eee
NC
64is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
65is ($buffer, $expect, "content what we expected?");
66$expect = join '', @left;
22954800 67undef $buffer;
02fc2eee
NC
68is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
69is ($buffer, $expect, "content what we expected?");
70
c5f49a01 71ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing");
02fc2eee 72# This will hang forever if eof is buggy.
c5f49a01
NC
73{
74 local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" };
75 alarm 3;
cd0506f1 76 $! = 0;
c5f49a01 77 ok (eof RIGHT, "right is at EOF");
cd0506f1 78 is ($!, '', 'and $! should report no error');
c5f49a01
NC
79 alarm 60;
80}
81
82$SIG{PIPE} = 'IGNORE';
83{
84 local $SIG{ALRM}
85 = sub { warn "syswrite to left didn't fail within 3 seconds" };
86 alarm 3;
87 is (syswrite (LEFT, "void"), undef, "syswrite to shutdown left should fail");
88 alarm 60;
89}
90SKIP: {
91 # This may need skipping on some OSes
92 ok ($! == EPIPE, '$! should be EPIPE')
93 or printf "\$\!=%d(%s)\n", $!, $!;
94}
02fc2eee
NC
95
96my @gripping = (chr 255, chr 127);
97foreach (@gripping) {
98 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
99}
100
101ok (!eof LEFT, "left is not at EOF");
102
103$expect = join '', @gripping;
22954800 104undef $buffer;
02fc2eee
NC
105is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
106is ($buffer, $expect, "content what we expected?");
107
108ok (close LEFT, "close left");
109ok (close RIGHT, "close right");
110
111# And now datagrams
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.
114
115ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
116 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
c5f49a01 117 or print "# \$\! = $!\n";
02fc2eee
NC
118
119foreach (@left) {
120 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
121 is (syswrite (LEFT, $_), length $_, "syswrite to left");
122}
123foreach (@right) {
124 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
125 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
126}
127
128# stream socket, so our writes will become joined:
129my ($total);
130$total = join '', @right;
131foreach $expect (@right) {
22954800 132 undef $buffer;
02fc2eee
NC
133 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
134 is ($buffer, $expect, "content what we expected?");
135}
136$total = join '', @left;
137foreach $expect (@left) {
22954800 138 undef $buffer;
02fc2eee
NC
139 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
140 is ($buffer, $expect, "content what we expected?");
141}
142
143ok (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
146# sent
147{
148 my $alarmed = 0;
149 local $SIG{ALRM} = sub { $alarmed = 1; };
150 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
151 alarm 3;
22954800 152 undef $buffer;
02fc2eee
NC
153 is (sysread (RIGHT, $buffer, 1), undef,
154 "read on right should be interrupted");
155 is ($alarmed, 1, "alarm should have fired");
156}
157alarm 30;
158
159#ok (eof RIGHT, "right is at EOF");
160
161foreach (@gripping) {
162 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
163}
164
165$total = join '', @gripping;
166foreach $expect (@gripping) {
22954800 167 undef $buffer;
02fc2eee
NC
168 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
169 is ($buffer, $expect, "content what we expected?");
170}
171
172ok (close LEFT, "close left");
173ok (close RIGHT, "close right");