Commit 3e0c21e0 authored by Mark Ryan's avatar Mark Ryan Committed by Brad Fitzpatrick

encoding: fix endless loop in TestDecoderBuffering

The ascii85, base32 and base64 packages all contain a test called
TestDecoderBuffering.  Each of these tests contain a loop that ignores
the error returned from the Read method of their decoders.  The result
being that the tests loop for ever if the decoders actually return an
error.  This commit fixes the issue by terminating the loops if an error
occurs and failing the tests with a suitable error message.

Change-Id: Idb385673cf9f3f6f8befe4288b4be366ab0985fd
Reviewed-on: https://go-review.googlesource.com/46010Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent c52aca1c
...@@ -135,11 +135,15 @@ func TestDecoderBuffering(t *testing.T) { ...@@ -135,11 +135,15 @@ func TestDecoderBuffering(t *testing.T) {
decoder := NewDecoder(strings.NewReader(bigtest.encoded)) decoder := NewDecoder(strings.NewReader(bigtest.encoded))
buf := make([]byte, len(bigtest.decoded)+12) buf := make([]byte, len(bigtest.decoded)+12)
var total int var total int
for total = 0; total < len(bigtest.decoded); { var n int
n, err := decoder.Read(buf[total : total+bs]) var err error
testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, error(nil)) for total = 0; total < len(bigtest.decoded) && err == nil; {
n, err = decoder.Read(buf[total : total+bs])
total += n total += n
} }
if err != nil && err != io.EOF {
t.Errorf("Read from %q at pos %d = %d, unexpected error %v", bigtest.encoded, total, n, err)
}
testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded) testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded)
} }
} }
......
...@@ -284,11 +284,15 @@ func TestDecoderBuffering(t *testing.T) { ...@@ -284,11 +284,15 @@ func TestDecoderBuffering(t *testing.T) {
decoder := NewDecoder(StdEncoding, strings.NewReader(bigtest.encoded)) decoder := NewDecoder(StdEncoding, strings.NewReader(bigtest.encoded))
buf := make([]byte, len(bigtest.decoded)+12) buf := make([]byte, len(bigtest.decoded)+12)
var total int var total int
for total = 0; total < len(bigtest.decoded); { var n int
n, err := decoder.Read(buf[total : total+bs]) var err error
testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, error(nil)) for total = 0; total < len(bigtest.decoded) && err == nil; {
n, err = decoder.Read(buf[total : total+bs])
total += n total += n
} }
if err != nil && err != io.EOF {
t.Errorf("Read from %q at pos %d = %d, unexpected error %v", bigtest.encoded, total, n, err)
}
testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded) testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded)
} }
} }
......
...@@ -189,11 +189,15 @@ func TestDecoderBuffering(t *testing.T) { ...@@ -189,11 +189,15 @@ func TestDecoderBuffering(t *testing.T) {
decoder := NewDecoder(StdEncoding, strings.NewReader(bigtest.encoded)) decoder := NewDecoder(StdEncoding, strings.NewReader(bigtest.encoded))
buf := make([]byte, len(bigtest.decoded)+12) buf := make([]byte, len(bigtest.decoded)+12)
var total int var total int
for total = 0; total < len(bigtest.decoded); { var n int
n, err := decoder.Read(buf[total : total+bs]) var err error
testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, error(nil)) for total = 0; total < len(bigtest.decoded) && err == nil; {
n, err = decoder.Read(buf[total : total+bs])
total += n total += n
} }
if err != nil && err != io.EOF {
t.Errorf("Read from %q at pos %d = %d, unexpected error %v", bigtest.encoded, total, n, err)
}
testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded) testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment