all: sync with master
This commit is contained in:
@@ -3,21 +3,11 @@ package aghtest
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
)
|
||||
|
||||
// DiscardLogOutput runs tests with discarded logger output.
|
||||
func DiscardLogOutput(m *testing.M) {
|
||||
// TODO(e.burkov): Refactor code and tests to not use the global mutable
|
||||
// logger.
|
||||
log.SetOutput(io.Discard)
|
||||
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
// ReplaceLogWriter moves logger output to w and uses Cleanup method of t to
|
||||
// revert changes.
|
||||
func ReplaceLogWriter(t testing.TB, w io.Writer) {
|
||||
|
||||
@@ -15,6 +15,8 @@ import (
|
||||
|
||||
// Standard Library
|
||||
|
||||
// Package fs
|
||||
|
||||
// type check
|
||||
var _ fs.FS = &FS{}
|
||||
|
||||
@@ -58,6 +60,8 @@ func (fsys *StatFS) Stat(name string) (fs.FileInfo, error) {
|
||||
return fsys.OnStat(name)
|
||||
}
|
||||
|
||||
// Package net
|
||||
|
||||
// type check
|
||||
var _ net.Listener = (*Listener)(nil)
|
||||
|
||||
@@ -83,31 +87,9 @@ func (l *Listener) Close() (err error) {
|
||||
return l.OnClose()
|
||||
}
|
||||
|
||||
// Module dnsproxy
|
||||
// Module adguard-home
|
||||
|
||||
// type check
|
||||
var _ upstream.Upstream = (*UpstreamMock)(nil)
|
||||
|
||||
// UpstreamMock is a mock [upstream.Upstream] implementation for tests.
|
||||
//
|
||||
// TODO(a.garipov): Replace with all uses of Upstream with UpstreamMock and
|
||||
// rename it to just Upstream.
|
||||
type UpstreamMock struct {
|
||||
OnAddress func() (addr string)
|
||||
OnExchange func(req *dns.Msg) (resp *dns.Msg, err error)
|
||||
}
|
||||
|
||||
// Address implements the [upstream.Upstream] interface for *UpstreamMock.
|
||||
func (u *UpstreamMock) Address() (addr string) {
|
||||
return u.OnAddress()
|
||||
}
|
||||
|
||||
// Exchange implements the [upstream.Upstream] interface for *UpstreamMock.
|
||||
func (u *UpstreamMock) Exchange(req *dns.Msg) (resp *dns.Msg, err error) {
|
||||
return u.OnExchange(req)
|
||||
}
|
||||
|
||||
// Module AdGuardHome
|
||||
// Package aghos
|
||||
|
||||
// type check
|
||||
var _ aghos.FSWatcher = (*FSWatcher)(nil)
|
||||
@@ -133,3 +115,35 @@ func (w *FSWatcher) Add(name string) (err error) {
|
||||
func (w *FSWatcher) Close() (err error) {
|
||||
return w.OnClose()
|
||||
}
|
||||
|
||||
// Module dnsproxy
|
||||
|
||||
// Package upstream
|
||||
|
||||
// type check
|
||||
var _ upstream.Upstream = (*UpstreamMock)(nil)
|
||||
|
||||
// UpstreamMock is a mock [upstream.Upstream] implementation for tests.
|
||||
//
|
||||
// TODO(a.garipov): Replace with all uses of Upstream with UpstreamMock and
|
||||
// rename it to just Upstream.
|
||||
type UpstreamMock struct {
|
||||
OnAddress func() (addr string)
|
||||
OnExchange func(req *dns.Msg) (resp *dns.Msg, err error)
|
||||
OnClose func() (err error)
|
||||
}
|
||||
|
||||
// Address implements the [upstream.Upstream] interface for *UpstreamMock.
|
||||
func (u *UpstreamMock) Address() (addr string) {
|
||||
return u.OnAddress()
|
||||
}
|
||||
|
||||
// Exchange implements the [upstream.Upstream] interface for *UpstreamMock.
|
||||
func (u *UpstreamMock) Exchange(req *dns.Msg) (resp *dns.Msg, err error) {
|
||||
return u.OnExchange(req)
|
||||
}
|
||||
|
||||
// Close implements the [upstream.Upstream] interface for *UpstreamMock.
|
||||
func (u *UpstreamMock) Close() (err error) {
|
||||
return u.OnClose()
|
||||
}
|
||||
|
||||
@@ -1,9 +1,3 @@
|
||||
package aghtest_test
|
||||
|
||||
import (
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
||||
)
|
||||
|
||||
// type check
|
||||
var _ aghos.FSWatcher = (*aghtest.FSWatcher)(nil)
|
||||
// Put interface checks that cause import cycles here.
|
||||
|
||||
@@ -5,12 +5,12 @@ import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/AdguardTeam/dnsproxy/upstream"
|
||||
"github.com/AdguardTeam/golibs/errors"
|
||||
"github.com/miekg/dns"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// Additional Upstream Testing Utilities
|
||||
@@ -25,50 +25,11 @@ type Upstream struct {
|
||||
IPv4 map[string][]net.IP
|
||||
// IPv6 is a map of hostname to IPv6.
|
||||
IPv6 map[string][]net.IP
|
||||
// Reverse is a map of address to domain name.
|
||||
Reverse map[string][]string
|
||||
// Addr is the address for Address method.
|
||||
Addr string
|
||||
}
|
||||
|
||||
// RespondTo returns a response with answer if req has class cl, question type
|
||||
// qt, and target targ.
|
||||
func RespondTo(t testing.TB, req *dns.Msg, cl, qt uint16, targ, answer string) (resp *dns.Msg) {
|
||||
t.Helper()
|
||||
var _ upstream.Upstream = (*Upstream)(nil)
|
||||
|
||||
require.NotNil(t, req)
|
||||
require.Len(t, req.Question, 1)
|
||||
|
||||
q := req.Question[0]
|
||||
targ = dns.Fqdn(targ)
|
||||
if q.Qclass != cl || q.Qtype != qt || q.Name != targ {
|
||||
return nil
|
||||
}
|
||||
|
||||
respHdr := dns.RR_Header{
|
||||
Name: targ,
|
||||
Rrtype: qt,
|
||||
Class: cl,
|
||||
Ttl: 60,
|
||||
}
|
||||
|
||||
resp = new(dns.Msg).SetReply(req)
|
||||
switch qt {
|
||||
case dns.TypePTR:
|
||||
resp.Answer = []dns.RR{
|
||||
&dns.PTR{
|
||||
Hdr: respHdr,
|
||||
Ptr: answer,
|
||||
},
|
||||
}
|
||||
default:
|
||||
t.Fatalf("unsupported question type: %s", dns.Type(qt))
|
||||
}
|
||||
|
||||
return resp
|
||||
}
|
||||
|
||||
// Exchange implements the upstream.Upstream interface for *Upstream.
|
||||
// Exchange implements the [upstream.Upstream] interface for *Upstream.
|
||||
//
|
||||
// TODO(a.garipov): Split further into handlers.
|
||||
func (u *Upstream) Exchange(m *dns.Msg) (resp *dns.Msg, err error) {
|
||||
@@ -102,10 +63,6 @@ func (u *Upstream) Exchange(m *dns.Msg) (resp *dns.Msg, err error) {
|
||||
for _, ip := range u.IPv6[name] {
|
||||
resp.Answer = append(resp.Answer, &dns.AAAA{Hdr: hdr, AAAA: ip})
|
||||
}
|
||||
case dns.TypePTR:
|
||||
for _, name := range u.Reverse[name] {
|
||||
resp.Answer = append(resp.Answer, &dns.PTR{Hdr: hdr, Ptr: name})
|
||||
}
|
||||
}
|
||||
if len(resp.Answer) == 0 {
|
||||
resp.SetRcode(m, dns.RcodeNameError)
|
||||
@@ -114,9 +71,106 @@ func (u *Upstream) Exchange(m *dns.Msg) (resp *dns.Msg, err error) {
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Address implements upstream.Upstream interface for *Upstream.
|
||||
// Address implements [upstream.Upstream] interface for *Upstream.
|
||||
func (u *Upstream) Address() string {
|
||||
return u.Addr
|
||||
return "todo.upstream.example"
|
||||
}
|
||||
|
||||
// Close implements [upstream.Upstream] interface for *Upstream.
|
||||
func (u *Upstream) Close() (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MatchedResponse is a test helper that returns a response with answer if req
|
||||
// has question type qt, and target targ. Otherwise, it returns nil.
|
||||
//
|
||||
// req must not be nil and req.Question must have a length of 1. Answer is
|
||||
// interpreted in the following ways:
|
||||
//
|
||||
// - For A and AAAA queries, answer must be an IP address of the corresponding
|
||||
// protocol version.
|
||||
//
|
||||
// - For PTR queries, answer should be a domain name in the response.
|
||||
//
|
||||
// If the answer does not correspond to the question type, MatchedResponse panics.
|
||||
// Panics are used instead of [testing.TB], because the helper is intended to
|
||||
// use in [UpstreamMock.OnExchange] callbacks, which are usually called in a
|
||||
// separate goroutine.
|
||||
//
|
||||
// TODO(a.garipov): Consider adding version with DNS class as well.
|
||||
func MatchedResponse(req *dns.Msg, qt uint16, targ, answer string) (resp *dns.Msg) {
|
||||
if req == nil || len(req.Question) != 1 {
|
||||
panic(fmt.Errorf("bad req: %+v", req))
|
||||
}
|
||||
|
||||
q := req.Question[0]
|
||||
targ = dns.Fqdn(targ)
|
||||
if q.Qclass != dns.ClassINET || q.Qtype != qt || q.Name != targ {
|
||||
return nil
|
||||
}
|
||||
|
||||
respHdr := dns.RR_Header{
|
||||
Name: targ,
|
||||
Rrtype: qt,
|
||||
Class: dns.ClassINET,
|
||||
Ttl: 60,
|
||||
}
|
||||
|
||||
resp = new(dns.Msg).SetReply(req)
|
||||
switch qt {
|
||||
case dns.TypeA:
|
||||
resp.Answer = mustAnsA(respHdr, answer)
|
||||
case dns.TypeAAAA:
|
||||
resp.Answer = mustAnsAAAA(respHdr, answer)
|
||||
case dns.TypePTR:
|
||||
resp.Answer = []dns.RR{&dns.PTR{
|
||||
Hdr: respHdr,
|
||||
Ptr: answer,
|
||||
}}
|
||||
default:
|
||||
panic(fmt.Errorf("aghtest: bad question type: %s", dns.Type(qt)))
|
||||
}
|
||||
|
||||
return resp
|
||||
}
|
||||
|
||||
// mustAnsA returns valid answer records if s is a valid IPv4 address.
|
||||
// Otherwise, mustAnsA panics.
|
||||
func mustAnsA(respHdr dns.RR_Header, s string) (ans []dns.RR) {
|
||||
ip, err := netip.ParseAddr(s)
|
||||
if err != nil || !ip.Is4() {
|
||||
panic(fmt.Errorf("aghtest: bad A answer: %+v", s))
|
||||
}
|
||||
|
||||
return []dns.RR{&dns.A{
|
||||
Hdr: respHdr,
|
||||
A: ip.AsSlice(),
|
||||
}}
|
||||
}
|
||||
|
||||
// mustAnsAAAA returns valid answer records if s is a valid IPv6 address.
|
||||
// Otherwise, mustAnsAAAA panics.
|
||||
func mustAnsAAAA(respHdr dns.RR_Header, s string) (ans []dns.RR) {
|
||||
ip, err := netip.ParseAddr(s)
|
||||
if err != nil || !ip.Is6() {
|
||||
panic(fmt.Errorf("aghtest: bad AAAA answer: %+v", s))
|
||||
}
|
||||
|
||||
return []dns.RR{&dns.AAAA{
|
||||
Hdr: respHdr,
|
||||
AAAA: ip.AsSlice(),
|
||||
}}
|
||||
}
|
||||
|
||||
// NewUpstreamMock returns an [*UpstreamMock], fields OnAddress and OnClose of
|
||||
// which are set to stubs that return "upstream.example" and nil respectively.
|
||||
// The field OnExchange is set to onExc.
|
||||
func NewUpstreamMock(onExc func(req *dns.Msg) (resp *dns.Msg, err error)) (u *UpstreamMock) {
|
||||
return &UpstreamMock{
|
||||
OnAddress: func() (addr string) { return "upstream.example" },
|
||||
OnExchange: onExc,
|
||||
OnClose: func() (err error) { return nil },
|
||||
}
|
||||
}
|
||||
|
||||
// NewBlockUpstream returns an [*UpstreamMock] that works like an upstream that
|
||||
@@ -144,9 +198,7 @@ func NewBlockUpstream(hostname string, shouldBlock bool) (u *UpstreamMock) {
|
||||
}
|
||||
|
||||
return &UpstreamMock{
|
||||
OnAddress: func() (addr string) {
|
||||
return "sbpc.upstream.example"
|
||||
},
|
||||
OnAddress: func() (addr string) { return "sbpc.upstream.example" },
|
||||
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
|
||||
resp = respTmpl.Copy()
|
||||
resp.SetReply(req)
|
||||
@@ -154,6 +206,7 @@ func NewBlockUpstream(hostname string, shouldBlock bool) (u *UpstreamMock) {
|
||||
|
||||
return resp, nil
|
||||
},
|
||||
OnClose: func() (err error) { return nil },
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,11 +218,10 @@ const ErrUpstream errors.Error = "test upstream error"
|
||||
// its Exchange method.
|
||||
func NewErrorUpstream() (u *UpstreamMock) {
|
||||
return &UpstreamMock{
|
||||
OnAddress: func() (addr string) {
|
||||
return "error.upstream.example"
|
||||
},
|
||||
OnAddress: func() (addr string) { return "error.upstream.example" },
|
||||
OnExchange: func(_ *dns.Msg) (resp *dns.Msg, err error) {
|
||||
return nil, errors.Error("test upstream error")
|
||||
},
|
||||
OnClose: func() (err error) { return nil },
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user