mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-23 03:39:35 -10:00
* 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]>
253 lines
12 KiB
C#
253 lines
12 KiB
C#
// 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.
|
|
|
|
using System.Collections.Generic;
|
|
using Xunit;
|
|
|
|
namespace System.Net.Mail.Tests
|
|
{
|
|
public class MailAddressTest
|
|
{
|
|
public static IEnumerable<object[]> GetValid_Address()
|
|
{
|
|
// 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" };
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(GetValid_Address))]
|
|
public void TestConstructor_Address(string inputAddress, string address, string displayName, string host, string toString, string 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 _));
|
|
}
|
|
|
|
public static IEnumerable<object[]> GetInvalid_Address()
|
|
{
|
|
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" };
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(GetInvalid_Address))]
|
|
public void TestInvalidAddressInConstructo_Address(string invalidAddress)
|
|
{
|
|
Assert.Throws<FormatException>(() => new MailAddress(invalidAddress));
|
|
}
|
|
|
|
public static IEnumerable<object[]> GetInvalid_AddressDisplayName()
|
|
{
|
|
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 MailAddress_AllMembers()
|
|
{
|
|
MailAddress address = new MailAddress("[email protected]", "Mr. Foo Bar");
|
|
Assert.Equal("[email protected]", address.Address);
|
|
Assert.Equal("Mr. Foo Bar", address.DisplayName);
|
|
Assert.Equal("example.com", address.Host);
|
|
Assert.Equal("foo", address.User);
|
|
Assert.Equal("\"Mr. Foo Bar\" <[email protected]>", address.ToString());
|
|
}
|
|
|
|
[Fact]
|
|
public void EqualsTest()
|
|
{
|
|
var n = new MailAddress("Mr. Bar <[email protected]>");
|
|
var n2 = new MailAddress("[email protected]", "Mr. Bar");
|
|
Assert.Equal(n, n2);
|
|
}
|
|
|
|
[Fact]
|
|
public void EqualsTest2()
|
|
{
|
|
var n = new MailAddress("Mr. Bar <[email protected]>");
|
|
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);
|
|
}
|
|
}
|
|
}
|