From b4b319671c31920c09db2e6ae82b43d6d60d24d2 Mon Sep 17 00:00:00 2001 From: pkositsyn Date: Wed, 17 Mar 2021 00:07:14 +0300 Subject: [PATCH] Add dup and remove same name reopened file --- fileleak/fileleak_test.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/fileleak/fileleak_test.go b/fileleak/fileleak_test.go index 936146e..af3f271 100644 --- a/fileleak/fileleak_test.go +++ b/fileleak/fileleak_test.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net" "os" + "syscall" "testing" "github.com/stretchr/testify/require" @@ -80,20 +81,6 @@ func TestFileLeak_ReopenFile(t *testing.T) { }) } -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() @@ -133,3 +120,23 @@ func TestFileLeak_SocketNoLeak(t *testing.T) { _ = conn.Close() }) } + +func TestFileLeak_DupLeak(t *testing.T) { + var fd int + defer syscall.Close(fd) + + checkLeak(t, true, func() { + var err error + fd, err = syscall.Dup(1) + require.NoError(t, err) + + }) +} + +func TestFileLeak_DupNoLeak(t *testing.T) { + checkLeak(t, false, func() { + fd, err := syscall.Dup(1) + require.NoError(t, err) + _ = syscall.Close(fd) + }) +}