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
This commit is contained in:
Eugene Burkov
2021-09-07 18:33:23 +03:00
parent bdd0ca5423
commit b77ceaaf55
8 changed files with 404 additions and 17 deletions

View File

@@ -4,9 +4,11 @@
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"
@@ -365,3 +367,83 @@ func TestNormalizeHostname(t *testing.T) {
})
}
}
// 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
}
}