-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv4range_maplist_test.go
40 lines (33 loc) · 1.03 KB
/
ipv4range_maplist_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
package ipsearch_test
import (
"github.com/guuzaa/ipsearch"
"github.com/stretchr/testify/assert"
"testing"
)
func TestIPv4CIDRMapList(t *testing.T) {
ipCIDRMapList := ipsearch.NewIPv4RangeMapList()
ipRangeList := ipsearch.NewIPv4RangeList(cidrs)
ipCIDRMapList.AppendBatch(ipRangeList)
// without sorting, it would be a bug
ip := ipCIDRMapList.Search("1.4.1.2")
assert.Nil(t, ip)
// with sorting, it would be ok
ipCIDRMapList.Sort()
ip = ipCIDRMapList.Search("1.4.1.3")
assert.NotNil(t, ip)
assert.Equal(t, ip.String(), "1.4.1.0/24")
ipCIDRMapList = ipsearch.NewIPv4RangeMapList()
ipRangeList = ipsearch.NewIPv4RangeList(cidrs)
ipCIDRMapList.InsertSortedCIDRs(ipRangeList)
for _, data := range testCIDRDataList {
ip := ipCIDRMapList.Search(data.ip)
assert.Equal(t, ip != nil, data.find)
if ip != nil {
assert.Equal(t, ip.String(), data.cidr)
}
}
ipCIDRMapList.InsertSorted(ipsearch.NewIPv4Range("1.3.0.0/16"))
ip = ipCIDRMapList.Search("1.3.1.1")
assert.NotNil(t, ip)
assert.Equal(t, ip.String(), "1.3.0.0/16")
}