forked from abh/geodns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtargeting_test.go
69 lines (52 loc) · 1.93 KB
/
targeting_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
. "github.com/abh/geodns/Godeps/_workspace/src/gopkg.in/check.v1"
"net"
)
type TargetingSuite struct {
}
var _ = Suite(&TargetingSuite{})
func (s *TargetingSuite) SetUpSuite(c *C) {
Config.GeoIP.Directory = "db"
}
func (s *TargetingSuite) TestTargetString(c *C) {
tgt := TargetOptions(TargetGlobal + TargetCountry + TargetContinent)
str := tgt.String()
c.Check(str, Equals, "@ continent country")
}
func (s *TargetingSuite) TestTargetParse(c *C) {
tgt, err := parseTargets("@ foo country")
str := tgt.String()
c.Check(str, Equals, "@ country")
c.Check(err.Error(), Equals, "Unknown targeting option 'foo'")
tgt, err = parseTargets("@ continent country asn")
c.Assert(err, IsNil)
str = tgt.String()
c.Check(str, Equals, "@ continent country asn")
}
func (s *TargetingSuite) TestGetTargets(c *C) {
ip := net.ParseIP("207.171.1.1")
geoIP.setupGeoIPCity()
geoIP.setupGeoIPCountry()
geoIP.setupGeoIPASN()
tgt, _ := parseTargets("@ continent country")
targets, _ := tgt.GetTargets(ip)
c.Check(targets, DeepEquals, []string{"us", "north-america", "@"})
if geoIP.city == nil {
c.Log("City GeoIP database requred for these tests")
return
}
tgt, _ = parseTargets("@ continent country region ")
targets, _ = tgt.GetTargets(ip)
c.Check(targets, DeepEquals, []string{"us-ca", "us", "north-america", "@"})
tgt, _ = parseTargets("@ continent regiongroup country region ")
targets, _ = tgt.GetTargets(ip)
c.Check(targets, DeepEquals, []string{"us-ca", "us-west", "us", "north-america", "@"})
tgt, _ = parseTargets("@ continent regiongroup country region asn ip")
targets, _ = tgt.GetTargets(ip)
c.Check(targets, DeepEquals, []string{"[207.171.1.1]", "[207.171.1.0]", "as7012", "us-ca", "us-west", "us", "north-america", "@"})
ip = net.ParseIP("2607:f238:2:0::ff:4")
tgt, _ = parseTargets("ip")
targets, _ = tgt.GetTargets(ip)
c.Check(targets, DeepEquals, []string{"[2607:f238:2::ff:4]", "[2607:f238:2::]"})
}