File: 11_net_emptyport.t

package info (click to toggle)
libtest-tcp-perl 2.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 224 kB
  • sloc: perl: 299; makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,371 bytes parent folder | download | duplicates (5)
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
use warnings;
use strict;
use Test::More tests => 11;
use Net::EmptyPort qw(empty_port check_port);
use IO::Socket::INET;

# This UDP test case does not portable if there is DNS cache server.

my ($port, $new_port, $sock);

foreach my $proto_uc ('TCP', 'UDP') {
    my $proto = lc $proto_uc;
    $port = empty_port(5000, $proto);
    ok( $port, $proto_uc.': port found via empty_port' );

    diag "Port: $port - $proto";

    $sock = new_ok( 'IO::Socket::INET' => [
        (($proto eq 'udp') ? () : (Listen => 5)),
        LocalAddr => '127.0.0.1',
        LocalPort => $port,
        Proto     => $proto,
        (($^O eq 'MSWin32') ? () : (ReuseAddr => 1)),
    ] );

    $new_port = empty_port($port, $proto);
    isnt( $new_port, $port, $proto_uc.': different port found via empty_port' );
    diag "New port: $new_port - $proto";

    $sock->close;
    $sock = new_ok( 'IO::Socket::INET' => [
        (($proto eq 'udp') ? () : (Listen => 5)),
        LocalAddr => '127.0.0.1',
        LocalPort => $port,
        PeerAddr  => '127.0.0.1',
        PeerPort  => $new_port,
        Proto     => $proto,
        (($^O eq 'MSWin32') ? () : (ReuseAddr => 1)),
    ] );

    is( check_port($port)    , 0, $proto_uc.': check_port($port)     == 0') if ($proto eq 'udp');
    is( check_port($new_port), 0, $proto_uc.': check_port($new_port) == 0');

    $sock->close;
}

1;