1use strict;
2use warnings;
3
4use Test::More;
5
6use Test::Requires (
7    'MaxMind::DB::Reader' => 0.040000,
8);
9
10use MaxMind::DB::Writer::Tree;
11
12use File::Temp qw( tempdir );
13use MaxMind::DB::Reader;
14use Net::Works::Network;
15
16my $tempdir = tempdir( CLEANUP => 1 );
17
18{
19    my $filename = _write_tree();
20
21    my $reader = MaxMind::DB::Reader->new( file => $filename );
22
23    for my $address (qw( 0.0.0.0 0.0.0.1 0.0.0.255 )) {
24        is_deeply(
25            $reader->record_for_address($address),
26            {
27                ip => '0.0.0.0',
28            },
29            "got expected data for $address"
30        );
31    }
32}
33
34done_testing();
35
36sub _write_tree {
37    my $tree = MaxMind::DB::Writer::Tree->new(
38        ip_version    => 4,
39        record_size   => 24,
40        database_type => 'Test',
41        languages     => [ 'en', 'zh' ],
42        description   => {
43            en => 'Test Database',
44            zh => 'Test Database Chinese',
45        },
46        map_key_type_callback    => sub { 'utf8_string' },
47        remove_reserved_networks => 0
48    );
49
50    my $subnet = Net::Works::Network->new_from_string(
51        string  => '0.0.0.0/24',
52        version => 4,
53    );
54
55    $tree->insert_network(
56        $subnet,
57        { ip => '0.0.0.0' },
58    );
59
60    my $filename = $tempdir . '/Test-0-network.mmdb';
61    open my $fh, '>', $filename or die $!;
62    $tree->write_tree($fh);
63    close $fh or die $!;
64
65    return $filename;
66}
67