mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-23 03:39:35 -10:00
Add MailAddress.TryCreate api (#1052)
* Add TryCreate apis * address PR feedback * Update src/libraries/Common/src/System/Net/Mail/MailAddressParser.cs Co-Authored-By: Stephen Toub <[email protected]> * Update src/libraries/Common/src/System/Net/Mail/DotAtomReader.cs Co-Authored-By: Stephen Toub <[email protected]> * Update src/libraries/Common/src/System/Net/Mail/MailAddressParser.cs Co-Authored-By: Stephen Toub <[email protected]> * Address PR feedback, split TryParse parameters for better visibility * Address PR feedback, add return api documentation, update code comments * set out parameter when we have result Co-authored-by: Stephen Toub <[email protected]>
This commit is contained in:
1 parent
349b6e0101
commit
aea45f4e75
14 files changed
+876
-416
No files matched your search
@@ -25,11 +25,11 @@ namespace System.Net.Mail
|
||||
// - The next index past the terminating bracket (data[index + 1] == StartSquareBracket).
|
||||
// e.g. In (user@[domain]), starting at index=12 (]) returns index=4 (@).
|
||||
//
|
||||
// A FormatException will be thrown if:
|
||||
// A FormatException will be thrown or false is returned if:
|
||||
// - A non-escaped character is encountered that is not valid in a domain literal, including Unicode.
|
||||
// - The final bracket is not found.
|
||||
//
|
||||
internal static int ReadReverse(string data, int index)
|
||||
internal static bool TryReadReverse(string data, int index, out int outIndex, bool throwExceptionIfFail)
|
||||
{
|
||||
Debug.Assert(0 <= index && index < data.Length, "index was outside the bounds of the string: " + index);
|
||||
Debug.Assert(data[index] == MailBnfHelper.EndSquareBracket, "data did not end with a square bracket");
|
||||
@@ -40,28 +40,48 @@ namespace System.Net.Mail
|
||||
do
|
||||
{
|
||||
// Check for valid whitespace
|
||||
index = WhitespaceReader.ReadFwsReverse(data, index);
|
||||
if (!WhitespaceReader.TryReadFwsReverse(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Check for escaped characters
|
||||
int quotedCharCount = QuotedPairReader.CountQuotedChars(data, index, false);
|
||||
if (!QuotedPairReader.TryCountQuotedChars(data, index, false, out int quotedCharCount, throwExceptionIfFail))
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (quotedCharCount > 0)
|
||||
{
|
||||
// Skip quoted pairs
|
||||
index = index - quotedCharCount;
|
||||
index -= quotedCharCount;
|
||||
}
|
||||
// Check for the terminating bracket
|
||||
else if (data[index] == MailBnfHelper.StartSquareBracket)
|
||||
{
|
||||
// We're done parsing
|
||||
return index - 1;
|
||||
outIndex = index - 1;
|
||||
return true;
|
||||
}
|
||||
// Check for invalid characters
|
||||
else if (data[index] > MailBnfHelper.Ascii7bitMaxValue || !MailBnfHelper.Dtext[data[index]])
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Valid char
|
||||
else
|
||||
@@ -71,9 +91,17 @@ namespace System.Net.Mail
|
||||
}
|
||||
while (index >= 0);
|
||||
|
||||
// We didn't find a matching '[', throw.
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter,
|
||||
MailBnfHelper.EndSquareBracket));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
// We didn't find a matching '[', throw.
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter,
|
||||
MailBnfHelper.EndSquareBracket));
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,17 +29,18 @@ namespace System.Net.Mail
|
||||
// - -1 if the dot-atom section terminated at the start of the data string.
|
||||
// e.g. "[email protected]", starting at index 8 (e) returns -1.
|
||||
//
|
||||
// A FormatException will be thrown if:
|
||||
// Throws FormatException or returns false if:
|
||||
// - The initial character at data[index] is invalid in a dot-atom.
|
||||
// e.g. "[email protected]", starting at index 1 (@).
|
||||
// - The leading character is a dot.
|
||||
// e.g. "[email protected]", starting at index 7 (m), throws because the leading char (index 2) is a dot.
|
||||
//
|
||||
internal static int ReadReverse(string data, int index)
|
||||
internal static bool TryReadReverse(string data, int index, out int outIndex, bool throwExceptionIfFail)
|
||||
{
|
||||
Debug.Assert(0 <= index && index < data.Length, "index was outside the bounds of the string: " + index);
|
||||
|
||||
int startIndex = index;
|
||||
|
||||
// Scan for the first invalid chars (including whitespace)
|
||||
for (; 0 <= index; index--)
|
||||
{
|
||||
@@ -53,14 +54,32 @@ namespace System.Net.Mail
|
||||
// Check for empty/invalid dot-atom
|
||||
if (startIndex == index)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Check for leading dot
|
||||
else if (data[index + 1] == MailBnfHelper.Dot)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, MailBnfHelper.Dot));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, MailBnfHelper.Dot));
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
|
||||
outIndex = index;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,16 +2,18 @@
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Text;
|
||||
|
||||
namespace System.Net.Mail
|
||||
{
|
||||
internal class MailAddress
|
||||
{
|
||||
public MailAddress(string address)
|
||||
{
|
||||
MailAddressParser.ParseAddress(address);
|
||||
MailAddressParser.TryParseAddress(address, out ParseAddressInfo _, throwExceptionIfFail: true);
|
||||
}
|
||||
|
||||
internal MailAddress(string displayName, string localPart, string domain)
|
||||
internal MailAddress(string displayName, string localPart, string domain, Encoding displayNameEncoding)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Mime;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace System.Net.Mail
|
||||
{
|
||||
@@ -22,18 +22,18 @@ namespace System.Net.Mail
|
||||
{
|
||||
// Parse a single MailAddress from the given string.
|
||||
//
|
||||
// Throws a FormatException if any part of the MailAddress is invalid.
|
||||
internal static MailAddress ParseAddress(string data)
|
||||
// Throws a FormatException or returns false if any part of the MailAddress is invalid.
|
||||
internal static bool TryParseAddress(string data, out ParseAddressInfo parsedAddress, bool throwExceptionIfFail)
|
||||
{
|
||||
int index = data.Length - 1;
|
||||
MailAddress parsedAddress = MailAddressParser.ParseAddress(data, false, ref index);
|
||||
Debug.Assert(index == -1, "The index indicates that part of the address was not parsed: " + index);
|
||||
return parsedAddress;
|
||||
bool parseSuccess = TryParseAddress(data, false, ref index, out parsedAddress, throwExceptionIfFail);
|
||||
Debug.Assert(!parseSuccess || index == -1, "The index indicates that part of the address was not parsed: " + index);
|
||||
return parseSuccess;
|
||||
}
|
||||
|
||||
// Parse a comma separated list of MailAddress's
|
||||
//
|
||||
// Throws a FormatException if any MailAddress is invalid.
|
||||
// Throws a FormatException or false is returned if any MailAddress is invalid.
|
||||
internal static List<MailAddress> ParseMultipleAddresses(string data)
|
||||
{
|
||||
List<MailAddress> results = new List<MailAddress>();
|
||||
@@ -41,7 +41,8 @@ namespace System.Net.Mail
|
||||
while (index >= 0)
|
||||
{
|
||||
// Because we're parsing in reverse, we must make an effort to preserve the order of the addresses.
|
||||
results.Insert(0, MailAddressParser.ParseAddress(data, true, ref index));
|
||||
TryParseAddress(data, true, ref index, out ParseAddressInfo parsedAddress, throwExceptionIfFail: true);
|
||||
results.Insert(0, new MailAddress(parsedAddress.DisplayName, parsedAddress.User, parsedAddress.Host, null));
|
||||
Debug.Assert(index == -1 || data[index] == MailBnfHelper.Comma,
|
||||
"separator not found while parsing multiple addresses");
|
||||
index--;
|
||||
@@ -61,19 +62,21 @@ namespace System.Net.Mail
|
||||
// - For a single MailAddress index is set to -1
|
||||
// - For a list data[index] is the comma separator or -1 if the end of the data string was reached.
|
||||
//
|
||||
// Throws a FormatException if any part of the MailAddress is invalid.
|
||||
private static MailAddress ParseAddress(string data, bool expectMultipleAddresses, ref int index)
|
||||
// Throws a FormatException or false is returned if any part of the MailAddress is invalid.
|
||||
private static bool TryParseAddress(string data, bool expectMultipleAddresses, ref int index, out ParseAddressInfo parseAddressInfo, bool throwExceptionIfFail)
|
||||
{
|
||||
Debug.Assert(!string.IsNullOrEmpty(data));
|
||||
Debug.Assert(index >= 0 && index < data.Length, "Index out of range: " + index + ", " + data.Length);
|
||||
|
||||
// Parsed components to be assembled as a MailAddress later
|
||||
string domain = null;
|
||||
string localPart = null;
|
||||
string displayName = null;
|
||||
string displayName;
|
||||
|
||||
// Skip comments and whitespace
|
||||
index = ReadCfwsAndThrowIfIncomplete(data, index);
|
||||
if (!TryReadCfwsAndThrowIfIncomplete(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
parseAddressInfo = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Do we expect angle brackets around the address?
|
||||
// e.g. ("display name" <user@domain>)
|
||||
@@ -84,18 +87,34 @@ namespace System.Net.Mail
|
||||
index--;
|
||||
}
|
||||
|
||||
domain = ParseDomain(data, ref index);
|
||||
if (!TryParseDomain(data, ref index, out string domain, throwExceptionIfFail))
|
||||
{
|
||||
parseAddressInfo = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
// The next character after the domain must be the '@' symbol
|
||||
if (data[index] != MailBnfHelper.At)
|
||||
{
|
||||
throw new FormatException(SR.MailAddressInvalidFormat);
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.MailAddressInvalidFormat);
|
||||
}
|
||||
else
|
||||
{
|
||||
parseAddressInfo = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Skip the '@' symbol
|
||||
index--;
|
||||
|
||||
localPart = ParseLocalPart(data, ref index, expectAngleBracket, expectMultipleAddresses);
|
||||
if (!TryParseLocalPart(data, ref index, expectAngleBracket, expectMultipleAddresses, out string localPart, throwExceptionIfFail))
|
||||
{
|
||||
parseAddressInfo = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for a matching angle bracket around the address
|
||||
if (expectAngleBracket)
|
||||
@@ -104,13 +123,25 @@ namespace System.Net.Mail
|
||||
{
|
||||
index--; // Skip the angle bracket
|
||||
// Skip whitespace, but leave comments, as they may be part of the display name.
|
||||
index = WhitespaceReader.ReadFwsReverse(data, index);
|
||||
if (!WhitespaceReader.TryReadFwsReverse(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
parseAddressInfo = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Mismatched angle brackets, throw
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter,
|
||||
(index >= 0 ? data[index] : MailBnfHelper.EndAngleBracket)));
|
||||
// Mismatched angle brackets
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter,
|
||||
(index >= 0 ? data[index] : MailBnfHelper.EndAngleBracket)));
|
||||
}
|
||||
else
|
||||
{
|
||||
parseAddressInfo = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,27 +149,47 @@ namespace System.Net.Mail
|
||||
// There could still be a display name or another address
|
||||
if (index >= 0 && !(expectMultipleAddresses && data[index] == MailBnfHelper.Comma))
|
||||
{
|
||||
displayName = ParseDisplayName(data, ref index, expectMultipleAddresses);
|
||||
if (!TryParseDisplayName(data, ref index, expectMultipleAddresses, out displayName, throwExceptionIfFail))
|
||||
{
|
||||
parseAddressInfo = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
displayName = string.Empty;
|
||||
}
|
||||
|
||||
return new MailAddress(displayName, localPart, domain);
|
||||
parseAddressInfo = new ParseAddressInfo(displayName, localPart, domain);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Read through a section of CFWS. If we reach the end of the data string then throw because not enough of the
|
||||
// MailAddress components were found.
|
||||
private static int ReadCfwsAndThrowIfIncomplete(string data, int index)
|
||||
private static bool TryReadCfwsAndThrowIfIncomplete(string data, int index, out int outIndex, bool throwExceptionIfFail)
|
||||
{
|
||||
index = WhitespaceReader.ReadCfwsReverse(data, index);
|
||||
if (!WhitespaceReader.TryReadCfwsReverse(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
// More components were expected. Incomplete address, invalid
|
||||
throw new FormatException(SR.MailAddressInvalidFormat);
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.MailAddressInvalidFormat);
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
|
||||
outIndex = index;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parses the domain section of an address. The domain may be in dot-atom format or surrounded by square
|
||||
@@ -152,13 +203,17 @@ namespace System.Net.Mail
|
||||
// - data[index] should refer to the '@' symbol
|
||||
// - returns the parsed domain, including any square brackets for domain-literals
|
||||
//
|
||||
// Throws a FormatException:
|
||||
// Throws a FormatException or returns false:
|
||||
// - For invalid un-escaped chars, including Unicode
|
||||
// - If the start of the data string is reached
|
||||
private static string ParseDomain(string data, ref int index)
|
||||
private static bool TryParseDomain(string data, ref int index, out string domain, bool throwExceptionIfFail)
|
||||
{
|
||||
// Skip comments and whitespace
|
||||
index = ReadCfwsAndThrowIfIncomplete(data, index);
|
||||
if (!TryReadCfwsAndThrowIfIncomplete(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
domain = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Mark one end of the domain component
|
||||
int startingIndex = index;
|
||||
@@ -166,19 +221,35 @@ namespace System.Net.Mail
|
||||
// Is the domain component in domain-literal format or dot-atom format?
|
||||
if (data[index] == MailBnfHelper.EndSquareBracket)
|
||||
{
|
||||
index = DomainLiteralReader.ReadReverse(data, index);
|
||||
if (!DomainLiteralReader.TryReadReverse(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
domain = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
index = DotAtomReader.ReadReverse(data, index);
|
||||
if (!DotAtomReader.TryReadReverse(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
domain = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
string domain = data.Substring(index + 1, startingIndex - index);
|
||||
domain = data.Substring(index + 1, startingIndex - index);
|
||||
|
||||
// Skip comments and whitespace
|
||||
index = ReadCfwsAndThrowIfIncomplete(data, index);
|
||||
if (!TryReadCfwsAndThrowIfIncomplete(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return NormalizeOrThrow(domain);
|
||||
if (!TryNormalizeOrThrow(domain, out domain, throwExceptionIfFail))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parses the local-part section of an address. The local-part may be in dot-atom format or
|
||||
@@ -193,14 +264,18 @@ namespace System.Net.Mail
|
||||
// - index == -1 if the beginning of the data string has been reached.
|
||||
// - returns the parsed local-part, including any bounding quotes around quoted-strings
|
||||
//
|
||||
// Throws a FormatException:
|
||||
// Throws a FormatException or false is returned:
|
||||
// - For invalid un-escaped chars, including Unicode
|
||||
// - If the final value of data[index] is not a valid character to precede the local-part
|
||||
private static string ParseLocalPart(string data, ref int index, bool expectAngleBracket,
|
||||
bool expectMultipleAddresses)
|
||||
private static bool TryParseLocalPart(string data, ref int index, bool expectAngleBracket,
|
||||
bool expectMultipleAddresses, out string localPart, bool throwExceptionIfFail)
|
||||
{
|
||||
// Skip comments and whitespace
|
||||
index = ReadCfwsAndThrowIfIncomplete(data, index);
|
||||
if (!TryReadCfwsAndThrowIfIncomplete(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
localPart = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Mark the start of the local-part
|
||||
int startingIndex = index;
|
||||
@@ -208,11 +283,19 @@ namespace System.Net.Mail
|
||||
// Is the local-part component in quoted-string format or dot-atom format?
|
||||
if (data[index] == MailBnfHelper.Quote)
|
||||
{
|
||||
index = QuotedStringFormatReader.ReadReverseQuoted(data, index, true);
|
||||
if (!QuotedStringFormatReader.TryReadReverseQuoted(data, index, true, out index, throwExceptionIfFail))
|
||||
{
|
||||
localPart = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
index = DotAtomReader.ReadReverse(data, index);
|
||||
if (!DotAtomReader.TryReadReverse(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
localPart = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check that the local-part is properly separated from the next component. It may be separated by a
|
||||
// comment, whitespace, an expected angle bracket, a quote for the display-name, or an expected comma
|
||||
@@ -229,15 +312,31 @@ namespace System.Net.Mail
|
||||
)
|
||||
)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
}
|
||||
else
|
||||
{
|
||||
localPart = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string localPart = data.Substring(index + 1, startingIndex - index);
|
||||
localPart = data.Substring(index + 1, startingIndex - index);
|
||||
|
||||
index = WhitespaceReader.ReadCfwsReverse(data, index);
|
||||
if (!WhitespaceReader.TryReadCfwsReverse(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return NormalizeOrThrow(localPart);
|
||||
if (!TryNormalizeOrThrow(localPart, out localPart, throwExceptionIfFail))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parses the display-name section of an address. In departure from the RFC, we attempt to read data in the
|
||||
@@ -252,26 +351,32 @@ namespace System.Net.Mail
|
||||
// - index == -1 if the beginning of the data string has been reached.
|
||||
// - returns the parsed display-name, excluding any bounding quotes around quoted-strings
|
||||
//
|
||||
// Throws a FormatException:
|
||||
// Throws a FormatException or false is returned:
|
||||
// - For invalid un-escaped chars, except Unicode
|
||||
// - If the postconditions cannot be met.
|
||||
private static string ParseDisplayName(string data, ref int index, bool expectMultipleAddresses)
|
||||
private static bool TryParseDisplayName(string data, ref int index, bool expectMultipleAddresses, out string displayName, bool throwExceptionIfFail)
|
||||
{
|
||||
string displayName;
|
||||
|
||||
// Whatever is left over must be the display name. The display name should be a single word/atom or a
|
||||
// quoted string, but for robustness we allow the quotes to be omitted, so long as we can find the comma
|
||||
// separator before the next address.
|
||||
|
||||
// Read the comment (if any). If the display name is contained in quotes, the surrounding comments are
|
||||
// omitted. Otherwise, mark this end of the comment so we can include it as part of the display name.
|
||||
int firstNonCommentIndex = WhitespaceReader.ReadCfwsReverse(data, index);
|
||||
if (!WhitespaceReader.TryReadCfwsReverse(data, index, out int firstNonCommentIndex, throwExceptionIfFail))
|
||||
{
|
||||
displayName = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check to see if there's a quoted-string display name
|
||||
if (firstNonCommentIndex >= 0 && data[firstNonCommentIndex] == MailBnfHelper.Quote)
|
||||
{
|
||||
// The preceding comment was not part of the display name. Read just the quoted string.
|
||||
index = QuotedStringFormatReader.ReadReverseQuoted(data, firstNonCommentIndex, true);
|
||||
if (!QuotedStringFormatReader.TryReadReverseQuoted(data, firstNonCommentIndex, true, out index, throwExceptionIfFail))
|
||||
{
|
||||
displayName = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
Debug.Assert(data[index + 1] == MailBnfHelper.Quote, "Mis-aligned index: " + index);
|
||||
|
||||
@@ -280,14 +385,24 @@ namespace System.Net.Mail
|
||||
displayName = data.Substring(leftIndex, firstNonCommentIndex - leftIndex);
|
||||
|
||||
// Skip any CFWS after the display name
|
||||
index = WhitespaceReader.ReadCfwsReverse(data, index);
|
||||
if (!WhitespaceReader.TryReadCfwsReverse(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for completion. We are valid if we hit the end of the data string or if the rest of the data
|
||||
// belongs to another address.
|
||||
if (index >= 0 && !(expectMultipleAddresses && data[index] == MailBnfHelper.Comma))
|
||||
{
|
||||
// If there was still data, only a comma could have been the next valid character
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -296,7 +411,11 @@ namespace System.Net.Mail
|
||||
int startingIndex = index;
|
||||
|
||||
// Read until the dividing comma or the end of the line.
|
||||
index = QuotedStringFormatReader.ReadReverseUnQuoted(data, index, true, expectMultipleAddresses);
|
||||
if (!QuotedStringFormatReader.TryReadReverseUnQuoted(data, index, true, expectMultipleAddresses, out index, throwExceptionIfFail))
|
||||
{
|
||||
displayName = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
Debug.Assert(index < 0 || data[index] == MailBnfHelper.Comma, "Mis-aligned index: " + index);
|
||||
|
||||
@@ -304,19 +423,42 @@ namespace System.Net.Mail
|
||||
// trim extra whitespace.
|
||||
displayName = data.SubstringTrim(index + 1, startingIndex - index);
|
||||
}
|
||||
return NormalizeOrThrow(displayName);
|
||||
|
||||
if (!TryNormalizeOrThrow(displayName, out displayName, throwExceptionIfFail))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
internal static string NormalizeOrThrow(string input)
|
||||
internal static bool TryNormalizeOrThrow(string input, out string normalizedString, bool throwExceptionIfFail)
|
||||
{
|
||||
try
|
||||
{
|
||||
return input.Normalize(Text.NormalizationForm.FormC);
|
||||
normalizedString = input.Normalize(Text.NormalizationForm.FormC);
|
||||
return true;
|
||||
}
|
||||
catch (ArgumentException e)
|
||||
{
|
||||
throw new FormatException(SR.MailAddressInvalidFormat, e);
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.MailAddressInvalidFormat, e);
|
||||
}
|
||||
else
|
||||
{
|
||||
normalizedString = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal readonly struct ParseAddressInfo
|
||||
{
|
||||
public readonly string DisplayName { get; }
|
||||
public readonly string User { get; }
|
||||
public readonly string Host { get; }
|
||||
public ParseAddressInfo(string displayName, string userName, string domain) => (DisplayName, User, Host) = (displayName, userName, domain);
|
||||
}
|
||||
}
|
||||
@@ -31,14 +31,15 @@ namespace System.Net.Mail
|
||||
// - The number of consecutive quoted chars, including multiple preceding quoted backslashes
|
||||
// e.g. (a\\\b) given index=4 returns 4, as 'b' is quoted, and so are the previous backslashes
|
||||
//
|
||||
// Throws a FormatException if the an escaped Unicode character is found but was not permitted.
|
||||
internal static int CountQuotedChars(string data, int index, bool permitUnicodeEscaping)
|
||||
// Throws a FormatException or false is returned if the an escaped Unicode character is found but was not permitted.
|
||||
internal static bool TryCountQuotedChars(string data, int index, bool permitUnicodeEscaping, out int outIndex, bool throwExceptionIfFail)
|
||||
{
|
||||
Debug.Assert(0 <= index && index < data.Length, "Index out of range: " + index + ", " + data.Length);
|
||||
|
||||
if (index <= 0 || data[index - 1] != MailBnfHelper.Backslash)
|
||||
{
|
||||
return 0;
|
||||
outIndex = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Count the preceding backslashes
|
||||
@@ -47,17 +48,27 @@ namespace System.Net.Mail
|
||||
// For an even number of backslashes, the original character was NOT escaped/quoted
|
||||
if (backslashCount % 2 == 0)
|
||||
{
|
||||
return 0; // No quoted pair to skip
|
||||
outIndex = 0; // No quoted pair to skip
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!permitUnicodeEscaping && data[index] > MailBnfHelper.Ascii7bitMaxValue)
|
||||
{
|
||||
// Cannot accept quoted Unicode
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
// Cannot accept quoted Unicode
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Skip the quoted char, and the odd number of backslashes preceding it
|
||||
return backslashCount + 1;
|
||||
outIndex = backslashCount + 1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,12 +28,12 @@ namespace System.Net.Mail
|
||||
// Return value: The next index past the terminating-quote (data[index + 1] == Quote).
|
||||
// e.g. In (bob "user name"@domain), starting at index=14 (") returns index=3 (space).
|
||||
//
|
||||
// A FormatException will be thrown if:
|
||||
// A FormatException will be thrown or false is returned if:
|
||||
// - A non-escaped character is encountered that is not valid in a quoted string.
|
||||
// - A Unicode character is encountered and Unicode has not been allowed.
|
||||
// - The final double quote is not found.
|
||||
//
|
||||
internal static int ReadReverseQuoted(string data, int index, bool permitUnicode)
|
||||
internal static bool TryReadReverseQuoted(string data, int index, bool permitUnicode, out int outIndex, bool throwExceptionIfFail)
|
||||
{
|
||||
Debug.Assert(0 <= index && index < data.Length, "Index out of range: " + index + ", " + data.Length);
|
||||
// Check for the first bounding quote
|
||||
@@ -45,29 +45,48 @@ namespace System.Net.Mail
|
||||
do
|
||||
{
|
||||
// Check for valid whitespace
|
||||
index = WhitespaceReader.ReadFwsReverse(data, index);
|
||||
if (!WhitespaceReader.TryReadFwsReverse(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Check for escaped characters
|
||||
int quotedCharCount = QuotedPairReader.CountQuotedChars(data, index, permitUnicode);
|
||||
if (!QuotedPairReader.TryCountQuotedChars(data, index, permitUnicode, out int quotedCharCount, throwExceptionIfFail))
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (quotedCharCount > 0)
|
||||
{
|
||||
// Skip quoted pairs
|
||||
index = index - quotedCharCount;
|
||||
index -= quotedCharCount;
|
||||
}
|
||||
// Check for the terminating quote
|
||||
else if (data[index] == MailBnfHelper.Quote)
|
||||
{
|
||||
// Skip the final bounding quote
|
||||
return index - 1;
|
||||
outIndex = index - 1;
|
||||
return true;
|
||||
}
|
||||
// Check invalid characters
|
||||
else if (!IsValidQtext(permitUnicode, data[index]))
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Valid char
|
||||
else
|
||||
@@ -77,8 +96,16 @@ namespace System.Net.Mail
|
||||
}
|
||||
while (index >= 0);
|
||||
|
||||
// We started with a quote, but did not end with one
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, MailBnfHelper.Quote));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
// We started with a quote, but did not end with one
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, MailBnfHelper.Quote));
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
@@ -93,27 +120,37 @@ namespace System.Net.Mail
|
||||
// - -1 if the terminating character was not found.
|
||||
// e.g. In (my name username@domain), starting at index=5 (e) returns index=-1.
|
||||
//
|
||||
// A FormatException will be thrown if:
|
||||
// A FormatException will be thrown or false is returned if:
|
||||
// - A non-escaped character is encountered that is not valid in a quoted string. This includes double quotes.
|
||||
// - A Unicode character is encountered and Unicode has not been allowed.
|
||||
//
|
||||
internal static int ReadReverseUnQuoted(string data, int index, bool permitUnicode, bool expectCommaDelimiter)
|
||||
internal static bool TryReadReverseUnQuoted(string data, int index, bool permitUnicode, bool expectCommaDelimiter, out int outIndex, bool throwExceptionIfFail)
|
||||
{
|
||||
Debug.Assert(0 <= index && index < data.Length, "Index out of range: " + index + ", " + data.Length);
|
||||
|
||||
do
|
||||
{
|
||||
// Check for valid whitespace
|
||||
index = WhitespaceReader.ReadFwsReverse(data, index);
|
||||
if (!WhitespaceReader.TryReadFwsReverse(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
// Check for escaped characters
|
||||
int quotedCharCount = QuotedPairReader.CountQuotedChars(data, index, permitUnicode);
|
||||
if (!QuotedPairReader.TryCountQuotedChars(data, index, permitUnicode, out int quotedCharCount, throwExceptionIfFail))
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (quotedCharCount > 0)
|
||||
{
|
||||
index = index - quotedCharCount;
|
||||
index -= quotedCharCount;
|
||||
}
|
||||
// Check for the terminating char
|
||||
else if (expectCommaDelimiter && data[index] == MailBnfHelper.Comma)
|
||||
@@ -123,7 +160,15 @@ namespace System.Net.Mail
|
||||
// Check invalid characters
|
||||
else if (!IsValidQtext(permitUnicode, data[index]))
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Valid char
|
||||
else
|
||||
@@ -133,7 +178,8 @@ namespace System.Net.Mail
|
||||
}
|
||||
while (index >= 0);
|
||||
|
||||
return index;
|
||||
outIndex = index;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Checks for Unicode characters and characters not allowed in quoted-strings. A quoted string may contain
|
||||
|
||||
@@ -28,11 +28,12 @@ namespace System.Net.Mail
|
||||
// - The index of the next character that is NOT a whitespace character.
|
||||
// - -1 if the beginning of the data string is reached.
|
||||
//
|
||||
// A FormatException will be thrown if a CR or LF is found NOT in the sequence CRLF.
|
||||
internal static int ReadFwsReverse(string data, int index)
|
||||
// A FormatException will be thrown or false is returned if a CR or LF is found NOT in the sequence CRLF.
|
||||
internal static bool TryReadFwsReverse(string data, int index, out int outIndex, bool throwExceptionIfFail)
|
||||
{
|
||||
Debug.Assert(!string.IsNullOrEmpty(data), "data was null or empty");
|
||||
Debug.Assert(index < data.Length, "index was outside the bounds of the string");
|
||||
|
||||
bool expectCR = false;
|
||||
|
||||
for (; index >= 0; index--)
|
||||
@@ -45,7 +46,15 @@ namespace System.Net.Mail
|
||||
// LF without CR, or CR without LF, invalid
|
||||
else if (data[index] == MailBnfHelper.CR || expectCR)
|
||||
{
|
||||
throw new FormatException(SR.MailAddressInvalidFormat);
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.MailAddressInvalidFormat);
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// LF is only valid if preceded by a CR.
|
||||
// Skip both if they're found together.
|
||||
@@ -67,10 +76,20 @@ namespace System.Net.Mail
|
||||
|
||||
if (expectCR)
|
||||
{
|
||||
// We found a LF without an immediately preceding CR, invalid.
|
||||
throw new FormatException(SR.MailAddressInvalidFormat);
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
// We found a LF without an immediately preceding CR, invalid.
|
||||
throw new FormatException(SR.MailAddressInvalidFormat);
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
|
||||
outIndex = index;
|
||||
return true;
|
||||
}
|
||||
|
||||
// This method functions similarly to ReadFwsReverse but will also skip any comments.
|
||||
@@ -92,8 +111,8 @@ namespace System.Net.Mail
|
||||
// - -1 if skipping the comments and/or whitespace moves you to the beginning of the data string.
|
||||
// e.g. " (comment) " returns -1
|
||||
//
|
||||
// Throws a FormatException for mismatched '(' and ')', or for unescaped characters not allowed in comments.
|
||||
internal static int ReadCfwsReverse(string data, int index)
|
||||
// Throws a FormatException or false is returned for mismatched '(' and ')', or for unescaped characters not allowed in comments.
|
||||
internal static bool TryReadCfwsReverse(string data, int index, out int outIndex, bool throwExceptionIfFail)
|
||||
{
|
||||
Debug.Assert(!string.IsNullOrEmpty(data), "data was null or empty");
|
||||
Debug.Assert(index < data.Length, "index was outside the bounds of the string");
|
||||
@@ -101,15 +120,24 @@ namespace System.Net.Mail
|
||||
int commentDepth = 0;
|
||||
|
||||
// Check for valid whitespace
|
||||
index = ReadFwsReverse(data, index);
|
||||
if (!TryReadFwsReverse(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
while (index >= 0)
|
||||
{
|
||||
// Check for escaped characters. They must be within comments.
|
||||
int quotedCharCount = QuotedPairReader.CountQuotedChars(data, index, true);
|
||||
if (!QuotedPairReader.TryCountQuotedChars(data, index, true, out int quotedCharCount, throwExceptionIfFail))
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (commentDepth > 0 && quotedCharCount > 0)
|
||||
{
|
||||
index = index - quotedCharCount;
|
||||
index -= quotedCharCount;
|
||||
}
|
||||
// Start a new comment
|
||||
else if (data[index] == MailBnfHelper.EndComment)
|
||||
@@ -123,9 +151,17 @@ namespace System.Net.Mail
|
||||
commentDepth--;
|
||||
if (commentDepth < 0)
|
||||
{
|
||||
// Mismatched '('
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter,
|
||||
MailBnfHelper.StartComment));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
// Mismatched '('
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter,
|
||||
MailBnfHelper.StartComment));
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
index--;
|
||||
}
|
||||
@@ -138,7 +174,15 @@ namespace System.Net.Mail
|
||||
// If we're still in a comment, this must be an invalid char
|
||||
else if (commentDepth > 0)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// We must no longer be in a comment, and this is not a whitespace char, return
|
||||
else
|
||||
@@ -147,16 +191,29 @@ namespace System.Net.Mail
|
||||
}
|
||||
|
||||
// Check for valid whitespace
|
||||
index = ReadFwsReverse(data, index);
|
||||
if (!TryReadFwsReverse(data, index, out index, throwExceptionIfFail))
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (commentDepth > 0)
|
||||
{
|
||||
// Mismatched ')', throw
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, MailBnfHelper.EndComment));
|
||||
if (throwExceptionIfFail)
|
||||
{
|
||||
// Mismatched ')', throw
|
||||
throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, MailBnfHelper.EndComment));
|
||||
}
|
||||
else
|
||||
{
|
||||
outIndex = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
outIndex = index;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -223,7 +223,7 @@
|
||||
<value>The 'q' value is invalid: '{0}'.</value>
|
||||
</data>
|
||||
<data name="net_http_log_headers_wrong_email_format" xml:space="preserve">
|
||||
<value>Value '{0}' is not a valid email address. Error: {1}</value>
|
||||
<value>Value '{0}' is not a valid email address</value>
|
||||
</data>
|
||||
<data name="net_http_handler_not_assigned" xml:space="preserve">
|
||||
<value>The inner handler has not been assigned.</value>
|
||||
|
||||
@@ -378,16 +378,15 @@ namespace System.Net.Http.Headers
|
||||
|
||||
internal static bool IsValidEmailAddress(string value)
|
||||
{
|
||||
try
|
||||
if (MailAddressParser.TryParseAddress(value, out ParseAddressInfo _, throwExceptionIfFail: false))
|
||||
{
|
||||
MailAddressParser.ParseAddress(value);
|
||||
return true;
|
||||
}
|
||||
catch (FormatException e)
|
||||
else
|
||||
{
|
||||
if (NetEventSource.IsEnabled) NetEventSource.Error(null, SR.Format(SR.net_http_log_headers_wrong_email_format, value, e.Message));
|
||||
if (NetEventSource.IsEnabled) NetEventSource.Error(null, SR.Format(SR.net_http_log_headers_wrong_email_format, value));
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void ValidateToken(HttpHeaderValueCollection<string> collection, string value)
|
||||
|
||||
@@ -112,6 +112,9 @@ namespace System.Net.Mail
|
||||
public string User { get { throw null; } }
|
||||
public override bool Equals(object value) { throw null; }
|
||||
public override int GetHashCode() { throw null; }
|
||||
public static bool TryCreate(string address, out MailAddress result) { throw null; }
|
||||
public static bool TryCreate(string address, string displayName, out MailAddress result) { throw null; }
|
||||
public static bool TryCreate(string address, string displayName, System.Text.Encoding displayNameEncoding, out MailAddress result) { throw null; }
|
||||
public override string ToString() { throw null; }
|
||||
}
|
||||
public partial class MailAddressCollection : System.Collections.ObjectModel.Collection<System.Net.Mail.MailAddress>
|
||||
|
||||
@@ -24,12 +24,12 @@ namespace System.Net.Mail
|
||||
|
||||
// For internal use only by MailAddressParser.
|
||||
// The components were already validated before this is called.
|
||||
internal MailAddress(string displayName, string userName, string domain)
|
||||
internal MailAddress(string displayName, string userName, string domain, Encoding displayNameEncoding)
|
||||
{
|
||||
_host = domain;
|
||||
_userName = userName;
|
||||
_displayName = displayName;
|
||||
_displayNameEncoding = Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
|
||||
_displayNameEncoding = displayNameEncoding ?? Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
|
||||
|
||||
Debug.Assert(_host != null,
|
||||
"host was null in internal constructor");
|
||||
@@ -63,6 +63,61 @@ namespace System.Net.Mail
|
||||
//
|
||||
// A FormatException will be thrown if any of the components in 'address' are invalid.
|
||||
public MailAddress(string address, string displayName, Encoding displayNameEncoding)
|
||||
{
|
||||
bool parseSuccess = TryParse(address, displayName, displayNameEncoding,
|
||||
out (string displayName, string user, string host, Encoding displayNameEncoding) parsedData,
|
||||
throwExceptionIfFail: true);
|
||||
|
||||
_displayName = parsedData.displayName;
|
||||
_userName = parsedData.user;
|
||||
_host = parsedData.host;
|
||||
_displayNameEncoding = parsedData.displayNameEncoding;
|
||||
|
||||
Debug.Assert(parseSuccess);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="MailAddress"/>. Does not throw an exception if the MailAddress cannot be created.
|
||||
/// </summary>
|
||||
/// <param name="address">A <see cref="string"/> that contains an email address.</param>
|
||||
/// <param name="result">When this method returns, contains the <see cref="MailAddress"/> instance if address parsing succeed</param>
|
||||
/// <returns>A <see cref="bool"/> value that is true if the <see cref="MailAddress"/> was successfully created; otherwise, false.</returns>
|
||||
public static bool TryCreate(string address, out MailAddress result) => TryCreate(address, displayName: null, out result);
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="MailAddress"/>. Does not throw an exception if the MailAddress cannot be created.
|
||||
/// </summary>
|
||||
/// <param name="address">A <see cref="string"/> that contains an email address.</param>
|
||||
/// <param name="displayName">A <see cref="string"/> that contains the display name associated with address. This parameter can be null.</param>
|
||||
/// <param name="result">When this method returns, contains the <see cref="MailAddress"/> instance if address parsing succeed</param>
|
||||
/// <returns>A <see cref="bool"/> value that is true if the <see cref="MailAddress"/> was successfully created; otherwise, false.</returns>
|
||||
public static bool TryCreate(string address, string displayName, out MailAddress result) => TryCreate(address, displayName, displayNameEncoding: null, out result);
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="MailAddress"/>. Does not throw an exception if the MailAddress cannot be created.
|
||||
/// </summary>
|
||||
/// <param name="address">A <see cref="string"/> that contains an email address.</param>
|
||||
/// <param name="displayName">A <see cref="string"/> that contains the display name associated with address. This parameter can be null.</param>
|
||||
/// <param name="displayNameEncoding">The <see cref="Encoding"/> that defines the character set used for displayName</param>
|
||||
/// <param name="result">When this method returns, contains the <see cref="MailAddress"/> instance if address parsing succeed</param>
|
||||
/// <returns>A <see cref="bool"/> value that is true if the <see cref="MailAddress"/> was successfully created; otherwise, false.</returns>
|
||||
public static bool TryCreate(string address, string displayName, Encoding displayNameEncoding, out MailAddress result)
|
||||
{
|
||||
if (TryParse(address, displayName, displayNameEncoding,
|
||||
out (string displayName, string user, string host, Encoding displayNameEncoding) parsed,
|
||||
throwExceptionIfFail: false))
|
||||
{
|
||||
result = new MailAddress(parsed.displayName, parsed.user, parsed.host, parsed.displayNameEncoding);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryParse(string address, string displayName, Encoding displayNameEncoding, out (string displayName, string user, string host, Encoding displayNameEncoding) parsedData, bool throwExceptionIfFail)
|
||||
{
|
||||
if (address == null)
|
||||
{
|
||||
@@ -73,32 +128,40 @@ namespace System.Net.Mail
|
||||
throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(address)), nameof(address));
|
||||
}
|
||||
|
||||
_displayNameEncoding = displayNameEncoding ?? Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
|
||||
_displayName = displayName ?? string.Empty;
|
||||
displayNameEncoding ??= Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
|
||||
displayName ??= string.Empty;
|
||||
|
||||
// Check for bounding quotes
|
||||
if (!string.IsNullOrEmpty(_displayName))
|
||||
if (!string.IsNullOrEmpty(displayName))
|
||||
{
|
||||
_displayName = MailAddressParser.NormalizeOrThrow(_displayName);
|
||||
if (!MailAddressParser.TryNormalizeOrThrow(displayName, out displayName, throwExceptionIfFail))
|
||||
{
|
||||
parsedData = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_displayName.Length >= 2 && _displayName[0] == '\"'
|
||||
&& _displayName[_displayName.Length - 1] == '\"')
|
||||
if (displayName.Length >= 2 && displayName[0] == '\"' && displayName[^1] == '\"')
|
||||
{
|
||||
// Peal bounding quotes, they'll get re-added later.
|
||||
_displayName = _displayName.Substring(1, _displayName.Length - 2);
|
||||
displayName = displayName.Substring(1, displayName.Length - 2);
|
||||
}
|
||||
}
|
||||
|
||||
MailAddress result = MailAddressParser.ParseAddress(address);
|
||||
|
||||
_host = result._host;
|
||||
_userName = result._userName;
|
||||
if (!MailAddressParser.TryParseAddress(address, out ParseAddressInfo info, throwExceptionIfFail))
|
||||
{
|
||||
parsedData = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
// If we were not given a display name, use the one parsed from 'address'.
|
||||
if (string.IsNullOrEmpty(_displayName))
|
||||
if (string.IsNullOrEmpty(displayName))
|
||||
{
|
||||
_displayName = result._displayName;
|
||||
displayName = info.DisplayName;
|
||||
}
|
||||
|
||||
parsedData = (displayName, info.User, info.Host, displayNameEncoding);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public string DisplayName
|
||||
|
||||
@@ -1,218 +1,181 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
//
|
||||
// MailAddressTest.cs - NUnit Test Cases for System.Net.MailAddress.MailAddress
|
||||
//
|
||||
// Authors:
|
||||
// John Luke ([email protected])
|
||||
//
|
||||
// (C) 2005 John Luke
|
||||
//
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Mail.Tests
|
||||
{
|
||||
public class MailAddressTest
|
||||
{
|
||||
MailAddress address;
|
||||
|
||||
public MailAddressTest()
|
||||
public static IEnumerable<object[]> GetValid_Address()
|
||||
{
|
||||
address = new MailAddress("[email protected]", "Mr. Foo Bar");
|
||||
// inputAddress, address, displayName, host, toString(), user
|
||||
yield return new object[] { " [email protected] ", "[email protected]", string.Empty, "example.com", "[email protected]", "foo" };
|
||||
yield return new object[] { "Mr. Foo Bar <[email protected]>", "[email protected]", "Mr. Foo Bar", "example.com", "\"Mr. Foo Bar\" <[email protected]>", "foo" };
|
||||
yield return new object[] { "FooBar <[email protected]>", "[email protected]", "FooBar", "example.com", "\"FooBar\" <[email protected]>", "foo" };
|
||||
yield return new object[] { "\"FooBar\"[email protected] ", "[email protected]", "FooBar", "example.com", "\"FooBar\" <[email protected]>", "foo" };
|
||||
yield return new object[] { "\" FooBar \"< [email protected] >", "[email protected]", " FooBar ", "example.com", "\" FooBar \" <[email protected]>", "foo" };
|
||||
yield return new object[] { "<[email protected]>", "[email protected]", string.Empty, "example.com", "[email protected]", "foo" };
|
||||
yield return new object[] { " < [email protected] >", "[email protected]", string.Empty, "example.com", "[email protected]", "foo" };
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestConstructorOverload1()
|
||||
[Theory]
|
||||
[MemberData(nameof(GetValid_Address))]
|
||||
public void TestConstructor_Address(string inputAddress, string address, string displayName, string host, string toString, string user)
|
||||
{
|
||||
address = new MailAddress(" [email protected] ");
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
Assert.Equal(string.Empty, address.DisplayName);
|
||||
Assert.Equal("example.com", address.Host);
|
||||
Assert.Equal("[email protected]", address.ToString());
|
||||
Assert.Equal("foo", address.User);
|
||||
|
||||
address = new MailAddress("Mr. Foo Bar <[email protected]>");
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
Assert.Equal("Mr. Foo Bar", address.DisplayName);
|
||||
Assert.Equal("example.com", address.Host);
|
||||
Assert.Equal("\"Mr. Foo Bar\" <[email protected]>", address.ToString());
|
||||
Assert.Equal("foo", address.User);
|
||||
|
||||
address = new MailAddress("FooBar <[email protected]>");
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
Assert.Equal("FooBar", address.DisplayName);
|
||||
Assert.Equal("example.com", address.Host);
|
||||
Assert.Equal("\"FooBar\" <[email protected]>", address.ToString());
|
||||
Assert.Equal("foo", address.User);
|
||||
|
||||
address = new MailAddress("\"FooBar\"[email protected] ");
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
Assert.Equal("FooBar", address.DisplayName);
|
||||
Assert.Equal("example.com", address.Host);
|
||||
Assert.Equal("\"FooBar\" <[email protected]>", address.ToString());
|
||||
Assert.Equal("foo", address.User);
|
||||
|
||||
address = new MailAddress("\" FooBar \"< [email protected] >");
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
Assert.Equal(" FooBar ", address.DisplayName);
|
||||
Assert.Equal("example.com", address.Host);
|
||||
Assert.Equal("\" FooBar \" <[email protected]>", address.ToString());
|
||||
Assert.Equal("foo", address.User);
|
||||
|
||||
address = new MailAddress("<[email protected]>");
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
Assert.Equal(string.Empty, address.DisplayName);
|
||||
Assert.Equal("example.com", address.Host);
|
||||
Assert.Equal("[email protected]", address.ToString());
|
||||
Assert.Equal("foo", address.User);
|
||||
|
||||
address = new MailAddress(" < [email protected] >");
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
Assert.Equal(string.Empty, address.DisplayName);
|
||||
Assert.Equal("example.com", address.Host);
|
||||
Assert.Equal("[email protected]", address.ToString());
|
||||
Assert.Equal("foo", address.User);
|
||||
MailAddress addressInstance = new MailAddress(inputAddress);
|
||||
Assert.Equal(address, addressInstance.Address);
|
||||
Assert.Equal(displayName, addressInstance.DisplayName);
|
||||
Assert.Equal(host, addressInstance.Host);
|
||||
Assert.Equal(toString, addressInstance.ToString());
|
||||
Assert.Equal(user, addressInstance.User);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestConstructorWithNullString()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => new MailAddress(null));
|
||||
Assert.Throws<ArgumentNullException>(() => _ = MailAddress.TryCreate(null, out MailAddress _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestConstructorWithEmptyString()
|
||||
{
|
||||
AssertExtensions.Throws<ArgumentException>("address", () => new MailAddress(""));
|
||||
AssertExtensions.Throws<ArgumentException>("address", () => _ = MailAddress.TryCreate("", out MailAddress _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestInvalidAddressInConstructor()
|
||||
public static IEnumerable<object[]> GetInvalid_Address()
|
||||
{
|
||||
Assert.Throws<FormatException>(() => new MailAddress("Mr. Foo Bar"));
|
||||
Assert.Throws<FormatException>(() => new MailAddress("foo@b@ar"));
|
||||
Assert.Throws<FormatException>(() => new MailAddress("Mr. Foo Bar <foo@exa<mple.com"));
|
||||
Assert.Throws<FormatException>(() => new MailAddress("Mr. Foo Bar <[email protected]"));
|
||||
Assert.Throws<FormatException>(() => new MailAddress("Mr. \"F@@ Bar\" <[email protected]> Whatever@You@Want"));
|
||||
Assert.Throws<FormatException>(() => new MailAddress("Mr. F@@ Bar <[email protected]> What\"ever@You@Want"));
|
||||
Assert.Throws<FormatException>(() => new MailAddress("\"MrFo@Bar\""));
|
||||
Assert.Throws<FormatException>(() => new MailAddress("\"MrFo@Bar\"<>"));
|
||||
Assert.Throws<FormatException>(() => new MailAddress(" "));
|
||||
Assert.Throws<FormatException>(() => new MailAddress("forbar"));
|
||||
Assert.Throws<FormatException>(() => new MailAddress("<[email protected]> WhatEver", " Mr. Foo Bar "));
|
||||
Assert.Throws<FormatException>(() => new MailAddress("Mr. Far Bar <[email protected]> Whatever", "BarFoo"));
|
||||
yield return new object[] { "Mr. Foo Bar" };
|
||||
yield return new object[] { "foo@b@ar" };
|
||||
yield return new object[] { "Mr. Foo Bar <foo@exa<mple.com" };
|
||||
yield return new object[] { "Mr. Foo Bar <[email protected]" };
|
||||
yield return new object[] { "Mr. \"F@@ Bar\" <[email protected]> Whatever@You@Want" };
|
||||
yield return new object[] { "Mr. F@@ Bar <[email protected]> What\"ever@You@Want" };
|
||||
yield return new object[] { "\"MrFo@Bar\"" };
|
||||
yield return new object[] { "\"MrFo@Bar\"<>" };
|
||||
yield return new object[] { " " };
|
||||
yield return new object[] { "forbar" };
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestConstructorOverload2()
|
||||
[Theory]
|
||||
[MemberData(nameof(GetInvalid_Address))]
|
||||
public void TestInvalidAddressInConstructo_Address(string invalidAddress)
|
||||
{
|
||||
address = new MailAddress(" [email protected] ", null);
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
Assert.Equal(string.Empty, address.DisplayName);
|
||||
Assert.Equal("example.com", address.Host);
|
||||
Assert.Equal("[email protected]", address.ToString());
|
||||
Assert.Equal("foo", address.User);
|
||||
|
||||
address = new MailAddress("Mr. Far Bar <[email protected]>", "BarFoo");
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
Assert.Equal("BarFoo", address.DisplayName);
|
||||
Assert.Equal("example.com", address.Host);
|
||||
Assert.Equal("\"BarFoo\" <[email protected]>", address.ToString());
|
||||
Assert.Equal("foo", address.User);
|
||||
|
||||
address = new MailAddress("Mr. Far Bar <[email protected]> ", string.Empty);
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
Assert.Equal("Mr. Far Bar", address.DisplayName);
|
||||
Assert.Equal("example.com", address.Host);
|
||||
Assert.Equal("\"Mr. Far Bar\" <[email protected]>", address.ToString());
|
||||
Assert.Equal("foo", address.User);
|
||||
|
||||
address = new MailAddress("Mr. Far Bar <[email protected]>", null);
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
Assert.Equal("Mr. Far Bar", address.DisplayName);
|
||||
Assert.Equal("example.com", address.Host);
|
||||
Assert.Equal("\"Mr. Far Bar\" <[email protected]>", address.ToString());
|
||||
Assert.Equal("foo", address.User);
|
||||
|
||||
address = new MailAddress("Mr. Far Bar <[email protected]> ", " ");
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
Assert.Equal(" ", address.DisplayName);
|
||||
Assert.Equal("example.com", address.Host);
|
||||
Assert.Equal("\" \" <[email protected]>", address.ToString());
|
||||
Assert.Equal("foo", address.User);
|
||||
Assert.Throws<FormatException>(() => new MailAddress(invalidAddress));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisplayName_Precedence()
|
||||
public static IEnumerable<object[]> GetInvalid_AddressDisplayName()
|
||||
{
|
||||
var ma = new MailAddress("Hola <[email protected]>");
|
||||
Assert.Equal("Hola", ma.DisplayName);
|
||||
ma = new MailAddress("Hola <[email protected]>", "Adios");
|
||||
Assert.Equal("Adios", ma.DisplayName);
|
||||
ma = new MailAddress("Hola <[email protected]>", "");
|
||||
Assert.Equal("Hola", ma.DisplayName);
|
||||
ma = new MailAddress("<[email protected]>", "");
|
||||
Assert.Equal("", ma.DisplayName);
|
||||
yield return new object[] { "<[email protected]> WhatEver", " Mr. Foo Bar " };
|
||||
yield return new object[] { "Mr. Far Bar <[email protected]> Whatever", "BarFoo" };
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetInvalid_AddressDisplayName))]
|
||||
public void TestInvalidAddressInConstructor_AddressDisplayName(string invalidAddress, string displayName)
|
||||
{
|
||||
Assert.Throws<FormatException>(() => new MailAddress(invalidAddress, displayName));
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> GetValid_AddressDisplayName()
|
||||
{
|
||||
// inputAddress, inputDisplayName, address, displayName, host, toString(), user
|
||||
yield return new object[] { " [email protected] ", null, "[email protected]", string.Empty, "example.com", "[email protected]", "foo" };
|
||||
yield return new object[] { "Mr. Far Bar <[email protected]>", "BarFoo", "[email protected]", "BarFoo", "example.com", "\"BarFoo\" <[email protected]>", "foo" };
|
||||
yield return new object[] { "Mr. Far Bar <[email protected]> ", string.Empty, "[email protected]", "Mr. Far Bar", "example.com", "\"Mr. Far Bar\" <[email protected]>", "foo" };
|
||||
yield return new object[] { "Mr. Far Bar <[email protected]>", null, "[email protected]", "Mr. Far Bar", "example.com", "\"Mr. Far Bar\" <[email protected]>", "foo" };
|
||||
yield return new object[] { "Mr. Far Bar <[email protected]> ", " ", "[email protected]", " ", "example.com", "\" \" <[email protected]>", "foo" };
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetValid_AddressDisplayName))]
|
||||
public void TestConstructor_AddressDisplayName(string inputAddress, string inputDisplayName, string address, string displayName, string host, string toString, string user)
|
||||
{
|
||||
MailAddress addressInstance = new MailAddress(inputAddress, inputDisplayName);
|
||||
Assert.Equal(address, addressInstance.Address);
|
||||
Assert.Equal(displayName, addressInstance.DisplayName);
|
||||
Assert.Equal(host, addressInstance.Host);
|
||||
Assert.Equal(toString, addressInstance.ToString());
|
||||
Assert.Equal(user, addressInstance.User);
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> GetAddress_DisplayNamePrecedence()
|
||||
{
|
||||
// inputAddress, displayName
|
||||
yield return new object[] { "Hola <[email protected]>", "Hola" };
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetAddress_DisplayNamePrecedence))]
|
||||
public void DisplayName_Precedence(string inputAddress, string displayName)
|
||||
{
|
||||
var ma = new MailAddress(inputAddress);
|
||||
Assert.Equal(displayName, ma.DisplayName);
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> GetAddressDisplayName_DisplayNamePrecedence()
|
||||
{
|
||||
// inputAddress, inputDisplayName, displayName
|
||||
yield return new object[] { "Hola <[email protected]>", "Adios", "Adios" };
|
||||
yield return new object[] { "Hola <[email protected]>", "", "Hola" };
|
||||
yield return new object[] { "<[email protected]>", "", "" };
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetAddressDisplayName_DisplayNamePrecedence))]
|
||||
public void AddressDisplayName_Precedence(string inputAddress, string inputDisplayName, string displayName)
|
||||
{
|
||||
var ma = new MailAddress(inputAddress, inputDisplayName);
|
||||
Assert.Equal(displayName, ma.DisplayName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Address_QuoteFirst()
|
||||
{
|
||||
new MailAddress("\"Hola\" <[email protected]>");
|
||||
Assert.True(MailAddress.TryCreate("\"Hola\" <[email protected]>", out MailAddress _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Address_QuoteNotFirst()
|
||||
{
|
||||
Assert.Throws<FormatException>(() => new MailAddress("H\"ola\" <[email protected]>"));
|
||||
Assert.False(MailAddress.TryCreate("H\"ola\" <[email protected]>", out MailAddress _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Address_NoClosingQuote()
|
||||
{
|
||||
Assert.Throws<FormatException>(() => new MailAddress("\"Hola <[email protected]>"));
|
||||
Assert.False(MailAddress.TryCreate("\"Hola <[email protected]>", out MailAddress _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Address_NoUser()
|
||||
{
|
||||
Assert.Throws<FormatException>(() => new MailAddress("Hola <@bar.com>"));
|
||||
Assert.False(MailAddress.TryCreate("Hola <@bar.com>", out MailAddress _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Address_NoUserNoHost()
|
||||
{
|
||||
Assert.Throws<FormatException>(() => new MailAddress("Hola <@>"));
|
||||
Assert.False(MailAddress.TryCreate("Hola <@>", out MailAddress _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Address()
|
||||
public void MailAddress_AllMembers()
|
||||
{
|
||||
MailAddress address = new MailAddress("[email protected]", "Mr. Foo Bar");
|
||||
Assert.Equal("[email protected]", address.Address);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisplayName()
|
||||
{
|
||||
Assert.Equal("Mr. Foo Bar", address.DisplayName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Host()
|
||||
{
|
||||
Assert.Equal("example.com", address.Host);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void User()
|
||||
{
|
||||
Assert.Equal("foo", address.User);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToStringTest()
|
||||
{
|
||||
Assert.Equal("\"Mr. Foo Bar\" <[email protected]>", address.ToString());
|
||||
}
|
||||
|
||||
@@ -231,5 +194,59 @@ namespace System.Net.Mail.Tests
|
||||
var n2 = new MailAddress("MR. BAR <[email protected]>");
|
||||
Assert.Equal(n, n2);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetInvalid_Address))]
|
||||
public void TryCreate_Invalid_Address(string address)
|
||||
{
|
||||
Assert.False(MailAddress.TryCreate(address, out MailAddress _));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetInvalid_AddressDisplayName))]
|
||||
public void TryCreate_Invalid_AddressAndDisplayName(string address, string displayName)
|
||||
{
|
||||
Assert.False(MailAddress.TryCreate(address, displayName, out MailAddress _));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetValid_Address))]
|
||||
public void TryCreate_Valid_Address(string inputAddress, string address, string displayName, string host, string toString, string user)
|
||||
{
|
||||
Assert.True(MailAddress.TryCreate(inputAddress, out MailAddress addressInstance));
|
||||
Assert.Equal(address, addressInstance.Address);
|
||||
Assert.Equal(displayName, addressInstance.DisplayName);
|
||||
Assert.Equal(host, addressInstance.Host);
|
||||
Assert.Equal(toString, addressInstance.ToString());
|
||||
Assert.Equal(user, addressInstance.User);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetValid_AddressDisplayName))]
|
||||
public void TryCreate_Valid_AddressDisplayName(string inputAddress, string inputDisplayName, string address, string displayName, string host, string toString, string user)
|
||||
{
|
||||
Assert.True(MailAddress.TryCreate(inputAddress, inputDisplayName, out MailAddress addressInstance));
|
||||
Assert.Equal(address, addressInstance.Address);
|
||||
Assert.Equal(displayName, addressInstance.DisplayName);
|
||||
Assert.Equal(host, addressInstance.Host);
|
||||
Assert.Equal(toString, addressInstance.ToString());
|
||||
Assert.Equal(user, addressInstance.User);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetAddress_DisplayNamePrecedence))]
|
||||
public void TryCreate_DisplayName_Precedence(string inputAddress, string displayName)
|
||||
{
|
||||
Assert.True(MailAddress.TryCreate(inputAddress, out MailAddress addressInstance));
|
||||
Assert.Equal(displayName, addressInstance.DisplayName);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetAddressDisplayName_DisplayNamePrecedence))]
|
||||
public void TryCreate_AddressDisplayName_Precedence(string inputAddress, string inputDisplayName, string displayName)
|
||||
{
|
||||
Assert.True(MailAddress.TryCreate(inputAddress, inputDisplayName, out MailAddress addressInstance));
|
||||
Assert.Equal(displayName, addressInstance.DisplayName);
|
||||
}
|
||||
}
|
||||
}
|
||||
+121
-38
@@ -2,16 +2,60 @@
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Mime;
|
||||
using System.Text;
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Mail.Tests
|
||||
{
|
||||
public class MailAddressEncodeTest
|
||||
{
|
||||
[Fact]
|
||||
public void EncodeSingleMailAddress_WithAddressAndNoUnicode_AndPaddingValueOfNonZero_ShouldEncodeCorrectly()
|
||||
public static IEnumerable<object[]> MailAddressFactory_Address()
|
||||
{
|
||||
MailAddress testAddress = new MailAddress("[email protected]");
|
||||
yield return new object[] { new Func<string, MailAddress>((string address) => new MailAddress(address)) };
|
||||
yield return new object[] { new Func<string, MailAddress>(
|
||||
(string address) =>
|
||||
{
|
||||
Assert.True(MailAddress.TryCreate(address, out MailAddress result));
|
||||
return result;
|
||||
})};
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> MailAddressFactory_AddressDisplayName()
|
||||
{
|
||||
yield return new object[] { new Func<string, string, MailAddress>((string address, string displayName) => new MailAddress(address, displayName)) };
|
||||
yield return new object[] { new Func<string,string, MailAddress>(
|
||||
(string address, string displayName) =>
|
||||
{
|
||||
Assert.True(MailAddress.TryCreate(address, displayName, out MailAddress result));
|
||||
return result;
|
||||
})};
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> MailAddressFactory_Mixed()
|
||||
{
|
||||
yield return new object[] { new Func<string, MailAddress>((string address) => new MailAddress(address)), new Func<string, string, MailAddress>((string address, string displayName) => new MailAddress(address, displayName)) };
|
||||
yield return new object[] {
|
||||
new Func<string, MailAddress>(
|
||||
(string address) =>
|
||||
{
|
||||
Assert.True(MailAddress.TryCreate(address, out MailAddress result));
|
||||
return result;
|
||||
}),
|
||||
new Func<string,string, MailAddress>(
|
||||
(string address, string displayName) =>
|
||||
{
|
||||
Assert.True(MailAddress.TryCreate(address, displayName, out MailAddress result));
|
||||
return result;
|
||||
})};
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(MailAddressFactory_Address))]
|
||||
public void EncodeSingleMailAddress_WithAddressAndNoUnicode_AndPaddingValueOfNonZero_ShouldEncodeCorrectly(Func<string, MailAddress> factory)
|
||||
{
|
||||
MailAddress testAddress = factory("[email protected]");
|
||||
|
||||
string result = testAddress.Encode(2, false);
|
||||
Assert.Equal("[email protected]", result);
|
||||
@@ -20,10 +64,11 @@ namespace System.Net.Mail.Tests
|
||||
Assert.Equal("[email protected]", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncodeSingleMailAddress_WithAddressAndUnicode_AndPaddingValueOfNonZero_ShouldEncodeCorrectly()
|
||||
[Theory]
|
||||
[MemberData(nameof(MailAddressFactory_AddressDisplayName))]
|
||||
public void EncodeSingleMailAddress_WithAddressAndUnicode_AndPaddingValueOfNonZero_ShouldEncodeCorrectly(Func<string, string, MailAddress> factory)
|
||||
{
|
||||
MailAddress testAddress = new MailAddress("[email protected]", "jeff \u00C3\u00DA\u00EA\u00EB\u00EF\u00EF");
|
||||
MailAddress testAddress = factory("[email protected]", "jeff \u00C3\u00DA\u00EA\u00EB\u00EF\u00EF");
|
||||
string result = testAddress.Encode(2, false);
|
||||
Assert.Equal("=?utf-8?Q?jeff_=C3=83=C3=9A=C3=AA=C3=AB=C3=AF=C3=AF?= <[email protected]>", result);
|
||||
|
||||
@@ -31,18 +76,20 @@ namespace System.Net.Mail.Tests
|
||||
Assert.Equal("\"jeff \u00C3\u00DA\u00EA\u00EB\u00EF\u00EF\" <[email protected]>", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncodeSingleMailAddress_WithAddressOnly_ShouldEncodeAsAddress()
|
||||
[Theory]
|
||||
[MemberData(nameof(MailAddressFactory_Address))]
|
||||
public void EncodeSingleMailAddress_WithAddressOnly_ShouldEncodeAsAddress(Func<string, MailAddress> factory)
|
||||
{
|
||||
MailAddress testAddress = new MailAddress("[email protected]");
|
||||
MailAddress testAddress = factory("[email protected]");
|
||||
string result = testAddress.Encode(0, false);
|
||||
Assert.Equal("[email protected]", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncodeSingleMailAddress_WithAddressAndDisplayName_ShouldEncodeAsDisplayNameAndAddressInBrackets()
|
||||
[Theory]
|
||||
[MemberData(nameof(MailAddressFactory_AddressDisplayName))]
|
||||
public void EncodeSingleMailAddress_WithAddressAndDisplayName_ShouldEncodeAsDisplayNameAndAddressInBrackets(Func<string, string, MailAddress> factory)
|
||||
{
|
||||
MailAddress testAddress = new MailAddress("[email protected]", "test");
|
||||
MailAddress testAddress = factory("[email protected]", "test");
|
||||
string result = testAddress.Encode(0, false);
|
||||
Assert.Equal("\"test\" <[email protected]>", result);
|
||||
|
||||
@@ -50,10 +97,11 @@ namespace System.Net.Mail.Tests
|
||||
Assert.Equal("\"test\" <[email protected]>", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncodeSingleMailAddress_WithAddressAndDisplayNameUnicode_ShouldQEncode()
|
||||
[Theory]
|
||||
[MemberData(nameof(MailAddressFactory_AddressDisplayName))]
|
||||
public void EncodeSingleMailAddress_WithAddressAndDisplayNameUnicode_ShouldQEncode(Func<string, string, MailAddress> factory)
|
||||
{
|
||||
MailAddress testAddress = new MailAddress("[email protected]", "test\u00DC");
|
||||
MailAddress testAddress = factory("[email protected]", "test\u00DC");
|
||||
|
||||
string result = testAddress.Encode(0, false);
|
||||
Assert.Equal("=?utf-8?Q?test=C3=9C?= <[email protected]>", result);
|
||||
@@ -62,10 +110,11 @@ namespace System.Net.Mail.Tests
|
||||
Assert.Equal("\"test\u00DC\" <[email protected]>", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncodeSingleMailAddress_WithAddressAndLongDisplayNameUnicode_ShouldQEncode()
|
||||
[Theory]
|
||||
[MemberData(nameof(MailAddressFactory_AddressDisplayName))]
|
||||
public void EncodeSingleMailAddress_WithAddressAndLongDisplayNameUnicode_ShouldQEncode(Func<string, string, MailAddress> factory)
|
||||
{
|
||||
MailAddress testAddress = new MailAddress("[email protected]",
|
||||
MailAddress testAddress = factory("[email protected]",
|
||||
"test\u00DCtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest");
|
||||
string result = testAddress.Encode(0, false);
|
||||
Assert.Equal("=?utf-8?Q?test=C3=9Ctesttesttesttesttesttesttesttesttesttesttesttest?=\r\n "
|
||||
@@ -76,10 +125,11 @@ namespace System.Net.Mail.Tests
|
||||
+ "<[email protected]>", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncodeSingleMailAddress_WithAddressAndNonAsciiAndRangeOfChars_ShouldQEncode()
|
||||
[Theory]
|
||||
[MemberData(nameof(MailAddressFactory_AddressDisplayName))]
|
||||
public void EncodeSingleMailAddress_WithAddressAndNonAsciiAndRangeOfChars_ShouldQEncode(Func<string, string, MailAddress> factory)
|
||||
{
|
||||
MailAddress testAddress = new MailAddress("[email protected]",
|
||||
MailAddress testAddress = factory("[email protected]",
|
||||
"\u00AE !#$%&'()+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");
|
||||
string result = testAddress.Encode(0, false);
|
||||
Assert.Equal("=?utf-8?Q?=C2=AE_=21=23=24=25=26=27=28=29=2B=2C=2D=2E=2F0123456789=3A?="
|
||||
@@ -92,10 +142,11 @@ namespace System.Net.Mail.Tests
|
||||
+ "<[email protected]>", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncodeMultipleMailAddress_WithOneAddressAndDisplayName_ShouldEncodeAsDisplayNameAndAddressInBrackets()
|
||||
[Theory]
|
||||
[MemberData(nameof(MailAddressFactory_AddressDisplayName))]
|
||||
public void EncodeMultipleMailAddress_WithOneAddressAndDisplayName_ShouldEncodeAsDisplayNameAndAddressInBrackets(Func<string, string, MailAddress> factory)
|
||||
{
|
||||
MailAddress testAddress = new MailAddress("[email protected]", "test");
|
||||
MailAddress testAddress = factory("[email protected]", "test");
|
||||
MailAddressCollection collection = new MailAddressCollection();
|
||||
collection.Add(testAddress);
|
||||
|
||||
@@ -106,11 +157,12 @@ namespace System.Net.Mail.Tests
|
||||
Assert.Equal("\"test\" <[email protected]>", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncodeMultipleMailAddress_WithTwoAddressesThatAreTheSame_ShouldEncodeCorrectly()
|
||||
[Theory]
|
||||
[MemberData(nameof(MailAddressFactory_AddressDisplayName))]
|
||||
public void EncodeMultipleMailAddress_WithTwoAddressesThatAreTheSame_ShouldEncodeCorrectly(Func<string, string, MailAddress> factory)
|
||||
{
|
||||
MailAddress testAddress = new MailAddress("[email protected]", "test");
|
||||
MailAddress testAddress2 = new MailAddress("[email protected]", "test");
|
||||
MailAddress testAddress = factory("[email protected]", "test");
|
||||
MailAddress testAddress2 = factory("[email protected]", "test");
|
||||
MailAddressCollection collection = new MailAddressCollection();
|
||||
collection.Add(testAddress);
|
||||
collection.Add(testAddress2);
|
||||
@@ -122,11 +174,12 @@ namespace System.Net.Mail.Tests
|
||||
Assert.Equal("\"test\" <[email protected]>, \"test\" <[email protected]>", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncodeMultipleMailAddress_WithTwoAddressesThatAreDifferentAndContainUnicode_ShouldEncodeCorrectly()
|
||||
[Theory]
|
||||
[MemberData(nameof(MailAddressFactory_AddressDisplayName))]
|
||||
public void EncodeMultipleMailAddress_WithTwoAddressesThatAreDifferentAndContainUnicode_ShouldEncodeCorrectly(Func<string, string, MailAddress> factory)
|
||||
{
|
||||
MailAddress testAddress = new MailAddress("[email protected]", "test");
|
||||
MailAddress testAddress2 = new MailAddress("[email protected]", "test\u00DC");
|
||||
MailAddress testAddress = factory("[email protected]", "test");
|
||||
MailAddress testAddress2 = factory("[email protected]", "test\u00DC");
|
||||
MailAddressCollection collection = new MailAddressCollection();
|
||||
collection.Add(testAddress);
|
||||
collection.Add(testAddress2);
|
||||
@@ -138,13 +191,14 @@ namespace System.Net.Mail.Tests
|
||||
Assert.Equal("\"test\" <[email protected]>, \"test\u00DC\" <[email protected]>", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncodeMultipleMailAddress_WithManyAddressesThatAreDifferentAndContainUnicode_ShouldEncodeCorrectly()
|
||||
[Theory]
|
||||
[MemberData(nameof(MailAddressFactory_Mixed))]
|
||||
public void EncodeMultipleMailAddress_WithManyAddressesThatAreDifferentAndContainUnicode_ShouldEncodeCorrectly(Func<string, MailAddress> factoryAddress, Func<string, string, MailAddress> factoryAddressDisplayName)
|
||||
{
|
||||
MailAddress testAddress = new MailAddress("[email protected]", "test");
|
||||
MailAddress testAddress2 = new MailAddress("[email protected]", "test\u00DC");
|
||||
MailAddress testAddress3 = new MailAddress("[email protected]");
|
||||
MailAddress testAddress4 = new MailAddress("[email protected]", "test\u00DC");
|
||||
MailAddress testAddress = factoryAddressDisplayName("[email protected]", "test");
|
||||
MailAddress testAddress2 = factoryAddressDisplayName("[email protected]", "test\u00DC");
|
||||
MailAddress testAddress3 = factoryAddress("[email protected]");
|
||||
MailAddress testAddress4 = factoryAddressDisplayName("[email protected]", "test\u00DC");
|
||||
|
||||
MailAddressCollection collection = new MailAddressCollection();
|
||||
collection.Add(testAddress);
|
||||
@@ -160,5 +214,34 @@ namespace System.Net.Mail.Tests
|
||||
Assert.Equal("\"test\" <[email protected]>, \"test\u00DC\" <[email protected]>, [email protected],"
|
||||
+ " \"test\u00DC\" <[email protected]>", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CustomEncoding_Null()
|
||||
{
|
||||
Encoding defaultCharset = Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
|
||||
|
||||
Assert.True(MailAddress.TryCreate("[email protected]", "te\u03a0st", null, out MailAddress mailAddress));
|
||||
Assert.Contains(defaultCharset.BodyName, mailAddress.Encode(0, false));
|
||||
|
||||
mailAddress = new MailAddress("[email protected]", "te\u03a0st", null);
|
||||
Assert.Contains(defaultCharset.BodyName, mailAddress.Encode(0, false));
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> EncodingList()
|
||||
{
|
||||
yield return new object[] { Encoding.Unicode };
|
||||
yield return new object[] { Encoding.ASCII };
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(EncodingList))]
|
||||
public void CustomEncoding(Encoding encoding)
|
||||
{
|
||||
Assert.True(MailAddress.TryCreate("[email protected]", "te\u03a0st", encoding, out MailAddress mailAddress));
|
||||
Assert.Contains(encoding.BodyName, mailAddress.Encode(0, false));
|
||||
|
||||
mailAddress = new MailAddress("[email protected]", "te\u03a0st", encoding);
|
||||
Assert.Contains(encoding.BodyName, mailAddress.Encode(0, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
+87
-97
@@ -45,257 +45,257 @@ namespace System.Net.Mail.Tests
|
||||
private const string ValidEmailAddressQuotedLocal = "\"jeff\"@example.com";
|
||||
|
||||
[Fact]
|
||||
public void ReadFWS_WithOnlyWhiteSpace_ShouldReadAll()
|
||||
public void TryReadFWS_WithOnlyWhiteSpace_ShouldReadAll()
|
||||
{
|
||||
int index = WhitespaceOnly.Length - 1;
|
||||
index = WhitespaceReader.ReadFwsReverse(WhitespaceOnly, index);
|
||||
Assert.True(WhitespaceReader.TryReadFwsReverse(WhitespaceOnly, index, out index, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(-1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadFWS_WithNoWhiteSpace_ShouldDoNothing()
|
||||
public void TryReadFWS_WithNoWhiteSpace_ShouldDoNothing()
|
||||
{
|
||||
int index = NoWhitespace.Length - 1;
|
||||
|
||||
index = WhitespaceReader.ReadFwsReverse(NoWhitespace, index);
|
||||
Assert.True(WhitespaceReader.TryReadFwsReverse(NoWhitespace, index, out index, throwExceptionIfFail: true));
|
||||
Assert.Equal(NoWhitespace.Length - 1, index);
|
||||
|
||||
index = WhitespaceInMiddle.Length - 1;
|
||||
|
||||
index = WhitespaceReader.ReadFwsReverse(WhitespaceInMiddle, index);
|
||||
Assert.True(WhitespaceReader.TryReadFwsReverse(WhitespaceInMiddle, index, out index, throwExceptionIfFail: true));
|
||||
Assert.Equal(WhitespaceInMiddle.Length - 1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadFWS_AtBeginningOfString_ShouldReadFWS()
|
||||
public void TryReadFWS_AtBeginningOfString_ShouldReadFWS()
|
||||
{
|
||||
int index = 3;
|
||||
|
||||
index = WhitespaceReader.ReadFwsReverse(WhitespaceAtBeginning, index);
|
||||
Assert.True(WhitespaceReader.TryReadFwsReverse(WhitespaceAtBeginning, index, out index, throwExceptionIfFail: true));
|
||||
Assert.Equal(-1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadCFWS_WithSingleSpace_ShouldReadTheSpace()
|
||||
public void TryReadCFWS_WithSingleSpace_ShouldReadTheSpace()
|
||||
{
|
||||
int index = SingleSpace.Length - 1;
|
||||
|
||||
index = WhitespaceReader.ReadCfwsReverse(SingleSpace, index);
|
||||
Assert.True(WhitespaceReader.TryReadCfwsReverse(SingleSpace, index, out index, throwExceptionIfFail: true));
|
||||
Assert.Equal(-1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadCFWS_WithNoComment_ShouldDoNothing()
|
||||
public void TryReadCFWS_WithNoComment_ShouldDoNothing()
|
||||
{
|
||||
int index = NoComments.Length - 1;
|
||||
|
||||
index = WhitespaceReader.ReadCfwsReverse(NoComments, index);
|
||||
Assert.True(WhitespaceReader.TryReadCfwsReverse(NoComments, index, out index, throwExceptionIfFail: true));
|
||||
Assert.Equal(index, NoComments.Length - 1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadCFWS_WithOnlyComment_ShouldReadAll()
|
||||
public void TryReadCFWS_WithOnlyComment_ShouldReadAll()
|
||||
{
|
||||
int index = OneComment.Length - 1;
|
||||
|
||||
index = WhitespaceReader.ReadCfwsReverse(OneComment, index);
|
||||
Assert.True(WhitespaceReader.TryReadCfwsReverse(OneComment, index, out index, throwExceptionIfFail: true));
|
||||
Assert.Equal(-1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadCFWS_WithNestedComments_ShouldWorkCorrectly()
|
||||
public void TryReadCFWS_WithNestedComments_ShouldWorkCorrectly()
|
||||
{
|
||||
int index = NestedComments.Length - 1;
|
||||
|
||||
index = WhitespaceReader.ReadCfwsReverse(NestedComments, index);
|
||||
Assert.True(WhitespaceReader.TryReadCfwsReverse(NestedComments, index, out index, throwExceptionIfFail: true));
|
||||
Assert.Equal(-1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadCFWS_WithCharsBeforeComment_ShouldWorkCorrectly()
|
||||
public void TryReadCFWS_WithCharsBeforeComment_ShouldWorkCorrectly()
|
||||
{
|
||||
int index = OneCommentWithAdditionalCharsBefore.Length - 1;
|
||||
|
||||
index = WhitespaceReader.ReadCfwsReverse(OneCommentWithAdditionalCharsBefore, index);
|
||||
Assert.True(WhitespaceReader.TryReadCfwsReverse(OneCommentWithAdditionalCharsBefore, index, out index, throwExceptionIfFail: true));
|
||||
Assert.Equal(3, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadQuotedPair_WithValidCharacters_ShouldReadCorrectly()
|
||||
public void TryReadQuotedPair_WithValidCharacters_ShouldReadCorrectly()
|
||||
{
|
||||
// a\\\\b, even quotes, b is unqouted
|
||||
string backslashes = "a\\\\\\\\b";
|
||||
int index = backslashes.Length - 1;
|
||||
int quotedCharCount = QuotedPairReader.CountQuotedChars(backslashes, index, false);
|
||||
Assert.True(QuotedPairReader.TryCountQuotedChars(backslashes, index, false, out int quotedCharCount, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(0, quotedCharCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadQuotedPair_WithValidCharacterAndEscapedCharacter_ShouldReadCorrectly()
|
||||
public void TryReadQuotedPair_WithValidCharacterAndEscapedCharacter_ShouldReadCorrectly()
|
||||
{
|
||||
// this is a\\\\\"
|
||||
string backslashes = "a\\\\\\\\\\\"";
|
||||
int index = backslashes.Length - 1;
|
||||
int quotedCharCount = QuotedPairReader.CountQuotedChars(backslashes, index, false);
|
||||
Assert.True(QuotedPairReader.TryCountQuotedChars(backslashes, index, false, out int quotedCharCount, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(6, quotedCharCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadQuotedPair_WithValidCharacterAndEscapedCharacterAndBackslashIsStartOfString_ShouldReadCorrectly()
|
||||
public void TryReadQuotedPair_WithValidCharacterAndEscapedCharacterAndBackslashIsStartOfString_ShouldReadCorrectly()
|
||||
{
|
||||
// this is \\\\\"
|
||||
string backslashes = "\\\\\\\\\\\"";
|
||||
int index = backslashes.Length - 1;
|
||||
int quotedCharCount = QuotedPairReader.CountQuotedChars(backslashes, index, false);
|
||||
Assert.True(QuotedPairReader.TryCountQuotedChars(backslashes, index, false, out int quotedCharCount, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(6, quotedCharCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadQuotedPair_WithInvalidNonescapedCharacter_ShouldThrow()
|
||||
public void TryReadQuotedPair_WithInvalidNonescapedCharacter_ShouldThrow()
|
||||
{
|
||||
// this is a\\\\"
|
||||
string backslashes = "a\\\\\\\\\"";
|
||||
int index = backslashes.Length - 1;
|
||||
int quotedCharCount = QuotedPairReader.CountQuotedChars(backslashes, index, false);
|
||||
Assert.True(QuotedPairReader.TryCountQuotedChars(backslashes, index, false, out int quotedCharCount, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(0, quotedCharCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadDomainLiteral_WithValidDomainLiteral_ShouldReadCorrectly()
|
||||
public void TryReadDomainLiteral_WithValidDomainLiteral_ShouldReadCorrectly()
|
||||
{
|
||||
int index = ValidDomainLiteral.Length - 1;
|
||||
index = DomainLiteralReader.ReadReverse(ValidDomainLiteral, index);
|
||||
Assert.True(DomainLiteralReader.TryReadReverse(ValidDomainLiteral, index, out index, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(-1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadDomainLiteral_WithLongListOfBackslashes_ShouldReadCorrectly()
|
||||
public void TryReadDomainLiteral_WithLongListOfBackslashes_ShouldReadCorrectly()
|
||||
{
|
||||
int index = ValidDomainBackslashList.Length - 1;
|
||||
index = DomainLiteralReader.ReadReverse(ValidDomainBackslashList, index);
|
||||
Assert.True(DomainLiteralReader.TryReadReverse(ValidDomainBackslashList, index, out index, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(-1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadDomainLiteral_WithValidDomainLiteralAndEscapedCharacters_ShouldReadCorrectly()
|
||||
public void TryReadDomainLiteral_WithValidDomainLiteralAndEscapedCharacters_ShouldReadCorrectly()
|
||||
{
|
||||
int index = ValidDomainLiteralEscapedChars.Length - 1;
|
||||
index = DomainLiteralReader.ReadReverse(ValidDomainLiteralEscapedChars, index);
|
||||
Assert.True(DomainLiteralReader.TryReadReverse(ValidDomainLiteralEscapedChars, index, out index, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(-1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadDomainLiteral_WithInvalidCharacter_ShouldThrow()
|
||||
public void TryReadDomainLiteral_WithInvalidCharacter_ShouldThrow()
|
||||
{
|
||||
int index = InvalidDomainLiteral.Length - 1;
|
||||
Assert.Throws<FormatException>(() => { DomainLiteralReader.ReadReverse(InvalidDomainLiteral, index); });
|
||||
Assert.Throws<FormatException>(() => { DomainLiteralReader.TryReadReverse(InvalidDomainLiteral, index, out int _, throwExceptionIfFail: true); });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadQuotedString_WithUnicodeAndUnicodeIsInvalid_ShouldThrow()
|
||||
public void TryReadQuotedString_WithUnicodeAndUnicodeIsInvalid_ShouldThrow()
|
||||
{
|
||||
int index = UnicodeQuotedString.Length - 1;
|
||||
Assert.Throws<FormatException>(() => { QuotedStringFormatReader.ReadReverseUnQuoted(UnicodeQuotedString, index, false, false); });
|
||||
Assert.Throws<FormatException>(() => { QuotedStringFormatReader.TryReadReverseUnQuoted(UnicodeQuotedString, index, false, false, out int _, throwExceptionIfFail: true); });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadQuotedString_WithValidUnicodeQuotedString_ShouldReadCorrectly()
|
||||
public void TryReadQuotedString_WithValidUnicodeQuotedString_ShouldReadCorrectly()
|
||||
{
|
||||
int index = UnicodeQuotedString.Length - 1;
|
||||
index = QuotedStringFormatReader.ReadReverseUnQuoted(UnicodeQuotedString, index, true, false);
|
||||
Assert.True(QuotedStringFormatReader.TryReadReverseUnQuoted(UnicodeQuotedString, index, true, false, out index, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(-1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadQuotedString_WithValidQuotedString_ShouldReadCorrectly()
|
||||
public void TryReadQuotedString_WithValidQuotedString_ShouldReadCorrectly()
|
||||
{
|
||||
int index = ValidQuotedString.Length - 1;
|
||||
index = QuotedStringFormatReader.ReadReverseQuoted(ValidQuotedString, index, false);
|
||||
Assert.True(QuotedStringFormatReader.TryReadReverseQuoted(ValidQuotedString, index, false, out index, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(-1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadQuotedString_WithEscapedCharacters_ShouldReadCorrectly()
|
||||
public void TryReadQuotedString_WithEscapedCharacters_ShouldReadCorrectly()
|
||||
{
|
||||
int index = ValidQuotedStringWithEscapedChars.Length - 1;
|
||||
index = QuotedStringFormatReader.ReadReverseQuoted(ValidQuotedStringWithEscapedChars, index, false);
|
||||
Assert.True(QuotedStringFormatReader.TryReadReverseQuoted(ValidQuotedStringWithEscapedChars, index, false, out index, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(-1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadQuotedString_WithInvalidCharacters_ShouldThrow()
|
||||
public void TryReadQuotedString_WithInvalidCharacters_ShouldThrow()
|
||||
{
|
||||
int index = InvalidQuotedString.Length - 1;
|
||||
Assert.Throws<FormatException>(() => { QuotedStringFormatReader.ReadReverseQuoted(InvalidQuotedString, index, false); });
|
||||
Assert.Throws<FormatException>(() => { QuotedStringFormatReader.TryReadReverseQuoted(InvalidQuotedString, index, false, out int _, throwExceptionIfFail: true); });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadQuotedString_WithValidMultipleStrings_ReturnTheDelimterIndex()
|
||||
public void TryReadQuotedString_WithValidMultipleStrings_ReturnTheDelimterIndex()
|
||||
{
|
||||
int index = ValidMultipleStrings.Length - 1;
|
||||
index = QuotedStringFormatReader.ReadReverseUnQuoted(ValidMultipleStrings, index, false, true);
|
||||
Assert.True(QuotedStringFormatReader.TryReadReverseUnQuoted(ValidMultipleStrings, index, false, true, out index, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(5, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadQuotedString_WithValidMultipleQuotedStrings_ReturnTheIndexPastTheQuote()
|
||||
public void TryReadQuotedString_WithValidMultipleQuotedStrings_ReturnTheIndexPastTheQuote()
|
||||
{
|
||||
int index = ValidMultipleQuotedStrings.Length - 1;
|
||||
index = QuotedStringFormatReader.ReadReverseQuoted(ValidMultipleQuotedStrings, index, false);
|
||||
Assert.True(QuotedStringFormatReader.TryReadReverseQuoted(ValidMultipleQuotedStrings, index, false, out index, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(8, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadDotAtom_WithValidDotAtom_ShouldReadCorrectly()
|
||||
public void TryReadDotAtom_WithValidDotAtom_ShouldReadCorrectly()
|
||||
{
|
||||
int index = ValidDotAtom.Length - 1;
|
||||
index = DotAtomReader.ReadReverse(ValidDotAtom, index);
|
||||
Assert.True(DotAtomReader.TryReadReverse(ValidDotAtom, index, out index, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(0, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadDotAtom_WithValidDotAtomAndDoubleDots_ShouldReadCorrectly()
|
||||
public void TryReadDotAtom_WithValidDotAtomAndDoubleDots_ShouldReadCorrectly()
|
||||
{
|
||||
int index = ValidDotAtomDoubleDots.Length - 1;
|
||||
index = DotAtomReader.ReadReverse(ValidDotAtomDoubleDots, index);
|
||||
Assert.True(DotAtomReader.TryReadReverse(ValidDotAtomDoubleDots, index, out index, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(0, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadDotAtom_EndsInDot_ShouldReadCorrectly()
|
||||
public void TryReadDotAtom_EndsInDot_ShouldReadCorrectly()
|
||||
{
|
||||
int index = ValidDotAtomEndsInDot.Length - 1;
|
||||
index = DotAtomReader.ReadReverse(ValidDotAtomEndsInDot, index);
|
||||
Assert.True(DotAtomReader.TryReadReverse(ValidDotAtomEndsInDot, index, out index, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(-1, index);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadDotAtom_WithDotAtBeginning_ShouldThrow()
|
||||
public void TryReadDotAtom_WithDotAtBeginning_ShouldThrow()
|
||||
{
|
||||
int index = InvalidDotAtomStartsWithDot.Length - 1;
|
||||
Assert.Throws<FormatException>(() => { DotAtomReader.ReadReverse(InvalidDotAtomStartsWithDot, index); });
|
||||
Assert.Throws<FormatException>(() => { DotAtomReader.TryReadReverse(InvalidDotAtomStartsWithDot, index, out int _, throwExceptionIfFail: true); });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithNoQuotes_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithNoQuotes_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result = MailAddressParser.ParseAddress("test(comment) [email protected]");
|
||||
Assert.True(MailAddressParser.TryParseAddress("test(comment) [email protected]", out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal("test", result.User);
|
||||
Assert.Equal("example.com", result.Host);
|
||||
@@ -303,10 +303,9 @@ namespace System.Net.Mail.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithDisplayNameAndAddress_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithDisplayNameAndAddress_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result;
|
||||
result = MailAddressParser.ParseAddress(ValidEmailAddressWithDisplayName);
|
||||
Assert.True(MailAddressParser.TryParseAddress(ValidEmailAddressWithDisplayName, out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal("jeff", result.DisplayName);
|
||||
Assert.Equal("jetucker", result.User);
|
||||
@@ -314,10 +313,9 @@ namespace System.Net.Mail.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithNoDisplayNameAndAngleAddress_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithNoDisplayNameAndAngleAddress_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result;
|
||||
result = MailAddressParser.ParseAddress(ValidEmailAddressWithNoDisplayName);
|
||||
Assert.True(MailAddressParser.TryParseAddress(ValidEmailAddressWithNoDisplayName, out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(string.Empty, result.DisplayName);
|
||||
Assert.Equal("jetucker", result.User);
|
||||
@@ -325,10 +323,9 @@ namespace System.Net.Mail.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithNoDisplayNameAndNoAngleBrackets_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithNoDisplayNameAndNoAngleBrackets_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result;
|
||||
result = MailAddressParser.ParseAddress(ValidEmailAddressWithNoAngleBrackets);
|
||||
Assert.True(MailAddressParser.TryParseAddress(ValidEmailAddressWithNoAngleBrackets, out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(string.Empty, result.DisplayName);
|
||||
Assert.Equal("jetucker", result.User);
|
||||
@@ -336,10 +333,9 @@ namespace System.Net.Mail.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithDomainLiteral_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithDomainLiteral_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result;
|
||||
result = MailAddressParser.ParseAddress(ValidEmailAddressWithDomainLiteral);
|
||||
Assert.True(MailAddressParser.TryParseAddress(ValidEmailAddressWithDomainLiteral, out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal("jeff", result.DisplayName);
|
||||
Assert.Equal("jetucker", result.User);
|
||||
@@ -347,10 +343,9 @@ namespace System.Net.Mail.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithQuotedLocalPartAndNoDisplayName_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithQuotedLocalPartAndNoDisplayName_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result;
|
||||
result = MailAddressParser.ParseAddress(ValidEmailAddressQuotedLocal);
|
||||
Assert.True(MailAddressParser.TryParseAddress(ValidEmailAddressQuotedLocal, out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(string.Empty, result.DisplayName);
|
||||
Assert.Equal("\"jeff\"", result.User);
|
||||
@@ -358,10 +353,9 @@ namespace System.Net.Mail.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithCommentsAndQuotedLocalAndNoDisplayName_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithCommentsAndQuotedLocalAndNoDisplayName_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result;
|
||||
result = MailAddressParser.ParseAddress("(comment)\" asciin;,oqu o.tesws \"(comment)@(comment)this.test.this(comment)");
|
||||
Assert.True(MailAddressParser.TryParseAddress("(comment)\" asciin;,oqu o.tesws \"(comment)@(comment)this.test.this(comment)", out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(string.Empty, result.DisplayName);
|
||||
Assert.Equal("\" asciin;,oqu o.tesws \"", result.User);
|
||||
@@ -369,10 +363,9 @@ namespace System.Net.Mail.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithCommentsAndUnquotedLocalAndUnquotedDisplayName_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithCommentsAndUnquotedLocalAndUnquotedDisplayName_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result;
|
||||
result = MailAddressParser.ParseAddress("(comment)this.test.this(comment)<(comment)this.test.this(comment)@(comment)[ test this ](comment)>");
|
||||
Assert.True(MailAddressParser.TryParseAddress("(comment)this.test.this(comment)<(comment)this.test.this(comment)@(comment)[ test this ](comment)>", out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal("(comment)this.test.this(comment)", result.DisplayName);
|
||||
Assert.Equal("this.test.this", result.User);
|
||||
@@ -380,9 +373,9 @@ namespace System.Net.Mail.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithEscapedCharacters_AndQuotedLocalPart_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithEscapedCharacters_AndQuotedLocalPart_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result = MailAddressParser.ParseAddress("\"jeff\\\\@\" <(jeff\\@s email)\"jeff\\\"\"@(comment)[ ncl\\@bld-001 \t ](comment)>");
|
||||
Assert.True(MailAddressParser.TryParseAddress("\"jeff\\\\@\" <(jeff\\@s email)\"jeff\\\"\"@(comment)[ ncl\\@bld-001 \t ](comment)>", out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal("jeff\\\\@", result.DisplayName);
|
||||
Assert.Equal("\"jeff\\\"\"", result.User);
|
||||
@@ -390,10 +383,9 @@ namespace System.Net.Mail.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithNoDisplayNameAndDotAtom_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithNoDisplayNameAndDotAtom_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result;
|
||||
result = MailAddressParser.ParseAddress("[email protected]");
|
||||
Assert.True(MailAddressParser.TryParseAddress("[email protected]", out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(string.Empty, result.DisplayName);
|
||||
Assert.Equal("a..b_b", result.User);
|
||||
@@ -401,10 +393,9 @@ namespace System.Net.Mail.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithQuotedDisplayNameandNoAngleAddress_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithQuotedDisplayNameandNoAngleAddress_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result;
|
||||
result = MailAddressParser.ParseAddress("\"Test user\" [email protected]");
|
||||
Assert.True(MailAddressParser.TryParseAddress("\"Test user\" [email protected]", out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal("Test user", result.DisplayName);
|
||||
Assert.Equal("testuser", result.User);
|
||||
@@ -412,22 +403,21 @@ namespace System.Net.Mail.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithInvalidLocalPart_ShouldThrow()
|
||||
public void TryParseAddress_WithInvalidLocalPart_ShouldThrow()
|
||||
{
|
||||
Assert.Throws<FormatException>(() => { MailAddressParser.ParseAddress("test[test]@test.com"); });
|
||||
Assert.Throws<FormatException>(() => { MailAddressParser.TryParseAddress("test[test]@test.com", out ParseAddressInfo _, true); });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithHangingAngleBracket_ShouldThrow()
|
||||
public void TryParseAddress_WithHangingAngleBracket_ShouldThrow()
|
||||
{
|
||||
Assert.Throws<FormatException>(() => { MailAddressParser.ParseAddress("<[email protected]"); });
|
||||
Assert.Throws<FormatException>(() => { MailAddressParser.TryParseAddress("<[email protected]", out ParseAddressInfo _, true); });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithQuotedDisplayNameAndQuotedLocalAndAngleBrackets_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithQuotedDisplayNameAndQuotedLocalAndAngleBrackets_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result;
|
||||
result = MailAddressParser.ParseAddress("(comment)\" asciin;,oqu o.tesws \"(comment)<(comment)\" asciin;,oqu o.tesws \"(comment)@(comment)[ test this ](comment)>");
|
||||
Assert.True(MailAddressParser.TryParseAddress("(comment)\" asciin;,oqu o.tesws \"(comment)<(comment)\" asciin;,oqu o.tesws \"(comment)@(comment)[ test this ](comment)>", out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal(" asciin;,oqu o.tesws ", result.DisplayName);
|
||||
Assert.Equal("\" asciin;,oqu o.tesws \"", result.User);
|
||||
@@ -435,15 +425,15 @@ namespace System.Net.Mail.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithInvalidLocalPartAtEnd_ShouldThrow()
|
||||
public void TryParseAddress_WithInvalidLocalPartAtEnd_ShouldThrow()
|
||||
{
|
||||
Assert.Throws<FormatException>(() => { MailAddressParser.ParseAddress("[test][email protected]"); });
|
||||
Assert.Throws<FormatException>(() => { MailAddressParser.TryParseAddress("[test][email protected]", out ParseAddressInfo _, true); });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAddress_WithCommaButNoQuotes_ShouldReadCorrectly()
|
||||
public void TryParseAddress_WithCommaButNoQuotes_ShouldReadCorrectly()
|
||||
{
|
||||
MailAddress result = MailAddressParser.ParseAddress("unqouted, comma display username@domain");
|
||||
Assert.True(MailAddressParser.TryParseAddress("unqouted, comma display username@domain", out ParseAddressInfo result, throwExceptionIfFail: true));
|
||||
|
||||
Assert.Equal("username", result.User);
|
||||
Assert.Equal("domain", result.Host);
|
||||
|
||||
Reference in new issue
Block a user