targeting_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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().SetupGeoIPCity()
  46. GeoIP().SetupGeoIPCountry()
  47. GeoIP().SetupGeoIPASN()
  48. tgt, _ := ParseTargets("@ continent country")
  49. targets, _, _ := tgt.GetTargets(ip, false)
  50. if !reflect.DeepEqual(targets, []string{"us", "north-america", "@"}) {
  51. t.Fatalf("Unexpected parse results of targets")
  52. }
  53. if geoIP.city == nil {
  54. t.Log("City GeoIP database requred for these tests")
  55. return
  56. }
  57. tests := []struct {
  58. Str string
  59. Targets []string
  60. IP string
  61. }{
  62. {
  63. "@ continent country region ",
  64. []string{"us-ca", "us", "north-america", "@"},
  65. "",
  66. },
  67. {
  68. "@ continent regiongroup country region ",
  69. []string{"us-ca", "us-west", "us", "north-america", "@"},
  70. "",
  71. },
  72. {
  73. "@ continent regiongroup country region asn ip",
  74. []string{"[207.171.1.1]", "[207.171.1.0]", "as7012", "us-ca", "us-west", "us", "north-america", "@"},
  75. "",
  76. },
  77. {
  78. "ip",
  79. []string{"[2607:f238:2::ff:4]", "[2607:f238:2::]"},
  80. "2607:f238:2:0::ff:4",
  81. },
  82. }
  83. for _, test := range tests {
  84. if len(test.IP) > 0 {
  85. ip = net.ParseIP(test.IP)
  86. }
  87. tgt, _ = ParseTargets(test.Str)
  88. targets, _, _ = tgt.GetTargets(ip, false)
  89. if !reflect.DeepEqual(targets, test.Targets) {
  90. t.Logf("For targets '%s' expected '%s', got '%s'", test.Str, test.Targets, targets)
  91. t.Fail()
  92. }
  93. }
  94. }