2022-09-14 16:36:29 +03:00
|
|
|
//go:build darwin || freebsd || linux || openbsd
|
2021-05-21 14:55:42 +03:00
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2023-04-12 14:48:42 +03:00
|
|
|
"net/netip"
|
2020-07-03 18:20:01 +03:00
|
|
|
"testing"
|
2023-04-12 14:48:42 +03:00
|
|
|
"time"
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
"github.com/insomniacslk/dhcp/dhcpv6"
|
|
|
|
"github.com/insomniacslk/dhcp/iana"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-12 13:27:44 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-03 18:20:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func notify6(flags uint32) {
|
|
|
|
}
|
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
func TestV6_AddRemove_static(t *testing.T) {
|
|
|
|
s, err := v6Create(V6ServerConf{
|
2020-07-03 18:20:01 +03:00
|
|
|
Enabled: true,
|
2021-01-20 17:27:53 +03:00
|
|
|
RangeStart: net.ParseIP("2001::1"),
|
2020-07-03 18:20:01 +03:00
|
|
|
notify: notify6,
|
2021-02-12 13:27:44 +03:00
|
|
|
})
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
require.Empty(t, s.GetLeases(LeasesStatic))
|
|
|
|
|
|
|
|
// Add static lease.
|
2021-06-16 16:48:46 +03:00
|
|
|
l := &Lease{
|
2023-04-12 14:48:42 +03:00
|
|
|
IP: netip.MustParseAddr("2001::1"),
|
2021-02-12 13:27:44 +03:00
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.AddStaticLease(l)
|
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
// Try to add the same static lease.
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.AddStaticLease(l)
|
|
|
|
require.Error(t, err)
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
ls := s.GetLeases(LeasesStatic)
|
2021-02-12 13:27:44 +03:00
|
|
|
require.Len(t, ls, 1)
|
2021-06-16 16:48:46 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
assert.Equal(t, l.IP, ls[0].IP)
|
|
|
|
assert.Equal(t, l.HWAddr, ls[0].HWAddr)
|
2023-04-18 16:07:11 +03:00
|
|
|
assert.True(t, ls[0].IsStatic)
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
// Try to remove non-existent static lease.
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.RemoveStaticLease(&Lease{
|
2023-04-12 14:48:42 +03:00
|
|
|
IP: netip.MustParseAddr("2001::2"),
|
2021-02-12 13:27:44 +03:00
|
|
|
HWAddr: l.HWAddr,
|
2021-06-16 16:48:46 +03:00
|
|
|
})
|
|
|
|
require.Error(t, err)
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
// Remove static lease.
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.RemoveStaticLease(l)
|
|
|
|
require.NoError(t, err)
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
assert.Empty(t, s.GetLeases(LeasesStatic))
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
func TestV6_AddReplace(t *testing.T) {
|
|
|
|
sIface, err := v6Create(V6ServerConf{
|
2020-07-03 18:20:01 +03:00
|
|
|
Enabled: true,
|
2021-01-20 17:27:53 +03:00
|
|
|
RangeStart: net.ParseIP("2001::1"),
|
2020-07-03 18:20:01 +03:00
|
|
|
notify: notify6,
|
2021-02-12 13:27:44 +03:00
|
|
|
})
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-04 15:12:34 +03:00
|
|
|
s, ok := sIface.(*v6Server)
|
2021-02-12 13:27:44 +03:00
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
// Add dynamic leases.
|
|
|
|
dynLeases := []*Lease{{
|
2023-04-12 14:48:42 +03:00
|
|
|
IP: netip.MustParseAddr("2001::1"),
|
2021-02-12 13:27:44 +03:00
|
|
|
HWAddr: net.HardwareAddr{0x11, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}, {
|
2023-04-12 14:48:42 +03:00
|
|
|
IP: netip.MustParseAddr("2001::2"),
|
2021-02-12 13:27:44 +03:00
|
|
|
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, l := range dynLeases {
|
|
|
|
s.addLease(l)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
stLeases := []*Lease{{
|
2023-04-12 14:48:42 +03:00
|
|
|
IP: netip.MustParseAddr("2001::1"),
|
2021-02-12 13:27:44 +03:00
|
|
|
HWAddr: net.HardwareAddr{0x33, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}, {
|
2023-04-12 14:48:42 +03:00
|
|
|
IP: netip.MustParseAddr("2001::3"),
|
2021-02-12 13:27:44 +03:00
|
|
|
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}}
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
for _, l := range stLeases {
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.AddStaticLease(l)
|
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
ls := s.GetLeases(LeasesStatic)
|
2021-02-12 13:27:44 +03:00
|
|
|
require.Len(t, ls, 2)
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
for i, l := range ls {
|
2023-04-12 14:48:42 +03:00
|
|
|
assert.Equal(t, stLeases[i].IP, l.IP)
|
2021-02-12 13:27:44 +03:00
|
|
|
assert.Equal(t, stLeases[i].HWAddr, l.HWAddr)
|
2023-04-18 16:07:11 +03:00
|
|
|
assert.True(t, l.IsStatic)
|
2021-02-12 13:27:44 +03:00
|
|
|
}
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestV6GetLease(t *testing.T) {
|
2021-02-12 13:27:44 +03:00
|
|
|
var err error
|
|
|
|
sIface, err := v6Create(V6ServerConf{
|
2020-07-03 18:20:01 +03:00
|
|
|
Enabled: true,
|
2021-01-20 17:27:53 +03:00
|
|
|
RangeStart: net.ParseIP("2001::1"),
|
2020-07-03 18:20:01 +03:00
|
|
|
notify: notify6,
|
2021-02-12 13:27:44 +03:00
|
|
|
})
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-04 15:12:34 +03:00
|
|
|
s, ok := sIface.(*v6Server)
|
2021-06-16 16:48:46 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
dnsAddr := net.ParseIP("2000::1")
|
|
|
|
s.conf.dnsIPAddrs = []net.IP{dnsAddr}
|
2020-07-03 18:20:01 +03:00
|
|
|
s.sid = dhcpv6.Duid{
|
2021-02-12 13:27:44 +03:00
|
|
|
Type: dhcpv6.DUID_LLT,
|
|
|
|
HwType: iana.HWTypeEthernet,
|
|
|
|
LinkLayerAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2021-02-12 13:27:44 +03:00
|
|
|
|
2021-06-16 16:48:46 +03:00
|
|
|
l := &Lease{
|
2023-04-12 14:48:42 +03:00
|
|
|
IP: netip.MustParseAddr("2001::1"),
|
2021-02-12 13:27:44 +03:00
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}
|
2021-06-16 16:48:46 +03:00
|
|
|
err = s.AddStaticLease(l)
|
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03: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 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
msg, err = req.GetInnerMessage()
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
resp, err = dhcpv6.NewAdvertiseFromSolicit(msg)
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
assert.True(t, s.process(msg, req, resp))
|
|
|
|
})
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
resp.AddOption(dhcpv6.OptServerID(s.sid))
|
|
|
|
|
2021-02-12 13:27:44 +03: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 16:48:46 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
oia = resp.Options.OneIANA()
|
|
|
|
oiaAddr = oia.Options.OneAddress()
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2023-04-12 14:48:42 +03:00
|
|
|
ip := net.IP(l.IP.AsSlice())
|
|
|
|
assert.Equal(t, ip, oiaAddr.IPv6Addr)
|
2021-02-12 13:27:44 +03:00
|
|
|
assert.Equal(t, s.conf.leaseTime.Seconds(), oiaAddr.ValidLifetime.Seconds())
|
|
|
|
})
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
t.Run("request", func(t *testing.T) {
|
|
|
|
req, err = dhcpv6.NewRequestFromAdvertise(resp)
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
msg, err = req.GetInnerMessage()
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
resp, err = dhcpv6.NewReplyFromMessage(msg)
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
assert.True(t, s.process(msg, req, resp))
|
|
|
|
})
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
t.Run("reply", func(t *testing.T) {
|
|
|
|
require.Equal(t, dhcpv6.MessageTypeReply, resp.Type())
|
2021-06-16 16:48:46 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
oia = resp.Options.OneIANA()
|
|
|
|
oiaAddr = oia.Options.OneAddress()
|
|
|
|
|
2023-04-12 14:48:42 +03:00
|
|
|
ip := net.IP(l.IP.AsSlice())
|
|
|
|
assert.Equal(t, ip, oiaAddr.IPv6Addr)
|
2021-02-12 13:27:44 +03: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 16:48:46 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
assert.Equal(t, l.IP, ls[0].IP)
|
|
|
|
assert.Equal(t, l.HWAddr, ls[0].HWAddr)
|
|
|
|
})
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestV6GetDynamicLease(t *testing.T) {
|
2021-02-12 13:27:44 +03:00
|
|
|
sIface, err := v6Create(V6ServerConf{
|
2020-07-03 18:20:01 +03:00
|
|
|
Enabled: true,
|
2021-01-20 17:27:53 +03:00
|
|
|
RangeStart: net.ParseIP("2001::2"),
|
2020-07-03 18:20:01 +03:00
|
|
|
notify: notify6,
|
2021-02-12 13:27:44 +03:00
|
|
|
})
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-04 15:12:34 +03:00
|
|
|
s, ok := sIface.(*v6Server)
|
2021-02-12 13:27:44 +03:00
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
dnsAddr := net.ParseIP("2000::1")
|
|
|
|
s.conf.dnsIPAddrs = []net.IP{dnsAddr}
|
2020-07-03 18:20:01 +03:00
|
|
|
s.sid = dhcpv6.Duid{
|
2021-02-12 13:27:44 +03:00
|
|
|
Type: dhcpv6.DUID_LLT,
|
|
|
|
HwType: iana.HWTypeEthernet,
|
|
|
|
LinkLayerAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2021-02-12 13:27:44 +03: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 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
msg, err = req.GetInnerMessage()
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
resp, err = dhcpv6.NewAdvertiseFromSolicit(msg)
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
assert.True(t, s.process(msg, req, resp))
|
|
|
|
})
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-03 18:20:01 +03:00
|
|
|
resp.AddOption(dhcpv6.OptServerID(s.sid))
|
|
|
|
|
2021-02-12 13:27:44 +03: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 16:48:46 +03:00
|
|
|
|
2021-02-12 13:27:44 +03: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 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
msg, err = req.GetInnerMessage()
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
resp, err = dhcpv6.NewReplyFromMessage(msg)
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2020-07-03 18:20:01 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
assert.True(t, s.process(msg, req, resp))
|
|
|
|
})
|
2021-06-16 16:48:46 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-12 13:27:44 +03:00
|
|
|
|
|
|
|
t.Run("reply", func(t *testing.T) {
|
|
|
|
require.Equal(t, dhcpv6.MessageTypeReply, resp.Type())
|
2021-06-16 16:48:46 +03:00
|
|
|
|
2021-02-12 13:27:44 +03:00
|
|
|
oia = resp.Options.OneIANA()
|
|
|
|
oiaAddr = oia.Options.OneAddress()
|
|
|
|
assert.Equal(t, "2001::2", oiaAddr.IPv6Addr.String())
|
|
|
|
})
|
2020-07-03 18:20:01 +03:00
|
|
|
|
|
|
|
dnsAddrs := resp.Options.DNS()
|
2021-02-12 13:27:44 +03:00
|
|
|
require.Len(t, dnsAddrs, 1)
|
2021-06-16 16:48:46 +03:00
|
|
|
|
2021-02-12 13:27:44 +03: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 16:48:46 +03:00
|
|
|
|
2021-02-12 13:27:44 +03: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 18:20:01 +03:00
|
|
|
}
|
2023-04-12 14:48:42 +03: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{
|
|
|
|
leases: []*Lease{{
|
|
|
|
Hostname: staticName,
|
|
|
|
HWAddr: staticMAC,
|
|
|
|
IP: staticIP,
|
|
|
|
IsStatic: true,
|
|
|
|
}, {
|
|
|
|
Expiry: time.Unix(10, 0),
|
|
|
|
Hostname: anotherName,
|
|
|
|
HWAddr: anotherMAC,
|
|
|
|
IP: anotherIP,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.leases = []*Lease{{
|
|
|
|
Hostname: staticName,
|
|
|
|
HWAddr: staticMAC,
|
|
|
|
IP: staticIP,
|
|
|
|
IsStatic: true,
|
|
|
|
}, {
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|