// 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 GetValid_Address() { // inputAddress, address, displayName, host, toString(), user yield return new object[] { " foo@example.com ", "foo@example.com", string.Empty, "example.com", "foo@example.com", "foo" }; yield return new object[] { "Mr. Foo Bar ", "foo@example.com", "Mr. Foo Bar", "example.com", "\"Mr. Foo Bar\" ", "foo" }; yield return new object[] { "FooBar ", "foo@example.com", "FooBar", "example.com", "\"FooBar\" ", "foo" }; yield return new object[] { "\"FooBar\"foo@example.com ", "foo@example.com", "FooBar", "example.com", "\"FooBar\" ", "foo" }; yield return new object[] { "\" FooBar \"< foo@example.com >", "foo@example.com", " FooBar ", "example.com", "\" FooBar \" ", "foo" }; yield return new object[] { "", "foo@example.com", string.Empty, "example.com", "foo@example.com", "foo" }; yield return new object[] { " < foo@example.com >", "foo@example.com", string.Empty, "example.com", "foo@example.com", "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(() => new MailAddress(null)); Assert.Throws(() => _ = MailAddress.TryCreate(null, out MailAddress _)); } [Fact] public void TestConstructorWithEmptyString() { AssertExtensions.Throws("address", () => new MailAddress("")); AssertExtensions.Throws("address", () => _ = MailAddress.TryCreate("", out MailAddress _)); } public static IEnumerable GetInvalid_Address() { yield return new object[] { "Mr. Foo Bar" }; yield return new object[] { "foo@b@ar" }; yield return new object[] { "Mr. Foo Bar Whatever@You@Want" }; yield return new object[] { "Mr. F@@ Bar 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(() => new MailAddress(invalidAddress)); } public static IEnumerable GetInvalid_AddressDisplayName() { yield return new object[] { " WhatEver", " Mr. Foo Bar " }; yield return new object[] { "Mr. Far Bar Whatever", "BarFoo" }; } [Theory] [MemberData(nameof(GetInvalid_AddressDisplayName))] public void TestInvalidAddressInConstructor_AddressDisplayName(string invalidAddress, string displayName) { Assert.Throws(() => new MailAddress(invalidAddress, displayName)); } public static IEnumerable GetValid_AddressDisplayName() { // inputAddress, inputDisplayName, address, displayName, host, toString(), user yield return new object[] { " foo@example.com ", null, "foo@example.com", string.Empty, "example.com", "foo@example.com", "foo" }; yield return new object[] { "Mr. Far Bar ", "BarFoo", "foo@example.com", "BarFoo", "example.com", "\"BarFoo\" ", "foo" }; yield return new object[] { "Mr. Far Bar ", string.Empty, "foo@example.com", "Mr. Far Bar", "example.com", "\"Mr. Far Bar\" ", "foo" }; yield return new object[] { "Mr. Far Bar ", null, "foo@example.com", "Mr. Far Bar", "example.com", "\"Mr. Far Bar\" ", "foo" }; yield return new object[] { "Mr. Far Bar ", " ", "foo@example.com", " ", "example.com", "\" \" ", "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 GetAddress_DisplayNamePrecedence() { // inputAddress, displayName yield return new object[] { "Hola ", "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 GetAddressDisplayName_DisplayNamePrecedence() { // inputAddress, inputDisplayName, displayName yield return new object[] { "Hola ", "Adios", "Adios" }; yield return new object[] { "Hola ", "", "Hola" }; yield return new object[] { "", "", "" }; } [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\" "); Assert.True(MailAddress.TryCreate("\"Hola\" ", out MailAddress _)); } [Fact] public void Address_QuoteNotFirst() { Assert.Throws(() => new MailAddress("H\"ola\" ")); Assert.False(MailAddress.TryCreate("H\"ola\" ", out MailAddress _)); } [Fact] public void Address_NoClosingQuote() { Assert.Throws(() => new MailAddress("\"Hola ")); Assert.False(MailAddress.TryCreate("\"Hola ", out MailAddress _)); } [Fact] public void Address_NoUser() { Assert.Throws(() => new MailAddress("Hola <@bar.com>")); Assert.False(MailAddress.TryCreate("Hola <@bar.com>", out MailAddress _)); } [Fact] public void Address_NoUserNoHost() { Assert.Throws(() => new MailAddress("Hola <@>")); Assert.False(MailAddress.TryCreate("Hola <@>", out MailAddress _)); } [Fact] public void MailAddress_AllMembers() { MailAddress address = new MailAddress("foo@example.com", "Mr. Foo Bar"); Assert.Equal("foo@example.com", 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\" ", address.ToString()); } [Fact] public void EqualsTest() { var n = new MailAddress("Mr. Bar "); var n2 = new MailAddress("a@example.com", "Mr. Bar"); Assert.Equal(n, n2); } [Fact] public void EqualsTest2() { var n = new MailAddress("Mr. Bar "); var n2 = new MailAddress("MR. BAR "); 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); } } }