mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-28 01:42:26 +03:00
a91a257b15
Merge in DNS/adguard-home from AG-20352-imp-leases-db to master Squashed commit of the following: commit 2235fb4671bb3f80c933847362cd35b5704dd18d Merge: 0c4d76d4f76a74b271
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 18 15:09:34 2023 +0300 Merge branch 'master' into AG-20352-imp-leases-db commit 0c4d76d4f6222ae06c568864d366df866dc55a54 Merge: e586b82c74afd39b22
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 18 11:07:27 2023 +0300 Merge branch 'master' into AG-20352-imp-leases-db commit e586b82c700c4d432e34f36400519eb08b2653ad Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 18 11:06:40 2023 +0300 dhcpd: imp docs commit 411d4e6f6e36051bf6a66c709380ed268c161c41 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 17 16:56:56 2023 +0300 dhcpd: imp code commit e457dc2c385ab62b36df7f96c949e6b90ed2034a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 17 14:29:29 2023 +0300 all: imp code more commit c2df20d0125d368d0155af0808af979921763e1f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 14 15:07:53 2023 +0300 all: imp code commit a4e9ffb9ae769c828c22d62ddf231f7bcfea14db Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 12 19:19:35 2023 +0300 dhcpd: fix test more commit 138d89414f1a89558b23962acb7174dce28346d9 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 12 18:08:29 2023 +0300 dhcpd: fix test commit e07e7a23e7c913951c8ecb38c12a3345ebe473be Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 12 17:22:27 2023 +0300 all: upd chlog commit 1b6a76e79cf4beed9ca980766ce97930b375bfde Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 12 13:24:11 2023 +0300 all: migrate leases db
138 lines
3.4 KiB
Go
138 lines
3.4 KiB
Go
//go:build darwin || freebsd || linux || openbsd
|
|
|
|
package dhcpd
|
|
|
|
import (
|
|
"net"
|
|
"net/netip"
|
|
"path/filepath"
|
|
"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: filepath.Join(t.TempDir(), dataFilename),
|
|
},
|
|
}
|
|
|
|
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)
|
|
|
|
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[0].HWAddr, ll[0].HWAddr)
|
|
assert.Equal(t, leases[0].IP, ll[0].IP)
|
|
assert.Equal(t, leases[0].Expiry.Unix(), ll[0].Expiry.Unix())
|
|
|
|
assert.Equal(t, leases[1].HWAddr, ll[1].HWAddr)
|
|
assert.Equal(t, leases[1].IP, ll[1].IP)
|
|
assert.True(t, ll[1].IsStatic)
|
|
}
|
|
|
|
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,
|
|
}
|
|
}
|