-
-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decode IDN-encoded local-parts in addrspecs when parsing
We already decoded IDN domains, but did not do it for local-parts. Now we do it for both. Also added new MailboxAddress.GetAddress(bool idnEncode) for use with the SmtpClient in MailKit. Should fix jstedfast/MailKit#1026
- Loading branch information
Showing
3 changed files
with
92 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
|
||
using System; | ||
using System.Text; | ||
using System.Globalization; | ||
using System.Collections.Generic; | ||
|
||
using NUnit.Framework; | ||
|
@@ -563,36 +564,85 @@ public void TestParseGroupNameColon () | |
AssertParseFailure (text, false, tokenIndex, errorIndex); | ||
} | ||
|
||
[Test] | ||
public void TestGetAddress () | ||
{ | ||
var idn = new IdnMapping (); | ||
MailboxAddress mailbox; | ||
|
||
mailbox = new MailboxAddress ("Unit Test", "點看@domain.com"); | ||
Assert.AreEqual ("點看@domain.com", mailbox.GetAddress (false), "IDN-decode #1"); | ||
Assert.AreEqual (idn.GetAscii ("點看") + "@domain.com", mailbox.GetAddress (true), "IDN-encode #1"); | ||
|
||
mailbox = new MailboxAddress ("Unit Test", idn.GetAscii ("點看") + "@domain.com"); | ||
Assert.AreEqual ("點看@domain.com", mailbox.GetAddress (false), "IDN-decode #2"); | ||
Assert.AreEqual (idn.GetAscii ("點看") + "@domain.com", mailbox.GetAddress (true), "IDN-encode #2"); | ||
|
||
mailbox = new MailboxAddress ("Unit Test", "user@名がドメイン.com"); | ||
Assert.AreEqual ("user@名がドメイン.com", mailbox.GetAddress (false), "IDN-decode #3"); | ||
Assert.AreEqual ("user@" + idn.GetAscii ("名がドメイン.com"), mailbox.GetAddress (true), "IDN-encode #3"); | ||
|
||
mailbox = new MailboxAddress ("Unit Test", "user@" + idn.GetAscii ("名がドメイン.com")); | ||
Assert.AreEqual ("user@名がドメイン.com", mailbox.GetAddress (false), "IDN-decode #4"); | ||
Assert.AreEqual ("user@" + idn.GetAscii ("名がドメイン.com"), mailbox.GetAddress (true), "IDN-encode #4"); | ||
|
||
mailbox = new MailboxAddress ("Unit Test", "點看@名がドメイン.com"); | ||
Assert.AreEqual ("點看@名がドメイン.com", mailbox.GetAddress (false), "IDN-decode #5"); | ||
Assert.AreEqual (idn.GetAscii ("點看") + "@" + idn.GetAscii ("名がドメイン.com"), mailbox.GetAddress (true), "IDN-encode #5"); | ||
|
||
mailbox = new MailboxAddress ("Unit Test", idn.GetAscii ("點看") + "@" + idn.GetAscii ("名がドメイン.com")); | ||
Assert.AreEqual ("點看@名がドメイン.com", mailbox.GetAddress (false), "IDN-decode #6"); | ||
Assert.AreEqual (idn.GetAscii ("點看") + "@" + idn.GetAscii ("名がドメイン.com"), mailbox.GetAddress (true), "IDN-encode #6"); | ||
} | ||
|
||
[Test] | ||
public void TestIsInternational () | ||
{ | ||
var mailbox = new MailboxAddress ("Kristoffer Brånemyr", "brå[email protected]"); | ||
const string expected = "Kristoffer Brånemyr <brå[email protected]>"; | ||
var options = FormatOptions.Default.Clone (); | ||
options.International = true; | ||
var idn = new IdnMapping (); | ||
MailboxAddress mailbox; | ||
string encoded; | ||
|
||
options.International = true; | ||
// Test IsInternational local-parts | ||
mailbox = new MailboxAddress ("Unit Test", "點看@domain.com"); | ||
Assert.IsTrue (mailbox.IsInternational, "IsInternational local-part"); | ||
encoded = mailbox.ToString (options, true); | ||
Assert.AreEqual ("Unit Test <點看@domain.com>", encoded, "ToString local-part"); | ||
|
||
// Test IsInternational IDN-encoded local-parts | ||
mailbox = new MailboxAddress ("Unit Test", idn.GetAscii ("點看") + "@domain.com"); | ||
Assert.IsTrue (mailbox.IsInternational, "IsInternational IDN-encoded local-part"); | ||
encoded = mailbox.ToString (options, true); | ||
Assert.AreEqual (expected, encoded, "ToString"); | ||
Assert.AreEqual ("Unit Test <點看@domain.com>", encoded, "ToString IDN-encoded local-part"); | ||
|
||
Assert.IsTrue (mailbox.IsInternational, "IsInternational"); | ||
// Test IsInternational domain | ||
mailbox = new MailboxAddress ("Unit Test", "user@名がドメイン.com"); | ||
Assert.IsTrue (mailbox.IsInternational, "IsInternational domain"); | ||
encoded = mailbox.ToString (options, true); | ||
Assert.AreEqual ("Unit Test <user@名がドメイン.com>", encoded, "ToString domain"); | ||
|
||
mailbox = new MailboxAddress ("Kristoffer Brånemyr", "[email protected]"); | ||
// Test IsInternational IDN-encoded domain | ||
mailbox = new MailboxAddress ("Unit Test", "user@" + idn.GetAscii ("名がドメイン.com")); | ||
Assert.IsTrue (mailbox.IsInternational, "IsInternational IDN-encoded domain"); | ||
encoded = mailbox.ToString (options, true); | ||
Assert.AreEqual ("Unit Test <user@名がドメイン.com>", encoded, "ToString IDN-encoded domain"); | ||
|
||
// Test IsInternational routes | ||
mailbox = new MailboxAddress ("Unit Test", "[email protected]"); | ||
Assert.IsFalse (mailbox.IsInternational, "IsInternational"); | ||
|
||
mailbox.Route.Add ("kristoffer"); // non-international route | ||
mailbox.Route.Add ("brånemyr"); // international route | ||
|
||
Assert.IsTrue (mailbox.IsInternational, "IsInternational"); | ||
mailbox.Route.Add ("route1"); // non-international route | ||
mailbox.Route.Add ("名がドメイン.com"); // international route | ||
Assert.IsTrue (mailbox.IsInternational, "IsInternational route"); | ||
encoded = mailbox.ToString (options, true); | ||
Assert.AreEqual ("Unit Test <@route1,@名がドメイン.com:[email protected]>", encoded, "ToString route"); | ||
} | ||
|
||
[Test] | ||
public void TestIdnEncoding () | ||
{ | ||
//const string userAscii = "xn--c1yn36f@domain"; | ||
//const string userUnicode = "點看@domain"; | ||
const string userAscii = "xn--c1yn36f@domain.com"; | ||
const string userUnicode = "點看@domain.com"; | ||
const string domainAscii = "[email protected]"; | ||
const string domainUnicode = "user@名がドメイン.com"; | ||
string encoded; | ||
|
@@ -603,11 +653,11 @@ public void TestIdnEncoding () | |
encoded = MailboxAddress.DecodeAddrspec (domainAscii); | ||
Assert.AreEqual (domainUnicode, encoded, "Domain (Decode)"); | ||
|
||
//encoded = MailboxAddress.EncodeAddrspec (userUnicode); | ||
//Assert.AreEqual (userAscii, encoded, "Local-part (Encode)"); | ||
encoded = MailboxAddress.EncodeAddrspec (userUnicode); | ||
Assert.AreEqual (userAscii, encoded, "Local-part (Encode)"); | ||
|
||
//encoded = MailboxAddress.DecodeAddrspec (userAscii); | ||
//Assert.AreEqual (userUnicode, encoded, "Local-part (Decode)"); | ||
encoded = MailboxAddress.DecodeAddrspec (userAscii); | ||
Assert.AreEqual (userUnicode, encoded, "Local-part (Decode)"); | ||
} | ||
|
||
[Test] | ||
|