diff options
author | Anatole Denis <natolumin@unverle.fr> | 2019-10-15 10:27:38 +0200 |
---|---|---|
committer | Anatole Denis <natolumin@unverle.fr> | 2019-10-15 11:19:43 +0200 |
commit | 62a3f6317a49b19232e67faa31d90f94b522eb82 (patch) | |
tree | 23b26a8f13dbe70e6f3b0f4f9379046d6a5caf29 | |
parent | 486c8612a71ba5e1f251fbbbf08a91e3d8f77bfa (diff) |
server4: Use port 0 in tests
Tests use a randPort() workaround for not using port 0, as port 0 is not
usable with raw sockets.
We don't actually use raw sockets, so port 0 is fine, this makes use of
it which ensures we avoid port collisions.
Signed-off-by: Anatole Denis <natolumin@unverle.fr>
-rw-r--r-- | dhcpv4/server4/server_test.go | 18 |
1 files changed, 6 insertions, 12 deletions
diff --git a/dhcpv4/server4/server_test.go b/dhcpv4/server4/server_test.go index e64be09..da2b199 100644 --- a/dhcpv4/server4/server_test.go +++ b/dhcpv4/server4/server_test.go @@ -22,13 +22,6 @@ func init() { rand.Seed(time.Now().UTC().UnixNano()) } -func randPort() int { - // can't use port 0 with raw sockets, so until we implement - // a non-raw-sockets client for non-static ports, we have to - // deal with this "randomness" - return 32*1024 + rand.Intn(65536-32*1024) -} - // DORAHandler is a server handler suitable for DORA transactions func DORAHandler(conn net.PacketConn, peer net.Addr, m *dhcpv4.DHCPv4) { if m == nil { @@ -65,27 +58,28 @@ func DORAHandler(conn net.PacketConn, peer net.Addr, m *dhcpv4.DHCPv4) { func setUpClientAndServer(t *testing.T, iface net.Interface, handler Handler) (*nclient4.Client, *Server) { // strong assumption, I know loAddr := net.ParseIP("127.0.0.1") - saddr := net.UDPAddr{ + saddr := &net.UDPAddr{ IP: loAddr, - Port: randPort(), + Port: 0, } caddr := net.UDPAddr{ IP: loAddr, - Port: randPort(), + Port: 0, } - s, err := NewServer("", &saddr, handler) + s, err := NewServer("", saddr, handler) if err != nil { t.Fatal(err) } go func() { _ = s.Serve() }() + saddr = s.conn.LocalAddr().(*net.UDPAddr) clientConn, err := NewIPv4UDPConn("", &caddr) if err != nil { t.Fatal(err) } - c, err := nclient4.NewWithConn(clientConn, iface.HardwareAddr, nclient4.WithServerAddr(&saddr)) + c, err := nclient4.NewWithConn(clientConn, iface.HardwareAddr, nclient4.WithServerAddr(saddr)) if err != nil { t.Fatal(err) } |