Skip to content

Commit 9ef9949

Browse files
committed
Review changes.
1 parent d218cde commit 9ef9949

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectionTestWithSSLCert/CertificateTestWithTdsServer.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.SqlServer.TDS.Servers;
1616
using Microsoft.Win32;
1717
using Xunit;
18+
#nullable enable
1819

1920
namespace Microsoft.Data.SqlClient.ManualTesting.Tests
2021
{
@@ -132,11 +133,7 @@ private void ConnectionTest(ConnectionTestParameters connectionTestParameters)
132133

133134
using TdsServer server = new TdsServer(new TdsServerArguments
134135
{
135-
#if NET9_0_OR_GREATER
136-
EncryptionCertificate = X509CertificateLoader.LoadPkcs12FromFile(s_fullPathToPfx, "nopassword", X509KeyStorageFlags.UserKeySet),
137-
#else
138-
EncryptionCertificate = new X509Certificate2(s_fullPathToPfx, "nopassword", X509KeyStorageFlags.UserKeySet),
139-
#endif
136+
EncryptionCertificate = GetEncryptionCertificate(s_fullPathToPfx, "nopassword", X509KeyStorageFlags.UserKeySet),
140137
EncryptionProtocols = connectionTestParameters.EncryptionProtocols,
141138
Encryption = connectionTestParameters.TdsEncryptionType,
142139
});
@@ -237,6 +234,22 @@ private static void RunPowershellScript(string script)
237234
}
238235
}
239236

237+
/// <summary>
238+
/// Loads the specified certificate.
239+
/// </summary>
240+
/// <param name="fileName">The full path of the certificate.</param>
241+
/// <param name="password">The certificate's password.</param>
242+
/// <param name="keyStorageFlags">Key storage flags to apply when loading the certificate</param>
243+
/// <returns>An <see cref="X509Certificate2"/> instance.</returns>
244+
private X509Certificate2 GetEncryptionCertificate(string fileName, string? password, X509KeyStorageFlags keyStorageFlags)
245+
{
246+
#if NET9_0_OR_GREATER
247+
return X509CertificateLoader.LoadPkcs12FromFile(fileName, password, keyStorageFlags);
248+
#else
249+
return new X509Certificate2(fileName, password, keyStorageFlags);
250+
#endif
251+
}
252+
240253
private void RemoveCertificate()
241254
{
242255
string thumbprint = File.ReadAllText(s_fullPathTothumbprint);
@@ -255,7 +268,7 @@ private void RemoveCertificate()
255268

256269
private static void RemoveForceEncryptionFromRegistryPath(string registryPath)
257270
{
258-
RegistryKey key = Registry.LocalMachine.OpenSubKey(registryPath, true);
271+
RegistryKey? key = Registry.LocalMachine.OpenSubKey(registryPath, true);
259272
key?.SetValue("ForceEncryption", 0, RegistryValueKind.DWord);
260273
key?.SetValue("Certificate", "", RegistryValueKind.String);
261274
ServiceController sc = new($"{s_instanceNamePrefix}{s_instanceName}");

src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionRoutingTests.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,9 @@ public void TransientFaultAtRoutedLocation_ShouldReturnToGateway(uint errorCode)
4848
Encrypt = false,
4949
};
5050
using SqlConnection connection = new(builder.ConnectionString);
51-
try
52-
{
53-
// Act
54-
connection.Open();
55-
}
56-
catch (Exception e)
57-
{
58-
Assert.Fail(e.Message);
59-
}
51+
52+
// Act
53+
connection.Open();
6054

6155
// Assert
6256
Assert.Equal(ConnectionState.Open, connection.State);

src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/AuthenticatingTdsServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public class AuthenticatingTdsServer : GenericTdsServer<AuthenticatingTdsServerA
1717
/// <summary>
1818
/// Initialization constructor
1919
/// </summary>
20-
public AuthenticatingTdsServer() :
21-
this(new AuthenticatingTdsServerArguments())
20+
public AuthenticatingTdsServer()
21+
: this(new AuthenticatingTdsServerArguments())
2222
{
2323
}
2424

0 commit comments

Comments
 (0)