Pull request 1943: 6046 Local PTR

Merge in DNS/adguard-home from 6046-local-ptr to master

Updates #6046.

Squashed commit of the following:

commit 3e90815f29173d2f68970278bd7b1b29cc0a4465
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Jul 27 18:17:41 2023 +0300

    all: log changes

commit 7639f6f785670c15911fb3ca20abeb4e2b8f8582
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Jul 27 17:40:49 2023 +0300

    all: fix 0 ttl ptr
This commit is contained in:
Eugene Burkov
2023-07-27 18:23:23 +03:00
parent 5f8fa006cf
commit 300821a7fb
6 changed files with 94 additions and 18 deletions

View File

@@ -101,6 +101,8 @@ func (r *Default) Process(ip netip.Addr) (host string, changed bool) {
log.Debug("rdns: cache: adding item %q: %s", ip, err)
}
// TODO(e.burkov): The name doesn't change if it's neither stored in cache
// nor resolved successfully. Is it correct?
return host, fromCache == "" || host != fromCache
}

View File

@@ -25,11 +25,6 @@ func TestDefault_Process(t *testing.T) {
localRevAddr1, err := netutil.IPToReversedAddr(localIP.AsSlice())
require.NoError(t, err)
config := &rdns.Config{
CacheSize: 100,
CacheTTL: time.Hour,
}
testCases := []struct {
name string
addr netip.Addr
@@ -60,21 +55,21 @@ func TestDefault_Process(t *testing.T) {
switch ip {
case ip1:
return revAddr1, 0, nil
return revAddr1, time.Hour, nil
case ip2:
return revAddr2, 0, nil
return revAddr2, time.Hour, nil
case localIP:
return localRevAddr1, 0, nil
return localRevAddr1, time.Hour, nil
default:
return "", 0, nil
return "", time.Hour, nil
}
}
exchanger := &aghtest.Exchanger{
OnExchange: onExchange,
}
config.Exchanger = exchanger
r := rdns.New(config)
r := rdns.New(&rdns.Config{
CacheSize: 100,
CacheTTL: time.Hour,
Exchanger: &aghtest.Exchanger{OnExchange: onExchange},
})
got, changed := r.Process(tc.addr)
require.True(t, changed)
@@ -90,4 +85,40 @@ func TestDefault_Process(t *testing.T) {
assert.Equal(t, 1, hit)
})
}
t.Run("zero_ttl", func(t *testing.T) {
const cacheTTL = time.Second / 2
zeroTTLExchanger := &aghtest.Exchanger{
OnExchange: func(ip netip.Addr) (host string, ttl time.Duration, err error) {
return revAddr1, 0, nil
},
}
r := rdns.New(&rdns.Config{
CacheSize: 1,
CacheTTL: cacheTTL,
Exchanger: zeroTTLExchanger,
})
got, changed := r.Process(ip1)
require.True(t, changed)
assert.Equal(t, revAddr1, got)
zeroTTLExchanger.OnExchange = func(ip netip.Addr) (host string, ttl time.Duration, err error) {
return revAddr2, time.Hour, nil
}
require.EventuallyWithT(t, func(t *assert.CollectT) {
got, changed = r.Process(ip1)
assert.True(t, changed)
assert.Equal(t, revAddr2, got)
}, 2*cacheTTL, time.Millisecond*100)
assert.Never(t, func() (changed bool) {
_, changed = r.Process(ip1)
return changed
}, 2*cacheTTL, time.Millisecond*100)
})
}