maxmind-geoip2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +12 -0
  3. data/Gemfile +9 -0
  4. data/Gemfile.lock +38 -0
  5. data/LICENSE-APACHE +202 -0
  6. data/LICENSE-MIT +17 -0
  7. data/README.dev.md +4 -0
  8. data/README.md +274 -0
  9. data/Rakefile +14 -0
  10. data/lib/maxmind/geoip2.rb +3 -0
  11. data/lib/maxmind/geoip2/errors.rb +7 -0
  12. data/lib/maxmind/geoip2/model/abstract.rb +27 -0
  13. data/lib/maxmind/geoip2/model/anonymous_ip.rb +63 -0
  14. data/lib/maxmind/geoip2/model/asn.rb +39 -0
  15. data/lib/maxmind/geoip2/model/city.rb +75 -0
  16. data/lib/maxmind/geoip2/model/connection_type.rb +32 -0
  17. data/lib/maxmind/geoip2/model/country.rb +63 -0
  18. data/lib/maxmind/geoip2/model/domain.rb +32 -0
  19. data/lib/maxmind/geoip2/model/enterprise.rb +15 -0
  20. data/lib/maxmind/geoip2/model/isp.rb +53 -0
  21. data/lib/maxmind/geoip2/reader.rb +277 -0
  22. data/lib/maxmind/geoip2/record/abstract.rb +22 -0
  23. data/lib/maxmind/geoip2/record/city.rb +38 -0
  24. data/lib/maxmind/geoip2/record/continent.rb +37 -0
  25. data/lib/maxmind/geoip2/record/country.rb +54 -0
  26. data/lib/maxmind/geoip2/record/location.rb +73 -0
  27. data/lib/maxmind/geoip2/record/place.rb +28 -0
  28. data/lib/maxmind/geoip2/record/postal.rb +30 -0
  29. data/lib/maxmind/geoip2/record/represented_country.rb +23 -0
  30. data/lib/maxmind/geoip2/record/subdivision.rb +48 -0
  31. data/lib/maxmind/geoip2/record/traits.rb +200 -0
  32. data/maxmind-geoip2.gemspec +24 -0
  33. data/test/data/LICENSE +4 -0
  34. data/test/data/MaxMind-DB-spec.md +570 -0
  35. data/test/data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
  36. data/test/data/README.md +4 -0
  37. data/test/data/bad-data/README.md +7 -0
  38. data/test/data/bad-data/libmaxminddb/libmaxminddb-offset-integer-overflow.mmdb +0 -0
  39. data/test/data/bad-data/maxminddb-golang/cyclic-data-structure.mmdb +0 -0
  40. data/test/data/bad-data/maxminddb-golang/invalid-bytes-length.mmdb +1 -0
  41. data/test/data/bad-data/maxminddb-golang/invalid-data-record-offset.mmdb +0 -0
  42. data/test/data/bad-data/maxminddb-golang/invalid-map-key-length.mmdb +0 -0
  43. data/test/data/bad-data/maxminddb-golang/invalid-string-length.mmdb +1 -0
  44. data/test/data/bad-data/maxminddb-golang/metadata-is-an-uint128.mmdb +1 -0
  45. data/test/data/bad-data/maxminddb-golang/unexpected-bytes.mmdb +0 -0
  46. data/test/data/perltidyrc +12 -0
  47. data/test/data/source-data/GeoIP2-Anonymous-IP-Test.json +48 -0
  48. data/test/data/source-data/GeoIP2-City-Test.json +12852 -0
  49. data/test/data/source-data/GeoIP2-Connection-Type-Test.json +102 -0
  50. data/test/data/source-data/GeoIP2-Country-Test.json +15916 -0
  51. data/test/data/source-data/GeoIP2-DensityIncome-Test.json +14 -0
  52. data/test/data/source-data/GeoIP2-Domain-Test.json +452 -0
  53. data/test/data/source-data/GeoIP2-Enterprise-Test.json +687 -0
  54. data/test/data/source-data/GeoIP2-ISP-Test.json +12593 -0
  55. data/test/data/source-data/GeoIP2-Precision-Enterprise-Test.json +2061 -0
  56. data/test/data/source-data/GeoIP2-Static-IP-Score-Test.json +2132 -0
  57. data/test/data/source-data/GeoIP2-User-Count-Test.json +2837 -0
  58. data/test/data/source-data/GeoLite2-ASN-Test.json +37 -0
  59. data/test/data/source-data/README +15 -0
  60. data/test/data/test-data/GeoIP2-Anonymous-IP-Test.mmdb +0 -0
  61. data/test/data/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb +0 -0
  62. data/test/data/test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb +0 -0
  63. data/test/data/test-data/GeoIP2-City-Test.mmdb +0 -0
  64. data/test/data/test-data/GeoIP2-Connection-Type-Test.mmdb +0 -0
  65. data/test/data/test-data/GeoIP2-Country-Test.mmdb +0 -0
  66. data/test/data/test-data/GeoIP2-DensityIncome-Test.mmdb +0 -0
  67. data/test/data/test-data/GeoIP2-Domain-Test.mmdb +0 -0
  68. data/test/data/test-data/GeoIP2-Enterprise-Test.mmdb +0 -0
  69. data/test/data/test-data/GeoIP2-ISP-Test.mmdb +0 -0
  70. data/test/data/test-data/GeoIP2-Precision-Enterprise-Test.mmdb +0 -0
  71. data/test/data/test-data/GeoIP2-Static-IP-Score-Test.mmdb +0 -0
  72. data/test/data/test-data/GeoIP2-User-Count-Test.mmdb +0 -0
  73. data/test/data/test-data/GeoLite2-ASN-Test.mmdb +0 -0
  74. data/test/data/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb +0 -0
  75. data/test/data/test-data/MaxMind-DB-string-value-entries.mmdb +0 -0
  76. data/test/data/test-data/MaxMind-DB-test-broken-pointers-24.mmdb +0 -0
  77. data/test/data/test-data/MaxMind-DB-test-broken-search-tree-24.mmdb +0 -0
  78. data/test/data/test-data/MaxMind-DB-test-decoder.mmdb +0 -0
  79. data/test/data/test-data/MaxMind-DB-test-ipv4-24.mmdb +0 -0
  80. data/test/data/test-data/MaxMind-DB-test-ipv4-28.mmdb +0 -0
  81. data/test/data/test-data/MaxMind-DB-test-ipv4-32.mmdb +0 -0
  82. data/test/data/test-data/MaxMind-DB-test-ipv6-24.mmdb +0 -0
  83. data/test/data/test-data/MaxMind-DB-test-ipv6-28.mmdb +0 -0
  84. data/test/data/test-data/MaxMind-DB-test-ipv6-32.mmdb +0 -0
  85. data/test/data/test-data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
  86. data/test/data/test-data/MaxMind-DB-test-mixed-24.mmdb +0 -0
  87. data/test/data/test-data/MaxMind-DB-test-mixed-28.mmdb +0 -0
  88. data/test/data/test-data/MaxMind-DB-test-mixed-32.mmdb +0 -0
  89. data/test/data/test-data/MaxMind-DB-test-nested.mmdb +0 -0
  90. data/test/data/test-data/README.md +26 -0
  91. data/test/data/test-data/maps-with-pointers.raw +0 -0
  92. data/test/data/test-data/write-test-data.pl +641 -0
  93. data/test/data/tidyall.ini +5 -0
  94. data/test/test_model_country.rb +80 -0
  95. data/test/test_model_names.rb +47 -0
  96. data/test/test_reader.rb +459 -0
  97. metadata +159 -0
