targeting_test.go 1.9 KB

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