From df665e5f4de12746c5b38580775ce80f78bccb25 Mon Sep 17 00:00:00 2001 From: Michel Zehnder Date: Tue, 3 Dec 2024 13:57:50 +0100 Subject: [PATCH] Fix RemoteCertificateNameMismatchErrorTest --- .../CertificateTest.cs | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectionTestWithSSLCert/CertificateTest.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectionTestWithSSLCert/CertificateTest.cs index e501a910c7..ea3278b4c2 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectionTestWithSSLCert/CertificateTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectionTestWithSSLCert/CertificateTest.cs @@ -9,6 +9,7 @@ using System.Linq; using System.Net; using System.Net.Sockets; +using System.Runtime.InteropServices; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; using System.ServiceProcess; @@ -95,7 +96,6 @@ private static bool IsLocalHost() private static bool AreConnStringsSetup() => DataTestUtility.AreConnStringsSetup(); private static bool IsNotAzureServer() => DataTestUtility.IsNotAzureServer(); - private static bool UseManagedSNIOnWindows() => DataTestUtility.UseManagedSNIOnWindows; private static bool IsAdminOnWindows() => DataTestUtility.IsAdminOnWindows; // [ActiveIssue("31754")] @@ -172,24 +172,31 @@ public void OpeningConnectionWitHNICTest() } } - [ActiveIssue("31754")] - [ConditionalFact(nameof(AreConnStringsSetup), nameof(UseManagedSNIOnWindows), nameof(IsNotAzureServer), nameof(IsLocalHost), nameof(IsAdminOnWindows), Skip = "For some reason does not throw on CI")] - [PlatformSpecific(TestPlatforms.Windows)] + [ConditionalFact(nameof(AreConnStringsSetup), nameof(IsNotAzureServer), nameof(IsLocalHost))] public void RemoteCertificateNameMismatchErrorTest() { SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionString) { - DataSource = GetLocalIpAddress(), - Encrypt = SqlConnectionEncryptOption.Mandatory, - HostNameInCertificate = "BadHostName" + DataSource = GetLocalIpAddress(), Encrypt = SqlConnectionEncryptOption.Mandatory, HostNameInCertificate = "BadHostName" }; using SqlConnection connection = new(builder.ConnectionString); SqlException exception = Assert.Throws(() => connection.Open()); - Assert.StartsWith("A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught)", exception.Message); - Assert.Equal(20, exception.Class); - Assert.IsType(exception.InnerException); - Assert.StartsWith("Certificate name mismatch. The provided 'DataSource' or 'HostNameInCertificate' does not match the name in the certificate.", exception.InnerException.Message); _testOutputHelper.WriteLine(exception.Message); + + if (DataTestUtility.IsUsingNativeSNI()) + { + Assert.StartsWith("A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate's CN name does not match the passed value.)", exception.Message); + Assert.Equal(20, exception.Class); + Assert.IsType(exception.InnerException); + Assert.StartsWith("The certificate's CN name does not match the passed value", exception.InnerException.Message); + } + else + { + Assert.StartsWith("A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught)", exception.Message); + Assert.Equal(20, exception.Class); + Assert.IsType(exception.InnerException); + Assert.StartsWith("Certificate name mismatch. The provided 'DataSource' or 'HostNameInCertificate' does not match the name in the certificate.", exception.InnerException.Message); + } } private static void CreateValidCertificate(string script)