forked from mostafa-asg/ip2country
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip2country_test.go
170 lines (145 loc) · 3.76 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package ip2country_test
import (
"fmt"
"ip2country"
"math/big"
"math/rand"
"testing"
)
func BenchmarkGetCountry(b *testing.B) {
ips, err := ip2country.Load("./dbip-country.csv")
if err != nil {
b.Fatal(err)
}
for i := 1; i <= b.N; i++ {
ips.GetCountry(createRandomIP())
}
}
func createRandomIP() 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) {
ips, err := ip2country.Load("./dbip-country.csv")
if err != nil {
t.Fatal(err)
}
country := ips.GetCountry("0.0.0.0")
if country != "ZZ" {
t.Errorf("Expected ZZ but found %s", country)
}
country = ips.GetCountry("0.0.0.1")
if country != "ZZ" {
t.Errorf("Expected ZZ but found %s", country)
}
country = ips.GetCountry("0.255.255.255")
if country != "ZZ" {
t.Errorf("Expected ZZ but found %s", country)
}
country = ips.GetCountry("50.97.196.208")
if country != "US" {
t.Errorf("Expected US but found %s", country)
}
country = ips.GetCountry("50.97.198.135")
if country != "CN" {
t.Errorf("Expected US but found %s", country)
}
country = ips.GetCountry("50.97.198.135")
if country != "CN" {
t.Errorf("Expected US but found %s", country)
}
country = ips.GetCountry("200.95.185.34")
if country != "CL" {
t.Errorf("Expected CL but found %s", country)
}
country = ips.GetCountry("223.255.255.255")
if country != "AU" {
t.Errorf("Expected AU but found %s", country)
}
}
func TestGetCountryMulti(t *testing.T) {
ips, err := ip2country.Load("./dbip-country.csv")
if err != nil {
t.Fatal(err)
}
countries := ips.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 TestIP4AddressToInt(t *testing.T) {
ipNumb, err := ip2country.Ip4ToInt("0.0.0.255")
if err != nil {
t.Error(err)
}
if ipNumb != 255 {
t.Errorf("Expected 255 but found %d", ipNumb)
}
//-----------------------------------------------
ipNumb, err = ip2country.Ip4ToInt("0.0.1.255")
if err != nil {
t.Error(err)
}
if ipNumb != 511 {
t.Errorf("Expected 511 but found %d", ipNumb)
}
//-----------------------------------------------
ipNumb, err = ip2country.Ip4ToInt("255.0.0.0")
if err != nil {
t.Error(err)
}
if ipNumb != 4278190080 {
t.Errorf("Expected 4278190080 but found %d", ipNumb)
}
//-----------------------------------------------
ipNumb, err = ip2country.Ip4ToInt("255.255.255.255")
if err != nil {
t.Error(err)
}
if ipNumb != 4294967295 {
t.Errorf("Expected 4294967295 but found %d", ipNumb)
}
}
func TestIP6AddressToInt(t *testing.T) {
b := big.NewInt(0)
ip2country.Ip6ToInt("fec0::", b)
t.Logf("result: %v", b)
b = big.NewInt(0)
ip2country.Ip6ToInt("::0000", b)
t.Logf("result: %v", b)
b = big.NewInt(0)
ip2country.Ip6ToInt("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", b)
t.Logf("result: %v", b)
b = big.NewInt(0)
ip2country.Ip6ToInt("0000:0000:0000:0000:0000:0000:0000:00ff", b)
t.Logf("result: %v", b)
}
func TestIP4and6(t *testing.T) {
ips, err := ip2country.Load("./dbip-country-test.csv")
if err != nil {
t.Fatalf("got err: %v", err)
}
v4orv6 := ips.GetCountry("1.1.1.1")
if v4orv6 != "V4" {
t.Fatalf("expected V4, got %v", v4orv6)
}
v4orv6 = ips.GetCountry("0fff:1fff:2fff:3fff:4fff:5fff:ffff:ffff")
if v4orv6 != "V6" {
t.Fatalf("expected V4, got %v", v4orv6)
}
}