mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-16 11:52:58 +03:00
a2ca8b5b4a
Squashed commit of the following: commit a5607f811ab4642a0e34d3042240e3a9aafc3d84 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Aug 30 15:44:26 2023 +0300 home: upgrade yaml commit 0593e4da17613b8770c4567fdd5fbfde31631e6f Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Aug 30 15:01:14 2023 +0300 home: upgrade yaml commit 59ec4ba0bbe59fb2e95290f4f8101891ce4ff959 Merge: 6555941f5a325c9b6b
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Aug 30 14:57:41 2023 +0300 Merge remote-tracking branch 'origin/master' into AG-25263-filtering-config # Conflicts: # internal/dnsforward/http.go # internal/dnsforward/http_test.go # internal/dnsforward/process.go # internal/dnsforward/process_internal_test.go commit 6555941f57e99aa8bc2473b8be9e4f096ce665e0 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Aug 30 14:38:01 2023 +0300 docs: changelog commit c66d14cecbb1f08869a90c46e58d08ec03e29de5 Merge: a50ff1622aac36a2d2
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Aug 30 12:58:41 2023 +0300 Merge remote-tracking branch 'origin/master' into AG-25263-filtering-config commit a50ff162291f7953912c1bb195bd7b5d802d0ab3 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Aug 30 11:22:12 2023 +0300 home: imp code commit ef40d073e00d6acb164c0d680ed9971c4e75fbb7 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Aug 29 16:19:14 2023 +0300 home: imp code commit 5fa09a95e901a72dc5d2a90ba47c96842152587b Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Aug 29 14:47:48 2023 +0300 home: imp code commit 52bb295120f468faf94c953d0e795d1d58696ef1 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Aug 29 13:33:01 2023 +0300 home: imp code commit 24cfccf071385d660aeba4ced744ebbacc819686 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Aug 29 12:24:20 2023 +0300 filtering: imp code commit 758242b69ee5f351b25407340f989ec6b90d3607 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Aug 29 10:32:51 2023 +0300 home: imp code commit 906deaa3bf86af5038fb05a54c34cc9511500ac9 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Aug 28 14:46:01 2023 +0300 dnsforward: imp code commit 978bb508349cd0b6a1a0bd3df5b879b6a4dd9e29 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Aug 28 14:40:31 2023 +0300 home: imp code commit d5b8dd5c930b0ccbe612f320a9908f2849370fd4 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Aug 28 12:34:32 2023 +0300 filtering: imp code commit d3e5726df6bc93112329ec81bc83932ccf64b8de Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Aug 28 11:57:33 2023 +0300 home: upgrade yaml commit 75d701553595786d5695540c001e6d46153351a9 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Aug 28 11:46:55 2023 +0300 home: upgrade yaml commit cd8cb623b0e6eb00fd05fb62b7445ca4315b94e0 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Aug 28 11:29:16 2023 +0300 docs: changelog commit 31c098d6f8d68a4afa30246c8d067a1ef4586de9 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Aug 28 11:08:48 2023 +0300 docs: changelog commit 24c88dd5d42b0168a13ea11b3f760eae1e804558 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Sun Aug 27 19:12:55 2023 +0300 home: imp code commit 94f2d386c9c1b6be5df06525078c90a3d068641f Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Sun Aug 27 16:15:05 2023 +0300 home: upgrade yaml ... and 10 more commits
237 lines
6.0 KiB
Go
237 lines
6.0 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
|
"github.com/miekg/dns"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestServer_FilterDNSRewrite(t *testing.T) {
|
|
// Helper data.
|
|
const domain = "example.com"
|
|
ip4, ip6 := netutil.IPv4Localhost(), netutil.IPv6Localhost()
|
|
mxVal := &rules.DNSMX{
|
|
Exchange: "mail.example.com",
|
|
Preference: 32,
|
|
}
|
|
svcbVal := &rules.DNSSVCB{
|
|
Params: map[string]string{"alpn": "h3", "dohpath": "/dns-query"},
|
|
Target: dns.Fqdn(domain),
|
|
Priority: 32,
|
|
}
|
|
srvVal := &rules.DNSSRV{
|
|
Priority: 32,
|
|
Weight: 60,
|
|
Port: 8080,
|
|
Target: dns.Fqdn(domain),
|
|
}
|
|
|
|
// Helper functions and entities.
|
|
srv := &Server{
|
|
dnsFilter: &filtering.DNSFilter{},
|
|
}
|
|
makeQ := func(qtype rules.RRType) (req *dns.Msg) {
|
|
return &dns.Msg{
|
|
Question: []dns.Question{{
|
|
Qtype: qtype,
|
|
}},
|
|
}
|
|
}
|
|
makeRes := func(rcode rules.RCode, rr rules.RRType, v rules.RRValue) (res *filtering.Result) {
|
|
resp := filtering.DNSRewriteResultResponse{
|
|
rr: []rules.RRValue{v},
|
|
}
|
|
return &filtering.Result{
|
|
DNSRewriteResult: &filtering.DNSRewriteResult{
|
|
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)
|
|
require.NoError(t, err)
|
|
|
|
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)
|
|
require.NoError(t, err)
|
|
|
|
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)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
|
assert.Equal(t, net.IP(ip4.AsSlice()), d.Res.Answer[0].(*dns.A).A)
|
|
})
|
|
|
|
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)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
|
assert.Equal(t, net.IP(ip6.AsSlice()), d.Res.Answer[0].(*dns.AAAA).AAAA)
|
|
})
|
|
|
|
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)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
|
assert.Equal(t, dns.Fqdn(domain), d.Res.Answer[0].(*dns.PTR).Ptr)
|
|
})
|
|
|
|
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)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
|
assert.Equal(t, []string{domain}, d.Res.Answer[0].(*dns.TXT).Txt)
|
|
})
|
|
|
|
t.Run("noerror_mx", func(t *testing.T) {
|
|
req := makeQ(dns.TypeMX)
|
|
res := makeRes(dns.RcodeSuccess, dns.TypeMX, mxVal)
|
|
d := &proxy.DNSContext{}
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
|
ans, ok := d.Res.Answer[0].(*dns.MX)
|
|
|
|
require.True(t, ok)
|
|
assert.Equal(t, dns.Fqdn(mxVal.Exchange), ans.Mx)
|
|
assert.Equal(t, mxVal.Preference, ans.Preference)
|
|
})
|
|
|
|
t.Run("noerror_svcb", func(t *testing.T) {
|
|
req := makeQ(dns.TypeSVCB)
|
|
res := makeRes(dns.RcodeSuccess, dns.TypeSVCB, svcbVal)
|
|
d := &proxy.DNSContext{}
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
|
ans, ok := d.Res.Answer[0].(*dns.SVCB)
|
|
|
|
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()},
|
|
)
|
|
assert.Equal(t, svcbVal.Target, ans.Target)
|
|
assert.Equal(t, svcbVal.Priority, ans.Priority)
|
|
})
|
|
|
|
t.Run("noerror_https", func(t *testing.T) {
|
|
req := makeQ(dns.TypeHTTPS)
|
|
res := makeRes(dns.RcodeSuccess, dns.TypeHTTPS, svcbVal)
|
|
d := &proxy.DNSContext{}
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
|
|
require.Len(t, d.Res.Answer, 1)
|
|
ans, ok := d.Res.Answer[0].(*dns.HTTPS)
|
|
|
|
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()},
|
|
)
|
|
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)
|
|
require.NoError(t, err)
|
|
|
|
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)
|
|
})
|
|
}
|