This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update the Change log in Module::CoreList to include recent commits
[perl5.git] / cpan / autodie / t / recv.t
CommitLineData
0b09a93a
PF
1#!/usr/bin/perl -w
2use strict;
3use Test::More tests => 8;
4use Socket;
5use autodie qw(socketpair);
6
7# All of this code is based around recv returning an empty
8# string when it gets data from a local machine (using AF_UNIX),
9# but returning an undefined value on error. Fatal/autodie
10# should be able to tell the difference.
11
12$SIG{PIPE} = 'IGNORE';
13
14my ($sock1, $sock2);
15socketpair($sock1, $sock2, AF_UNIX, SOCK_STREAM, PF_UNSPEC);
16
17my $buffer;
18send($sock1, "xyz", 0);
19my $ret = recv($sock2, $buffer, 2, 0);
20
21use autodie qw(recv);
22
23SKIP: {
24
25 skip('recv() never returns empty string with socketpair emulation',4)
26 if ($ret);
27
28 is($buffer,'xy',"recv() operational without autodie");
29
30 # Read the last byte from the socket.
31 eval { $ret = recv($sock2, $buffer, 1, 0); };
32
33 is($@, "", "recv should not die on returning an emtpy string.");
34
35 is($buffer,"z","recv() operational with autodie");
36 is($ret,"","recv returns undying empty string for local sockets");
37
38}
39
40eval {
41 # STDIN isn't a socket, so this should fail.
42 recv(STDIN,$buffer,1,0);
43};
44
45ok($@,'recv dies on returning undef');
46isa_ok($@,'autodie::exception');
47
48$buffer = "# Not an empty string\n";
49
50# Terminate writing for $sock1
51shutdown($sock1, 1);
52
53eval {
54 use autodie qw(send);
55 # Writing to a socket terminated for writing should fail.
56 send($sock1,$buffer,0);
57};
58
59ok($@,'send dies on returning undef');
60isa_ok($@,'autodie::exception');