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