From 5d4bbd8ddaf9104f0b5e7570d84e3adb89c3ab79 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 17 May 2023 12:43:20 +1000 Subject: [PATCH] Improve test assertion for Linux --- reuse_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/reuse_test.go b/reuse_test.go index ba53f288..5da4ab80 100644 --- a/reuse_test.go +++ b/reuse_test.go @@ -2,7 +2,6 @@ package torrent import ( "context" - "errors" "net" "sync/atomic" "syscall" @@ -19,17 +18,25 @@ func TestTcpPortReuseIsABadIdea(t *testing.T) { c.Assert(err, qt.IsNil) defer remote.Close() dialer := net.Dialer{} + // Show that we can't duplicate an existing connection even with various socket options. dialer.Control = func(network, address string, c syscall.RawConn) (err error) { return c.Control(func(fd uintptr) { err = setReusePortSockOpts(fd) }) } + // Tie up a local port to the remote. first, err := dialer.Dial("tcp", remote.Addr().String()) c.Assert(err, qt.IsNil) defer first.Close() + // Show that dialling the remote with the same local port fails. dialer.LocalAddr = first.LocalAddr() _, err = dialer.Dial("tcp", remote.Addr().String()) - c.Assert(errors.Is(err, syscall.EADDRINUSE), qt.IsTrue) + c.Assert(err, qt.IsNotNil) + // Show that not fixing the local port again allows connections to succeed. + dialer.LocalAddr = nil + second, err := dialer.Dial("tcp", remote.Addr().String()) + c.Assert(err, qt.IsNil) + second.Close() } // Show that multiple connections from the same local utp socket to the same remote port will -- 2.44.0