From fc99a5d7869299ca4dbe4567ac521789e7f15402 Mon Sep 17 00:00:00 2001 From: John Starich Date: Thu, 25 Jun 2020 23:25:49 -0500 Subject: [PATCH 1/2] Add failing test for reading a small zip file --- zipfs/file_test.go | 43 +++++++++++++++++++++++++++++++++++++++ zipfs/testdata/small.zip | Bin 0 -> 149 bytes 2 files changed, 43 insertions(+) create mode 100644 zipfs/file_test.go create mode 100644 zipfs/testdata/small.zip diff --git a/zipfs/file_test.go b/zipfs/file_test.go new file mode 100644 index 00000000..8a0aaee1 --- /dev/null +++ b/zipfs/file_test.go @@ -0,0 +1,43 @@ +package zipfs + +import ( + "archive/zip" + "io" + "testing" +) + +func TestFileRead(t *testing.T) { + zrc, err := zip.OpenReader("testdata/small.zip") + if err != nil { + t.Fatal(err) + } + zfs := New(&zrc.Reader) + f, err := zfs.Open("smallFile") + if err != nil { + t.Fatal(err) + } + info, err := f.Stat() + if err != nil { + t.Fatal(err) + } + chunkSize := info.Size() * 2 // read with extra large buffer + + buf := make([]byte, chunkSize) + n, err := f.Read(buf) + if err != io.EOF { + t.Fatal("Failed to read file to completion:", err) + } + if n != int(info.Size()) { + t.Errorf("Expected read length to be %d, found: %d", info.Size(), n) + } + + // read a second time to check f.offset and f.buf are correct + buf = make([]byte, chunkSize) + n, err = f.Read(buf) + if err != io.EOF { + t.Fatal("Failed to read a fully read file:", err) + } + if n != 0 { + t.Errorf("Expected read length to be 0, found: %d", n) + } +} diff --git a/zipfs/testdata/small.zip b/zipfs/testdata/small.zip new file mode 100644 index 0000000000000000000000000000000000000000..65d45a1ce67c7b569f969545a25e354dc6f9af56 GIT binary patch literal 149 zcmWIWW@Zs#-~d8&zzGy6&P~k8am&m}{p4}>LezA Date: Thu, 25 Jun 2020 23:27:17 -0500 Subject: [PATCH 2/2] Fix panic when not filling up zipfs's read buffer --- zipfs/file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zipfs/file.go b/zipfs/file.go index 57d72012..355f5f45 100644 --- a/zipfs/file.go +++ b/zipfs/file.go @@ -61,7 +61,7 @@ func (f *File) Read(p []byte) (n int, err error) { } err = f.fillBuffer(f.offset + int64(len(p))) n = copy(p, f.buf[f.offset:]) - f.offset += int64(len(p)) + f.offset += int64(n) return }