Skip to content

Commit

Permalink
Fix RemoteCertificateNameMismatchErrorTest
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelZ committed Dec 3, 2024
1 parent 54f8a1d commit df665e5
Showing 1 changed file with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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<SqlException>(() => 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<AuthenticationException>(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<System.ComponentModel.Win32Exception>(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<AuthenticationException>(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)
Expand Down

0 comments on commit df665e5

Please sign in to comment.