|
@@ -4,6 +4,8 @@ import (
|
|
"net"
|
|
"net"
|
|
"reflect"
|
|
"reflect"
|
|
"testing"
|
|
"testing"
|
|
|
|
+
|
|
|
|
+ "github.com/abh/geodns/targeting/geoip2"
|
|
)
|
|
)
|
|
|
|
|
|
func TestTargetString(t *testing.T) {
|
|
func TestTargetString(t *testing.T) {
|
|
@@ -50,28 +52,36 @@ 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("207.171.1.1")
|
|
|
|
|
|
- GeoIP().SetDirectory("../db")
|
|
|
|
|
|
+ g, err := geoip2.New("../db")
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatalf("opening geoip2: %s", err)
|
|
|
|
+ }
|
|
|
|
+ Setup(g)
|
|
|
|
|
|
- GeoIP().SetupGeoIPCity()
|
|
|
|
- GeoIP().SetupGeoIPCountry()
|
|
|
|
- GeoIP().SetupGeoIPASN()
|
|
|
|
|
|
+ // GeoIP().SetDirectory("../db")
|
|
|
|
+ // GeoIP().SetupGeoIPCity()
|
|
|
|
+ // GeoIP().SetupGeoIPCountry()
|
|
|
|
+ // GeoIP().SetupGeoIPASN()
|
|
|
|
|
|
tgt, _ := ParseTargets("@ continent country")
|
|
tgt, _ := ParseTargets("@ continent country")
|
|
targets, _, _ := tgt.GetTargets(ip, false)
|
|
targets, _, _ := tgt.GetTargets(ip, false)
|
|
- if !reflect.DeepEqual(targets, []string{"us", "north-america", "@"}) {
|
|
|
|
- t.Fatalf("Unexpected parse results of targets")
|
|
|
|
|
|
+ expect := []string{"us", "north-america", "@"}
|
|
|
|
+ if !reflect.DeepEqual(targets, expect) {
|
|
|
|
+ t.Fatalf("Unexpected parse results of targets, got '%s', expected '%s'", targets, expect)
|
|
}
|
|
}
|
|
|
|
|
|
- if geoIP.city == nil {
|
|
|
|
|
|
+ if !g.HasLocation() {
|
|
t.Log("City GeoIP database requred for these tests")
|
|
t.Log("City GeoIP database requred for these tests")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- tests := []struct {
|
|
|
|
|
|
+ type test struct {
|
|
Str string
|
|
Str string
|
|
Targets []string
|
|
Targets []string
|
|
IP string
|
|
IP string
|
|
- }{
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ tests := []test{
|
|
{
|
|
{
|
|
"@ continent country region ",
|
|
"@ continent country region ",
|
|
[]string{"us-ca", "us", "north-america", "@"},
|
|
[]string{"us-ca", "us", "north-america", "@"},
|
|
@@ -82,11 +92,6 @@ func TestGetTargets(t *testing.T) {
|
|
[]string{"us-ca", "us-west", "us", "north-america", "@"},
|
|
[]string{"us-ca", "us-west", "us", "north-america", "@"},
|
|
"",
|
|
"",
|
|
},
|
|
},
|
|
- {
|
|
|
|
- "@ continent regiongroup country region asn ip",
|
|
|
|
- []string{"[207.171.1.1]", "[207.171.1.0]", "as7012", "us-ca", "us-west", "us", "north-america", "@"},
|
|
|
|
- "",
|
|
|
|
- },
|
|
|
|
{
|
|
{
|
|
"ip",
|
|
"ip",
|
|
[]string{"[2607:f238:2::ff:4]", "[2607:f238:2::]"},
|
|
[]string{"[2607:f238:2::ff:4]", "[2607:f238:2::]"},
|
|
@@ -94,6 +99,14 @@ func TestGetTargets(t *testing.T) {
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if g.HasASN() {
|
|
|
|
+ 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", "@"},
|
|
|
|
+ "",
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
for _, test := range tests {
|
|
for _, test := range tests {
|
|
if len(test.IP) > 0 {
|
|
if len(test.IP) > 0 {
|
|
ip = net.ParseIP(test.IP)
|
|
ip = net.ParseIP(test.IP)
|
|
@@ -103,7 +116,7 @@ func TestGetTargets(t *testing.T) {
|
|
targets, _, _ = tgt.GetTargets(ip, false)
|
|
targets, _, _ = tgt.GetTargets(ip, false)
|
|
|
|
|
|
if !reflect.DeepEqual(targets, test.Targets) {
|
|
if !reflect.DeepEqual(targets, test.Targets) {
|
|
- t.Logf("For targets '%s' expected '%s', got '%s'", 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()
|
|
}
|
|
}
|
|
|
|
|