Re: socketpair emulation
[p5sagit/p5-mst-13.2.git] / ext / Socket / socketpair.t
1 #!./perl -w
2
3 BEGIN {
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         
14 use Socket;
15 use Test::More;
16 use strict;
17 use warnings;
18 use Errno 'EPIPE';
19
20 my $skip_reason;
21
22 if( !$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 {
34       plan tests => 44;
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"};
42 alarm(60);
43
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";
47
48 my @left = ("hello ", "world\n");
49 my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
50
51 foreach (@left) {
52   # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
53   is (syswrite (LEFT, $_), length $_, "syswrite to left");
54 }
55 foreach (@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:
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?");
68
69 ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing");
70 # This will hang forever if eof is buggy.
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 }
86 SKIP: {
87   # This may need skipping on some OSes
88   ok ($! == EPIPE, '$! should be EPIPE')
89     or printf "\$\!=%d(%s)\n", $!, $!;
90 }
91
92 my @gripping = (chr 255, chr 127);
93 foreach (@gripping) {
94   is (syswrite (RIGHT, $_), length $_, "syswrite to right");
95 }
96
97 ok (!eof LEFT, "left is not at EOF");
98
99 $expect = join '', @gripping;
100 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
101 is ($buffer, $expect, "content what we expected?");
102
103 ok (close LEFT, "close left");
104 ok (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
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";
113
114 foreach (@left) {
115   # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
116   is (syswrite (LEFT, $_), length $_, "syswrite to left");
117 }
118 foreach (@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:
124 my ($total);
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?");
129 }
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?");
134 }
135
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
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 }
149 alarm 30;
150
151 #ok (eof RIGHT, "right is at EOF");
152
153 foreach (@gripping) {
154   is (syswrite (RIGHT, $_), length $_, "syswrite to right");
155 }
156
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?");
161 }
162
163 ok (close LEFT, "close left");
164 ok (close RIGHT, "close right");