summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/perl/dist/IO/t/io_udp.t
blob: 2adc6a4a6927e7b21f3a2571fa2c8d6e385eefcd (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!./perl

BEGIN {
    require($ENV{PERL_CORE} ? '../../t/test.pl' : './t/test.pl');

    use Config;
    my $reason;
    if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bSocket\b/) {
      $reason = 'Socket was not built';
    }
    elsif ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bIO\b/) {
      $reason = 'IO was not built';
    }
    undef $reason if $^O eq 'VMS' and $Config{d_socket};
    skip_all($reason) if $reason;
}

use strict;

sub compare_addr {
    no utf8;
    my $a = shift;
    my $b = shift;
    if (length($a) != length $b) {
	my $min = (length($a) < length $b) ? length($a) : length $b;
	if ($min and substr($a, 0, $min) eq substr($b, 0, $min)) {
	    printf "# Apparently: %d bytes junk at the end of %s\n# %s\n",
		abs(length($a) - length ($b)),
		$_[length($a) < length ($b) ? 1 : 0],
		"consider decreasing bufsize of recfrom.";
	    substr($a, $min) = "";
	    substr($b, $min) = "";
	}
	return 0;
    }
    my @a = unpack_sockaddr_in($a);
    my @b = unpack_sockaddr_in($b);
    "$a[0]$a[1]" eq "$b[0]$b[1]";
}

plan(15);
watchdog(15);

use Socket;
use IO::Socket qw(AF_INET SOCK_DGRAM INADDR_ANY);

my $udpa = IO::Socket::INET->new(Proto => 'udp', LocalAddr => 'localhost')
     || IO::Socket::INET->new(Proto => 'udp', LocalAddr => '127.0.0.1')
    or die "$! (maybe your system does not have a localhost at all, 'localhost' or 127.0.0.1)";
ok(1);

my $udpb = IO::Socket::INET->new(Proto => 'udp', LocalAddr => 'localhost')
     || IO::Socket::INET->new(Proto => 'udp', LocalAddr => '127.0.0.1')
    or die "$! (maybe your system does not have a localhost at all, 'localhost' or 127.0.0.1)";
ok(1);

$udpa->send('BORK', 0, $udpb->sockname);

ok(compare_addr($udpa->peername,$udpb->sockname, 'peername', 'sockname'));

my $buf;
my $where = $udpb->recv($buf="", 4);
is($buf, 'BORK');

my @xtra = ();

if (! ok(compare_addr($where,$udpa->sockname, 'recv name', 'sockname'))) {
    @xtra = (0, $udpa->sockname);
}

$udpb->send('FOObar', @xtra);
$udpa->recv($buf="", 6);
is($buf, 'FOObar');

{
    # check the TO parameter passed to $sock->send() is honoured for UDP sockets
    # [perl #133936]
    my $udpc = IO::Socket::INET->new(Proto => 'udp', LocalAddr => 'localhost')
      || IO::Socket::INET->new(Proto => 'udp', LocalAddr => '127.0.0.1')
      or die "$! (maybe your system does not have a localhost at all, 'localhost' or 127.0.0.1)";
    pass("created C socket");

    ok($udpc->connect($udpa->sockname), "connect C to A");

    ok($udpc->connected, "connected a UDP socket");

    ok($udpc->send("fromctoa"), "send to a");

    ok($udpa->recv($buf = "", 8), "recv it");
    is($buf, "fromctoa", "check value received");

  SKIP:
    {
        $^O eq "linux"
	  or skip "This is non-portable, known to 'work' on Linux", 3;
        ok($udpc->send("fromctob", 0, $udpb->sockname), "send to non-connected socket");
        ok($udpb->recv($buf = "", 8), "recv it");
        is($buf, "fromctob", "check value received");
    }
}

exit(0);

# EOF