forked from abh/geoip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
geoip_test.go
108 lines (85 loc) · 2.25 KB
/
geoip_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package geoip
import (
"fmt"
. "launchpad.net/gocheck"
"testing"
)
// Hook up gocheck into the gotest runner.
func Test(t *testing.T) { TestingT(t) }
type GeoIPSuite struct {
}
var _ = Suite(&GeoIPSuite{})
func (s *GeoIPSuite) Testv4(c *C) {
gi, err := Open()
if gi == nil || err != nil {
fmt.Printf("Could not open GeoIP database: %s\n", err)
return
}
c.Check(gi, NotNil)
country, netmask := gi.GetCountry("207.171.7.51")
c.Check(country, Equals, "US")
c.Check(netmask, Equals, 15)
country, netmask = gi.GetCountry("149.20.64.42")
c.Check(country, Equals, "US")
c.Check(netmask, Equals, 13)
}
func (s *GeoIPSuite) TestOpenType(c *C) {
// SetCustomDirectory("/Users/ask/go/src/geoip/db")
// Open Country database
gi, err := OpenType(GEOIP_COUNTRY_EDITION)
c.Check(err, IsNil)
c.Assert(gi, NotNil)
country, _ := gi.GetCountry("207.171.7.51")
c.Check(country, Equals, "US")
}
func (s *GeoIPSuite) Benchmark_GetCountry(c *C) {
gi, err := Open()
if gi == nil || err != nil {
fmt.Printf("Could not open GeoIP database: %s\n", err)
return
}
for i := 0; i < c.N; i++ {
gi.GetCountry("207.171.7.51")
}
}
func (s *GeoIPSuite) Testv4Record(c *C) {
gi, err := Open("db/GeoLiteCity.dat")
if gi == nil || err != nil {
fmt.Printf("Could not open GeoIP database: %s\n", err)
return
}
c.Check(gi, NotNil)
record := gi.GetRecord("207.171.7.51")
c.Assert(record, NotNil)
c.Check(record.CountryCode, Equals, "US")
fmt.Printf("Record: %#v\n", record)
}
func (s *GeoIPSuite) Benchmark_GetRecord(c *C) {
gi, err := Open("db/GeoIPCity.dat")
if gi == nil || err != nil {
fmt.Printf("Could not open GeoIP database: %s\n", err)
return
}
for i := 0; i < c.N; i++ {
record := gi.GetRecord("207.171.7.51")
if record == nil {
panic("")
}
}
}
func (s *GeoIPSuite) Testv4Region(c *C) {
gi, err := Open("db/GeoIPRegion.dat")
if gi == nil || err != nil {
fmt.Printf("Could not open GeoIP database: %s\n", err)
return
}
country, region := gi.GetRegion("207.171.7.51")
c.Check(country, Equals, "US")
c.Check(region, Equals, "CA")
}
func (s *GeoIPSuite) TestRegionName(c *C) {
regionName := GetRegionName("NL", "07")
c.Check(regionName, Equals, "Noord-Holland")
regionName = GetRegionName("CA", "ON")
c.Check(regionName, Equals, "Ontario")
}