Skip to content

Commit e6e0ae0

Browse files
authored
AGDNS-4409 Imp upstream tests
1 parent 73f4766 commit e6e0ae0

18 files changed

Lines changed: 255 additions & 172 deletions

dnsproxytest/dnsproxytest.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package dnsproxytest provides test utilities and mock implementations
2+
// for the dnsproxy module interfaces.
3+
package dnsproxytest
Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package dnsproxytest
22

33
import (
4-
"github.com/AdguardTeam/dnsproxy/internal/dnsmsg"
4+
"context"
5+
6+
"github.com/AdguardTeam/dnsproxy/proxy"
57
"github.com/AdguardTeam/dnsproxy/upstream"
68
"github.com/AdguardTeam/golibs/testutil"
79
"github.com/miekg/dns"
10+
"github.com/quic-go/quic-go"
11+
"github.com/quic-go/quic-go/qlogwriter"
812
)
913

1014
// Upstream is a mock [upstream.Upstream] implementation for tests.
11-
//
12-
// TODO(e.burkov): Move to golibs.
1315
type Upstream struct {
1416
OnAddress func() (addr string)
1517
OnExchange func(req *dns.Msg) (resp *dns.Msg, err error)
@@ -34,7 +36,55 @@ func (u *Upstream) Close() (err error) {
3436
return u.OnClose()
3537
}
3638

37-
// MessageConstructor is a mock [dnsmsg.MessageConstructor] implementation for
39+
// Handler is a mock [proxy.Handler] implementation for tests.
40+
type Handler struct {
41+
OnHandle func(ctx context.Context, p *proxy.Proxy, dctx *proxy.DNSContext) (err error)
42+
}
43+
44+
// type check
45+
var _ proxy.Handler = (*Handler)(nil)
46+
47+
// ServeDNS implements the [proxy.Handler] interface for *Handler.
48+
func (h *Handler) ServeDNS(ctx context.Context, p *proxy.Proxy, dctx *proxy.DNSContext) (err error) {
49+
return h.OnHandle(ctx, p, dctx)
50+
}
51+
52+
// Middleware is a mock [proxy.Middleware] implementation for tests.
53+
type Middleware struct {
54+
OnWrap func(h proxy.Handler) (wrapped proxy.Handler)
55+
}
56+
57+
// type check
58+
var _ proxy.Middleware = (*Middleware)(nil)
59+
60+
// Wrap implements the [proxy.Middleware] interface for *Middleware.
61+
func (m *Middleware) Wrap(h proxy.Handler) (wrapped proxy.Handler) {
62+
return m.OnWrap(h)
63+
}
64+
65+
// QUICTracer is a mock [upstream.QUICTracer] implementation for tests.
66+
type QUICTracer struct {
67+
OnTraceForConnection func(
68+
ctx context.Context,
69+
isClient bool,
70+
connID quic.ConnectionID,
71+
) (trace qlogwriter.Trace)
72+
}
73+
74+
// type check
75+
var _ upstream.QUICTracer = (*QUICTracer)(nil)
76+
77+
// TraceForConnection implements the [upstream.QUICTracer] interface for
78+
// *QUICTracer.
79+
func (t *QUICTracer) TraceForConnection(
80+
ctx context.Context,
81+
isClient bool,
82+
connID quic.ConnectionID,
83+
) (trace qlogwriter.Trace) {
84+
return t.OnTraceForConnection(ctx, isClient, connID)
85+
}
86+
87+
// MessageConstructor is a mock [proxy.MessageConstructor] implementation for
3888
// tests.
3989
type MessageConstructor struct {
4090
OnNewMsgNXDOMAIN func(req *dns.Msg) (resp *dns.Msg)
@@ -44,8 +94,8 @@ type MessageConstructor struct {
4494
OnNewMsgFORMERR func(req *dns.Msg) (resp *dns.Msg)
4595
}
4696

47-
// NewMessageConstructor creates a new *TestMessageConstructor with all it's
48-
// methods set to panic.
97+
// NewMessageConstructor creates a new *MessageConstructor with all its methods
98+
// set to panic.
4999
func NewMessageConstructor() (c *MessageConstructor) {
50100
return &MessageConstructor{
51101
OnNewMsgNXDOMAIN: func(req *dns.Msg) (_ *dns.Msg) {
@@ -67,7 +117,7 @@ func NewMessageConstructor() (c *MessageConstructor) {
67117
}
68118

69119
// type check
70-
var _ dnsmsg.MessageConstructor = (*MessageConstructor)(nil)
120+
var _ proxy.MessageConstructor = (*MessageConstructor)(nil)
71121

72122
// NewMsgNXDOMAIN implements the [proxy.MessageConstructor] interface for
73123
// *MessageConstructor.

internal/dnsproxytest/dnsproxytest.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Package dnsproxytest provides a set of test utilities for the dnsproxy
2-
// module.
1+
// Package dnsproxytest provides test utilities and mock implementations
2+
// for the dnsproxy module interfaces.
33
package dnsproxytest
44

55
import (
@@ -15,6 +15,8 @@ import (
1515
// that can be used for testing. Note that there is theoretically a TOCTTOU
1616
// race here: the port may be reoccupied between the time it is released and the
1717
// time the caller binds to it.
18+
//
19+
// TODO(m.kazantsev): Move to the top-level dnsproxytest package.
1820
func NewFreePort(tb testing.TB) (p uint) {
1921
tb.Helper()
2022

internal/middleware/middleware_internal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"testing"
99
"time"
1010

11+
"github.com/AdguardTeam/dnsproxy/dnsproxytest"
1112
"github.com/AdguardTeam/dnsproxy/internal/dnsmsg"
12-
"github.com/AdguardTeam/dnsproxy/internal/dnsproxytest"
1313
"github.com/AdguardTeam/golibs/errors"
1414
"github.com/AdguardTeam/golibs/logutil/slogutil"
1515
"github.com/AdguardTeam/golibs/netutil"

proxy/bogusnxdomain_internal_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ import (
1414
)
1515

1616
func TestProxy_IsBogusNXDomain(t *testing.T) {
17+
var ans []dns.RR
18+
19+
onExchange := newECSReplyHandler(&ans, nil, nil)
20+
u := newTestECSUpstream(onExchange)
21+
22+
upsConf := newTestUpstreamConfig(t, defaultTimeout, testDefaultUpstreamAddr)
23+
upsConf.Upstreams = []upstream.Upstream{u}
24+
1725
prx := mustNew(t, &Config{
1826
Logger: testLogger,
1927
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(localhostAnyPort)},
2028
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(localhostAnyPort)},
21-
UpstreamConfig: newTestUpstreamConfig(t, defaultTimeout, testDefaultUpstreamAddr),
29+
UpstreamConfig: upsConf,
2230
TrustedProxies: defaultTrustedProxies,
2331
CacheEnabled: true,
2432
BogusNXDomain: []netip.Prefix{
@@ -77,19 +85,16 @@ func TestProxy_IsBogusNXDomain(t *testing.T) {
7785
wantRcode: dns.RcodeSuccess,
7886
}}
7987

80-
u := testUpstream{}
81-
prx.upstreamConf.Upstreams = []upstream.Upstream{&u}
82-
8388
servicetest.RequireRun(t, prx, testTimeout)
8489

8590
d := &DNSContext{
8691
Req: newHostTestMessage("host"),
8792
}
8893

8994
for _, tc := range testCases {
90-
u.ans = tc.ans
91-
9295
t.Run(tc.name, func(t *testing.T) {
96+
ans = tc.ans
97+
9398
err := prx.Resolve(testutil.ContextWithTimeout(t, defaultTimeout), d)
9499
require.NoError(t, err)
95100
require.NotNil(t, d.Res)

proxy/cache_internal_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"testing"
1010
"time"
1111

12-
"github.com/AdguardTeam/dnsproxy/internal/dnsproxytest"
1312
"github.com/AdguardTeam/dnsproxy/upstream"
1413
"github.com/AdguardTeam/golibs/netutil"
1514
"github.com/AdguardTeam/golibs/testutil"
@@ -28,7 +27,7 @@ const testUpsAddr = "https://upstream.address"
2827

2928
// upstreamWithAddr is a [dnsproxytest.Upstream] that is only expected to be
3029
// used to get its address.
31-
var upstreamWithAddr = &dnsproxytest.Upstream{
30+
var upstreamWithAddr = &testUpstream{
3231
OnExchange: func(m *dns.Msg) (_ *dns.Msg, _ error) { panic(testutil.UnexpectedCall(m)) },
3332
OnClose: func() (_ error) { panic(testutil.UnexpectedCall()) },
3433
OnAddress: func() (addr string) { return testUpsAddr },
@@ -361,14 +360,17 @@ func TestCacheExpiration(t *testing.T) {
361360
}
362361

363362
func TestCacheExpirationWithTTLOverride(t *testing.T) {
364-
u := testUpstream{}
363+
var ans []dns.RR
364+
365+
onExchange := newECSReplyHandler(&ans, nil, nil)
366+
u := newTestECSUpstream(onExchange)
365367

366368
dnsProxy := mustNew(t, &Config{
367369
Logger: testLogger,
368370
UDPListenAddr: []*net.UDPAddr{net.UDPAddrFromAddrPort(localhostAnyPort)},
369371
TCPListenAddr: []*net.TCPAddr{net.TCPAddrFromAddrPort(localhostAnyPort)},
370372
UpstreamConfig: &UpstreamConfig{
371-
Upstreams: []upstream.Upstream{&u},
373+
Upstreams: []upstream.Upstream{u},
372374
},
373375
TrustedProxies: defaultTrustedProxies,
374376
CacheEnabled: true,
@@ -385,7 +387,7 @@ func TestCacheExpirationWithTTLOverride(t *testing.T) {
385387
d.Req = newHostTestMessage("host")
386388
d.Addr = netip.AddrPort{}
387389

388-
u.ans = []dns.RR{&dns.A{
390+
ans = []dns.RR{&dns.A{
389391
Hdr: dns.RR_Header{
390392
Rrtype: dns.TypeA,
391393
Name: "host.",
@@ -409,7 +411,7 @@ func TestCacheExpirationWithTTLOverride(t *testing.T) {
409411
d.Req = newHostTestMessage("host2")
410412
d.Addr = netip.AddrPort{}
411413

412-
u.ans = []dns.RR{&dns.A{
414+
ans = []dns.RR{&dns.A{
413415
Hdr: dns.RR_Header{
414416
Rrtype: dns.TypeA,
415417
Name: "host2.",

proxy/dns64_internal_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"sync"
77
"testing"
88

9-
"github.com/AdguardTeam/dnsproxy/internal/dnsproxytest"
109
"github.com/AdguardTeam/dnsproxy/upstream"
1110
"github.com/AdguardTeam/golibs/netutil"
1211
"github.com/AdguardTeam/golibs/testutil"
@@ -20,7 +19,7 @@ const ipv4OnlyFqdn = "ipv4.only."
2019

2120
func TestDNS64Race(t *testing.T) {
2221
ans := newRR(t, ipv4OnlyFqdn, dns.TypeA, 3600, net.ParseIP("1.2.3.4"))
23-
ups := &dnsproxytest.Upstream{
22+
ups := &testUpstream{
2423
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
2524
resp = (&dns.Msg{}).SetReply(req)
2625
if req.Question[0].Qtype == dns.TypeA {
@@ -32,7 +31,7 @@ func TestDNS64Race(t *testing.T) {
3231
OnAddress: func() (addr string) { return "fake.address" },
3332
OnClose: func() (err error) { return nil },
3433
}
35-
localUps := &dnsproxytest.Upstream{
34+
localUps := &testUpstream{
3635
OnExchange: func(m *dns.Msg) (_ *dns.Msg, _ error) { panic(testutil.UnexpectedCall(m)) },
3736
OnAddress: func() (addr string) { return "fake.address" },
3837
OnClose: func() (err error) { return nil },
@@ -180,7 +179,7 @@ func TestProxy_Resolve_dns64(t *testing.T) {
180179

181180
pt := testutil.PanicT{}
182181
newUps := func(answers answerMap) (u upstream.Upstream) {
183-
return &dnsproxytest.Upstream{
182+
return &testUpstream{
184183
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
185184
q := req.Question[0]
186185
require.Contains(pt, answers, q.Qtype)
@@ -200,7 +199,7 @@ func TestProxy_Resolve_dns64(t *testing.T) {
200199
}
201200

202201
localRR := newRR(t, ptr64Domain, dns.TypePTR, 3600, domainPointed)
203-
localUps := &dnsproxytest.Upstream{
202+
localUps := &testUpstream{
204203
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
205204
require.Equal(pt, req.Question[0].Name, ptr64Domain)
206205
resp = (&dns.Msg{}).SetReply(req)

proxy/exchange_internal_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/AdguardTeam/dnsproxy/internal/dnsproxytest"
1211
"github.com/AdguardTeam/dnsproxy/upstream"
1312
"github.com/AdguardTeam/golibs/netutil"
1413
"github.com/AdguardTeam/golibs/testutil"
@@ -24,7 +23,7 @@ import (
2423
func newUpstreamWithErrorRate(rate uint, name string) (u upstream.Upstream) {
2524
var n uint
2625

27-
return &dnsproxytest.Upstream{
26+
return &testUpstream{
2827
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
2928
n++
3029
if n%rate == 0 {
@@ -87,7 +86,7 @@ func TestProxy_Exchange_loadBalance(t *testing.T) {
8786
},
8887
}
8988

90-
fastUps := &dnsproxytest.Upstream{
89+
fastUps := &testUpstream{
9190
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
9291
currentNow = zeroTime.Add(testRTT / 100)
9392

@@ -96,7 +95,7 @@ func TestProxy_Exchange_loadBalance(t *testing.T) {
9695
OnAddress: func() (addr string) { return "fast" },
9796
OnClose: func() (_ error) { panic(testutil.UnexpectedCall()) },
9897
}
99-
slowerUps := &dnsproxytest.Upstream{
98+
slowerUps := &testUpstream{
10099
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
101100
currentNow = zeroTime.Add(testRTT / 10)
102101

@@ -105,7 +104,7 @@ func TestProxy_Exchange_loadBalance(t *testing.T) {
105104
OnAddress: func() (addr string) { return "slower" },
106105
OnClose: func() (_ error) { panic(testutil.UnexpectedCall()) },
107106
}
108-
slowestUps := &dnsproxytest.Upstream{
107+
slowestUps := &testUpstream{
109108
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
110109
currentNow = zeroTime.Add(testRTT / 2)
111110

@@ -115,20 +114,20 @@ func TestProxy_Exchange_loadBalance(t *testing.T) {
115114
OnClose: func() (_ error) { panic(testutil.UnexpectedCall()) },
116115
}
117116

118-
err1Ups := &dnsproxytest.Upstream{
117+
err1Ups := &testUpstream{
119118
OnExchange: func(_ *dns.Msg) (r *dns.Msg, err error) { return nil, assert.AnError },
120119
OnAddress: func() (addr string) { return "error1" },
121120
OnClose: func() (_ error) { panic(testutil.UnexpectedCall()) },
122121
}
123-
err2Ups := &dnsproxytest.Upstream{
122+
err2Ups := &testUpstream{
124123
OnExchange: func(_ *dns.Msg) (r *dns.Msg, err error) { return nil, assert.AnError },
125124
OnAddress: func() (addr string) { return "error2" },
126125
OnClose: func() (_ error) { panic(testutil.UnexpectedCall()) },
127126
}
128127

129128
singleError := &sync.Once{}
130129
// fastestUps responds with an error on the first request.
131-
fastestUps := &dnsproxytest.Upstream{
130+
fastestUps := &testUpstream{
132131
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
133132
singleError.Do(func() { err = assert.AnError })
134133
currentNow = zeroTime.Add(testRTT / 200)

proxy/handler_internal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestFilteringHandler(t *testing.T) {
1717
m := &sync.RWMutex{}
1818
blockResponse := false
1919

20-
reqHandler := &TestHandler{
20+
reqHandler := &testHandler{
2121
OnHandle: func(ctx context.Context, p *Proxy, d *DNSContext) (err error) {
2222
m.Lock()
2323
defer m.Unlock()

proxy/pending_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/AdguardTeam/dnsproxy/internal/dnsproxytest"
11+
"github.com/AdguardTeam/dnsproxy/dnsproxytest"
1212
"github.com/AdguardTeam/dnsproxy/proxy"
1313
"github.com/AdguardTeam/dnsproxy/upstream"
1414
"github.com/AdguardTeam/golibs/netutil"
@@ -79,7 +79,7 @@ func TestPendingRequests(t *testing.T) {
7979
workloadWG := &sync.WaitGroup{}
8080
workloadWG.Add(reqsNum)
8181

82-
reqHandler := &proxy.TestHandler{
82+
reqHandler := &dnsproxytest.Handler{
8383
OnHandle: func(ctx context.Context, p *proxy.Proxy, d *proxy.DNSContext) (err error) {
8484
workloadWG.Done()
8585

0 commit comments

Comments
 (0)