Ooops. Retract the regex parts of #14090.
[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" };
b4023995 101 local $TODO = "Known problems with unix sockets on $^O" if $^O eq 'hpux';
c5f49a01 102 alarm 3;
cd0506f1 103 $! = 0;
c5f49a01 104 ok (eof RIGHT, "right is at EOF");
cd0506f1 105 is ($!, '', 'and $! should report no error');
c5f49a01 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}
117SKIP: {
118 # This may need skipping on some OSes
5ec8c883 119 ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN')
c5f49a01 120 or printf "\$\!=%d(%s)\n", $!, $!;
121}
02fc2eee 122
123my @gripping = (chr 255, chr 127);
124foreach (@gripping) {
125 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
126}
127
128ok (!eof LEFT, "left is not at EOF");
129
130$expect = join '', @gripping;
22954800 131undef $buffer;
02fc2eee 132is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
133is ($buffer, $expect, "content what we expected?");
134
135ok (close LEFT, "close left");
136ok (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
142ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
143 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
c5f49a01 144 or print "# \$\! = $!\n";
02fc2eee 145
146foreach (@left) {
147 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
148 is (syswrite (LEFT, $_), length $_, "syswrite to left");
149}
150foreach (@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:
156my ($total);
157$total = join '', @right;
158foreach $expect (@right) {
22954800 159 undef $buffer;
02fc2eee 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;
164foreach $expect (@left) {
22954800 165 undef $buffer;
02fc2eee 166 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
167 is ($buffer, $expect, "content what we expected?");
168}
169
170ok (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;
22954800 179 undef $buffer;
02fc2eee 180 is (sysread (RIGHT, $buffer, 1), undef,
181 "read on right should be interrupted");
182 is ($alarmed, 1, "alarm should have fired");
183}
184alarm 30;
185
186#ok (eof RIGHT, "right is at EOF");
187
188foreach (@gripping) {
189 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
190}
191
192$total = join '', @gripping;
193foreach $expect (@gripping) {
22954800 194 undef $buffer;
02fc2eee 195 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
196 is ($buffer, $expect, "content what we expected?");
197}
198
199ok (close LEFT, "close left");
200ok (close RIGHT, "close right");
b4023995 201
202kill "INT", $child or warn "Failed to kill child process $child: $!";
203exit 0;