| 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; |
| 18 | use Errno 'EPIPE'; |
| 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 { |
| 34 | plan tests => 45; |
| 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)") |
| 46 | or print "# \$\! = $!\n"; |
| 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 | undef $buffer; |
| 64 | is (read (LEFT, $buffer, length $expect), length $expect, "read on left"); |
| 65 | is ($buffer, $expect, "content what we expected?"); |
| 66 | $expect = join '', @left; |
| 67 | undef $buffer; |
| 68 | is (read (RIGHT, $buffer, length $expect), length $expect, "read on right"); |
| 69 | is ($buffer, $expect, "content what we expected?"); |
| 70 | |
| 71 | ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing"); |
| 72 | # This will hang forever if eof is buggy. |
| 73 | { |
| 74 | local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" }; |
| 75 | alarm 3; |
| 76 | $! = 0; |
| 77 | ok (eof RIGHT, "right is at EOF"); |
| 78 | is ($!, '', 'and $! should report no error'); |
| 79 | alarm 60; |
| 80 | } |
| 81 | |
| 82 | $SIG{PIPE} = 'IGNORE'; |
| 83 | { |
| 84 | local $SIG{ALRM} |
| 85 | = sub { warn "syswrite to left didn't fail within 3 seconds" }; |
| 86 | alarm 3; |
| 87 | is (syswrite (LEFT, "void"), undef, "syswrite to shutdown left should fail"); |
| 88 | alarm 60; |
| 89 | } |
| 90 | SKIP: { |
| 91 | # This may need skipping on some OSes |
| 92 | ok ($! == EPIPE, '$! should be EPIPE') |
| 93 | or printf "\$\!=%d(%s)\n", $!, $!; |
| 94 | } |
| 95 | |
| 96 | my @gripping = (chr 255, chr 127); |
| 97 | foreach (@gripping) { |
| 98 | is (syswrite (RIGHT, $_), length $_, "syswrite to right"); |
| 99 | } |
| 100 | |
| 101 | ok (!eof LEFT, "left is not at EOF"); |
| 102 | |
| 103 | $expect = join '', @gripping; |
| 104 | undef $buffer; |
| 105 | is (read (LEFT, $buffer, length $expect), length $expect, "read on left"); |
| 106 | is ($buffer, $expect, "content what we expected?"); |
| 107 | |
| 108 | ok (close LEFT, "close left"); |
| 109 | ok (close RIGHT, "close right"); |
| 110 | |
| 111 | # And now datagrams |
| 112 | # I suspect we also need a self destruct time-bomb for these, as I don't see any |
| 113 | # guarantee that the stack won't drop a UDP packet, even if it is for localhost. |
| 114 | |
| 115 | ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC), |
| 116 | "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)") |
| 117 | or print "# \$\! = $!\n"; |
| 118 | |
| 119 | foreach (@left) { |
| 120 | # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left"); |
| 121 | is (syswrite (LEFT, $_), length $_, "syswrite to left"); |
| 122 | } |
| 123 | foreach (@right) { |
| 124 | # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right"); |
| 125 | is (syswrite (RIGHT, $_), length $_, "syswrite to right"); |
| 126 | } |
| 127 | |
| 128 | # stream socket, so our writes will become joined: |
| 129 | my ($total); |
| 130 | $total = join '', @right; |
| 131 | foreach $expect (@right) { |
| 132 | undef $buffer; |
| 133 | is (sysread (LEFT, $buffer, length $total), length $expect, "read on left"); |
| 134 | is ($buffer, $expect, "content what we expected?"); |
| 135 | } |
| 136 | $total = join '', @left; |
| 137 | foreach $expect (@left) { |
| 138 | undef $buffer; |
| 139 | is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right"); |
| 140 | is ($buffer, $expect, "content what we expected?"); |
| 141 | } |
| 142 | |
| 143 | ok (shutdown(LEFT, 1), "shutdown left for writing"); |
| 144 | # eof uses buffering. eof is indicated by a sysread of zero. |
| 145 | # but for a datagram socket there's no way it can know nothing will ever be |
| 146 | # sent |
| 147 | { |
| 148 | my $alarmed = 0; |
| 149 | local $SIG{ALRM} = sub { $alarmed = 1; }; |
| 150 | print "# Approximate forever as 3 seconds. Wait 'forever'...\n"; |
| 151 | alarm 3; |
| 152 | undef $buffer; |
| 153 | is (sysread (RIGHT, $buffer, 1), undef, |
| 154 | "read on right should be interrupted"); |
| 155 | is ($alarmed, 1, "alarm should have fired"); |
| 156 | } |
| 157 | alarm 30; |
| 158 | |
| 159 | #ok (eof RIGHT, "right is at EOF"); |
| 160 | |
| 161 | foreach (@gripping) { |
| 162 | is (syswrite (RIGHT, $_), length $_, "syswrite to right"); |
| 163 | } |
| 164 | |
| 165 | $total = join '', @gripping; |
| 166 | foreach $expect (@gripping) { |
| 167 | undef $buffer; |
| 168 | is (sysread (LEFT, $buffer, length $total), length $expect, "read on left"); |
| 169 | is ($buffer, $expect, "content what we expected?"); |
| 170 | } |
| 171 | |
| 172 | ok (close LEFT, "close left"); |
| 173 | ok (close RIGHT, "close right"); |