Moar parsers
This commit is contained in:
63
dataset/parser/list_test.go
Normal file
63
dataset/parser/list_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseDomains(t *testing.T) {
|
||||
test := `# This is a comment
|
||||
facebook.com
|
||||
tiktok.com
|
||||
bogus ignored
|
||||
youtube.com`
|
||||
want := []string{"facebook.com", "tiktok.com", "youtube.com"}
|
||||
|
||||
parsed, ignored, err := ParseDomains(strings.NewReader(test))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
sort.Strings(parsed)
|
||||
if !reflect.DeepEqual(parsed, want) {
|
||||
t.Errorf("expected ParseDomains(domains) to return %v, got %v", want, parsed)
|
||||
}
|
||||
if ignored != 1 {
|
||||
t.Errorf("expected 1 ignored, got %d", ignored)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseNetworks(t *testing.T) {
|
||||
test := `# This is a comment
|
||||
127.0.0.1
|
||||
10.42.66.0/24
|
||||
bogus ignored
|
||||
::ffff:abcd:ef
|
||||
fe80:0:0::0/8`
|
||||
want := []netip.Prefix{
|
||||
netip.MustParsePrefix("10.42.66.0/24"),
|
||||
netip.MustParsePrefix("127.0.0.1/32"),
|
||||
netip.MustParsePrefix("171.205.0.239/32"),
|
||||
netip.MustParsePrefix("fe80::/8"),
|
||||
}
|
||||
|
||||
parsed, ignored, err := ParseNetworks(strings.NewReader(test))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
sort.SliceStable(parsed, func(i, j int) bool {
|
||||
return parsed[i].Addr().Less(parsed[j].Addr())
|
||||
})
|
||||
if !reflect.DeepEqual(parsed, want) {
|
||||
t.Errorf("expected ParseNetworks(prefixes) to return %v, got %v", want, parsed)
|
||||
}
|
||||
if ignored != 1 {
|
||||
t.Errorf("expected 1 ignored, got %d", ignored)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user