geoip_test.go 5.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2021, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package server
  20. import (
  21. "io/ioutil"
  22. "testing"
  23. )
  24. /*
  25. This test GeoIP database maps all IPv4 values to the "user assigned" ISO
  26. 3166-1 alpha-2 country code 'ZZ'.
  27. The database was generated with
  28. https://github.com/maxmind/MaxMind-DB-Writer-perl, using the following perl
  29. script.
  30. The script output was processed with "hexdump -ve '"\\\x" 1/1 "%.2x"'" to
  31. produce the embedded string.
  32. -------------------------------------------------------------------------------
  33. use MaxMind::DB::Writer::Tree;
  34. my %types = (
  35.     country => 'map',
  36.     iso_code  => 'utf8_string',
  37. );
  38. my $tree = MaxMind::DB::Writer::Tree->new(
  39.     ip_version            => 6,
  40.     record_size           => 24,
  41.     database_type         => 'Psiphon-Test-GeoIP',
  42.     languages             => ['en'],
  43.     description           => { en => 'Psiphon GeoIP test data' },
  44.     map_key_type_callback => sub { $types{ $_[0] } },
  45.     remove_reserved_networks => 0,
  46. );
  47. $tree->insert_network(
  48.     '0.0.0.0/0',
  49.     {
  50.         country => {
  51.             iso_code => 'ZZ',
  52.         },
  53.     },
  54. );
  55. $tree->insert_network(
  56.     '::/0',
  57.     {
  58.         country => {
  59.             iso_code => 'ZZ',
  60.         },
  61.     },
  62. );
  63. open my $fh, '>:raw', 'psiphon-test.mmdb';
  64. $tree->write_tree($fh);
  65. */
  66. func paveGeoIPDatabaseFile(t *testing.T, geoIPDatabaseFilename string) {
  67. err := ioutil.WriteFile(geoIPDatabaseFilename, []byte(testGeoIPDatabase), 0600)
  68. if err != nil {
  69. t.Fatalf("error paving GeoPIP database file: %s", err)
  70. }
  71. }
  72. var testGeoIPDatabase = "\x00\x00\x01\x00\x00\x60\x00\x00\x02\x00\x00\x60\x00\x00\x03\x00\x00\x60\x00\x00\x04\x00\x00\x60\x00\x00\x05\x00\x00\x60\x00\x00\x06\x00\x00\x60\x00\x00\x07\x00\x00\x60\x00\x00\x08\x00\x00\x60\x00\x00\x09\x00\x00\x60\x00\x00\x0a\x00\x00\x60\x00\x00\x0b\x00\x00\x60\x00\x00\x0c\x00\x00\x60\x00\x00\x0d\x00\x00\x60\x00\x00\x0e\x00\x00\x60\x00\x00\x0f\x00\x00\x60\x00\x00\x10\x00\x00\x60\x00\x00\x11\x00\x00\x60\x00\x00\x12\x00\x00\x60\x00\x00\x13\x00\x00\x60\x00\x00\x14\x00\x00\x60\x00\x00\x15\x00\x00\x60\x00\x00\x16\x00\x00\x60\x00\x00\x17\x00\x00\x60\x00\x00\x18\x00\x00\x60\x00\x00\x19\x00\x00\x60\x00\x00\x1a\x00\x00\x60\x00\x00\x1b\x00\x00\x60\x00\x00\x1c\x00\x00\x60\x00\x00\x1d\x00\x00\x60\x00\x00\x1e\x00\x00\x60\x00\x00\x1f\x00\x00\x60\x00\x00\x20\x00\x00\x60\x00\x00\x21\x00\x00\x60\x00\x00\x22\x00\x00\x60\x00\x00\x23\x00\x00\x60\x00\x00\x24\x00\x00\x60\x00\x00\x25\x00\x00\x60\x00\x00\x26\x00\x00\x60\x00\x00\x27\x00\x00\x60\x00\x00\x28\x00\x00\x60\x00\x00\x29\x00\x00\x60\x00\x00\x2a\x00\x00\x60\x00\x00\x2b\x00\x00\x60\x00\x00\x2c\x00\x00\x60\x00\x00\x2d\x00\x00\x60\x00\x00\x2e\x00\x00\x60\x00\x00\x2f\x00\x00\x60\x00\x00\x30\x00\x00\x60\x00\x00\x31\x00\x00\x60\x00\x00\x32\x00\x00\x60\x00\x00\x33\x00\x00\x60\x00\x00\x34\x00\x00\x60\x00\x00\x35\x00\x00\x60\x00\x00\x36\x00\x00\x60\x00\x00\x37\x00\x00\x60\x00\x00\x38\x00\x00\x60\x00\x00\x39\x00\x00\x60\x00\x00\x3a\x00\x00\x60\x00\x00\x3b\x00\x00\x60\x00\x00\x3c\x00\x00\x60\x00\x00\x3d\x00\x00\x60\x00\x00\x3e\x00\x00\x60\x00\x00\x3f\x00\x00\x60\x00\x00\x40\x00\x00\x60\x00\x00\x41\x00\x00\x60\x00\x00\x42\x00\x00\x60\x00\x00\x43\x00\x00\x60\x00\x00\x44\x00\x00\x60\x00\x00\x45\x00\x00\x60\x00\x00\x46\x00\x00\x60\x00\x00\x47\x00\x00\x60\x00\x00\x48\x00\x00\x60\x00\x00\x49\x00\x00\x60\x00\x00\x4a\x00\x00\x60\x00\x00\x4b\x00\x00\x60\x00\x00\x4c\x00\x00\x60\x00\x00\x4d\x00\x00\x60\x00\x00\x4e\x00\x00\x60\x00\x00\x4f\x00\x00\x60\x00\x00\x50\x00\x00\x60\x00\x00\x51\x00\x00\x60\x00\x00\x52\x00\x00\x60\x00\x00\x53\x00\x00\x60\x00\x00\x54\x00\x00\x60\x00\x00\x55\x00\x00\x60\x00\x00\x56\x00\x00\x60\x00\x00\x57\x00\x00\x60\x00\x00\x58\x00\x00\x60\x00\x00\x59\x00\x00\x60\x00\x00\x5a\x00\x00\x60\x00\x00\x5b\x00\x00\x60\x00\x00\x5c\x00\x00\x60\x00\x00\x5d\x00\x00\x60\x00\x00\x5e\x00\x00\x60\x00\x00\x5f\x00\x00\x60\x00\x00\x70\x00\x00\x60\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe1\x47\x63\x6f\x75\x6e\x74\x72\x79\xe1\x48\x69\x73\x6f\x5f\x63\x6f\x64\x65\x42\x5a\x5a\xab\xcd\xef\x4d\x61\x78\x4d\x69\x6e\x64\x2e\x63\x6f\x6d\xe9\x5b\x62\x69\x6e\x61\x72\x79\x5f\x66\x6f\x72\x6d\x61\x74\x5f\x6d\x61\x6a\x6f\x72\x5f\x76\x65\x72\x73\x69\x6f\x6e\xa1\x02\x5b\x62\x69\x6e\x61\x72\x79\x5f\x66\x6f\x72\x6d\x61\x74\x5f\x6d\x69\x6e\x6f\x72\x5f\x76\x65\x72\x73\x69\x6f\x6e\xa0\x4b\x62\x75\x69\x6c\x64\x5f\x65\x70\x6f\x63\x68\x04\x02\x60\x30\x01\x8a\x4d\x64\x61\x74\x61\x62\x61\x73\x65\x5f\x74\x79\x70\x65\x52\x50\x73\x69\x70\x68\x6f\x6e\x2d\x54\x65\x73\x74\x2d\x47\x65\x6f\x49\x50\x4b\x64\x65\x73\x63\x72\x69\x70\x74\x69\x6f\x6e\xe1\x42\x65\x6e\x57\x50\x73\x69\x70\x68\x6f\x6e\x20\x47\x65\x6f\x49\x50\x20\x74\x65\x73\x74\x20\x64\x61\x74\x61\x4a\x69\x70\x5f\x76\x65\x72\x73\x69\x6f\x6e\xa1\x06\x49\x6c\x61\x6e\x67\x75\x61\x67\x65\x73\x01\x04\x42\x65\x6e\x4a\x6e\x6f\x64\x65\x5f\x63\x6f\x75\x6e\x74\xc1\x60\x4b\x72\x65\x63\x6f\x72\x64\x5f\x73\x69\x7a\x65\xa1\x18"