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