2021-10-14 16:39:21 +00:00
|
|
|
package aghnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"net"
|
2021-11-23 15:01:48 +00:00
|
|
|
"os"
|
2021-10-14 16:39:21 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"testing/fstest"
|
2022-01-12 09:14:59 +00:00
|
|
|
"time"
|
2021-10-14 16:39:21 +00:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2021-11-16 13:16:38 +00:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2021-10-14 16:39:21 +00:00
|
|
|
"github.com/AdguardTeam/urlfilter"
|
2021-11-23 15:01:48 +00:00
|
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
2021-10-14 16:39:21 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
nl = "\n"
|
|
|
|
sp = " "
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewHostsContainer(t *testing.T) {
|
|
|
|
const dirname = "dir"
|
|
|
|
const filename = "file1"
|
|
|
|
|
|
|
|
p := path.Join(dirname, filename)
|
|
|
|
|
|
|
|
testFS := fstest.MapFS{
|
|
|
|
p: &fstest.MapFile{Data: []byte("127.0.0.1 localhost")},
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2021-11-17 14:21:10 +00:00
|
|
|
wantErr error
|
|
|
|
name string
|
|
|
|
paths []string
|
2021-10-14 16:39:21 +00:00
|
|
|
}{{
|
2021-11-17 14:21:10 +00:00
|
|
|
wantErr: nil,
|
|
|
|
name: "one_file",
|
|
|
|
paths: []string{p},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
2021-11-17 14:21:10 +00:00
|
|
|
wantErr: ErrNoHostsPaths,
|
|
|
|
name: "no_files",
|
|
|
|
paths: []string{},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
2021-11-17 14:21:10 +00:00
|
|
|
wantErr: ErrNoHostsPaths,
|
|
|
|
name: "non-existent_file",
|
|
|
|
paths: []string{path.Join(dirname, filename+"2")},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
2021-11-17 14:21:10 +00:00
|
|
|
wantErr: nil,
|
|
|
|
name: "whole_dir",
|
|
|
|
paths: []string{dirname},
|
2021-10-14 16:39:21 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
onAdd := func(name string) (err error) {
|
|
|
|
assert.Contains(t, tc.paths, name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var eventsCalledCounter uint32
|
|
|
|
eventsCh := make(chan struct{})
|
|
|
|
onEvents := func() (e <-chan struct{}) {
|
|
|
|
assert.Equal(t, uint32(1), atomic.AddUint32(&eventsCalledCounter, 1))
|
|
|
|
|
|
|
|
return eventsCh
|
|
|
|
}
|
|
|
|
|
2021-11-26 15:25:43 +00:00
|
|
|
hc, err := NewHostsContainer(0, testFS, &aghtest.FSWatcher{
|
2021-10-14 16:39:21 +00:00
|
|
|
OnEvents: onEvents,
|
|
|
|
OnAdd: onAdd,
|
2021-11-17 14:21:10 +00:00
|
|
|
OnClose: func() (err error) { panic("not implemented") },
|
2021-10-14 16:39:21 +00:00
|
|
|
}, tc.paths...)
|
|
|
|
if tc.wantErr != nil {
|
|
|
|
require.ErrorIs(t, err, tc.wantErr)
|
|
|
|
|
|
|
|
assert.Nil(t, hc)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, hc)
|
|
|
|
|
|
|
|
assert.NotNil(t, <-hc.Upd())
|
|
|
|
|
|
|
|
eventsCh <- struct{}{}
|
|
|
|
assert.Equal(t, uint32(1), atomic.LoadUint32(&eventsCalledCounter))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("nil_fs", func(t *testing.T) {
|
|
|
|
require.Panics(t, func() {
|
2021-11-26 15:25:43 +00:00
|
|
|
_, _ = NewHostsContainer(0, nil, &aghtest.FSWatcher{
|
2021-10-14 16:39:21 +00:00
|
|
|
// Those shouldn't panic.
|
|
|
|
OnEvents: func() (e <-chan struct{}) { return nil },
|
|
|
|
OnAdd: func(name string) (err error) { return nil },
|
|
|
|
OnClose: func() (err error) { return nil },
|
|
|
|
}, p)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil_watcher", func(t *testing.T) {
|
|
|
|
require.Panics(t, func() {
|
2021-11-26 15:25:43 +00:00
|
|
|
_, _ = NewHostsContainer(0, testFS, nil, p)
|
2021-10-14 16:39:21 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("err_watcher", func(t *testing.T) {
|
|
|
|
const errOnAdd errors.Error = "error"
|
|
|
|
|
|
|
|
errWatcher := &aghtest.FSWatcher{
|
|
|
|
OnEvents: func() (e <-chan struct{}) { panic("not implemented") },
|
|
|
|
OnAdd: func(name string) (err error) { return errOnAdd },
|
|
|
|
OnClose: func() (err error) { panic("not implemented") },
|
|
|
|
}
|
|
|
|
|
2021-11-26 15:25:43 +00:00
|
|
|
hc, err := NewHostsContainer(0, testFS, errWatcher, p)
|
2021-10-14 16:39:21 +00:00
|
|
|
require.ErrorIs(t, err, errOnAdd)
|
|
|
|
|
|
|
|
assert.Nil(t, hc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
func TestHostsContainer_refresh(t *testing.T) {
|
|
|
|
// TODO(e.burkov): Test the case with no actual updates.
|
2021-10-14 16:39:21 +00:00
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
ip := net.IP{127, 0, 0, 1}
|
|
|
|
ipStr := ip.String()
|
2021-10-14 16:39:21 +00:00
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
testFS := fstest.MapFS{"dir/file1": &fstest.MapFile{Data: []byte(ipStr + ` hostname` + nl)}}
|
2021-10-14 16:39:21 +00:00
|
|
|
|
2021-11-16 13:16:38 +00:00
|
|
|
// event is a convenient alias for an empty struct{} to emit test events.
|
|
|
|
type event = struct{}
|
|
|
|
|
|
|
|
eventsCh := make(chan event, 1)
|
2021-10-14 16:39:21 +00:00
|
|
|
t.Cleanup(func() { close(eventsCh) })
|
|
|
|
|
|
|
|
w := &aghtest.FSWatcher{
|
2021-11-16 13:16:38 +00:00
|
|
|
OnEvents: func() (e <-chan event) { return eventsCh },
|
2021-10-14 16:39:21 +00:00
|
|
|
OnAdd: func(name string) (err error) {
|
2022-01-12 09:14:59 +00:00
|
|
|
assert.Equal(t, "dir", name)
|
2021-10-14 16:39:21 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2021-11-17 14:21:10 +00:00
|
|
|
OnClose: func() (err error) { panic("not implemented") },
|
2021-10-14 16:39:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
hc, err := NewHostsContainer(0, testFS, w, "dir")
|
2021-10-14 16:39:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
checkRefresh := func(t *testing.T, wantHosts Hosts) {
|
2021-10-14 16:39:21 +00:00
|
|
|
upd, ok := <-hc.Upd()
|
|
|
|
require.True(t, ok)
|
|
|
|
require.NotNil(t, upd)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, upd.Len())
|
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
v, ok := upd.Get(ip)
|
2021-10-14 16:39:21 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
var hosts *Hosts
|
|
|
|
hosts, ok = v.(*Hosts)
|
2021-10-14 16:39:21 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
assert.Equal(t, wantHosts.Main, hosts.Main)
|
|
|
|
assert.True(t, hosts.Aliases.Equal(wantHosts.Aliases))
|
2021-10-14 16:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("initial_refresh", func(t *testing.T) {
|
2022-01-12 09:14:59 +00:00
|
|
|
checkRefresh(t, Hosts{Main: "hostname"})
|
2021-10-14 16:39:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("second_refresh", func(t *testing.T) {
|
2022-01-12 09:14:59 +00:00
|
|
|
testFS["dir/file2"] = &fstest.MapFile{Data: []byte(ipStr + ` alias` + nl)}
|
|
|
|
eventsCh <- event{}
|
|
|
|
checkRefresh(t, Hosts{Main: "hostname", Aliases: stringutil.NewSet("alias")})
|
2021-11-16 13:16:38 +00:00
|
|
|
})
|
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
t.Run("double_refresh", func(t *testing.T) {
|
|
|
|
// Make a change once.
|
|
|
|
testFS["dir/file1"] = &fstest.MapFile{Data: []byte(ipStr + ` alias` + nl)}
|
|
|
|
eventsCh <- event{}
|
|
|
|
|
|
|
|
// Require the changes are written.
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
res, ok := hc.MatchRequest(urlfilter.DNSRequest{
|
|
|
|
Hostname: "hostname",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
})
|
2021-11-16 13:16:38 +00:00
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
return !ok && res.DNSRewrites() == nil
|
|
|
|
}, 5*time.Second, time.Second/2)
|
|
|
|
|
|
|
|
// Make a change again.
|
|
|
|
testFS["dir/file2"] = &fstest.MapFile{Data: []byte(ipStr + ` hostname` + nl)}
|
|
|
|
eventsCh <- event{}
|
|
|
|
|
|
|
|
// Require the changes are written.
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
res, ok := hc.MatchRequest(urlfilter.DNSRequest{
|
|
|
|
Hostname: "hostname",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
})
|
|
|
|
|
|
|
|
return !ok && res.DNSRewrites() != nil
|
|
|
|
}, 5*time.Second, time.Second/2)
|
|
|
|
|
|
|
|
assert.Len(t, hc.Upd(), 1)
|
2021-10-14 16:39:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHostsContainer_PathsToPatterns(t *testing.T) {
|
|
|
|
gsfs := fstest.MapFS{
|
2022-01-12 09:14:59 +00:00
|
|
|
"dir_0/file_1": &fstest.MapFile{Data: []byte{1}},
|
|
|
|
"dir_0/file_2": &fstest.MapFile{Data: []byte{2}},
|
|
|
|
"dir_0/dir_1/file_3": &fstest.MapFile{Data: []byte{3}},
|
2021-10-14 16:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2022-01-12 09:14:59 +00:00
|
|
|
name string
|
|
|
|
paths []string
|
|
|
|
want []string
|
2021-10-14 16:39:21 +00:00
|
|
|
}{{
|
2022-01-12 09:14:59 +00:00
|
|
|
name: "no_paths",
|
|
|
|
paths: nil,
|
|
|
|
want: nil,
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
2022-01-12 09:14:59 +00:00
|
|
|
name: "single_file",
|
|
|
|
paths: []string{"dir_0/file_1"},
|
|
|
|
want: []string{"dir_0/file_1"},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
2022-01-12 09:14:59 +00:00
|
|
|
name: "several_files",
|
|
|
|
paths: []string{"dir_0/file_1", "dir_0/file_2"},
|
|
|
|
want: []string{"dir_0/file_1", "dir_0/file_2"},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
2022-01-12 09:14:59 +00:00
|
|
|
name: "whole_dir",
|
|
|
|
paths: []string{"dir_0"},
|
|
|
|
want: []string{"dir_0/*"},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
2022-01-12 09:14:59 +00:00
|
|
|
name: "file_and_dir",
|
|
|
|
paths: []string{"dir_0/file_1", "dir_0/dir_1"},
|
|
|
|
want: []string{"dir_0/file_1", "dir_0/dir_1/*"},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
2022-01-12 09:14:59 +00:00
|
|
|
name: "non-existing",
|
|
|
|
paths: []string{path.Join("dir_0", "file_3")},
|
|
|
|
want: nil,
|
2021-10-14 16:39:21 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
patterns, err := pathsToPatterns(gsfs, tc.paths)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, patterns)
|
|
|
|
})
|
|
|
|
}
|
2021-11-17 14:21:10 +00:00
|
|
|
|
|
|
|
t.Run("bad_file", func(t *testing.T) {
|
|
|
|
const errStat errors.Error = "bad file"
|
|
|
|
|
|
|
|
badFS := &aghtest.StatFS{
|
|
|
|
OnStat: func(name string) (fs.FileInfo, error) {
|
|
|
|
return nil, errStat
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := pathsToPatterns(badFS, []string{""})
|
|
|
|
assert.ErrorIs(t, err, errStat)
|
|
|
|
})
|
2021-10-14 16:39:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
func TestHostsContainer_Translate(t *testing.T) {
|
|
|
|
testdata := os.DirFS("./testdata")
|
|
|
|
stubWatcher := aghtest.FSWatcher{
|
|
|
|
OnEvents: func() (e <-chan struct{}) { return nil },
|
|
|
|
OnAdd: func(name string) (err error) { return nil },
|
|
|
|
OnClose: func() (err error) { panic("not implemented") },
|
|
|
|
}
|
|
|
|
|
|
|
|
hc, err := NewHostsContainer(0, testdata, &stubWatcher, "etc_hosts")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
rule string
|
|
|
|
wantTrans []string
|
|
|
|
}{{
|
|
|
|
name: "simplehost",
|
|
|
|
rule: "|simplehost^$dnsrewrite=NOERROR;A;1.0.0.1",
|
|
|
|
wantTrans: []string{"1.0.0.1", "simplehost"},
|
|
|
|
}, {
|
|
|
|
name: "hello",
|
|
|
|
rule: "|hello^$dnsrewrite=NOERROR;A;1.0.0.0",
|
|
|
|
wantTrans: []string{"1.0.0.0", "hello", "hello.world", "hello.world.again"},
|
|
|
|
}, {
|
|
|
|
name: "simplehost_v6",
|
|
|
|
rule: "|simplehost^$dnsrewrite=NOERROR;AAAA;::1",
|
|
|
|
wantTrans: []string{"::1", "simplehost"},
|
|
|
|
}, {
|
|
|
|
name: "hello_v6",
|
|
|
|
rule: "|hello^$dnsrewrite=NOERROR;AAAA;::",
|
|
|
|
wantTrans: []string{"::", "hello", "hello.world", "hello.world.again"},
|
|
|
|
}, {
|
|
|
|
name: "simplehost_ptr",
|
|
|
|
rule: "|1.0.0.1.in-addr.arpa^$dnsrewrite=NOERROR;PTR;simplehost.",
|
|
|
|
wantTrans: []string{"1.0.0.1", "simplehost"},
|
|
|
|
}, {
|
|
|
|
name: "hello_ptr",
|
|
|
|
rule: "|0.0.0.1.in-addr.arpa^$dnsrewrite=NOERROR;PTR;hello.",
|
|
|
|
wantTrans: []string{"1.0.0.0", "hello", "hello.world", "hello.world.again"},
|
|
|
|
}, {
|
|
|
|
name: "simplehost_ptr_v6",
|
|
|
|
rule: "|1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa" +
|
|
|
|
"^$dnsrewrite=NOERROR;PTR;simplehost.",
|
|
|
|
wantTrans: []string{"::1", "simplehost"},
|
|
|
|
}, {
|
|
|
|
name: "hello_ptr_v6",
|
|
|
|
rule: "|0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa" +
|
|
|
|
"^$dnsrewrite=NOERROR;PTR;hello.",
|
|
|
|
wantTrans: []string{"::", "hello", "hello.world", "hello.world.again"},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
want := stringutil.NewSet(tc.wantTrans...)
|
|
|
|
got := stringutil.NewSet(strings.Fields(hc.Translate(tc.rule))...)
|
|
|
|
assert.True(t, want.Equal(got))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 15:01:48 +00:00
|
|
|
func TestHostsContainer(t *testing.T) {
|
2021-11-26 15:25:43 +00:00
|
|
|
const listID = 1234
|
|
|
|
|
2021-11-23 15:01:48 +00:00
|
|
|
testdata := os.DirFS("./testdata")
|
|
|
|
|
|
|
|
nRewrites := func(t *testing.T, res *urlfilter.DNSResult, n int) (rws []*rules.DNSRewrite) {
|
|
|
|
rewrites := res.DNSRewrites()
|
2022-01-12 09:14:59 +00:00
|
|
|
require.Len(t, rewrites, n)
|
2021-10-14 16:39:21 +00:00
|
|
|
|
2021-11-23 15:01:48 +00:00
|
|
|
for _, rewrite := range rewrites {
|
2021-11-26 15:25:43 +00:00
|
|
|
require.Equal(t, listID, rewrite.FilterListID)
|
|
|
|
|
2021-11-23 15:01:48 +00:00
|
|
|
rw := rewrite.DNSRewrite
|
|
|
|
require.NotNil(t, rw)
|
2021-10-14 16:39:21 +00:00
|
|
|
|
2021-11-23 15:01:48 +00:00
|
|
|
rws = append(rws, rw)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rws
|
|
|
|
}
|
2021-10-14 16:39:21 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
2021-11-23 15:01:48 +00:00
|
|
|
testTail func(t *testing.T, res *urlfilter.DNSResult)
|
|
|
|
name string
|
|
|
|
req urlfilter.DNSRequest
|
2021-10-14 16:39:21 +00:00
|
|
|
}{{
|
2021-11-23 15:01:48 +00:00
|
|
|
name: "simple",
|
|
|
|
req: urlfilter.DNSRequest{
|
|
|
|
Hostname: "simplehost",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
|
|
|
testTail: func(t *testing.T, res *urlfilter.DNSResult) {
|
|
|
|
rws := nRewrites(t, res, 2)
|
|
|
|
|
|
|
|
v, ok := rws[0].Value.(net.IP)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
assert.True(t, net.IP{1, 0, 0, 1}.Equal(v))
|
|
|
|
|
|
|
|
v, ok = rws[1].Value.(net.IP)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
// It's ::1.
|
|
|
|
assert.True(t, net.IP(append((&[15]byte{})[:], byte(1))).Equal(v))
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "hello_alias",
|
|
|
|
req: urlfilter.DNSRequest{
|
|
|
|
Hostname: "hello.world",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
|
|
|
testTail: func(t *testing.T, res *urlfilter.DNSResult) {
|
|
|
|
assert.Equal(t, "hello", nRewrites(t, res, 1)[0].NewCNAME)
|
|
|
|
},
|
2022-01-12 09:14:59 +00:00
|
|
|
}, {
|
|
|
|
name: "other_line_alias",
|
|
|
|
req: urlfilter.DNSRequest{
|
|
|
|
Hostname: "hello.world.again",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
|
|
|
testTail: func(t *testing.T, res *urlfilter.DNSResult) {
|
|
|
|
assert.Equal(t, "hello", nRewrites(t, res, 1)[0].NewCNAME)
|
|
|
|
},
|
2021-12-23 13:35:10 +00:00
|
|
|
}, {
|
|
|
|
name: "hello_subdomain",
|
|
|
|
req: urlfilter.DNSRequest{
|
|
|
|
Hostname: "say.hello",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
|
|
|
testTail: func(t *testing.T, res *urlfilter.DNSResult) {
|
|
|
|
assert.Empty(t, res.DNSRewrites())
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "hello_alias_subdomain",
|
|
|
|
req: urlfilter.DNSRequest{
|
|
|
|
Hostname: "say.hello.world",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
|
|
|
testTail: func(t *testing.T, res *urlfilter.DNSResult) {
|
|
|
|
assert.Empty(t, res.DNSRewrites())
|
|
|
|
},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
2021-11-23 15:01:48 +00:00
|
|
|
name: "lots_of_aliases",
|
|
|
|
req: urlfilter.DNSRequest{
|
|
|
|
Hostname: "for.testing",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
|
|
|
testTail: func(t *testing.T, res *urlfilter.DNSResult) {
|
|
|
|
assert.Equal(t, "a.whole", nRewrites(t, res, 1)[0].NewCNAME)
|
|
|
|
},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
2021-11-23 15:01:48 +00:00
|
|
|
name: "reverse",
|
|
|
|
req: urlfilter.DNSRequest{
|
|
|
|
Hostname: "1.0.0.1.in-addr.arpa",
|
|
|
|
DNSType: dns.TypePTR,
|
|
|
|
},
|
|
|
|
testTail: func(t *testing.T, res *urlfilter.DNSResult) {
|
|
|
|
rws := nRewrites(t, res, 1)
|
|
|
|
|
|
|
|
assert.Equal(t, dns.TypePTR, rws[0].RRType)
|
|
|
|
assert.Equal(t, "simplehost.", rws[0].Value)
|
|
|
|
},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
2021-11-23 15:01:48 +00:00
|
|
|
name: "non-existing",
|
|
|
|
req: urlfilter.DNSRequest{
|
|
|
|
Hostname: "nonexisting",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
|
|
|
testTail: func(t *testing.T, res *urlfilter.DNSResult) {
|
|
|
|
require.NotNil(t, res)
|
|
|
|
|
|
|
|
assert.Nil(t, res.DNSRewrites())
|
|
|
|
},
|
2021-10-14 16:39:21 +00:00
|
|
|
}}
|
|
|
|
|
2021-11-23 15:01:48 +00:00
|
|
|
stubWatcher := aghtest.FSWatcher{
|
|
|
|
OnEvents: func() (e <-chan struct{}) { return nil },
|
|
|
|
OnAdd: func(name string) (err error) { return nil },
|
|
|
|
OnClose: func() (err error) { panic("not implemented") },
|
|
|
|
}
|
2021-10-14 16:39:21 +00:00
|
|
|
|
2021-11-26 15:25:43 +00:00
|
|
|
hc, err := NewHostsContainer(listID, testdata, &stubWatcher, "etc_hosts")
|
2021-11-23 15:01:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2021-10-14 16:39:21 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-11-23 15:01:48 +00:00
|
|
|
res, ok := hc.MatchRequest(tc.req)
|
|
|
|
require.False(t, ok)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
|
|
|
|
tc.testTail(t, res)
|
2021-10-14 16:39:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUniqueRules_ParseLine(t *testing.T) {
|
2022-01-12 09:14:59 +00:00
|
|
|
ip := net.IP{127, 0, 0, 1}
|
|
|
|
ipStr := ip.String()
|
2021-10-14 16:39:21 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
line string
|
|
|
|
wantIP net.IP
|
|
|
|
wantHosts []string
|
|
|
|
}{{
|
|
|
|
name: "simple",
|
2022-01-12 09:14:59 +00:00
|
|
|
line: ipStr + ` hostname`,
|
|
|
|
wantIP: ip,
|
|
|
|
wantHosts: []string{"hostname"},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
|
|
|
name: "aliases",
|
2022-01-12 09:14:59 +00:00
|
|
|
line: ipStr + ` hostname alias`,
|
|
|
|
wantIP: ip,
|
|
|
|
wantHosts: []string{"hostname", "alias"},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
|
|
|
name: "invalid_line",
|
2022-01-12 09:14:59 +00:00
|
|
|
line: ipStr,
|
2021-10-14 16:39:21 +00:00
|
|
|
wantIP: nil,
|
|
|
|
wantHosts: nil,
|
|
|
|
}, {
|
|
|
|
name: "invalid_line_hostname",
|
2022-01-12 09:14:59 +00:00
|
|
|
line: ipStr + ` # hostname`,
|
|
|
|
wantIP: ip,
|
2021-10-14 16:39:21 +00:00
|
|
|
wantHosts: nil,
|
|
|
|
}, {
|
|
|
|
name: "commented_aliases",
|
2022-01-12 09:14:59 +00:00
|
|
|
line: ipStr + ` hostname # alias`,
|
|
|
|
wantIP: ip,
|
|
|
|
wantHosts: []string{"hostname"},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
|
|
|
name: "whole_comment",
|
2022-01-12 09:14:59 +00:00
|
|
|
line: `# ` + ipStr + ` hostname`,
|
2021-10-14 16:39:21 +00:00
|
|
|
wantIP: nil,
|
|
|
|
wantHosts: nil,
|
|
|
|
}, {
|
|
|
|
name: "partial_comment",
|
2022-01-12 09:14:59 +00:00
|
|
|
line: ipStr + ` host#name`,
|
|
|
|
wantIP: ip,
|
|
|
|
wantHosts: []string{"host"},
|
2021-10-14 16:39:21 +00:00
|
|
|
}, {
|
|
|
|
name: "empty",
|
|
|
|
line: ``,
|
|
|
|
wantIP: nil,
|
|
|
|
wantHosts: nil,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
hp := hostsParser{}
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-01-12 09:14:59 +00:00
|
|
|
got, hosts := hp.parseLine(tc.line)
|
|
|
|
assert.True(t, tc.wantIP.Equal(got))
|
2021-10-14 16:39:21 +00:00
|
|
|
assert.Equal(t, tc.wantHosts, hosts)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|