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