2021-05-21 12:55:42 +01:00
|
|
|
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
2021-06-15 17:42:41 +01:00
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
2021-05-21 12:55:42 +01:00
|
|
|
|
2020-07-03 16:20:01 +01:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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.
|
2021-06-16 14:48:46 +01:00
|
|
|
l := &Lease{
|
2021-02-12 10:27:44 +00:00
|
|
|
IP: net.ParseIP("2001::1"),
|
|
|
|
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)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.EqualValues(t, leaseExpireStatic, ls[0].Expiry.Unix())
|
2020-07-03 16:20:01 +01:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
// Try to remove non-existent static lease.
|
2021-06-16 14:48:46 +01:00
|
|
|
err = s.RemoveStaticLease(&Lease{
|
2021-02-12 10:27:44 +00:00
|
|
|
IP: net.ParseIP("2001::2"),
|
|
|
|
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.
|
|
|
|
dynLeases := []*Lease{{
|
|
|
|
IP: net.ParseIP("2001::1"),
|
|
|
|
HWAddr: net.HardwareAddr{0x11, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}, {
|
|
|
|
IP: net.ParseIP("2001::2"),
|
|
|
|
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, l := range dynLeases {
|
|
|
|
s.addLease(l)
|
2020-07-03 16:20:01 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 14:48:46 +01:00
|
|
|
stLeases := []*Lease{{
|
2021-02-12 10:27:44 +00:00
|
|
|
IP: net.ParseIP("2001::1"),
|
|
|
|
HWAddr: net.HardwareAddr{0x33, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
}, {
|
|
|
|
IP: net.ParseIP("2001::3"),
|
|
|
|
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 {
|
|
|
|
assert.True(t, stLeases[i].IP.Equal(l.IP))
|
|
|
|
assert.Equal(t, stLeases[i].HWAddr, l.HWAddr)
|
|
|
|
assert.EqualValues(t, leaseExpireStatic, l.Expiry.Unix())
|
|
|
|
}
|
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}
|
2020-07-03 16:20:01 +01:00
|
|
|
s.sid = dhcpv6.Duid{
|
2021-02-12 10:27:44 +00:00
|
|
|
Type: dhcpv6.DUID_LLT,
|
|
|
|
HwType: iana.HWTypeEthernet,
|
|
|
|
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
|
|
|
|
2021-06-16 14:48:46 +01:00
|
|
|
l := &Lease{
|
2021-02-12 10:27:44 +00:00
|
|
|
IP: net.ParseIP("2001::1"),
|
|
|
|
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
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, l.IP, oiaAddr.IPv6Addr)
|
|
|
|
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()
|
|
|
|
|
|
|
|
assert.Equal(t, l.IP, oiaAddr.IPv6Addr)
|
|
|
|
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}
|
2020-07-03 16:20:01 +01:00
|
|
|
s.sid = dhcpv6.Duid{
|
2021-02-12 10:27:44 +00:00
|
|
|
Type: dhcpv6.DUID_LLT,
|
|
|
|
HwType: iana.HWTypeEthernet,
|
|
|
|
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
|
|
|
}
|