diff options
| author | MathiasB <git@denbeke.be> | 2015-04-10 12:14:16 +0200 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2015-07-22 20:36:46 +0000 |
| commit | daaa45073e887cb6d8075c2caafdfe8425bca25a (patch) | |
| tree | 04e26ddf5671cf0438753468d52b6a69c4453e92 /src/net/mail/message_test.go | |
| parent | 5201bf7ad184cac07466016a78b80aed5e472be4 (diff) | |
| download | go-daaa45073e887cb6d8075c2caafdfe8425bca25a.tar.xz | |
net/mail: enhanced Address.String and ParseAddress to match RFC 5322
Updated Address.String so it restores quoted local parts, which wasn't
done before.
When parsing `<" "@example.com>`, the formatted string returned
`< @example>`, which doens't match RFC 5322, since a space is not atext.
Another example is `<"bob@valid"@example.com>` which returned
`<bob@valid@example.com>`, which is completely invalid.
I also added support for quotes and backslashes in a quoted local part.
Besides formatting a parsed Address, the ParseAddress function also
needed more testing and finetuning for special cases.
Things like `<.john.doe@example.com>` and `<john..doe@example.com>`
e.a. were accepted, but are invalid.
I fixed those details and add tests for some other special cases.
Fixes #10768
Change-Id: Ib0caf8ad603eb21e32fcb957a5f1a0fe5d1c6e6e
Reviewed-on: https://go-review.googlesource.com/8724
Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/net/mail/message_test.go')
| -rw-r--r-- | src/net/mail/message_test.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/net/mail/message_test.go b/src/net/mail/message_test.go index 43574c6188..1da3213f7e 100644 --- a/src/net/mail/message_test.go +++ b/src/net/mail/message_test.go @@ -458,6 +458,14 @@ func TestAddressFormatting(t *testing.T) { &Address{Address: "bob@example.com"}, "<bob@example.com>", }, + { // quoted local parts: RFC 5322, 3.4.1. and 3.2.4. + &Address{Address: `my@idiot@address@example.com`}, + `<"my@idiot@address"@example.com>`, + }, + { // quoted local parts + &Address{Address: ` @example.com`}, + `<" "@example.com>`, + }, { &Address{Name: "Bob", Address: "bob@example.com"}, `"Bob" <bob@example.com>`, @@ -483,3 +491,87 @@ func TestAddressFormatting(t *testing.T) { } } } + +// Check if all valid addresses can be parsed, formatted and parsed again +func TestAddressParsingAndFormatting(t *testing.T) { + + // Should pass + tests := []string{ + `<Bob@example.com>`, + `<bob.bob@example.com>`, + `<".bob"@example.com>`, + `<" "@example.com>`, + `<some.mail-with-dash@example.com>`, + `<"dot.and space"@example.com>`, + `<"very.unusual.@.unusual.com"@example.com>`, + `<admin@mailserver1>`, + `<postmaster@localhost>`, + "<#!$%&'*+-/=?^_`{}|~@example.org>", + `<"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com>`, // escaped quotes + `<"()<>[]:,;@\\\"!#$%&'*+-/=?^_{}| ~.a"@example.org>`, // escaped backslashes + `<"Abc\\@def"@example.com>`, + `<"Joe\\Blow"@example.com>`, + `<test1/test2=test3@example.com>`, + `<def!xyz%abc@example.com>`, + `<_somename@example.com>`, + `<joe@uk>`, + `<~@example.com>`, + `<"..."@test.com>`, + `<"john..doe"@example.com>`, + `<"john.doe."@example.com>`, + `<".john.doe"@example.com>`, + `<"."@example.com>`, + `<".."@example.com>`, + } + + for _, test := range tests { + addr, err := ParseAddress(test) + if err != nil { + t.Errorf("Couldn't parse address %s: %s", test, err.Error()) + continue + } + str := addr.String() + addr, err = ParseAddress(str) + if err != nil { + t.Errorf("ParseAddr(%q) error: %v", test, err) + continue + } + + if addr.String() != test { + t.Errorf("String() round-trip = %q; want %q", addr, test) + continue + } + + } + + // Should fail + badTests := []string{ + `<Abc.example.com>`, + `<A@b@c@example.com>`, + `<a"b(c)d,e:f;g<h>i[j\k]l@example.com>`, + `<just"not"right@example.com>`, + `<this is"not\allowed@example.com>`, + `<this\ still\"not\\allowed@example.com>`, + `<john..doe@example.com>`, + `<john.doe@example..com>`, + `<john.doe@example..com>`, + `<john.doe.@example.com>`, + `<john.doe.@.example.com>`, + `<.john.doe@example.com>`, + `<@example.com>`, + `<.@example.com>`, + `<test@.>`, + `< @example.com>`, + `<""test""blah""@example.com>`, + } + + for _, test := range badTests { + _, err := ParseAddress(test) + if err == nil { + t.Errorf("Should have failed to parse address: %s", test) + continue + } + + } + +} |
