Commit 10529a01 authored by Gustav Westling's avatar Gustav Westling Committed by Ian Lance Taylor

encoding/base32: handle NoPadding when using buffered encoding in Close

This changes makes encoder.Close aware of how many bytes to write if there
is any data left in the buffer.

Fixes #25295

Change-Id: I4138891359935509cb561c453b8059ba2b9e576b
GitHub-Last-Rev: f374096d2f3cae8635506074f59e1cd440c14844
GitHub-Pull-Request: golang/go#25316
Reviewed-on: https://go-review.googlesource.com/112515
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 0f2d4d00
......@@ -244,8 +244,9 @@ func (e *encoder) Close() error {
// If there's anything left in the buffer, flush it out
if e.err == nil && e.nbuf > 0 {
e.enc.Encode(e.out[0:], e.buf[0:e.nbuf])
encodedLen := e.enc.EncodedLen(e.nbuf)
e.nbuf = 0
_, e.err = e.w.Write(e.out[0:8])
_, e.err = e.w.Write(e.out[0:encodedLen])
}
return e.err
}
......
......@@ -658,3 +658,31 @@ func TestEncodedDecodedLen(t *testing.T) {
})
}
}
func TestWithoutPaddingClose(t *testing.T) {
encodings := []*Encoding{
StdEncoding,
StdEncoding.WithPadding(NoPadding),
}
for _, encoding := range encodings {
for _, testpair := range pairs {
var buf bytes.Buffer
encoder := NewEncoder(encoding, &buf)
encoder.Write([]byte(testpair.decoded))
encoder.Close()
expected := testpair.encoded
if encoding.padChar == NoPadding {
expected = strings.Replace(expected, "=", "", -1)
}
res := buf.String()
if res != expected {
t.Errorf("Expected %s got %s; padChar=%d", expected, res, encoding.padChar)
}
}
}
}
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