Source file
src/mime/encodedword_test.go
1
2
3
4
5 package mime
6
7 import (
8 "errors"
9 "io"
10 "strings"
11 "testing"
12 )
13
14 func TestEncodeWord(t *testing.T) {
15 utf8, iso88591 := "utf-8", "iso-8859-1"
16 tests := []struct {
17 enc WordEncoder
18 charset string
19 src, exp string
20 }{
21 {QEncoding, utf8, "François-Jérôme", "=?utf-8?q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?="},
22 {BEncoding, utf8, "Café", "=?utf-8?b?Q2Fmw6k=?="},
23 {QEncoding, iso88591, "La Seleção", "=?iso-8859-1?q?La_Sele=C3=A7=C3=A3o?="},
24 {QEncoding, utf8, "", ""},
25 {QEncoding, utf8, "A", "A"},
26 {QEncoding, iso88591, "a", "a"},
27 {QEncoding, utf8, "123 456", "123 456"},
28 {QEncoding, utf8, "\t !\"#$%&'()*+,-./ :;<>?@[\\]^_`{|}~", "\t !\"#$%&'()*+,-./ :;<>?@[\\]^_`{|}~"},
29 {QEncoding, utf8, strings.Repeat("é", 10), "=?utf-8?q?" + strings.Repeat("=C3=A9", 10) + "?="},
30 {QEncoding, utf8, strings.Repeat("é", 11), "=?utf-8?q?" + strings.Repeat("=C3=A9", 10) + "?= =?utf-8?q?=C3=A9?="},
31 {QEncoding, iso88591, strings.Repeat("\xe9", 22), "=?iso-8859-1?q?" + strings.Repeat("=E9", 22) + "?="},
32 {QEncoding, utf8, strings.Repeat("\x80", 22), "=?utf-8?q?" + strings.Repeat("=80", 21) + "?= =?utf-8?q?=80?="},
33 {BEncoding, iso88591, strings.Repeat("\xe9", 45), "=?iso-8859-1?b?" + strings.Repeat("6enp", 15) + "?="},
34 {BEncoding, utf8, strings.Repeat("\x80", 48), "=?utf-8?b?" + strings.Repeat("gICA", 15) + "?= =?utf-8?b?gICA?="},
35 }
36
37 for _, test := range tests {
38 if s := test.enc.Encode(test.charset, test.src); s != test.exp {
39 t.Errorf("Encode(%q) = %q, want %q", test.src, s, test.exp)
40 }
41 }
42 }
43
44 func TestEncodedWordLength(t *testing.T) {
45 tests := []struct {
46 enc WordEncoder
47 src string
48 }{
49 {QEncoding, strings.Repeat("à", 30)},
50 {QEncoding, strings.Repeat("é", 60)},
51 {BEncoding, strings.Repeat("ï", 25)},
52 {BEncoding, strings.Repeat("ô", 37)},
53 {BEncoding, strings.Repeat("\x80", 50)},
54 {QEncoding, "{$firstname} Bienvendio a Apostolica, aquà inicia el camino de tu"},
55 }
56
57 for _, test := range tests {
58 s := test.enc.Encode("utf-8", test.src)
59 wordLen := 0
60 for i := 0; i < len(s); i++ {
61 if s[i] == ' ' {
62 wordLen = 0
63 continue
64 }
65
66 wordLen++
67 if wordLen > maxEncodedWordLen {
68 t.Errorf("Encode(%q) has more than %d characters: %q",
69 test.src, maxEncodedWordLen, s)
70 }
71 }
72 }
73 }
74
75 func TestDecodeWord(t *testing.T) {
76 tests := []struct {
77 src, exp string
78 hasErr bool
79 }{
80 {"=?UTF-8?Q?=C2=A1Hola,_se=C3=B1or!?=", "¡Hola, señor!", false},
81 {"=?UTF-8?Q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?=", "François-Jérôme", false},
82 {"=?UTF-8?q?ascii?=", "ascii", false},
83 {"=?utf-8?B?QW5kcsOp?=", "André", false},
84 {"=?ISO-8859-1?Q?Rapha=EBl_Dupont?=", "Raphaël Dupont", false},
85 {"=?utf-8?b?IkFudG9uaW8gSm9zw6kiIDxqb3NlQGV4YW1wbGUub3JnPg==?=", `"Antonio José" <jose@example.org>`, false},
86 {"=?UTF-8?A?Test?=", "", true},
87 {"=?UTF-8?Q?A=B?=", "", true},
88 {"=?UTF-8?Q?=A?=", "", true},
89 {"=?UTF-8?A?A?=", "", true},
90 {"=????=", "", true},
91 {"=?UTF-8???=", "", true},
92 {"=?UTF-8?Q??=", "", false},
93 }
94
95 for _, test := range tests {
96 dec := new(WordDecoder)
97 s, err := dec.Decode(test.src)
98 if test.hasErr && err == nil {
99 t.Errorf("Decode(%q) should return an error", test.src)
100 continue
101 }
102 if !test.hasErr && err != nil {
103 t.Errorf("Decode(%q): %v", test.src, err)
104 continue
105 }
106 if s != test.exp {
107 t.Errorf("Decode(%q) = %q, want %q", test.src, s, test.exp)
108 }
109 }
110 }
111
112 func TestDecodeHeader(t *testing.T) {
113 tests := []struct {
114 src, exp string
115 }{
116 {"=?UTF-8?Q?=C2=A1Hola,_se=C3=B1or!?=", "¡Hola, señor!"},
117 {"=?UTF-8?Q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?=", "François-Jérôme"},
118 {"=?UTF-8?q?ascii?=", "ascii"},
119 {"=?utf-8?B?QW5kcsOp?=", "André"},
120 {"=?ISO-8859-1?Q?Rapha=EBl_Dupont?=", "Raphaël Dupont"},
121 {"Jean", "Jean"},
122 {"=?utf-8?b?IkFudG9uaW8gSm9zw6kiIDxqb3NlQGV4YW1wbGUub3JnPg==?=", `"Antonio José" <jose@example.org>`},
123 {"=?UTF-8?A?Test?=", "=?UTF-8?A?Test?="},
124 {"=?UTF-8?Q?A=B?=", "=?UTF-8?Q?A=B?="},
125 {"=?UTF-8?Q?=A?=", "=?UTF-8?Q?=A?="},
126 {"=?UTF-8?A?A?=", "=?UTF-8?A?A?="},
127
128 {"=?", "=?"},
129 {"=?UTF-8?", "=?UTF-8?"},
130 {"=?UTF-8?=", "=?UTF-8?="},
131 {"=?UTF-8?Q", "=?UTF-8?Q"},
132 {"=?UTF-8?Q?", "=?UTF-8?Q?"},
133 {"=?UTF-8?Q?=", "=?UTF-8?Q?="},
134 {"=?UTF-8?Q?A", "=?UTF-8?Q?A"},
135 {"=?UTF-8?Q?A?", "=?UTF-8?Q?A?"},
136
137 {"=?ISO-8859-1?Q?a?=", "a"},
138 {"=?ISO-8859-1?Q?a?= b", "a b"},
139 {"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=", "ab"},
140 {"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=", "ab"},
141 {"=?ISO-8859-1?Q?a?= \r\n\t =?ISO-8859-1?Q?b?=", "ab"},
142 {"=?ISO-8859-1?Q?a_b?=", "a b"},
143
144 {"=?UTF-8?b?garbage?= =?UTF-8?b?QW5kcsOp?= =?UTF-8?b?garbage?=", "=?UTF-8?b?garbage?= André =?UTF-8?b?garbage?="},
145 {"=?UTF-8?b?QW5kcsOp", "=?UTF-8?b?QW5kcsOp"},
146 {"=?UTF-8?x?y?=?UTF-8?x?y=?", "=?UTF-8?x?y?=?UTF-8?x?y=?"},
147 }
148
149 for _, test := range tests {
150 dec := new(WordDecoder)
151 s, err := dec.DecodeHeader(test.src)
152 if err != nil {
153 t.Errorf("DecodeHeader(%q): %v", test.src, err)
154 }
155 if s != test.exp {
156 t.Errorf("DecodeHeader(%q) = %q, want %q", test.src, s, test.exp)
157 }
158 }
159 }
160
161 func TestCharsetDecoder(t *testing.T) {
162 tests := []struct {
163 src string
164 want string
165 charsets []string
166 content []string
167 }{
168 {"=?utf-8?b?Q2Fmw6k=?=", "Café", nil, nil},
169 {"=?ISO-8859-1?Q?caf=E9?=", "café", nil, nil},
170 {"=?US-ASCII?Q?foo_bar?=", "foo bar", nil, nil},
171 {"=?utf-8?Q?=?=", "=?utf-8?Q?=?=", nil, nil},
172 {"=?utf-8?Q?=A?=", "=?utf-8?Q?=A?=", nil, nil},
173 {
174 "=?ISO-8859-15?Q?f=F5=F6?= =?windows-1252?Q?b=E0r?=",
175 "f\xf5\xf6b\xe0r",
176 []string{"iso-8859-15", "windows-1252"},
177 []string{"f\xf5\xf6", "b\xe0r"},
178 },
179 }
180
181 for _, test := range tests {
182 i := 0
183 dec := &WordDecoder{
184 CharsetReader: func(charset string, input io.Reader) (io.Reader, error) {
185 if charset != test.charsets[i] {
186 t.Errorf("DecodeHeader(%q), got charset %q, want %q", test.src, charset, test.charsets[i])
187 }
188 content, err := io.ReadAll(input)
189 if err != nil {
190 t.Errorf("DecodeHeader(%q), error in reader: %v", test.src, err)
191 }
192 got := string(content)
193 if got != test.content[i] {
194 t.Errorf("DecodeHeader(%q), got content %q, want %q", test.src, got, test.content[i])
195 }
196 i++
197
198 return strings.NewReader(got), nil
199 },
200 }
201 got, err := dec.DecodeHeader(test.src)
202 if err != nil {
203 t.Errorf("DecodeHeader(%q): %v", test.src, err)
204 }
205 if got != test.want {
206 t.Errorf("DecodeHeader(%q) = %q, want %q", test.src, got, test.want)
207 }
208 }
209 }
210
211 func TestCharsetDecoderError(t *testing.T) {
212 dec := &WordDecoder{
213 CharsetReader: func(charset string, input io.Reader) (io.Reader, error) {
214 return nil, errors.New("Test error")
215 },
216 }
217
218 if _, err := dec.DecodeHeader("=?charset?Q?foo?="); err == nil {
219 t.Error("DecodeHeader should return an error")
220 }
221 }
222
223 func BenchmarkQEncodeWord(b *testing.B) {
224 for i := 0; i < b.N; i++ {
225 QEncoding.Encode("UTF-8", "¡Hola, señor!")
226 }
227 }
228
229 func BenchmarkQDecodeWord(b *testing.B) {
230 dec := new(WordDecoder)
231
232 for i := 0; i < b.N; i++ {
233 dec.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
234 }
235 }
236
237 func BenchmarkQDecodeHeader(b *testing.B) {
238 dec := new(WordDecoder)
239
240 for i := 0; i < b.N; i++ {
241 dec.DecodeHeader("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
242 }
243 }
244
View as plain text