From 0386d22cc946dfc58b31bd8d3936158a5a6b2800 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 3 Aug 2019 18:24:12 +0100 Subject: [PATCH] vfs: add test for 0 length files read in the way mount does --- vfs/write_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/vfs/write_test.go b/vfs/write_test.go index 6c607ec08..867326357 100644 --- a/vfs/write_test.go +++ b/vfs/write_test.go @@ -2,6 +2,7 @@ package vfs import ( "context" + "io" "os" "sync" "testing" @@ -286,3 +287,32 @@ func TestWriteFileModTimeWithOpenWriters(t *testing.T) { assert.Equal(t, info.ModTime().Unix(), mtime.Unix()) } } + +func TestFileZeroLength(t *testing.T) { + r := fstest.NewRun(t) + defer r.Finalise() + vfs, fh := writeHandleCreate(t, r) + + // Close the file without writing to it + err := fh.Close() + if errors.Cause(err) == fs.ErrorCantUploadEmptyFiles { + t.Logf("skipping test: %v", err) + return + } + assert.NoError(t, err) + + // read the 0 length file back in using ReadAt into a buffer + // this simulates what mount does + rd, err := vfs.OpenFile("file1", os.O_RDONLY, 0) + require.NoError(t, err) + + buf := make([]byte, 1024) + n, err := rd.ReadAt(buf, 0) + if err != io.EOF { + assert.NoError(t, err) + } + assert.Equal(t, 0, n) + + err = rd.Close() + assert.NoError(t, err) +}