Commit | Line | Data |
02fc2eee |
1 | #!./perl -w |
2 | |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
5 | @INC = '../lib'; |
6 | require Config; import Config; |
7 | if ($Config{'extensions'} !~ /\bSocket\b/ && |
8 | !(($^O eq 'VMS') && $Config{d_socket})) { |
9 | print "1..0\n"; |
10 | exit 0; |
11 | } |
12 | } |
13 | |
14 | use Socket; |
15 | use Test::More; |
16 | use strict; |
17 | use warnings; |
c5f49a01 |
18 | use Errno 'EPIPE'; |
02fc2eee |
19 | |
20 | my $skip_reason; |
21 | |
22 | if( !$Config{d_alarm} ) { |
23 | plan skip_all => "alarm() not implemented on this platform"; |
24 | } else { |
25 | # This should fail but not die if there is real socketpair |
26 | eval {socketpair LEFT, RIGHT, -1, -1, -1}; |
27 | if ($@ =~ /^Unsupported socket function "socketpair" called/) { |
28 | plan skip_all => 'No socketpair (real or emulated)'; |
29 | } else { |
30 | eval {AF_UNIX}; |
31 | if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) { |
32 | plan skip_all => 'No AF_UNIX'; |
33 | } else { |
cd0506f1 |
34 | plan tests => 45; |
02fc2eee |
35 | } |
36 | } |
37 | } |
38 | |
39 | # Too many things in this test will hang forever if something is wrong, so |
40 | # we need a self destruct timer. |
41 | $SIG{ALRM} = sub {die "Something unexpectedly hung during testing"}; |
42 | alarm(60); |
43 | |
44 | ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC), |
45 | "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)") |
c5f49a01 |
46 | or print "# \$\! = $!\n"; |
02fc2eee |
47 | |
48 | my @left = ("hello ", "world\n"); |
49 | my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here. |
50 | |
51 | foreach (@left) { |
52 | # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left"); |
53 | is (syswrite (LEFT, $_), length $_, "syswrite to left"); |
54 | } |
55 | foreach (@right) { |
56 | # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right"); |
57 | is (syswrite (RIGHT, $_), length $_, "syswrite to right"); |
58 | } |
59 | |
60 | # stream socket, so our writes will become joined: |
61 | my ($buffer, $expect); |
62 | $expect = join '', @right; |
63 | is (read (LEFT, $buffer, length $expect), length $expect, "read on left"); |
64 | is ($buffer, $expect, "content what we expected?"); |
65 | $expect = join '', @left; |
66 | is (read (RIGHT, $buffer, length $expect), length $expect, "read on right"); |
67 | is ($buffer, $expect, "content what we expected?"); |
68 | |
c5f49a01 |
69 | ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing"); |
02fc2eee |
70 | # This will hang forever if eof is buggy. |
c5f49a01 |
71 | { |
72 | local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" }; |
73 | alarm 3; |
cd0506f1 |
74 | $! = 0; |
c5f49a01 |
75 | ok (eof RIGHT, "right is at EOF"); |
cd0506f1 |
76 | is ($!, '', 'and $! should report no error'); |
c5f49a01 |
77 | alarm 60; |
78 | } |
79 | |
80 | $SIG{PIPE} = 'IGNORE'; |
81 | { |
82 | local $SIG{ALRM} |
83 | = sub { warn "syswrite to left didn't fail within 3 seconds" }; |
84 | alarm 3; |
85 | is (syswrite (LEFT, "void"), undef, "syswrite to shutdown left should fail"); |
86 | alarm 60; |
87 | } |
88 | SKIP: { |
89 | # This may need skipping on some OSes |
90 | ok ($! == EPIPE, '$! should be EPIPE') |
91 | or printf "\$\!=%d(%s)\n", $!, $!; |
92 | } |
02fc2eee |
93 | |
94 | my @gripping = (chr 255, chr 127); |
95 | foreach (@gripping) { |
96 | is (syswrite (RIGHT, $_), length $_, "syswrite to right"); |
97 | } |
98 | |
99 | ok (!eof LEFT, "left is not at EOF"); |
100 | |
101 | $expect = join '', @gripping; |
102 | is (read (LEFT, $buffer, length $expect), length $expect, "read on left"); |
103 | is ($buffer, $expect, "content what we expected?"); |
104 | |
105 | ok (close LEFT, "close left"); |
106 | ok (close RIGHT, "close right"); |
107 | |
108 | # And now datagrams |
109 | # I suspect we also need a self destruct time-bomb for these, as I don't see any |
110 | # guarantee that the stack won't drop a UDP packet, even if it is for localhost. |
111 | |
112 | ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC), |
113 | "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)") |
c5f49a01 |
114 | or print "# \$\! = $!\n"; |
02fc2eee |
115 | |
116 | foreach (@left) { |
117 | # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left"); |
118 | is (syswrite (LEFT, $_), length $_, "syswrite to left"); |
119 | } |
120 | foreach (@right) { |
121 | # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right"); |
122 | is (syswrite (RIGHT, $_), length $_, "syswrite to right"); |
123 | } |
124 | |
125 | # stream socket, so our writes will become joined: |
126 | my ($total); |
127 | $total = join '', @right; |
128 | foreach $expect (@right) { |
129 | is (sysread (LEFT, $buffer, length $total), length $expect, "read on left"); |
130 | is ($buffer, $expect, "content what we expected?"); |
131 | } |
132 | $total = join '', @left; |
133 | foreach $expect (@left) { |
134 | is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right"); |
135 | is ($buffer, $expect, "content what we expected?"); |
136 | } |
137 | |
138 | ok (shutdown(LEFT, 1), "shutdown left for writing"); |
139 | # eof uses buffering. eof is indicated by a sysread of zero. |
140 | # but for a datagram socket there's no way it can know nothing will ever be |
141 | # sent |
142 | { |
143 | my $alarmed = 0; |
144 | local $SIG{ALRM} = sub { $alarmed = 1; }; |
145 | print "# Approximate forever as 3 seconds. Wait 'forever'...\n"; |
146 | alarm 3; |
147 | is (sysread (RIGHT, $buffer, 1), undef, |
148 | "read on right should be interrupted"); |
149 | is ($alarmed, 1, "alarm should have fired"); |
150 | } |
151 | alarm 30; |
152 | |
153 | #ok (eof RIGHT, "right is at EOF"); |
154 | |
155 | foreach (@gripping) { |
156 | is (syswrite (RIGHT, $_), length $_, "syswrite to right"); |
157 | } |
158 | |
159 | $total = join '', @gripping; |
160 | foreach $expect (@gripping) { |
161 | is (sysread (LEFT, $buffer, length $total), length $expect, "read on left"); |
162 | is ($buffer, $expect, "content what we expected?"); |
163 | } |
164 | |
165 | ok (close LEFT, "close left"); |
166 | ok (close RIGHT, "close right"); |