mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-17 10:58:29 -07:00
fa33568fab
Merge in DNS/adguard-home from imp-errcheck to master Squashed commit of the following: commit ed046b8ef59a092a27c623cd14b3fc2ef305fc3d Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 4 15:00:04 2021 +0300 stats: imp cleanup more commit e53a9240d3e3eec2417c768b98c267a8cd54d992 Merge: da734317676f8c76
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 4 14:59:40 2021 +0300 Merge branch 'master' into imp-errcheck commit da734317035543b52e5a9030813084bdc92ba90a Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 4 14:37:26 2021 +0300 stats: imp cleanup commit 8b4ad150129111a09be6fa2944a21bd06ab8e5a1 Merge: 385c8a6c5081ead0
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 4 14:34:26 2021 +0300 Merge branch 'master' into imp-errcheck commit 385c8a6c91e3bf07a457da370c8cc77820b91600 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Jan 29 20:41:57 2021 +0300 all: imp errcheck in tests
179 lines
4.7 KiB
Go
179 lines
4.7 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
|
"github.com/miekg/dns"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestServer_FilterDNSRewrite(t *testing.T) {
|
|
// Helper data.
|
|
ip4 := net.IP{127, 0, 0, 1}
|
|
ip6 := net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
|
|
mx := &rules.DNSMX{
|
|
Exchange: "mail.example.com",
|
|
Preference: 32,
|
|
}
|
|
svcb := &rules.DNSSVCB{
|
|
Params: map[string]string{"alpn": "h3"},
|
|
Target: "example.com",
|
|
Priority: 32,
|
|
}
|
|
const domain = "example.com"
|
|
|
|
// Helper functions and entities.
|
|
srv := &Server{}
|
|
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 dnsfilter.Result) {
|
|
resp := dnsfilter.DNSRewriteResultResponse{
|
|
rr: []rules.RRValue{v},
|
|
}
|
|
return dnsfilter.Result{
|
|
DNSRewriteResult: &dnsfilter.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)
|
|
assert.Nil(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)
|
|
assert.Nil(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)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
if assert.Len(t, d.Res.Answer, 1) {
|
|
assert.Equal(t, ip4, 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)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
if assert.Len(t, d.Res.Answer, 1) {
|
|
assert.Equal(t, ip6, 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)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
if assert.Len(t, d.Res.Answer, 1) {
|
|
assert.Equal(t, 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)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
if assert.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, mx)
|
|
d := &proxy.DNSContext{}
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
if assert.Len(t, d.Res.Answer, 1) {
|
|
ans, ok := d.Res.Answer[0].(*dns.MX)
|
|
if assert.True(t, ok) {
|
|
assert.Equal(t, mx.Exchange, ans.Mx)
|
|
assert.Equal(t, mx.Preference, ans.Preference)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("noerror_svcb", func(t *testing.T) {
|
|
req := makeQ(dns.TypeSVCB)
|
|
res := makeRes(dns.RcodeSuccess, dns.TypeSVCB, svcb)
|
|
d := &proxy.DNSContext{}
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
if assert.Len(t, d.Res.Answer, 1) {
|
|
ans, ok := d.Res.Answer[0].(*dns.SVCB)
|
|
if assert.True(t, ok) {
|
|
assert.Equal(t, dns.SVCB_ALPN, ans.Value[0].Key())
|
|
assert.Equal(t, svcb.Params["alpn"], ans.Value[0].String())
|
|
assert.Equal(t, svcb.Target, ans.Target)
|
|
assert.Equal(t, svcb.Priority, ans.Priority)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("noerror_https", func(t *testing.T) {
|
|
req := makeQ(dns.TypeHTTPS)
|
|
res := makeRes(dns.RcodeSuccess, dns.TypeHTTPS, svcb)
|
|
d := &proxy.DNSContext{}
|
|
|
|
err := srv.filterDNSRewrite(req, res, d)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, dns.RcodeSuccess, d.Res.Rcode)
|
|
if assert.Len(t, d.Res.Answer, 1) {
|
|
ans, ok := d.Res.Answer[0].(*dns.HTTPS)
|
|
if assert.True(t, ok) {
|
|
assert.Equal(t, dns.SVCB_ALPN, ans.Value[0].Key())
|
|
assert.Equal(t, svcb.Params["alpn"], ans.Value[0].String())
|
|
assert.Equal(t, svcb.Target, ans.Target)
|
|
assert.Equal(t, svcb.Priority, ans.Priority)
|
|
}
|
|
}
|
|
})
|
|
}
|