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