badguardhome/internal/dhcpd/broadcast_bsd_test.go
Eugene Burkov b77ceaaf55 Pull request: 3443 dhcp broadcast
Merge in DNS/adguard-home from 3443-fix-wired-dhcp to master

Updates #3443.

Squashed commit of the following:

commit ec7c3b73e274ba7c05c5856cb2f4ea218ad46870
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 7 18:21:07 2021 +0300

    dhcpd: imp docs & naming

commit d87c646af44d837c81aa032e82aad846bde97bc8
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 7 16:56:36 2021 +0300

    dhcpd: fix build tags & log changes

commit cbd0b3c1aa0efb32aeaa9130111719ab3861e817
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 7 16:26:50 2021 +0300

    dhcpd: revert 3443 & imp broadcasting
2021-09-07 18:33:23 +03:00

88 lines
1.7 KiB
Go

//go:build freebsd || openbsd
// +build freebsd openbsd
package dhcpd
import (
"bytes"
"net"
"testing"
"github.com/AdguardTeam/golibs/netutil"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestV4Server_Send_broadcast(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,
}
s := &v4Server{
conf: V4ServerConf{
broadcastIP: net.IP{1, 2, 3, 255},
},
}
testCases := []struct {
name string
req *dhcpv4.DHCPv4
resp *dhcpv4.DHCPv4
}{{
name: "nak",
req: &dhcpv4.DHCPv4{
GatewayIPAddr: netutil.IPv4Zero(),
},
resp: &dhcpv4.DHCPv4{
Options: dhcpv4.OptionsFromList(
dhcpv4.OptMessageType(dhcpv4.MessageTypeNak),
),
},
}, {
name: "fully_unspecified",
req: &dhcpv4.DHCPv4{
GatewayIPAddr: netutil.IPv4Zero(),
ClientIPAddr: netutil.IPv4Zero(),
},
resp: &dhcpv4.DHCPv4{
Options: dhcpv4.OptionsFromList(
dhcpv4.OptMessageType(dhcpv4.MessageTypeOffer),
),
},
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
s.send(cloneUDPAddr(defaultPeer), conn, tc.req, tc.resp)
assert.EqualValues(t, tc.resp.ToBytes(), b.Bytes())
assert.Equal(t, &net.UDPAddr{
IP: s.conf.broadcastIP,
Port: defaultPeer.Port,
Zone: defaultPeer.Zone,
}, peer)
})
b.Reset()
peer = nil
}
}