Alexandre Cesaro | 2b03610 | 2015-03-20 10:22:55 +0100 | [diff] [blame] | 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" |
Alexandre Cesaro | 2b03610 | 2015-03-20 10:22:55 +0100 | [diff] [blame] | 9 | "io" |
| 10 | "io/ioutil" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
Alexandre Cesaro | 2b03610 | 2015-03-20 10:22:55 +0100 | [diff] [blame] | 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 | } |
| 31 | |
| 32 | for _, test := range tests { |
| 33 | if s := test.enc.Encode(test.charset, test.src); s != test.exp { |
| 34 | t.Errorf("Encode(%q) = %q, want %q", test.src, s, test.exp) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestDecodeWord(t *testing.T) { |
| 40 | tests := []struct { |
| 41 | src, exp string |
| 42 | hasErr bool |
| 43 | }{ |
| 44 | {"=?UTF-8?Q?=C2=A1Hola,_se=C3=B1or!?=", "¡Hola, señor!", false}, |
| 45 | {"=?UTF-8?Q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?=", "François-Jérôme", false}, |
| 46 | {"=?UTF-8?q?ascii?=", "ascii", false}, |
| 47 | {"=?utf-8?B?QW5kcsOp?=", "André", false}, |
| 48 | {"=?ISO-8859-1?Q?Rapha=EBl_Dupont?=", "Raphaël Dupont", false}, |
| 49 | {"=?utf-8?b?IkFudG9uaW8gSm9zw6kiIDxqb3NlQGV4YW1wbGUub3JnPg==?=", `"Antonio José" <jose@example.org>`, false}, |
| 50 | {"=?UTF-8?A?Test?=", "", true}, |
| 51 | {"=?UTF-8?Q?A=B?=", "", true}, |
| 52 | {"=?UTF-8?Q?=A?=", "", true}, |
| 53 | {"=?UTF-8?A?A?=", "", true}, |
| 54 | } |
| 55 | |
| 56 | for _, test := range tests { |
| 57 | dec := new(WordDecoder) |
| 58 | s, err := dec.Decode(test.src) |
| 59 | if test.hasErr && err == nil { |
| 60 | t.Errorf("Decode(%q) should return an error", test.src) |
| 61 | continue |
| 62 | } |
| 63 | if !test.hasErr && err != nil { |
| 64 | t.Errorf("Decode(%q): %v", test.src, err) |
| 65 | continue |
| 66 | } |
| 67 | if s != test.exp { |
| 68 | t.Errorf("Decode(%q) = %q, want %q", test.src, s, test.exp) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestDecodeHeader(t *testing.T) { |
| 74 | tests := []struct { |
| 75 | src, exp string |
| 76 | }{ |
| 77 | {"=?UTF-8?Q?=C2=A1Hola,_se=C3=B1or!?=", "¡Hola, señor!"}, |
| 78 | {"=?UTF-8?Q?Fran=C3=A7ois-J=C3=A9r=C3=B4me?=", "François-Jérôme"}, |
| 79 | {"=?UTF-8?q?ascii?=", "ascii"}, |
| 80 | {"=?utf-8?B?QW5kcsOp?=", "André"}, |
| 81 | {"=?ISO-8859-1?Q?Rapha=EBl_Dupont?=", "Raphaël Dupont"}, |
| 82 | {"Jean", "Jean"}, |
| 83 | {"=?utf-8?b?IkFudG9uaW8gSm9zw6kiIDxqb3NlQGV4YW1wbGUub3JnPg==?=", `"Antonio José" <jose@example.org>`}, |
| 84 | {"=?UTF-8?A?Test?=", "=?UTF-8?A?Test?="}, |
| 85 | {"=?UTF-8?Q?A=B?=", "=?UTF-8?Q?A=B?="}, |
| 86 | {"=?UTF-8?Q?=A?=", "=?UTF-8?Q?=A?="}, |
| 87 | {"=?UTF-8?A?A?=", "=?UTF-8?A?A?="}, |
| 88 | // Incomplete words |
| 89 | {"=?", "=?"}, |
| 90 | {"=?UTF-8?", "=?UTF-8?"}, |
| 91 | {"=?UTF-8?=", "=?UTF-8?="}, |
| 92 | {"=?UTF-8?Q", "=?UTF-8?Q"}, |
| 93 | {"=?UTF-8?Q?", "=?UTF-8?Q?"}, |
| 94 | {"=?UTF-8?Q?=", "=?UTF-8?Q?="}, |
| 95 | {"=?UTF-8?Q?A", "=?UTF-8?Q?A"}, |
| 96 | {"=?UTF-8?Q?A?", "=?UTF-8?Q?A?"}, |
| 97 | // Tests from RFC 2047 |
| 98 | {"=?ISO-8859-1?Q?a?=", "a"}, |
| 99 | {"=?ISO-8859-1?Q?a?= b", "a b"}, |
| 100 | {"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=", "ab"}, |
| 101 | {"=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=", "ab"}, |
| 102 | {"=?ISO-8859-1?Q?a?= \r\n\t =?ISO-8859-1?Q?b?=", "ab"}, |
| 103 | {"=?ISO-8859-1?Q?a_b?=", "a b"}, |
| 104 | } |
| 105 | |
| 106 | for _, test := range tests { |
| 107 | dec := new(WordDecoder) |
| 108 | s, err := dec.DecodeHeader(test.src) |
| 109 | if err != nil { |
| 110 | t.Errorf("DecodeHeader(%q): %v", test.src, err) |
| 111 | } |
| 112 | if s != test.exp { |
| 113 | t.Errorf("DecodeHeader(%q) = %q, want %q", test.src, s, test.exp) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestCharsetDecoder(t *testing.T) { |
| 119 | tests := []struct { |
| 120 | src string |
| 121 | want string |
| 122 | charsets []string |
| 123 | content []string |
| 124 | }{ |
| 125 | {"=?utf-8?b?Q2Fmw6k=?=", "Café", nil, nil}, |
| 126 | {"=?ISO-8859-1?Q?caf=E9?=", "café", nil, nil}, |
| 127 | {"=?US-ASCII?Q?foo_bar?=", "foo bar", nil, nil}, |
| 128 | {"=?utf-8?Q?=?=", "=?utf-8?Q?=?=", nil, nil}, |
| 129 | {"=?utf-8?Q?=A?=", "=?utf-8?Q?=A?=", nil, nil}, |
| 130 | { |
| 131 | "=?ISO-8859-15?Q?f=F5=F6?= =?windows-1252?Q?b=E0r?=", |
| 132 | "f\xf5\xf6b\xe0r", |
| 133 | []string{"iso-8859-15", "windows-1252"}, |
| 134 | []string{"f\xf5\xf6", "b\xe0r"}, |
| 135 | }, |
| 136 | } |
| 137 | |
| 138 | for _, test := range tests { |
| 139 | i := 0 |
| 140 | dec := &WordDecoder{ |
| 141 | CharsetReader: func(charset string, input io.Reader) (io.Reader, error) { |
| 142 | if charset != test.charsets[i] { |
| 143 | t.Errorf("DecodeHeader(%q), got charset %q, want %q", test.src, charset, test.charsets[i]) |
| 144 | } |
| 145 | content, err := ioutil.ReadAll(input) |
| 146 | if err != nil { |
| 147 | t.Errorf("DecodeHeader(%q), error in reader: %v", test.src, err) |
| 148 | } |
| 149 | got := string(content) |
| 150 | if got != test.content[i] { |
| 151 | t.Errorf("DecodeHeader(%q), got content %q, want %q", test.src, got, test.content[i]) |
| 152 | } |
| 153 | i++ |
| 154 | |
| 155 | return strings.NewReader(got), nil |
| 156 | }, |
| 157 | } |
| 158 | got, err := dec.DecodeHeader(test.src) |
| 159 | if err != nil { |
| 160 | t.Errorf("DecodeHeader(%q): %v", test.src, err) |
| 161 | } |
| 162 | if got != test.want { |
| 163 | t.Errorf("DecodeHeader(%q) = %q, want %q", test.src, got, test.want) |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func TestCharsetDecoderError(t *testing.T) { |
| 169 | dec := &WordDecoder{ |
| 170 | CharsetReader: func(charset string, input io.Reader) (io.Reader, error) { |
| 171 | return nil, errors.New("Test error") |
| 172 | }, |
| 173 | } |
| 174 | |
| 175 | if _, err := dec.DecodeHeader("=?charset?Q?foo?="); err == nil { |
| 176 | t.Error("DecodeHeader should return an error") |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | func BenchmarkQEncodeWord(b *testing.B) { |
| 181 | for i := 0; i < b.N; i++ { |
| 182 | QEncoding.Encode("UTF-8", "¡Hola, señor!") |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func BenchmarkQDecodeWord(b *testing.B) { |
| 187 | dec := new(WordDecoder) |
| 188 | |
| 189 | for i := 0; i < b.N; i++ { |
| 190 | dec.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=") |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | func BenchmarkQDecodeHeader(b *testing.B) { |
| 195 | dec := new(WordDecoder) |
| 196 | |
| 197 | for i := 0; i < b.N; i++ { |
| 198 | dec.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=") |
| 199 | } |
| 200 | } |