| 1 |
package enmime |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"net/textproto" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
// Test re-encoding to base64 |
| 11 |
func TestDecodeToUTF8Base64Header(t *testing.T) { |
| 12 |
var testTable = []struct { |
| 13 |
in, want string |
| 14 |
}{ |
| 15 |
{"no encoding", "no encoding"}, |
| 16 |
{"=?utf-8?q?abcABC_=24_=c2=a2_=e2=82=ac?=", "=?UTF-8?b?YWJjQUJDICQgwqIg4oKs?="}, |
| 17 |
{"=?iso-8859-1?q?#=a3_c=a9_r=ae_u=b5?=", "=?UTF-8?b?I8KjIGPCqSBywq4gdcK1?="}, |
| 18 |
{"=?big5?q?=a1=5d_=a1=61_=a1=71?=", "=?UTF-8?b?77yIIO+9myDjgIg=?="}, |
| 19 |
// Must respect separate tokens |
| 20 |
{"=?UTF-8?Q?Miros=C5=82aw?= <u@h>", "=?UTF-8?b?TWlyb3PFgmF3?= <u@h>"}, |
| 21 |
{"First Last <u@h> (=?iso-8859-1?q?#=a3_c=a9_r=ae_u=b5?=)", |
| 22 |
"First Last <u@h> (=?UTF-8?b?I8KjIGPCqSBywq4gdcK1?=)"}, |
| 23 |
// Quoted display name without space before angle-addr spec, Issue #112 |
| 24 |
{"\"=?UTF-8?b?TWlyb3PFgmF3?=\"<u@h>", "=?UTF-8?b?Ik1pcm9zxYJhdyI=?= <u@h>"}, |
| 25 |
} |
| 26 |
|
| 27 |
for _, tt := range testTable { |
| 28 |
got := decodeToUTF8Base64Header(tt.in) |
| 29 |
if got != tt.want { |
| 30 |
t.Errorf("DecodeHeader(%q) == %q, want: %q", tt.in, got, tt.want) |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func TestReadHeader(t *testing.T) { |
| 36 |
// These values will surround the test table input string. |
| 37 |
prefix := "From: hooman\n \n being\n" |
| 38 |
suffix := "Subject: hi\n\nPart body\n" |
| 39 |
|
| 40 |
data := make([]byte, 16*1024) |
| 41 |
for i := 0; i < len(data); i++ { |
| 42 |
data[i] = 'x' |
| 43 |
} |
| 44 |
sdata := string(data) |
| 45 |
var ttable = []struct { |
| 46 |
label, input, hname, want string |
| 47 |
correct bool |
| 48 |
extras []string |
| 49 |
}{ |
| 50 |
{ |
| 51 |
label: "basic crlf", |
| 52 |
input: "Foo: bar\r\n", |
| 53 |
hname: "Foo", |
| 54 |
want: "bar", |
| 55 |
correct: true, |
| 56 |
}, |
| 57 |
{ |
| 58 |
label: "basic lf", |
| 59 |
input: "To: anybody\n", |
| 60 |
hname: "To", |
| 61 |
want: "anybody", |
| 62 |
correct: true, |
| 63 |
}, |
| 64 |
{ |
| 65 |
label: "hyphenated", |
| 66 |
input: "Content-Language: en\r\n", |
| 67 |
hname: "Content-Language", |
| 68 |
want: "en", |
| 69 |
correct: true, |
| 70 |
}, |
| 71 |
{ |
| 72 |
label: "numeric", |
| 73 |
input: "Privilege: 127\n", |
| 74 |
hname: "Privilege", |
| 75 |
want: "127", |
| 76 |
correct: true, |
| 77 |
}, |
| 78 |
{ |
| 79 |
label: "space before colon", |
| 80 |
input: "SID : 0\r\n", |
| 81 |
hname: "SID", |
| 82 |
want: "0", |
| 83 |
correct: true, |
| 84 |
}, |
| 85 |
{ |
| 86 |
label: "space in name", |
| 87 |
input: "Audio Mode : None\r\n", |
| 88 |
hname: "Audio Mode", |
| 89 |
want: "None", |
| 90 |
correct: true, |
| 91 |
}, |
| 92 |
{ |
| 93 |
label: "sdata", |
| 94 |
input: "Cookie: " + sdata + "\r\n", |
| 95 |
hname: "Cookie", |
| 96 |
want: sdata, |
| 97 |
correct: true, |
| 98 |
}, |
| 99 |
{ |
| 100 |
label: "missing name", |
| 101 |
input: ": line1=foo\r\n", |
| 102 |
correct: false, |
| 103 |
}, |
| 104 |
{ |
| 105 |
label: "blank line in continuation", |
| 106 |
input: "X-Continuation: line1=foo\r\n" + |
| 107 |
" \r\n" + |
| 108 |
" line2=bar\r\n", |
| 109 |
hname: "X-Continuation", |
| 110 |
want: "line1=foo line2=bar", |
| 111 |
correct: true, |
| 112 |
}, |
| 113 |
{ |
| 114 |
label: "lf-space continuation", |
| 115 |
input: "Content-Type: text/plain;\n charset=us-ascii\n", |
| 116 |
hname: "Content-Type", |
| 117 |
want: "text/plain; charset=us-ascii", |
| 118 |
correct: true, |
| 119 |
}, |
| 120 |
{ |
| 121 |
label: "lf-tab continuation", |
| 122 |
input: "X-Tabbed-Continuation: line1=foo;\n\tline2=bar\n", |
| 123 |
hname: "X-Tabbed-Continuation", |
| 124 |
want: "line1=foo; line2=bar", |
| 125 |
correct: true, |
| 126 |
}, |
| 127 |
{ |
| •128 |
label: "permitted specials in name", |
| 129 |
input: "Begin!#$%&'*+-.^_`|~End: text\n", |
| 130 |
hname: "Begin!#$%&'*+-.^_`|~End", |
| 131 |
want: "text", |
| 132 |
correct: true, |
| 133 |
}, |
| •134 |
{ |
| 135 |
label: "equals in name", |
| 136 |
input: "name=value:text\n", |
| 137 |
correct: false, |
| 138 |
}, |
| 139 |
{ |
| 140 |
label: "no space before continuation", |
| 141 |
input: "X-Bad-Continuation: line1=foo;\nline2=bar\n", |
| 142 |
hname: "X-Bad-Continuation", |
| 143 |
want: "line1=foo; line2=bar", |
| 144 |
correct: false, |
| 145 |
}, |
| 146 |
{ |
| 147 |
label: "not really a continuation", |
| 148 |
input: "X-Not-Continuation: line1=foo;\nline2: bar\n", |
| 149 |
hname: "X-Not-Continuation", |
| 150 |
want: "line1=foo;", |
| 151 |
correct: true, |
| 152 |
extras: []string{"line2"}, |
| 153 |
}, |
| 154 |
{ |
| 155 |
label: "continuation with header style", |
| 156 |
input: "X-Continuation: line1=foo;\n not-a-header 15 X-Not-Header: bar\n", |
| 157 |
hname: "X-Continuation", |
| 158 |
want: "line1=foo; not-a-header 15 X-Not-Header: bar", |
| 159 |
correct: true, |
| 160 |
}, |
| 161 |
{ |
| 162 |
label: "multiline continuation with header style, few spaces", |
| 163 |
input: "X-Continuation-DKIM-like: line1=foo;\n" + |
| 164 |
" h=Subject:From:Reply-To:To:Date:Message-ID: List-ID:List-Unsubscribe:\n" + |
| 165 |
" Content-Type:MIME-Version;\n", |
| 166 |
hname: "X-Continuation-DKIM-like", |
| 167 |
want: "line1=foo;" + |
| 168 |
" h=Subject:From:Reply-To:To:Date:Message-ID: List-ID:List-Unsubscribe:" + |
| 169 |
" Content-Type:MIME-Version;", |
| 170 |
correct: true, |
| 171 |
}, |
| 172 |
{ |
| 173 |
label: "multiline continuation, few colons", |
| 174 |
input: "Authentication-Results: mx.google.com;\n" + |
| 175 |
" spf=pass (google.com: sender)\n" + |
| 176 |
" dkim=pass header.i=@1;\n" + |
| 177 |
" dkim=pass header.i=@2\n", |
| 178 |
hname: "Authentication-Results", |
| 179 |
want: "mx.google.com;" + |
| 180 |
" spf=pass (google.com: sender)" + |
| 181 |
" dkim=pass header.i=@1;" + |
| 182 |
" dkim=pass header.i=@2", |
| 183 |
correct: true, |
| 184 |
}, |
| 185 |
{ |
| 186 |
label: "continuation containing early name-colon", |
| 187 |
input: "DKIM-Signature: a=rsa-sha256; v=1; q=dns/txt;\r\n" + |
| 188 |
" s=krs; t=1603674005; h=Content-Transfer-Encoding: Mime-Version:\r\n" + |
| 189 |
" Content-Type: Subject: From: To: Message-Id: Sender: Date;\r\n", |
| 190 |
hname: "DKIM-Signature", |
| 191 |
want: "a=rsa-sha256; v=1; q=dns/txt;" + |
| 192 |
" s=krs; t=1603674005; h=Content-Transfer-Encoding: Mime-Version:" + |
| 193 |
" Content-Type: Subject: From: To: Message-Id: Sender: Date;", |
| 194 |
correct: true, |
| 195 |
}, |
| 196 |
} |
| 197 |
|
| 198 |
for _, tt := range ttable { |
| 199 |
t.Run(tt.label, func(t *testing.T) { |
| 200 |
if lastc := tt.input[len(tt.input)-1]; lastc != '\r' && lastc != '\n' { |
| 201 |
t.Fatalf("Malformed test case, %q input does not end with a CR or LF", tt.label) |
| 202 |
} |
| 203 |
|
| 204 |
// Reader we will share with readHeader() |
| 205 |
r := bufio.NewReader(strings.NewReader(prefix + tt.input + suffix)) |
| 206 |
|
| 207 |
p := &Part{} |
| 208 |
header, err := readHeader(r, p) |
| 209 |
if err != nil { |
| 210 |
t.Fatal(err) |
| 211 |
} |
| 212 |
|
| 213 |
// Check exepcted prefix header. |
| 214 |
got := header.Get("From") |
| 215 |
want := "hooman being" |
| 216 |
if got != want { |
| 217 |
t.Errorf("Prefix (From) header mangled\ngot: %q, want: %q", got, want) |
| 218 |
} |
| 219 |
|
| 220 |
// Check exepcted suffix header. |
| 221 |
got = header.Get("Subject") |
| 222 |
want = "hi" |
| 223 |
if got != want { |
| 224 |
t.Errorf("Suffix (Subject) header mangled\ngot: %q, want: %q", got, want) |
| 225 |
} |
| 226 |
|
| 227 |
// Check exepcted header from ttable. |
| 228 |
got = header.Get(tt.hname) |
| 229 |
if got != tt.want { |
| 230 |
t.Errorf( |
| 231 |
"Stripped %q header value mismatch\ngot : %q,\nwant: %q", tt.hname, got, tt.want) |
| 232 |
} |
| 233 |
|
| 234 |
// Check error count. |
| 235 |
wantErrs := 0 |
| 236 |
if !tt.correct { |
| 237 |
wantErrs = 1 |
| 238 |
} |
| 239 |
gotErrs := len(p.Errors) |
| 240 |
if gotErrs != wantErrs { |
| 241 |
t.Errorf("Got %v p.Errors, want %v", gotErrs, wantErrs) |
| •242 |
if gotErrs > 0 { |
| 243 |
for _, e := range p.Errors { |
| 244 |
t.Log(e) |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// Check for extra headers by removing expected ones. |
| 250 |
delete(header, "From") |
| 251 |
delete(header, "Subject") |
| 252 |
delete(header, textproto.CanonicalMIMEHeaderKey(tt.hname)) |
| 253 |
for _, hname := range tt.extras { |
| 254 |
delete(header, textproto.CanonicalMIMEHeaderKey(hname)) |
| 255 |
} |
| 256 |
for hname := range header { |
| 257 |
t.Errorf("Found unexpected header %q after parsing", hname) |
| 258 |
} |
| 259 |
|
| 260 |
// Output input if any check failed. |
| 261 |
if t.Failed() { |
| 262 |
t.Errorf("input: %q", tt.input) |
| 263 |
} |
| 264 |
|
| 265 |
// readHeader should have consumed the two header lines, and the blank line, but not the |
| 266 |
// body |
| 267 |
want = "Part body" |
| 268 |
line, isPrefix, err := r.ReadLine() |
| 269 |
got = string(line) |
| 270 |
if err != nil { |
| 271 |
t.Fatal(err) |
| 272 |
} |
| 273 |
if isPrefix { |
| 274 |
t.Fatal("isPrefix was true, wanted false") |
| 275 |
} |
| 276 |
if got != want { |
| 277 |
t.Errorf("Line got: %q, want: %q", got, want) |
| 278 |
} |
| 279 |
}) |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
func TestCommaDelimitedAddressLists(t *testing.T) { |
| 284 |
testData := []struct { |
| 285 |
have string |
| 286 |
want string |
| 287 |
}{ |
| 288 |
{ |
| 289 |
have: `"Joe @ Company" <[email protected]> <[email protected]>`, |
| 290 |
want: `"Joe @ Company" <[email protected]>, <[email protected]>`, |
| 291 |
}, |
| 292 |
{ |
| 293 |
have: `Joe Company <[email protected]> <[email protected]>`, |
| 294 |
want: `Joe Company <[email protected]>, <[email protected]>`, |
| 295 |
}, |
| 296 |
{ |
| 297 |
have: `Joe Company:Joey <[email protected]> John <[email protected]>;`, |
| 298 |
want: `Joe Company:Joey <[email protected]>, John <[email protected]>;`, |
| 299 |
}, |
| 300 |
{ |
| 301 |
have: `Joe Company:Joey <[email protected]> John <[email protected]>; Jimmy John <[email protected]>`, |
| 302 |
want: `Joe Company:Joey <[email protected]>, John <[email protected]>;`, |
| 303 |
}, |
| 304 |
{ |
| 305 |
have: `Joe Company <[email protected]> John Company <[email protected]>`, |
| 306 |
want: `Joe Company <[email protected]>, John Company <[email protected]>`, |
| 307 |
}, |
| 308 |
{ |
| 309 |
have: `Joe Company <[email protected]>,John Company <[email protected]>`, |
| 310 |
want: `Joe Company <[email protected]>,John Company <[email protected]>`, |
| 311 |
}, |
| 312 |
{ |
| 313 |
have: `[email protected] [email protected]`, |
| 314 |
want: `[email protected], [email protected]`, |
| 315 |
}, |
| 316 |
{ |
| 317 |
have: `Jimmy John <[email protected]> [email protected] [email protected]`, |
| 318 |
want: `Jimmy John <[email protected]>, [email protected], [email protected]`, |
| 319 |
}, |
| 320 |
{ |
| 321 |
have: `Jimmy John <[email protected]> [email protected] John Company <[email protected]>`, |
| 322 |
want: `Jimmy John <[email protected]>, [email protected], John Company <[email protected]>`, |
| 323 |
}, |
| 324 |
{ |
| 325 |
have: `<[email protected]> "Giant; \"Big\" Box" <[email protected]>`, |
| 326 |
want: `<[email protected]>, "Giant; \"Big\" Box" <[email protected]>`, |
| 327 |
}, |
| 328 |
{ |
| 329 |
have: `A Group:Ed Jones <[email protected]>,[email protected],John <[email protected]>;`, |
| 330 |
want: `A Group:Ed Jones <[email protected]>,[email protected],John <[email protected]>;`, |
| 331 |
}, |
| 332 |
{ |
| 333 |
have: `A Group:Ed Jones <[email protected]> [email protected] John <[email protected]>;`, |
| 334 |
want: `A Group:Ed Jones <[email protected]>, [email protected], John <[email protected]>;`, |
| 335 |
}, |
| 336 |
} |
| 337 |
for i := range testData { |
| 338 |
v := ensureCommaDelimitedAddresses(testData[i].have) |
| 339 |
if testData[i].want != v { |
| 340 |
t.Fatalf("Expected %s, but got %s", testData[i].want, v) |
| 341 |
} |
| 342 |
} |
| 343 |
} |