targeting_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "net"
  4. . "gopkg.in/check.v1"
  5. )
  6. type TargetingSuite struct {
  7. }
  8. var _ = Suite(&TargetingSuite{})
  9. func (s *TargetingSuite) SetUpSuite(c *C) {
  10. Config.GeoIP.Directory = "db"
  11. }
  12. func (s *TargetingSuite) TestTargetString(c *C) {
  13. var tgt TargetOptions
  14. tgt = TargetGlobal + TargetCountry + TargetContinent
  15. str := tgt.String()
  16. c.Check(str, Equals, "@ continent country")
  17. }
  18. func (s *TargetingSuite) TestTargetParse(c *C) {
  19. tgt, err := parseTargets("@ foo country")
  20. str := tgt.String()
  21. c.Check(str, Equals, "@ country")
  22. c.Check(err.Error(), Equals, "Unknown targeting option 'foo'")
  23. tgt, err = parseTargets("@ continent country asn")
  24. c.Assert(err, IsNil)
  25. str = tgt.String()
  26. c.Check(str, Equals, "@ continent country asn")
  27. }
  28. func (s *TargetingSuite) TestGetTargets(c *C) {
  29. ip := net.ParseIP("207.171.1.1")
  30. geoIP.setupGeoIPCity()
  31. geoIP.setupGeoIPCountry()
  32. geoIP.setupGeoIPASN()
  33. tgt, _ := parseTargets("@ continent country")
  34. targets, _ := tgt.GetTargets(ip)
  35. c.Check(targets, DeepEquals, []string{"us", "north-america", "@"})
  36. if geoIP.city == nil {
  37. c.Log("City GeoIP database requred for these tests")
  38. return
  39. }
  40. tgt, _ = parseTargets("@ continent country region ")
  41. targets, _ = tgt.GetTargets(ip)
  42. c.Check(targets, DeepEquals, []string{"us-ca", "us", "north-america", "@"})
  43. tgt, _ = parseTargets("@ continent regiongroup country region ")
  44. targets, _ = tgt.GetTargets(ip)
  45. c.Check(targets, DeepEquals, []string{"us-ca", "us-west", "us", "north-america", "@"})
  46. tgt, _ = parseTargets("@ continent regiongroup country region asn ip")
  47. targets, _ = tgt.GetTargets(ip)
  48. c.Check(targets, DeepEquals, []string{"[207.171.1.1]", "[207.171.1.0]", "as7012", "us-ca", "us-west", "us", "north-america", "@"})
  49. ip = net.ParseIP("2607:f238:2:0::ff:4")
  50. tgt, _ = parseTargets("ip")
  51. targets, _ = tgt.GetTargets(ip)
  52. c.Check(targets, DeepEquals, []string{"[2607:f238:2::ff:4]", "[2607:f238:2::]"})
  53. }