From 62a3f6317a49b19232e67faa31d90f94b522eb82 Mon Sep 17 00:00:00 2001 From: Anatole Denis Date: Tue, 15 Oct 2019 10:27:38 +0200 Subject: 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 --- dhcpv4/server4/server_test.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'dhcpv4/server4/server_test.go') 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) } -- cgit v1.2.3