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