2021-01-14 15:30:39 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2021-01-14 15:30:39 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2023-08-23 16:58:24 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-01-14 15:30:39 +03:00
|
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-11 17:32:58 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-01-14 15:30:39 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServer_FilterDNSRewrite(t *testing.T) {
|
|
|
|
// Helper data.
|
2021-03-15 13:08:13 +03:00
|
|
|
const domain = "example.com"
|
2023-08-23 16:58:24 +03:00
|
|
|
ip4, ip6 := netutil.IPv4Localhost(), netutil.IPv6Localhost()
|
2021-03-15 13:08:13 +03:00
|
|
|
mxVal := &rules.DNSMX{
|
2021-01-14 15:30:39 +03:00
|
|
|
Exchange: "mail.example.com",
|
|
|
|
Preference: 32,
|
|
|
|
}
|
2021-03-15 13:08:13 +03:00
|
|
|
svcbVal := &rules.DNSSVCB{
|
2022-04-12 15:45:18 +03:00
|
|
|
Params: map[string]string{"alpn": "h3", "dohpath": "/dns-query"},
|
2021-03-15 17:17:16 +03:00
|
|
|
Target: dns.Fqdn(domain),
|
2021-01-14 15:30:39 +03:00
|
|
|
Priority: 32,
|
|
|
|
}
|
2021-03-15 13:08:13 +03:00
|
|
|
srvVal := &rules.DNSSRV{
|
|
|
|
Priority: 32,
|
|
|
|
Weight: 60,
|
|
|
|
Port: 8080,
|
2021-03-15 17:17:16 +03:00
|
|
|
Target: dns.Fqdn(domain),
|
2021-03-15 13:08:13 +03:00
|
|
|
}
|
2021-01-14 15:30:39 +03:00
|
|
|
|
|
|
|
// Helper functions and entities.
|
2023-09-04 17:18:43 +03:00
|
|
|
srv := createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, ServerConfig{
|
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-09-04 17:18:43 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2024-03-11 18:17:04 +03:00
|
|
|
})
|
2023-09-04 17:18:43 +03:00
|
|
|
|
2021-01-14 15:30:39 +03:00
|
|
|
makeQ := func(qtype rules.RRType) (req *dns.Msg) {
|
|
|
|
return &dns.Msg{
|
|
|
|
Question: []dns.Question{{
|
|
|
|
Qtype: qtype,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
}
|
2022-09-02 14:52:19 +03:00
|
|
|
makeRes := func(rcode rules.RCode, rr rules.RRType, v rules.RRValue) (res *filtering.Result) {
|
2021-05-21 16:15:47 +03:00
|
|
|
resp := filtering.DNSRewriteResultResponse{
|
2021-01-14 15:30:39 +03:00
|
|
|
rr: []rules.RRValue{v},
|
|
|
|
}
|
2022-09-02 14:52:19 +03:00
|
|
|
return &filtering.Result{
|
2021-05-21 16:15:47 +03:00
|
|
|
DNSRewriteResult: &filtering.DNSRewriteResult{
|
2021-01-14 15:30:39 +03:00
|
|
|
RCode: rcode,
|
|
|
|
Response: resp,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests.
|
|
|
|
t.Run("nxdomain", func(t *testing.T) {
|
|
|
|
req := makeQ(dns.TypeA)
|
|
|
|
res := makeRes(dns.RcodeNameError, 0, nil)
|
|
|
|
d := &proxy.DNSContext{}
|
|
|
|
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2021-01-14 15:30:39 +03:00
|
|
|
assert.Equal(t, dns.RcodeNameError, d.Res.Rcode)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("noerror_empty", func(t *testing.T) {
|
|
|
|
req := makeQ(dns.TypeA)
|
|
|
|
res := makeRes(dns.RcodeSuccess, 0, nil)
|
|
|
|
d := &proxy.DNSContext{}
|
|
|
|
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-14 15:30:39 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
|
|
assert.Empty(t, d.Res.Answer)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("noerror_a", func(t *testing.T) {
|
|
|
|
req := makeQ(dns.TypeA)
|
|
|
|
res := makeRes(dns.RcodeSuccess, dns.TypeA, ip4)
|
|
|
|
d := &proxy.DNSContext{}
|
|
|
|
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-14 15:30:39 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
2023-08-23 16:58:24 +03:00
|
|
|
assert.Equal(t, net.IP(ip4.AsSlice()), d.Res.Answer[0].(*dns.A).A)
|
2021-01-14 15:30:39 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("noerror_aaaa", func(t *testing.T) {
|
|
|
|
req := makeQ(dns.TypeAAAA)
|
|
|
|
res := makeRes(dns.RcodeSuccess, dns.TypeAAAA, ip6)
|
|
|
|
d := &proxy.DNSContext{}
|
|
|
|
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-14 15:30:39 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
2023-08-23 16:58:24 +03:00
|
|
|
assert.Equal(t, net.IP(ip6.AsSlice()), d.Res.Answer[0].(*dns.AAAA).AAAA)
|
2021-01-14 15:30:39 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("noerror_ptr", func(t *testing.T) {
|
|
|
|
req := makeQ(dns.TypePTR)
|
|
|
|
res := makeRes(dns.RcodeSuccess, dns.TypePTR, domain)
|
|
|
|
d := &proxy.DNSContext{}
|
|
|
|
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-14 15:30:39 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
2021-03-15 17:17:16 +03:00
|
|
|
assert.Equal(t, dns.Fqdn(domain), d.Res.Answer[0].(*dns.PTR).Ptr)
|
2021-01-14 15:30:39 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("noerror_txt", func(t *testing.T) {
|
|
|
|
req := makeQ(dns.TypeTXT)
|
|
|
|
res := makeRes(dns.RcodeSuccess, dns.TypeTXT, domain)
|
|
|
|
d := &proxy.DNSContext{}
|
|
|
|
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-14 15:30:39 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
|
|
|
assert.Equal(t, []string{domain}, d.Res.Answer[0].(*dns.TXT).Txt)
|
2021-01-14 15:30:39 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("noerror_mx", func(t *testing.T) {
|
|
|
|
req := makeQ(dns.TypeMX)
|
2021-03-15 13:08:13 +03:00
|
|
|
res := makeRes(dns.RcodeSuccess, dns.TypeMX, mxVal)
|
2021-01-14 15:30:39 +03:00
|
|
|
d := &proxy.DNSContext{}
|
|
|
|
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-14 15:30:39 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
|
|
|
ans, ok := d.Res.Answer[0].(*dns.MX)
|
|
|
|
|
|
|
|
require.True(t, ok)
|
2021-03-15 17:17:16 +03:00
|
|
|
assert.Equal(t, dns.Fqdn(mxVal.Exchange), ans.Mx)
|
2021-03-15 13:08:13 +03:00
|
|
|
assert.Equal(t, mxVal.Preference, ans.Preference)
|
2021-01-14 15:30:39 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("noerror_svcb", func(t *testing.T) {
|
|
|
|
req := makeQ(dns.TypeSVCB)
|
2021-03-15 13:08:13 +03:00
|
|
|
res := makeRes(dns.RcodeSuccess, dns.TypeSVCB, svcbVal)
|
2021-01-14 15:30:39 +03:00
|
|
|
d := &proxy.DNSContext{}
|
|
|
|
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-14 15:30:39 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
|
|
|
ans, ok := d.Res.Answer[0].(*dns.SVCB)
|
|
|
|
|
2022-04-12 15:45:18 +03:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Len(t, ans.Value, 2)
|
|
|
|
|
|
|
|
assert.ElementsMatch(
|
|
|
|
t,
|
|
|
|
[]dns.SVCBKey{dns.SVCB_ALPN, dns.SVCB_DOHPATH},
|
|
|
|
[]dns.SVCBKey{ans.Value[0].Key(), ans.Value[1].Key()},
|
|
|
|
)
|
|
|
|
assert.ElementsMatch(
|
|
|
|
t,
|
|
|
|
[]string{svcbVal.Params["alpn"], svcbVal.Params["dohpath"]},
|
|
|
|
[]string{ans.Value[0].String(), ans.Value[1].String()},
|
|
|
|
)
|
2021-03-15 13:08:13 +03:00
|
|
|
assert.Equal(t, svcbVal.Target, ans.Target)
|
|
|
|
assert.Equal(t, svcbVal.Priority, ans.Priority)
|
2021-01-14 15:30:39 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("noerror_https", func(t *testing.T) {
|
|
|
|
req := makeQ(dns.TypeHTTPS)
|
2021-03-15 13:08:13 +03:00
|
|
|
res := makeRes(dns.RcodeSuccess, dns.TypeHTTPS, svcbVal)
|
2021-01-14 15:30:39 +03:00
|
|
|
d := &proxy.DNSContext{}
|
|
|
|
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-14 15:30:39 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
|
|
|
ans, ok := d.Res.Answer[0].(*dns.HTTPS)
|
|
|
|
|
|
|
|
require.True(t, ok)
|
2022-04-12 15:45:18 +03:00
|
|
|
require.Len(t, ans.Value, 2)
|
|
|
|
|
|
|
|
assert.ElementsMatch(
|
|
|
|
t,
|
|
|
|
[]dns.SVCBKey{dns.SVCB_ALPN, dns.SVCB_DOHPATH},
|
|
|
|
[]dns.SVCBKey{ans.Value[0].Key(), ans.Value[1].Key()},
|
|
|
|
)
|
|
|
|
assert.ElementsMatch(
|
|
|
|
t,
|
|
|
|
[]string{svcbVal.Params["alpn"], svcbVal.Params["dohpath"]},
|
|
|
|
[]string{ans.Value[0].String(), ans.Value[1].String()},
|
|
|
|
)
|
2021-03-15 13:08:13 +03:00
|
|
|
assert.Equal(t, svcbVal.Target, ans.Target)
|
|
|
|
assert.Equal(t, svcbVal.Priority, ans.Priority)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("noerror_srv", func(t *testing.T) {
|
|
|
|
req := makeQ(dns.TypeSRV)
|
|
|
|
res := makeRes(dns.RcodeSuccess, dns.TypeSRV, srvVal)
|
|
|
|
d := &proxy.DNSContext{}
|
|
|
|
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-15 13:08:13 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
|
|
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
|
|
|
ans, ok := d.Res.Answer[0].(*dns.SRV)
|
|
|
|
|
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, srvVal.Priority, ans.Priority)
|
|
|
|
assert.Equal(t, srvVal.Weight, ans.Weight)
|
|
|
|
assert.Equal(t, srvVal.Port, ans.Port)
|
|
|
|
assert.Equal(t, srvVal.Target, ans.Target)
|
2021-01-14 15:30:39 +03:00
|
|
|
})
|
|
|
|
}
|