-
Notifications
You must be signed in to change notification settings - Fork 22
/
iploc_test.go
119 lines (110 loc) · 2.65 KB
/
iploc_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
package iploc
import (
"net"
"net/netip"
"testing"
)
func TestCountry(t *testing.T) {
cases := []struct {
IP string
Country string
}{
{"", ""},
{"0.0.0.0", "ZZ"},
{"0.1.1.1", "ZZ"},
// {"1.1.1.1", "AU"},
{"121.229.143.64", "CN"},
{"122.96.43.186", "CN"},
{"153.3.123.160", "CN"},
{"153.3.131.201", "CN"},
{"180.109.81.198", "CN"},
{"180.111.103.88", "CN"},
{"183.206.11.225", "CN"},
{"192.210.171.249", "US"},
{"223.112.9.2", "CN"},
{"23.16.28.232", "CA"},
{"58.240.115.210", "CN"},
{"61.155.4.66", "CN"},
{"255.255.255.255", "ZZ"},
// {"2001:4860:4860::8888", "US"},
{"2001::6ca0:a535", "ZZ"},
{"2001:dc7:1000::1", "CN"},
{"2400:3200::1", "CN"},
{"2400:da00::6666", "CN"},
{"2404:6800:4008:801::2004", "TW"},
{"2404:6800:4012:1::200e", "AU"},
{"240C::6666", "CN"},
{"240e:4c:4008::1", "CN"},
{"240e:e8:f089:4877:70d2:775c:91d1:ab12", "CN"},
{"2620:0:2d0:200::7", "US"},
{"2a04:4e42:600::223", "NL"},
{"::", "ZZ"},
{"::1", "ZZ"},
}
for _, c := range cases {
country := Country(net.ParseIP(c.IP))
if country != c.Country {
t.Errorf("Country(%#v) return \"%s\", expect %#v", c.IP, country, c.Country)
}
}
}
func TestIPCountry(t *testing.T) {
cases := []struct {
IP string
Country string
}{
{"0.0.0.0", "ZZ"},
{"0.1.1.1", "ZZ"},
// {"1.1.1.1", "AU"},
{"121.229.143.64", "CN"},
{"122.96.43.186", "CN"},
{"153.3.123.160", "CN"},
{"153.3.131.201", "CN"},
{"180.109.81.198", "CN"},
{"180.111.103.88", "CN"},
{"183.206.11.225", "CN"},
{"192.210.171.249", "US"},
{"223.112.9.2", "CN"},
{"23.16.28.232", "CA"},
{"58.240.115.210", "CN"},
{"61.155.4.66", "CN"},
{"255.255.255.255", "ZZ"},
// {"2001:4860:4860::8888", "US"},
{"2001::6ca0:a535", "ZZ"},
{"2001:dc7:1000::1", "CN"},
{"2400:3200::1", "CN"},
{"2400:da00::6666", "CN"},
{"2404:6800:4008:801::2004", "TW"},
{"2404:6800:4012:1::200e", "AU"},
{"240C::6666", "CN"},
{"240e:4c:4008::1", "CN"},
{"240e:e8:f089:4877:70d2:775c:91d1:ab12", "CN"},
{"2620:0:2d0:200::7", "US"},
{"2a04:4e42:600::223", "NL"},
{"::", "ZZ"},
{"::1", "ZZ"},
}
for _, c := range cases {
country := IPCountry(netip.MustParseAddr(c.IP))
// println(c.IP, country)
if country != c.Country {
t.Errorf("Country(%#v) return \"%s\", expect %#v", c.IP, country, c.Country)
}
}
}
func BenchmarkIPCountryForIPv4(b *testing.B) {
ip := netip.MustParseAddr("9.8.8.8")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
IPCountry(ip)
}
}
func BenchmarkIPCountryForIPv6(b *testing.B) {
ip := netip.MustParseAddr("2001:4860:4860::8888")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
IPCountry(ip)
}
}