-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_test.go
93 lines (78 loc) · 1.87 KB
/
update_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
package geoip
import (
"encoding/hex"
"net"
"os"
"testing"
"github.com/IncSW/geoip2"
)
// GeoIP tests
func TestGetSHA256(t *testing.T) {
// Get the license key through environment variables
licenseKey := os.Getenv("MAXMIND_LICENSE_KEY")
if licenseKey == "" {
t.Error("MAXMIND_LICENSE_KEY environment variable not set")
return
}
// Download the sha256 checksum
sha256, err := downloadHash(licenseKey)
if err != nil {
t.Error(err)
}
// the checksum should be a hex string that is 64 characters long
if len(sha256) != 64 {
t.Error("sha256 checksum is not 64 characters long")
}
// decode the sha256 checksum
_, err = hex.DecodeString(sha256)
if err != nil {
t.Error(err)
}
}
// Download a new database
func TestDownloadDatabase(t *testing.T) {
// Get the license key through environment variables
licenseKey := os.Getenv("MAXMIND_LICENSE_KEY")
if licenseKey == "" {
t.Error("MAXMIND_LICENSE_KEY environment variable not set")
return
}
// Prepare the checksum
sha256, err := downloadHash(licenseKey)
if err != nil {
t.Error(err)
}
// Download the database
bytes, err := downloadDatabase(licenseKey, sha256)
if err != nil {
t.Error(err)
}
// Verify that the database can be opened
_, err = geoip2.NewCityReader(bytes)
if err != nil {
t.Error(err)
}
}
func TestLookups(t *testing.T) {
// Get the license key through environment variables
licenseKey := os.Getenv("MAXMIND_LICENSE_KEY")
if licenseKey == "" {
t.Error("MAXMIND_LICENSE_KEY environment variable not set")
return
}
geoip, err := NewGeoIPHandler(licenseKey)
if err != nil {
t.Error(err)
return
}
// Lookup some IP addresses
ips := []string{"128.153.145.19", "2605:6480:c051:100::1"}
for _, ip := range ips {
_, err := geoip.Lookup(net.ParseIP(ip))
if err != nil {
t.Error(ip, err)
}
}
// TODO: Add a test that ensures maxmind knows where mirror is
geoip.Close()
}