forked from oschwald/geoip2-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
186 lines (158 loc) · 4.26 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
package geoip2
import (
"fmt"
"math/rand"
"net"
"testing"
"time"
. "launchpad.net/gocheck"
)
func TestGeoIP2(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestReader(c *C) {
reader, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb")
if err != nil {
c.Log(err)
c.Fail()
}
defer reader.Close()
record, err := reader.City(net.ParseIP("81.2.69.160"))
if err != nil {
c.Log(err)
c.Fail()
}
c.Assert(record.City.GeoNameID, Equals, uint(2643743))
c.Assert(record.City.Names, DeepEquals, map[string]string{
"de": "London",
"en": "London",
"es": "Londres",
"fr": "Londres",
"ja": "ロンドン",
"pt-BR": "Londres",
"ru": "Лондон",
})
c.Assert(record.Continent.GeoNameID, Equals, uint(6255148))
c.Assert(record.Continent.Code, Equals, "EU")
c.Assert(record.Continent.Names, DeepEquals, map[string]string{
"de": "Europa",
"en": "Europe",
"es": "Europa",
"fr": "Europe",
"ja": "ヨーロッパ",
"pt-BR": "Europa",
"ru": "Европа",
"zh-CN": "欧洲",
})
c.Assert(record.Country.GeoNameID, Equals, uint(2635167))
c.Assert(record.Country.IsoCode, Equals, "GB")
c.Assert(record.Country.Names, DeepEquals, map[string]string{
"de": "Vereinigtes Königreich",
"en": "United Kingdom",
"es": "Reino Unido",
"fr": "Royaume-Uni",
"ja": "イギリス",
"pt-BR": "Reino Unido",
"ru": "Великобритания",
"zh-CN": "英国",
})
c.Assert(record.Location.Latitude, Equals, 51.5142)
c.Assert(record.Location.Longitude, Equals, -0.0931)
c.Assert(record.Location.TimeZone, Equals, "Europe/London")
c.Assert(record.Subdivisions[0].GeoNameID, Equals, uint(6269131))
c.Assert(record.Subdivisions[0].IsoCode, Equals, "ENG")
c.Assert(record.Subdivisions[0].Names, DeepEquals, map[string]string{
"en": "England",
"pt-BR": "Inglaterra",
"fr": "Angleterre",
"es": "Inglaterra",
})
c.Assert(record.RegisteredCountry.GeoNameID, Equals, uint(6252001))
c.Assert(record.RegisteredCountry.IsoCode, Equals, "US")
c.Assert(record.RegisteredCountry.Names, DeepEquals, map[string]string{
"de": "USA",
"en": "United States",
"es": "Estados Unidos",
"fr": "États-Unis",
"ja": "アメリカ合衆国",
"pt-BR": "Estados Unidos",
"ru": "США",
"zh-CN": "美国",
})
}
func (s *MySuite) TestMetroCode(c *C) {
reader, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb")
if err != nil {
c.Log(err)
c.Fail()
}
defer reader.Close()
record, err := reader.City(net.ParseIP("216.160.83.56"))
if err != nil {
c.Log(err)
c.Fail()
}
c.Assert(record.Location.MetroCode, Equals, uint(819))
}
func (s *MySuite) TestConnectionType(c *C) {
reader, err := Open("test-data/test-data/GeoIP2-Connection-Type-Test.mmdb")
if err != nil {
c.Log(err)
c.Fail()
}
defer reader.Close()
record, err := reader.ConnectionType(net.ParseIP("1.0.1.0"))
if err != nil {
c.Log(err)
c.Fail()
}
c.Assert(record.ConnectionType, Equals, "Cable/DSL")
}
func (s *MySuite) TestDomain(c *C) {
reader, err := Open("test-data/test-data/GeoIP2-Domain-Test.mmdb")
if err != nil {
c.Log(err)
c.Fail()
}
defer reader.Close()
record, err := reader.Domain(net.ParseIP("1.2.0.0"))
if err != nil {
c.Log(err)
c.Fail()
}
c.Assert(record.Domain, Equals, "maxmind.com")
}
func (s *MySuite) TestISP(c *C) {
reader, err := Open("test-data/test-data/GeoIP2-ISP-Test.mmdb")
if err != nil {
c.Log(err)
c.Fail()
}
defer reader.Close()
record, err := reader.ISP(net.ParseIP("1.128.0.0"))
if err != nil {
c.Log(err)
c.Fail()
}
c.Assert(record.AutonomousSystemNumber, Equals, uint(1221))
c.Assert(record.AutonomousSystemOrganization, Equals, "Telstra Pty Ltd")
c.Assert(record.ISP, Equals, "Telstra Internet")
c.Assert(record.Organization, Equals, "Telstra Internet")
}
func BenchmarkMaxMindDB(b *testing.B) {
db, err := Open("GeoLite2-City.mmdb")
if err != nil {
b.Fatal(err)
}
defer db.Close()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < b.N; i++ {
num := r.Uint32()
ip := net.ParseIP(fmt.Sprintf("%d.%d.%d.%d", (0xFF000000&num)>>24,
(0x00FF0000&num)>>16, (0x0000FF00&num)>>8, 0x000000F&num))
_, err := db.City(ip)
if err != nil {
b.Fatal(err)
}
}
}