Browse Source

Fix targeting tests (GeoIP data changed...)

Ask Bjørn Hansen 5 years ago
parent
commit
011d8cfc65
2 changed files with 30 additions and 9 deletions
  1. 10 3
      targeting/geo/geo.go
  2. 20 6
      targeting/targeting_test.go

+ 10 - 3
targeting/geo/geo.go

@@ -7,6 +7,7 @@ import (
 	"github.com/golang/geo/s2"
 	"github.com/golang/geo/s2"
 )
 )
 
 
+// Provider is the interface for geoip providers
 type Provider interface {
 type Provider interface {
 	HasCountry() (bool, error)
 	HasCountry() (bool, error)
 	GetCountry(ip net.IP) (country, continent string, netmask int)
 	GetCountry(ip net.IP) (country, continent string, netmask int)
@@ -16,8 +17,12 @@ type Provider interface {
 	GetLocation(ip net.IP) (location *Location, err error)
 	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 {
 type Location struct {
 	Country     string
 	Country     string
 	Continent   string
 	Continent   string
@@ -28,13 +33,15 @@ type Location struct {
 	Netmask     int
 	Netmask     int
 }
 }
 
 
+// MaxDistance() returns the MaxDistance constant
 func (l *Location) MaxDistance() float64 {
 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 {
 func (l *Location) Distance(to *Location) float64 {
 	if to == nil {
 	if to == nil {
-		return MAX_DISTANCE
+		return MaxDistance
 	}
 	}
 	ll1 := s2.LatLngFromDegrees(l.Latitude, l.Longitude)
 	ll1 := s2.LatLngFromDegrees(l.Latitude, l.Longitude)
 	ll2 := s2.LatLngFromDegrees(to.Latitude, to.Longitude)
 	ll2 := s2.LatLngFromDegrees(to.Latitude, to.Longitude)

+ 20 - 6
targeting/targeting_test.go

@@ -50,7 +50,7 @@ func TestTargetParse(t *testing.T) {
 }
 }
 
 
 func TestGetTargets(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())
 	g, err := geoip2.New(geoip2.FindDB())
 	if err != nil {
 	if err != nil {
@@ -79,12 +79,12 @@ func TestGetTargets(t *testing.T) {
 	tests := []test{
 	tests := []test{
 		{
 		{
 			"@ continent country region ",
 			"@ continent country region ",
-			[]string{"us-ca", "us", "north-america", "@"},
+			[]string{"us-ma", "us", "north-america", "@"},
 			"",
 			"",
 		},
 		},
 		{
 		{
 			"@ continent regiongroup country region ",
 			"@ continent regiongroup country region ",
-			[]string{"us-ca", "us-west", "us", "north-america", "@"},
+			[]string{"us-ma", "us-east", "us", "north-america", "@"},
 			"",
 			"",
 		},
 		},
 		{
 		{
@@ -92,14 +92,26 @@ func TestGetTargets(t *testing.T) {
 			[]string{"[2607:f238:2::ff:4]", "[2607:f238:2::]"},
 			[]string{"[2607:f238:2::ff:4]", "[2607:f238:2::]"},
 			"2607:f238:2:0::ff:4",
 			"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 {
 	if ok, _ := g.HasASN(); ok {
 		tests = append(tests,
 		tests = append(tests,
 			test{"@ continent regiongroup country region asn ip",
 			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 {
 	for _, test := range tests {
@@ -110,6 +122,8 @@ func TestGetTargets(t *testing.T) {
 		tgt, _ = ParseTargets(test.Str)
 		tgt, _ = ParseTargets(test.Str)
 		targets, _, _ = tgt.GetTargets(ip, false)
 		targets, _, _ = tgt.GetTargets(ip, false)
 
 
+		t.Logf("testing %s, got %q", ip, targets)
+
 		if !reflect.DeepEqual(targets, test.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.Logf("For IP '%s' targets '%s' expected '%s', got '%s'", ip, test.Str, test.Targets, targets)
 			t.Fail()
 			t.Fail()