Must cast constants if they can be quads.
[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;
18
19my $skip_reason;
20
21if( !$Config{d_alarm} ) {
22 plan skip_all => "alarm() not implemented on this platform";
23} else {
24 # This should fail but not die if there is real socketpair
25 eval {socketpair LEFT, RIGHT, -1, -1, -1};
26 if ($@ =~ /^Unsupported socket function "socketpair" called/) {
27 plan skip_all => 'No socketpair (real or emulated)';
28 } else {
29 eval {AF_UNIX};
30 if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) {
31 plan skip_all => 'No AF_UNIX';
32 } else {
33 plan tests => 42;
34 }
35 }
36}
37
38# Too many things in this test will hang forever if something is wrong, so
39# we need a self destruct timer.
40$SIG{ALRM} = sub {die "Something unexpectedly hung during testing"};
41alarm(60);
42
43ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC),
44 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)")
45 or print "# \$\! = $!";
46
47my @left = ("hello ", "world\n");
48my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
49
50foreach (@left) {
51 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
52 is (syswrite (LEFT, $_), length $_, "syswrite to left");
53}
54foreach (@right) {
55 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
56 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
57}
58
59# stream socket, so our writes will become joined:
60my ($buffer, $expect);
61$expect = join '', @right;
62is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
63is ($buffer, $expect, "content what we expected?");
64$expect = join '', @left;
65is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
66is ($buffer, $expect, "content what we expected?");
67
68ok (shutdown(LEFT, 1), "shutdown left for writing");
69# This will hang forever if eof is buggy.
70ok (eof RIGHT, "right is at EOF");
71
72my @gripping = (chr 255, chr 127);
73foreach (@gripping) {
74 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
75}
76
77ok (!eof LEFT, "left is not at EOF");
78
79$expect = join '', @gripping;
80is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
81is ($buffer, $expect, "content what we expected?");
82
83ok (close LEFT, "close left");
84ok (close RIGHT, "close right");
85
86# And now datagrams
87# I suspect we also need a self destruct time-bomb for these, as I don't see any
88# guarantee that the stack won't drop a UDP packet, even if it is for localhost.
89
90ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
91 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
92 or print "# \$\! = $!";
93
94foreach (@left) {
95 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
96 is (syswrite (LEFT, $_), length $_, "syswrite to left");
97}
98foreach (@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:
104my ($total);
105$total = join '', @right;
106foreach $expect (@right) {
107 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
108 is ($buffer, $expect, "content what we expected?");
109}
110$total = join '', @left;
111foreach $expect (@left) {
112 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
113 is ($buffer, $expect, "content what we expected?");
114}
115
116ok (shutdown(LEFT, 1), "shutdown left for writing");
117# eof uses buffering. eof is indicated by a sysread of zero.
118# but for a datagram socket there's no way it can know nothing will ever be
119# sent
120{
121 my $alarmed = 0;
122 local $SIG{ALRM} = sub { $alarmed = 1; };
123 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
124 alarm 3;
125 is (sysread (RIGHT, $buffer, 1), undef,
126 "read on right should be interrupted");
127 is ($alarmed, 1, "alarm should have fired");
128}
129alarm 30;
130
131#ok (eof RIGHT, "right is at EOF");
132
133foreach (@gripping) {
134 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
135}
136
137$total = join '', @gripping;
138foreach $expect (@gripping) {
139 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
140 is ($buffer, $expect, "content what we expected?");
141}
142
143ok (close LEFT, "close left");
144ok (close RIGHT, "close right");