targeting_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. {"@ continent country asn", "@ continent country asn"},
  29. {"asn country", "country asn"},
  30. {"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("93.184.216.34")
  46. g, err := geoip2.New(geoip2.FindDB())
  47. if err != nil {
  48. t.Fatalf("opening geoip2: %s", err)
  49. }
  50. Setup(g)
  51. tgt, _ := ParseTargets("@ continent country")
  52. targets, _, _ := tgt.GetTargets(ip, false)
  53. expect := []string{"us", "north-america", "@"}
  54. if !reflect.DeepEqual(targets, expect) {
  55. t.Fatalf("Unexpected parse results of targets, got '%s', expected '%s'", targets, expect)
  56. }
  57. if ok, err := g.HasLocation(); !ok {
  58. t.Logf("City GeoIP database required for these tests: %s", err)
  59. return
  60. }
  61. type test struct {
  62. Str string
  63. Targets []string
  64. IP string
  65. }
  66. tests := []test{
  67. {
  68. "@ continent country region ",
  69. []string{"us-ma", "us", "north-america", "@"},
  70. "",
  71. },
  72. {
  73. "@ continent regiongroup country region ",
  74. []string{"us-ma", "us-east", "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. // GeoLite2 doesn't have cities/regions for IPv6 addresses?
  84. "country",
  85. []string{"us"},
  86. "2606:2800:220:1:248:1893:25c8:1946",
  87. },
  88. }
  89. if ok, _ := g.HasASN(); ok {
  90. tests = append(tests,
  91. test{
  92. "@ continent regiongroup country region asn ip",
  93. []string{"[98.248.0.1]", "[98.248.0.0]", "as7922", "us-ca", "us-west", "us", "north-america", "@"},
  94. "98.248.0.1",
  95. },
  96. test{
  97. "country asn",
  98. []string{"as8674", "se"},
  99. "2a01:3f0:1:3::1",
  100. },
  101. )
  102. }
  103. for _, test := range tests {
  104. if len(test.IP) > 0 {
  105. ip = net.ParseIP(test.IP)
  106. }
  107. tgt, _ = ParseTargets(test.Str)
  108. targets, _, _ = tgt.GetTargets(ip, false)
  109. t.Logf("testing %s, got %q", ip, targets)
  110. if !reflect.DeepEqual(targets, test.Targets) {
  111. t.Logf("For IP '%s' targets '%s' expected '%s', got '%s'", ip, test.Str, test.Targets, targets)
  112. t.Fail()
  113. }
  114. }
  115. }