targeting_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package targeting
  2. import (
  3. "net"
  4. "reflect"
  5. "testing"
  6. "github.com/abh/geodns/targeting/geoip2"
  7. )
  8. func TestTargetString(t *testing.T) {
  9. tgt := TargetOptions(TargetGlobal + TargetCountry + TargetContinent)
  10. str := tgt.String()
  11. if str != "@ continent country" {
  12. t.Logf("wrong target string '%s'", str)
  13. t.Fail()
  14. }
  15. }
  16. func TestTargetParse(t *testing.T) {
  17. tgt, err := ParseTargets("@ foo country")
  18. str := tgt.String()
  19. if str != "@ country" {
  20. t.Logf("Expected '@ country', got '%s'", str)
  21. t.Fail()
  22. }
  23. if err.Error() != "Unknown targeting option 'foo'" {
  24. t.Log("Failed erroring on an unknown targeting option")
  25. t.Fail()
  26. }
  27. tests := [][]string{
  28. []string{"@ continent country asn", "@ continent country asn"},
  29. []string{"asn country", "country asn"},
  30. []string{"continent @ country", "@ continent country"},
  31. }
  32. for _, strs := range tests {
  33. tgt, err = ParseTargets(strs[0])
  34. if err != nil {
  35. t.Fatalf("Parsing '%s': %s", strs[0], err)
  36. }
  37. if tgt.String() != strs[1] {
  38. t.Logf("Unexpected result parsing '%s', got '%s', expected '%s'",
  39. strs[0], tgt.String(), strs[1])
  40. t.Fail()
  41. }
  42. }
  43. }
  44. func TestGetTargets(t *testing.T) {
  45. ip := net.ParseIP("207.171.1.1")
  46. g, err := geoip2.New("../db")
  47. if err != nil {
  48. t.Fatalf("opening geoip2: %s", err)
  49. }
  50. Setup(g)
  51. // GeoIP().SetDirectory("../db")
  52. // GeoIP().SetupGeoIPCity()
  53. // GeoIP().SetupGeoIPCountry()
  54. // GeoIP().SetupGeoIPASN()
  55. tgt, _ := ParseTargets("@ continent country")
  56. targets, _, _ := tgt.GetTargets(ip, false)
  57. expect := []string{"us", "north-america", "@"}
  58. if !reflect.DeepEqual(targets, expect) {
  59. t.Fatalf("Unexpected parse results of targets, got '%s', expected '%s'", targets, expect)
  60. }
  61. if !g.HasLocation() {
  62. t.Log("City GeoIP database requred for these tests")
  63. return
  64. }
  65. type test struct {
  66. Str string
  67. Targets []string
  68. IP string
  69. }
  70. tests := []test{
  71. {
  72. "@ continent country region ",
  73. []string{"us-ca", "us", "north-america", "@"},
  74. "",
  75. },
  76. {
  77. "@ continent regiongroup country region ",
  78. []string{"us-ca", "us-west", "us", "north-america", "@"},
  79. "",
  80. },
  81. {
  82. "ip",
  83. []string{"[2607:f238:2::ff:4]", "[2607:f238:2::]"},
  84. "2607:f238:2:0::ff:4",
  85. },
  86. }
  87. if g.HasASN() {
  88. tests = append(tests,
  89. test{"@ continent regiongroup country region asn ip",
  90. []string{"[207.171.1.1]", "[207.171.1.0]", "as7012", "us-ca", "us-west", "us", "north-america", "@"},
  91. "",
  92. })
  93. }
  94. for _, test := range tests {
  95. if len(test.IP) > 0 {
  96. ip = net.ParseIP(test.IP)
  97. }
  98. tgt, _ = ParseTargets(test.Str)
  99. targets, _, _ = tgt.GetTargets(ip, false)
  100. if !reflect.DeepEqual(targets, test.Targets) {
  101. t.Logf("For IP '%s' targets '%s' expected '%s', got '%s'", ip, test.Str, test.Targets, targets)
  102. t.Fail()
  103. }
  104. }
  105. }