mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-28 18:08:51 +03:00
fffa656758
Merge in DNS/adguard-home from 4722-dhcp-http-panic to master Updates #4722. Squashed commit of the following: commit 8a8db48c3bd4f6bb7fabe65b5b7b162f0986fc76 Merge: 39b344f9b74b92fc
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 20:11:23 2022 +0300 Merge branch 'master' into 4722-dhcp-http-panic commit 39b344f97180af17ab22041e5655a27bcc99c29e Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 18:33:56 2022 +0300 dhcpd: imp code, fmt commit a36d70d2c25791b2e657e21d6f4681b33497f0cd Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 17:38:17 2022 +0300 dhcpd: imp names, docs commit 600d63da7af62de5cb52fc7670ef28c9f4fe95a7 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 17:36:17 2022 +0300 dhcpd: rename files, imp tags commit 44f5507649db8536a07c4c21c8ad6e4a60ba3f43 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 16:40:26 2022 +0300 dhcpd: add mock commit cfc3cfb714705067d3aa71a7cb5df4245e091cfd Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 16:15:27 2022 +0300 all: use ptr instead of value commit ec526c2cf22df3470641296cfc402113c23c3f9b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 14:57:10 2022 +0300 all: log changes commit 0eca09f4c72bbdc73a2334c839d7781847ba3962 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 14:50:32 2022 +0300 dhcpd: let v4 be unconfigured commit 59636e9ff48aea989d7bdfd216b37899b57137d2 Merge: 9238ca0abc1503af
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 13 14:50:17 2022 +0300 Merge branch 'master' into 4722-dhcp-http-panic commit 9238ca0a1e190ddc344f01959f474932809f086a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Sep 7 18:28:56 2022 +0300 dhcpd: imp conf commit 5f801c9be96c2fa735a50373495d8c6ca2914f32 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Sep 6 16:31:13 2022 +0300 dhcpd: hide behind iface commit a95c2741a7e3e5bfe8775bf937a3709217b76da0 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Aug 31 16:24:02 2022 +0300 dhcpd: separate os files
310 lines
7.1 KiB
Go
310 lines
7.1 KiB
Go
//go:build darwin || freebsd || linux || openbsd
|
|
|
|
package dhcpd
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/insomniacslk/dhcp/dhcpv6"
|
|
"github.com/insomniacslk/dhcp/iana"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func notify6(flags uint32) {
|
|
}
|
|
|
|
func TestV6_AddRemove_static(t *testing.T) {
|
|
s, err := v6Create(V6ServerConf{
|
|
Enabled: true,
|
|
RangeStart: net.ParseIP("2001::1"),
|
|
notify: notify6,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, s.GetLeases(LeasesStatic))
|
|
|
|
// Add static lease.
|
|
l := &Lease{
|
|
IP: net.ParseIP("2001::1"),
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
}
|
|
err = s.AddStaticLease(l)
|
|
require.NoError(t, err)
|
|
|
|
// Try to add the same static lease.
|
|
err = s.AddStaticLease(l)
|
|
require.Error(t, err)
|
|
|
|
ls := s.GetLeases(LeasesStatic)
|
|
require.Len(t, ls, 1)
|
|
|
|
assert.Equal(t, l.IP, ls[0].IP)
|
|
assert.Equal(t, l.HWAddr, ls[0].HWAddr)
|
|
assert.EqualValues(t, leaseExpireStatic, ls[0].Expiry.Unix())
|
|
|
|
// Try to remove non-existent static lease.
|
|
err = s.RemoveStaticLease(&Lease{
|
|
IP: net.ParseIP("2001::2"),
|
|
HWAddr: l.HWAddr,
|
|
})
|
|
require.Error(t, err)
|
|
|
|
// Remove static lease.
|
|
err = s.RemoveStaticLease(l)
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, s.GetLeases(LeasesStatic))
|
|
}
|
|
|
|
func TestV6_AddReplace(t *testing.T) {
|
|
sIface, err := v6Create(V6ServerConf{
|
|
Enabled: true,
|
|
RangeStart: net.ParseIP("2001::1"),
|
|
notify: notify6,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
s, ok := sIface.(*v6Server)
|
|
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)
|
|
}
|
|
|
|
stLeases := []*Lease{{
|
|
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},
|
|
}}
|
|
|
|
for _, l := range stLeases {
|
|
err = s.AddStaticLease(l)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
ls := s.GetLeases(LeasesStatic)
|
|
require.Len(t, ls, 2)
|
|
|
|
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())
|
|
}
|
|
}
|
|
|
|
func TestV6GetLease(t *testing.T) {
|
|
var err error
|
|
sIface, err := v6Create(V6ServerConf{
|
|
Enabled: true,
|
|
RangeStart: net.ParseIP("2001::1"),
|
|
notify: notify6,
|
|
})
|
|
require.NoError(t, err)
|
|
s, ok := sIface.(*v6Server)
|
|
|
|
require.True(t, ok)
|
|
|
|
dnsAddr := net.ParseIP("2000::1")
|
|
s.conf.dnsIPAddrs = []net.IP{dnsAddr}
|
|
s.sid = dhcpv6.Duid{
|
|
Type: dhcpv6.DUID_LLT,
|
|
HwType: iana.HWTypeEthernet,
|
|
LinkLayerAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
}
|
|
|
|
l := &Lease{
|
|
IP: net.ParseIP("2001::1"),
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
}
|
|
err = s.AddStaticLease(l)
|
|
require.NoError(t, err)
|
|
|
|
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)
|
|
require.NoError(t, err)
|
|
|
|
msg, err = req.GetInnerMessage()
|
|
require.NoError(t, err)
|
|
|
|
resp, err = dhcpv6.NewAdvertiseFromSolicit(msg)
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, s.process(msg, req, resp))
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
resp.AddOption(dhcpv6.OptServerID(s.sid))
|
|
|
|
var oia *dhcpv6.OptIANA
|
|
var oiaAddr *dhcpv6.OptIAAddress
|
|
t.Run("advertise", func(t *testing.T) {
|
|
require.Equal(t, dhcpv6.MessageTypeAdvertise, resp.Type())
|
|
|
|
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())
|
|
})
|
|
|
|
t.Run("request", func(t *testing.T) {
|
|
req, err = dhcpv6.NewRequestFromAdvertise(resp)
|
|
require.NoError(t, err)
|
|
|
|
msg, err = req.GetInnerMessage()
|
|
require.NoError(t, err)
|
|
|
|
resp, err = dhcpv6.NewReplyFromMessage(msg)
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, s.process(msg, req, resp))
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
t.Run("reply", func(t *testing.T) {
|
|
require.Equal(t, dhcpv6.MessageTypeReply, resp.Type())
|
|
|
|
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)
|
|
|
|
assert.Equal(t, l.IP, ls[0].IP)
|
|
assert.Equal(t, l.HWAddr, ls[0].HWAddr)
|
|
})
|
|
}
|
|
|
|
func TestV6GetDynamicLease(t *testing.T) {
|
|
sIface, err := v6Create(V6ServerConf{
|
|
Enabled: true,
|
|
RangeStart: net.ParseIP("2001::2"),
|
|
notify: notify6,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
s, ok := sIface.(*v6Server)
|
|
require.True(t, ok)
|
|
|
|
dnsAddr := net.ParseIP("2000::1")
|
|
s.conf.dnsIPAddrs = []net.IP{dnsAddr}
|
|
s.sid = dhcpv6.Duid{
|
|
Type: dhcpv6.DUID_LLT,
|
|
HwType: iana.HWTypeEthernet,
|
|
LinkLayerAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
}
|
|
|
|
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)
|
|
require.NoError(t, err)
|
|
|
|
msg, err = req.GetInnerMessage()
|
|
require.NoError(t, err)
|
|
|
|
resp, err = dhcpv6.NewAdvertiseFromSolicit(msg)
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, s.process(msg, req, resp))
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
resp.AddOption(dhcpv6.OptServerID(s.sid))
|
|
|
|
var oia *dhcpv6.OptIANA
|
|
var oiaAddr *dhcpv6.OptIAAddress
|
|
t.Run("advertise", func(t *testing.T) {
|
|
require.Equal(t, dhcpv6.MessageTypeAdvertise, resp.Type())
|
|
|
|
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)
|
|
require.NoError(t, err)
|
|
|
|
msg, err = req.GetInnerMessage()
|
|
require.NoError(t, err)
|
|
|
|
resp, err = dhcpv6.NewReplyFromMessage(msg)
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, s.process(msg, req, resp))
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
t.Run("reply", func(t *testing.T) {
|
|
require.Equal(t, dhcpv6.MessageTypeReply, resp.Type())
|
|
|
|
oia = resp.Options.OneIANA()
|
|
oiaAddr = oia.Options.OneAddress()
|
|
assert.Equal(t, "2001::2", oiaAddr.IPv6Addr.String())
|
|
})
|
|
|
|
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(LeasesDynamic)
|
|
require.Len(t, ls, 1)
|
|
|
|
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))
|
|
})
|
|
}
|
|
}
|