Merge in DNS/adguard-home from AGDNS-2750-find-client to master Squashed commit of the following: commit 98f1a8ca4622b6f502a5092273b9724203fe0bd8 Merge: 9270222d84ccc2a213Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 23 17:53:20 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 9270222d8e9e03038e9434b54496cbb6164463cd Merge: 6468ceec8c7c62ad3bAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 21 19:40:58 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 6468ceec82d30084771a53ff6720a8c11c68bf2f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 21 19:40:52 2025 +0300 home: imp docs commit 3fd4735a0d6db4fdf2d46f3da9794a687fdcaa8b Merge: 1311a5869a8fdf1c55Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 18 19:43:36 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 1311a58695de00f20c9704378ee6e964a44d1c59 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 18 19:42:41 2025 +0300 home: imp code commit b1f2c4c883c9476c5135140abac31f8ae6609b4f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 16 16:47:59 2025 +0300 home: imp code commit d0a5abd66587c1ad602c2ccf6c8a45a3dfe39a5c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 15 14:58:31 2025 +0300 client: imp naming commit 5accdca325551237f003f1c416891b488fe5290b Merge: 6a00232f74d258972dAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 14 19:40:40 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 6a00232f76a0fe5ce781aa01637b6e04ace7250d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 14 19:30:32 2025 +0300 home: imp code commit 8633886457c6aab75f5676494b1f49d9811e9ab9 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 11 15:29:25 2025 +0300 all: imp code commit d6f16879e7b054a5ffac59131d2a6eff1da659c0 Merge: 58236fdec6d282ae71Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 21:35:23 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 58236fdec5b64e83a44680ff8a89badc18ec81f1 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 21:23:01 2025 +0300 all: upd ci commit 3c4d946d7970987677d4ac984394e18987a29f9a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 21:16:03 2025 +0300 all: upd go commit cc1c97734506a9ffbe70fd3c676284e58a21ba46 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 20:58:56 2025 +0300 all: imp code commit 8f061c933152481a4c80eef2af575efd4919d82b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 9 16:49:11 2025 +0300 all: imp docs commit 8d19355f1c519211a56cec3f23d527922d4f2ee0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 7 21:35:06 2025 +0300 all: imp code commit f1e853f57e5d54d13bedcdab4f8e21e112f3a356 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 2 14:57:40 2025 +0300 all: imp code commit 6a6ac7f899f29ddc90a583c80562233e646ba1d6 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 1 19:51:56 2025 +0300 client: imp tests commit 52040ee7393d0483c682f2f37d7b70f12f9cf621 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 1 19:28:18 2025 +0300 all: imp code commit 1e09208dbd2d35c3f6b2ade169324e23d1a643a5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 26 15:33:02 2025 +0300 all: imp code ... and 2 more commits
575 lines
14 KiB
Go
575 lines
14 KiB
Go
package dhcpsvc_test
|
|
|
|
import (
|
|
"io/fs"
|
|
"net"
|
|
"net/netip"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// testdata is a filesystem containing data for tests.
|
|
var testdata = os.DirFS("testdata")
|
|
|
|
// newTempDB copies the leases database file located in the testdata FS, under
|
|
// tb.Name()/leases.json, to a temporary directory and returns the path to the
|
|
// copied file.
|
|
func newTempDB(tb testing.TB) (dst string) {
|
|
tb.Helper()
|
|
|
|
const filename = "leases.json"
|
|
|
|
data, err := fs.ReadFile(testdata, path.Join(tb.Name(), filename))
|
|
require.NoError(tb, err)
|
|
|
|
dst = filepath.Join(tb.TempDir(), filename)
|
|
|
|
err = os.WriteFile(dst, data, dhcpsvc.DatabasePerm)
|
|
require.NoError(tb, err)
|
|
|
|
return dst
|
|
}
|
|
|
|
func TestNew(t *testing.T) {
|
|
validIPv4Conf := &dhcpsvc.IPv4Config{
|
|
Enabled: true,
|
|
GatewayIP: netip.MustParseAddr("192.168.0.1"),
|
|
SubnetMask: netip.MustParseAddr("255.255.255.0"),
|
|
RangeStart: netip.MustParseAddr("192.168.0.2"),
|
|
RangeEnd: netip.MustParseAddr("192.168.0.254"),
|
|
LeaseDuration: 1 * time.Hour,
|
|
}
|
|
gwInRangeConf := &dhcpsvc.IPv4Config{
|
|
Enabled: true,
|
|
GatewayIP: netip.MustParseAddr("192.168.0.100"),
|
|
SubnetMask: netip.MustParseAddr("255.255.255.0"),
|
|
RangeStart: netip.MustParseAddr("192.168.0.1"),
|
|
RangeEnd: netip.MustParseAddr("192.168.0.254"),
|
|
LeaseDuration: 1 * time.Hour,
|
|
}
|
|
badStartConf := &dhcpsvc.IPv4Config{
|
|
Enabled: true,
|
|
GatewayIP: netip.MustParseAddr("192.168.0.1"),
|
|
SubnetMask: netip.MustParseAddr("255.255.255.0"),
|
|
RangeStart: netip.MustParseAddr("127.0.0.1"),
|
|
RangeEnd: netip.MustParseAddr("192.168.0.254"),
|
|
LeaseDuration: 1 * time.Hour,
|
|
}
|
|
|
|
validIPv6Conf := &dhcpsvc.IPv6Config{
|
|
Enabled: true,
|
|
RangeStart: netip.MustParseAddr("2001:db8::1"),
|
|
LeaseDuration: 1 * time.Hour,
|
|
RAAllowSLAAC: true,
|
|
RASLAACOnly: true,
|
|
}
|
|
|
|
leasesPath := filepath.Join(t.TempDir(), "leases.json")
|
|
|
|
testCases := []struct {
|
|
conf *dhcpsvc.Config
|
|
name string
|
|
wantErrMsg string
|
|
}{{
|
|
conf: &dhcpsvc.Config{
|
|
Enabled: true,
|
|
Logger: discardLog,
|
|
LocalDomainName: testLocalTLD,
|
|
Interfaces: map[string]*dhcpsvc.InterfaceConfig{
|
|
"eth0": {
|
|
IPv4: validIPv4Conf,
|
|
IPv6: validIPv6Conf,
|
|
},
|
|
},
|
|
DBFilePath: leasesPath,
|
|
},
|
|
name: "valid",
|
|
wantErrMsg: "",
|
|
}, {
|
|
conf: &dhcpsvc.Config{
|
|
Enabled: true,
|
|
Logger: discardLog,
|
|
LocalDomainName: testLocalTLD,
|
|
Interfaces: map[string]*dhcpsvc.InterfaceConfig{
|
|
"eth0": {
|
|
IPv4: &dhcpsvc.IPv4Config{Enabled: false},
|
|
IPv6: &dhcpsvc.IPv6Config{Enabled: false},
|
|
},
|
|
},
|
|
DBFilePath: leasesPath,
|
|
},
|
|
name: "disabled_interfaces",
|
|
wantErrMsg: "",
|
|
}, {
|
|
conf: &dhcpsvc.Config{
|
|
Enabled: true,
|
|
Logger: discardLog,
|
|
LocalDomainName: testLocalTLD,
|
|
Interfaces: map[string]*dhcpsvc.InterfaceConfig{
|
|
"eth0": {
|
|
IPv4: gwInRangeConf,
|
|
IPv6: validIPv6Conf,
|
|
},
|
|
},
|
|
DBFilePath: leasesPath,
|
|
},
|
|
name: "gateway_within_range",
|
|
wantErrMsg: `creating interfaces: interface "eth0": ipv4: ` +
|
|
`gateway ip 192.168.0.100 in the ip range 192.168.0.1-192.168.0.254`,
|
|
}, {
|
|
conf: &dhcpsvc.Config{
|
|
Enabled: true,
|
|
Logger: discardLog,
|
|
LocalDomainName: testLocalTLD,
|
|
Interfaces: map[string]*dhcpsvc.InterfaceConfig{
|
|
"eth0": {
|
|
IPv4: badStartConf,
|
|
IPv6: validIPv6Conf,
|
|
},
|
|
},
|
|
DBFilePath: leasesPath,
|
|
},
|
|
name: "bad_start",
|
|
wantErrMsg: `creating interfaces: interface "eth0": ipv4: ` +
|
|
`range start 127.0.0.1 is not within 192.168.0.1/24`,
|
|
}}
|
|
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
_, err := dhcpsvc.New(ctx, tc.conf)
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDHCPServer_AddLease(t *testing.T) {
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
|
|
leasesPath := filepath.Join(t.TempDir(), "leases.json")
|
|
srv, err := dhcpsvc.New(ctx, &dhcpsvc.Config{
|
|
Enabled: true,
|
|
Logger: discardLog,
|
|
LocalDomainName: testLocalTLD,
|
|
Interfaces: testInterfaceConf,
|
|
DBFilePath: leasesPath,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
const (
|
|
existHost = "host1"
|
|
newHost = "host2"
|
|
ipv6Host = "host3"
|
|
)
|
|
|
|
var (
|
|
existIP = netip.MustParseAddr("192.168.0.2")
|
|
newIP = netip.MustParseAddr("192.168.0.3")
|
|
newIPv6 = netip.MustParseAddr("2001:db8::2")
|
|
|
|
existMAC = errors.Must(net.ParseMAC("01:02:03:04:05:06"))
|
|
newMAC = errors.Must(net.ParseMAC("06:05:04:03:02:01"))
|
|
ipv6MAC = errors.Must(net.ParseMAC("02:03:04:05:06:07"))
|
|
)
|
|
|
|
require.NoError(t, srv.AddLease(ctx, &dhcpsvc.Lease{
|
|
Hostname: existHost,
|
|
IP: existIP,
|
|
HWAddr: existMAC,
|
|
IsStatic: true,
|
|
}))
|
|
|
|
testCases := []struct {
|
|
name string
|
|
lease *dhcpsvc.Lease
|
|
wantErrMsg string
|
|
}{{
|
|
name: "outside_range",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: newHost,
|
|
IP: netip.MustParseAddr("1.2.3.4"),
|
|
HWAddr: newMAC,
|
|
},
|
|
wantErrMsg: "adding lease: no interface for ip 1.2.3.4",
|
|
}, {
|
|
name: "duplicate_ip",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: newHost,
|
|
IP: existIP,
|
|
HWAddr: newMAC,
|
|
},
|
|
wantErrMsg: "adding lease: lease for ip " + existIP.String() +
|
|
" already exists",
|
|
}, {
|
|
name: "duplicate_hostname",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: existHost,
|
|
IP: newIP,
|
|
HWAddr: newMAC,
|
|
},
|
|
wantErrMsg: "adding lease: lease for hostname " + existHost +
|
|
" already exists",
|
|
}, {
|
|
name: "duplicate_hostname_case",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: strings.ToUpper(existHost),
|
|
IP: newIP,
|
|
HWAddr: newMAC,
|
|
},
|
|
wantErrMsg: "adding lease: lease for hostname " +
|
|
strings.ToUpper(existHost) + " already exists",
|
|
}, {
|
|
name: "duplicate_mac",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: newHost,
|
|
IP: newIP,
|
|
HWAddr: existMAC,
|
|
},
|
|
wantErrMsg: "adding lease: lease for mac " + existMAC.String() +
|
|
" already exists",
|
|
}, {
|
|
name: "valid",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: newHost,
|
|
IP: newIP,
|
|
HWAddr: newMAC,
|
|
},
|
|
wantErrMsg: "",
|
|
}, {
|
|
name: "valid_v6",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: ipv6Host,
|
|
IP: newIPv6,
|
|
HWAddr: ipv6MAC,
|
|
},
|
|
wantErrMsg: "",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, srv.AddLease(ctx, tc.lease))
|
|
})
|
|
}
|
|
|
|
assert.NotEmpty(t, srv.Leases())
|
|
assert.FileExists(t, leasesPath)
|
|
}
|
|
|
|
func TestDHCPServer_index(t *testing.T) {
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
|
|
leasesPath := newTempDB(t)
|
|
srv, err := dhcpsvc.New(ctx, &dhcpsvc.Config{
|
|
Enabled: true,
|
|
Logger: discardLog,
|
|
LocalDomainName: testLocalTLD,
|
|
Interfaces: testInterfaceConf,
|
|
DBFilePath: leasesPath,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
const (
|
|
host1 = "host1"
|
|
host2 = "host2"
|
|
host3 = "host3"
|
|
host4 = "host4"
|
|
host5 = "host5"
|
|
)
|
|
|
|
var (
|
|
ip1 = netip.MustParseAddr("192.168.0.2")
|
|
ip2 = netip.MustParseAddr("192.168.0.3")
|
|
ip3 = netip.MustParseAddr("172.16.0.3")
|
|
ip4 = netip.MustParseAddr("172.16.0.4")
|
|
|
|
mac1 = errors.Must(net.ParseMAC("01:02:03:04:05:06"))
|
|
mac2 = errors.Must(net.ParseMAC("06:05:04:03:02:01"))
|
|
mac3 = errors.Must(net.ParseMAC("02:03:04:05:06:07"))
|
|
)
|
|
|
|
t.Run("ip_idx", func(t *testing.T) {
|
|
assert.Equal(t, ip1, srv.IPByHost(host1))
|
|
assert.Equal(t, ip2, srv.IPByHost(host2))
|
|
assert.Equal(t, ip3, srv.IPByHost(host3))
|
|
assert.Equal(t, ip4, srv.IPByHost(host4))
|
|
assert.Zero(t, srv.IPByHost(host5))
|
|
})
|
|
|
|
t.Run("name_idx", func(t *testing.T) {
|
|
assert.Equal(t, host1, srv.HostByIP(ip1))
|
|
assert.Equal(t, host2, srv.HostByIP(ip2))
|
|
assert.Equal(t, host3, srv.HostByIP(ip3))
|
|
assert.Equal(t, host4, srv.HostByIP(ip4))
|
|
assert.Zero(t, srv.HostByIP(netip.Addr{}))
|
|
})
|
|
|
|
t.Run("mac_idx", func(t *testing.T) {
|
|
assert.Equal(t, mac1, srv.MACByIP(ip1))
|
|
assert.Equal(t, mac2, srv.MACByIP(ip2))
|
|
assert.Equal(t, mac3, srv.MACByIP(ip3))
|
|
assert.Equal(t, mac1, srv.MACByIP(ip4))
|
|
assert.Zero(t, srv.MACByIP(netip.Addr{}))
|
|
})
|
|
}
|
|
|
|
func TestDHCPServer_UpdateStaticLease(t *testing.T) {
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
|
|
leasesPath := newTempDB(t)
|
|
srv, err := dhcpsvc.New(ctx, &dhcpsvc.Config{
|
|
Enabled: true,
|
|
Logger: discardLog,
|
|
LocalDomainName: testLocalTLD,
|
|
Interfaces: testInterfaceConf,
|
|
DBFilePath: leasesPath,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
const (
|
|
host1 = "host1"
|
|
host2 = "host2"
|
|
host3 = "host3"
|
|
host4 = "host4"
|
|
host5 = "host5"
|
|
host6 = "host6"
|
|
)
|
|
|
|
var (
|
|
ip1 = netip.MustParseAddr("192.168.0.2")
|
|
ip2 = netip.MustParseAddr("192.168.0.3")
|
|
ip3 = netip.MustParseAddr("192.168.0.4")
|
|
ip4 = netip.MustParseAddr("2001:db8::3")
|
|
|
|
mac1 = errors.Must(net.ParseMAC("01:02:03:04:05:06"))
|
|
mac2 = errors.Must(net.ParseMAC("06:05:04:03:02:01"))
|
|
mac3 = errors.Must(net.ParseMAC("06:05:04:03:02:02"))
|
|
)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
lease *dhcpsvc.Lease
|
|
wantErrMsg string
|
|
}{{
|
|
name: "outside_range",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: host1,
|
|
IP: netip.MustParseAddr("1.2.3.4"),
|
|
HWAddr: mac1,
|
|
},
|
|
wantErrMsg: "updating static lease: no interface for ip 1.2.3.4",
|
|
}, {
|
|
name: "not_found",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: host3,
|
|
IP: ip3,
|
|
HWAddr: mac2,
|
|
},
|
|
wantErrMsg: "updating static lease: no lease for mac " + mac2.String(),
|
|
}, {
|
|
name: "duplicate_ip",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: host1,
|
|
IP: ip2,
|
|
HWAddr: mac1,
|
|
},
|
|
wantErrMsg: "updating static lease: lease for ip " + ip2.String() +
|
|
" already exists",
|
|
}, {
|
|
name: "duplicate_hostname",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: host2,
|
|
IP: ip1,
|
|
HWAddr: mac1,
|
|
},
|
|
wantErrMsg: "updating static lease: lease for hostname " + host2 +
|
|
" already exists",
|
|
}, {
|
|
name: "duplicate_hostname_case",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: strings.ToUpper(host2),
|
|
IP: ip1,
|
|
HWAddr: mac1,
|
|
},
|
|
wantErrMsg: "updating static lease: lease for hostname " +
|
|
strings.ToUpper(host2) + " already exists",
|
|
}, {
|
|
name: "valid",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: host3,
|
|
IP: ip3,
|
|
HWAddr: mac1,
|
|
},
|
|
wantErrMsg: "",
|
|
}, {
|
|
name: "valid_v6",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: host6,
|
|
IP: ip4,
|
|
HWAddr: mac3,
|
|
},
|
|
wantErrMsg: "",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, srv.UpdateStaticLease(ctx, tc.lease))
|
|
})
|
|
}
|
|
|
|
assert.FileExists(t, leasesPath)
|
|
}
|
|
|
|
func TestDHCPServer_RemoveLease(t *testing.T) {
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
|
|
leasesPath := newTempDB(t)
|
|
srv, err := dhcpsvc.New(ctx, &dhcpsvc.Config{
|
|
Enabled: true,
|
|
Logger: discardLog,
|
|
LocalDomainName: testLocalTLD,
|
|
Interfaces: testInterfaceConf,
|
|
DBFilePath: leasesPath,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
const (
|
|
host1 = "host1"
|
|
host2 = "host2"
|
|
host3 = "host3"
|
|
)
|
|
|
|
var (
|
|
existIP = netip.MustParseAddr("192.168.0.2")
|
|
newIP = netip.MustParseAddr("192.168.0.3")
|
|
newIPv6 = netip.MustParseAddr("2001:db8::2")
|
|
|
|
existMAC = errors.Must(net.ParseMAC("01:02:03:04:05:06"))
|
|
newMAC = errors.Must(net.ParseMAC("02:03:04:05:06:07"))
|
|
ipv6MAC = errors.Must(net.ParseMAC("06:05:04:03:02:01"))
|
|
)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
lease *dhcpsvc.Lease
|
|
wantErrMsg string
|
|
}{{
|
|
name: "not_found_mac",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: host1,
|
|
IP: existIP,
|
|
HWAddr: newMAC,
|
|
},
|
|
wantErrMsg: "removing lease: no lease for mac " + newMAC.String(),
|
|
}, {
|
|
name: "not_found_ip",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: host1,
|
|
IP: newIP,
|
|
HWAddr: existMAC,
|
|
},
|
|
wantErrMsg: "removing lease: no lease for ip " + newIP.String(),
|
|
}, {
|
|
name: "not_found_host",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: host2,
|
|
IP: existIP,
|
|
HWAddr: existMAC,
|
|
},
|
|
wantErrMsg: "removing lease: no lease for hostname " + host2,
|
|
}, {
|
|
name: "valid",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: host1,
|
|
IP: existIP,
|
|
HWAddr: existMAC,
|
|
},
|
|
wantErrMsg: "",
|
|
}, {
|
|
name: "valid_v6",
|
|
lease: &dhcpsvc.Lease{
|
|
Hostname: host3,
|
|
IP: newIPv6,
|
|
HWAddr: ipv6MAC,
|
|
},
|
|
wantErrMsg: "",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, srv.RemoveLease(ctx, tc.lease))
|
|
})
|
|
}
|
|
|
|
assert.FileExists(t, leasesPath)
|
|
assert.Empty(t, srv.Leases())
|
|
}
|
|
|
|
func TestDHCPServer_Reset(t *testing.T) {
|
|
leasesPath := newTempDB(t)
|
|
conf := &dhcpsvc.Config{
|
|
Enabled: true,
|
|
Logger: discardLog,
|
|
LocalDomainName: testLocalTLD,
|
|
Interfaces: testInterfaceConf,
|
|
DBFilePath: leasesPath,
|
|
}
|
|
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
srv, err := dhcpsvc.New(ctx, conf)
|
|
require.NoError(t, err)
|
|
|
|
const leasesNum = 4
|
|
|
|
require.Len(t, srv.Leases(), leasesNum)
|
|
|
|
require.NoError(t, srv.Reset(ctx))
|
|
|
|
assert.FileExists(t, leasesPath)
|
|
assert.Empty(t, srv.Leases())
|
|
}
|
|
|
|
func TestServer_Leases(t *testing.T) {
|
|
leasesPath := newTempDB(t)
|
|
conf := &dhcpsvc.Config{
|
|
Enabled: true,
|
|
Logger: discardLog,
|
|
LocalDomainName: testLocalTLD,
|
|
Interfaces: testInterfaceConf,
|
|
DBFilePath: leasesPath,
|
|
}
|
|
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
|
|
srv, err := dhcpsvc.New(ctx, conf)
|
|
require.NoError(t, err)
|
|
|
|
expiry, err := time.Parse(time.RFC3339, "2042-01-02T03:04:05Z")
|
|
require.NoError(t, err)
|
|
|
|
wantLeases := []*dhcpsvc.Lease{{
|
|
Expiry: expiry,
|
|
IP: netip.MustParseAddr("192.168.0.3"),
|
|
Hostname: "example.host",
|
|
HWAddr: errors.Must(net.ParseMAC("AA:AA:AA:AA:AA:AA")),
|
|
IsStatic: false,
|
|
}, {
|
|
Expiry: time.Time{},
|
|
IP: netip.MustParseAddr("192.168.0.4"),
|
|
Hostname: "example.static.host",
|
|
HWAddr: errors.Must(net.ParseMAC("BB:BB:BB:BB:BB:BB")),
|
|
IsStatic: true,
|
|
}}
|
|
assert.ElementsMatch(t, wantLeases, srv.Leases())
|
|
}
|