2014-11-29 02:04:15 +08:00
|
|
|
package iplist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2015-09-23 16:25:22 +08:00
|
|
|
"bytes"
|
2015-03-08 14:43:44 +08:00
|
|
|
"fmt"
|
2014-11-29 02:04:15 +08:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-03-08 14:43:44 +08:00
|
|
|
|
2015-08-03 22:29:01 +08:00
|
|
|
"github.com/anacrolix/missinggo"
|
2015-03-08 14:43:44 +08:00
|
|
|
"github.com/bradfitz/iter"
|
2015-09-23 16:25:22 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2014-11-29 02:04:15 +08:00
|
|
|
)
|
|
|
|
|
2015-09-23 16:25:22 +08:00
|
|
|
var (
|
|
|
|
// Note the shared description "eff". The overlapping ranges at 1.2.8.2
|
|
|
|
// will cause problems. Don't overlap your ranges.
|
|
|
|
sample = `
|
2014-11-29 02:04:15 +08:00
|
|
|
# List distributed by iblocklist.com
|
|
|
|
|
|
|
|
a:1.2.4.0-1.2.4.255
|
2015-03-08 14:43:44 +08:00
|
|
|
b:1.2.8.0-1.2.8.255
|
2015-09-23 16:25:22 +08:00
|
|
|
eff:1.2.8.2-1.2.8.2
|
|
|
|
something:more detail:86.59.95.195-86.59.95.195
|
|
|
|
eff:127.0.0.0-127.0.0.1`
|
|
|
|
packedSample []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
list, err := NewFromReader(strings.NewReader(sample))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = list.WritePacked(&buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
packedSample = buf.Bytes()
|
|
|
|
}
|
2015-03-08 14:43:44 +08:00
|
|
|
|
|
|
|
func TestIPv4RangeLen(t *testing.T) {
|
|
|
|
ranges, _ := sampleRanges(t)
|
|
|
|
for i := range iter.N(3) {
|
|
|
|
if len(ranges[i].First) != 4 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if len(ranges[i].Last) != 4 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-29 02:04:15 +08:00
|
|
|
|
2014-12-03 04:54:37 +08:00
|
|
|
func sampleRanges(tb testing.TB) (ranges []Range, err error) {
|
2014-11-29 02:04:15 +08:00
|
|
|
scanner := bufio.NewScanner(strings.NewReader(sample))
|
|
|
|
for scanner.Scan() {
|
2015-03-08 14:43:44 +08:00
|
|
|
r, ok, err := ParseBlocklistP2PLine(scanner.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
2014-11-29 02:04:15 +08:00
|
|
|
if ok {
|
|
|
|
ranges = append(ranges, r)
|
|
|
|
}
|
|
|
|
}
|
2014-12-03 04:54:37 +08:00
|
|
|
err = scanner.Err()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkParseP2pBlocklist(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
sampleRanges(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-08 14:43:44 +08:00
|
|
|
func connRemoteAddrIP(network, laddr string, dialHost string) net.IP {
|
|
|
|
l, err := net.Listen(network, laddr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
go func() {
|
2015-08-03 22:29:01 +08:00
|
|
|
c, err := net.Dial(network, net.JoinHostPort(dialHost, fmt.Sprintf("%d", missinggo.AddrPort(l.Addr()))))
|
2015-03-08 14:43:44 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
}()
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
2015-08-03 22:29:01 +08:00
|
|
|
ret := missinggo.AddrIP(c.RemoteAddr())
|
2015-03-08 14:43:44 +08:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-10-18 21:00:26 +08:00
|
|
|
func lookupOk(r Range, ok bool) bool {
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2015-03-27 23:54:17 +08:00
|
|
|
func TestBadIP(t *testing.T) {
|
2015-09-23 16:25:22 +08:00
|
|
|
for _, iplist := range []Ranger{
|
|
|
|
New(nil),
|
|
|
|
NewFromPacked([]byte("\x00\x00\x00\x00\x00\x00\x00\x00")),
|
|
|
|
} {
|
2015-10-18 21:00:26 +08:00
|
|
|
assert.False(t, lookupOk(iplist.Lookup(net.IP(make([]byte, 4)))), "%v", iplist)
|
|
|
|
assert.False(t, lookupOk(iplist.Lookup(net.IP(make([]byte, 16)))))
|
|
|
|
r, ok := iplist.Lookup(nil)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, r.Description, "bad IP")
|
|
|
|
assert.True(t, lookupOk(iplist.Lookup(net.IP(make([]byte, 5)))))
|
2015-03-27 23:54:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:25:22 +08:00
|
|
|
func testLookuperSimple(t *testing.T, iplist Ranger) {
|
2014-11-29 02:04:15 +08:00
|
|
|
for _, _case := range []struct {
|
|
|
|
IP string
|
|
|
|
Hit bool
|
|
|
|
Desc string
|
|
|
|
}{
|
|
|
|
{"1.2.3.255", false, ""},
|
|
|
|
{"1.2.8.0", true, "b"},
|
|
|
|
{"1.2.4.255", true, "a"},
|
2015-03-27 23:54:17 +08:00
|
|
|
// Try to roll over to the next octet on the parse. Note the final
|
|
|
|
// octet is overbounds. In the next case.
|
2015-09-23 16:25:22 +08:00
|
|
|
{"1.2.7.256", true, "bad IP"},
|
|
|
|
{"1.2.8.1", true, "b"},
|
|
|
|
{"1.2.8.2", true, "eff"},
|
2014-11-29 02:04:15 +08:00
|
|
|
} {
|
2015-03-27 23:54:17 +08:00
|
|
|
ip := net.ParseIP(_case.IP)
|
2015-10-18 21:00:26 +08:00
|
|
|
r, ok := iplist.Lookup(ip)
|
|
|
|
assert.Equal(t, _case.Hit, ok, "%s", _case)
|
2014-11-29 02:04:15 +08:00
|
|
|
if !_case.Hit {
|
|
|
|
continue
|
|
|
|
}
|
2015-09-23 16:25:22 +08:00
|
|
|
assert.Equal(t, _case.Desc, r.Description, "%T", iplist)
|
2014-11-29 02:04:15 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 16:25:22 +08:00
|
|
|
|
|
|
|
func TestSimple(t *testing.T) {
|
|
|
|
ranges, err := sampleRanges(t)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, ranges, 5)
|
|
|
|
iplist := New(ranges)
|
|
|
|
testLookuperSimple(t, iplist)
|
|
|
|
packed := NewFromPacked(packedSample)
|
|
|
|
testLookuperSimple(t, packed)
|
|
|
|
}
|