1515using Microsoft . SqlServer . TDS . Servers ;
1616using Microsoft . Win32 ;
1717using Xunit ;
18+ #nullable enable
1819
1920namespace 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 } ") ;
0 commit comments