2023-07-20 12:26:35 +01:00
|
|
|
package aghnet_test
|
2021-10-14 17:39:21 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"testing/fstest"
|
2022-01-12 09:14:59 +00:00
|
|
|
"time"
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2022-10-04 14:02:55 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghchan"
|
2023-07-20 12:26:35 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-10-14 17:39:21 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2022-10-25 13:08:12 +01:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-11-16 13:16:38 +00:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2022-01-27 17:18:52 +00:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2021-10-14 17:39:21 +01:00
|
|
|
"github.com/AdguardTeam/urlfilter"
|
2021-11-23 15:01:48 +00:00
|
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
2021-10-14 17:39:21 +01:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-07-20 12:26:35 +01:00
|
|
|
const nl = "\n"
|
2021-10-14 17:39:21 +01:00
|
|
|
|
|
|
|
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 17:39:21 +01:00
|
|
|
}{{
|
2021-11-17 14:21:10 +00:00
|
|
|
wantErr: nil,
|
|
|
|
name: "one_file",
|
|
|
|
paths: []string{p},
|
2021-10-14 17:39:21 +01:00
|
|
|
}, {
|
2023-07-20 12:26:35 +01:00
|
|
|
wantErr: aghnet.ErrNoHostsPaths,
|
2021-11-17 14:21:10 +00:00
|
|
|
name: "no_files",
|
|
|
|
paths: []string{},
|
2021-10-14 17:39:21 +01:00
|
|
|
}, {
|
2023-07-20 12:26:35 +01:00
|
|
|
wantErr: aghnet.ErrNoHostsPaths,
|
2021-11-17 14:21:10 +00:00
|
|
|
name: "non-existent_file",
|
|
|
|
paths: []string{path.Join(dirname, filename+"2")},
|
2021-10-14 17:39:21 +01:00
|
|
|
}, {
|
2021-11-17 14:21:10 +00:00
|
|
|
wantErr: nil,
|
|
|
|
name: "whole_dir",
|
|
|
|
paths: []string{dirname},
|
2021-10-14 17:39:21 +01: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
|
|
|
|
}
|
|
|
|
|
2023-07-20 12:26:35 +01:00
|
|
|
hc, err := aghnet.NewHostsContainer(0, testFS, &aghtest.FSWatcher{
|
2021-10-14 17:39:21 +01:00
|
|
|
OnEvents: onEvents,
|
|
|
|
OnAdd: onAdd,
|
2023-03-28 11:27:46 +01:00
|
|
|
OnClose: func() (err error) { return nil },
|
2021-10-14 17:39:21 +01:00
|
|
|
}, tc.paths...)
|
|
|
|
if tc.wantErr != nil {
|
|
|
|
require.ErrorIs(t, err, tc.wantErr)
|
|
|
|
|
|
|
|
assert.Nil(t, hc)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2022-01-27 17:18:52 +00:00
|
|
|
testutil.CleanupAndRequireSuccess(t, hc.Close)
|
2021-10-14 17:39:21 +01:00
|
|
|
|
|
|
|
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() {
|
2023-07-20 12:26:35 +01:00
|
|
|
_, _ = aghnet.NewHostsContainer(0, nil, &aghtest.FSWatcher{
|
2021-10-14 17:39:21 +01: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() {
|
2023-07-20 12:26:35 +01:00
|
|
|
_, _ = aghnet.NewHostsContainer(0, testFS, nil, p)
|
2021-10-14 17:39:21 +01: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 },
|
2023-03-28 11:27:46 +01:00
|
|
|
OnClose: func() (err error) { return nil },
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
|
2023-07-20 12:26:35 +01:00
|
|
|
hc, err := aghnet.NewHostsContainer(0, testFS, errWatcher, p)
|
2021-10-14 17:39:21 +01: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 17:39:21 +01:00
|
|
|
|
2022-10-25 13:08:12 +01:00
|
|
|
ip := netutil.IPv4Localhost()
|
2022-01-12 09:14:59 +00:00
|
|
|
ipStr := ip.String()
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
testFS := fstest.MapFS{"dir/file1": &fstest.MapFile{Data: []byte(ipStr + ` hostname` + nl)}}
|
2021-10-14 17:39:21 +01: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 17:39:21 +01: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 17:39:21 +01:00
|
|
|
OnAdd: func(name string) (err error) {
|
2022-01-12 09:14:59 +00:00
|
|
|
assert.Equal(t, "dir", name)
|
2021-10-14 17:39:21 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2023-03-28 11:27:46 +01:00
|
|
|
OnClose: func() (err error) { return nil },
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
|
2023-07-20 12:26:35 +01:00
|
|
|
hc, err := aghnet.NewHostsContainer(0, testFS, w, "dir")
|
2021-10-14 17:39:21 +01:00
|
|
|
require.NoError(t, err)
|
2022-01-27 17:18:52 +00:00
|
|
|
testutil.CleanupAndRequireSuccess(t, hc.Close)
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2023-07-20 12:26:35 +01:00
|
|
|
checkRefresh := func(t *testing.T, want *aghnet.HostsRecord) {
|
2022-07-07 17:33:32 +01:00
|
|
|
t.Helper()
|
|
|
|
|
2022-10-04 14:02:55 +01:00
|
|
|
upd, ok := aghchan.MustReceive(hc.Upd(), 1*time.Second)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.NotNil(t, upd)
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2022-10-25 13:08:12 +01:00
|
|
|
assert.Len(t, upd, 1)
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2022-10-25 13:08:12 +01:00
|
|
|
rec, ok := upd[ip]
|
2021-10-14 17:39:21 +01:00
|
|
|
require.True(t, ok)
|
2022-07-07 17:33:32 +01:00
|
|
|
require.NotNil(t, rec)
|
|
|
|
|
2023-07-20 12:26:35 +01:00
|
|
|
assert.Truef(t, rec.Equal(want), "%+v != %+v", rec, want)
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("initial_refresh", func(t *testing.T) {
|
2023-07-20 12:26:35 +01:00
|
|
|
checkRefresh(t, &aghnet.HostsRecord{
|
2022-07-07 17:33:32 +01:00
|
|
|
Aliases: stringutil.NewSet(),
|
|
|
|
Canonical: "hostname",
|
|
|
|
})
|
2021-10-14 17:39:21 +01: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{}
|
2022-07-07 17:33:32 +01:00
|
|
|
|
2023-07-20 12:26:35 +01:00
|
|
|
checkRefresh(t, &aghnet.HostsRecord{
|
2022-07-07 17:33:32 +01:00
|
|
|
Aliases: stringutil.NewSet("alias"),
|
|
|
|
Canonical: "hostname",
|
|
|
|
})
|
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 {
|
2022-04-13 16:16:33 +01:00
|
|
|
res, ok := hc.MatchRequest(&urlfilter.DNSRequest{
|
2022-01-12 09:14:59 +00:00
|
|
|
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 {
|
2022-04-13 16:16:33 +01:00
|
|
|
res, ok := hc.MatchRequest(&urlfilter.DNSRequest{
|
2022-01-12 09:14:59 +00:00
|
|
|
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 17:39:21 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-12 09:14:59 +00:00
|
|
|
func TestHostsContainer_Translate(t *testing.T) {
|
|
|
|
stubWatcher := aghtest.FSWatcher{
|
|
|
|
OnEvents: func() (e <-chan struct{}) { return nil },
|
|
|
|
OnAdd: func(name string) (err error) { return nil },
|
2023-03-28 11:27:46 +01:00
|
|
|
OnClose: func() (err error) { return nil },
|
2022-01-12 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 13:11:57 +01:00
|
|
|
require.NoError(t, fstest.TestFS(testdata, "etc_hosts"))
|
|
|
|
|
2023-07-20 12:26:35 +01:00
|
|
|
hc, err := aghnet.NewHostsContainer(0, testdata, &stubWatcher, "etc_hosts")
|
2022-01-12 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-27 17:18:52 +00:00
|
|
|
testutil.CleanupAndRequireSuccess(t, hc.Close)
|
2022-01-12 09:14:59 +00:00
|
|
|
|
|
|
|
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",
|
2022-01-27 17:18:52 +00:00
|
|
|
wantTrans: []string{"1.0.0.0", "hello", "hello.world"},
|
|
|
|
}, {
|
|
|
|
name: "hello-alias",
|
|
|
|
rule: "|hello.world.again^$dnsrewrite=NOERROR;A;1.0.0.0",
|
|
|
|
wantTrans: []string{"1.0.0.0", "hello.world.again"},
|
2022-01-12 09:14:59 +00:00
|
|
|
}, {
|
|
|
|
name: "simplehost_v6",
|
|
|
|
rule: "|simplehost^$dnsrewrite=NOERROR;AAAA;::1",
|
|
|
|
wantTrans: []string{"::1", "simplehost"},
|
|
|
|
}, {
|
|
|
|
name: "hello_v6",
|
|
|
|
rule: "|hello^$dnsrewrite=NOERROR;AAAA;::",
|
2022-01-27 17:18:52 +00:00
|
|
|
wantTrans: []string{"::", "hello", "hello.world"},
|
|
|
|
}, {
|
|
|
|
name: "hello_v6-alias",
|
|
|
|
rule: "|hello.world.again^$dnsrewrite=NOERROR;AAAA;::",
|
|
|
|
wantTrans: []string{"::", "hello.world.again"},
|
2022-01-12 09:14:59 +00:00
|
|
|
}, {
|
|
|
|
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.",
|
2022-01-27 17:18:52 +00:00
|
|
|
wantTrans: []string{"1.0.0.0", "hello", "hello.world"},
|
|
|
|
}, {
|
|
|
|
name: "hello_ptr-alias",
|
|
|
|
rule: "|0.0.0.1.in-addr.arpa^$dnsrewrite=NOERROR;PTR;hello.world.again.",
|
|
|
|
wantTrans: []string{"1.0.0.0", "hello.world.again"},
|
2022-01-12 09:14:59 +00:00
|
|
|
}, {
|
|
|
|
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.",
|
2022-01-27 17:18:52 +00:00
|
|
|
wantTrans: []string{"::", "hello", "hello.world"},
|
|
|
|
}, {
|
|
|
|
name: "hello_ptr_v6-alias",
|
|
|
|
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.world.again.",
|
|
|
|
wantTrans: []string{"::", "hello.world.again"},
|
2022-01-12 09:14:59 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
got := stringutil.NewSet(strings.Fields(hc.Translate(tc.rule))...)
|
2022-01-27 17:18:52 +00:00
|
|
|
assert.True(t, stringutil.NewSet(tc.wantTrans...).Equal(got))
|
2022-01-12 09:14:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 15:01:48 +00:00
|
|
|
func TestHostsContainer(t *testing.T) {
|
2021-11-26 15:25:43 +00:00
|
|
|
const listID = 1234
|
|
|
|
|
2022-03-30 13:11:57 +01:00
|
|
|
require.NoError(t, fstest.TestFS(testdata, "etc_hosts"))
|
|
|
|
|
2021-10-14 17:39:21 +01:00
|
|
|
testCases := []struct {
|
2022-04-13 16:16:33 +01:00
|
|
|
req *urlfilter.DNSRequest
|
2022-07-07 17:33:32 +01:00
|
|
|
name string
|
|
|
|
want []*rules.DNSRewrite
|
2021-10-14 17:39:21 +01:00
|
|
|
}{{
|
2022-07-07 17:33:32 +01:00
|
|
|
req: &urlfilter.DNSRequest{
|
|
|
|
Hostname: "simplehost",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
|
|
|
name: "simple",
|
2022-01-21 16:21:38 +00:00
|
|
|
want: []*rules.DNSRewrite{{
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
Value: net.IPv4(1, 0, 0, 1),
|
|
|
|
RRType: dns.TypeA,
|
|
|
|
}, {
|
|
|
|
RCode: dns.RcodeSuccess,
|
2022-01-27 17:18:52 +00:00
|
|
|
Value: net.ParseIP("::1"),
|
2022-01-21 16:21:38 +00:00
|
|
|
RRType: dns.TypeAAAA,
|
|
|
|
}},
|
2022-07-07 17:33:32 +01:00
|
|
|
}, {
|
2022-04-13 16:16:33 +01:00
|
|
|
req: &urlfilter.DNSRequest{
|
2022-07-07 17:33:32 +01:00
|
|
|
Hostname: "hello.world",
|
2021-11-23 15:01:48 +00:00
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
2022-07-07 17:33:32 +01:00
|
|
|
name: "hello_alias",
|
2022-01-21 16:21:38 +00:00
|
|
|
want: []*rules.DNSRewrite{{
|
2022-01-27 17:18:52 +00:00
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
Value: net.IPv4(1, 0, 0, 0),
|
|
|
|
RRType: dns.TypeA,
|
|
|
|
}, {
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
Value: net.ParseIP("::"),
|
|
|
|
RRType: dns.TypeAAAA,
|
2022-01-21 16:21:38 +00:00
|
|
|
}},
|
2022-07-07 17:33:32 +01:00
|
|
|
}, {
|
2022-04-13 16:16:33 +01:00
|
|
|
req: &urlfilter.DNSRequest{
|
2022-07-07 17:33:32 +01:00
|
|
|
Hostname: "hello.world.again",
|
2021-11-23 15:01:48 +00:00
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
2022-07-07 17:33:32 +01:00
|
|
|
name: "other_line_alias",
|
2022-01-21 16:21:38 +00:00
|
|
|
want: []*rules.DNSRewrite{{
|
2022-01-27 17:18:52 +00:00
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
Value: net.IPv4(1, 0, 0, 0),
|
|
|
|
RRType: dns.TypeA,
|
|
|
|
}, {
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
Value: net.ParseIP("::"),
|
|
|
|
RRType: dns.TypeAAAA,
|
2022-01-21 16:21:38 +00:00
|
|
|
}},
|
2021-12-23 13:35:10 +00:00
|
|
|
}, {
|
2022-04-13 16:16:33 +01:00
|
|
|
req: &urlfilter.DNSRequest{
|
2021-12-23 13:35:10 +00:00
|
|
|
Hostname: "say.hello",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
2022-07-07 17:33:32 +01:00
|
|
|
name: "hello_subdomain",
|
2022-01-21 16:21:38 +00:00
|
|
|
want: []*rules.DNSRewrite{},
|
2022-07-07 17:33:32 +01:00
|
|
|
}, {
|
2022-04-13 16:16:33 +01:00
|
|
|
req: &urlfilter.DNSRequest{
|
2021-12-23 13:35:10 +00:00
|
|
|
Hostname: "say.hello.world",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
2022-07-07 17:33:32 +01:00
|
|
|
name: "hello_alias_subdomain",
|
|
|
|
want: []*rules.DNSRewrite{},
|
2021-10-14 17:39:21 +01:00
|
|
|
}, {
|
2022-07-07 17:33:32 +01:00
|
|
|
req: &urlfilter.DNSRequest{
|
|
|
|
Hostname: "for.testing",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
|
|
|
name: "lots_of_aliases",
|
2022-01-21 16:21:38 +00:00
|
|
|
want: []*rules.DNSRewrite{{
|
2022-01-27 17:18:52 +00:00
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
RRType: dns.TypeA,
|
|
|
|
Value: net.IPv4(1, 0, 0, 2),
|
|
|
|
}, {
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
RRType: dns.TypeAAAA,
|
|
|
|
Value: net.ParseIP("::2"),
|
2022-01-21 16:21:38 +00:00
|
|
|
}},
|
2022-07-07 17:33:32 +01:00
|
|
|
}, {
|
2022-04-13 16:16:33 +01:00
|
|
|
req: &urlfilter.DNSRequest{
|
2022-07-07 17:33:32 +01:00
|
|
|
Hostname: "1.0.0.1.in-addr.arpa",
|
|
|
|
DNSType: dns.TypePTR,
|
2021-11-23 15:01:48 +00:00
|
|
|
},
|
2022-07-07 17:33:32 +01:00
|
|
|
name: "reverse",
|
2022-01-21 16:21:38 +00:00
|
|
|
want: []*rules.DNSRewrite{{
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
RRType: dns.TypePTR,
|
|
|
|
Value: "simplehost.",
|
|
|
|
}},
|
2021-10-14 17:39:21 +01:00
|
|
|
}, {
|
2022-04-13 16:16:33 +01:00
|
|
|
req: &urlfilter.DNSRequest{
|
2022-08-16 11:21:25 +01:00
|
|
|
Hostname: "nonexistent.example",
|
2021-11-23 15:01:48 +00:00
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
2022-07-07 17:33:32 +01:00
|
|
|
name: "non-existing",
|
|
|
|
want: []*rules.DNSRewrite{},
|
2022-01-21 16:21:38 +00:00
|
|
|
}, {
|
2022-04-13 16:16:33 +01:00
|
|
|
req: &urlfilter.DNSRequest{
|
2022-01-21 16:21:38 +00:00
|
|
|
Hostname: "1.0.0.1.in-addr.arpa",
|
|
|
|
DNSType: dns.TypeSRV,
|
2021-11-23 15:01:48 +00:00
|
|
|
},
|
2022-07-07 17:33:32 +01:00
|
|
|
name: "bad_type",
|
|
|
|
want: nil,
|
2022-01-27 17:18:52 +00:00
|
|
|
}, {
|
2022-07-07 17:33:32 +01:00
|
|
|
req: &urlfilter.DNSRequest{
|
|
|
|
Hostname: "domain",
|
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
|
|
|
name: "issue_4216_4_6",
|
2022-01-27 17:18:52 +00:00
|
|
|
want: []*rules.DNSRewrite{{
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
RRType: dns.TypeA,
|
|
|
|
Value: net.IPv4(4, 2, 1, 6),
|
|
|
|
}, {
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
RRType: dns.TypeAAAA,
|
|
|
|
Value: net.ParseIP("::42"),
|
|
|
|
}},
|
2022-07-07 17:33:32 +01:00
|
|
|
}, {
|
2022-04-13 16:16:33 +01:00
|
|
|
req: &urlfilter.DNSRequest{
|
2022-07-07 17:33:32 +01:00
|
|
|
Hostname: "domain4",
|
2022-01-27 17:18:52 +00:00
|
|
|
DNSType: dns.TypeA,
|
|
|
|
},
|
2022-07-07 17:33:32 +01:00
|
|
|
name: "issue_4216_4",
|
2022-01-27 17:18:52 +00:00
|
|
|
want: []*rules.DNSRewrite{{
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
RRType: dns.TypeA,
|
|
|
|
Value: net.IPv4(7, 5, 3, 1),
|
|
|
|
}, {
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
RRType: dns.TypeA,
|
|
|
|
Value: net.IPv4(1, 3, 5, 7),
|
|
|
|
}},
|
2022-07-07 17:33:32 +01:00
|
|
|
}, {
|
2022-04-13 16:16:33 +01:00
|
|
|
req: &urlfilter.DNSRequest{
|
2022-07-07 17:33:32 +01:00
|
|
|
Hostname: "domain6",
|
|
|
|
DNSType: dns.TypeAAAA,
|
2022-01-27 17:18:52 +00:00
|
|
|
},
|
2022-07-07 17:33:32 +01:00
|
|
|
name: "issue_4216_6",
|
2022-01-27 17:18:52 +00:00
|
|
|
want: []*rules.DNSRewrite{{
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
RRType: dns.TypeAAAA,
|
|
|
|
Value: net.ParseIP("::13"),
|
|
|
|
}, {
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
RRType: dns.TypeAAAA,
|
|
|
|
Value: net.ParseIP("::31"),
|
|
|
|
}},
|
2021-10-14 17:39:21 +01: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 },
|
2023-03-28 11:27:46 +01:00
|
|
|
OnClose: func() (err error) { return nil },
|
2021-11-23 15:01:48 +00:00
|
|
|
}
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2023-07-20 12:26:35 +01:00
|
|
|
hc, err := aghnet.NewHostsContainer(listID, testdata, &stubWatcher, "etc_hosts")
|
2021-11-23 15:01:48 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-27 17:18:52 +00:00
|
|
|
testutil.CleanupAndRequireSuccess(t, hc.Close)
|
2021-11-23 15:01:48 +00:00
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2021-10-14 17:39:21 +01: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)
|
2022-01-21 16:21:38 +00:00
|
|
|
|
|
|
|
if tc.want == nil {
|
|
|
|
assert.Nil(t, res)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-23 15:01:48 +00:00
|
|
|
require.NotNil(t, res)
|
|
|
|
|
2022-01-21 16:21:38 +00:00
|
|
|
rewrites := res.DNSRewrites()
|
|
|
|
require.Len(t, rewrites, len(tc.want))
|
|
|
|
|
|
|
|
for i, rewrite := range rewrites {
|
|
|
|
require.Equal(t, listID, rewrite.FilterListID)
|
|
|
|
|
|
|
|
rw := rewrite.DNSRewrite
|
|
|
|
require.NotNil(t, rw)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want[i], rw)
|
|
|
|
}
|
2021-10-14 17:39:21 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|