[REPATCH] Re: [PATCH] Re: socketpair blip on unicos/mk, too
[p5sagit/p5-mst-13.2.git] / ext / Socket / socketpair.t
1 #!./perl -w
2
3 my $child;
4
5 BEGIN {
6     chdir 't' if -d 't';
7     @INC = '../lib';
8     require Config; import Config;
9     if ($Config{'extensions'} !~ /\bSocket\b/ && 
10         !(($^O eq 'VMS') && $Config{d_socket})) {
11         print "1..0\n";
12         exit 0;
13       }
14
15     # Too many things in this test will hang forever if something is wrong,
16     # so we need a self destruct timer. And IO can hang despite an alarm.
17
18     # This is convoluted, but we must fork before Test::More, else child's
19     # Test::More thinks that it ran no tests, and prints a message to that
20     # effect
21     if( $Config{d_fork} ) {
22       my $parent = $$;
23       $child = fork;
24       die "Fork failed" unless defined $child;
25       if (!$child) {
26         $SIG{INT} = sub {exit 0}; # You have 60 seconds. Your time starts now.
27         my $must_finish_by = time + 60;
28         my $remaining;
29         while ($remaining = time - $must_finish_by) {
30           sleep $remaining;
31         }
32         warn "Something unexpectedly hung during testing";
33         kill "INT", $parent or die "Kill failed: $!";
34         exit 1;
35       }
36     }
37 }
38
39 use Socket;
40 use Test::More;
41 use strict;
42 use warnings;
43 use Errno;
44
45 my $skip_reason;
46
47 if( !$Config{d_alarm} ) {
48   plan skip_all => "alarm() not implemented on this platform";
49 } elsif( !$Config{d_fork} ) {
50   plan skip_all => "fork() not implemented on this platform";
51 } else {
52   # This should fail but not die if there is real socketpair
53   eval {socketpair LEFT, RIGHT, -1, -1, -1};
54   if ($@ =~ /^Unsupported socket function "socketpair" called/) {
55     plan skip_all => 'No socketpair (real or emulated)';
56   } else {
57     eval {AF_UNIX};
58     if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) {
59       plan skip_all => 'No AF_UNIX';
60     } else {
61       plan tests => 45;
62     }
63   }
64 }
65
66 # But we'll install an alarm handler in case any of the races below fail.
67 $SIG{ALRM} = sub {die "Unexpected alarm during testing"};
68
69 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC),
70     "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)")
71   or print "# \$\! = $!\n";
72
73 my @left = ("hello ", "world\n");
74 my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
75
76 foreach (@left) {
77   # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
78   is (syswrite (LEFT, $_), length $_, "syswrite to left");
79 }
80 foreach (@right) {
81   # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
82   is (syswrite (RIGHT, $_), length $_, "syswrite to right");
83 }
84
85 # stream socket, so our writes will become joined:
86 my ($buffer, $expect);
87 $expect = join '', @right;
88 undef $buffer;
89 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
90 is ($buffer, $expect, "content what we expected?");
91 $expect = join '', @left;
92 undef $buffer;
93 is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
94 is ($buffer, $expect, "content what we expected?");
95
96 ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing");
97 # This will hang forever if eof is buggy, and alarm doesn't interrupt system
98 # Calls. Hence the child process minder.
99 {
100   local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" };
101   local $TODO = "Known problems with unix sockets on $^O" if $^O eq 'hpux';
102   alarm 3;
103   $! = 0;
104   ok (eof RIGHT, "right is at EOF");
105   is ($!, '', 'and $! should report no error');
106   alarm 60;
107 }
108
109 $SIG{PIPE} = 'IGNORE';
110 {
111   local $SIG{ALRM}
112     = sub { warn "syswrite to left didn't fail within 3 seconds" };
113   alarm 3;
114   is (syswrite (LEFT, "void"), undef, "syswrite to shutdown left should fail");
115   alarm 60;
116 }
117 SKIP: {
118   # This may need skipping on some OSes
119   ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN')
120     or printf "\$\!=%d(%s)\n", $!, $!;
121 }
122
123 my @gripping = (chr 255, chr 127);
124 foreach (@gripping) {
125   is (syswrite (RIGHT, $_), length $_, "syswrite to right");
126 }
127
128 ok (!eof LEFT, "left is not at EOF");
129
130 $expect = join '', @gripping;
131 undef $buffer;
132 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
133 is ($buffer, $expect, "content what we expected?");
134
135 ok (close LEFT, "close left");
136 ok (close RIGHT, "close right");
137
138 # And now datagrams
139 # I suspect we also need a self destruct time-bomb for these, as I don't see any
140 # guarantee that the stack won't drop a UDP packet, even if it is for localhost.
141
142 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
143     "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
144   or print "# \$\! = $!\n";
145
146 foreach (@left) {
147   # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
148   is (syswrite (LEFT, $_), length $_, "syswrite to left");
149 }
150 foreach (@right) {
151   # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
152   is (syswrite (RIGHT, $_), length $_, "syswrite to right");
153 }
154
155 # stream socket, so our writes will become joined:
156 my ($total);
157 $total = join '', @right;
158 foreach $expect (@right) {
159   undef $buffer;
160   is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
161   is ($buffer, $expect, "content what we expected?");
162 }
163 $total = join '', @left;
164 foreach $expect (@left) {
165   undef $buffer;
166   is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
167   is ($buffer, $expect, "content what we expected?");
168 }
169
170 ok (shutdown(LEFT, 1), "shutdown left for writing");
171 # eof uses buffering. eof is indicated by a sysread of zero.
172 # but for a datagram socket there's no way it can know nothing will ever be
173 # sent
174 {
175   my $alarmed = 0;
176   local $SIG{ALRM} = sub { $alarmed = 1; };
177   print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
178   alarm 3;
179   undef $buffer;
180   is (sysread (RIGHT, $buffer, 1), undef,
181       "read on right should be interrupted");
182   is ($alarmed, 1, "alarm should have fired");
183 }
184 alarm 30;
185
186 #ok (eof RIGHT, "right is at EOF");
187
188 foreach (@gripping) {
189   is (syswrite (RIGHT, $_), length $_, "syswrite to right");
190 }
191
192 $total = join '', @gripping;
193 foreach $expect (@gripping) {
194   undef $buffer;
195   is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
196   is ($buffer, $expect, "content what we expected?");
197 }
198
199 ok (close LEFT, "close left");
200 ok (close RIGHT, "close right");
201
202 kill "INT", $child or warn "Failed to kill child process $child: $!";
203 exit 0;