-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull request 324: 6723 fix caching resolver
Updates AdguardTeam/AdGuardHome#6723. Squashed commit of the following: commit 9c72e49 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Feb 21 16:24:09 2024 +0300 dnsproxytest: imp code commit 6951c76 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Feb 21 14:22:42 2024 +0300 all: imp file names, add dnsproxytest commit bb5d612 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Feb 20 16:26:00 2024 +0300 upstream: fix expire commit 2232083 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Feb 20 15:40:59 2024 +0300 upstream: split internal tests commit f4d9592 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Feb 20 15:36:27 2024 +0300 upstream: add staleness cache commit d0c2995 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Feb 19 19:28:28 2024 +0300 all: fix resolver cache
- Loading branch information
1 parent
d918c7f
commit ad97a28
Showing
14 changed files
with
215 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package dnsproxytest provides a set of test utilities for the dnsproxy | ||
// module. | ||
package dnsproxytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package dnsproxytest | ||
|
||
import ( | ||
"github.com/miekg/dns" | ||
) | ||
|
||
// FakeUpstream is a fake [Upstream] implementation for tests. | ||
// | ||
// TODO(e.burkov): Move this to the golibs? | ||
type FakeUpstream struct { | ||
OnAddress func() (addr string) | ||
OnExchange func(req *dns.Msg) (resp *dns.Msg, err error) | ||
OnClose func() (err error) | ||
} | ||
|
||
// Address implements the [Upstream] interface for *FakeUpstream. | ||
func (u *FakeUpstream) Address() (addr string) { | ||
return u.OnAddress() | ||
} | ||
|
||
// Exchange implements the [Upstream] interface for *FakeUpstream. | ||
func (u *FakeUpstream) Exchange(req *dns.Msg) (resp *dns.Msg, err error) { | ||
return u.OnExchange(req) | ||
} | ||
|
||
// Close implements the [Upstream] interface for *FakeUpstream. | ||
func (u *FakeUpstream) Close() (err error) { | ||
return u.OnClose() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dnsproxytest_test | ||
|
||
import ( | ||
"github.com/AdguardTeam/dnsproxy/internal/dnsproxytest" | ||
"github.com/AdguardTeam/dnsproxy/upstream" | ||
) | ||
|
||
// type check | ||
var _ upstream.Upstream = (*dnsproxytest.FakeUpstream)(nil) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package upstream | ||
|
||
import ( | ||
"context" | ||
"net/netip" | ||
"testing" | ||
"time" | ||
|
||
"github.com/AdguardTeam/dnsproxy/internal/bootstrap" | ||
"github.com/AdguardTeam/dnsproxy/internal/dnsproxytest" | ||
"github.com/AdguardTeam/golibs/testutil" | ||
"github.com/miekg/dns" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCachingResolver_staleness(t *testing.T) { | ||
ip4 := netip.MustParseAddr("1.2.3.4") | ||
ip6 := netip.MustParseAddr("2001:db8::1") | ||
|
||
const ( | ||
smallTTL = 10 * time.Second | ||
largeTTL = 1000 * time.Second | ||
|
||
fqdn = "test.fully.qualified.name." | ||
) | ||
|
||
onExchange := func(req *dns.Msg) (resp *dns.Msg, err error) { | ||
resp = (&dns.Msg{}).SetReply(req) | ||
|
||
hdr := dns.RR_Header{ | ||
Name: req.Question[0].Name, | ||
Rrtype: req.Question[0].Qtype, | ||
Class: dns.ClassINET, | ||
} | ||
var rr dns.RR | ||
switch q := req.Question[0]; q.Qtype { | ||
case dns.TypeA: | ||
hdr.Ttl = uint32(smallTTL.Seconds()) | ||
rr = &dns.A{Hdr: hdr, A: ip4.AsSlice()} | ||
case dns.TypeAAAA: | ||
hdr.Ttl = uint32(largeTTL.Seconds()) | ||
rr = &dns.AAAA{Hdr: hdr, AAAA: ip6.AsSlice()} | ||
default: | ||
require.Contains(testutil.PanicT{}, []uint16{dns.TypeA, dns.TypeAAAA}, q.Qtype) | ||
} | ||
resp.Answer = append(resp.Answer, rr) | ||
|
||
return resp, nil | ||
} | ||
|
||
ups := &dnsproxytest.FakeUpstream{ | ||
OnAddress: func() (_ string) { panic("not implemented") }, | ||
OnClose: func() (_ error) { panic("not implemented") }, | ||
OnExchange: onExchange, | ||
} | ||
|
||
r := NewCachingResolver(&UpstreamResolver{Upstream: ups}) | ||
|
||
require.True(t, t.Run("resolve", func(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
network bootstrap.Network | ||
want []netip.Addr | ||
}{{ | ||
name: "ip4", | ||
network: bootstrap.NetworkIP4, | ||
want: []netip.Addr{ip4}, | ||
}, { | ||
name: "ip6", | ||
network: bootstrap.NetworkIP6, | ||
want: []netip.Addr{ip6}, | ||
}, { | ||
name: "both", | ||
network: bootstrap.NetworkIP, | ||
want: []netip.Addr{ip4, ip6}, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
if tc.name != "both" { | ||
t.Skip(`TODO(e.burkov): Bootstrap now only uses "ip" network, see TODO there.`) | ||
} | ||
|
||
res, err := r.LookupNetIP(context.Background(), tc.network, fqdn) | ||
require.NoError(t, err) | ||
|
||
assert.ElementsMatch(t, tc.want, res) | ||
}) | ||
} | ||
})) | ||
|
||
t.Run("staleness", func(t *testing.T) { | ||
cached := r.findCached(fqdn, time.Now()) | ||
require.ElementsMatch(t, []netip.Addr{ip4, ip6}, cached) | ||
|
||
cached = r.findCached(fqdn, time.Now().Add(smallTTL)) | ||
require.Empty(t, cached) | ||
}) | ||
} |
Oops, something went wrong.