-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipsearch_test.go
136 lines (114 loc) · 3.09 KB
/
ipsearch_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
package ipsearch_test
import (
"github.com/guuzaa/ipsearch"
"github.com/stretchr/testify/assert"
"net/http"
"strings"
"testing"
"time"
)
const (
TestDir = "data/"
IPv4CIDRFile = "data/ipv4/cn.cidr"
IPv6CIDRFile = "data/ipv6/kg.cidr"
)
func TestLoadFromFile(t *testing.T) {
search, err := ipsearch.NewIPv4SearchWithFile("not-exist-file")
assert.NotNil(t, err)
search, err = ipsearch.NewIPv4SearchWithFile(IPv4CIDRFile)
assert.Nil(t, err)
testCIDRSearch(t, search)
searchV6, err := ipsearch.NewIPv6SearchWithFile("not-exist-file")
assert.NotNil(t, err)
searchV6, err = ipsearch.NewIPv6SearchWithFile(IPv6CIDRFile)
assert.Nil(t, err)
testV6CIDRSearch(t, searchV6)
}
func TestLoadWithCountry(t *testing.T) {
// IPv4
search, err := ipsearch.NewIPv4SearchWithCountry("cn")
assert.Nil(t, err)
testCIDRSearch(t, search)
search, err = ipsearch.NewIPv4SearchWithCountry("CN")
assert.Nil(t, err)
testCIDRSearch(t, search)
search, err = ipsearch.NewIPv4SearchWithCountry("not-exist")
assert.NotNil(t, err)
// IPv6
searchV6, err := ipsearch.NewIPv6SearchWithCountry("kg")
assert.Nil(t, err)
testV6CIDRSearch(t, searchV6)
searchV6, err = ipsearch.NewIPv6SearchWithCountry("KG")
assert.Nil(t, err)
testV6CIDRSearch(t, searchV6)
searchV6, err = ipsearch.NewIPv6SearchWithCountry("not-exist")
assert.NotNil(t, err)
}
func TestLoadFromURL(t *testing.T) {
endpoint := "127.0.0.1:9898"
protocol := "http://" + endpoint + "/"
search, err := ipsearch.NewIPv4SearchWithFileFromURL(protocol + "not-exist-file")
assert.NotNil(t, err)
// start a http server
go func() {
http.ListenAndServe(endpoint, http.FileServer(http.Dir(TestDir)))
}()
for {
search, err = ipsearch.NewIPv4SearchWithFileFromURL(protocol + "ipv4/cn.cidr")
if err != nil && strings.Contains(err.Error(), "connection refused") {
time.Sleep(100 * time.Millisecond)
continue
}
assert.Nil(t, err)
testCIDRSearch(t, search)
break
}
for {
searchV6, err := ipsearch.NewIPv6SearchWithFileFromURL(protocol + "ipv6/kg.cidr")
if err != nil && strings.Contains(err.Error(), "connection refused") {
time.Sleep(100 * time.Millisecond)
continue
}
assert.Nil(t, err)
testV6CIDRSearch(t, searchV6)
break
}
}
func testCIDRSearch(t *testing.T, search *ipsearch.IPv4Search) {
type testCIDRData struct {
ip string
cidr string
find bool
}
var testCIDRDataList = []testCIDRData{
{"1.0.1.24", "1.0.1.0/24", true},
{"8.8.8.8", "", false},
{"1.1.1.1", "", false},
}
for _, data := range testCIDRDataList {
ip := search.Search(data.ip)
assert.Equal(t, ip != nil, data.find)
if ip != nil {
assert.Equal(t, ip.CIDR(), data.cidr)
}
}
}
func testV6CIDRSearch(t *testing.T, search *ipsearch.IPv6Search) {
type testCIDRData struct {
ip string
cidr string
find bool
}
var testCIDRDataList = []testCIDRData{
{"2a00:47a0::44", "2a00:47a0::/32", true},
{"2001:da8::33", "", false},
{"2001:da8:3::1", "", false},
}
for _, data := range testCIDRDataList {
ip := search.Search(data.ip)
assert.Equal(t, ip != nil, data.find)
if ip != nil {
assert.Equal(t, ip.CIDR(), data.cidr)
}
}
}