mime: fix maximum length of encoded-words

RFC 2047 recommends a maximum length of 75 characters for
encoded-words. Due to a bug, encoded-words were limited to 77
characters instead of 75.

Change-Id: I2ff9d013ab922df6fd542464ace70b1c46dc7ae7
Reviewed-on: https://go-review.googlesource.com/20918
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Alexandre Cesaro 2016-03-20 17:29:56 +01:00 committed by Brad Fitzpatrick
parent 1b6402ea9d
commit d733cef728
2 changed files with 33 additions and 4 deletions

View file

@ -71,7 +71,7 @@ const (
maxEncodedWordLen = 75
// maxContentLen is how much content can be encoded, ignoring the header and
// 2-byte footer.
maxContentLen = maxEncodedWordLen - len("=?UTF-8?") - len("?=")
maxContentLen = maxEncodedWordLen - len("=?UTF-8?q?") - len("?=")
)
var maxBase64Len = base64.StdEncoding.DecodedLen(maxContentLen)

View file

@ -31,10 +31,8 @@ func TestEncodeWord(t *testing.T) {
{QEncoding, utf8, strings.Repeat("é", 11), "=?utf-8?q?" + strings.Repeat("=C3=A9", 10) + "?= =?utf-8?q?=C3=A9?="},
{QEncoding, iso88591, strings.Repeat("\xe9", 22), "=?iso-8859-1?q?" + strings.Repeat("=E9", 22) + "?="},
{QEncoding, utf8, strings.Repeat("\x80", 22), "=?utf-8?q?" + strings.Repeat("=80", 21) + "?= =?utf-8?q?=80?="},
{BEncoding, utf8, strings.Repeat("é", 24), "=?utf-8?b?" + strings.Repeat("w6nDqcOp", 8) + "?="},
{BEncoding, utf8, strings.Repeat("é", 27), "=?utf-8?b?" + strings.Repeat("w6nDqcOp", 8) + "?= =?utf-8?b?w6nDqcOp?="},
{BEncoding, iso88591, strings.Repeat("\xe9", 45), "=?iso-8859-1?b?" + strings.Repeat("6enp", 15) + "?="},
{BEncoding, utf8, strings.Repeat("\x80", 51), "=?utf-8?b?" + strings.Repeat("gICA", 16) + "?= =?utf-8?b?gICA?="},
{BEncoding, utf8, strings.Repeat("\x80", 48), "=?utf-8?b?" + strings.Repeat("gICA", 15) + "?= =?utf-8?b?gICA?="},
}
for _, test := range tests {
@ -44,6 +42,37 @@ func TestEncodeWord(t *testing.T) {
}
}
func TestEncodedWordLength(t *testing.T) {
tests := []struct {
enc WordEncoder
src string
}{
{QEncoding, strings.Repeat("à", 30)},
{QEncoding, strings.Repeat("é", 60)},
{BEncoding, strings.Repeat("ï", 25)},
{BEncoding, strings.Repeat("ô", 37)},
{BEncoding, strings.Repeat("\x80", 50)},
{QEncoding, "{$firstname} Bienvendio a Apostolica, aquà inicia el camino de tu"},
}
for _, test := range tests {
s := test.enc.Encode("utf-8", test.src)
wordLen := 0
for i := 0; i < len(s); i++ {
if s[i] == ' ' {
wordLen = 0
continue
}
wordLen++
if wordLen > maxEncodedWordLen {
t.Errorf("Encode(%q) has more than %d characters: %q",
test.src, maxEncodedWordLen, s)
}
}
}
}
func TestDecodeWord(t *testing.T) {
tests := []struct {
src, exp string