targeting_test.go 2.4 KB

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