2023-10-02 13:21:16 +03:00
|
|
|
package dhcpsvc_test
|
|
|
|
|
|
|
|
import (
|
2024-07-09 20:04:24 +03:00
|
|
|
"io/fs"
|
2023-10-02 13:21:16 +03:00
|
|
|
"net/netip"
|
2024-07-09 20:04:24 +03:00
|
|
|
"os"
|
2024-07-10 13:03:36 +03:00
|
|
|
"path"
|
2024-07-09 20:04:24 +03:00
|
|
|
"path/filepath"
|
2024-02-20 14:52:38 +03:00
|
|
|
"strings"
|
2023-10-02 13:21:16 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2024-01-31 14:50:27 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-10-02 13:21:16 +03:00
|
|
|
)
|
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
// testdata is a filesystem containing data for tests.
|
|
|
|
var testdata = os.DirFS("testdata")
|
2024-02-20 14:52:38 +03:00
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
// newTempDB copies the leases database file located in the testdata FS, under
|
2024-07-10 13:03:36 +03:00
|
|
|
// tb.Name()/leases.json, to a temporary directory and returns the path to the
|
2024-07-09 20:04:24 +03:00
|
|
|
// copied file.
|
|
|
|
func newTempDB(tb testing.TB) (dst string) {
|
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
const filename = "leases.json"
|
2024-02-20 14:52:38 +03:00
|
|
|
|
2024-07-10 13:03:36 +03:00
|
|
|
data, err := fs.ReadFile(testdata, path.Join(tb.Name(), filename))
|
2024-07-09 20:04:24 +03:00
|
|
|
require.NoError(tb, err)
|
|
|
|
|
|
|
|
dst = filepath.Join(tb.TempDir(), filename)
|
|
|
|
|
|
|
|
err = os.WriteFile(dst, data, dhcpsvc.DatabasePerm)
|
|
|
|
require.NoError(tb, err)
|
|
|
|
|
|
|
|
return dst
|
2024-02-20 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2023-10-02 13:21:16 +03:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
leasesPath := filepath.Join(t.TempDir(), "leases.json")
|
|
|
|
|
2023-10-02 13:21:16 +03:00
|
|
|
testCases := []struct {
|
|
|
|
conf *dhcpsvc.Config
|
|
|
|
name string
|
|
|
|
wantErrMsg string
|
|
|
|
}{{
|
|
|
|
conf: &dhcpsvc.Config{
|
|
|
|
Enabled: true,
|
2024-07-03 15:29:54 +03:00
|
|
|
Logger: discardLog,
|
2023-10-02 13:21:16 +03:00
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: map[string]*dhcpsvc.InterfaceConfig{
|
|
|
|
"eth0": {
|
|
|
|
IPv4: validIPv4Conf,
|
|
|
|
IPv6: validIPv6Conf,
|
|
|
|
},
|
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2023-10-02 13:21:16 +03:00
|
|
|
},
|
|
|
|
name: "valid",
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
conf: &dhcpsvc.Config{
|
|
|
|
Enabled: true,
|
2024-07-03 15:29:54 +03:00
|
|
|
Logger: discardLog,
|
2023-10-02 13:21:16 +03:00
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: map[string]*dhcpsvc.InterfaceConfig{
|
|
|
|
"eth0": {
|
|
|
|
IPv4: &dhcpsvc.IPv4Config{Enabled: false},
|
|
|
|
IPv6: &dhcpsvc.IPv6Config{Enabled: false},
|
|
|
|
},
|
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2023-10-02 13:21:16 +03:00
|
|
|
},
|
|
|
|
name: "disabled_interfaces",
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
conf: &dhcpsvc.Config{
|
|
|
|
Enabled: true,
|
2024-07-03 15:29:54 +03:00
|
|
|
Logger: discardLog,
|
2023-10-02 13:21:16 +03:00
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: map[string]*dhcpsvc.InterfaceConfig{
|
|
|
|
"eth0": {
|
|
|
|
IPv4: gwInRangeConf,
|
|
|
|
IPv6: validIPv6Conf,
|
|
|
|
},
|
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2023-10-02 13:21:16 +03:00
|
|
|
},
|
|
|
|
name: "gateway_within_range",
|
2024-07-10 16:17:56 +03:00
|
|
|
wantErrMsg: `creating interfaces: interface "eth0": ipv4: ` +
|
2023-10-02 13:21:16 +03:00
|
|
|
`gateway ip 192.168.0.100 in the ip range 192.168.0.1-192.168.0.254`,
|
|
|
|
}, {
|
|
|
|
conf: &dhcpsvc.Config{
|
|
|
|
Enabled: true,
|
2024-07-03 15:29:54 +03:00
|
|
|
Logger: discardLog,
|
2023-10-02 13:21:16 +03:00
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: map[string]*dhcpsvc.InterfaceConfig{
|
|
|
|
"eth0": {
|
|
|
|
IPv4: badStartConf,
|
|
|
|
IPv6: validIPv6Conf,
|
|
|
|
},
|
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2023-10-02 13:21:16 +03:00
|
|
|
},
|
|
|
|
name: "bad_start",
|
2024-07-10 16:17:56 +03:00
|
|
|
wantErrMsg: `creating interfaces: interface "eth0": ipv4: ` +
|
2023-10-02 13:21:16 +03:00
|
|
|
`range start 127.0.0.1 is not within 192.168.0.1/24`,
|
|
|
|
}}
|
|
|
|
|
2024-07-03 15:29:54 +03:00
|
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
|
|
|
2023-10-02 13:21:16 +03:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-07-03 15:29:54 +03:00
|
|
|
_, err := dhcpsvc.New(ctx, tc.conf)
|
2023-10-02 13:21:16 +03:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-01-31 14:50:27 +03:00
|
|
|
|
2024-02-20 14:52:38 +03:00
|
|
|
func TestDHCPServer_AddLease(t *testing.T) {
|
2024-07-03 15:29:54 +03:00
|
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
leasesPath := filepath.Join(t.TempDir(), "leases.json")
|
2024-07-03 15:29:54 +03:00
|
|
|
srv, err := dhcpsvc.New(ctx, &dhcpsvc.Config{
|
2024-01-31 14:50:27 +03:00
|
|
|
Enabled: true,
|
2024-07-03 15:29:54 +03:00
|
|
|
Logger: discardLog,
|
2024-01-31 14:50:27 +03:00
|
|
|
LocalDomainName: testLocalTLD,
|
2024-02-20 14:52:38 +03:00
|
|
|
Interfaces: testInterfaceConf,
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2024-02-20 14:52:38 +03:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
const (
|
2024-07-09 20:04:24 +03:00
|
|
|
existHost = "host1"
|
|
|
|
newHost = "host2"
|
|
|
|
ipv6Host = "host3"
|
2024-02-20 14:52:38 +03:00
|
|
|
)
|
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
var (
|
|
|
|
existIP = netip.MustParseAddr("192.168.0.2")
|
|
|
|
newIP = netip.MustParseAddr("192.168.0.3")
|
|
|
|
newIPv6 = netip.MustParseAddr("2001:db8::2")
|
2024-02-20 14:52:38 +03:00
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
existMAC = mustParseMAC(t, "01:02:03:04:05:06")
|
|
|
|
newMAC = mustParseMAC(t, "06:05:04:03:02:01")
|
|
|
|
ipv6MAC = mustParseMAC(t, "02:03:04:05:06:07")
|
|
|
|
)
|
2024-02-20 14:52:38 +03:00
|
|
|
|
2024-07-03 15:29:54 +03:00
|
|
|
require.NoError(t, srv.AddLease(ctx, &dhcpsvc.Lease{
|
2024-07-09 20:04:24 +03:00
|
|
|
Hostname: existHost,
|
|
|
|
IP: existIP,
|
|
|
|
HWAddr: existMAC,
|
2024-02-20 14:52:38 +03:00
|
|
|
IsStatic: true,
|
|
|
|
}))
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
lease *dhcpsvc.Lease
|
|
|
|
wantErrMsg string
|
|
|
|
}{{
|
|
|
|
name: "outside_range",
|
|
|
|
lease: &dhcpsvc.Lease{
|
2024-07-09 20:04:24 +03:00
|
|
|
Hostname: newHost,
|
2024-02-20 14:52:38 +03:00
|
|
|
IP: netip.MustParseAddr("1.2.3.4"),
|
2024-07-09 20:04:24 +03:00
|
|
|
HWAddr: newMAC,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
|
|
|
wantErrMsg: "adding lease: no interface for ip 1.2.3.4",
|
|
|
|
}, {
|
|
|
|
name: "duplicate_ip",
|
|
|
|
lease: &dhcpsvc.Lease{
|
2024-07-09 20:04:24 +03:00
|
|
|
Hostname: newHost,
|
|
|
|
IP: existIP,
|
|
|
|
HWAddr: newMAC,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
wantErrMsg: "adding lease: lease for ip " + existIP.String() +
|
2024-02-20 14:52:38 +03:00
|
|
|
" already exists",
|
|
|
|
}, {
|
|
|
|
name: "duplicate_hostname",
|
|
|
|
lease: &dhcpsvc.Lease{
|
2024-07-09 20:04:24 +03:00
|
|
|
Hostname: existHost,
|
|
|
|
IP: newIP,
|
|
|
|
HWAddr: newMAC,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
wantErrMsg: "adding lease: lease for hostname " + existHost +
|
2024-02-20 14:52:38 +03:00
|
|
|
" already exists",
|
|
|
|
}, {
|
|
|
|
name: "duplicate_hostname_case",
|
|
|
|
lease: &dhcpsvc.Lease{
|
2024-07-09 20:04:24 +03:00
|
|
|
Hostname: strings.ToUpper(existHost),
|
|
|
|
IP: newIP,
|
|
|
|
HWAddr: newMAC,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
|
|
|
wantErrMsg: "adding lease: lease for hostname " +
|
2024-07-09 20:04:24 +03:00
|
|
|
strings.ToUpper(existHost) + " already exists",
|
2024-02-20 14:52:38 +03:00
|
|
|
}, {
|
|
|
|
name: "duplicate_mac",
|
|
|
|
lease: &dhcpsvc.Lease{
|
2024-07-09 20:04:24 +03:00
|
|
|
Hostname: newHost,
|
|
|
|
IP: newIP,
|
|
|
|
HWAddr: existMAC,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
wantErrMsg: "adding lease: lease for mac " + existMAC.String() +
|
2024-02-20 14:52:38 +03:00
|
|
|
" already exists",
|
|
|
|
}, {
|
|
|
|
name: "valid",
|
|
|
|
lease: &dhcpsvc.Lease{
|
2024-07-09 20:04:24 +03:00
|
|
|
Hostname: newHost,
|
|
|
|
IP: newIP,
|
|
|
|
HWAddr: newMAC,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "valid_v6",
|
|
|
|
lease: &dhcpsvc.Lease{
|
2024-07-09 20:04:24 +03:00
|
|
|
Hostname: ipv6Host,
|
|
|
|
IP: newIPv6,
|
|
|
|
HWAddr: ipv6MAC,
|
2024-01-31 14:50:27 +03:00
|
|
|
},
|
2024-02-20 14:52:38 +03:00
|
|
|
wantErrMsg: "",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-07-03 15:29:54 +03:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, srv.AddLease(ctx, tc.lease))
|
2024-02-20 14:52:38 +03:00
|
|
|
})
|
|
|
|
}
|
2024-07-09 20:04:24 +03:00
|
|
|
|
|
|
|
assert.NotEmpty(t, srv.Leases())
|
|
|
|
assert.FileExists(t, leasesPath)
|
2024-02-20 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDHCPServer_index(t *testing.T) {
|
2024-07-03 15:29:54 +03:00
|
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
leasesPath := newTempDB(t)
|
2024-07-03 15:29:54 +03:00
|
|
|
srv, err := dhcpsvc.New(ctx, &dhcpsvc.Config{
|
2024-02-20 14:52:38 +03:00
|
|
|
Enabled: true,
|
2024-07-03 15:29:54 +03:00
|
|
|
Logger: discardLog,
|
2024-02-20 14:52:38 +03:00
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: testInterfaceConf,
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2024-01-31 14:50:27 +03:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
const (
|
|
|
|
host1 = "host1"
|
|
|
|
host2 = "host2"
|
|
|
|
host3 = "host3"
|
|
|
|
host4 = "host4"
|
|
|
|
host5 = "host5"
|
|
|
|
)
|
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
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")
|
2024-01-31 14:50:27 +03:00
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
mac1 = mustParseMAC(t, "01:02:03:04:05:06")
|
|
|
|
mac2 = mustParseMAC(t, "06:05:04:03:02:01")
|
|
|
|
mac3 = mustParseMAC(t, "02:03:04:05:06:07")
|
|
|
|
)
|
2024-01-31 14:50:27 +03:00
|
|
|
|
|
|
|
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))
|
2024-07-09 20:04:24 +03:00
|
|
|
assert.Zero(t, srv.IPByHost(host5))
|
2024-01-31 14:50:27 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
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))
|
2024-07-09 20:04:24 +03:00
|
|
|
assert.Zero(t, srv.HostByIP(netip.Addr{}))
|
2024-01-31 14:50:27 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
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))
|
2024-07-09 20:04:24 +03:00
|
|
|
assert.Zero(t, srv.MACByIP(netip.Addr{}))
|
2024-01-31 14:50:27 +03:00
|
|
|
})
|
|
|
|
}
|
2024-02-20 14:52:38 +03:00
|
|
|
|
|
|
|
func TestDHCPServer_UpdateStaticLease(t *testing.T) {
|
2024-07-03 15:29:54 +03:00
|
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
leasesPath := newTempDB(t)
|
2024-07-03 15:29:54 +03:00
|
|
|
srv, err := dhcpsvc.New(ctx, &dhcpsvc.Config{
|
2024-02-20 14:52:38 +03:00
|
|
|
Enabled: true,
|
2024-07-03 15:29:54 +03:00
|
|
|
Logger: discardLog,
|
2024-02-20 14:52:38 +03:00
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: testInterfaceConf,
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2024-02-20 14:52:38 +03:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
const (
|
|
|
|
host1 = "host1"
|
|
|
|
host2 = "host2"
|
|
|
|
host3 = "host3"
|
|
|
|
host4 = "host4"
|
|
|
|
host5 = "host5"
|
|
|
|
host6 = "host6"
|
|
|
|
)
|
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
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 = mustParseMAC(t, "01:02:03:04:05:06")
|
|
|
|
mac2 = mustParseMAC(t, "06:05:04:03:02:01")
|
|
|
|
mac3 = mustParseMAC(t, "06:05:04:03:02:02")
|
|
|
|
)
|
2024-02-20 14:52:38 +03:00
|
|
|
|
|
|
|
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,
|
2024-07-09 20:04:24 +03:00
|
|
|
HWAddr: mac2,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
wantErrMsg: "updating static lease: no lease for mac " + mac2.String(),
|
2024-02-20 14:52:38 +03:00
|
|
|
}, {
|
|
|
|
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,
|
2024-07-09 20:04:24 +03:00
|
|
|
IP: ip4,
|
|
|
|
HWAddr: mac3,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-07-03 15:29:54 +03:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, srv.UpdateStaticLease(ctx, tc.lease))
|
2024-02-20 14:52:38 +03:00
|
|
|
})
|
|
|
|
}
|
2024-07-09 20:04:24 +03:00
|
|
|
|
|
|
|
assert.FileExists(t, leasesPath)
|
2024-02-20 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDHCPServer_RemoveLease(t *testing.T) {
|
2024-07-03 15:29:54 +03:00
|
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
leasesPath := newTempDB(t)
|
2024-07-03 15:29:54 +03:00
|
|
|
srv, err := dhcpsvc.New(ctx, &dhcpsvc.Config{
|
2024-02-20 14:52:38 +03:00
|
|
|
Enabled: true,
|
2024-07-03 15:29:54 +03:00
|
|
|
Logger: discardLog,
|
2024-02-20 14:52:38 +03:00
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: testInterfaceConf,
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
2024-02-20 14:52:38 +03:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
const (
|
|
|
|
host1 = "host1"
|
|
|
|
host2 = "host2"
|
|
|
|
host3 = "host3"
|
|
|
|
)
|
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
var (
|
|
|
|
existIP = netip.MustParseAddr("192.168.0.2")
|
|
|
|
newIP = netip.MustParseAddr("192.168.0.3")
|
|
|
|
newIPv6 = netip.MustParseAddr("2001:db8::2")
|
2024-02-20 14:52:38 +03:00
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
existMAC = mustParseMAC(t, "01:02:03:04:05:06")
|
|
|
|
newMAC = mustParseMAC(t, "02:03:04:05:06:07")
|
|
|
|
ipv6MAC = mustParseMAC(t, "06:05:04:03:02:01")
|
|
|
|
)
|
2024-02-20 14:52:38 +03:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
lease *dhcpsvc.Lease
|
|
|
|
wantErrMsg string
|
|
|
|
}{{
|
|
|
|
name: "not_found_mac",
|
|
|
|
lease: &dhcpsvc.Lease{
|
|
|
|
Hostname: host1,
|
2024-07-09 20:04:24 +03:00
|
|
|
IP: existIP,
|
|
|
|
HWAddr: newMAC,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
wantErrMsg: "removing lease: no lease for mac " + newMAC.String(),
|
2024-02-20 14:52:38 +03:00
|
|
|
}, {
|
|
|
|
name: "not_found_ip",
|
|
|
|
lease: &dhcpsvc.Lease{
|
|
|
|
Hostname: host1,
|
2024-07-09 20:04:24 +03:00
|
|
|
IP: newIP,
|
|
|
|
HWAddr: existMAC,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
2024-07-09 20:04:24 +03:00
|
|
|
wantErrMsg: "removing lease: no lease for ip " + newIP.String(),
|
2024-02-20 14:52:38 +03:00
|
|
|
}, {
|
|
|
|
name: "not_found_host",
|
|
|
|
lease: &dhcpsvc.Lease{
|
|
|
|
Hostname: host2,
|
2024-07-09 20:04:24 +03:00
|
|
|
IP: existIP,
|
|
|
|
HWAddr: existMAC,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
|
|
|
wantErrMsg: "removing lease: no lease for hostname " + host2,
|
|
|
|
}, {
|
|
|
|
name: "valid",
|
|
|
|
lease: &dhcpsvc.Lease{
|
|
|
|
Hostname: host1,
|
2024-07-09 20:04:24 +03:00
|
|
|
IP: existIP,
|
|
|
|
HWAddr: existMAC,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "valid_v6",
|
|
|
|
lease: &dhcpsvc.Lease{
|
|
|
|
Hostname: host3,
|
2024-07-09 20:04:24 +03:00
|
|
|
IP: newIPv6,
|
|
|
|
HWAddr: ipv6MAC,
|
2024-02-20 14:52:38 +03:00
|
|
|
},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-07-03 15:29:54 +03:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, srv.RemoveLease(ctx, tc.lease))
|
2024-02-20 14:52:38 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
assert.FileExists(t, leasesPath)
|
2024-02-20 14:52:38 +03:00
|
|
|
assert.Empty(t, srv.Leases())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDHCPServer_Reset(t *testing.T) {
|
2024-07-09 20:04:24 +03:00
|
|
|
leasesPath := newTempDB(t)
|
|
|
|
conf := &dhcpsvc.Config{
|
|
|
|
Enabled: true,
|
|
|
|
Logger: discardLog,
|
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: testInterfaceConf,
|
|
|
|
DBFilePath: leasesPath,
|
|
|
|
}
|
|
|
|
|
2024-07-03 15:29:54 +03:00
|
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
2024-07-09 20:04:24 +03:00
|
|
|
srv, err := dhcpsvc.New(ctx, conf)
|
|
|
|
require.NoError(t, err)
|
2024-07-03 15:29:54 +03:00
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
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{
|
2024-02-20 14:52:38 +03:00
|
|
|
Enabled: true,
|
2024-07-03 15:29:54 +03:00
|
|
|
Logger: discardLog,
|
2024-02-20 14:52:38 +03:00
|
|
|
LocalDomainName: testLocalTLD,
|
|
|
|
Interfaces: testInterfaceConf,
|
2024-07-09 20:04:24 +03:00
|
|
|
DBFilePath: leasesPath,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := testutil.ContextWithTimeout(t, testTimeout)
|
|
|
|
|
|
|
|
srv, err := dhcpsvc.New(ctx, conf)
|
2024-02-20 14:52:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-07-09 20:04:24 +03:00
|
|
|
expiry, err := time.Parse(time.RFC3339, "2042-01-02T03:04:05Z")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wantLeases := []*dhcpsvc.Lease{{
|
|
|
|
Expiry: expiry,
|
2024-02-20 14:52:38 +03:00
|
|
|
IP: netip.MustParseAddr("192.168.0.3"),
|
2024-07-09 20:04:24 +03:00
|
|
|
Hostname: "example.host",
|
|
|
|
HWAddr: mustParseMAC(t, "AA:AA:AA:AA:AA:AA"),
|
|
|
|
IsStatic: false,
|
2024-02-20 14:52:38 +03:00
|
|
|
}, {
|
2024-07-09 20:04:24 +03:00
|
|
|
Expiry: time.Time{},
|
|
|
|
IP: netip.MustParseAddr("192.168.0.4"),
|
|
|
|
Hostname: "example.static.host",
|
|
|
|
HWAddr: mustParseMAC(t, "BB:BB:BB:BB:BB:BB"),
|
2024-02-20 14:52:38 +03:00
|
|
|
IsStatic: true,
|
|
|
|
}}
|
2024-07-10 16:17:56 +03:00
|
|
|
assert.ElementsMatch(t, wantLeases, srv.Leases())
|
2024-02-20 14:52:38 +03:00
|
|
|
}
|