From 59bb74c9ecc743a35ef5401f5b2baf2f44e986f2 Mon Sep 17 00:00:00 2001 From: Kenneth Pouncey Date: Fri, 9 Oct 2020 15:09:46 +0200 Subject: [PATCH] [wasm][net] System.Net.Mail should not throw PNSE for full assembly. (#42974) * [wasm][net] System.Net.Mail should not throw PNSE for full assembly. * Address review comment. Remove comment line * Address review comments. Remove redundant defs * Activate tests on CI - SmtpClient relevant tests are not supported * Add `[UnsupportedOSPlatform("browser")]` to SmtpClient * Add back System.Net.Mail Unit tests * Remove redundant $(TargetsBrowser) from ItemGroup * Add `[UnsupportedOSPlatform("browser")]` to SmtpClient methods and properties - It does not hurt to have this. * Remove unnecessary Unsupported attribute at the method level * Remove unnecessary Unsupported attribute at the method level * Modify message on skipped test. * Address review comments and remove string resource * Remove redundant AssemblyInfo per review comment * Remove condition attribute * Attribute is needed * Fix build issues --- .../System.Net.Mail/Directory.Build.props | 3 +- .../System.Net.Mail/ref/System.Net.Mail.cs | 1 + .../src/Resources/Strings.resx | 3 - .../src/System.Net.Mail.csproj | 70 ++++---- .../src/System/Net/Mail/SmtpClient.Browser.cs | 160 ++++++++++++++++++ .../src/System/Net/Mail/SmtpClient.cs | 2 + .../Net/Mail/SmtpFailedRecipientException.cs | 3 + .../tests/Functional/AssemblyInfo.cs | 1 - .../tests/Functional/LoggingTest.cs | 1 + .../tests/Functional/MailMessageTest.cs | 1 + .../Functional/SmtpClientCredentialsTest.cs | 3 +- .../tests/Functional/SmtpClientTest.cs | 1 + .../System.Net.Mail.Functional.Tests.csproj | 4 +- .../tests/Unit/AssemblyInfo.cs | 6 - .../EhloParseExtensionsTest.cs | 1 + .../Unit/System.Net.Mail.Unit.Tests.csproj | 39 +++-- 16 files changed, 233 insertions(+), 66 deletions(-) create mode 100644 src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.Browser.cs delete mode 100644 src/libraries/System.Net.Mail/tests/Unit/AssemblyInfo.cs diff --git a/src/libraries/System.Net.Mail/Directory.Build.props b/src/libraries/System.Net.Mail/Directory.Build.props index 4784967b7f8..5a1a0f181ca 100644 --- a/src/libraries/System.Net.Mail/Directory.Build.props +++ b/src/libraries/System.Net.Mail/Directory.Build.props @@ -3,6 +3,5 @@ Open true - browser - \ No newline at end of file + diff --git a/src/libraries/System.Net.Mail/ref/System.Net.Mail.cs b/src/libraries/System.Net.Mail/ref/System.Net.Mail.cs index e59605eb60f..5826634dd14 100644 --- a/src/libraries/System.Net.Mail/ref/System.Net.Mail.cs +++ b/src/libraries/System.Net.Mail/ref/System.Net.Mail.cs @@ -165,6 +165,7 @@ public enum MailPriority High = 2, } public delegate void SendCompletedEventHandler(object sender, System.ComponentModel.AsyncCompletedEventArgs e); + [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")] public partial class SmtpClient : System.IDisposable { public SmtpClient() { } diff --git a/src/libraries/System.Net.Mail/src/Resources/Strings.resx b/src/libraries/System.Net.Mail/src/Resources/Strings.resx index b04e8c6d8d1..a72e327927c 100644 --- a/src/libraries/System.Net.Mail/src/Resources/Strings.resx +++ b/src/libraries/System.Net.Mail/src/Resources/Strings.resx @@ -334,7 +334,4 @@ IIS delivery is not supported. - - System.Net.Mail is not supported on this platform. - diff --git a/src/libraries/System.Net.Mail/src/System.Net.Mail.csproj b/src/libraries/System.Net.Mail/src/System.Net.Mail.csproj index 0bc8b1c63c4..cad02d8a38a 100644 --- a/src/libraries/System.Net.Mail/src/System.Net.Mail.csproj +++ b/src/libraries/System.Net.Mail/src/System.Net.Mail.csproj @@ -5,10 +5,7 @@ enable - - SR.SystemNetMail_PlatformNotSupported - - + @@ -43,42 +40,22 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public int Port + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public bool UseDefaultCredentials + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public ICredentialsByHost? Credentials + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public int Timeout + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public ServicePoint ServicePoint + { + get => throw new PlatformNotSupportedException(); + } + + public SmtpDeliveryMethod DeliveryMethod + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public SmtpDeliveryFormat DeliveryFormat + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + public string? PickupDirectoryLocation + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + /// + /// Set to true if we need SSL + /// + public bool EnableSsl + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + /// + /// Certificates used by the client for establishing an SSL connection with the server. + /// + public X509CertificateCollection ClientCertificates => throw new PlatformNotSupportedException(); + + public string? TargetName + { + get => throw new PlatformNotSupportedException(); + set => throw new PlatformNotSupportedException(); + } + + private bool ServerSupportsEai => throw new PlatformNotSupportedException(); + + public void Send(string from, string recipients, string? subject, string? body) => throw new PlatformNotSupportedException(); + + public void Send(MailMessage message) => throw new PlatformNotSupportedException(); + + public void SendAsync(string from, string recipients, string? subject, string? body, object? userToken) => throw new PlatformNotSupportedException(); + + public void SendAsync(MailMessage message, object? userToken) => throw new PlatformNotSupportedException(); + + public void SendAsyncCancel() => throw new PlatformNotSupportedException(); + + //************* Task-based async public methods ************************* + public Task SendMailAsync(string from, string recipients, string? subject, string? body) => throw new PlatformNotSupportedException(); + + public Task SendMailAsync(MailMessage message) => throw new PlatformNotSupportedException(); + + public Task SendMailAsync(string from, string recipients, string? subject, string? body, CancellationToken cancellationToken) => throw new PlatformNotSupportedException(); + + public Task SendMailAsync(MailMessage message, CancellationToken cancellationToken) => throw new PlatformNotSupportedException(); + + protected void OnSendCompleted(AsyncCompletedEventArgs e) => throw new PlatformNotSupportedException(); + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { } + } +} diff --git a/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs b/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs index 9e87770828e..cceff5d15fa 100644 --- a/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs +++ b/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs @@ -6,6 +6,7 @@ using System.Globalization; using System.IO; using System.Net.NetworkInformation; +using System.Runtime.Versioning; using System.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; @@ -31,6 +32,7 @@ public enum SmtpDeliveryFormat International = 1, // SMTPUTF8 - Email Address Internationalization (EAI) } + [UnsupportedOSPlatform("browser")] public class SmtpClient : IDisposable { private string? _host; diff --git a/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpFailedRecipientException.cs b/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpFailedRecipientException.cs index 6919990791a..f3a5d300f0c 100644 --- a/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpFailedRecipientException.cs +++ b/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpFailedRecipientException.cs @@ -12,7 +12,10 @@ namespace System.Net.Mail public class SmtpFailedRecipientException : SmtpException, ISerializable { private readonly string? _failedRecipient; + +#pragma warning disable CS0649 // Browser - never assigned to internal bool fatal; +#pragma warning restore CS0649 public SmtpFailedRecipientException() : base() { } diff --git a/src/libraries/System.Net.Mail/tests/Functional/AssemblyInfo.cs b/src/libraries/System.Net.Mail/tests/Functional/AssemblyInfo.cs index 4f5a0ace5de..54c8a4d926e 100644 --- a/src/libraries/System.Net.Mail/tests/Functional/AssemblyInfo.cs +++ b/src/libraries/System.Net.Mail/tests/Functional/AssemblyInfo.cs @@ -4,4 +4,3 @@ using Xunit; [assembly: ActiveIssue("https://github.com/dotnet/runtime/issues/34690", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)] -[assembly: SkipOnMono("System.Net.Mail is not supported on wasm", TestPlatforms.Browser)] diff --git a/src/libraries/System.Net.Mail/tests/Functional/LoggingTest.cs b/src/libraries/System.Net.Mail/tests/Functional/LoggingTest.cs index 65c9d1c10ab..34f29d5b650 100644 --- a/src/libraries/System.Net.Mail/tests/Functional/LoggingTest.cs +++ b/src/libraries/System.Net.Mail/tests/Functional/LoggingTest.cs @@ -9,6 +9,7 @@ namespace System.Net.Mail.Tests { + [PlatformSpecific(~TestPlatforms.Browser)] // SmtpClient is not supported on Browser public class LoggingTest { [Fact] diff --git a/src/libraries/System.Net.Mail/tests/Functional/MailMessageTest.cs b/src/libraries/System.Net.Mail/tests/Functional/MailMessageTest.cs index 8b6382bb64e..1d7f0e236ef 100644 --- a/src/libraries/System.Net.Mail/tests/Functional/MailMessageTest.cs +++ b/src/libraries/System.Net.Mail/tests/Functional/MailMessageTest.cs @@ -146,6 +146,7 @@ public void SubjectAndEncodingTest() } [Fact] + [PlatformSpecific(~TestPlatforms.Browser)] // Not passing as internal System.Net.Mail.MailWriter stripped from build public void SentSpecialLengthMailAttachment_Base64Decode_Success() { // The special length follows pattern: (3N - 1) * 0x4400 + 1 diff --git a/src/libraries/System.Net.Mail/tests/Functional/SmtpClientCredentialsTest.cs b/src/libraries/System.Net.Mail/tests/Functional/SmtpClientCredentialsTest.cs index 824717a0d9c..64d884412b8 100644 --- a/src/libraries/System.Net.Mail/tests/Functional/SmtpClientCredentialsTest.cs +++ b/src/libraries/System.Net.Mail/tests/Functional/SmtpClientCredentialsTest.cs @@ -11,10 +11,11 @@ namespace System.Net.Mail.Functional.Tests { + [PlatformSpecific(~TestPlatforms.Browser)] // SmtpClient is not supported on Browser public class SmtpClientCredentialsTest { private readonly string UserName = "user"; - private readonly string Password = Guid.NewGuid().ToString(); + private readonly string Password = Guid.NewGuid().ToString(); [Fact] public void Credentials_Unset_Null() diff --git a/src/libraries/System.Net.Mail/tests/Functional/SmtpClientTest.cs b/src/libraries/System.Net.Mail/tests/Functional/SmtpClientTest.cs index 3d55574f0ca..9519ff7529f 100644 --- a/src/libraries/System.Net.Mail/tests/Functional/SmtpClientTest.cs +++ b/src/libraries/System.Net.Mail/tests/Functional/SmtpClientTest.cs @@ -19,6 +19,7 @@ namespace System.Net.Mail.Tests { + [PlatformSpecific(~TestPlatforms.Browser)] // SmtpClient is not supported on Browser public class SmtpClientTest : FileCleanupTestBase { private SmtpClient _smtp; diff --git a/src/libraries/System.Net.Mail/tests/Functional/System.Net.Mail.Functional.Tests.csproj b/src/libraries/System.Net.Mail/tests/Functional/System.Net.Mail.Functional.Tests.csproj index 83bb83a34f4..7519783c1cb 100644 --- a/src/libraries/System.Net.Mail/tests/Functional/System.Net.Mail.Functional.Tests.csproj +++ b/src/libraries/System.Net.Mail/tests/Functional/System.Net.Mail.Functional.Tests.csproj @@ -2,8 +2,6 @@ true $(NetCoreAppCurrent) - - true @@ -31,4 +29,4 @@ - \ No newline at end of file + diff --git a/src/libraries/System.Net.Mail/tests/Unit/AssemblyInfo.cs b/src/libraries/System.Net.Mail/tests/Unit/AssemblyInfo.cs deleted file mode 100644 index 2991b965dfe..00000000000 --- a/src/libraries/System.Net.Mail/tests/Unit/AssemblyInfo.cs +++ /dev/null @@ -1,6 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using Xunit; - -[assembly: SkipOnMono("System.Net.Mail is not supported on wasm", TestPlatforms.Browser)] diff --git a/src/libraries/System.Net.Mail/tests/Unit/SmtpConnectionTests/EhloParseExtensionsTest.cs b/src/libraries/System.Net.Mail/tests/Unit/SmtpConnectionTests/EhloParseExtensionsTest.cs index fa4333b952e..f0c3f437c18 100644 --- a/src/libraries/System.Net.Mail/tests/Unit/SmtpConnectionTests/EhloParseExtensionsTest.cs +++ b/src/libraries/System.Net.Mail/tests/Unit/SmtpConnectionTests/EhloParseExtensionsTest.cs @@ -5,6 +5,7 @@ namespace System.Net.Mail.Tests { + [PlatformSpecific(~TestPlatforms.Browser)] // SmtpClient is not supported on Browser public class EhloParseExtensionsTest { private SmtpConnection _smtpConnection; diff --git a/src/libraries/System.Net.Mail/tests/Unit/System.Net.Mail.Unit.Tests.csproj b/src/libraries/System.Net.Mail/tests/Unit/System.Net.Mail.Unit.Tests.csproj index 79709820e29..06a2e27ed64 100644 --- a/src/libraries/System.Net.Mail/tests/Unit/System.Net.Mail.Unit.Tests.csproj +++ b/src/libraries/System.Net.Mail/tests/Unit/System.Net.Mail.Unit.Tests.csproj @@ -2,11 +2,10 @@ true ../../src/Resources/Strings.resx - $(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix + $(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser annotations - @@ -19,7 +18,7 @@ - + @@ -36,16 +35,8 @@ Link="ProductionCode\MailAddress.cs" /> - - - - - - - - + + + + + + + + + + + + -- GitLab