File: Tree-ipv6-aliases.t

package info (click to toggle)
libmaxmind-db-writer-perl 0.300003-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,336 kB
  • sloc: ansic: 3,059; perl: 2,895; makefile: 5; sh: 4
file content (104 lines) | stat: -rw-r--r-- 3,529 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;

use Test::More;

use MaxMind::DB::Writer::Tree ();

use File::Temp qw( tempdir );
use Net::Works::Address ();
use Net::Works::Network ();
use Test::Fatal qw( exception );

my $tempdir = tempdir( CLEANUP => 1 );

{
    my %tests = (
        '::1.1.1.1'        => { subnet => '::1.1.1.1/128' },
        '::1.1.1.2'        => { subnet => '::1.1.1.2/127' },
        '::255.255.255.2'  => { subnet => '::255.255.255.0/120' },
        '::fffe:0:0'       => { subnet => '::fffe:0:0/96' },
        '::fffe:9:a'       => { subnet => '::fffe:0:0/96' },
        '::ffff:ff02'      => { subnet => '::255.255.255.0/120' },
        '::1.1.1.3'        => { subnet => '::1.1.1.2/127' },
        '::ffff:101:101'   => { subnet => '::1.1.1.1/128' },
        '::ffff:1.1.1.2'   => { subnet => '::1.1.1.2/127' },
        '::ffff:101:103'   => { subnet => '::1.1.1.2/127' },
        '::ffff:ffff:ff02' => { subnet => '::255.255.255.0/120' },
        '2001:0:101:101::' => { subnet => '::1.1.1.1/128' },
        '2001:0:101:102::' => { subnet => '::1.1.1.2/127' },
        '2001:0:101:103::' => { subnet => '::1.1.1.2/127' },
        '2002:101:101::'   => { subnet => '::1.1.1.1/128' },
        '2002:101:102::'   => { subnet => '::1.1.1.2/127' },
        '2002:101:103::'   => { subnet => '::1.1.1.2/127' },
        '2002:ffff:ff02::' => { subnet => '::255.255.255.0/120' },
        '2004::'           => { subnet => '2004::/96' },
        '2004::9:a'        => { subnet => '2004::/96' },
    );

    my $tree = _write_tree();

    for my $address ( sort keys %tests ) {
        is_deeply(
            $tree->lookup_ip_address(
                Net::Works::Address->new_from_string(
                    string => $address, version => 6
                )
            ),
            $tests{$address},
            "got expected data for $address"
        );
    }

    # Aliases should never be overwritten. Currently we just throw an
    # exception is someone tries to overwrite one. In the future, we could do
    # something smarter, _but_ it isn't clear what the right behavior is.
    like(
        exception { $tree->insert_network( '2001::/32', {} ) },
        qr/Attempted to overwrite an aliased network./,
        'received expected error when trying to overwrite an alias node'
    );
}

done_testing();

sub _write_tree {
    my $tree = MaxMind::DB::Writer::Tree->new(
        ip_version    => 6,
        record_size   => 24,
        database_type => 'Test',
        languages     => [ 'en', 'zh' ],
        description   => {
            en => 'Test Database',
            zh => 'Test Database Chinese',
        },
        alias_ipv6_to_ipv4    => 1,
        map_key_type_callback => sub { 'utf8_string' },

        # Below we try to insert into reserved networks, which fails if we flag
        # them as fixed empty (255.255.255.0/8 falls under 240.0.0.0/4).
        remove_reserved_networks => 0,
    );

    # Note: we don't want all of the alias nodes (::ffff:0.0.0.0 and 2002::)
    # to have adjacent nodes with data, as we want to make sure that the
    # merging of empty nodes does not merge the alias nodes.
    my @subnets = map {
        Net::Works::Network->new_from_string( string => $_, version => 6 )
        } qw(
        ::1.1.1.1/128
        ::1.1.1.2/127
        ::255.255.255.0/120
        ::fffe:0:0/96
        2004::/96
    );

    for my $net (@subnets) {
        $tree->insert_network(
            $net,
            { subnet => $net->as_string() },
        );
    }

    return $tree;
}