2021-05-21 11:55:42 +00:00
|
|
|
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
2021-06-15 16:42:41 +00:00
|
|
|
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
|
2021-05-21 11:55:42 +00:00
|
|
|
|
2020-07-03 15:20:01 +00:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
2021-09-07 15:33:23 +00:00
|
|
|
"bytes"
|
2020-07-03 15:20:01 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-07 15:33:23 +00:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2020-07-03 15:20:01 +00:00
|
|
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-12 10:27:44 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-03 15:20:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func notify4(flags uint32) {
|
|
|
|
}
|
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
func TestV4_AddRemove_static(t *testing.T) {
|
|
|
|
s, err := v4Create(V4ServerConf{
|
2020-07-03 15:20:01 +00:00
|
|
|
Enabled: true,
|
2021-01-13 13:56:05 +00:00
|
|
|
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},
|
2020-07-03 15:20:01 +00:00
|
|
|
notify: notify4,
|
2021-02-12 10:27:44 +00:00
|
|
|
})
|
2021-04-15 14:52:53 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-03 15:20:01 +00:00
|
|
|
|
|
|
|
ls := s.GetLeases(LeasesStatic)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.Empty(t, ls)
|
2020-07-03 15:20:01 +00:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
// Add static lease.
|
2021-06-16 13:48:46 +00:00
|
|
|
l := &Lease{
|
2021-05-06 10:02:48 +00:00
|
|
|
Hostname: "static-1.local",
|
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
IP: net.IP{192, 168, 10, 150},
|
2021-02-12 10:27:44 +00:00
|
|
|
}
|
2021-04-15 14:52:53 +00:00
|
|
|
|
|
|
|
err = s.AddStaticLease(l)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = s.AddStaticLease(l)
|
|
|
|
assert.Error(t, err)
|
2020-07-03 15:20:01 +00:00
|
|
|
|
|
|
|
ls = s.GetLeases(LeasesStatic)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.Len(t, ls, 1)
|
2021-04-15 14:52:53 +00:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.True(t, l.IP.Equal(ls[0].IP))
|
|
|
|
assert.Equal(t, l.HWAddr, ls[0].HWAddr)
|
2021-03-18 14:07:13 +00:00
|
|
|
assert.True(t, ls[0].IsStatic())
|
2020-07-03 15:20:01 +00:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
// Try to remove static lease.
|
2021-06-16 13:48:46 +00:00
|
|
|
err = s.RemoveStaticLease(&Lease{
|
2021-02-12 10:27:44 +00:00
|
|
|
IP: net.IP{192, 168, 10, 110},
|
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
2021-04-15 14:52:53 +00:00
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2020-07-03 15:20:01 +00:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
// Remove static lease.
|
2021-04-15 14:52:53 +00:00
|
|
|
err = s.RemoveStaticLease(l)
|
|
|
|
require.NoError(t, err)
|
2020-07-03 15:20:01 +00:00
|
|
|
ls = s.GetLeases(LeasesStatic)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.Empty(t, ls)
|
2020-07-03 15:20:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
func TestV4_AddReplace(t *testing.T) {
|
|
|
|
sIface, err := v4Create(V4ServerConf{
|
2020-07-03 15:20:01 +00:00
|
|
|
Enabled: true,
|
2021-01-13 13:56:05 +00:00
|
|
|
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},
|
2020-07-03 15:20:01 +00:00
|
|
|
notify: notify4,
|
2021-02-12 10:27:44 +00:00
|
|
|
})
|
2021-04-15 14:52:53 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
2021-02-04 12:12:34 +00:00
|
|
|
s, ok := sIface.(*v4Server)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
dynLeases := []Lease{{
|
2021-05-06 10:02:48 +00:00
|
|
|
Hostname: "dynamic-1.local",
|
|
|
|
HWAddr: net.HardwareAddr{0x11, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
IP: net.IP{192, 168, 10, 150},
|
2021-02-12 10:27:44 +00:00
|
|
|
}, {
|
2021-05-06 10:02:48 +00:00
|
|
|
Hostname: "dynamic-2.local",
|
|
|
|
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
IP: net.IP{192, 168, 10, 151},
|
2021-02-12 10:27:44 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for i := range dynLeases {
|
2021-03-18 14:07:13 +00:00
|
|
|
err = s.addLease(&dynLeases[i])
|
2021-04-15 14:52:53 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-03 15:20:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 13:48:46 +00:00
|
|
|
stLeases := []*Lease{{
|
2021-05-06 10:02:48 +00:00
|
|
|
Hostname: "static-1.local",
|
|
|
|
HWAddr: net.HardwareAddr{0x33, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
IP: net.IP{192, 168, 10, 150},
|
2021-02-12 10:27:44 +00:00
|
|
|
}, {
|
2021-05-06 10:02:48 +00:00
|
|
|
Hostname: "static-2.local",
|
|
|
|
HWAddr: net.HardwareAddr{0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
IP: net.IP{192, 168, 10, 152},
|
2021-02-12 10:27:44 +00:00
|
|
|
}}
|
2020-07-03 15:20:01 +00:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
for _, l := range stLeases {
|
2021-04-15 14:52:53 +00:00
|
|
|
err = s.AddStaticLease(l)
|
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
}
|
2020-07-03 15:20:01 +00:00
|
|
|
|
|
|
|
ls := s.GetLeases(LeasesStatic)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.Len(t, ls, 2)
|
2020-07-03 15:20:01 +00: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)
|
2021-03-18 14:07:13 +00:00
|
|
|
assert.True(t, l.IsStatic())
|
2021-02-12 10:27:44 +00:00
|
|
|
}
|
2020-07-03 15:20:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
func TestV4StaticLease_Get(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
sIface, err := v4Create(V4ServerConf{
|
2020-07-03 15:20:01 +00:00
|
|
|
Enabled: true,
|
2021-01-13 13:56:05 +00:00
|
|
|
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},
|
2020-07-03 15:20:01 +00:00
|
|
|
notify: notify4,
|
2021-02-12 10:27:44 +00:00
|
|
|
})
|
2021-04-15 14:52:53 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
2021-02-04 12:12:34 +00:00
|
|
|
s, ok := sIface.(*v4Server)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.True(t, ok)
|
2021-04-15 14:52:53 +00:00
|
|
|
|
2021-01-13 13:56:05 +00:00
|
|
|
s.conf.dnsIPAddrs = []net.IP{{192, 168, 10, 1}}
|
2020-07-03 15:20:01 +00:00
|
|
|
|
2021-06-16 13:48:46 +00:00
|
|
|
l := &Lease{
|
2021-05-06 10:02:48 +00:00
|
|
|
Hostname: "static-1.local",
|
|
|
|
HWAddr: net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},
|
|
|
|
IP: net.IP{192, 168, 10, 150},
|
2021-02-12 10:27:44 +00:00
|
|
|
}
|
2021-04-15 14:52:53 +00:00
|
|
|
err = s.AddStaticLease(l)
|
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
var req, resp *dhcpv4.DHCPv4
|
|
|
|
mac := net.HardwareAddr{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
|
|
|
|
|
|
|
|
t.Run("discover", func(t *testing.T) {
|
2021-04-15 14:52:53 +00:00
|
|
|
req, err = dhcpv4.NewDiscovery(mac)
|
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
2021-04-15 14:52:53 +00:00
|
|
|
resp, err = dhcpv4.NewReplyFromRequest(req)
|
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, s.process(req, resp))
|
|
|
|
})
|
2021-04-15 14:52:53 +00:00
|
|
|
|
|
|
|
// Don't continue if we got any errors in the previous subtest.
|
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
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()))
|
2021-04-20 12:07:57 +00:00
|
|
|
assert.Equal(t, s.conf.subnet.Mask, resp.SubnetMask())
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("request", func(t *testing.T) {
|
|
|
|
req, err = dhcpv4.NewRequestFromOffer(resp)
|
2021-04-15 14:52:53 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
resp, err = dhcpv4.NewReplyFromRequest(req)
|
2021-04-15 14:52:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, 1, s.process(req, resp))
|
|
|
|
})
|
2021-04-15 14:52:53 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
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()))
|
2021-04-20 12:07:57 +00:00
|
|
|
assert.Equal(t, s.conf.subnet.Mask, resp.SubnetMask())
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
|
|
|
|
})
|
2020-07-03 15:20:01 +00:00
|
|
|
|
|
|
|
dnsAddrs := resp.DNS()
|
2021-02-12 10:27:44 +00:00
|
|
|
require.Len(t, dnsAddrs, 1)
|
2021-04-15 14:52:53 +00:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
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)
|
2021-04-15 14:52:53 +00:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.True(t, l.IP.Equal(ls[0].IP))
|
|
|
|
assert.Equal(t, mac, ls[0].HWAddr)
|
|
|
|
})
|
2020-07-03 15:20:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
func TestV4DynamicLease_Get(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
sIface, err := v4Create(V4ServerConf{
|
2020-07-03 15:20:01 +00:00
|
|
|
Enabled: true,
|
2021-01-13 13:56:05 +00:00
|
|
|
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},
|
2020-07-03 15:20:01 +00:00
|
|
|
notify: notify4,
|
2020-08-25 11:07:11 +00:00
|
|
|
Options: []string{
|
|
|
|
"81 hex 303132",
|
|
|
|
"82 ip 1.2.3.4",
|
|
|
|
},
|
2021-02-12 10:27:44 +00:00
|
|
|
})
|
2021-04-15 14:52:53 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
2021-02-04 12:12:34 +00:00
|
|
|
s, ok := sIface.(*v4Server)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.True(t, ok)
|
2021-04-15 14:52:53 +00:00
|
|
|
|
2021-01-13 13:56:05 +00:00
|
|
|
s.conf.dnsIPAddrs = []net.IP{{192, 168, 10, 1}}
|
2020-07-03 15:20:01 +00:00
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
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)
|
2021-04-15 14:52:53 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
resp, err = dhcpv4.NewReplyFromRequest(req)
|
2021-04-15 14:52:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, 1, s.process(req, resp))
|
|
|
|
})
|
2021-03-16 16:11:32 +00:00
|
|
|
|
|
|
|
// Don't continue if we got any errors in the previous subtest.
|
2021-04-15 14:52:53 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
t.Run("offer", func(t *testing.T) {
|
|
|
|
assert.Equal(t, dhcpv4.MessageTypeOffer, resp.MessageType())
|
|
|
|
assert.Equal(t, mac, resp.ClientHWAddr)
|
2021-03-16 16:11:32 +00:00
|
|
|
|
|
|
|
assert.Equal(t, s.conf.RangeStart, resp.YourIPAddr)
|
|
|
|
assert.Equal(t, s.conf.GatewayIP, resp.ServerIdentifier())
|
|
|
|
|
|
|
|
router := resp.Router()
|
|
|
|
require.Len(t, router, 1)
|
2021-04-15 14:52:53 +00:00
|
|
|
|
2021-03-16 16:11:32 +00:00
|
|
|
assert.Equal(t, s.conf.GatewayIP, router[0])
|
|
|
|
|
2021-04-20 12:07:57 +00:00
|
|
|
assert.Equal(t, s.conf.subnet.Mask, resp.SubnetMask())
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
|
|
|
|
assert.Equal(t, []byte("012"), resp.Options[uint8(dhcpv4.OptionFQDN)])
|
2021-03-16 16:11:32 +00:00
|
|
|
|
|
|
|
assert.Equal(t, net.IP{1, 2, 3, 4}, net.IP(resp.RelayAgentInfo().ToBytes()))
|
2021-02-12 10:27:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("request", func(t *testing.T) {
|
2021-04-15 14:52:53 +00:00
|
|
|
req, err = dhcpv4.NewRequestFromOffer(resp)
|
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
2021-04-15 14:52:53 +00:00
|
|
|
resp, err = dhcpv4.NewReplyFromRequest(req)
|
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, s.process(req, resp))
|
|
|
|
})
|
2021-04-15 14:52:53 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
2021-02-12 10:27:44 +00:00
|
|
|
|
|
|
|
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))
|
2021-05-06 10:02:48 +00:00
|
|
|
|
|
|
|
router := resp.Router()
|
|
|
|
require.Len(t, router, 1)
|
|
|
|
|
|
|
|
assert.Equal(t, s.conf.GatewayIP, router[0])
|
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.True(t, s.conf.GatewayIP.Equal(resp.ServerIdentifier()))
|
2021-04-20 12:07:57 +00:00
|
|
|
assert.Equal(t, s.conf.subnet.Mask, resp.SubnetMask())
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.Equal(t, s.conf.leaseTime.Seconds(), resp.IPAddressLeaseTime(-1).Seconds())
|
|
|
|
})
|
2020-07-03 15:20:01 +00:00
|
|
|
|
|
|
|
dnsAddrs := resp.DNS()
|
2021-02-12 10:27:44 +00:00
|
|
|
require.Len(t, dnsAddrs, 1)
|
2021-04-15 14:52:53 +00:00
|
|
|
|
2021-01-20 14:27:53 +00:00
|
|
|
assert.True(t, net.IP{192, 168, 10, 1}.Equal(dnsAddrs[0]))
|
2020-07-03 15:20:01 +00:00
|
|
|
|
|
|
|
// check lease
|
2021-02-12 10:27:44 +00:00
|
|
|
t.Run("check_lease", func(t *testing.T) {
|
|
|
|
ls := s.GetLeases(LeasesDynamic)
|
2021-04-15 14:52:53 +00:00
|
|
|
require.Len(t, ls, 1)
|
|
|
|
|
2021-02-12 10:27:44 +00:00
|
|
|
assert.True(t, net.IP{192, 168, 10, 100}.Equal(ls[0].IP))
|
|
|
|
assert.Equal(t, mac, ls[0].HWAddr)
|
|
|
|
})
|
|
|
|
}
|
2021-04-13 13:00:09 +00:00
|
|
|
|
2021-04-19 13:04:40 +00:00
|
|
|
func TestNormalizeHostname(t *testing.T) {
|
2021-04-13 13:00:09 +00:00
|
|
|
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",
|
|
|
|
}, {
|
2021-04-19 13:04:40 +00:00
|
|
|
name: "success_underscores",
|
|
|
|
hostname: "my_device_01",
|
|
|
|
wantErrMsg: "",
|
|
|
|
want: "my-device-01",
|
2021-04-13 13:00:09 +00:00
|
|
|
}, {
|
2021-04-19 13:04:40 +00:00
|
|
|
name: "error_part",
|
|
|
|
hostname: "device !!!",
|
|
|
|
wantErrMsg: "",
|
|
|
|
want: "device",
|
|
|
|
}, {
|
|
|
|
name: "error_part_spaces",
|
|
|
|
hostname: "device ! ! !",
|
|
|
|
wantErrMsg: "",
|
|
|
|
want: "device",
|
|
|
|
}, {
|
|
|
|
name: "error",
|
|
|
|
hostname: "!!!",
|
2021-05-06 10:02:48 +00:00
|
|
|
wantErrMsg: `normalizing "!!!": no valid parts`,
|
2021-04-19 13:04:40 +00:00
|
|
|
want: "",
|
|
|
|
}, {
|
|
|
|
name: "error_spaces",
|
|
|
|
hostname: "! ! !",
|
2021-05-06 10:02:48 +00:00
|
|
|
wantErrMsg: `normalizing "! ! !": no valid parts`,
|
2021-04-19 13:04:40 +00:00
|
|
|
want: "",
|
2021-04-13 13:00:09 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-04-19 13:04:40 +00:00
|
|
|
got, err := normalizeHostname(tc.hostname)
|
2021-04-13 13:00:09 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 15:33:23 +00:00
|
|
|
|
|
|
|
// fakePacketConn is a mock implementation of net.PacketConn to simplify
|
|
|
|
// testing.
|
|
|
|
type fakePacketConn struct {
|
|
|
|
// writeTo is used to substitute net.PacketConn's WriteTo method.
|
|
|
|
writeTo func(p []byte, addr net.Addr) (n int, err error)
|
|
|
|
// net.PacketConn is embedded here simply to make *fakePacketConn a
|
|
|
|
// net.PacketConn without actually implementing all methods.
|
|
|
|
net.PacketConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo implements net.PacketConn interface for *fakePacketConn.
|
|
|
|
func (fc *fakePacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|
|
|
return fc.writeTo(p, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestV4Server_Send_unicast(t *testing.T) {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
var peer *net.UDPAddr
|
|
|
|
|
|
|
|
conn := &fakePacketConn{
|
|
|
|
writeTo: func(p []byte, addr net.Addr) (n int, err error) {
|
|
|
|
udpPeer, ok := addr.(*net.UDPAddr)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
peer = cloneUDPAddr(udpPeer)
|
|
|
|
|
|
|
|
n, err = b.Write(p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultPeer := &net.UDPAddr{
|
|
|
|
IP: net.IP{1, 2, 3, 4},
|
|
|
|
// Use neither client nor server port.
|
|
|
|
Port: 1234,
|
|
|
|
}
|
|
|
|
defaultResp := &dhcpv4.DHCPv4{
|
|
|
|
OpCode: dhcpv4.OpcodeBootReply,
|
|
|
|
}
|
|
|
|
s := &v4Server{}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
req *dhcpv4.DHCPv4
|
|
|
|
wantPeer net.Addr
|
|
|
|
}{{
|
|
|
|
name: "relay_agent",
|
|
|
|
req: &dhcpv4.DHCPv4{
|
|
|
|
GatewayIPAddr: defaultPeer.IP,
|
|
|
|
},
|
|
|
|
wantPeer: &net.UDPAddr{
|
|
|
|
IP: defaultPeer.IP,
|
|
|
|
Port: dhcpv4.ServerPort,
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
name: "known_client",
|
|
|
|
req: &dhcpv4.DHCPv4{
|
|
|
|
GatewayIPAddr: netutil.IPv4Zero(),
|
|
|
|
ClientIPAddr: net.IP{2, 3, 4, 5},
|
|
|
|
},
|
|
|
|
wantPeer: &net.UDPAddr{
|
|
|
|
IP: net.IP{2, 3, 4, 5},
|
|
|
|
Port: dhcpv4.ClientPort,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
s.send(defaultPeer, conn, tc.req, defaultResp)
|
|
|
|
assert.EqualValues(t, defaultResp.ToBytes(), b.Bytes())
|
|
|
|
assert.Equal(t, tc.wantPeer, peer)
|
|
|
|
})
|
|
|
|
|
|
|
|
b.Reset()
|
|
|
|
peer = nil
|
|
|
|
}
|
|
|
|
}
|