@@ -0,0 +1,5 @@
1
+ [PerlTidy]
2
+ select = **/*.{pl,pm,t}
3
+
4
+ [JSON]
5
+ select = **/*.json
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maxmind/geoip2'
4
+ require 'minitest/autorun'
5
+
6
+ class CountryModelTest < Minitest::Test
7
+ RAW = {
8
+ 'continent' => {
9
+ 'code' => 'NA',
10
+ 'geoname_id' => 42,
11
+ 'names' => { 'en' => 'North America' },
12
+ },
13
+ 'country' => {
14
+ 'geoname_id' => 1,
15
+ 'iso_code' => 'US',
16
+ 'names' => { 'en' => 'United States of America' },
17
+ },
18
+ 'registered_country' => {
19
+ 'geoname_id' => 2,
20
+ 'is_in_european_union' => true,
21
+ 'iso_code' => 'DE',
22
+ 'names' => { 'en' => 'Germany' },
23
+ },
24
+ 'traits' => {
25
+ 'ip_address' => '1.2.3.4',
26
+ 'prefix_length' => 24,
27
+ },
28
+ }.freeze
29
+
30
+ def test_objects
31
+ model = MaxMind::GeoIP2::Model::Country.new(RAW, ['en'])
32
+ assert_equal(MaxMind::GeoIP2::Model::Country, model.class)
33
+ assert_equal(MaxMind::GeoIP2::Record::Continent, model.continent.class)
34
+ assert_equal(MaxMind::GeoIP2::Record::Country, model.country.class)
35
+ assert_equal(
36
+ MaxMind::GeoIP2::Record::Country,
37
+ model.registered_country.class,
38
+ )
39
+ assert_equal(
40
+ MaxMind::GeoIP2::Record::RepresentedCountry,
41
+ model.represented_country.class,
42
+ )
43
+ assert_equal(
44
+ MaxMind::GeoIP2::Record::Traits,
45
+ model.traits.class,
46
+ )
47
+ end
48
+
49
+ def test_values
50
+ model = MaxMind::GeoIP2::Model::Country.new(RAW, ['en'])
51
+
52
+ assert_equal(42, model.continent.geoname_id)
53
+ assert_equal('NA', model.continent.code)
54
+ assert_equal({ 'en' => 'North America' }, model.continent.names)
55
+ assert_equal('North America', model.continent.name)
56
+
57
+ assert_equal(1, model.country.geoname_id)
58
+ assert_equal(false, model.country.in_european_union?)
59
+ assert_equal('US', model.country.iso_code)
60
+ assert_equal({ 'en' => 'United States of America' }, model.country.names)
61
+ assert_equal('United States of America', model.country.name)
62
+ assert_nil(model.country.confidence)
63
+
64
+ assert_equal(2, model.registered_country.geoname_id)
65
+ assert_equal(true, model.registered_country.in_european_union?)
66
+ assert_equal('DE', model.registered_country.iso_code)
67
+ assert_equal({ 'en' => 'Germany' }, model.registered_country.names)
68
+ assert_equal('Germany', model.registered_country.name)
69
+ end
70
+
71
+ def test_unknown_record
72
+ model = MaxMind::GeoIP2::Model::Country.new(RAW, ['en'])
73
+ assert_raises(NoMethodError) { model.unknown_record }
74
+ end
75
+
76
+ def test_unknown_trait
77
+ model = MaxMind::GeoIP2::Model::Country.new(RAW, ['en'])
78
+ assert_raises(NoMethodError) { model.traits.unknown }
79
+ end
80
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maxmind/geoip2'
4
+ require 'minitest/autorun'
5
+
6
+ class ModelNameTest < Minitest::Test
7
+ RAW = {
8
+ 'continent' => {
9
+ 'code' => 'NA',
10
+ 'geoname_id' => 42,
11
+ 'names' => {
12
+ 'en' => 'North America',
13
+ 'zh-CN' => '北美洲',
14
+ },
15
+ },
16
+ 'country' => {
17
+ 'geoname_id' => 1,
18
+ 'iso_code' => 'US',
19
+ 'names' => {
20
+ 'en' => 'United States of America',
21
+ 'ru' => 'объединяет государства',
22
+ 'zh-CN' => '美国',
23
+ },
24
+ },
25
+ 'traits' => {
26
+ 'ip_address' => '1.2.3.4',
27
+ },
28
+ }.freeze
29
+
30
+ def test_fallback
31
+ model = MaxMind::GeoIP2::Model::Country.new(RAW, %w[ru zh-CN en])
32
+ assert_equal('北美洲', model.continent.name)
33
+ assert_equal('объединяет государства', model.country.name)
34
+ end
35
+
36
+ def test_two_fallbacks
37
+ model = MaxMind::GeoIP2::Model::Country.new(RAW, %w[ru jp])
38
+ assert_nil(model.continent.name)
39
+ assert_equal('объединяет государства', model.country.name)
40
+ end
41
+
42
+ def test_no_fallbacks
43
+ model = MaxMind::GeoIP2::Model::Country.new(RAW, %w[jp])
44
+ assert_nil(model.continent.name)
45
+ assert_nil(model.country.name)
46
+ end
47
+ end
@@ -0,0 +1,459 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ipaddr'
4
+ require 'maxmind/geoip2'
5
+ require 'minitest/autorun'
6
+
7
+ class ReaderTest < Minitest::Test
8
+ def test_anonymous_ip
9
+ reader = MaxMind::GeoIP2::Reader.new(
10
+ 'test/data/test-data/GeoIP2-Anonymous-IP-Test.mmdb',
11
+ )
12
+ ip = '1.2.0.1'
13
+ record = reader.anonymous_ip(ip)
14
+
15
+ assert_equal(true, record.anonymous?)
16
+ assert_equal(true, record.anonymous_vpn?)
17
+ assert_equal(false, record.hosting_provider?)
18
+ assert_equal(false, record.public_proxy?)
19
+ assert_equal(false, record.tor_exit_node?)
20
+ assert_equal(ip, record.ip_address)
21
+ assert_equal('1.2.0.0/16', record.network)
22
+
23
+ reader.close
24
+ end
25
+
26
+ def test_asn
27
+ reader = MaxMind::GeoIP2::Reader.new(
28
+ 'test/data/test-data/GeoLite2-ASN-Test.mmdb',
29
+ )
30
+ ip = '1.128.0.1'
31
+ record = reader.asn(ip)
32
+
33
+ assert_equal(1221, record.autonomous_system_number)
34
+ assert_equal('Telstra Pty Ltd', record.autonomous_system_organization)
35
+ assert_equal(ip, record.ip_address)
36
+ assert_equal('1.128.0.0/11', record.network)
37
+
38
+ reader.close
39
+ end
40
+
41
+ def test_city
42
+ reader = MaxMind::GeoIP2::Reader.new(
43
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
44
+ )
45
+ record = reader.city('2.125.160.216')
46
+
47
+ assert_equal('EU', record.continent.code)
48
+
49
+ assert_equal(2_655_045, record.city.geoname_id)
50
+ assert_equal({ 'en' => 'Boxford' }, record.city.names)
51
+ assert_equal('Boxford', record.city.name)
52
+ assert_nil(record.city.confidence)
53
+
54
+ assert_equal(100, record.location.accuracy_radius)
55
+ assert_equal(51.75, record.location.latitude)
56
+ assert_equal(-1.25, record.location.longitude)
57
+ assert_equal('Europe/London', record.location.time_zone)
58
+
59
+ assert_equal(2, record.subdivisions.size)
60
+ assert_equal('England', record.subdivisions[0].name)
61
+ assert_equal('West Berkshire', record.subdivisions[1].name)
62
+ assert_equal('West Berkshire', record.most_specific_subdivision.name)
63
+
64
+ record = reader.city('216.160.83.56')
65
+
66
+ assert_equal(819, record.location.metro_code)
67
+
68
+ assert_equal('98354', record.postal.code)
69
+
70
+ assert_equal(1, record.subdivisions.size)
71
+ assert_equal(5_815_135, record.subdivisions[0].geoname_id)
72
+ assert_equal('WA', record.subdivisions[0].iso_code)
73
+ assert_equal(
74
+ {
75
+ 'en' => 'Washington',
76
+ 'es' => 'Washington',
77
+ 'fr' => 'État de Washington',
78
+ 'ja' => 'ワシントン州',
79
+ 'ru' => 'Вашингтон',
80
+ 'zh-CN' => '华盛顿州',
81
+ },
82
+ record.subdivisions[0].names,
83
+ assert_equal('WA', record.most_specific_subdivision.iso_code)
84
+ )
85
+
86
+ reader.close
87
+ end
88
+
89
+ def test_city_no_subdivisions
90
+ reader = MaxMind::GeoIP2::Reader.new(
91
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
92
+ )
93
+ record = reader.city('2001:218::')
94
+
95
+ assert_equal([], record.subdivisions)
96
+ assert_nil(record.most_specific_subdivision)
97
+
98
+ reader.close
99
+ end
100
+
101
+ def test_connection_type
102
+ reader = MaxMind::GeoIP2::Reader.new(
103
+ 'test/data/test-data/GeoIP2-Connection-Type-Test.mmdb',
104
+ )
105
+ ip = '1.0.1.1'
106
+ record = reader.connection_type(ip)
107
+
108
+ assert_equal('Cable/DSL', record.connection_type)
109
+ assert_equal(ip, record.ip_address)
110
+ assert_equal('1.0.1.0/24', record.network)
111
+
112
+ reader.close
113
+ end
114
+
115
+ def test_country
116
+ reader = MaxMind::GeoIP2::Reader.new(
117
+ 'test/data/test-data/GeoIP2-Country-Test.mmdb',
118
+ )
119
+ record = reader.country('2.125.160.216')
120
+
121
+ assert_equal('EU', record.continent.code)
122
+ assert_equal(6_255_148, record.continent.geoname_id)
123
+ assert_equal(
124
+ {
125
+ 'de' => 'Europa',
126
+ 'en' => 'Europe',
127
+ 'es' => 'Europa',
128
+ 'fr' => 'Europe',
129
+ 'ja' => 'ヨーロッパ',
130
+ 'pt-BR' => 'Europa',
131
+ 'ru' => 'Европа',
132
+ 'zh-CN' => '欧洲',
133
+ },
134
+ record.continent.names,
135
+ )
136
+ assert_equal('Europe', record.continent.name)
137
+
138
+ assert_equal(2_635_167, record.country.geoname_id)
139
+ assert_equal(true, record.country.in_european_union?)
140
+ assert_equal('GB', record.country.iso_code)
141
+ assert_equal(
142
+ {
143
+ 'de' => 'Vereinigtes Königreich',
144
+ 'en' => 'United Kingdom',
145
+ 'es' => 'Reino Unido',
146
+ 'fr' => 'Royaume-Uni',
147
+ 'ja' => 'イギリス',
148
+ 'pt-BR' => 'Reino Unido',
149
+ 'ru' => 'Великобритания',
150
+ 'zh-CN' => '英国',
151
+ },
152
+ record.country.names,
153
+ )
154
+ assert_equal('United Kingdom', record.country.name)
155
+
156
+ assert_equal(3_017_382, record.registered_country.geoname_id)
157
+ assert_equal(true, record.registered_country.in_european_union?)
158
+ assert_equal('FR', record.registered_country.iso_code)
159
+ assert_equal(
160
+ {
161
+ 'de' => 'Frankreich',
162
+ 'en' => 'France',
163
+ 'es' => 'Francia',
164
+ 'fr' => 'France',
165
+ 'ja' => 'フランス共和国',
166
+ 'pt-BR' => 'França',
167
+ 'ru' => 'Франция',
168
+ 'zh-CN' => '法国',
169
+ },
170
+ record.registered_country.names,
171
+ )
172
+ assert_equal('France', record.registered_country.name)
173
+
174
+ record = reader.country('202.196.224.0')
175
+
176
+ assert_equal(6_252_001, record.represented_country.geoname_id)
177
+ assert_equal('US', record.represented_country.iso_code)
178
+ assert_equal(
179
+ {
180
+ 'de' => 'USA',
181
+ 'en' => 'United States',
182
+ 'es' => 'Estados Unidos',
183
+ 'fr' => 'États-Unis',
184
+ 'ja' => 'アメリカ合衆国',
185
+ 'pt-BR' => 'Estados Unidos',
186
+ 'ru' => 'США',
187
+ 'zh-CN' => '美国',
188
+ },
189
+ record.represented_country.names,
190
+ )
191
+ assert_equal('United States', record.represented_country.name)
192
+ assert_equal('military', record.represented_country.type)
193
+
194
+ record = reader.country('81.2.69.163')
195
+ assert_equal('81.2.69.163', record.traits.ip_address)
196
+ assert_equal('81.2.69.160/27', record.traits.network)
197
+
198
+ assert_raises(NoMethodError) { record.foo }
199
+ reader.close
200
+ end
201
+
202
+ def test_is_method_returns_false
203
+ reader = MaxMind::GeoIP2::Reader.new(
204
+ 'test/data/test-data/GeoIP2-Country-Test.mmdb',
205
+ )
206
+ record = reader.country('74.209.24.0')
207
+ assert_equal(false, record.country.in_european_union?)
208
+
209
+ reader.close
210
+ end
211
+
212
+ def test_domain
213
+ reader = MaxMind::GeoIP2::Reader.new(
214
+ 'test/data/test-data/GeoIP2-Domain-Test.mmdb',
215
+ )
216
+ ip = '1.2.0.1'
217
+ record = reader.domain(ip)
218
+
219
+ assert_equal('maxmind.com', record.domain)
220
+ assert_equal(ip, record.ip_address)
221
+ assert_equal('1.2.0.0/16', record.network)
222
+
223
+ reader.close
224
+ end
225
+
226
+ def test_enterprise
227
+ reader = MaxMind::GeoIP2::Reader.new(
228
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
229
+ )
230
+ record = reader.enterprise('2.125.160.216')
231
+
232
+ assert_equal(50, record.city.confidence)
233
+ assert_equal(2_655_045, record.city.geoname_id)
234
+ assert_equal({ 'en' => 'Boxford' }, record.city.names)
235
+ assert_equal('Boxford', record.city.name)
236
+
237
+ ip_address = '74.209.24.0'
238
+ record = reader.enterprise(ip_address)
239
+
240
+ assert_equal(11, record.city.confidence)
241
+ assert_equal(99, record.country.confidence)
242
+ assert_equal(6_252_001, record.country.geoname_id)
243
+ assert_equal(false, record.country.in_european_union?)
244
+
245
+ assert_equal(27, record.location.accuracy_radius)
246
+
247
+ assert_equal(false, record.registered_country.in_european_union?)
248
+
249
+ assert_equal('Cable/DSL', record.traits.connection_type)
250
+ assert_equal(true, record.traits.legitimate_proxy?)
251
+
252
+ assert_equal(ip_address, record.traits.ip_address)
253
+ assert_equal('74.209.16.0/20', record.traits.network)
254
+
255
+ reader.close
256
+ end
257
+
258
+ def test_isp
259
+ reader = MaxMind::GeoIP2::Reader.new(
260
+ 'test/data/test-data/GeoIP2-ISP-Test.mmdb',
261
+ )
262
+ ip = '1.128.1.1'
263
+ record = reader.isp(ip)
264
+
265
+ assert_equal(1221, record.autonomous_system_number)
266
+ assert_equal('Telstra Pty Ltd', record.autonomous_system_organization)
267
+ assert_equal('Telstra Internet', record.isp)
268
+ assert_equal('Telstra Internet', record.organization)
269
+ assert_equal(ip, record.ip_address)
270
+ assert_equal('1.128.0.0/11', record.network)
271
+
272
+ reader.close
273
+ end
274
+
275
+ def test_no_traits
276
+ reader = MaxMind::GeoIP2::Reader.new(
277
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
278
+ )
279
+ record = reader.enterprise('2.125.160.216')
280
+
281
+ assert_equal('2.125.160.216', record.traits.ip_address)
282
+ assert_equal('2.125.160.216/29', record.traits.network)
283
+ assert_nil(record.traits.autonomous_system_number)
284
+ assert_equal(false, record.traits.anonymous?)
285
+
286
+ reader.close
287
+ end
288
+
289
+ def test_no_location
290
+ reader = MaxMind::GeoIP2::Reader.new(
291
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
292
+ )
293
+ record = reader.enterprise('212.47.235.81')
294
+
295
+ assert_nil(record.location.accuracy_radius)
296
+
297
+ reader.close
298
+ end
299
+
300
+ def test_no_postal
301
+ reader = MaxMind::GeoIP2::Reader.new(
302
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
303
+ )
304
+ record = reader.enterprise('212.47.235.81')
305
+
306
+ assert_nil(record.postal.code)
307
+
308
+ reader.close
309
+ end
310
+
311
+ def test_no_city
312
+ reader = MaxMind::GeoIP2::Reader.new(
313
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
314
+ )
315
+ record = reader.enterprise('212.47.235.81')
316
+
317
+ assert_nil(record.city.confidence)
318
+ assert_nil(record.city.name)
319
+ assert_nil(record.city.names)
320
+
321
+ reader.close
322
+ end
323
+
324
+ def test_no_continent
325
+ reader = MaxMind::GeoIP2::Reader.new(
326
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
327
+ )
328
+ record = reader.enterprise('212.47.235.81')
329
+
330
+ assert_nil(record.continent.code)
331
+
332
+ reader.close
333
+ end
334
+
335
+ def test_no_country
336
+ reader = MaxMind::GeoIP2::Reader.new(
337
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
338
+ )
339
+ record = reader.enterprise('212.47.235.81')
340
+
341
+ assert_nil(record.country.confidence)
342
+
343
+ reader.close
344
+ end
345
+
346
+ def test_no_represented_country
347
+ reader = MaxMind::GeoIP2::Reader.new(
348
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
349
+ )
350
+ record = reader.enterprise('212.47.235.81')
351
+
352
+ assert_nil(record.represented_country.type)
353
+
354
+ reader.close
355
+ end
356
+
357
+ def database_types
358
+ [
359
+ { 'file' => 'City', 'method' => 'city' },
360
+ { 'file' => 'Country', 'method' => 'country' },
361
+ ]
362
+ end
363
+
364
+ def test_default_locale
365
+ database_types.each do |t|
366
+ reader = MaxMind::GeoIP2::Reader.new(
367
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
368
+ )
369
+ record = reader.send(t['method'], '81.2.69.160')
370
+ assert_equal('United Kingdom', record.country.name)
371
+ reader.close
372
+ end
373
+ end
374
+
375
+ def test_locale_list
376
+ database_types.each do |t|
377
+ reader = MaxMind::GeoIP2::Reader.new(
378
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
379
+ %w[xx ru pt-BR es en],
380
+ )
381
+ record = reader.send(t['method'], '81.2.69.160')
382
+ assert_equal('Великобритания', record.country.name)
383
+ reader.close
384
+ end
385
+ end
386
+
387
+ def test_has_ip_address_and_network
388
+ database_types.each do |t|
389
+ reader = MaxMind::GeoIP2::Reader.new(
390
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
391
+ )
392
+ record = reader.send(t['method'], '81.2.69.163')
393
+ assert_equal('81.2.69.163', record.traits.ip_address)
394
+ assert_equal('81.2.69.160/27', record.traits.network)
395
+ reader.close
396
+ end
397
+ end
398
+
399
+ def test_is_in_european_union
400
+ database_types.each do |t|
401
+ reader = MaxMind::GeoIP2::Reader.new(
402
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
403
+ )
404
+ record = reader.send(t['method'], '81.2.69.160')
405
+ assert_equal(true, record.country.in_european_union?)
406
+ assert_equal(false, record.registered_country.in_european_union?)
407
+ reader.close
408
+ end
409
+ end
410
+
411
+ def test_unknown_address
412
+ database_types.each do |t|
413
+ reader = MaxMind::GeoIP2::Reader.new(
414
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
415
+ )
416
+ error = assert_raises(
417
+ MaxMind::GeoIP2::AddressNotFoundError,
418
+ ) { reader.send(t['method'], '10.10.10.0') }
419
+ assert_equal(
420
+ 'The address 10.10.10.0 is not in the database.',
421
+ error.message,
422
+ )
423
+ reader.close
424
+ end
425
+ end
426
+
427
+ def test_incorrect_database
428
+ reader = MaxMind::GeoIP2::Reader.new(
429
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
430
+ )
431
+ error = assert_raises(ArgumentError) { reader.country('10.10.10.10') }
432
+ assert_equal(
433
+ 'The country method cannot be used with the GeoIP2-City database.',
434
+ error.message,
435
+ )
436
+ reader.close
437
+ end
438
+
439
+ def test_invalid_address
440
+ reader = MaxMind::GeoIP2::Reader.new(
441
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
442
+ )
443
+ error = assert_raises(
444
+ IPAddr::InvalidAddressError,
445
+ ) { reader.city('invalid') }
446
+ # Ruby 2.5 says just 'invalid address'. Ruby 2.6+ says 'invalid address:
447
+ # invalid'.
448
+ assert_match('invalid address', error.message)
449
+ reader.close
450
+ end
451
+
452
+ def test_metadata
453
+ reader = MaxMind::GeoIP2::Reader.new(
454
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
455
+ )
456
+ assert_equal('GeoIP2-City', reader.metadata.database_type)
457
+ reader.close
458
+ end
459
+ end