-
Notifications
You must be signed in to change notification settings - Fork 195
/
reader_test.go
316 lines (257 loc) · 8.23 KB
/
reader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package geoip2
import (
"math/rand"
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReader(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb")
require.NoError(t, err)
defer reader.Close()
record, err := reader.City(net.ParseIP("81.2.69.160"))
require.NoError(t, err)
m := reader.Metadata()
assert.Equal(t, uint(2), m.BinaryFormatMajorVersion)
assert.Equal(t, uint(0), m.BinaryFormatMinorVersion)
assert.NotZero(t, m.BuildEpoch)
assert.Equal(t, "GeoIP2-City", m.DatabaseType)
assert.Equal(t,
map[string]string{
"en": "GeoIP2 City Test Database (fake GeoIP2 data, for example purposes only)",
"zh": "小型数据库",
},
m.Description,
)
assert.Equal(t, uint(6), m.IPVersion)
assert.Equal(t, []string{"en", "zh"}, m.Languages)
assert.NotZero(t, m.NodeCount)
assert.Equal(t, uint(28), m.RecordSize)
assert.Equal(t, uint(2643743), record.City.GeoNameID)
assert.Equal(t,
map[string]string{
"de": "London",
"en": "London",
"es": "Londres",
"fr": "Londres",
"ja": "ロンドン",
"pt-BR": "Londres",
"ru": "Лондон",
},
record.City.Names,
)
assert.Equal(t, uint(6255148), record.Continent.GeoNameID)
assert.Equal(t, "EU", record.Continent.Code)
assert.Equal(t,
map[string]string{
"de": "Europa",
"en": "Europe",
"es": "Europa",
"fr": "Europe",
"ja": "ヨーロッパ",
"pt-BR": "Europa",
"ru": "Европа",
"zh-CN": "欧洲",
},
record.Continent.Names,
)
assert.Equal(t, uint(2635167), record.Country.GeoNameID)
assert.False(t, record.Country.IsInEuropeanUnion)
assert.Equal(t, "GB", record.Country.IsoCode)
assert.Equal(t,
map[string]string{
"de": "Vereinigtes Königreich",
"en": "United Kingdom",
"es": "Reino Unido",
"fr": "Royaume-Uni",
"ja": "イギリス",
"pt-BR": "Reino Unido",
"ru": "Великобритания",
"zh-CN": "英国",
},
record.Country.Names,
)
assert.Equal(t, uint16(100), record.Location.AccuracyRadius)
assert.InEpsilon(t, 51.5142, record.Location.Latitude, 1e-10)
assert.InEpsilon(t, -0.0931, record.Location.Longitude, 1e-10)
assert.Equal(t, "Europe/London", record.Location.TimeZone)
assert.Equal(t, uint(6269131), record.Subdivisions[0].GeoNameID)
assert.Equal(t, "ENG", record.Subdivisions[0].IsoCode)
assert.Equal(t,
map[string]string{
"en": "England",
"pt-BR": "Inglaterra",
"fr": "Angleterre",
"es": "Inglaterra",
},
record.Subdivisions[0].Names,
)
assert.Equal(t, uint(6252001), record.RegisteredCountry.GeoNameID)
assert.False(t, record.RegisteredCountry.IsInEuropeanUnion)
assert.Equal(t, "US", record.RegisteredCountry.IsoCode)
assert.Equal(t,
map[string]string{
"de": "USA",
"en": "United States",
"es": "Estados Unidos",
"fr": "États-Unis",
"ja": "アメリカ合衆国",
"pt-BR": "Estados Unidos",
"ru": "США",
"zh-CN": "美国",
},
record.RegisteredCountry.Names,
)
assert.False(t, record.RepresentedCountry.IsInEuropeanUnion)
}
func TestIsAnycast(t *testing.T) {
for _, test := range []string{"Country", "City", "Enterprise"} {
t.Run(test, func(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-" + test + "-Test.mmdb")
require.NoError(t, err)
defer reader.Close()
record, err := reader.City(net.ParseIP("214.1.1.0"))
require.NoError(t, err)
assert.True(t, record.Traits.IsAnycast)
})
}
}
func TestMetroCode(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb")
require.NoError(t, err)
defer reader.Close()
record, err := reader.City(net.ParseIP("216.160.83.56"))
require.NoError(t, err)
assert.Equal(t, uint(819), record.Location.MetroCode)
}
func TestAnonymousIP(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-Anonymous-IP-Test.mmdb")
require.NoError(t, err)
defer reader.Close()
record, err := reader.AnonymousIP(net.ParseIP("1.2.0.0"))
require.NoError(t, err)
assert.True(t, record.IsAnonymous)
assert.True(t, record.IsAnonymousVPN)
assert.False(t, record.IsHostingProvider)
assert.False(t, record.IsPublicProxy)
assert.False(t, record.IsTorExitNode)
assert.False(t, record.IsResidentialProxy)
}
func TestASN(t *testing.T) {
reader, err := Open("test-data/test-data/GeoLite2-ASN-Test.mmdb")
require.NoError(t, err)
defer reader.Close()
record, err := reader.ASN(net.ParseIP("1.128.0.0"))
require.NoError(t, err)
assert.Equal(t, uint(1221), record.AutonomousSystemNumber)
assert.Equal(t, "Telstra Pty Ltd", record.AutonomousSystemOrganization)
}
func TestConnectionType(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-Connection-Type-Test.mmdb")
require.NoError(t, err)
defer reader.Close()
record, err := reader.ConnectionType(net.ParseIP("1.0.1.0"))
require.NoError(t, err)
assert.Equal(t, "Cellular", record.ConnectionType)
}
func TestCountry(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-Country-Test.mmdb")
require.NoError(t, err)
defer reader.Close()
record, err := reader.Country(net.ParseIP("81.2.69.160"))
require.NoError(t, err)
assert.False(t, record.Country.IsInEuropeanUnion)
assert.False(t, record.RegisteredCountry.IsInEuropeanUnion)
assert.False(t, record.RepresentedCountry.IsInEuropeanUnion)
}
func TestDomain(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-Domain-Test.mmdb")
require.NoError(t, err)
defer reader.Close()
record, err := reader.Domain(net.ParseIP("1.2.0.0"))
require.NoError(t, err)
assert.Equal(t, "maxmind.com", record.Domain)
}
func TestEnterprise(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-Enterprise-Test.mmdb")
require.NoError(t, err)
defer reader.Close()
record, err := reader.Enterprise(net.ParseIP("74.209.24.0"))
require.NoError(t, err)
assert.Equal(t, uint8(11), record.City.Confidence)
assert.Equal(t, uint(14671), record.Traits.AutonomousSystemNumber)
assert.Equal(t, "FairPoint Communications", record.Traits.AutonomousSystemOrganization)
assert.Equal(t, "Cable/DSL", record.Traits.ConnectionType)
assert.Equal(t, "frpt.net", record.Traits.Domain)
assert.InEpsilon(t, float64(0.34), record.Traits.StaticIPScore, 1e-10)
record, err = reader.Enterprise(net.ParseIP("149.101.100.0"))
require.NoError(t, err)
assert.Equal(t, uint(6167), record.Traits.AutonomousSystemNumber)
assert.Equal(t, "CELLCO-PART", record.Traits.AutonomousSystemOrganization)
assert.Equal(t, "Verizon Wireless", record.Traits.ISP)
assert.Equal(t, "310", record.Traits.MobileCountryCode)
assert.Equal(t, "004", record.Traits.MobileNetworkCode)
}
func TestISP(t *testing.T) {
reader, err := Open("test-data/test-data/GeoIP2-ISP-Test.mmdb")
require.NoError(t, err)
defer reader.Close()
record, err := reader.ISP(net.ParseIP("149.101.100.0"))
require.NoError(t, err)
assert.Equal(t, uint(6167), record.AutonomousSystemNumber)
assert.Equal(t, "CELLCO-PART", record.AutonomousSystemOrganization)
assert.Equal(t, "Verizon Wireless", record.ISP)
assert.Equal(t, "310", record.MobileCountryCode)
assert.Equal(t, "004", record.MobileNetworkCode)
assert.Equal(t, "Verizon Wireless", record.Organization)
}
// This ensures the compiler does not optimize away the function call.
var cityResult *City
func BenchmarkCity(b *testing.B) {
db, err := Open("GeoLite2-City.mmdb")
if err != nil {
b.Fatal(err)
}
defer db.Close()
//nolint:gosec // this is just a benchmark
r := rand.New(rand.NewSource(0))
var city *City
ip := make(net.IP, 4)
for i := 0; i < b.N; i++ {
randomIPv4Address(r, ip)
city, err = db.City(ip)
if err != nil {
b.Fatal(err)
}
}
cityResult = city
}
// This ensures the compiler does not optimize away the function call.
var asnResult *ASN
func BenchmarkASN(b *testing.B) {
db, err := Open("GeoLite2-ASN.mmdb")
if err != nil {
b.Fatal(err)
}
defer db.Close()
//nolint:gosec // this is just a benchmark
r := rand.New(rand.NewSource(0))
var asn *ASN
ip := make(net.IP, 4)
for i := 0; i < b.N; i++ {
randomIPv4Address(r, ip)
asn, err = db.ASN(ip)
if err != nil {
b.Fatal(err)
}
}
asnResult = asn
}
func randomIPv4Address(r *rand.Rand, ip net.IP) {
num := r.Uint32()
ip[0] = byte(num >> 24)
ip[1] = byte(num >> 16)
ip[2] = byte(num >> 8)
ip[3] = byte(num)
}