mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-25 06:25:44 +03:00
bea39934bd
Merge in DNS/adguard-home from AG-20352-dhcpd-lease-netip-addr to master Squashed commit of the following: commit 4acd094e2d6ed972bac99cdb671670f6d8e61721 Merge: 51f61c19df61741f
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Mar 23 16:44:17 2023 +0300 Merge branch 'master' into AG-20352-dhcpd-lease-netip-addr commit 51f61c193fdd31ee675be5598fc361228e407eb3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 22 18:25:30 2023 +0300 dhcpd: fix typo commit 2e64ad55475957925d2a3010c649e0adc5f18c4f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 22 17:20:31 2023 +0300 dhcpd: add todo commit 668d4f62fd2c5b2e168025bf0b6bb36d7b617c80 Merge: 0020006e306c1983
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 22 15:09:38 2023 +0300 Merge branch 'master' into AG-20352-dhcpd-lease-netip-addr commit 0020006e89f336dc58db1a2ca3ce90d2e7a5ca16 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 22 15:08:58 2023 +0300 all: imp code commit 9a77f79869cdbde6de760734c0d8cf504e0464ef Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 20 18:13:35 2023 +0300 dhcpd: add todo commit 638c4ce2af72235bf065a6492d58f3f1b1e7644a Merge: c82b18f148431f8b
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 20 13:57:19 2023 +0300 Merge branch 'master' into AG-20352-dhcpd-lease-netip-addr commit c82b18f1408d9e353aec517f4283e2f3eb260890 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Mar 20 13:56:31 2023 +0300 all: imp code commit 27e518120024103c292ac1cf134c6801fffc967e Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 17 12:14:02 2023 +0300 dhcpd: imp tests commit 8e919b0ceb0b20d1935587e717c7965cd8a33ad9 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 17 11:02:50 2023 +0300 dhcpd: add tests commit 78ddefa73a255509af1c788147d6b1c332bb66ba Merge: c68e85c49f7a582d
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Mar 16 14:15:24 2023 +0300 Merge branch 'master' into AG-20352-dhcpd-lease-netip-addr commit c68e85c40947b6c83516424a2bd7af89b99447b4 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Mar 16 14:14:43 2023 +0300 all: add tests commit f338086309a68c4b71036fa14f757e39358702d4 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 15 12:44:06 2023 +0300 all: dhcpd lease netip addr
163 lines
4 KiB
Go
163 lines
4 KiB
Go
//go:build darwin || freebsd || linux || openbsd
|
|
|
|
package dhcpd
|
|
|
|
import (
|
|
"net"
|
|
"net/netip"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
testutil.DiscardLogOutput(m)
|
|
}
|
|
|
|
func testNotify(flags uint32) {
|
|
}
|
|
|
|
// Leases database store/load.
|
|
func TestDB(t *testing.T) {
|
|
var err error
|
|
s := server{
|
|
conf: &ServerConfig{
|
|
DBFilePath: dbFilename,
|
|
},
|
|
}
|
|
|
|
s.srv4, err = v4Create(&V4ServerConf{
|
|
Enabled: true,
|
|
RangeStart: netip.MustParseAddr("192.168.10.100"),
|
|
RangeEnd: netip.MustParseAddr("192.168.10.200"),
|
|
GatewayIP: netip.MustParseAddr("192.168.10.1"),
|
|
SubnetMask: netip.MustParseAddr("255.255.255.0"),
|
|
notify: testNotify,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
s.srv6, err = v6Create(V6ServerConf{})
|
|
require.NoError(t, err)
|
|
|
|
leases := []*Lease{{
|
|
Expiry: time.Now().Add(time.Hour),
|
|
Hostname: "static-1.local",
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
IP: netip.MustParseAddr("192.168.10.100"),
|
|
}, {
|
|
Hostname: "static-2.local",
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xBB},
|
|
IP: netip.MustParseAddr("192.168.10.101"),
|
|
}}
|
|
|
|
srv4, ok := s.srv4.(*v4Server)
|
|
require.True(t, ok)
|
|
|
|
err = srv4.addLease(leases[0])
|
|
require.NoError(t, err)
|
|
|
|
err = s.srv4.AddStaticLease(leases[1])
|
|
require.NoError(t, err)
|
|
|
|
err = s.dbStore()
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, func() (err error) { return os.Remove(dbFilename) })
|
|
|
|
err = s.srv4.ResetLeases(nil)
|
|
require.NoError(t, err)
|
|
|
|
err = s.dbLoad()
|
|
require.NoError(t, err)
|
|
|
|
ll := s.srv4.GetLeases(LeasesAll)
|
|
require.Len(t, ll, len(leases))
|
|
|
|
assert.Equal(t, leases[1].HWAddr, ll[0].HWAddr)
|
|
assert.Equal(t, leases[1].IP, ll[0].IP)
|
|
assert.True(t, ll[0].IsStatic())
|
|
|
|
assert.Equal(t, leases[0].HWAddr, ll[1].HWAddr)
|
|
assert.Equal(t, leases[0].IP, ll[1].IP)
|
|
assert.Equal(t, leases[0].Expiry.Unix(), ll[1].Expiry.Unix())
|
|
}
|
|
|
|
func TestNormalizeLeases(t *testing.T) {
|
|
dynLeases := []*Lease{{
|
|
HWAddr: net.HardwareAddr{1, 2, 3, 4},
|
|
}, {
|
|
HWAddr: net.HardwareAddr{1, 2, 3, 5},
|
|
}}
|
|
|
|
staticLeases := []*Lease{{
|
|
HWAddr: net.HardwareAddr{1, 2, 3, 4},
|
|
IP: netip.MustParseAddr("0.2.3.4"),
|
|
}, {
|
|
HWAddr: net.HardwareAddr{2, 2, 3, 4},
|
|
}}
|
|
|
|
leases := normalizeLeases(staticLeases, dynLeases)
|
|
require.Len(t, leases, 3)
|
|
|
|
assert.Equal(t, leases[0].HWAddr, dynLeases[0].HWAddr)
|
|
assert.Equal(t, leases[0].IP, staticLeases[0].IP)
|
|
assert.Equal(t, leases[1].HWAddr, staticLeases[1].HWAddr)
|
|
assert.Equal(t, leases[2].HWAddr, dynLeases[1].HWAddr)
|
|
}
|
|
|
|
func TestV4Server_badRange(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
gatewayIP netip.Addr
|
|
subnetMask netip.Addr
|
|
wantErrMsg string
|
|
}{{
|
|
name: "gateway_in_range",
|
|
gatewayIP: netip.MustParseAddr("192.168.10.120"),
|
|
subnetMask: netip.MustParseAddr("255.255.255.0"),
|
|
wantErrMsg: "dhcpv4: gateway ip 192.168.10.120 in the ip range: " +
|
|
"192.168.10.20-192.168.10.200",
|
|
}, {
|
|
name: "outside_range_start",
|
|
gatewayIP: netip.MustParseAddr("192.168.10.1"),
|
|
subnetMask: netip.MustParseAddr("255.255.255.240"),
|
|
wantErrMsg: "dhcpv4: range start 192.168.10.20 is outside network " +
|
|
"192.168.10.1/28",
|
|
}, {
|
|
name: "outside_range_end",
|
|
gatewayIP: netip.MustParseAddr("192.168.10.1"),
|
|
subnetMask: netip.MustParseAddr("255.255.255.224"),
|
|
wantErrMsg: "dhcpv4: range end 192.168.10.200 is outside network " +
|
|
"192.168.10.1/27",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
conf := V4ServerConf{
|
|
Enabled: true,
|
|
RangeStart: netip.MustParseAddr("192.168.10.20"),
|
|
RangeEnd: netip.MustParseAddr("192.168.10.200"),
|
|
GatewayIP: tc.gatewayIP,
|
|
SubnetMask: tc.subnetMask,
|
|
notify: testNotify,
|
|
}
|
|
|
|
_, err := v4Create(&conf)
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
})
|
|
}
|
|
}
|
|
|
|
// cloneUDPAddr returns a deep copy of a.
|
|
func cloneUDPAddr(a *net.UDPAddr) (clone *net.UDPAddr) {
|
|
return &net.UDPAddr{
|
|
IP: slices.Clone(a.IP),
|
|
Port: a.Port,
|
|
Zone: a.Zone,
|
|
}
|
|
}
|