From f9500729b79afeeb60f1d7aa58c20c542afe2465 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 10 May 2017 11:19:32 +0100 Subject: [PATCH] mountlib: fix cross platform tests --- cmd/mountlib/mounttest/read_non_unix.go | 13 ++++++ cmd/mountlib/mounttest/read_unix.go | 53 ++++++++++++++++++++++++ cmd/mountlib/mounttest/write_non_unix.go | 13 ++++++ cmd/mountlib/mounttest/write_unix.go | 50 ++++++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 cmd/mountlib/mounttest/read_non_unix.go create mode 100644 cmd/mountlib/mounttest/read_unix.go create mode 100644 cmd/mountlib/mounttest/write_non_unix.go create mode 100644 cmd/mountlib/mounttest/write_unix.go diff --git a/cmd/mountlib/mounttest/read_non_unix.go b/cmd/mountlib/mounttest/read_non_unix.go new file mode 100644 index 000000000..6545a8fec --- /dev/null +++ b/cmd/mountlib/mounttest/read_non_unix.go @@ -0,0 +1,13 @@ +// +build !linux,!darwin,!freebsd + +package mounttest + +import ( + "runtime" + "testing" +) + +// TestReadFileDoubleClose tests double close on read +func TestReadFileDoubleClose(t *testing.T) { + t.Skip("not supported on " + runtime.GOOS) +} diff --git a/cmd/mountlib/mounttest/read_unix.go b/cmd/mountlib/mounttest/read_unix.go new file mode 100644 index 000000000..a1c47c7f5 --- /dev/null +++ b/cmd/mountlib/mounttest/read_unix.go @@ -0,0 +1,53 @@ +// +build linux darwin freebsd + +package mounttest + +import ( + "os" + "syscall" + "testing" + + "github.com/stretchr/testify/assert" +) + +// TestReadFileDoubleClose tests double close on read +func TestReadFileDoubleClose(t *testing.T) { + run.skipIfNoFUSE(t) + + run.createFile(t, "testdoubleclose", "hello") + + in, err := os.Open(run.path("testdoubleclose")) + assert.NoError(t, err) + fd := in.Fd() + + fd1, err := syscall.Dup(int(fd)) + assert.NoError(t, err) + + fd2, err := syscall.Dup(int(fd)) + assert.NoError(t, err) + + // close one of the dups - should produce no error + err = syscall.Close(fd1) + assert.NoError(t, err) + + // read from the file + buf := make([]byte, 1) + _, err = in.Read(buf) + assert.NoError(t, err) + + // close it + err = in.Close() + assert.NoError(t, err) + + // read from the other dup - should produce no error as this + // file is now buffered + n, err := syscall.Read(fd2, buf) + assert.NoError(t, err) + assert.Equal(t, 1, n) + + // close the dup - should not produce an error + err = syscall.Close(fd2) + assert.NoError(t, err, "input/output error") + + run.rm(t, "testdoubleclose") +} diff --git a/cmd/mountlib/mounttest/write_non_unix.go b/cmd/mountlib/mounttest/write_non_unix.go new file mode 100644 index 000000000..7ef701270 --- /dev/null +++ b/cmd/mountlib/mounttest/write_non_unix.go @@ -0,0 +1,13 @@ +// +build !linux,!darwin,!freebsd + +package mounttest + +import ( + "runtime" + "testing" +) + +// TestWriteFileDoubleClose tests double close on write +func TestWriteFileDoubleClose(t *testing.T) { + t.Skip("not supported on " + runtime.GOOS) +} diff --git a/cmd/mountlib/mounttest/write_unix.go b/cmd/mountlib/mounttest/write_unix.go new file mode 100644 index 000000000..9fd279c8a --- /dev/null +++ b/cmd/mountlib/mounttest/write_unix.go @@ -0,0 +1,50 @@ +// +build linux darwin freebsd + +package mounttest + +import ( + "os" + "syscall" + "testing" + + "github.com/stretchr/testify/assert" +) + +// TestWriteFileDoubleClose tests double close on write +func TestWriteFileDoubleClose(t *testing.T) { + run.skipIfNoFUSE(t) + + out, err := os.Create(run.path("testdoubleclose")) + assert.NoError(t, err) + fd := out.Fd() + + fd1, err := syscall.Dup(int(fd)) + assert.NoError(t, err) + + fd2, err := syscall.Dup(int(fd)) + assert.NoError(t, err) + + // close one of the dups - should produce no error + err = syscall.Close(fd1) + assert.NoError(t, err) + + // write to the file + buf := []byte("hello") + n, err := out.Write(buf) + assert.NoError(t, err) + assert.Equal(t, 5, n) + + // close it + err = out.Close() + assert.NoError(t, err) + + // write to the other dup - should produce an error + n, err = syscall.Write(fd2, buf) + assert.Error(t, err, "input/output error") + + // close the dup - should produce an error + err = syscall.Close(fd2) + assert.Error(t, err, "input/output error") + + run.rm(t, "testdoubleclose") +}