Fast Latin1<->UTF-8 conversion for older Perls.
[p5sagit/p5-mst-13.2.git] / ext / Socket / socketpair.t
CommitLineData
02fc2eee 1#!./perl -w
2
3BEGIN {
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
14use Socket;
15use Test::More;
16use strict;
17use warnings;
c5f49a01 18use Errno 'EPIPE';
02fc2eee 19
20my $skip_reason;
21
22if( !$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"};
42alarm(60);
43
44ok (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
48my @left = ("hello ", "world\n");
49my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
50
51foreach (@left) {
52 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
53 is (syswrite (LEFT, $_), length $_, "syswrite to left");
54}
55foreach (@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:
61my ($buffer, $expect);
62$expect = join '', @right;
63is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
64is ($buffer, $expect, "content what we expected?");
65$expect = join '', @left;
66is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
67is ($buffer, $expect, "content what we expected?");
68
c5f49a01 69ok (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}
88SKIP: {
89 # This may need skipping on some OSes
90 ok ($! == EPIPE, '$! should be EPIPE')
91 or printf "\$\!=%d(%s)\n", $!, $!;
92}
02fc2eee 93
94my @gripping = (chr 255, chr 127);
95foreach (@gripping) {
96 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
97}
98
99ok (!eof LEFT, "left is not at EOF");
100
101$expect = join '', @gripping;
102is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
103is ($buffer, $expect, "content what we expected?");
104
105ok (close LEFT, "close left");
106ok (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
112ok (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
116foreach (@left) {
117 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
118 is (syswrite (LEFT, $_), length $_, "syswrite to left");
119}
120foreach (@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:
126my ($total);
127$total = join '', @right;
128foreach $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;
133foreach $expect (@left) {
134 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
135 is ($buffer, $expect, "content what we expected?");
136}
137
138ok (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}
151alarm 30;
152
153#ok (eof RIGHT, "right is at EOF");
154
155foreach (@gripping) {
156 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
157}
158
159$total = join '', @gripping;
160foreach $expect (@gripping) {
161 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
162 is ($buffer, $expect, "content what we expected?");
163}
164
165ok (close LEFT, "close left");
166ok (close RIGHT, "close right");