File: recv.t

package info (click to toggle)
libautodie-perl 2.12-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 564 kB
  • sloc: perl: 4,186; makefile: 2
file content (63 lines) | stat: -rwxr-xr-x 1,564 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/perl -w
use strict;
use Test::More tests => 8;
use Socket;
use autodie qw(socketpair);

# All of this code is based around recv returning an empty
# string when it gets data from a local machine (using AF_UNIX),
# but returning an undefined value on error.  Fatal/autodie
# should be able to tell the difference.

$SIG{PIPE} = 'IGNORE';

my ($sock1, $sock2);
socketpair($sock1, $sock2, AF_UNIX, SOCK_STREAM, PF_UNSPEC);

my $buffer;
send($sock1, "xyz", 0);
my $ret = recv($sock2, $buffer, 2, 0);

use autodie qw(recv);

SKIP: {

    skip('recv() never returns empty string with socketpair emulation',4)
        if ($ret);

    is($buffer,'xy',"recv() operational without autodie");

    # Read the last byte from the socket.
    eval { $ret = recv($sock2, $buffer, 1, 0); };

    is($@, "", "recv should not die on returning an emtpy string.");

    is($buffer,"z","recv() operational with autodie");
    is($ret,"","recv returns undying empty string for local sockets");

}

eval {
    my $string = "now is the time...";
    open(my $fh, '<', \$string) or die("Can't open \$string for read");
    # $fh isn't a socket, so this should fail.
    recv($fh,$buffer,1,0);
};

ok($@,'recv dies on returning undef');
isa_ok($@,'autodie::exception')
    or diag("$@");

$buffer = "# Not an empty string\n";

# Terminate writing for $sock1
shutdown($sock1, 1);

eval {
    use autodie qw(send);
    # Writing to a socket terminated for writing should fail.
    send($sock1,$buffer,0);
};

ok($@,'send dies on returning undef');
isa_ok($@,'autodie::exception');