2022-09-13 21:45:35 +01:00
|
|
|
//go:build darwin || freebsd || linux || openbsd
|
2021-05-21 12:55:42 +01:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2023-03-23 13:52:01 +00:00
|
|
|
"net/netip"
|
2020-07-03 16:20:01 +01:00
|
|
|
"testing"
|
2023-03-23 13:52:01 +00:00
|
|
|
"time"
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2023-11-16 11:14:40 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dhcpsvc"
|
2020-07-03 16:20:01 +01:00
|
|
|
"github.com/insomniacslk/dhcp/dhcpv6"
|
|
|
|
"github.com/insomniacslk/dhcp/iana"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-12 10:27:44 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-03 16:20:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func notify6(flags uint32) {
|
|
|
|
}
|
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
func TestV6_AddRemove_static(t *testing.T) {
|
|
|
|
s, err := v6Create(V6ServerConf{
|
2020-07-03 16:20:01 +01:00
|
|
|
Enabled: true,
|
2021-01-20 14:27:53 +00:00
|
|
|
RangeStart: net.ParseIP("2001::1"),
|
2020-07-03 16:20:01 +01:00
|
|
|
notify: notify6,
|
2021-02-12 10:27:44 +00:00
|
|
|
})
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
require.Empty(t, s.GetLeases(LeasesStatic))
|
|
|
|
|
|
|
|
// Add static lease.
|
2023-11-16 11:14:40 +00:00
|
|
|
l := &dhcpsvc.Lease{
|
2023-03-23 13:52:01 +00:00
|
|
|
IP: netip.MustParseAddr("2001::1"),
|
2021-02-12 10:27:44 +00:00
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
2021-06-16 14:48:46 +01:00
|
|
|
err = s.AddStaticLease(l)
|
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
// Try to add the same static lease.
|
2021-06-16 14:48:46 +01:00
|
|
|
err = s.AddStaticLease(l)
|
|
|
|
require.Error(t, err)
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
ls := s.GetLeases(LeasesStatic)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.Len(t, ls, 1)
|
2021-06-16 14:48:46 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, l.IP, ls[0].IP)
|
|
|
|
assert.Equal(t, l.HWAddr, ls[0].HWAddr)
|
2023-04-18 13:12:11 +01:00
|
|
|
assert.True(t, ls[0].IsStatic)
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
// Try to remove non-existent static lease.
|
2023-11-16 11:14:40 +00:00
|
|
|
err = s.RemoveStaticLease(&dhcpsvc.Lease{
|
2023-03-23 13:52:01 +00:00
|
|
|
IP: netip.MustParseAddr("2001::2"),
|
2021-02-12 10:27:44 +00:00
|
|
|
HWAddr: l.HWAddr,
|
2021-06-16 14:48:46 +01:00
|
|
|
})
|
|
|
|
require.Error(t, err)
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
// Remove static lease.
|
2021-06-16 14:48:46 +01:00
|
|
|
err = s.RemoveStaticLease(l)
|
|
|
|
require.NoError(t, err)
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Empty(t, s.GetLeases(LeasesStatic))
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
func TestV6_AddReplace(t *testing.T) {
|
|
|
|
sIface, err := v6Create(V6ServerConf{
|
2020-07-03 16:20:01 +01:00
|
|
|
Enabled: true,
|
2021-01-20 14:27:53 +00:00
|
|
|
RangeStart: net.ParseIP("2001::1"),
|
2020-07-03 16:20:01 +01:00
|
|
|
notify: notify6,
|
2021-02-12 10:27:44 +00:00
|
|
|
})
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-04 12:12:34 +00:00
|
|
|
s, ok := sIface.(*v6Server)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
// Add dynamic leases.
|
2023-11-16 11:14:40 +00:00
|
|
|
dynLeases := []*dhcpsvc.Lease{{
|
2023-03-23 13:52:01 +00:00
|
|
|
IP: netip.MustParseAddr("2001::1"),
|
2021-02-12 10:27:44 +00:00
|
|
|
HWAddr: net.HardwareAddr{0x11, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}, {
|
2023-03-23 13:52:01 +00:00
|
|
|
IP: netip.MustParseAddr("2001::2"),
|
2021-02-12 10:27:44 +00:00
|
|
|
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, l := range dynLeases {
|
|
|
|
s.addLease(l)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2023-11-16 11:14:40 +00:00
|
|
|
stLeases := []*dhcpsvc.Lease{{
|
2023-03-23 13:52:01 +00:00
|
|
|
IP: netip.MustParseAddr("2001::1"),
|
2021-02-12 10:27:44 +00:00
|
|
|
HWAddr: net.HardwareAddr{0x33, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}, {
|
2023-03-23 13:52:01 +00:00
|
|
|
IP: netip.MustParseAddr("2001::3"),
|
2021-02-12 10:27:44 +00:00
|
|
|
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}}
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
for _, l := range stLeases {
|
2021-06-16 14:48:46 +01:00
|
|
|
err = s.AddStaticLease(l)
|
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
}
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
ls := s.GetLeases(LeasesStatic)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.Len(t, ls, 2)
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
for i, l := range ls {
|
2023-03-23 13:52:01 +00:00
|
|
|
assert.Equal(t, stLeases[i].IP, l.IP)
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, stLeases[i].HWAddr, l.HWAddr)
|
2023-04-18 13:12:11 +01:00
|
|
|
assert.True(t, l.IsStatic)
|
2021-02-12 10:27:44 +00:00
|
|
|
}
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestV6GetLease(t *testing.T) {
|
2021-02-12 10:27:44 +00:00
|
|
|
var err error
|
|
|
|
sIface, err := v6Create(V6ServerConf{
|
2020-07-03 16:20:01 +01:00
|
|
|
Enabled: true,
|
2021-01-20 14:27:53 +00:00
|
|
|
RangeStart: net.ParseIP("2001::1"),
|
2020-07-03 16:20:01 +01:00
|
|
|
notify: notify6,
|
2021-02-12 10:27:44 +00:00
|
|
|
})
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-04 12:12:34 +00:00
|
|
|
s, ok := sIface.(*v6Server)
|
2021-06-16 14:48:46 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
dnsAddr := net.ParseIP("2000::1")
|
|
|
|
s.conf.dnsIPAddrs = []net.IP{dnsAddr}
|
2023-05-17 13:16:02 +01:00
|
|
|
s.sid = &dhcpv6.DUIDLL{
|
|
|
|
HWType: iana.HWTypeEthernet,
|
2021-02-12 10:27:44 +00:00
|
|
|
LinkLayerAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
2021-02-12 10:27:44 +00:00
|
|
|
|
2023-11-16 11:14:40 +00:00
|
|
|
l := &dhcpsvc.Lease{
|
2023-03-23 13:52:01 +00:00
|
|
|
IP: netip.MustParseAddr("2001::1"),
|
2021-02-12 10:27:44 +00:00
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}
|
2021-06-16 14:48:46 +01:00
|
|
|
err = s.AddStaticLease(l)
|
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
var req, resp, msg *dhcpv6.Message
|
|
|
|
mac := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
|
|
|
|
t.Run("solicit", func(t *testing.T) {
|
|
|
|
req, err = dhcpv6.NewSolicit(mac)
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
msg, err = req.GetInnerMessage()
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
resp, err = dhcpv6.NewAdvertiseFromSolicit(msg)
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
assert.True(t, s.process(msg, req, resp))
|
|
|
|
})
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
resp.AddOption(dhcpv6.OptServerID(s.sid))
|
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
var oia *dhcpv6.OptIANA
|
|
|
|
var oiaAddr *dhcpv6.OptIAAddress
|
|
|
|
t.Run("advertise", func(t *testing.T) {
|
|
|
|
require.Equal(t, dhcpv6.MessageTypeAdvertise, resp.Type())
|
2021-06-16 14:48:46 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
oia = resp.Options.OneIANA()
|
|
|
|
oiaAddr = oia.Options.OneAddress()
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2023-03-23 13:52:01 +00:00
|
|
|
ip := net.IP(l.IP.AsSlice())
|
|
|
|
assert.Equal(t, ip, oiaAddr.IPv6Addr)
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, s.conf.leaseTime.Seconds(), oiaAddr.ValidLifetime.Seconds())
|
|
|
|
})
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
t.Run("request", func(t *testing.T) {
|
|
|
|
req, err = dhcpv6.NewRequestFromAdvertise(resp)
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
msg, err = req.GetInnerMessage()
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
resp, err = dhcpv6.NewReplyFromMessage(msg)
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
assert.True(t, s.process(msg, req, resp))
|
|
|
|
})
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
t.Run("reply", func(t *testing.T) {
|
|
|
|
require.Equal(t, dhcpv6.MessageTypeReply, resp.Type())
|
2021-06-16 14:48:46 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
oia = resp.Options.OneIANA()
|
|
|
|
oiaAddr = oia.Options.OneAddress()
|
|
|
|
|
2023-03-23 13:52:01 +00:00
|
|
|
ip := net.IP(l.IP.AsSlice())
|
|
|
|
assert.Equal(t, ip, oiaAddr.IPv6Addr)
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, s.conf.leaseTime.Seconds(), oiaAddr.ValidLifetime.Seconds())
|
|
|
|
})
|
|
|
|
|
|
|
|
dnsAddrs := resp.Options.DNS()
|
|
|
|
require.Len(t, dnsAddrs, 1)
|
|
|
|
assert.Equal(t, dnsAddr, dnsAddrs[0])
|
|
|
|
|
|
|
|
t.Run("lease", func(t *testing.T) {
|
|
|
|
ls := s.GetLeases(LeasesStatic)
|
|
|
|
require.Len(t, ls, 1)
|
2021-06-16 14:48:46 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, l.IP, ls[0].IP)
|
|
|
|
assert.Equal(t, l.HWAddr, ls[0].HWAddr)
|
|
|
|
})
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestV6GetDynamicLease(t *testing.T) {
|
2021-02-12 10:27:44 +00:00
|
|
|
sIface, err := v6Create(V6ServerConf{
|
2020-07-03 16:20:01 +01:00
|
|
|
Enabled: true,
|
2021-01-20 14:27:53 +00:00
|
|
|
RangeStart: net.ParseIP("2001::2"),
|
2020-07-03 16:20:01 +01:00
|
|
|
notify: notify6,
|
2021-02-12 10:27:44 +00:00
|
|
|
})
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-04 12:12:34 +00:00
|
|
|
s, ok := sIface.(*v6Server)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
dnsAddr := net.ParseIP("2000::1")
|
|
|
|
s.conf.dnsIPAddrs = []net.IP{dnsAddr}
|
2023-05-17 13:16:02 +01:00
|
|
|
s.sid = &dhcpv6.DUIDLL{
|
|
|
|
HWType: iana.HWTypeEthernet,
|
2021-02-12 10:27:44 +00:00
|
|
|
LinkLayerAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
var req, resp, msg *dhcpv6.Message
|
|
|
|
mac := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
|
|
|
|
t.Run("solicit", func(t *testing.T) {
|
|
|
|
req, err = dhcpv6.NewSolicit(mac)
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
msg, err = req.GetInnerMessage()
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
resp, err = dhcpv6.NewAdvertiseFromSolicit(msg)
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
assert.True(t, s.process(msg, req, resp))
|
|
|
|
})
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
resp.AddOption(dhcpv6.OptServerID(s.sid))
|
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
var oia *dhcpv6.OptIANA
|
|
|
|
var oiaAddr *dhcpv6.OptIAAddress
|
|
|
|
t.Run("advertise", func(t *testing.T) {
|
|
|
|
require.Equal(t, dhcpv6.MessageTypeAdvertise, resp.Type())
|
2021-06-16 14:48:46 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
oia = resp.Options.OneIANA()
|
|
|
|
oiaAddr = oia.Options.OneAddress()
|
|
|
|
assert.Equal(t, "2001::2", oiaAddr.IPv6Addr.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("request", func(t *testing.T) {
|
|
|
|
req, err = dhcpv6.NewRequestFromAdvertise(resp)
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
msg, err = req.GetInnerMessage()
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
resp, err = dhcpv6.NewReplyFromMessage(msg)
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.True(t, s.process(msg, req, resp))
|
|
|
|
})
|
2021-06-16 14:48:46 +01:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
t.Run("reply", func(t *testing.T) {
|
|
|
|
require.Equal(t, dhcpv6.MessageTypeReply, resp.Type())
|
2021-06-16 14:48:46 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
oia = resp.Options.OneIANA()
|
|
|
|
oiaAddr = oia.Options.OneAddress()
|
|
|
|
assert.Equal(t, "2001::2", oiaAddr.IPv6Addr.String())
|
|
|
|
})
|
2020-07-03 16:20:01 +01:00
|
|
|
|
|
|
|
dnsAddrs := resp.Options.DNS()
|
2021-02-12 10:27:44 +00:00
|
|
|
require.Len(t, dnsAddrs, 1)
|
2021-06-16 14:48:46 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, dnsAddr, dnsAddrs[0])
|
|
|
|
|
|
|
|
t.Run("lease", func(t *testing.T) {
|
|
|
|
ls := s.GetLeases(LeasesDynamic)
|
|
|
|
require.Len(t, ls, 1)
|
2021-06-16 14:48:46 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, "2001::2", ls[0].IP.String())
|
|
|
|
assert.Equal(t, mac, ls[0].HWAddr)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIP6InRange(t *testing.T) {
|
|
|
|
start := net.ParseIP("2001::2")
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
ip net.IP
|
|
|
|
want bool
|
|
|
|
}{{
|
|
|
|
ip: net.ParseIP("2001::1"),
|
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
ip: net.ParseIP("2002::2"),
|
|
|
|
want: false,
|
|
|
|
}, {
|
|
|
|
ip: start,
|
|
|
|
want: true,
|
|
|
|
}, {
|
|
|
|
ip: net.ParseIP("2001::3"),
|
|
|
|
want: true,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.ip.String(), func(t *testing.T) {
|
|
|
|
assert.Equal(t, tc.want, ip6InRange(start, tc.ip))
|
|
|
|
})
|
|
|
|
}
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
2023-03-23 13:52:01 +00:00
|
|
|
|
|
|
|
func TestV6_FindMACbyIP(t *testing.T) {
|
|
|
|
const (
|
|
|
|
staticName = "static-client"
|
|
|
|
anotherName = "another-client"
|
|
|
|
)
|
|
|
|
|
|
|
|
staticIP := netip.MustParseAddr("2001::1")
|
|
|
|
staticMAC := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
|
|
|
|
|
|
|
|
anotherIP := netip.MustParseAddr("2001::100")
|
|
|
|
anotherMAC := net.HardwareAddr{0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB}
|
|
|
|
|
|
|
|
s := &v6Server{
|
2023-11-16 11:14:40 +00:00
|
|
|
leases: []*dhcpsvc.Lease{{
|
2023-03-23 13:52:01 +00:00
|
|
|
Hostname: staticName,
|
|
|
|
HWAddr: staticMAC,
|
|
|
|
IP: staticIP,
|
2023-04-10 09:17:05 +01:00
|
|
|
IsStatic: true,
|
2023-03-23 13:52:01 +00:00
|
|
|
}, {
|
|
|
|
Expiry: time.Unix(10, 0),
|
|
|
|
Hostname: anotherName,
|
|
|
|
HWAddr: anotherMAC,
|
|
|
|
IP: anotherIP,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2023-11-16 11:14:40 +00:00
|
|
|
s.leases = []*dhcpsvc.Lease{{
|
2023-03-23 13:52:01 +00:00
|
|
|
Hostname: staticName,
|
|
|
|
HWAddr: staticMAC,
|
|
|
|
IP: staticIP,
|
2023-04-10 09:17:05 +01:00
|
|
|
IsStatic: true,
|
2023-03-23 13:52:01 +00:00
|
|
|
}, {
|
|
|
|
Expiry: time.Unix(10, 0),
|
|
|
|
Hostname: anotherName,
|
|
|
|
HWAddr: anotherMAC,
|
|
|
|
IP: anotherIP,
|
|
|
|
}}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
want net.HardwareAddr
|
|
|
|
ip netip.Addr
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
name: "basic",
|
|
|
|
ip: staticIP,
|
|
|
|
want: staticMAC,
|
|
|
|
}, {
|
|
|
|
name: "not_found",
|
|
|
|
ip: netip.MustParseAddr("ffff::1"),
|
|
|
|
want: nil,
|
|
|
|
}, {
|
|
|
|
name: "expired",
|
|
|
|
ip: anotherIP,
|
|
|
|
want: nil,
|
|
|
|
}, {
|
|
|
|
name: "v4",
|
|
|
|
ip: netip.MustParseAddr("1.2.3.4"),
|
|
|
|
want: nil,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
mac := s.FindMACbyIP(tc.ip)
|
|
|
|
|
|
|
|
require.Equal(t, tc.want, mac)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|