From 60383327defb3317ca49e0dbc0381696065907b3 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Fri, 19 Feb 2021 19:31:40 +1100 Subject: [PATCH] gunzip: add EOF-handling tests Before this patchset, these tests would either lock up or fail with spurrious EOF errors. Signed-off-by: Aleksa Sarai --- gunzip_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/gunzip_test.go b/gunzip_test.go index 135a6a5..d73932d 100644 --- a/gunzip_test.go +++ b/gunzip_test.go @@ -570,6 +570,65 @@ func TestWriteTo(t *testing.T) { } } +func TestCopyAfterReadAll(t *testing.T) { + for _, tt := range gunzipTests { + if tt.err != nil { + // Only use valid gzip tests. + continue + } + + dec, err := NewReader(bytes.NewBuffer(tt.gzip)) + if err != nil { + t.Fatal(err) + } + + // Read the stream twice with io.Copy. + if b, err := ioutil.ReadAll(ioutil.NopCloser(dec)); err != nil { + t.Fatalf("ioutil.ReadAll end of stream: unexpected error %v", err) + } else if !bytes.Equal(b, []byte(tt.raw)) { + t.Fatal("ioutil.ReadAll output didn't match input") + } + if n, err := io.Copy(ioutil.Discard, dec); err != nil { + t.Fatalf("io.Copy end of stream: unexpected error %v", err) + } else if n != 0 { + t.Fatalf("io.Copy at end of stream should read no bytes, got %v", n) + } + } +} + +func TestMultipleCopy(t *testing.T) { + for _, tt := range gunzipTests { + if tt.err != nil { + // Only use valid gzip tests. + continue + } + + dec, err := NewReader(bytes.NewBuffer(tt.gzip)) + if err != nil { + t.Fatal(err) + } + + // Read to the end of the stream (using WriteTo). + if n, err := io.Copy(ioutil.Discard, dec); err != nil { + t.Fatalf("io.Copy full stream: unexpected error %v", err) + } else if n != int64(len(tt.raw)) { + t.Fatal("did not decompress everything") + } + + // Now try to read again using Read and WriteTo. + if b, err := ioutil.ReadAll(ioutil.NopCloser(dec)); err != nil { + t.Fatalf("ioutil.ReadAll end of stream: unexpected error %v", err) + } else if len(b) != 0 { + t.Fatalf("ioutil.ReadAll at end of stream should read no bytes, got %v", b) + } + if n, err := io.Copy(ioutil.Discard, dec); err != nil { + t.Fatalf("io.Copy end of stream: unexpected error %v", err) + } else if n != 0 { + t.Fatalf("io.Copy at end of stream should read no bytes, got %v", n) + } + } +} + func BenchmarkGunzipCopy(b *testing.B) { dat, _ := ioutil.ReadFile("testdata/test.json") dat = append(dat, dat...)