Abstract out the cloning of SvPVX and handle shared pv in a
[p5sagit/p5-mst-13.2.git] / ext / Socket / socketpair.t
CommitLineData
02fc2eee 1#!./perl -w
2
b4023995 3my $child;
4
02fc2eee 5BEGIN {
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;
b4023995 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 }
02fc2eee 36 }
37}
b4023995 38
02fc2eee 39use Socket;
40use Test::More;
41use strict;
42use warnings;
5ec8c883 43use Errno;
02fc2eee 44
45my $skip_reason;
46
47if( !$Config{d_alarm} ) {
48 plan skip_all => "alarm() not implemented on this platform";
b4023995 49} elsif( !$Config{d_fork} ) {
50 plan skip_all => "fork() not implemented on this platform";
02fc2eee 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 {
cd0506f1 61 plan tests => 45;
02fc2eee 62 }
63 }
64}
65
b4023995 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"};
02fc2eee 68
69ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC),
70 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)")
c5f49a01 71 or print "# \$\! = $!\n";
02fc2eee 72
73my @left = ("hello ", "world\n");
74my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
75
76foreach (@left) {
77 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
78 is (syswrite (LEFT, $_), length $_, "syswrite to left");
79}
80foreach (@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:
86my ($buffer, $expect);
87$expect = join '', @right;
22954800 88undef $buffer;
02fc2eee 89is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
90is ($buffer, $expect, "content what we expected?");
91$expect = join '', @left;
22954800 92undef $buffer;
02fc2eee 93is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
94is ($buffer, $expect, "content what we expected?");
95
c5f49a01 96ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing");
b4023995 97# This will hang forever if eof is buggy, and alarm doesn't interrupt system
98# Calls. Hence the child process minder.
c5f49a01 99{
100 local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" };
e03d5f0a 101 local $TODO = "Known problems with unix sockets on $^O"
102 if $^O eq 'hpux' || $^O eq 'unicosmk';
c5f49a01 103 alarm 3;
cd0506f1 104 $! = 0;
c5f49a01 105 ok (eof RIGHT, "right is at EOF");
cd0506f1 106 is ($!, '', 'and $! should report no error');
c5f49a01 107 alarm 60;
108}
109
110$SIG{PIPE} = 'IGNORE';
111{
112 local $SIG{ALRM}
113 = sub { warn "syswrite to left didn't fail within 3 seconds" };
114 alarm 3;
115 is (syswrite (LEFT, "void"), undef, "syswrite to shutdown left should fail");
116 alarm 60;
117}
118SKIP: {
119 # This may need skipping on some OSes
5ec8c883 120 ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN')
c5f49a01 121 or printf "\$\!=%d(%s)\n", $!, $!;
122}
02fc2eee 123
124my @gripping = (chr 255, chr 127);
125foreach (@gripping) {
126 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
127}
128
129ok (!eof LEFT, "left is not at EOF");
130
131$expect = join '', @gripping;
22954800 132undef $buffer;
02fc2eee 133is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
134is ($buffer, $expect, "content what we expected?");
135
136ok (close LEFT, "close left");
137ok (close RIGHT, "close right");
138
139# And now datagrams
140# I suspect we also need a self destruct time-bomb for these, as I don't see any
141# guarantee that the stack won't drop a UDP packet, even if it is for localhost.
142
143ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
144 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
c5f49a01 145 or print "# \$\! = $!\n";
02fc2eee 146
147foreach (@left) {
148 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
149 is (syswrite (LEFT, $_), length $_, "syswrite to left");
150}
151foreach (@right) {
152 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
153 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
154}
155
156# stream socket, so our writes will become joined:
157my ($total);
158$total = join '', @right;
159foreach $expect (@right) {
22954800 160 undef $buffer;
02fc2eee 161 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
162 is ($buffer, $expect, "content what we expected?");
163}
164$total = join '', @left;
165foreach $expect (@left) {
22954800 166 undef $buffer;
02fc2eee 167 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
168 is ($buffer, $expect, "content what we expected?");
169}
170
171ok (shutdown(LEFT, 1), "shutdown left for writing");
172# eof uses buffering. eof is indicated by a sysread of zero.
173# but for a datagram socket there's no way it can know nothing will ever be
174# sent
175{
176 my $alarmed = 0;
177 local $SIG{ALRM} = sub { $alarmed = 1; };
178 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
179 alarm 3;
22954800 180 undef $buffer;
02fc2eee 181 is (sysread (RIGHT, $buffer, 1), undef,
182 "read on right should be interrupted");
183 is ($alarmed, 1, "alarm should have fired");
184}
185alarm 30;
186
187#ok (eof RIGHT, "right is at EOF");
188
189foreach (@gripping) {
190 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
191}
192
193$total = join '', @gripping;
194foreach $expect (@gripping) {
22954800 195 undef $buffer;
02fc2eee 196 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
197 is ($buffer, $expect, "content what we expected?");
198}
199
200ok (close LEFT, "close left");
201ok (close RIGHT, "close right");
b4023995 202
203kill "INT", $child or warn "Failed to kill child process $child: $!";
204exit 0;