Skip to content

Commit

Permalink
add reader tests based on MaxMind-DB test db
Browse files Browse the repository at this point in the history
  • Loading branch information
IncSW committed Sep 6, 2020
1 parent bd8ab36 commit 889d341
Show file tree
Hide file tree
Showing 3 changed files with 406 additions and 90 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ testdata/GeoLite2-City.mmdb
testdata/GeoLite2-Country.mmdb

testdata/GeoIP2-Anonymous-IP-Test.mmdb
testdata/GeoIP2-City-Test.mmdb
testdata/GeoIP2-Connection-Type-Test.mmdb
testdata/GeoIP2-Country-Test.mmdb
testdata/GeoIP2-Domain-Test.mmdb
testdata/GeoIP2-Enterprise-Test.mmdb
testdata/GeoIP2-ISP-Test.mmdb
testdata/GeoLite2-ASN-Test.mmdb
90 changes: 0 additions & 90 deletions geoip2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package geoip2
import (
"net"
"testing"
"time"
)

func TestReader(t *testing.T) {
Expand Down Expand Up @@ -71,95 +70,6 @@ func TestReader(t *testing.T) {
}
}

func TestDomain(t *testing.T) {
reader, err := NewDomainReaderFromFile("testdata/GeoIP2-Domain-Test.mmdb")
if err != nil {
t.Fatal(err)
}
record, err := reader.Lookup(net.ParseIP("1.2.0.0"))
if err != nil {
t.Fatal(err)
}
if record != "maxmind.com" {
t.Fatal()
}
}

func TestAnonymousIP(t *testing.T) {
reader, err := NewAnonymousIPReaderFromFile("testdata/GeoIP2-Anonymous-IP-Test.mmdb")
if err != nil {
t.Fatal(err)
}
record, err := reader.Lookup(net.ParseIP("1.2.0.0"))
if err != nil {
t.Fatal(err)
}
if record.IsAnonymous != true {
t.Fatal()
}
if record.IsAnonymousVPN != true {
t.Fatal()
}
if record.IsHostingProvider != false {
t.Fatal()
}
if record.IsPublicProxy != false {
t.Fatal()
}
if record.IsTorExitNode != false {
t.Fatal()
}
}

func TestEnterprise(t *testing.T) {
reader, err := NewEnterpriseReaderFromFile("testdata/GeoIP2-Enterprise-Test.mmdb")
if err != nil {
t.Fatal(err)
}
record, err := reader.Lookup(net.ParseIP("74.209.24.0"))
if err != nil {
t.Fatal(err)
}
if record.City.Confidence != 11 {
t.Fatal()
}
if record.Traits.AutonomousSystemNumber != 14671 {
t.Fatal()
}
if record.Traits.AutonomousSystemOrganization != "FairPoint Communications" {
t.Fatal()
}
if record.Traits.ConnectionType != "Cable/DSL" {
t.Fatal()
}
if record.Traits.Domain != "frpt.net" {
t.Fatal()
}
if record.Traits.StaticIPScore != 0.34 {
t.Fatal()
}
}

func TestBench(t *testing.T) {
reader, err := NewCityReaderFromFile("testdata/GeoIP2-City.mmdb")
if err != nil {
t.Fatal(err)
}
ip := net.ParseIP("81.2.69.142")
var minDuration time.Duration
for i := 0; i < 200; i++ {
start := time.Now()
for j := 0; j < 10000; j++ {
_, _ = reader.Lookup(ip)
}
duration := time.Since(start)
if minDuration == 0 || minDuration > duration {
minDuration = duration
}
}
t.Log(int(minDuration/10000), "ns/op")
}

func BenchmarkGeoIP2(b *testing.B) {
ip := net.ParseIP("81.2.69.142")
b.ReportAllocs()
Expand Down
Loading

0 comments on commit 889d341

Please sign in to comment.