2014-11-29 02:04:15 +08:00
|
|
|
package iplist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
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
|
|
|
|
|
|
|
"github.com/bradfitz/iter"
|
|
|
|
|
2015-03-20 13:37:44 +08:00
|
|
|
"github.com/anacrolix/torrent/util"
|
2014-11-29 02:04:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var sample = `
|
|
|
|
# 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
|
|
|
|
something:more detail:86.59.95.195-86.59.95.195`
|
|
|
|
|
|
|
|
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() {
|
|
|
|
c, err := net.Dial(network, net.JoinHostPort(dialHost, fmt.Sprintf("%d", util.AddrPort(l.Addr()))))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
}()
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
ret := util.AddrIP(c.RemoteAddr())
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2014-12-03 04:54:37 +08:00
|
|
|
func TestSimple(t *testing.T) {
|
|
|
|
ranges, err := sampleRanges(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2014-11-29 02:04:15 +08:00
|
|
|
}
|
2015-03-08 14:43:44 +08:00
|
|
|
if len(ranges) != 3 {
|
|
|
|
t.Fatalf("expected 3 ranges but got %d", len(ranges))
|
2014-11-29 02:04:15 +08:00
|
|
|
}
|
|
|
|
iplist := New(ranges)
|
|
|
|
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"},
|
|
|
|
// Try to roll over to the next octet on the parse.
|
|
|
|
{"1.2.7.256", false, ""},
|
|
|
|
{"1.2.8.254", true, "b"},
|
|
|
|
} {
|
|
|
|
r := iplist.Lookup(net.ParseIP(_case.IP))
|
|
|
|
if !_case.Hit {
|
|
|
|
if r != nil {
|
|
|
|
t.Fatalf("got hit when none was expected")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if r == nil {
|
|
|
|
t.Fatalf("expected hit for %q", _case.IP)
|
|
|
|
}
|
|
|
|
if r.Description != _case.Desc {
|
|
|
|
t.Fatalf("%q != %q", r.Description, _case.Desc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|