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