File: v4-aton.t

package info (click to toggle)
libnetaddr-ip-perl 4.079%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 1,580 kB
  • ctags: 251
  • sloc: perl: 1,417; cpp: 67; sh: 51; makefile: 9
file content (59 lines) | stat: -rw-r--r-- 1,577 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
48
49
50
51
52
53
54
55
56
57
58
59
use Test::More tests => 19;

my @addr = (
	[ 'localhost', '127.0.0.1' ],
	[ 'broadcast', '255.255.255.255' ],
	[ '254.254.0.1', '254.254.0.1' ],
	[ 'default', '0.0.0.0' ],
	[ '10.0.0.1', '10.0.0.1' ],

);
my %addr = (
        localhost       => pack('N',0x7f000001),
        broadcast       => pack('N',0xffffffff),
        '254.254.0.1'   => pack('N',0xfefe0001),
        default         => pack('N',0),
        '10.0.0.1'      => pack('N',0x0a000001),
	'127.0.0.1'	=> pack('N',0x7f000001),
	'255.255.255.255' => pack('N',0xffffffff),
	'0.0.0.0'	=> pack('N',0),
);

# local inet_aton, don't use perl's Socket

sub l_inet_aton {
  my $rv = (exists $addr{$_[0]}) ? $addr{$_[0]} : undef;
}


# Verify that Accept_Binary_IP works...

my $x;

SKIP:
{
    skip "Failed to load NetAddr::IP::Lite", 17
	unless use_ok('NetAddr::IP::Lite');

    ok(! defined NetAddr::IP::Lite->new("\1\1\1\1"),
       "binary unrecognized by default ". ($x ? $x->addr :''));

    # This mimicks the actual use with :aton
    NetAddr::IP::Lite::import(':aton');

    ok(defined ($x = NetAddr::IP::Lite->new("\1\1\1\1")),
       "...but can be recognized ". $x->addr);

    ok(!defined ($x = NetAddr::IP::Lite->new('bad rfc-952 characters')),
	"bad rfc-952 characters ". ($x ? $x->addr :''));

    is(NetAddr::IP::Lite->new($_->[0])->aton, l_inet_aton($_->[1]), "->aton($_->[0])")
	for @addr;

    ok(defined NetAddr::IP::Lite->new(l_inet_aton($_->[1])), "->new aton($_->[1])")
	for @addr;

    is(NetAddr::IP::Lite->new(l_inet_aton($_->[1]))->addr, $_->[1],
       "->new aton($_->[1])")
	for @addr;
};