[DOC PATCH] left is right and right is left
[p5sagit/p5-mst-13.2.git] / ext / Socket / socketpair.t
CommitLineData
02fc2eee 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 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 {
c5f49a01 34 plan tests => 44;
02fc2eee 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 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;
63is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
64is ($buffer, $expect, "content what we expected?");
65$expect = join '', @left;
66is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
67is ($buffer, $expect, "content what we expected?");
68
c5f49a01 69ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing");
02fc2eee 70# This will hang forever if eof is buggy.
c5f49a01 71{
72 local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" };
73 alarm 3;
74 ok (eof RIGHT, "right is at EOF");
75 alarm 60;
76}
77
78$SIG{PIPE} = 'IGNORE';
79{
80 local $SIG{ALRM}
81 = sub { warn "syswrite to left didn't fail within 3 seconds" };
82 alarm 3;
83 is (syswrite (LEFT, "void"), undef, "syswrite to shutdown left should fail");
84 alarm 60;
85}
86SKIP: {
87 # This may need skipping on some OSes
88 ok ($! == EPIPE, '$! should be EPIPE')
89 or printf "\$\!=%d(%s)\n", $!, $!;
90}
02fc2eee 91
92my @gripping = (chr 255, chr 127);
93foreach (@gripping) {
94 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
95}
96
97ok (!eof LEFT, "left is not at EOF");
98
99$expect = join '', @gripping;
100is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
101is ($buffer, $expect, "content what we expected?");
102
103ok (close LEFT, "close left");
104ok (close RIGHT, "close right");
105
106# And now datagrams
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.
109
110ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
111 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
c5f49a01 112 or print "# \$\! = $!\n";
02fc2eee 113
114foreach (@left) {
115 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
116 is (syswrite (LEFT, $_), length $_, "syswrite to left");
117}
118foreach (@right) {
119 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
120 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
121}
122
123# stream socket, so our writes will become joined:
124my ($total);
125$total = join '', @right;
126foreach $expect (@right) {
127 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
128 is ($buffer, $expect, "content what we expected?");
129}
130$total = join '', @left;
131foreach $expect (@left) {
132 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
133 is ($buffer, $expect, "content what we expected?");
134}
135
136ok (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
139# sent
140{
141 my $alarmed = 0;
142 local $SIG{ALRM} = sub { $alarmed = 1; };
143 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
144 alarm 3;
145 is (sysread (RIGHT, $buffer, 1), undef,
146 "read on right should be interrupted");
147 is ($alarmed, 1, "alarm should have fired");
148}
149alarm 30;
150
151#ok (eof RIGHT, "right is at EOF");
152
153foreach (@gripping) {
154 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
155}
156
157$total = join '', @gripping;
158foreach $expect (@gripping) {
159 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
160 is ($buffer, $expect, "content what we expected?");
161}
162
163ok (close LEFT, "close left");
164ok (close RIGHT, "close right");