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