64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package parser
|
|
|
|
import (
|
|
"net/netip"
|
|
"reflect"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const testHosts = `##
|
|
# Host Database
|
|
#
|
|
# localhost is used to configure the loopback interface
|
|
# when the system is booting. Do not change this entry.
|
|
##
|
|
127.0.0.1 localhost dragon dragon.local dragon.maze.network
|
|
255.255.255.255 broadcasthost
|
|
::1 localhost
|
|
ff00::1 multicast
|
|
1.2.3.4
|
|
`
|
|
|
|
func TestParseHosts(t *testing.T) {
|
|
parsed, ignored, err := ParseDomains(strings.NewReader(testHosts))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
return
|
|
}
|
|
|
|
sort.Strings(parsed)
|
|
want := []string{"broadcasthost", "dragon", "dragon.local", "dragon.maze.network", "localhost", "multicast"}
|
|
if !reflect.DeepEqual(parsed, want) {
|
|
t.Errorf("expected ParseDomains(hosts) to return %v, got %v", want, parsed)
|
|
}
|
|
if ignored != 1 {
|
|
t.Errorf("expected 1 ignored, got %d", ignored)
|
|
}
|
|
}
|
|
|
|
func TestParseHostsNetworks(t *testing.T) {
|
|
parsed, ignored, err := ParseNetworks(strings.NewReader(testHosts))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
return
|
|
}
|
|
|
|
sort.SliceStable(parsed, func(i, j int) bool {
|
|
return parsed[i].Addr().Less(parsed[j].Addr())
|
|
})
|
|
want := []netip.Prefix{
|
|
netip.MustParsePrefix("127.0.0.1/32"),
|
|
netip.MustParsePrefix("255.255.255.255/32"),
|
|
netip.MustParsePrefix("::1/128"),
|
|
netip.MustParsePrefix("ff00::1/128"),
|
|
}
|
|
if !reflect.DeepEqual(parsed, want) {
|
|
t.Errorf("expected ParseNetworks(hosts) to return %v, got %v", want, parsed)
|
|
}
|
|
if ignored != 1 {
|
|
t.Errorf("expected 1 ignored, got %d", ignored)
|
|
}
|
|
}
|