| 1 | use strictscip-perl cpan MaxMind-DB-Writer 0.300004 strict/; |
| 2 | use warningsscip-perl cpan MaxMind-DB-Writer 0.300004 warnings/; |
| 3 | use utf8scip-perl cpan MaxMind-DB-Writer 0.300004 utf8/; |
| 4 | use autodiescip-perl cpan MaxMind-DB-Writer 0.300004 autodie/; |
| 5 | |
| 6 | use libscip-perl cpan MaxMind-DB-Writer 0.300004 lib/ 't/lib'; |
| 7 | |
| 8 | use Test::Requiresscip-perl cpan MaxMind-DB-Writer 0.300004 Test/Requires/ { |
| 9 | JSON => 0, |
| 10 | 'MaxMind::DB::Reader' => 0.040000, |
| 11 | }; |
| 12 | |
| 13 | use Test::MaxMind::DB::Writerscip-perl cpan MaxMind-DB-Writer 0.300004 Test/MaxMind/DB/Writer/ qw( test_freeze_thaw ); |
| 14 | use Test::Morescip-perl cpan MaxMind-DB-Writer 0.300004 Test/More/; |
| 15 | |
| 16 | use File::Tempscip-perl cpan MaxMind-DB-Writer 0.300004 File/Temp/ qw( tempdir ); |
| 17 | use JSONscip-perl cpan MaxMind-DB-Writer 0.300004 JSON/; |
| 18 | use Math::Int128scip-perl cpan MaxMind-DB-Writer 0.300004 Math/Int128/ qw( uint128 ); |
| 19 | use MaxMind::DB::Readerscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Reader/; |
| 20 | use MaxMind::DB::Writer::Treescip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/; |
| 21 | use Net::Works::Networkscip-perl cpan MaxMind-DB-Writer 0.300004 Net/Works/Network/; |
| 22 | |
| 23 | my $json_file = 't/test-data/GeoLite2-Country.json'; |
| 24 | unless ( -f $json_file ) { |
| 25 | diagscip-perl cpan MaxMind-DB-Writer 0.300004 main/diag().(<<"EOF"); |
| 26 | |
| 27 | In order to run this test you need to create a JSON dump of a GeoLite2 Country |
| 28 | database. You can do this with the mmdb-dump-database script that ships with |
| 29 | MaxMind::DB::Reader. Note that this script doesn't generate 100% correct JSON, |
| 30 | so you'll need to trim off the trailing comma it leaves in on the last record |
| 31 | of the array. |
| 32 | |
| 33 | Save this JSON file to $json_file |
| 34 | |
| 35 | We don't include this file in the repo since it ends up being around 650MB in |
| 36 | size. |
| 37 | |
| 38 | EOF |
| 39 | planscip-perl cpan MaxMind-DB-Writer 0.300004 main/plan(). skip_all => "This test requires the $json_file file"; |
| 40 | } |
| 41 | |
| 42 | my @languages = qw( |
| 43 | de |
| 44 | en |
| 45 | es |
| 46 | fr |
| 47 | ja |
| 48 | pt-BR |
| 49 | ru |
| 50 | zh-CN |
| 51 | ); |
| 52 | |
| 53 | my %type_map = ( |
| 54 | city => 'map', |
| 55 | continent => 'map', |
| 56 | country => 'map', |
| 57 | geoname_id => 'uint32', |
| 58 | is_anonymous_proxy => 'boolean', |
| 59 | is_satellite_provider => 'boolean', |
| 60 | latitude => 'double', |
| 61 | location => 'map', |
| 62 | longitude => 'double', |
| 63 | metro_code => 'uint16', |
| 64 | names => 'map', |
| 65 | postal => 'map', |
| 66 | registered_country => 'map', |
| 67 | represented_country => 'map', |
| 68 | subdivisions => [ 'array', 'map' ], |
| 69 | traits => 'map', |
| 70 | ); |
| 71 | |
| 72 | my $map_key_type_callback = sub { $type_map{ $_scip-perl cpan MaxMind-DB-Writer 0.300004 main/`@_`.[0] } // 'utf8_string' }; |
| 73 | |
| 74 | my $tree = MaxMind::DB::Writer::Tree->newscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/new().( |
| 75 | ip_version => 6, |
| 76 | record_size => 32, |
| 77 | database_type => 'Test-GeoLite2-Country', |
| 78 | languages => \@languages, |
| 79 | description => { en => 'Test GeoLite2 Country' }, |
| 80 | merge_record_collisions => 1, |
| 81 | alias_ipv6_to_ipv4 => 1, |
| 82 | map_key_type_callback => $map_key_type_callback, |
| 83 | ); |
| 84 | |
| 85 | openscip-perl cpan MaxMind-DB-Writer 0.300004 main/open(). my $fh, '<', $json_file or diescip-perl cpan MaxMind-DB-Writer 0.300004 main/die(). $!scip-perl cpan MaxMind-DB-Writer 0.300004 main/`$!`.; |
| 86 | my $geolite2_data = do { local $/scip-perl cpan MaxMind-DB-Writer 0.300004 main/`$/`. = undef; <$fh> }; |
| 87 | my $geolite_data = JSON->newscip-perl cpan MaxMind-DB-Writer 0.300004 JSON/new().->decodescip-perl cpan MaxMind-DB-Writer 0.300004 main/decode().($geolite2_data); |
| 88 | closescip-perl cpan MaxMind-DB-Writer 0.300004 main/close(). $fh or diescip-perl cpan MaxMind-DB-Writer 0.300004 main/die(). $!scip-perl cpan MaxMind-DB-Writer 0.300004 main/`$!`.; |
| 89 | |
| 90 | my $i = 0; |
| 91 | for my $geolite_entry ( @{$geolite_data} ) { |
| 92 | my ($network) = keysscip-perl cpan MaxMind-DB-Writer 0.300004 main/keys(). %{$geolite_entry}; |
| 93 | $tree->insert_networkscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/insert_network().( |
| 94 | Net::Works::Network->new_from_stringscip-perl cpan MaxMind-DB-Writer 0.300004 Net/Works/Network/new_from_string().( string => $network ), |
| 95 | $geolite_entry->{$network}, |
| 96 | ); |
| 97 | |
| 98 | $i++; |
| 99 | diagscip-perl cpan MaxMind-DB-Writer 0.300004 main/diag().("Inserted $i records") unless $i % 100_000; |
| 100 | } |
| 101 | |
| 102 | test_freeze_thawscip-perl cpan MaxMind-DB-Writer 0.300004 Test/MaxMind/DB/Writer/test_freeze_thaw().($tree); |
| 103 | |
| 104 | my $dir = tempdirscip-perl cpan MaxMind-DB-Writer 0.300004 main/tempdir().( CLEANUP => 1 ); |
| 105 | my $frozen_file = "$dir/Test-GeoLite2-Country.frozen"; |
| 106 | $tree->freeze_treescip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/freeze_tree().($frozen_file); |
| 107 | |
| 108 | my $mmdb_file = "$dir/Test-GeoLite2-Country.mmdb"; |
| 109 | |
| 110 | if ( my $pid = forkscip-perl cpan MaxMind-DB-Writer 0.300004 main/fork(). ) { |
| 111 | waitpidscip-perl cpan MaxMind-DB-Writer 0.300004 main/waitpid(). $pid, 0 or diescip-perl cpan MaxMind-DB-Writer 0.300004 main/die(). $!scip-perl cpan MaxMind-DB-Writer 0.300004 main/`$!`.; |
| 112 | } |
| 113 | else { |
| 114 | my $thawed_tree = MaxMind::DB::Writer::Tree->new_from_frozen_treescip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/new_from_frozen_tree().( |
| 115 | filename => $frozen_file, |
| 116 | map_key_type_callback => $map_key_type_callback, |
| 117 | ); |
| 118 | |
| 119 | openscip-perl cpan MaxMind-DB-Writer 0.300004 main/open(). $fh, '>', $mmdb_file or diescip-perl cpan MaxMind-DB-Writer 0.300004 main/die(). $!scip-perl cpan MaxMind-DB-Writer 0.300004 main/`$!`.; |
| 120 | $thawed_tree->write_treescip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/write_tree().($fh); |
| 121 | closescip-perl cpan MaxMind-DB-Writer 0.300004 main/close(). $fh or diescip-perl cpan MaxMind-DB-Writer 0.300004 main/die(). $!scip-perl cpan MaxMind-DB-Writer 0.300004 main/`$!`.; |
| 122 | |
| 123 | exitscip-perl cpan MaxMind-DB-Writer 0.300004 main/exit(). 0; |
| 124 | } |
| 125 | |
| 126 | my $reader = MaxMind::DB::Reader->newscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Reader/new().( file => $mmdb_file ); |
| 127 | |
| 128 | for my $i ( 0 .. intscip-perl cpan MaxMind-DB-Writer 0.300004 main/int().( ( scalarscip-perl cpan MaxMind-DB-Writer 0.300004 main/scalar(). @{$geolite_data} ) / 777 ) ) { |
| 129 | my $geolite_entry = $geolite_data->[ $i * 777 ]; |
| 130 | my ($network) = keysscip-perl cpan MaxMind-DB-Writer 0.300004 main/keys(). %{$geolite_entry}; |
| 131 | my $ip = $network =~ s{/.+$}{}r; |
| 132 | |
| 133 | is_deeplyscip-perl cpan MaxMind-DB-Writer 0.300004 main/is_deeply().( |
| 134 | $reader->record_for_addressscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Reader/record_for_address().($ip), |
| 135 | $geolite_entry->{$network}, |
| 136 | "record for $ip" |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | done_testingscip-perl cpan MaxMind-DB-Writer 0.300004 main/done_testing().(); |