Codebase list golang-github-oschwald-geoip2-golang / fresh-snapshots/main
New upstream release. Debian Janitor 2 years ago
13 changed file(s) with 272 addition(s) and 117 deletion(s). Raw diff Collapse all Expand all
0 version: 2
1 updates:
2 - package-ecosystem: gomod
3 directory: "/"
4 schedule:
5 interval: daily
6 time: "13:00"
7 open-pull-requests-limit: 10
0 name: "Code scanning - action"
1
2 on:
3 push:
4 pull_request:
5 schedule:
6 - cron: '0 12 * * 5'
7
8 jobs:
9 CodeQL-Build:
10
11 runs-on: ubuntu-latest
12
13 steps:
14 - name: Checkout repository
15 uses: actions/checkout@v2
16 with:
17 # We must fetch at least the immediate parents so that if this is
18 # a pull request then we can checkout the head.
19 fetch-depth: 2
20
21 # If this run was triggered by a pull request event, then checkout
22 # the head of the pull request instead of the merge commit.
23 - run: git checkout HEAD^2
24 if: ${{ github.event_name == 'pull_request' }}
25
26 # Initializes the CodeQL tools for scanning.
27 - name: Initialize CodeQL
28 uses: github/codeql-action/init@v1
29 # Override language selection by uncommenting this and choosing your languages
30 # with:
31 # languages: go, javascript, csharp, python, cpp, java
32
33 # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
34 # If this step fails, then you should remove it and run the build manually (see below)
35 - name: Autobuild
36 uses: github/codeql-action/autobuild@v1
37
38 # ℹī¸ Command-line programs to run using the OS shell.
39 # 📚 https://git.io/JvXDl
40
41 # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines
42 # and modify them (or add more) to build your code if your project
43 # uses a compiled language
44
45 #- run: |
46 # make bootstrap
47 # make release
48
49 - name: Perform CodeQL Analysis
50 uses: github/codeql-action/analyze@v1
0 name: Go
1
2 on: [push, pull_request]
3
4 jobs:
5
6 build:
7 name: Build
8 strategy:
9 matrix:
10 go-version: [1.13.x, 1.14.x, 1.15.x]
11 platform: [ubuntu-latest, macos-latest, windows-latest]
12 runs-on: ${{ matrix.platform }}
13 steps:
14 - name: Set up Go 1.x
15 uses: actions/setup-go@v2
16 with:
17 go-version: ${{ matrix.go-version }}
18 id: go
19
20 - name: Check out code into the Go module directory
21 uses: actions/checkout@v2
22 with:
23 submodules: true
24
25 - name: Get dependencies
26 run: go get -v -t -d ./...
27
28 - name: Build
29 run: go build -v .
30
31 - name: Test
32 run: go test -race -v ./...
0 name: golangci-lint
1
2 on: [push, pull_request]
3
4 jobs:
5 golangci:
6 name: lint
7 runs-on: ubuntu-latest
8 steps:
9 - uses: actions/checkout@v2
10 - name: golangci-lint
11 uses: golangci/golangci-lint-action@v2
12 with:
13 version: v1.37
44 [linters]
55 disable-all = true
66 enable = [
7 "bodyclose",
78 "deadcode",
89 "depguard",
910 "errcheck",
11 "exportloopref",
1012 "goconst",
1113 "gocyclo",
1214 "gocritic",
13 "gofmt",
15 "gofumpt",
1416 "golint",
1517 "gosec",
1618 "gosimple",
19 "govet",
1720 "ineffassign",
1821 "maligned",
1922 "misspell",
2023 "nakedret",
24 "noctx",
25 "nolintlint",
26 "sqlclosecheck",
2127 "staticcheck",
2228 "structcheck",
29 "stylecheck",
2330 "typecheck",
2431 "unconvert",
2532 "unparam",
33 "unused",
2634 "varcheck",
27 "vet",
2835 "vetshadow",
2936 ]
37
38 [linters-settings.errcheck]
39 ignore = "Close,fmt:.*"
40
41 [linters-settings.gofumpt]
42 extra-rules = true
43
44 [issues]
45 exclude-use-default = false
46
47 [[issues.exclude-rules]]
48 linters = [
49 "gosec"
50 ]
51
52 # G304 - Potential file inclusion via variable (gosec)
53 # G404 - "Use of weak random number generator (math/rand instead of crypto/rand)"
54 # We only use this in tests.
55 text = "G304|G404"
+0
-48
.travis.yml less more
0 language: go
1
2 go:
3 - 1.9.x
4 - 1.10.x
5 - 1.11.x
6 - 1.12.x
7 - 1.13.x
8 - tip
9
10 os:
11 - linux
12 - linux-ppc64le
13 - osx
14 - windows
15
16 matrix:
17 allow_failures:
18 - go: tip
19
20 install:
21 - go get -v -t ./...
22
23 before_script:
24 - |
25 if [[ $TRAVIS_GO_VERSION == '1.13.x' && $TRAVIS_OS_NAME == 'linux' && $(arch) != 'ppc64le' ]]; then
26 curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin
27 fi
28
29 script:
30 - |
31 if [ $(arch) == "ppc64le" ]; then
32 go test -cpu 1,4 -v
33 else
34 go test -race -cpu 1,4 -v
35 fi
36 - |
37 if [ $(arch) == "ppc64le" ]; then
38 go test -v -tags appengine
39 else
40 go test -race -v -tags appengine
41 fi
42 - |
43 if [[ $TRAVIS_GO_VERSION == '1.13.x' && $TRAVIS_OS_NAME == 'linux' && $(arch) != 'ppc64le' ]]; then
44 golangci-lint run
45 fi
46
47 sudo: false
00 # GeoIP2 Reader for Go #
11
2 [![Build Status](https://travis-ci.org/oschwald/geoip2-golang.png?branch=master)](https://travis-ci.org/oschwald/geoip2-golang)
3 [![GoDoc](https://godoc.org/github.com/oschwald/geoip2-golang?status.png)](https://godoc.org/github.com/oschwald/geoip2-golang)
2 [![PkgGoDev](https://pkg.go.dev/badge/github.com/oschwald/geoip2-golang)](https://pkg.go.dev/github.com/oschwald/geoip2-golang)
43
54 This library reads MaxMind [GeoLite2](http://dev.maxmind.com/geoip/geoip2/geolite2/)
65 and [GeoIP2](http://www.maxmind.com/en/geolocation_landing) databases.
109 All data for the database record is decoded using this library. If you only
1110 need several fields, you may get superior performance by using maxminddb's
1211 `Lookup` directly with a result struct that only contains the required fields.
13 (See [example_test.go](https://github.com/oschwald/maxminddb-golang/blob/master/example_test.go)
12 (See [example_test.go](https://github.com/oschwald/maxminddb-golang/blob/main/example_test.go)
1413 in the maxminddb repository for an example of this.)
1514
1615 ## Installation ##
0 golang-github-oschwald-geoip2-golang (1.6.1-1) UNRELEASED; urgency=low
1
2 * New upstream release.
3
4 -- Debian Janitor <janitor@jelmer.uk> Sat, 29 Jan 2022 22:08:35 -0000
5
06 golang-github-oschwald-geoip2-golang (1.4.0-1) unstable; urgency=medium
17
28 [ Cyril Brulebois ]
1010 func Example() {
1111 db, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb")
1212 if err != nil {
13 log.Fatal(err)
13 log.Panic(err)
1414 }
1515 defer db.Close()
1616 // If you are using strings that may be invalid, check that ip is not nil
1717 ip := net.ParseIP("81.2.69.142")
1818 record, err := db.City(ip)
1919 if err != nil {
20 log.Fatal(err)
20 log.Panic(err)
2121 }
2222 fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"])
2323 fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["en"])
22 go 1.9
33
44 require (
5 github.com/oschwald/maxminddb-golang v1.6.0
6 github.com/stretchr/testify v1.4.0
5 github.com/oschwald/maxminddb-golang v1.8.0
6 github.com/stretchr/testify v1.7.0
77 )
11 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22 github.com/oschwald/maxminddb-golang v1.6.0 h1:KAJSjdHQ8Kv45nFIbtoLGrGWqHFajOIm7skTyz/+Dls=
33 github.com/oschwald/maxminddb-golang v1.6.0/go.mod h1:DUJFucBg2cvqx42YmDa/+xHvb0elJtOm3o4aFQ/nb/w=
4 github.com/oschwald/maxminddb-golang v1.7.0 h1:JmU4Q1WBv5Q+2KZy5xJI+98aUwTIrPPxZUkd5Cwr8Zc=
5 github.com/oschwald/maxminddb-golang v1.7.0/go.mod h1:RXZtst0N6+FY/3qCNmZMBApR19cdQj43/NM9VkrNAis=
6 github.com/oschwald/maxminddb-golang v1.8.0 h1:Uh/DSnGoxsyp/KYbY1AuP0tYEwfs0sCph9p/UMXK/Hk=
7 github.com/oschwald/maxminddb-golang v1.8.0/go.mod h1:RXZtst0N6+FY/3qCNmZMBApR19cdQj43/NM9VkrNAis=
48 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
59 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
10 github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
611 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
712 github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
813 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
14 github.com/stretchr/testify v1.5.0 h1:DMOzIV76tmoDNE9pX6RSN0aDtCYeCg5VueieJaAo1uw=
15 github.com/stretchr/testify v1.5.0/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
16 github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
17 github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
18 github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho=
19 github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
20 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
21 github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
22 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
923 golang.org/x/sys v0.0.0-20191224085550-c709ea063b76 h1:Dho5nD6R3PcW2SH1or8vS0dszDaXRxIw55lBX7XiE5g=
1024 golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1125 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1226 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1327 gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
1428 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
29 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
30 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6565 Names map[string]string `maxminddb:"names"`
6666 } `maxminddb:"subdivisions"`
6767 Traits struct {
68 AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"`
69 AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
70 ConnectionType string `maxminddb:"connection_type"`
71 Domain string `maxminddb:"domain"`
72 IsAnonymousProxy bool `maxminddb:"is_anonymous_proxy"`
73 IsLegitimateProxy bool `maxminddb:"is_legitimate_proxy"`
74 IsSatelliteProvider bool `maxminddb:"is_satellite_provider"`
75 ISP string `maxminddb:"isp"`
76 Organization string `maxminddb:"organization"`
77 UserType string `maxminddb:"user_type"`
68 AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"`
69 AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
70 ConnectionType string `maxminddb:"connection_type"`
71 Domain string `maxminddb:"domain"`
72 IsAnonymousProxy bool `maxminddb:"is_anonymous_proxy"`
73 IsLegitimateProxy bool `maxminddb:"is_legitimate_proxy"`
74 IsSatelliteProvider bool `maxminddb:"is_satellite_provider"`
75 ISP string `maxminddb:"isp"`
76 MobileCountryCode string `maxminddb:"mobile_country_code"`
77 MobileNetworkCode string `maxminddb:"mobile_network_code"`
78 Organization string `maxminddb:"organization"`
79 StaticIPScore float64 `maxminddb:"static_ip_score"`
80 UserType string `maxminddb:"user_type"`
7881 } `maxminddb:"traits"`
7982 }
8083
166169 // The AnonymousIP struct corresponds to the data in the GeoIP2
167170 // Anonymous IP database.
168171 type AnonymousIP struct {
169 IsAnonymous bool `maxminddb:"is_anonymous"`
170 IsAnonymousVPN bool `maxminddb:"is_anonymous_vpn"`
171 IsHostingProvider bool `maxminddb:"is_hosting_provider"`
172 IsPublicProxy bool `maxminddb:"is_public_proxy"`
173 IsTorExitNode bool `maxminddb:"is_tor_exit_node"`
172 IsAnonymous bool `maxminddb:"is_anonymous"`
173 IsAnonymousVPN bool `maxminddb:"is_anonymous_vpn"`
174 IsHostingProvider bool `maxminddb:"is_hosting_provider"`
175 IsPublicProxy bool `maxminddb:"is_public_proxy"`
176 IsResidentialProxy bool `maxminddb:"is_residential_proxy"`
177 IsTorExitNode bool `maxminddb:"is_tor_exit_node"`
174178 }
175179
176180 // The ASN struct corresponds to the data in the GeoLite2 ASN database.
195199 AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"`
196200 AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
197201 ISP string `maxminddb:"isp"`
202 MobileCountryCode string `maxminddb:"mobile_country_code"`
203 MobileNetworkCode string `maxminddb:"mobile_network_code"`
198204 Organization string `maxminddb:"organization"`
199205 }
200206
256262
257263 // FromBytes takes a byte slice corresponding to a GeoIP2/GeoLite2 database
258264 // file and returns a Reader struct or an error. Note that the byte slice is
259 // use directly; any modification of it after opening the database will result
265 // used directly; any modification of it after opening the database will result
260266 // in errors while reading from the database.
261267 func FromBytes(bytes []byte) (*Reader, error) {
262268 reader, err := maxminddb.FromBytes(bytes)
271277 switch reader.Metadata.DatabaseType {
272278 case "GeoIP2-Anonymous-IP":
273279 return isAnonymousIP, nil
274 case "GeoLite2-ASN":
280 case "DBIP-ASN-Lite (compat=GeoLite2-ASN)",
281 "GeoLite2-ASN":
275282 return isASN, nil
276283 // We allow City lookups on Country for back compat
277284 case "DBIP-City-Lite",
278 "DBIP-City",
279285 "DBIP-Country-Lite",
280286 "DBIP-Country",
287 "DBIP-Location (compat=City)",
281288 "GeoLite2-City",
282289 "GeoIP2-City",
283290 "GeoIP2-City-Africa",
293300 return isConnectionType, nil
294301 case "GeoIP2-Domain":
295302 return isDomain, nil
296 case "DBIP-Location-ISP (compat=Enterprise)",
303 case "DBIP-ISP (compat=Enterprise)",
304 "DBIP-Location-ISP (compat=Enterprise)",
297305 "GeoIP2-Enterprise":
298306 return isEnterprise | isCity | isCountry, nil
299307 case "GeoIP2-ISP",
1010
1111 func TestReader(t *testing.T) {
1212 reader, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb")
13 assert.Nil(t, err)
13 require.NoError(t, err)
1414
1515 defer reader.Close()
1616
1717 record, err := reader.City(net.ParseIP("81.2.69.160"))
18 assert.Nil(t, err)
18 require.NoError(t, err)
1919
2020 m := reader.Metadata()
2121 assert.Equal(t, uint(2), m.BinaryFormatMajorVersion)
6464 )
6565
6666 assert.Equal(t, uint(2635167), record.Country.GeoNameID)
67 assert.True(t, record.Country.IsInEuropeanUnion)
67 assert.False(t, record.Country.IsInEuropeanUnion)
6868 assert.Equal(t, "GB", record.Country.IsoCode)
6969 assert.Equal(t,
7070 map[string]string{
119119
120120 func TestMetroCode(t *testing.T) {
121121 reader, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb")
122 assert.Nil(t, err)
122 require.NoError(t, err)
123123 defer reader.Close()
124124
125125 record, err := reader.City(net.ParseIP("216.160.83.56"))
126 assert.Nil(t, err)
126 require.NoError(t, err)
127127
128128 assert.Equal(t, uint(819), record.Location.MetroCode)
129129 }
130130
131131 func TestAnonymousIP(t *testing.T) {
132132 reader, err := Open("test-data/test-data/GeoIP2-Anonymous-IP-Test.mmdb")
133 assert.Nil(t, err)
133 require.NoError(t, err)
134134 defer reader.Close()
135135
136136 record, err := reader.AnonymousIP(net.ParseIP("1.2.0.0"))
137 assert.Nil(t, err)
138
139 assert.Equal(t, true, record.IsAnonymous)
140
141 assert.Equal(t, true, record.IsAnonymousVPN)
142 assert.Equal(t, false, record.IsHostingProvider)
143 assert.Equal(t, false, record.IsPublicProxy)
144 assert.Equal(t, false, record.IsTorExitNode)
137 require.NoError(t, err)
138
139 assert.True(t, record.IsAnonymous)
140
141 assert.True(t, record.IsAnonymousVPN)
142 assert.False(t, record.IsHostingProvider)
143 assert.False(t, record.IsPublicProxy)
144 assert.False(t, record.IsTorExitNode)
145 assert.False(t, record.IsResidentialProxy)
145146 }
146147
147148 func TestASN(t *testing.T) {
148149 reader, err := Open("test-data/test-data/GeoLite2-ASN-Test.mmdb")
149 assert.Nil(t, err)
150 require.NoError(t, err)
150151 defer reader.Close()
151152
152153 record, err := reader.ASN(net.ParseIP("1.128.0.0"))
153 assert.Nil(t, err)
154 require.NoError(t, err)
154155
155156 assert.Equal(t, uint(1221), record.AutonomousSystemNumber)
156157
159160
160161 func TestConnectionType(t *testing.T) {
161162 reader, err := Open("test-data/test-data/GeoIP2-Connection-Type-Test.mmdb")
162 assert.Nil(t, err)
163 require.NoError(t, err)
163164
164165 defer reader.Close()
165166
166167 record, err := reader.ConnectionType(net.ParseIP("1.0.1.0"))
167 assert.Nil(t, err)
168 require.NoError(t, err)
168169
169170 assert.Equal(t, "Cable/DSL", record.ConnectionType)
170171 }
171172
172173 func TestCountry(t *testing.T) {
173174 reader, err := Open("test-data/test-data/GeoIP2-Country-Test.mmdb")
174 assert.Nil(t, err)
175 require.NoError(t, err)
175176
176177 defer reader.Close()
177178
178179 record, err := reader.Country(net.ParseIP("81.2.69.160"))
179 assert.Nil(t, err)
180
181 assert.True(t, record.Country.IsInEuropeanUnion)
180 require.NoError(t, err)
181
182 assert.False(t, record.Country.IsInEuropeanUnion)
182183 assert.False(t, record.RegisteredCountry.IsInEuropeanUnion)
183184 assert.False(t, record.RepresentedCountry.IsInEuropeanUnion)
184185 }
185186
186187 func TestDomain(t *testing.T) {
187188 reader, err := Open("test-data/test-data/GeoIP2-Domain-Test.mmdb")
188 assert.Nil(t, err)
189 require.NoError(t, err)
189190 defer reader.Close()
190191
191192 record, err := reader.Domain(net.ParseIP("1.2.0.0"))
192 assert.Nil(t, err)
193 require.NoError(t, err)
193194 assert.Equal(t, "maxmind.com", record.Domain)
194195 }
195196
208209 assert.Equal(t, "FairPoint Communications", record.Traits.AutonomousSystemOrganization)
209210 assert.Equal(t, "Cable/DSL", record.Traits.ConnectionType)
210211 assert.Equal(t, "frpt.net", record.Traits.Domain)
212 assert.Equal(t, float64(0.34), record.Traits.StaticIPScore)
213
214 record, err = reader.Enterprise(net.ParseIP("149.101.100.0"))
215 require.NoError(t, err)
216
217 assert.Equal(t, uint(6167), record.Traits.AutonomousSystemNumber)
218
219 assert.Equal(t, "CELLCO-PART", record.Traits.AutonomousSystemOrganization)
220 assert.Equal(t, "Verizon Wireless", record.Traits.ISP)
221 assert.Equal(t, "310", record.Traits.MobileCountryCode)
222 assert.Equal(t, "004", record.Traits.MobileNetworkCode)
211223 }
212224
213225 func TestISP(t *testing.T) {
214226 reader, err := Open("test-data/test-data/GeoIP2-ISP-Test.mmdb")
215 assert.Nil(t, err)
216 defer reader.Close()
217
218 record, err := reader.ISP(net.ParseIP("1.128.0.0"))
219 assert.Nil(t, err)
220
221 assert.Equal(t, uint(1221), record.AutonomousSystemNumber)
222
223 assert.Equal(t, "Telstra Pty Ltd", record.AutonomousSystemOrganization)
224 assert.Equal(t, "Telstra Internet", record.ISP)
225 assert.Equal(t, "Telstra Internet", record.Organization)
227 require.NoError(t, err)
228 defer reader.Close()
229
230 record, err := reader.ISP(net.ParseIP("149.101.100.0"))
231 require.NoError(t, err)
232
233 assert.Equal(t, uint(6167), record.AutonomousSystemNumber)
234
235 assert.Equal(t, "CELLCO-PART", record.AutonomousSystemOrganization)
236 assert.Equal(t, "Verizon Wireless", record.ISP)
237 assert.Equal(t, "310", record.MobileCountryCode)
238 assert.Equal(t, "004", record.MobileNetworkCode)
239 assert.Equal(t, "Verizon Wireless", record.Organization)
226240 }
227241
228242 // This ensures the compiler does not optimize away the function call
229243 var cityResult *City
230244
231 func BenchmarkMaxMindDB(b *testing.B) {
245 func BenchmarkCity(b *testing.B) {
232246 db, err := Open("GeoLite2-City.mmdb")
233247 if err != nil {
234248 b.Fatal(err)
239253
240254 var city *City
241255
256 ip := make(net.IP, 4)
242257 for i := 0; i < b.N; i++ {
243 ip := randomIPv4Address(r)
258 randomIPv4Address(r, ip)
244259 city, err = db.City(ip)
245260 if err != nil {
246261 b.Fatal(err)
249264 cityResult = city
250265 }
251266
252 func randomIPv4Address(r *rand.Rand) net.IP {
267 // This ensures the compiler does not optimize away the function call
268 var asnResult *ASN
269
270 func BenchmarkASN(b *testing.B) {
271 db, err := Open("GeoLite2-ASN.mmdb")
272 if err != nil {
273 b.Fatal(err)
274 }
275 defer db.Close()
276
277 r := rand.New(rand.NewSource(0))
278
279 var asn *ASN
280
281 ip := make(net.IP, 4)
282 for i := 0; i < b.N; i++ {
283 randomIPv4Address(r, ip)
284 asn, err = db.ASN(ip)
285 if err != nil {
286 b.Fatal(err)
287 }
288 }
289 asnResult = asn
290 }
291
292 func randomIPv4Address(r *rand.Rand, ip net.IP) {
253293 num := r.Uint32()
254 return []byte{byte(num >> 24), byte(num >> 16), byte(num >> 8),
255 byte(num)}
256 }
294 ip[0] = byte(num >> 24)
295 ip[1] = byte(num >> 16)
296 ip[2] = byte(num >> 8)
297 ip[3] = byte(num)
298 }