diff options
author | Joe Tsai <joetsai@digital-static.net> | 2015-09-22 02:14:28 -0700 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2015-09-23 08:29:13 +0000 |
commit | b0a1f6462f34c9907ea35813b7ffa0f600646e80 (patch) | |
tree | a2ad7118b4e7f6ac2e45ee1cdd9973c67c978544 /src/compress/gzip/gunzip_test.go | |
parent | 9deb940d2481396944f090dd9f205f2d5b3d94ee (diff) | |
download | go-b0a1f6462f34c9907ea35813b7ffa0f600646e80.tar.gz go-b0a1f6462f34c9907ea35813b7ffa0f600646e80.zip |
compress/gzip: detect truncated streams
Reader fails to detect truncated streams since calls to io.ReadFull
do not check if the error is io.EOF.
Change-Id: I052cd03161e43fec17e3d328106c40e17923e52b
Reviewed-on: https://go-review.googlesource.com/14832
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/compress/gzip/gunzip_test.go')
-rw-r--r-- | src/compress/gzip/gunzip_test.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/compress/gzip/gunzip_test.go b/src/compress/gzip/gunzip_test.go index 0636dec9ab..209896a7fc 100644 --- a/src/compress/gzip/gunzip_test.go +++ b/src/compress/gzip/gunzip_test.go @@ -6,6 +6,7 @@ package gzip import ( "bytes" + "compress/flate" "io" "io/ioutil" "os" @@ -408,3 +409,34 @@ Found: t.Fatalf("third reset: err=%v, want io.EOF", err) } } + +func TestNilStream(t *testing.T) { + // Go liberally interprets RFC1952 section 2.2 to mean that a gzip file + // consist of zero or more members. Thus, we test that a nil stream is okay. + _, err := NewReader(bytes.NewReader(nil)) + if err != io.EOF { + t.Fatalf("NewReader(nil) on empty stream: got %v, want &v", err, io.EOF) + } +} + +func TestTruncatedStreams(t *testing.T) { + const data = "\x1f\x8b\b\x04\x00\tn\x88\x00\xff\a\x00foo bar\xcbH\xcd\xc9\xc9\xd7Q(\xcf/\xcaI\x01\x04:r\xab\xff\f\x00\x00\x00" + + // Intentionally iterate starting with at least one byte in the stream. + for i := 1; i < len(data)-1; i++ { + r, err := NewReader(strings.NewReader(data[:i])) + if err != nil { + if err != io.ErrUnexpectedEOF { + t.Errorf("NewReader(%d) on truncated stream: got %v, want %v", i, err, io.ErrUnexpectedEOF) + } + continue + } + _, err = io.Copy(ioutil.Discard, r) + if ferr, ok := err.(*flate.ReadError); ok { + err = ferr.Err + } + if err != io.ErrUnexpectedEOF { + t.Errorf("io.Copy(%d) on truncated stream: got %v, want %v", i, err, io.ErrUnexpectedEOF) + } + } +} |