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