| 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( |
| 14 | make_tree_from_pairs |
| 15 | test_freeze_thaw |
| 16 | test_freeze_thaw_optional_params |
| 17 | ); |
| 18 | use Test::Morescip-perl cpan MaxMind-DB-Writer 0.300004 Test/More/; |
| 19 | |
| 20 | use File::Tempscip-perl cpan MaxMind-DB-Writer 0.300004 File/Temp/ qw( tempdir ); |
| 21 | use JSONscip-perl cpan MaxMind-DB-Writer 0.300004 JSON/ (); |
| 22 | use Math::Int128scip-perl cpan MaxMind-DB-Writer 0.300004 Math/Int128/ qw( uint128 ); |
| 23 | use MaxMind::DB::Readerscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Reader/ (); |
| 24 | use MaxMind::DB::Writer::Treescip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/ (); |
| 25 | use Net::Works::Networkscip-perl cpan MaxMind-DB-Writer 0.300004 Net/Works/Network/ (); |
| 26 | |
| 27 | # The record size really has nothing to do with the freeze/thaw code, but it |
| 28 | # doesn't really hurt to test this either. |
| 29 | for my $record_size ( 24, 28, 32 ) { |
| 30 | { |
| 31 | my $tree = MaxMind::DB::Writer::Tree->newscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/new().( |
| 32 | ip_version => 4, |
| 33 | record_size => $record_size, |
| 34 | database_type => 'Test', |
| 35 | languages => [ 'en', 'fr' ], |
| 36 | description => { en => 'Test tree' }, |
| 37 | merge_strategy => 'toplevel', |
| 38 | map_key_type_callback => sub { 'uint32' }, |
| 39 | |
| 40 | # Below we try to insert into reserved space, which fails if we |
| 41 | # flag them as fixed empty. |
| 42 | remove_reserved_networks => 0, |
| 43 | ); |
| 44 | |
| 45 | my $count = 2**8; |
| 46 | |
| 47 | for my $i ( 1 .. $count ) { |
| 48 | my $ipv4 = Net::Works::Network->new_from_integerscip-perl cpan MaxMind-DB-Writer 0.300004 Net/Works/Network/new_from_integer().( |
| 49 | integer => $i, |
| 50 | prefix_length => 32, |
| 51 | version => 4, |
| 52 | ); |
| 53 | $tree->insert_networkscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/insert_network().( $ipv4, { i => $i } ); |
| 54 | } |
| 55 | |
| 56 | subtestscip-perl cpan MaxMind-DB-Writer 0.300004 main/subtest().( |
| 57 | "Tree with $count networks - IPv4 only - $record_size-bit records", |
| 58 | sub { |
| 59 | test_freeze_thawscip-perl cpan MaxMind-DB-Writer 0.300004 Test/MaxMind/DB/Writer/test_freeze_thaw().($tree); |
| 60 | test_freeze_thaw_optional_paramsscip-perl cpan MaxMind-DB-Writer 0.300004 Test/MaxMind/DB/Writer/test_freeze_thaw_optional_params().($tree); |
| 61 | } |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | { |
| 66 | my $cb = sub { |
| 67 | my $key = $_scip-perl cpan MaxMind-DB-Writer 0.300004 main/`@_`.[0]; |
| 68 | $key =~ s/X$//; |
| 69 | return $key eq 'array' ? [ 'array', 'uint32' ] : $key; |
| 70 | }; |
| 71 | |
| 72 | my $tree = MaxMind::DB::Writer::Tree->newscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/new().( |
| 73 | ip_version => 6, |
| 74 | record_size => 24, |
| 75 | database_type => 'Test', |
| 76 | languages => ['en'], |
| 77 | description => { en => 'Test tree' }, |
| 78 | merge_strategy => 'toplevel', |
| 79 | map_key_type_callback => $cb, |
| 80 | |
| 81 | # Below we try to insert into reserved space, which fails if we |
| 82 | # flag them as fixed empty. |
| 83 | remove_reserved_networks => 0, |
| 84 | ); |
| 85 | |
| 86 | my $count = 2**14; |
| 87 | my $ipv6_offset = uint128scip-perl cpan MaxMind-DB-Writer 0.300004 main/uint128().(2)**34; |
| 88 | |
| 89 | for my $i ( 1 .. $count ) { |
| 90 | my $ipv4 = Net::Works::Network->new_from_integerscip-perl cpan MaxMind-DB-Writer 0.300004 Net/Works/Network/new_from_integer().( |
| 91 | integer => $i, |
| 92 | prefix_length => 128, |
| 93 | version => 6 |
| 94 | ); |
| 95 | $tree->insert_networkscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/insert_network().( $ipv4, _data_recordscip-perl cpan MaxMind-DB-Writer 0.300004 main/_data_record().( $i % 16 ) ); |
| 96 | |
| 97 | my $ipv6 = Net::Works::Network->new_from_integerscip-perl cpan MaxMind-DB-Writer 0.300004 Net/Works/Network/new_from_integer().( |
| 98 | integer => $i + $ipv6_offset, |
| 99 | prefix_length => 128, |
| 100 | version => 6 |
| 101 | ); |
| 102 | $tree->insert_networkscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/insert_network().( $ipv6, _data_recordscip-perl cpan MaxMind-DB-Writer 0.300004 main/_data_record().( $i % 16 ) ); |
| 103 | } |
| 104 | |
| 105 | subtestscip-perl cpan MaxMind-DB-Writer 0.300004 main/subtest().( |
| 106 | "Tree with $count networks - mixed IPv4 and IPv6 - $record_size-bit records", |
| 107 | sub { |
| 108 | test_freeze_thawscip-perl cpan MaxMind-DB-Writer 0.300004 Test/MaxMind/DB/Writer/test_freeze_thaw().( $tree, $cb ); |
| 109 | } |
| 110 | ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | { |
| 115 | openscip-perl cpan MaxMind-DB-Writer 0.300004 main/open(). my $fh, '<', 't/test-data/geolite2-sample.json' or diescip-perl cpan MaxMind-DB-Writer 0.300004 main/die(). $!scip-perl cpan MaxMind-DB-Writer 0.300004 main/`$!`.; |
| 116 | my $geolite2_data = do { local $/scip-perl cpan MaxMind-DB-Writer 0.300004 main/`$/`. = undef; <$fh> }; |
| 117 | my $records = JSON->newscip-perl cpan MaxMind-DB-Writer 0.300004 JSON/new().->decodescip-perl cpan MaxMind-DB-Writer 0.300004 main/decode().($geolite2_data); |
| 118 | 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/`$!`.; |
| 119 | |
| 120 | my $tree = make_tree_from_pairsscip-perl cpan MaxMind-DB-Writer 0.300004 Test/MaxMind/DB/Writer/make_tree_from_pairs().( |
| 121 | 'network', |
| 122 | $records, |
| 123 | { |
| 124 | root_data_type => 'utf8_string', |
| 125 | alias_ipv6_to_ipv4 => 1, |
| 126 | }, |
| 127 | ); |
| 128 | |
| 129 | subtestscip-perl cpan MaxMind-DB-Writer 0.300004 main/subtest().( |
| 130 | 'Tree made from GeoLite2 sample data', |
| 131 | sub { |
| 132 | my %trees; |
| 133 | @trees{ 'pre-thaw', 'post-thaw' } = test_freeze_thawscip-perl cpan MaxMind-DB-Writer 0.300004 Test/MaxMind/DB/Writer/test_freeze_thaw().($tree); |
| 134 | |
| 135 | my $dir = tempdirscip-perl cpan MaxMind-DB-Writer 0.300004 main/tempdir().( CLEANUP => 1 ); |
| 136 | |
| 137 | my %readers; |
| 138 | for my $key ( keysscip-perl cpan MaxMind-DB-Writer 0.300004 main/keys(). %trees ) { |
| 139 | my $file = "$dir/$key.mmdb"; |
| 140 | openscip-perl cpan MaxMind-DB-Writer 0.300004 main/open(). my $fh, '>:raw', $file; |
| 141 | $trees{$key}->write_treescip-perl cpan MaxMind-DB-Writer 0.300004 main/write_tree().($fh); |
| 142 | closescip-perl cpan MaxMind-DB-Writer 0.300004 main/close(). $fh; |
| 143 | |
| 144 | $readers{$key} = MaxMind::DB::Reader->newscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Reader/new().( file => $file ); |
| 145 | } |
| 146 | |
| 147 | my %expect = ( |
| 148 | '1.64.242.0' => 'HK', |
| 149 | '1.72.10.0' => 'JP', |
| 150 | '1.72.10.1' => 'JP', |
| 151 | '1.72.10.127' => 'JP', |
| 152 | '2001:230::abcd:abcd' => 'KR', |
| 153 | '2001:67c:50:1928:1234::42ff' => 'NL', |
| 154 | ); |
| 155 | |
| 156 | for my $ip ( sortscip-perl cpan MaxMind-DB-Writer 0.300004 main/sort(). keysscip-perl cpan MaxMind-DB-Writer 0.300004 main/keys(). %expect ) { |
| 157 | for my $key ( sortscip-perl cpan MaxMind-DB-Writer 0.300004 main/sort(). keysscip-perl cpan MaxMind-DB-Writer 0.300004 main/keys(). %readers ) { |
| 158 | my $reader = $readers{$key}; |
| 159 | isscip-perl cpan MaxMind-DB-Writer 0.300004 main/is().( |
| 160 | $reader->record_for_addressscip-perl cpan MaxMind-DB-Writer 0.300004 main/record_for_address().($ip), |
| 161 | $expect{$ip}, |
| 162 | "$ip record is $expect{$ip} - $key" |
| 163 | ); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | { |
| 171 | my $tree = MaxMind::DB::Writer::Tree->newscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/new().( |
| 172 | ip_version => 6, |
| 173 | record_size => 24, |
| 174 | database_type => 'Test', |
| 175 | languages => ['en'], |
| 176 | description => { en => 'Test tree' }, |
| 177 | merge_strategy => 'toplevel', |
| 178 | map_key_type_callback => sub { 'uint32' }, |
| 179 | |
| 180 | # Like the ::/0 insertion case in Tree.t, if we don't turn this off, |
| 181 | # then our insert below of ::/1 silently gets ignored, since that would |
| 182 | # contain FIXED_EMPTY records. |
| 183 | remove_reserved_networks => 0, |
| 184 | ); |
| 185 | |
| 186 | $tree->insert_networkscip-perl cpan MaxMind-DB-Writer 0.300004 MaxMind/DB/Writer/Tree/insert_network().( |
| 187 | Net::Works::Network->new_from_stringscip-perl cpan MaxMind-DB-Writer 0.300004 Net/Works/Network/new_from_string().( string => '::0/1' ), |
| 188 | { value => 42 }, |
| 189 | ); |
| 190 | |
| 191 | subtestscip-perl cpan MaxMind-DB-Writer 0.300004 main/subtest().( |
| 192 | 'Tree with only one network - ::0/1', |
| 193 | sub { |
| 194 | test_freeze_thawscip-perl cpan MaxMind-DB-Writer 0.300004 Test/MaxMind/DB/Writer/test_freeze_thaw().($tree); |
| 195 | test_freeze_thaw_optional_paramsscip-perl cpan MaxMind-DB-Writer 0.300004 Test/MaxMind/DB/Writer/test_freeze_thaw_optional_params().($tree); |
| 196 | } |
| 197 | ); |
| 198 | } |
| 199 | |
| 200 | done_testingscip-perl cpan MaxMind-DB-Writer 0.300004 main/done_testing().(); |
| 201 | |
| 202 | sub _data_recordscip-perl cpan MaxMind-DB-Writer 0.300004 main/_data_record(). { |
| 203 | my $i = shiftscip-perl cpan MaxMind-DB-Writer 0.300004 main/shift().; |
| 204 | |
| 205 | return { |
| 206 | utf8_string => 'unicode! ☯ - ♫ - ' . $i, |
| 207 | double => 42.123456 + $i, |
| 208 | bytes => packscip-perl cpan MaxMind-DB-Writer 0.300004 main/pack().( 'N', 42 + $i ), |
| 209 | uint16 => 100 + $i, |
| 210 | uint32 => 2**28 + $i, |
| 211 | int32 => -1 * ( 2**28 + $i ), |
| 212 | uint64 => ( uint128scip-perl cpan MaxMind-DB-Writer 0.300004 main/uint128().(1) << 60 ) + $i, |
| 213 | uint128 => ( uint128scip-perl cpan MaxMind-DB-Writer 0.300004 main/uint128().(1) << 120 ) + $i, |
| 214 | array => [ 1, 2, 3, $i ], |
| 215 | map => { |
| 216 | mapX => { |
| 217 | utf8_stringX => 'hello - ' . $i, |
| 218 | arrayX => [ 7, 8, 9, $i ], |
| 219 | }, |
| 220 | }, |
| 221 | boolean => $i % 2, |
| 222 | float => 1.1 + $i, |
| 223 | }; |
| 224 | } |