improve test dynamic certificate generation (#41659)
authorTomas Weinfurt <tweinfurt@yahoo.com>
Fri, 4 Sep 2020 18:48:21 +0000 (11:48 -0700)
committerGitHub <noreply@github.com>
Fri, 4 Sep 2020 18:48:21 +0000 (11:48 -0700)
* improve test dynamic certificate generation

* feedback from review

src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamNetworkStreamTest.cs
src/libraries/System.Net.Security/tests/FunctionalTests/TestHelper.cs

index 14d61a3..258b8a1 100644 (file)
@@ -16,19 +16,15 @@ namespace System.Net.Security.Tests
 {
     using Configuration = System.Net.Test.Common.Configuration;
 
-    public class SslStreamNetworkStreamTest : IDisposable
+    public class SslStreamNetworkStreamTest
     {
-        private readonly X509Certificate2 _serverCert;
-        private readonly X509Certificate2Collection _serverChain;
+        private static readonly X509Certificate2 _serverCert;
+        private static readonly X509Certificate2Collection _serverChain;
 
-        public SslStreamNetworkStreamTest()
+        static SslStreamNetworkStreamTest()
         {
-            (_serverCert, _serverChain) = TestHelper.GenerateCertificates("localhost", this.GetType().Name);
-        }
-
-        public void Dispose()
-        {
-            TestHelper.CleanupCertificates(this.GetType().Name);
+            TestHelper.CleanupCertificates(nameof(SslStreamNetworkStreamTest));
+            (_serverCert, _serverChain) = TestHelper.GenerateCertificates("localhost", nameof(SslStreamNetworkStreamTest));
         }
 
         [Fact]
index b16bcac..97e3a4a 100644 (file)
@@ -7,6 +7,7 @@ using System.Net.Test.Common;
 using System.Security.Cryptography;
 using System.Security.Cryptography.X509Certificates;
 using System.Security.Cryptography.X509Certificates.Tests.Common;
+using System.Runtime.CompilerServices;
 using System.Text;
 
 namespace System.Net.Security.Tests
@@ -107,7 +108,8 @@ namespace System.Net.Security.Tests
             }
             catch { };
         }
-        internal static (X509Certificate2 certificate, X509Certificate2Collection) GenerateCertificates(string targetName, string? testName = null)
+
+        internal static (X509Certificate2 certificate, X509Certificate2Collection) GenerateCertificates(string targetName, [CallerMemberName] string? testName = null)
         {
             if (PlatformDetection.IsWindows && testName != null)
             {