mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-25 22:45:46 +03:00
c6888326b0
Closes #2576. Updates #2275. Updates #2419. Updates #2443. Squashed commit of the following: commit b1a4809ada298d675de12740051ba26fb9945957 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri May 21 14:01:40 2021 +0300 all: add --local-frontend, upd docker commit 619ee7c82f27e3405753003dbec556ffb056d025 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu May 20 15:02:33 2021 +0300 bamboo-specs: bump docker version commit 5c2b2fbce80afdcc81fd0cb83674dc3d64facbf1 Merge: 6536b32d9c60aef6
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu May 20 15:01:47 2021 +0300 Merge branch 'master' into 2275-upd-go commit 6536b32dd4580425f7dedde6765463a79b9bd699 Merge: 9bb32bc46f7fd33a
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed May 19 20:38:48 2021 +0300 Merge branch 'master' into 2275-upd-go commit 9bb32bc4c0ac0f3a97195adc75359e48c9c58897 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed May 19 18:48:50 2021 +0300 all: fix build, imp err handling commit 6868eac7f7d2980fb706881f53e72afe5f7c3447 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed May 19 18:09:32 2021 +0300 all: fix github lint commit ebbb9c55f32fbd57e34e8b161016aa6b291c097c Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed May 19 17:36:56 2021 +0300 all: update go and backend tools
368 lines
9.2 KiB
Go
368 lines
9.2 KiB
Go
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
|
|
|
package dhcpd
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func notify4(flags uint32) {
|
|
}
|
|
|
|
func TestV4_AddRemove_static(t *testing.T) {
|
|
s, err := v4Create(V4ServerConf{
|
|
Enabled: true,
|
|
RangeStart: net.IP{192, 168, 10, 100},
|
|
RangeEnd: net.IP{192, 168, 10, 200},
|
|
GatewayIP: net.IP{192, 168, 10, 1},
|
|
SubnetMask: net.IP{255, 255, 255, 0},
|
|
notify: notify4,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
ls := s.GetLeases(LeasesStatic)
|
|
assert.Empty(t, ls)
|
|
|
|
// Add static lease.
|
|
l := Lease{
|
|
Hostname: "static-1.local",
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
IP: net.IP{192, 168, 10, 150},
|
|
}
|
|
|
|
err = s.AddStaticLease(l)
|
|
require.NoError(t, err)
|
|
|
|
err = s.AddStaticLease(l)
|
|
assert.Error(t, err)
|
|
|
|
ls = s.GetLeases(LeasesStatic)
|
|
require.Len(t, ls, 1)
|
|
|
|
assert.True(t, l.IP.Equal(ls[0].IP))
|
|
assert.Equal(t, l.HWAddr, ls[0].HWAddr)
|
|
assert.True(t, ls[0].IsStatic())
|
|
|
|
// Try to remove static lease.
|
|
err = s.RemoveStaticLease(Lease{
|
|
IP: net.IP{192, 168, 10, 110},
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
})
|
|
assert.Error(t, err)
|
|
|
|
// Remove static lease.
|
|
err = s.RemoveStaticLease(l)
|
|
require.NoError(t, err)
|
|
ls = s.GetLeases(LeasesStatic)
|
|
assert.Empty(t, ls)
|
|
}
|
|
|
|
func TestV4_AddReplace(t *testing.T) {
|
|
sIface, err := v4Create(V4ServerConf{
|
|
Enabled: true,
|
|
RangeStart: net.IP{192, 168, 10, 100},
|
|
RangeEnd: net.IP{192, 168, 10, 200},
|
|
GatewayIP: net.IP{192, 168, 10, 1},
|
|
SubnetMask: net.IP{255, 255, 255, 0},
|
|
notify: notify4,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
s, ok := sIface.(*v4Server)
|
|
require.True(t, ok)
|
|
|
|
dynLeases := []Lease{{
|
|
Hostname: "dynamic-1.local",
|
|
HWAddr: net.HardwareAddr{0x11, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
IP: net.IP{192, 168, 10, 150},
|
|
}, {
|
|
Hostname: "dynamic-2.local",
|
|
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
IP: net.IP{192, 168, 10, 151},
|
|
}}
|
|
|
|
for i := range dynLeases {
|
|
err = s.addLease(&dynLeases[i])
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
stLeases := []Lease{{
|
|
Hostname: "static-1.local",
|
|
HWAddr: net.HardwareAddr{0x33, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
IP: net.IP{192, 168, 10, 150},
|
|
}, {
|
|
Hostname: "static-2.local",
|
|
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
IP: net.IP{192, 168, 10, 152},
|
|
}}
|
|
|
|
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.True(t, l.IsStatic())
|
|
}
|
|
}
|
|
|
|
func TestV4StaticLease_Get(t *testing.T) {
|
|
var err error
|
|
sIface, err := v4Create(V4ServerConf{
|
|
Enabled: true,
|
|
RangeStart: net.IP{192, 168, 10, 100},
|
|
RangeEnd: net.IP{192, 168, 10, 200},
|
|
GatewayIP: net.IP{192, 168, 10, 1},
|
|
SubnetMask: net.IP{255, 255, 255, 0},
|
|
notify: notify4,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
s, ok := sIface.(*v4Server)
|
|
require.True(t, ok)
|
|
|
|
s.conf.dnsIPAddrs = []net.IP{{192, 168, 10, 1}}
|
|
|
|
l := Lease{
|
|
Hostname: "static-1.local",
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
IP: net.IP{192, 168, 10, 150},
|
|
}
|
|
err = s.AddStaticLease(l)
|
|
require.NoError(t, err)
|
|
|
|
var req, resp *dhcpv4.DHCPv4
|
|
mac := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
|
|
|
|
t.Run("discover", func(t *testing.T) {
|
|
req, err = dhcpv4.NewDiscovery(mac)
|
|
require.NoError(t, err)
|
|
|
|
resp, err = dhcpv4.NewReplyFromRequest(req)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, s.process(req, resp))
|
|
})
|
|
|
|
// Don't continue if we got any errors in the previous subtest.
|
|
require.NoError(t, err)
|
|
|
|
t.Run("offer", func(t *testing.T) {
|
|
assert.Equal(t, dhcpv4.MessageTypeOffer, resp.MessageType())
|
|
assert.Equal(t, mac, resp.ClientHWAddr)
|
|
assert.True(t, l.IP.Equal(resp.YourIPAddr))
|
|
assert.True(t, s.conf.GatewayIP.Equal(resp.Router()[0]))
|
|
assert.True(t, s.conf.GatewayIP.Equal(resp.ServerIdentifier()))
|
|
assert.Equal(t, s.conf.subnet.Mask, resp.SubnetMask())
|
|
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
|
|
})
|
|
|
|
t.Run("request", func(t *testing.T) {
|
|
req, err = dhcpv4.NewRequestFromOffer(resp)
|
|
require.NoError(t, err)
|
|
|
|
resp, err = dhcpv4.NewReplyFromRequest(req)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, s.process(req, resp))
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Run("ack", func(t *testing.T) {
|
|
assert.Equal(t, dhcpv4.MessageTypeAck, resp.MessageType())
|
|
assert.Equal(t, mac, resp.ClientHWAddr)
|
|
assert.True(t, l.IP.Equal(resp.YourIPAddr))
|
|
assert.True(t, s.conf.GatewayIP.Equal(resp.Router()[0]))
|
|
assert.True(t, s.conf.GatewayIP.Equal(resp.ServerIdentifier()))
|
|
assert.Equal(t, s.conf.subnet.Mask, resp.SubnetMask())
|
|
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
|
|
})
|
|
|
|
dnsAddrs := resp.DNS()
|
|
require.Len(t, dnsAddrs, 1)
|
|
|
|
assert.True(t, s.conf.GatewayIP.Equal(dnsAddrs[0]))
|
|
|
|
t.Run("check_lease", func(t *testing.T) {
|
|
ls := s.GetLeases(LeasesStatic)
|
|
require.Len(t, ls, 1)
|
|
|
|
assert.True(t, l.IP.Equal(ls[0].IP))
|
|
assert.Equal(t, mac, ls[0].HWAddr)
|
|
})
|
|
}
|
|
|
|
func TestV4DynamicLease_Get(t *testing.T) {
|
|
var err error
|
|
sIface, err := v4Create(V4ServerConf{
|
|
Enabled: true,
|
|
RangeStart: net.IP{192, 168, 10, 100},
|
|
RangeEnd: net.IP{192, 168, 10, 200},
|
|
GatewayIP: net.IP{192, 168, 10, 1},
|
|
SubnetMask: net.IP{255, 255, 255, 0},
|
|
notify: notify4,
|
|
Options: []string{
|
|
"81 hex 303132",
|
|
"82 ip 1.2.3.4",
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
s, ok := sIface.(*v4Server)
|
|
require.True(t, ok)
|
|
|
|
s.conf.dnsIPAddrs = []net.IP{{192, 168, 10, 1}}
|
|
|
|
var req, resp *dhcpv4.DHCPv4
|
|
mac := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
|
|
|
|
t.Run("discover", func(t *testing.T) {
|
|
req, err = dhcpv4.NewDiscovery(mac)
|
|
require.NoError(t, err)
|
|
|
|
resp, err = dhcpv4.NewReplyFromRequest(req)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, s.process(req, resp))
|
|
})
|
|
|
|
// Don't continue if we got any errors in the previous subtest.
|
|
require.NoError(t, err)
|
|
|
|
t.Run("offer", func(t *testing.T) {
|
|
assert.Equal(t, dhcpv4.MessageTypeOffer, resp.MessageType())
|
|
assert.Equal(t, mac, resp.ClientHWAddr)
|
|
|
|
assert.Equal(t, s.conf.RangeStart, resp.YourIPAddr)
|
|
assert.Equal(t, s.conf.GatewayIP, resp.ServerIdentifier())
|
|
|
|
router := resp.Router()
|
|
require.Len(t, router, 1)
|
|
|
|
assert.Equal(t, s.conf.GatewayIP, router[0])
|
|
|
|
assert.Equal(t, s.conf.subnet.Mask, resp.SubnetMask())
|
|
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
|
|
assert.Equal(t, []byte("012"), resp.Options[uint8(dhcpv4.OptionFQDN)])
|
|
|
|
assert.Equal(t, net.IP{1, 2, 3, 4}, net.IP(resp.RelayAgentInfo().ToBytes()))
|
|
})
|
|
|
|
t.Run("request", func(t *testing.T) {
|
|
req, err = dhcpv4.NewRequestFromOffer(resp)
|
|
require.NoError(t, err)
|
|
|
|
resp, err = dhcpv4.NewReplyFromRequest(req)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, s.process(req, resp))
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Run("ack", func(t *testing.T) {
|
|
assert.Equal(t, dhcpv4.MessageTypeAck, resp.MessageType())
|
|
assert.Equal(t, mac, resp.ClientHWAddr)
|
|
assert.True(t, s.conf.RangeStart.Equal(resp.YourIPAddr))
|
|
|
|
router := resp.Router()
|
|
require.Len(t, router, 1)
|
|
|
|
assert.Equal(t, s.conf.GatewayIP, router[0])
|
|
|
|
assert.True(t, s.conf.GatewayIP.Equal(resp.ServerIdentifier()))
|
|
assert.Equal(t, s.conf.subnet.Mask, resp.SubnetMask())
|
|
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
|
|
})
|
|
|
|
dnsAddrs := resp.DNS()
|
|
require.Len(t, dnsAddrs, 1)
|
|
|
|
assert.True(t, net.IP{192, 168, 10, 1}.Equal(dnsAddrs[0]))
|
|
|
|
// check lease
|
|
t.Run("check_lease", func(t *testing.T) {
|
|
ls := s.GetLeases(LeasesDynamic)
|
|
require.Len(t, ls, 1)
|
|
|
|
assert.True(t, net.IP{192, 168, 10, 100}.Equal(ls[0].IP))
|
|
assert.Equal(t, mac, ls[0].HWAddr)
|
|
})
|
|
}
|
|
|
|
func TestNormalizeHostname(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
hostname string
|
|
wantErrMsg string
|
|
want string
|
|
}{{
|
|
name: "success",
|
|
hostname: "example.com",
|
|
wantErrMsg: "",
|
|
want: "example.com",
|
|
}, {
|
|
name: "success_empty",
|
|
hostname: "",
|
|
wantErrMsg: "",
|
|
want: "",
|
|
}, {
|
|
name: "success_spaces",
|
|
hostname: "my device 01",
|
|
wantErrMsg: "",
|
|
want: "my-device-01",
|
|
}, {
|
|
name: "success_underscores",
|
|
hostname: "my_device_01",
|
|
wantErrMsg: "",
|
|
want: "my-device-01",
|
|
}, {
|
|
name: "error_part",
|
|
hostname: "device !!!",
|
|
wantErrMsg: "",
|
|
want: "device",
|
|
}, {
|
|
name: "error_part_spaces",
|
|
hostname: "device ! ! !",
|
|
wantErrMsg: "",
|
|
want: "device",
|
|
}, {
|
|
name: "error",
|
|
hostname: "!!!",
|
|
wantErrMsg: `normalizing "!!!": no valid parts`,
|
|
want: "",
|
|
}, {
|
|
name: "error_spaces",
|
|
hostname: "! ! !",
|
|
wantErrMsg: `normalizing "! ! !": no valid parts`,
|
|
want: "",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got, err := normalizeHostname(tc.hostname)
|
|
if tc.wantErrMsg == "" {
|
|
assert.NoError(t, err)
|
|
} else {
|
|
require.Error(t, err)
|
|
|
|
assert.Equal(t, tc.wantErrMsg, err.Error())
|
|
}
|
|
|
|
assert.Equal(t, tc.want, got)
|
|
})
|
|
}
|
|
}
|