-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip2country_test.go
134 lines (115 loc) · 2.81 KB
/
ip2country_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package ip2country
import (
"fmt"
"math/rand"
"testing"
)
func BenchmarkGetCountry(b *testing.B) {
err := Load("./dbip-country.csv")
if err != nil {
b.Fatal(err)
}
for i := 1; i <= b.N; i++ {
GetCountry(createRandomeIP())
}
}
func createRandomeIP() string {
p1 := rand.Int31n(256)
p2 := rand.Int31n(256)
p3 := rand.Int31n(256)
p4 := rand.Int31n(256)
return fmt.Sprintf("%d.%d.%d.%d", p1, p2, p3, p4)
}
func TestIPtoCountryLookup(t *testing.T) {
err := Load("./dbip-country.csv")
if err != nil {
t.Fatal(err)
}
country := GetCountry("0.0.0.0")
if country != "ZZ" {
t.Errorf("Expected ZZ but found %s", country)
}
country = GetCountry("0.0.0.1")
if country != "ZZ" {
t.Errorf("Expected ZZ but found %s", country)
}
country = GetCountry("0.255.255.255")
if country != "ZZ" {
t.Errorf("Expected ZZ but found %s", country)
}
country = GetCountry("50.97.196.208")
if country != "US" {
t.Errorf("Expected US but found %s", country)
}
country = GetCountry("50.97.198.135")
if country != "CN" {
t.Errorf("Expected US but found %s", country)
}
country = GetCountry("50.97.198.135")
if country != "CN" {
t.Errorf("Expected US but found %s", country)
}
country = GetCountry("200.95.185.34")
if country != "CL" {
t.Errorf("Expected CL but found %s", country)
}
country = GetCountry("223.255.255.255")
if country != "AU" {
t.Errorf("Expected AU but found %s", country)
}
}
func TestGetCountryMulti(t *testing.T) {
err := Load("./dbip-country.csv")
if err != nil {
t.Fatal(err)
}
countries := GetCountryMulti("35.185.131.112", "35.185.133.191", "64.215.100.142", "94.46.48.46", "159.8.131.119")
if countries[0] != "US" {
t.Errorf("Expected US but found %s", countries[0])
}
if countries[1] != "TW" {
t.Errorf("Expected TW but found %s", countries[1])
}
if countries[2] != "BR" {
t.Errorf("Expected BR but found %s", countries[2])
}
if countries[3] != "GB" {
t.Errorf("Expected GB but found %s", countries[3])
}
if countries[4] != "NL" {
t.Errorf("Expected NL but found %s", countries[4])
}
}
func TestIPAddressToInt(t *testing.T) {
ipNumb, err := ipToInt("0.0.0.255")
if err != nil {
t.Error(err)
}
if ipNumb != 255 {
t.Errorf("Expected 255 but found %d", ipNumb)
}
//-----------------------------------------------
ipNumb, err = ipToInt("0.0.1.255")
if err != nil {
t.Error(err)
}
if ipNumb != 511 {
t.Errorf("Expected 511 but found %d", ipNumb)
}
//-----------------------------------------------
ipNumb, err = ipToInt("255.0.0.0")
if err != nil {
t.Error(err)
}
if ipNumb != 4278190080 {
t.Errorf("Expected 4278190080 but found %d", ipNumb)
}
//-----------------------------------------------
ipNumb, err = ipToInt("255.255.255.255")
if err != nil {
t.Error(err)
}
if ipNumb != 4294967295 {
t.Errorf("Expected 4294967295 but found %d", ipNumb)
}
}