| 1 |
// Copyright 2011 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 mail |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"mime" |
| 12 |
"reflect" |
| 13 |
"slices" |
| 14 |
"strings" |
| 15 |
"testing" |
| 16 |
"time" |
| 17 |
) |
| 18 |
|
| 19 |
var parseTests = []struct { |
| 20 |
in string |
| 21 |
header Header |
| 22 |
body string |
| 23 |
}{ |
| 24 |
{ |
| 25 |
// RFC 5322, Appendix A.1.1 |
| 26 |
in: `From: John Doe <[email protected]> |
| 27 |
To: Mary Smith <[email protected]> |
| 28 |
Subject: Saying Hello |
| 29 |
Date: Fri, 21 Nov 1997 09:55:06 -0600 |
| 30 |
Message-ID: <[email protected]> |
| 31 |
|
| 32 |
This is a message just to say hello. |
| 33 |
So, "Hello". |
| 34 |
`, |
| 35 |
header: Header{ |
| 36 |
"From": []string{"John Doe <[email protected]>"}, |
| 37 |
"To": []string{"Mary Smith <[email protected]>"}, |
| 38 |
"Subject": []string{"Saying Hello"}, |
| 39 |
"Date": []string{"Fri, 21 Nov 1997 09:55:06 -0600"}, |
| 40 |
"Message-Id": []string{"<[email protected]>"}, |
| 41 |
}, |
| 42 |
body: "This is a message just to say hello.\nSo, \"Hello\".\n", |
| 43 |
}, |
| 44 |
{ |
| 45 |
// RFC 5965, Appendix B.1, a part of the multipart message (a header-only sub message) |
| 46 |
in: `Feedback-Type: abuse |
| 47 |
User-Agent: SomeGenerator/1.0 |
| 48 |
Version: 1 |
| 49 |
`, |
| 50 |
header: Header{ |
| 51 |
"Feedback-Type": []string{"abuse"}, |
| 52 |
"User-Agent": []string{"SomeGenerator/1.0"}, |
| 53 |
"Version": []string{"1"}, |
| 54 |
}, |
| 55 |
body: "", |
| 56 |
}, |
| 57 |
{ |
| 58 |
// RFC 5322 permits any printable ASCII character, |
| 59 |
// except colon, in a header key. Issue #58862. |
| 60 |
in: `From: [email protected] |
| 61 |
Custom/Header: v |
| 62 |
|
| 63 |
Body |
| 64 |
`, |
| 65 |
header: Header{ |
| 66 |
"From": []string{"[email protected]"}, |
| 67 |
"Custom/Header": []string{"v"}, |
| 68 |
}, |
| 69 |
body: "Body\n", |
| 70 |
}, |
| 71 |
{ |
| 72 |
// RFC 4155 mbox format. We've historically permitted this, |
| 73 |
// so we continue to permit it. Issue #60332. |
| 74 |
in: `From [email protected] Mon Jun 19 00:00:00 2023 |
| 75 |
From: [email protected] |
| 76 |
|
| 77 |
Hello, gophers! |
| 78 |
`, |
| 79 |
header: Header{ |
| 80 |
"From": []string{"[email protected]"}, |
| 81 |
"From [email protected] Mon Jun 19 00": []string{"00:00 2023"}, |
| 82 |
}, |
| 83 |
body: "Hello, gophers!\n", |
| 84 |
}, |
| 85 |
} |
| 86 |
|
| 87 |
func TestParsing(t *testing.T) { |
| 88 |
for i, test := range parseTests { |
| 89 |
msg, err := ReadMessage(bytes.NewBuffer([]byte(test.in))) |
| 90 |
if err != nil { |
| 91 |
t.Errorf("test #%d: Failed parsing message: %v", i, err) |
| 92 |
continue |
| 93 |
} |
| 94 |
if !headerEq(msg.Header, test.header) { |
| 95 |
t.Errorf("test #%d: Incorrectly parsed message header.\nGot:\n%+v\nWant:\n%+v", |
| 96 |
i, msg.Header, test.header) |
| 97 |
} |
| 98 |
body, err := io.ReadAll(msg.Body) |
| 99 |
if err != nil { |
| 100 |
t.Errorf("test #%d: Failed reading body: %v", i, err) |
| 101 |
continue |
| 102 |
} |
| 103 |
bodyStr := string(body) |
| 104 |
if bodyStr != test.body { |
| 105 |
t.Errorf("test #%d: Incorrectly parsed message body.\nGot:\n%+v\nWant:\n%+v", |
| 106 |
i, bodyStr, test.body) |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func headerEq(a, b Header) bool { |
| 112 |
if len(a) != len(b) { |
| 113 |
return false |
| 114 |
} |
| 115 |
for k, as := range a { |
| 116 |
bs, ok := b[k] |
| 117 |
if !ok { |
| 118 |
return false |
| 119 |
} |
| 120 |
if !slices.Equal(as, bs) { |
| 121 |
return false |
| 122 |
} |
| 123 |
} |
| 124 |
return true |
| 125 |
} |
| 126 |
|
| 127 |
func TestDateParsing(t *testing.T) { |
| 128 |
tests := []struct { |
| 129 |
dateStr string |
| 130 |
exp time.Time |
| 131 |
}{ |
| 132 |
// RFC 5322, Appendix A.1.1 |
| 133 |
{ |
| 134 |
"Fri, 21 Nov 1997 09:55:06 -0600", |
| 135 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 136 |
}, |
| 137 |
// RFC 5322, Appendix A.6.2 |
| 138 |
// Obsolete date. |
| 139 |
{ |
| 140 |
"21 Nov 97 09:55:06 GMT", |
| 141 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("GMT", 0)), |
| 142 |
}, |
| 143 |
// Commonly found format not specified by RFC 5322. |
| 144 |
{ |
| 145 |
"Fri, 21 Nov 1997 09:55:06 -0600 (MDT)", |
| 146 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 147 |
}, |
| 148 |
{ |
| 149 |
"Thu, 20 Nov 1997 09:55:06 -0600 (MDT)", |
| 150 |
time.Date(1997, 11, 20, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 151 |
}, |
| 152 |
{ |
| 153 |
"Thu, 20 Nov 1997 09:55:06 GMT (GMT)", |
| 154 |
time.Date(1997, 11, 20, 9, 55, 6, 0, time.UTC), |
| 155 |
}, |
| 156 |
{ |
| 157 |
"Fri, 21 Nov 1997 09:55:06 +1300 (TOT)", |
| 158 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", +13*60*60)), |
| 159 |
}, |
| 160 |
} |
| 161 |
for _, test := range tests { |
| 162 |
hdr := Header{ |
| 163 |
"Date": []string{test.dateStr}, |
| 164 |
} |
| 165 |
date, err := hdr.Date() |
| 166 |
if err != nil { |
| 167 |
t.Errorf("Header(Date: %s).Date(): %v", test.dateStr, err) |
| 168 |
} else if !date.Equal(test.exp) { |
| 169 |
t.Errorf("Header(Date: %s).Date() = %+v, want %+v", test.dateStr, date, test.exp) |
| 170 |
} |
| 171 |
|
| 172 |
date, err = ParseDate(test.dateStr) |
| 173 |
if err != nil { |
| 174 |
t.Errorf("ParseDate(%s): %v", test.dateStr, err) |
| 175 |
} else if !date.Equal(test.exp) { |
| 176 |
t.Errorf("ParseDate(%s) = %+v, want %+v", test.dateStr, date, test.exp) |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func TestDateParsingCFWS(t *testing.T) { |
| 182 |
tests := []struct { |
| 183 |
dateStr string |
| 184 |
exp time.Time |
| 185 |
valid bool |
| 186 |
}{ |
| 187 |
// FWS-only. No date. |
| 188 |
{ |
| 189 |
" ", |
| 190 |
// nil is not allowed |
| 191 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 192 |
false, |
| 193 |
}, |
| 194 |
// FWS is allowed before optional day of week. |
| 195 |
{ |
| 196 |
" Fri, 21 Nov 1997 09:55:06 -0600", |
| 197 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 198 |
true, |
| 199 |
}, |
| 200 |
{ |
| 201 |
"21 Nov 1997 09:55:06 -0600", |
| 202 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 203 |
true, |
| 204 |
}, |
| 205 |
{ |
| 206 |
"Fri 21 Nov 1997 09:55:06 -0600", |
| 207 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 208 |
false, // missing , |
| 209 |
}, |
| 210 |
// FWS is allowed before day of month but HTAB fails. |
| 211 |
{ |
| 212 |
"Fri, 21 Nov 1997 09:55:06 -0600", |
| 213 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 214 |
true, |
| 215 |
}, |
| 216 |
// FWS is allowed before and after year but HTAB fails. |
| 217 |
{ |
| 218 |
"Fri, 21 Nov 1997 09:55:06 -0600", |
| 219 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 220 |
true, |
| 221 |
}, |
| 222 |
// FWS is allowed before zone but HTAB is not handled. Obsolete timezone is handled. |
| 223 |
{ |
| 224 |
"Fri, 21 Nov 1997 09:55:06 CST", |
| 225 |
time.Time{}, |
| 226 |
true, |
| 227 |
}, |
| 228 |
// FWS is allowed after date and a CRLF is already replaced. |
| 229 |
{ |
| 230 |
"Fri, 21 Nov 1997 09:55:06 CST (no leading FWS and a trailing CRLF) \r\n", |
| 231 |
time.Time{}, |
| 232 |
true, |
| 233 |
}, |
| 234 |
// CFWS is a reduced set of US-ASCII where space and accentuated are obsolete. No error. |
| 235 |
{ |
| 236 |
"Fri, 21 Nov 1997 09:55:06 -0600 (MDT and non-US-ASCII signs éèç )", |
| 237 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 238 |
true, |
| 239 |
}, |
| 240 |
// CFWS is allowed after zone including a nested comment. |
| 241 |
// Trailing FWS is allowed. |
| 242 |
{ |
| 243 |
"Fri, 21 Nov 1997 09:55:06 -0600 \r\n (thisisa(valid)cfws) \t ", |
| 244 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 245 |
true, |
| 246 |
}, |
| 247 |
// CRLF is incomplete and misplaced. |
| 248 |
{ |
| 249 |
"Fri, 21 Nov 1997 \r 09:55:06 -0600 \r\n (thisisa(valid)cfws) \t ", |
| 250 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 251 |
false, |
| 252 |
}, |
| 253 |
// CRLF is complete but misplaced. No error is returned. |
| 254 |
{ |
| 255 |
"Fri, 21 Nov 199\r\n7 09:55:06 -0600 \r\n (thisisa(valid)cfws) \t ", |
| 256 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 257 |
true, // should be false in the strict interpretation of RFC 5322. |
| 258 |
}, |
| 259 |
// Invalid ASCII in date. |
| 260 |
{ |
| 261 |
"Fri, 21 Nov 1997 ù 09:55:06 -0600 \r\n (thisisa(valid)cfws) \t ", |
| 262 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 263 |
false, |
| 264 |
}, |
| 265 |
// CFWS chars () in date. |
| 266 |
{ |
| 267 |
"Fri, 21 Nov () 1997 09:55:06 -0600 \r\n (thisisa(valid)cfws) \t ", |
| 268 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 269 |
false, |
| 270 |
}, |
| 271 |
// Timezone is invalid but T is found in comment. |
| 272 |
{ |
| 273 |
"Fri, 21 Nov 1997 09:55:06 -060 \r\n (Thisisa(valid)cfws) \t ", |
| 274 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 275 |
false, |
| 276 |
}, |
| 277 |
// Date has no month. |
| 278 |
{ |
| 279 |
"Fri, 21 1997 09:55:06 -0600", |
| 280 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 281 |
false, |
| 282 |
}, |
| 283 |
// Invalid month : OCT iso Oct |
| 284 |
{ |
| 285 |
"Fri, 21 OCT 1997 09:55:06 CST", |
| 286 |
time.Time{}, |
| 287 |
false, |
| 288 |
}, |
| 289 |
// A too short time zone. |
| 290 |
{ |
| 291 |
"Fri, 21 Nov 1997 09:55:06 -060", |
| 292 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 293 |
false, |
| 294 |
}, |
| 295 |
// A too short obsolete time zone. |
| 296 |
{ |
| 297 |
"Fri, 21 1997 09:55:06 GT", |
| 298 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)), |
| 299 |
false, |
| 300 |
}, |
| 301 |
// Ensure that the presence of "T" in the date |
| 302 |
// doesn't trip out ParseDate, as per issue 39260. |
| 303 |
{ |
| 304 |
"Tue, 26 May 2020 14:04:40 GMT", |
| 305 |
time.Date(2020, 05, 26, 14, 04, 40, 0, time.UTC), |
| 306 |
true, |
| 307 |
}, |
| 308 |
{ |
| 309 |
"Tue, 26 May 2020 14:04:40 UT", |
| 310 |
time.Date(2020, 05, 26, 14, 04, 40, 0, time.UTC), |
| 311 |
true, |
| 312 |
}, |
| 313 |
{ |
| 314 |
"Thu, 21 May 2020 14:04:40 UT", |
| 315 |
time.Date(2020, 05, 21, 14, 04, 40, 0, time.UTC), |
| 316 |
true, |
| 317 |
}, |
| 318 |
{ |
| 319 |
"Tue, 26 May 2020 14:04:40 XT", |
| 320 |
time.Date(2020, 05, 26, 14, 04, 40, 0, time.UTC), |
| 321 |
false, |
| 322 |
}, |
| 323 |
{ |
| 324 |
"Thu, 21 May 2020 14:04:40 XT", |
| 325 |
time.Date(2020, 05, 21, 14, 04, 40, 0, time.UTC), |
| 326 |
false, |
| 327 |
}, |
| 328 |
{ |
| 329 |
"Thu, 21 May 2020 14:04:40 UTC", |
| 330 |
time.Date(2020, 05, 21, 14, 04, 40, 0, time.UTC), |
| 331 |
true, |
| 332 |
}, |
| 333 |
{ |
| 334 |
"Fri, 21 Nov 1997 09:55:06 GMT (GMT)", |
| 335 |
time.Date(1997, 11, 21, 9, 55, 6, 0, time.UTC), |
| 336 |
true, |
| 337 |
}, |
| 338 |
} |
| 339 |
for _, test := range tests { |
| 340 |
hdr := Header{ |
| 341 |
"Date": []string{test.dateStr}, |
| 342 |
} |
| 343 |
date, err := hdr.Date() |
| 344 |
if err != nil && test.valid { |
| 345 |
t.Errorf("Header(Date: %s).Date(): %v", test.dateStr, err) |
| 346 |
} else if err == nil && test.exp.IsZero() { |
| 347 |
// OK. Used when exact result depends on the |
| 348 |
// system's local zoneinfo. |
| 349 |
} else if err == nil && !date.Equal(test.exp) && test.valid { |
| 350 |
t.Errorf("Header(Date: %s).Date() = %+v, want %+v", test.dateStr, date, test.exp) |
| 351 |
} else if err == nil && !test.valid { // an invalid expression was tested |
| 352 |
t.Errorf("Header(Date: %s).Date() did not return an error but %v", test.dateStr, date) |
| 353 |
} |
| 354 |
|
| 355 |
date, err = ParseDate(test.dateStr) |
| 356 |
if err != nil && test.valid { |
| 357 |
t.Errorf("ParseDate(%s): %v", test.dateStr, err) |
| 358 |
} else if err == nil && test.exp.IsZero() { |
| 359 |
// OK. Used when exact result depends on the |
| 360 |
// system's local zoneinfo. |
| 361 |
} else if err == nil && !test.valid { // an invalid expression was tested |
| 362 |
t.Errorf("ParseDate(%s) did not return an error but %v", test.dateStr, date) |
| 363 |
} else if err == nil && test.valid && !date.Equal(test.exp) { |
| 364 |
t.Errorf("ParseDate(%s) = %+v, want %+v", test.dateStr, date, test.exp) |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
func TestAddressParsingError(t *testing.T) { |
| 370 |
mustErrTestCases := [...]struct { |
| 371 |
text string |
| 372 |
wantErrText string |
| 373 |
}{ |
| 374 |
0: {"=?iso-8859-2?Q?Bogl=E1rka_Tak=E1cs?= <[email protected]>", "charset not supported"}, |
| 375 |
1: {"[email protected] [email protected]", "expected single address"}, |
| 376 |
2: {string([]byte{0xed, 0xa0, 0x80}) + " <[email protected]>", "invalid utf-8 in address"}, |
| 377 |
3: {"\"" + string([]byte{0xed, 0xa0, 0x80}) + "\" <[email protected]>", "invalid utf-8 in quoted-string"}, |
| 378 |
4: {"\"\\" + string([]byte{0x80}) + "\" <[email protected]>", "invalid utf-8 in quoted-string"}, |
| 379 |
5: {"\"\x00\" <[email protected]>", "bad character in quoted-string"}, |
| 380 |
6: {"\"\\\x00\" <[email protected]>", "bad character in quoted-string"}, |
| 381 |
7: {"John Doe", "no angle-addr"}, |
| 382 |
8: {`<jdoe#machine.example>`, "missing @ in addr-spec"}, |
| 383 |
9: {`John <middle> Doe <[email protected]>`, "missing @ in addr-spec"}, |
| 384 |
10: {"[email protected] (", "misformatted parenthetical comment"}, |
| 385 |
11: {"empty group: ;", "empty group"}, |
| 386 |
12: {"root group: embed group: [email protected];", "no angle-addr"}, |
| 387 |
13: {"group not closed: [email protected]", "expected comma"}, |
| 388 |
14: {"group: [email protected], [email protected];", "group with multiple addresses"}, |
| 389 |
15: {"john.doe", "missing '@' or angle-addr"}, |
| 390 |
16: {"john.doe@", "missing '@' or angle-addr"}, |
| 391 |
17: {"John [email protected]", "no angle-addr"}, |
| 392 |
18: {" group: [email protected]; (asd", "misformatted parenthetical comment"}, |
| 393 |
19: {" group: ; (asd", "misformatted parenthetical comment"}, |
| 394 |
20: {`(John) Doe <[email protected]>`, "missing word in phrase:"}, |
| 395 |
21: {"<jdoe@[" + string([]byte{0xed, 0xa0, 0x80}) + "192.168.0.1]>", "invalid utf-8 in domain-literal"}, |
| 396 |
22: {"<jdoe@[[192.168.0.1]>", "bad character in domain-literal"}, |
| 397 |
23: {"<jdoe@[192.168.0.1>", "unclosed domain-literal"}, |
| 398 |
24: {"<jdoe@[256.0.0.1]>", "invalid IP address in domain-literal"}, |
| 399 |
} |
| 400 |
|
| 401 |
for i, tc := range mustErrTestCases { |
| 402 |
_, err := ParseAddress(tc.text) |
| 403 |
if err == nil || !strings.Contains(err.Error(), tc.wantErrText) { |
| 404 |
t.Errorf(`mail.ParseAddress(%q) #%d want %q, got %v`, tc.text, i, tc.wantErrText, err) |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
t.Run("CustomWordDecoder", func(t *testing.T) { |
| 409 |
p := &AddressParser{WordDecoder: &mime.WordDecoder{}} |
| 410 |
for i, tc := range mustErrTestCases { |
| 411 |
_, err := p.Parse(tc.text) |
| 412 |
if err == nil || !strings.Contains(err.Error(), tc.wantErrText) { |
| 413 |
t.Errorf(`p.Parse(%q) #%d want %q, got %v`, tc.text, i, tc.wantErrText, err) |
| 414 |
} |
| 415 |
} |
| 416 |
}) |
| 417 |
|
| 418 |
} |
| 419 |
|
| 420 |
func TestAddressParsing(t *testing.T) { |
| 421 |
tests := []struct { |
| 422 |
addrsStr string |
| 423 |
exp []*Address |
| 424 |
}{ |
| 425 |
// Bare address |
| 426 |
{ |
| 427 |
`[email protected]`, |
| 428 |
[]*Address{{ |
| 429 |
Address: "[email protected]", |
| 430 |
}}, |
| 431 |
}, |
| 432 |
// RFC 5322, Appendix A.1.1 |
| 433 |
{ |
| 434 |
`John Doe <[email protected]>`, |
| 435 |
[]*Address{{ |
| 436 |
Name: "John Doe", |
| 437 |
Address: "[email protected]", |
| 438 |
}}, |
| 439 |
}, |
| 440 |
// RFC 5322, Appendix A.1.2 |
| 441 |
{ |
| 442 |
`"Joe Q. Public" <[email protected]>`, |
| 443 |
[]*Address{{ |
| 444 |
Name: "Joe Q. Public", |
| 445 |
Address: "[email protected]", |
| 446 |
}}, |
| 447 |
}, |
| 448 |
// Comment in display name |
| 449 |
{ |
| 450 |
`John (middle) Doe <[email protected]>`, |
| 451 |
[]*Address{{ |
| 452 |
Name: "John Doe", |
| 453 |
Address: "[email protected]", |
| 454 |
}}, |
| 455 |
}, |
| 456 |
// Display name is quoted string, so comment is not a comment |
| 457 |
{ |
| 458 |
`"John (middle) Doe" <[email protected]>`, |
| 459 |
[]*Address{{ |
| 460 |
Name: "John (middle) Doe", |
| 461 |
Address: "[email protected]", |
| 462 |
}}, |
| 463 |
}, |
| 464 |
{ |
| 465 |
`"John <middle> Doe" <[email protected]>`, |
| 466 |
[]*Address{{ |
| 467 |
Name: "John <middle> Doe", |
| 468 |
Address: "[email protected]", |
| 469 |
}}, |
| 470 |
}, |
| 471 |
{ |
| 472 |
`Mary Smith <[email protected]>, [email protected], Who? <[email protected]>`, |
| 473 |
[]*Address{ |
| 474 |
{ |
| 475 |
Name: "Mary Smith", |
| 476 |
Address: "[email protected]", |
| 477 |
}, |
| 478 |
{ |
| 479 |
Address: "[email protected]", |
| 480 |
}, |
| 481 |
{ |
| 482 |
Name: "Who?", |
| 483 |
Address: "[email protected]", |
| 484 |
}, |
| 485 |
}, |
| 486 |
}, |
| 487 |
{ |
| 488 |
`<[email protected]>, "Giant; \"Big\" Box" <[email protected]>`, |
| 489 |
[]*Address{ |
| 490 |
{ |
| 491 |
Address: "[email protected]", |
| 492 |
}, |
| 493 |
{ |
| 494 |
Name: `Giant; "Big" Box`, |
| 495 |
Address: "[email protected]", |
| 496 |
}, |
| 497 |
}, |
| 498 |
}, |
| 499 |
// RFC 5322, Appendix A.6.1 |
| 500 |
{ |
| 501 |
`Joe Q. Public <[email protected]>`, |
| 502 |
[]*Address{{ |
| 503 |
Name: "Joe Q. Public", |
| 504 |
Address: "[email protected]", |
| 505 |
}}, |
| 506 |
}, |
| 507 |
// RFC 5322, Appendix A.1.3 |
| 508 |
{ |
| 509 |
`group1: [email protected];`, |
| 510 |
[]*Address{ |
| 511 |
{ |
| 512 |
Name: "", |
| 513 |
Address: "[email protected]", |
| 514 |
}, |
| 515 |
}, |
| 516 |
}, |
| 517 |
{ |
| 518 |
`empty group: ;`, |
| 519 |
[]*Address(nil), |
| 520 |
}, |
| 521 |
{ |
| 522 |
`A Group:Ed Jones <[email protected]>,[email protected],John <[email protected]>;`, |
| 523 |
[]*Address{ |
| 524 |
{ |
| 525 |
Name: "Ed Jones", |
| 526 |
Address: "[email protected]", |
| 527 |
}, |
| 528 |
{ |
| 529 |
Name: "", |
| 530 |
Address: "[email protected]", |
| 531 |
}, |
| 532 |
{ |
| 533 |
Name: "John", |
| 534 |
Address: "[email protected]", |
| 535 |
}, |
| 536 |
}, |
| 537 |
}, |
| 538 |
// RFC5322 4.4 obs-addr-list |
| 539 |
{ |
| 540 |
` , [email protected],,John <[email protected]>,`, |
| 541 |
[]*Address{ |
| 542 |
{ |
| 543 |
Name: "", |
| 544 |
Address: "[email protected]", |
| 545 |
}, |
| 546 |
{ |
| 547 |
Name: "John", |
| 548 |
Address: "[email protected]", |
| 549 |
}, |
| 550 |
}, |
| 551 |
}, |
| 552 |
{ |
| 553 |
` , [email protected],,John <[email protected]>,,`, |
| 554 |
[]*Address{ |
| 555 |
{ |
| 556 |
Name: "", |
| 557 |
Address: "[email protected]", |
| 558 |
}, |
| 559 |
{ |
| 560 |
Name: "John", |
| 561 |
Address: "[email protected]", |
| 562 |
}, |
| 563 |
}, |
| 564 |
}, |
| 565 |
{ |
| 566 |
`Group1: <[email protected]>;, Group 2: [email protected];, John <[email protected]>`, |
| 567 |
[]*Address{ |
| 568 |
{ |
| 569 |
Name: "", |
| 570 |
Address: "[email protected]", |
| 571 |
}, |
| 572 |
{ |
| 573 |
Name: "", |
| 574 |
Address: "[email protected]", |
| 575 |
}, |
| 576 |
{ |
| 577 |
Name: "John", |
| 578 |
Address: "[email protected]", |
| 579 |
}, |
| 580 |
}, |
| 581 |
}, |
| 582 |
// RFC 2047 "Q"-encoded ISO-8859-1 address. |
| 583 |
{ |
| 584 |
`=?iso-8859-1?q?J=F6rg_Doe?= <[email protected]>`, |
| 585 |
[]*Address{ |
| 586 |
{ |
| 587 |
Name: `Jörg Doe`, |
| 588 |
Address: "[email protected]", |
| 589 |
}, |
| 590 |
}, |
| 591 |
}, |
| 592 |
// RFC 2047 "Q"-encoded US-ASCII address. Dumb but legal. |
| 593 |
{ |
| 594 |
`=?us-ascii?q?J=6Frg_Doe?= <[email protected]>`, |
| 595 |
[]*Address{ |
| 596 |
{ |
| 597 |
Name: `Jorg Doe`, |
| 598 |
Address: "[email protected]", |
| 599 |
}, |
| 600 |
}, |
| 601 |
}, |
| 602 |
// RFC 2047 "Q"-encoded UTF-8 address. |
| 603 |
{ |
| 604 |
`=?utf-8?q?J=C3=B6rg_Doe?= <[email protected]>`, |
| 605 |
[]*Address{ |
| 606 |
{ |
| 607 |
Name: `Jörg Doe`, |
| 608 |
Address: "[email protected]", |
| 609 |
}, |
| 610 |
}, |
| 611 |
}, |
| 612 |
// RFC 2047 "Q"-encoded UTF-8 address with multiple encoded-words. |
| 613 |
{ |
| 614 |
`=?utf-8?q?J=C3=B6rg?= =?utf-8?q?Doe?= <[email protected]>`, |
| 615 |
[]*Address{ |
| 616 |
{ |
| 617 |
Name: `JörgDoe`, |
| 618 |
Address: "[email protected]", |
| 619 |
}, |
| 620 |
}, |
| 621 |
}, |
| 622 |
// RFC 2047, Section 8. |
| 623 |
{ |
| 624 |
`=?ISO-8859-1?Q?Andr=E9?= Pirard <[email protected]>`, |
| 625 |
[]*Address{ |
| 626 |
{ |
| 627 |
Name: `André Pirard`, |
| 628 |
Address: "[email protected]", |
| 629 |
}, |
| 630 |
}, |
| 631 |
}, |
| 632 |
// Custom example of RFC 2047 "B"-encoded ISO-8859-1 address. |
| 633 |
{ |
| 634 |
`=?ISO-8859-1?B?SvZyZw==?= <[email protected]>`, |
| 635 |
[]*Address{ |
| 636 |
{ |
| 637 |
Name: `Jörg`, |
| 638 |
Address: "[email protected]", |
| 639 |
}, |
| 640 |
}, |
| 641 |
}, |
| 642 |
// Custom example of RFC 2047 "B"-encoded UTF-8 address. |
| 643 |
{ |
| 644 |
`=?UTF-8?B?SsO2cmc=?= <[email protected]>`, |
| 645 |
[]*Address{ |
| 646 |
{ |
| 647 |
Name: `Jörg`, |
| 648 |
Address: "[email protected]", |
| 649 |
}, |
| 650 |
}, |
| 651 |
}, |
| 652 |
// Custom example with "." in name. For issue 4938 |
| 653 |
{ |
| 654 |
`Asem H. <[email protected]>`, |
| 655 |
[]*Address{ |
| 656 |
{ |
| 657 |
Name: `Asem H.`, |
| 658 |
Address: "[email protected]", |
| 659 |
}, |
| 660 |
}, |
| 661 |
}, |
| 662 |
// RFC 6532 3.2.3, qtext /= UTF8-non-ascii |
| 663 |
{ |
| 664 |
`"Gø Pher" <[email protected]>`, |
| 665 |
[]*Address{ |
| 666 |
{ |
| 667 |
Name: `Gø Pher`, |
| 668 |
Address: "[email protected]", |
| 669 |
}, |
| 670 |
}, |
| 671 |
}, |
| 672 |
// RFC 6532 3.2, atext /= UTF8-non-ascii |
| 673 |
{ |
| 674 |
`µ <[email protected]>`, |
| 675 |
[]*Address{ |
| 676 |
{ |
| 677 |
Name: `µ`, |
| 678 |
Address: "[email protected]", |
| 679 |
}, |
| 680 |
}, |
| 681 |
}, |
| 682 |
// RFC 6532 3.2.2, local address parts allow UTF-8 |
| 683 |
{ |
| 684 |
`Micro <µ@example.com>`, |
| 685 |
[]*Address{ |
| 686 |
{ |
| 687 |
Name: `Micro`, |
| 688 |
Address: "µ@example.com", |
| 689 |
}, |
| 690 |
}, |
| 691 |
}, |
| 692 |
// RFC 6532 3.2.4, domains parts allow UTF-8 |
| 693 |
{ |
| 694 |
`Micro <micro@µ.example.com>`, |
| 695 |
[]*Address{ |
| 696 |
{ |
| 697 |
Name: `Micro`, |
| 698 |
Address: "micro@µ.example.com", |
| 699 |
}, |
| 700 |
}, |
| 701 |
}, |
| 702 |
// Issue 14866 |
| 703 |
{ |
| 704 |
`"" <[email protected]>`, |
| 705 |
[]*Address{ |
| 706 |
{ |
| 707 |
Name: "", |
| 708 |
Address: "[email protected]", |
| 709 |
}, |
| 710 |
}, |
| 711 |
}, |
| 712 |
// CFWS |
| 713 |
{ |
| 714 |
`<[email protected]> (CFWS (cfws)) (another comment)`, |
| 715 |
[]*Address{ |
| 716 |
{ |
| 717 |
Name: "", |
| 718 |
Address: "[email protected]", |
| 719 |
}, |
| 720 |
}, |
| 721 |
}, |
| 722 |
{ |
| 723 |
`<[email protected]> () (another comment), <[email protected]> (another)`, |
| 724 |
[]*Address{ |
| 725 |
{ |
| 726 |
Name: "", |
| 727 |
Address: "[email protected]", |
| 728 |
}, |
| 729 |
{ |
| 730 |
Name: "", |
| 731 |
Address: "[email protected]", |
| 732 |
}, |
| 733 |
}, |
| 734 |
}, |
| 735 |
// Comment as display name |
| 736 |
{ |
| 737 |
`[email protected] (John Doe)`, |
| 738 |
[]*Address{ |
| 739 |
{ |
| 740 |
Name: "John Doe", |
| 741 |
Address: "[email protected]", |
| 742 |
}, |
| 743 |
}, |
| 744 |
}, |
| 745 |
// Comment and display name |
| 746 |
{ |
| 747 |
`John Doe <[email protected]> (Joey)`, |
| 748 |
[]*Address{ |
| 749 |
{ |
| 750 |
Name: "John Doe", |
| 751 |
Address: "[email protected]", |
| 752 |
}, |
| 753 |
}, |
| 754 |
}, |
| 755 |
// Comment as display name, no space |
| 756 |
{ |
| 757 |
`[email protected](John Doe)`, |
| 758 |
[]*Address{ |
| 759 |
{ |
| 760 |
Name: "John Doe", |
| 761 |
Address: "[email protected]", |
| 762 |
}, |
| 763 |
}, |
| 764 |
}, |
| 765 |
// Comment as display name, Q-encoded |
| 766 |
{ |
| 767 |
`[email protected] (Adam =?utf-8?Q?Sj=C3=B8gren?=)`, |
| 768 |
[]*Address{ |
| 769 |
{ |
| 770 |
Name: "Adam Sjøgren", |
| 771 |
Address: "[email protected]", |
| 772 |
}, |
| 773 |
}, |
| 774 |
}, |
| 775 |
// Comment as display name, Q-encoded and tab-separated |
| 776 |
{ |
| 777 |
`[email protected] (Adam =?utf-8?Q?Sj=C3=B8gren?=)`, |
| 778 |
[]*Address{ |
| 779 |
{ |
| 780 |
Name: "Adam Sjøgren", |
| 781 |
Address: "[email protected]", |
| 782 |
}, |
| 783 |
}, |
| 784 |
}, |
| 785 |
// Nested comment as display name, Q-encoded |
| 786 |
{ |
| 787 |
`[email protected] (Adam =?utf-8?Q?Sj=C3=B8gren?= (Debian))`, |
| 788 |
[]*Address{ |
| 789 |
{ |
| 790 |
Name: "Adam Sjøgren (Debian)", |
| 791 |
Address: "[email protected]", |
| 792 |
}, |
| 793 |
}, |
| 794 |
}, |
| 795 |
// Comment in group display name |
| 796 |
{ |
| 797 |
`group (comment:): [email protected], [email protected];`, |
| 798 |
[]*Address{ |
| 799 |
{ |
| 800 |
Address: "[email protected]", |
| 801 |
}, |
| 802 |
{ |
| 803 |
Address: "[email protected]", |
| 804 |
}, |
| 805 |
}, |
| 806 |
}, |
| 807 |
{ |
| 808 |
`x(:"):"@a.example;("@b.example;`, |
| 809 |
[]*Address{ |
| 810 |
{ |
| 811 |
Address: `@a.example;(@b.example`, |
| 812 |
}, |
| 813 |
}, |
| 814 |
}, |
| 815 |
// Domain-literal |
| 816 |
{ |
| 817 |
`jdoe@[192.168.0.1]`, |
| 818 |
[]*Address{{ |
| 819 |
Address: "jdoe@[192.168.0.1]", |
| 820 |
}}, |
| 821 |
}, |
| 822 |
{ |
| 823 |
`John Doe <jdoe@[192.168.0.1]>`, |
| 824 |
[]*Address{{ |
| 825 |
Name: "John Doe", |
| 826 |
Address: "jdoe@[192.168.0.1]", |
| 827 |
}}, |
| 828 |
}, |
| 829 |
} |
| 830 |
for _, test := range tests { |
| 831 |
if len(test.exp) == 1 { |
| 832 |
addr, err := ParseAddress(test.addrsStr) |
| 833 |
if err != nil { |
| 834 |
t.Errorf("Failed parsing (single) %q: %v", test.addrsStr, err) |
| 835 |
continue |
| 836 |
} |
| 837 |
if !reflect.DeepEqual([]*Address{addr}, test.exp) { |
| 838 |
t.Errorf("Parse (single) of %q: got %+v, want %+v", test.addrsStr, addr, test.exp) |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
addrs, err := ParseAddressList(test.addrsStr) |
| 843 |
if err != nil { |
| 844 |
t.Errorf("Failed parsing (list) %q: %v", test.addrsStr, err) |
| 845 |
continue |
| 846 |
} |
| 847 |
if !reflect.DeepEqual(addrs, test.exp) { |
| 848 |
t.Errorf("Parse (list) of %q: got %+v, want %+v", test.addrsStr, addrs, test.exp) |
| 849 |
} |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
func TestAddressParser(t *testing.T) { |
| 854 |
tests := []struct { |
| 855 |
addrsStr string |
| 856 |
exp []*Address |
| 857 |
}{ |
| 858 |
// Bare address |
| 859 |
{ |
| 860 |
`[email protected]`, |
| 861 |
[]*Address{{ |
| 862 |
Address: "[email protected]", |
| 863 |
}}, |
| 864 |
}, |
| 865 |
// RFC 5322, Appendix A.1.1 |
| 866 |
{ |
| 867 |
`John Doe <[email protected]>`, |
| 868 |
[]*Address{{ |
| 869 |
Name: "John Doe", |
| 870 |
Address: "[email protected]", |
| 871 |
}}, |
| 872 |
}, |
| 873 |
// RFC 5322, Appendix A.1.2 |
| 874 |
{ |
| 875 |
`"Joe Q. Public" <[email protected]>`, |
| 876 |
[]*Address{{ |
| 877 |
Name: "Joe Q. Public", |
| 878 |
Address: "[email protected]", |
| 879 |
}}, |
| 880 |
}, |
| 881 |
{ |
| 882 |
`Mary Smith <[email protected]>, [email protected], Who? <[email protected]>`, |
| 883 |
[]*Address{ |
| 884 |
{ |
| 885 |
Name: "Mary Smith", |
| 886 |
Address: "[email protected]", |
| 887 |
}, |
| 888 |
{ |
| 889 |
Address: "[email protected]", |
| 890 |
}, |
| 891 |
{ |
| 892 |
Name: "Who?", |
| 893 |
Address: "[email protected]", |
| 894 |
}, |
| 895 |
}, |
| 896 |
}, |
| 897 |
{ |
| 898 |
`<[email protected]>, "Giant; \"Big\" Box" <[email protected]>`, |
| 899 |
[]*Address{ |
| 900 |
{ |
| 901 |
Address: "[email protected]", |
| 902 |
}, |
| 903 |
{ |
| 904 |
Name: `Giant; "Big" Box`, |
| 905 |
Address: "[email protected]", |
| 906 |
}, |
| 907 |
}, |
| 908 |
}, |
| 909 |
// RFC 2047 "Q"-encoded ISO-8859-1 address. |
| 910 |
{ |
| 911 |
`=?iso-8859-1?q?J=F6rg_Doe?= <[email protected]>`, |
| 912 |
[]*Address{ |
| 913 |
{ |
| 914 |
Name: `Jörg Doe`, |
| 915 |
Address: "[email protected]", |
| 916 |
}, |
| 917 |
}, |
| 918 |
}, |
| 919 |
// RFC 2047 "Q"-encoded US-ASCII address. Dumb but legal. |
| 920 |
{ |
| 921 |
`=?us-ascii?q?J=6Frg_Doe?= <[email protected]>`, |
| 922 |
[]*Address{ |
| 923 |
{ |
| 924 |
Name: `Jorg Doe`, |
| 925 |
Address: "[email protected]", |
| 926 |
}, |
| 927 |
}, |
| 928 |
}, |
| 929 |
// RFC 2047 "Q"-encoded ISO-8859-15 address. |
| 930 |
{ |
| 931 |
`=?ISO-8859-15?Q?J=F6rg_Doe?= <[email protected]>`, |
| 932 |
[]*Address{ |
| 933 |
{ |
| 934 |
Name: `Jörg Doe`, |
| 935 |
Address: "[email protected]", |
| 936 |
}, |
| 937 |
}, |
| 938 |
}, |
| 939 |
// RFC 2047 "B"-encoded windows-1252 address. |
| 940 |
{ |
| 941 |
`=?windows-1252?q?Andr=E9?= Pirard <[email protected]>`, |
| 942 |
[]*Address{ |
| 943 |
{ |
| 944 |
Name: `André Pirard`, |
| 945 |
Address: "[email protected]", |
| 946 |
}, |
| 947 |
}, |
| 948 |
}, |
| 949 |
// Custom example of RFC 2047 "B"-encoded ISO-8859-15 address. |
| 950 |
{ |
| 951 |
`=?ISO-8859-15?B?SvZyZw==?= <[email protected]>`, |
| 952 |
[]*Address{ |
| 953 |
{ |
| 954 |
Name: `Jörg`, |
| 955 |
Address: "[email protected]", |
| 956 |
}, |
| 957 |
}, |
| 958 |
}, |
| 959 |
// Custom example of RFC 2047 "B"-encoded UTF-8 address. |
| 960 |
{ |
| 961 |
`=?UTF-8?B?SsO2cmc=?= <[email protected]>`, |
| 962 |
[]*Address{ |
| 963 |
{ |
| 964 |
Name: `Jörg`, |
| 965 |
Address: "[email protected]", |
| 966 |
}, |
| 967 |
}, |
| 968 |
}, |
| 969 |
// Custom example with "." in name. For issue 4938 |
| 970 |
{ |
| 971 |
`Asem H. <[email protected]>`, |
| 972 |
[]*Address{ |
| 973 |
{ |
| 974 |
Name: `Asem H.`, |
| 975 |
Address: "[email protected]", |
| 976 |
}, |
| 977 |
}, |
| 978 |
}, |
| 979 |
// Domain-literal |
| 980 |
{ |
| 981 |
`jdoe@[192.168.0.1]`, |
| 982 |
[]*Address{{ |
| 983 |
Address: "jdoe@[192.168.0.1]", |
| 984 |
}}, |
| 985 |
}, |
| 986 |
{ |
| 987 |
`John Doe <jdoe@[192.168.0.1]>`, |
| 988 |
[]*Address{{ |
| 989 |
Name: "John Doe", |
| 990 |
Address: "jdoe@[192.168.0.1]", |
| 991 |
}}, |
| 992 |
}, |
| 993 |
} |
| 994 |
|
| 995 |
ap := AddressParser{WordDecoder: &mime.WordDecoder{ |
| 996 |
CharsetReader: func(charset string, input io.Reader) (io.Reader, error) { |
| 997 |
in, err := io.ReadAll(input) |
| 998 |
if err != nil { |
| 999 |
return nil, err |
| 1000 |
} |
| 1001 |
|
| 1002 |
switch charset { |
| 1003 |
case "iso-8859-15": |
| 1004 |
in = bytes.ReplaceAll(in, []byte("\xf6"), []byte("ö")) |
| 1005 |
case "windows-1252": |
| 1006 |
in = bytes.ReplaceAll(in, []byte("\xe9"), []byte("é")) |
| 1007 |
} |
| 1008 |
|
| 1009 |
return bytes.NewReader(in), nil |
| 1010 |
}, |
| 1011 |
}} |
| 1012 |
|
| 1013 |
for _, test := range tests { |
| 1014 |
if len(test.exp) == 1 { |
| 1015 |
addr, err := ap.Parse(test.addrsStr) |
| 1016 |
if err != nil { |
| 1017 |
t.Errorf("Failed parsing (single) %q: %v", test.addrsStr, err) |
| 1018 |
continue |
| 1019 |
} |
| 1020 |
if !reflect.DeepEqual([]*Address{addr}, test.exp) { |
| 1021 |
t.Errorf("Parse (single) of %q: got %+v, want %+v", test.addrsStr, addr, test.exp) |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
addrs, err := ap.ParseList(test.addrsStr) |
| 1026 |
if err != nil { |
| 1027 |
t.Errorf("Failed parsing (list) %q: %v", test.addrsStr, err) |
| 1028 |
continue |
| 1029 |
} |
| 1030 |
if !reflect.DeepEqual(addrs, test.exp) { |
| 1031 |
t.Errorf("Parse (list) of %q: got %+v, want %+v", test.addrsStr, addrs, test.exp) |
| 1032 |
} |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
func TestAddressString(t *testing.T) { |
| 1037 |
tests := []struct { |
| 1038 |
addr *Address |
| 1039 |
exp string |
| 1040 |
}{ |
| 1041 |
{ |
| 1042 |
&Address{Address: "[email protected]"}, |
| 1043 |
"<[email protected]>", |
| 1044 |
}, |
| 1045 |
{ // quoted local parts: RFC 5322, 3.4.1. and 3.2.4. |
| 1046 |
&Address{Address: `my@idiot@[email protected]`}, |
| 1047 |
`<"my@idiot@address"@example.com>`, |
| 1048 |
}, |
| 1049 |
{ // quoted local parts |
| 1050 |
&Address{Address: ` @example.com`}, |
| 1051 |
`<" "@example.com>`, |
| 1052 |
}, |
| 1053 |
{ |
| 1054 |
&Address{Name: "Bob", Address: "[email protected]"}, |
| 1055 |
`"Bob" <[email protected]>`, |
| 1056 |
}, |
| 1057 |
{ |
| 1058 |
// note the ö (o with an umlaut) |
| 1059 |
&Address{Name: "Böb", Address: "[email protected]"}, |
| 1060 |
`=?utf-8?q?B=C3=B6b?= <[email protected]>`, |
| 1061 |
}, |
| 1062 |
{ |
| 1063 |
&Address{Name: "Bob Jane", Address: "[email protected]"}, |
| 1064 |
`"Bob Jane" <[email protected]>`, |
| 1065 |
}, |
| 1066 |
{ |
| 1067 |
&Address{Name: "Böb Jacöb", Address: "[email protected]"}, |
| 1068 |
`=?utf-8?q?B=C3=B6b_Jac=C3=B6b?= <[email protected]>`, |
| 1069 |
}, |
| 1070 |
{ // https://golang.org/issue/12098 |
| 1071 |
&Address{Name: "Rob", Address: ""}, |
| 1072 |
`"Rob" <@>`, |
| 1073 |
}, |
| 1074 |
{ // https://golang.org/issue/12098 |
| 1075 |
&Address{Name: "Rob", Address: "@"}, |
| 1076 |
`"Rob" <@>`, |
| 1077 |
}, |
| 1078 |
{ |
| 1079 |
&Address{Name: "Böb, Jacöb", Address: "[email protected]"}, |
| 1080 |
`=?utf-8?b?QsO2YiwgSmFjw7Zi?= <[email protected]>`, |
| 1081 |
}, |
| 1082 |
{ |
| 1083 |
&Address{Name: "=??Q?x?=", Address: "[email protected]"}, |
| 1084 |
`"=??Q?x?=" <[email protected]>`, |
| 1085 |
}, |
| 1086 |
{ |
| 1087 |
&Address{Name: "=?hello", Address: "[email protected]"}, |
| 1088 |
`"=?hello" <[email protected]>`, |
| 1089 |
}, |
| 1090 |
{ |
| 1091 |
&Address{Name: "world?=", Address: "[email protected]"}, |
| 1092 |
`"world?=" <[email protected]>`, |
| 1093 |
}, |
| 1094 |
{ |
| 1095 |
// should q-encode even for invalid utf-8. |
| 1096 |
&Address{Name: string([]byte{0xed, 0xa0, 0x80}), Address: "[email protected]"}, |
| 1097 |
"=?utf-8?q?=ED=A0=80?= <[email protected]>", |
| 1098 |
}, |
| 1099 |
// Domain-literal |
| 1100 |
{ |
| 1101 |
&Address{Address: "bob@[192.168.0.1]"}, |
| 1102 |
"<bob@[192.168.0.1]>", |
| 1103 |
}, |
| 1104 |
{ |
| 1105 |
&Address{Name: "Bob", Address: "bob@[192.168.0.1]"}, |
| 1106 |
`"Bob" <bob@[192.168.0.1]>`, |
| 1107 |
}, |
| 1108 |
} |
| 1109 |
for _, test := range tests { |
| 1110 |
s := test.addr.String() |
| 1111 |
if s != test.exp { |
| 1112 |
t.Errorf("Address%+v.String() = %v, want %v", *test.addr, s, test.exp) |
| 1113 |
continue |
| 1114 |
} |
| 1115 |
|
| 1116 |
// Check round-trip. |
| 1117 |
if test.addr.Address != "" && test.addr.Address != "@" { |
| 1118 |
a, err := ParseAddress(test.exp) |
| 1119 |
if err != nil { |
| 1120 |
t.Errorf("ParseAddress(%#q): %v", test.exp, err) |
| 1121 |
continue |
| 1122 |
} |
| 1123 |
if a.Name != test.addr.Name || a.Address != test.addr.Address { |
| 1124 |
t.Errorf("ParseAddress(%#q) = %#v, want %#v", test.exp, a, test.addr) |
| 1125 |
} |
| 1126 |
} |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
// Check if all valid addresses can be parsed, formatted and parsed again |
| 1131 |
func TestAddressParsingAndFormatting(t *testing.T) { |
| 1132 |
|
| 1133 |
// Should pass |
| 1134 |
tests := []string{ |
| 1135 |
`<[email protected]>`, |
| 1136 |
`<[email protected]>`, |
| 1137 |
`<".bob"@example.com>`, |
| 1138 |
`<" "@example.com>`, |
| 1139 |
`<[email protected]>`, |
| 1140 |
`<"dot.and space"@example.com>`, |
| 1141 |
`<"[email protected]"@example.com>`, |
| 1142 |
`<admin@mailserver1>`, |
| 1143 |
`<postmaster@localhost>`, |
| 1144 |
"<#!$%&'*+-/=?^_`{}|[email protected]>", |
| 1145 |
`<"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com>`, // escaped quotes |
| 1146 |
`<"()<>[]:,;@\\\"!#$%&'*+-/=?^_{}| ~.a"@example.org>`, // escaped backslashes |
| 1147 |
`<"Abc\\@def"@example.com>`, |
| 1148 |
`<"Joe\\Blow"@example.com>`, |
| 1149 |
`<test1/[email protected]>`, |
| 1150 |
`<def!xyz%[email protected]>`, |
| 1151 |
`<[email protected]>`, |
| 1152 |
`<joe@uk>`, |
| 1153 |
`<[email protected]>`, |
| 1154 |
`<"..."@test.com>`, |
| 1155 |
`<"john..doe"@example.com>`, |
| 1156 |
`<"john.doe."@example.com>`, |
| 1157 |
`<".john.doe"@example.com>`, |
| 1158 |
`<"."@example.com>`, |
| 1159 |
`<".."@example.com>`, |
| 1160 |
`<"0:"@0>`, |
| 1161 |
`<Bob@[192.168.0.1]>`, |
| 1162 |
} |
| 1163 |
|
| 1164 |
for _, test := range tests { |
| 1165 |
addr, err := ParseAddress(test) |
| 1166 |
if err != nil { |
| 1167 |
t.Errorf("Couldn't parse address %s: %s", test, err.Error()) |
| 1168 |
continue |
| 1169 |
} |
| 1170 |
str := addr.String() |
| 1171 |
addr, err = ParseAddress(str) |
| 1172 |
if err != nil { |
| 1173 |
t.Errorf("ParseAddr(%q) error: %v", test, err) |
| 1174 |
continue |
| 1175 |
} |
| 1176 |
|
| 1177 |
if addr.String() != test { |
| 1178 |
t.Errorf("String() round-trip = %q; want %q", addr, test) |
| 1179 |
continue |
| 1180 |
} |
| 1181 |
|
| 1182 |
} |
| 1183 |
|
| 1184 |
// Should fail |
| 1185 |
badTests := []string{ |
| 1186 |
`<Abc.example.com>`, |
| 1187 |
`<A@b@[email protected]>`, |
| 1188 |
`<a"b(c)d,e:f;g<h>i[j\k][email protected]>`, |
| 1189 |
`<just"not"[email protected]>`, |
| 1190 |
`<this is"not\[email protected]>`, |
| 1191 |
`<this\ still\"not\\[email protected]>`, |
| 1192 |
`<[email protected]>`, |
| 1193 |
`<[email protected]>`, |
| 1194 |
`<[email protected]>`, |
| 1195 |
`<[email protected]>`, |
| 1196 |
`<[email protected]>`, |
| 1197 |
`<[email protected]>`, |
| 1198 |
`<@example.com>`, |
| 1199 |
`<[email protected]>`, |
| 1200 |
`<test@.>`, |
| 1201 |
`< @example.com>`, |
| 1202 |
`<""test""blah""@example.com>`, |
| 1203 |
`<""@0>`, |
| 1204 |
} |
| 1205 |
|
| 1206 |
for _, test := range badTests { |
| 1207 |
_, err := ParseAddress(test) |
| 1208 |
if err == nil { |
| 1209 |
t.Errorf("Should have failed to parse address: %s", test) |
| 1210 |
continue |
| 1211 |
} |
| 1212 |
|
| 1213 |
} |
| 1214 |
|
| 1215 |
} |
| 1216 |
|
| 1217 |
func TestAddressFormattingAndParsing(t *testing.T) { |
| 1218 |
tests := []*Address{ |
| 1219 |
{Name: "@lïce", Address: "[email protected]"}, |
| 1220 |
{Name: "Böb O'Connor", Address: "[email protected]"}, |
| 1221 |
{Name: "???", Address: "[email protected]"}, |
| 1222 |
{Name: "Böb ???", Address: "[email protected]"}, |
| 1223 |
{Name: "Böb (Jacöb)", Address: "[email protected]"}, |
| 1224 |
{Name: "à#$%&'(),.:;<>@[]^`{|}~'", Address: "[email protected]"}, |
| 1225 |
// https://golang.org/issue/11292 |
| 1226 |
{Name: "\"\\\x1f,\"", Address: "0@0"}, |
| 1227 |
// https://golang.org/issue/12782 |
| 1228 |
{Name: "naé, mée", Address: "[email protected]"}, |
| 1229 |
} |
| 1230 |
|
| 1231 |
for i, test := range tests { |
| 1232 |
parsed, err := ParseAddress(test.String()) |
| 1233 |
if err != nil { |
| 1234 |
t.Errorf("test #%d: ParseAddr(%q) error: %v", i, test.String(), err) |
| 1235 |
continue |
| 1236 |
} |
| 1237 |
if parsed.Name != test.Name { |
| 1238 |
t.Errorf("test #%d: Parsed name = %q; want %q", i, parsed.Name, test.Name) |
| 1239 |
} |
| 1240 |
if parsed.Address != test.Address { |
| 1241 |
t.Errorf("test #%d: Parsed address = %q; want %q", i, parsed.Address, test.Address) |
| 1242 |
} |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
func TestEmptyAddress(t *testing.T) { |
| 1247 |
parsed, err := ParseAddress("") |
| 1248 |
if parsed != nil || err == nil { |
| 1249 |
t.Errorf(`ParseAddress("") = %v, %v, want nil, error`, parsed, err) |
| 1250 |
} |
| 1251 |
list, err := ParseAddressList("") |
| 1252 |
if len(list) > 0 || err == nil { |
| 1253 |
t.Errorf(`ParseAddressList("") = %v, %v, want nil, error`, list, err) |
| 1254 |
} |
| 1255 |
list, err = ParseAddressList(",") |
| 1256 |
if len(list) > 0 || err == nil { |
| 1257 |
t.Errorf(`ParseAddressList("") = %v, %v, want nil, error`, list, err) |
| 1258 |
} |
| 1259 |
list, err = ParseAddressList("a@b c@d") |
| 1260 |
if len(list) > 0 || err == nil { |
| 1261 |
t.Errorf(`ParseAddressList("") = %v, %v, want nil, error`, list, err) |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
func BenchmarkConsumePhrase(b *testing.B) { |
| 1266 |
for _, n := range []int{10, 100, 1000, 10000} { |
| 1267 |
b.Run(fmt.Sprintf("words-%d", n), func(b *testing.B) { |
| 1268 |
input := strings.Repeat("=?utf-8?q?hello?= ", n) + "<[email protected]>" |
| 1269 |
for b.Loop() { |
| 1270 |
(&addrParser{s: input}).consumePhrase() |
| 1271 |
} |
| 1272 |
}) |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
func BenchmarkConsumeComment(b *testing.B) { |
| 1277 |
for _, n := range []int{10, 100, 1000, 10000} { |
| 1278 |
b.Run(fmt.Sprintf("depth-%d", n), func(b *testing.B) { |
| 1279 |
// Build a deeply nested comment: (((...a...))) |
| 1280 |
open := strings.Repeat("(", n) |
| 1281 |
close := strings.Repeat(")", n) |
| 1282 |
// consumeComment expects the leading '(' already consumed, |
| 1283 |
// so we start with one fewer opening paren and the parser |
| 1284 |
// will handle nesting from there. |
| 1285 |
input := open[:n-1] + "a" + close |
| 1286 |
for b.Loop() { |
| 1287 |
p := addrParser{s: input} |
| 1288 |
p.consumeComment() |
| 1289 |
} |
| 1290 |
}) |
| 1291 |
} |
| 1292 |
} |