From d90ffa81630d367380fa22a71d174dc2be5e527d Mon Sep 17 00:00:00 2001 From: pkositsyn Date: Tue, 16 Mar 2021 23:50:48 +0300 Subject: [PATCH] Add tests for file leak --- fileleak/fileleak_test.go | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/fileleak/fileleak_test.go b/fileleak/fileleak_test.go index e8e36d7..936146e 100644 --- a/fileleak/fileleak_test.go +++ b/fileleak/fileleak_test.go @@ -4,12 +4,14 @@ import ( "bytes" "fmt" "io/ioutil" + "net" "os" "testing" "github.com/stretchr/testify/require" "gitlab.com/slon/shad-go/fileleak" + "gitlab.com/slon/shad-go/tools/testtool" ) type fakeT struct { @@ -77,3 +79,57 @@ func TestFileLeak_ReopenFile(t *testing.T) { f = ff }) } + +func TestFileLeak_ReopenSameFile(t *testing.T) { + f, err := os.Open("/proc/self/exe") + require.NoError(t, err) + defer f.Close() + + checkLeak(t, true, func() { + _ = f.Close() + + ff, err := os.Open("/proc/self/exe") + require.NoError(t, err) + f = ff + }) +} + +func TestFileLeak_PipeLeak(t *testing.T) { + checkLeak(t, true, func() { + f, _, err := os.Pipe() + require.NoError(t, err) + _ = f.Close() + }) +} + +func TestFileLeak_PipeNoLeak(t *testing.T) { + checkLeak(t, false, func() { + f, w, err := os.Pipe() + require.NoError(t, err) + _, _ = f.Close(), w.Close() + }) +} + +func TestFileLeak_SocketLeak(t *testing.T) { + var conn net.Listener + defer conn.Close() + + checkLeak(t, true, func() { + addr, err := testtool.GetFreePort() + require.NoError(t, err) + + conn, err = net.Listen("tcp", addr) + require.NoError(t, err) + }) +} + +func TestFileLeak_SocketNoLeak(t *testing.T) { + checkLeak(t, false, func() { + addr, err := testtool.GetFreePort() + require.NoError(t, err) + + conn, err := net.Listen("tcp", addr) + require.NoError(t, err) + _ = conn.Close() + }) +}