AdGuardHome/internal/dhcpd/broadcast_others_test.go
Eugene Burkov da86620288 Pull request: 3443 dhcp broadcast vol.2
Merge in DNS/adguard-home from 3443-dhcp-broadcast-vol.2 to master

Closes #3443.

Squashed commit of the following:

commit a85af89cb43f2489126fe3c12366fc034e89f59d
Merge: 72eb3a88 a4e07827
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Sep 30 18:08:19 2021 +0300

    Merge branch 'master' into 3443-dhcp-broadcast-vol.2

commit 72eb3a8853540b06ee1096decf50e836b539fe45
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Sep 30 18:03:19 2021 +0300

    dhcpd: imp code readability

commit 2d1fbc40d04a4125855d6be9f02e09d15430150d
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Sep 30 14:16:59 2021 +0300

    dhcpd: imp tests

commit 889fad3084ad2b81edfc12100e2ce29d323227ba
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Sep 29 20:09:25 2021 +0300

    dhcpd: imp code, docs

commit 1fd6b2346ff66e033bceaa169aed751be5822ca8
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Sep 23 16:08:18 2021 +0300

    dhcpd: unicast to mac address
2021-09-30 18:28:19 +03:00

57 lines
1.2 KiB
Go

//go:build aix || darwin || dragonfly || linux || netbsd || solaris
// +build aix darwin dragonfly linux netbsd solaris
package dhcpd
import (
"bytes"
"net"
"testing"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDHCPConn_Broadcast(t *testing.T) {
b := &bytes.Buffer{}
var peers []*net.UDPAddr
udpConn := &fakePacketConn{
writeTo: func(p []byte, addr net.Addr) (n int, err error) {
udpPeer, ok := addr.(*net.UDPAddr)
require.True(t, ok)
peers = append(peers, cloneUDPAddr(udpPeer))
n, err = b.Write(p)
require.NoError(t, err)
return n, nil
},
}
conn := &dhcpConn{
udpConn: udpConn,
bcastIP: net.IP{1, 2, 3, 255},
}
defaultPeer := &net.UDPAddr{
IP: net.IP{1, 2, 3, 4},
// Use neither client nor server port.
Port: 1234,
}
respData := (&dhcpv4.DHCPv4{}).ToBytes()
_, _ = conn.broadcast(respData, cloneUDPAddr(defaultPeer))
// The same response is written twice but for different peers.
assert.EqualValues(t, append(respData, respData...), b.Bytes())
require.Len(t, peers, 2)
assert.Equal(t, cloneUDPAddr(defaultPeer), peers[0])
assert.Equal(t, &net.UDPAddr{
IP: conn.bcastIP,
Port: defaultPeer.Port,
}, peers[1])
}