AdGuardHome/internal/rdns/rdns_test.go

106 lines
2.2 KiB
Go
Raw Normal View History

Pull request 1878: AG-22597-imp-rdns Squashed commit of the following: commit ccad155c34989943d88a0a260c50845d1f4ece6b Merge: 0cd889f6a 5a195b441 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 6 17:00:58 2023 +0300 Merge branch 'master' into AG-22597-imp-rdns commit 0cd889f6a500f5616af0f8d8fdcde0403b87ad4f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 6 12:20:49 2023 +0300 dnsforward: imp code commit 1aaa1998b914b0d53142c21fa3bdcae502e4f3f6 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Jul 4 20:11:55 2023 +0300 home: add todo commit aed232fcf70ef546f373d5235b73abcb4fbb4b6c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Jul 4 13:25:28 2023 +0300 all: imp code, tests commit 5c028c2766ffb8ebdc358a245a249c6a55d9ad81 Merge: 83d6ae7f6 97af062f7 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Jul 3 18:54:42 2023 +0300 Merge branch 'master' into AG-22597-imp-rdns commit 83d6ae7f61a7b81a8d73cd6d747035278c64fb70 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Jul 3 18:53:05 2023 +0300 home: imp code commit 8153988dece0406e51a90a43eaffae59dba30a36 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jun 30 18:06:09 2023 +0300 all: imp code commit 00d3cc11a9378318f176aae00ddf972f255d575c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jun 30 13:05:04 2023 +0300 all: add tests commit ffdc95f237bfdb780922b4390d82cdc0154b0621 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jun 29 15:20:00 2023 +0300 all: imp code, docs commit 0dc60e2b355750ca701558927d22fb9ad187ea7e Merge: 69dd56bdb d4a4bda64 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jun 29 15:13:19 2023 +0300 Merge branch 'master' into AG-22597-imp-rdns commit 69dd56bdb75056b0fa6bcf6538af7fff93383323 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jun 23 14:36:29 2023 +0300 rdns: add tests commit 16909b51adbe3a3f230291834cc9486dd8a0e8f8 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Jun 19 16:28:26 2023 +0300 rdns: extract rdns
2023-07-06 15:10:06 +01:00
package rdns_test
import (
"net/netip"
"testing"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/rdns"
"github.com/AdguardTeam/golibs/netutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// fakeRDNSExchanger is a mock [rdns.Exchanger] implementation for tests.
type fakeRDNSExchanger struct {
OnExchange func(ip netip.Addr) (host string, err error)
}
// type check
var _ rdns.Exchanger = (*fakeRDNSExchanger)(nil)
// Exchange implements [rdns.Exchanger] interface for *fakeRDNSExchanger.
func (e *fakeRDNSExchanger) Exchange(ip netip.Addr) (host string, err error) {
return e.OnExchange(ip)
}
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)
config := &rdns.Config{
CacheSize: 100,
CacheTTL: time.Hour,
}
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
onExchange := func(ip netip.Addr) (host string, err error) {
hit++
switch ip {
case ip1:
return revAddr1, nil
case ip2:
return revAddr2, nil
case localIP:
return localRevAddr1, nil
default:
return "", nil
}
}
exchanger := &fakeRDNSExchanger{
OnExchange: onExchange,
}
config.Exchanger = exchanger
r := rdns.New(config)
got, changed := r.Process(tc.addr)
require.True(t, changed)
assert.Equal(t, tc.want, got)
assert.Equal(t, 1, hit)
// From cache.
got, changed = r.Process(tc.addr)
require.False(t, changed)
assert.Equal(t, tc.want, got)
assert.Equal(t, 1, hit)
})
}
}