2023-07-06 17:10:06 +03:00
|
|
|
package rdns_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/netip"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-07-18 14:02:32 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
2023-07-06 17:10:06 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/rdns"
|
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2024-08-29 16:20:05 +03:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2023-07-06 17:10:06 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2024-08-29 16:20:05 +03:00
|
|
|
// testTimeout is a common timeout for tests and contexts.
|
|
|
|
const testTimeout = 1 * time.Second
|
|
|
|
|
2023-07-06 17:10:06 +03:00
|
|
|
func TestDefault_Process(t *testing.T) {
|
|
|
|
ip1 := netip.MustParseAddr("1.2.3.4")
|
|
|
|
revAddr1, err := netutil.IPToReversedAddr(ip1.AsSlice())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ip2 := netip.MustParseAddr("4.3.2.1")
|
|
|
|
revAddr2, err := netutil.IPToReversedAddr(ip2.AsSlice())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
localIP := netip.MustParseAddr("192.168.0.1")
|
|
|
|
localRevAddr1, err := netutil.IPToReversedAddr(localIP.AsSlice())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
addr netip.Addr
|
|
|
|
want string
|
|
|
|
}{{
|
|
|
|
name: "first",
|
|
|
|
addr: ip1,
|
|
|
|
want: revAddr1,
|
|
|
|
}, {
|
|
|
|
name: "second",
|
|
|
|
addr: ip2,
|
|
|
|
want: revAddr2,
|
|
|
|
}, {
|
|
|
|
name: "empty",
|
|
|
|
addr: netip.MustParseAddr("0.0.0.0"),
|
|
|
|
want: "",
|
|
|
|
}, {
|
|
|
|
name: "private",
|
|
|
|
addr: localIP,
|
|
|
|
want: localRevAddr1,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
hit := 0
|
2023-07-25 14:16:26 +03:00
|
|
|
onExchange := func(ip netip.Addr) (host string, ttl time.Duration, err error) {
|
2023-07-06 17:10:06 +03:00
|
|
|
hit++
|
|
|
|
|
|
|
|
switch ip {
|
|
|
|
case ip1:
|
2023-07-27 18:23:23 +03:00
|
|
|
return revAddr1, time.Hour, nil
|
2023-07-06 17:10:06 +03:00
|
|
|
case ip2:
|
2023-07-27 18:23:23 +03:00
|
|
|
return revAddr2, time.Hour, nil
|
2023-07-06 17:10:06 +03:00
|
|
|
case localIP:
|
2023-07-27 18:23:23 +03:00
|
|
|
return localRevAddr1, time.Hour, nil
|
2023-07-06 17:10:06 +03:00
|
|
|
default:
|
2023-07-27 18:23:23 +03:00
|
|
|
return "", time.Hour, nil
|
2023-07-06 17:10:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:23:23 +03:00
|
|
|
r := rdns.New(&rdns.Config{
|
|
|
|
CacheSize: 100,
|
|
|
|
CacheTTL: time.Hour,
|
|
|
|
Exchanger: &aghtest.Exchanger{OnExchange: onExchange},
|
|
|
|
})
|
2023-07-06 17:10:06 +03:00
|
|
|
|
2024-08-29 16:20:05 +03:00
|
|
|
got, changed := r.Process(testutil.ContextWithTimeout(t, testTimeout), tc.addr)
|
2023-07-06 17:10:06 +03:00
|
|
|
require.True(t, changed)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, got)
|
|
|
|
assert.Equal(t, 1, hit)
|
|
|
|
|
|
|
|
// From cache.
|
2024-08-29 16:20:05 +03:00
|
|
|
got, changed = r.Process(testutil.ContextWithTimeout(t, testTimeout), tc.addr)
|
2023-07-06 17:10:06 +03:00
|
|
|
require.False(t, changed)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, got)
|
|
|
|
assert.Equal(t, 1, hit)
|
|
|
|
})
|
|
|
|
}
|
2023-07-27 18:23:23 +03:00
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
2024-08-29 16:20:05 +03:00
|
|
|
got, changed := r.Process(testutil.ContextWithTimeout(t, testTimeout), ip1)
|
2023-07-27 18:23:23 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-29 16:20:05 +03:00
|
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
2023-07-27 18:23:23 +03:00
|
|
|
require.EventuallyWithT(t, func(t *assert.CollectT) {
|
2024-08-29 16:20:05 +03:00
|
|
|
got, changed = r.Process(ctx, ip1)
|
2023-07-27 18:23:23 +03:00
|
|
|
assert.True(t, changed)
|
|
|
|
assert.Equal(t, revAddr2, got)
|
|
|
|
}, 2*cacheTTL, time.Millisecond*100)
|
|
|
|
|
|
|
|
assert.Never(t, func() (changed bool) {
|
2024-08-29 16:20:05 +03:00
|
|
|
_, changed = r.Process(testutil.ContextWithTimeout(t, testTimeout), ip1)
|
2023-07-27 18:23:23 +03:00
|
|
|
|
|
|
|
return changed
|
|
|
|
}, 2*cacheTTL, time.Millisecond*100)
|
|
|
|
})
|
2023-07-06 17:10:06 +03:00
|
|
|
}
|