Skip to content

Commit

Permalink
Fix targeting tests (GeoIP data changed...)
Browse files Browse the repository at this point in the history
  • Loading branch information
abh committed Dec 28, 2019
1 parent b9443b0 commit 011d8cf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
13 changes: 10 additions & 3 deletions targeting/geo/geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/golang/geo/s2"
)

// Provider is the interface for geoip providers
type Provider interface {
HasCountry() (bool, error)
GetCountry(ip net.IP) (country, continent string, netmask int)
Expand All @@ -16,8 +17,12 @@ type Provider interface {
GetLocation(ip net.IP) (location *Location, err error)
}

const MAX_DISTANCE = 360
// MaxDistance is the distance returned if Distance() is
// called with a nil location
const MaxDistance = 360

// Location is the struct the GeoIP provider packages use to
// return location details for an IP.
type Location struct {
Country string
Continent string
Expand All @@ -28,13 +33,15 @@ type Location struct {
Netmask int
}

// MaxDistance() returns the MaxDistance constant
func (l *Location) MaxDistance() float64 {
return MAX_DISTANCE
return MaxDistance
}

// Distance returns the distance between the two locations
func (l *Location) Distance(to *Location) float64 {
if to == nil {
return MAX_DISTANCE
return MaxDistance
}
ll1 := s2.LatLngFromDegrees(l.Latitude, l.Longitude)
ll2 := s2.LatLngFromDegrees(to.Latitude, to.Longitude)
Expand Down
26 changes: 20 additions & 6 deletions targeting/targeting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestTargetParse(t *testing.T) {
}

func TestGetTargets(t *testing.T) {
ip := net.ParseIP("207.171.1.1")
ip := net.ParseIP("93.184.216.34")

g, err := geoip2.New(geoip2.FindDB())
if err != nil {
Expand Down Expand Up @@ -79,27 +79,39 @@ func TestGetTargets(t *testing.T) {
tests := []test{
{
"@ continent country region ",
[]string{"us-ca", "us", "north-america", "@"},
[]string{"us-ma", "us", "north-america", "@"},
"",
},
{
"@ continent regiongroup country region ",
[]string{"us-ca", "us-west", "us", "north-america", "@"},
[]string{"us-ma", "us-east", "us", "north-america", "@"},
"",
},
{
"ip",
[]string{"[2607:f238:2::ff:4]", "[2607:f238:2::]"},
"2607:f238:2:0::ff:4",
},
{
// GeoLite2 doesn't have cities/regions for IPv6 addresses?
"country",
[]string{"us"},
"2606:2800:220:1:248:1893:25c8:1946",
},
}

if ok, _ := g.HasASN(); ok {
tests = append(tests,
test{"@ continent regiongroup country region asn ip",
[]string{"[207.171.1.1]", "[207.171.1.0]", "as7012", "us-ca", "us-west", "us", "north-america", "@"},
"207.171.1.1",
})
[]string{"[98.248.0.1]", "[98.248.0.0]", "as7922", "us-ca", "us-west", "us", "north-america", "@"},
"98.248.0.1",
},
test{
"country asn",
[]string{"as8674", "se"},
"2a01:3f0:1:3::1",
},
)
}

for _, test := range tests {
Expand All @@ -110,6 +122,8 @@ func TestGetTargets(t *testing.T) {
tgt, _ = ParseTargets(test.Str)
targets, _, _ = tgt.GetTargets(ip, false)

t.Logf("testing %s, got %q", ip, targets)

if !reflect.DeepEqual(targets, test.Targets) {
t.Logf("For IP '%s' targets '%s' expected '%s', got '%s'", ip, test.Str, test.Targets, targets)
t.Fail()
Expand Down

0 comments on commit 011d8cf

Please sign in to comment.