Skip to content

Commit 9d06173

Browse files
committed
Address comment that we don't need a CspParameters object as part of the test arguments
* Move test arguments into property (the class was only used in a single location) * Cleanup test code * Tweak default provider discovery code to handle edge cases a bit better
1 parent 3dfd7ca commit 9d06173

File tree

3 files changed

+60
-55
lines changed

3 files changed

+60
-55
lines changed

src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/CspProviderExt.cs

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
67
using System.Linq;
78
using System.Security.Cryptography.X509Certificates;
89
using Xunit;
910
using System.Security.Cryptography;
1011
using Microsoft.Data.SqlClient.TestUtilities.Fixtures;
11-
#if !NETFRAMEWORK
12+
using Microsoft.Win32;
13+
14+
#if NET
1215
using System.Runtime.Versioning;
1316
#endif
1417

@@ -26,7 +29,7 @@ public class CspProviderExt
2629
{
2730
// [Fact(Skip="Run this in non-parallel mode")] or [ConditionalFact()]
2831
[Fact(Skip = "Failing in TCE")]
29-
public void TestRoundTripWithCSPAndCertStoreProvider()
32+
public void TestRoundTripWithCspAndCertStoreProvider()
3033
{
3134
using CspCertificateFixture cspCertificateFixture = new CspCertificateFixture();
3235

@@ -48,26 +51,75 @@ public void TestRoundTripWithCSPAndCertStoreProvider()
4851
}
4952

5053
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE))]
51-
[ClassData(typeof(AEConnectionStringProviderWithCspParameters))]
52-
public void TestEncryptDecryptWithCSP(string connectionString, CspParameters cspParameters)
54+
[MemberData(nameof(TestEncryptDecryptWithCsp_Data))]
55+
public void TestEncryptDecryptWithCsp(string connectionString, string providerName, int providerType)
5356
{
5457
string keyIdentifier = DataTestUtility.GetUniqueNameForSqlServer("CSP");
55-
CspParameters namedCspParameters = new CspParameters(cspParameters.ProviderType, cspParameters.ProviderName, keyIdentifier);
58+
CspParameters namedCspParameters = new CspParameters(providerType, providerName, keyIdentifier);
5659
using SQLSetupStrategyCspProvider sqlSetupStrategyCsp = new SQLSetupStrategyCspProvider(namedCspParameters);
5760

5861
using SqlConnection sqlConn = new(connectionString);
5962
sqlConn.Open();
6063

6164
// Test INPUT parameter on an encrypted parameter
62-
using SqlCommand sqlCommand = new(@$"SELECT CustomerId, FirstName, LastName FROM [{sqlSetupStrategyCsp.ApiTestTable.Name}] WHERE FirstName = @firstName",
63-
sqlConn, null, SqlCommandColumnEncryptionSetting.Enabled);
65+
string commandText = @$"SELECT CustomerId, FirstName, LastName " +
66+
@$"FROM [{sqlSetupStrategyCsp.ApiTestTable.Name}] " +
67+
@$"WHERE FirstName = @firstName";
68+
using SqlCommand sqlCommand = new(commandText, sqlConn, null, SqlCommandColumnEncryptionSetting.Enabled);
69+
6470
SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft");
6571
customerFirstParam.Direction = System.Data.ParameterDirection.Input;
6672

6773
using SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
6874
ValidateResultSet(sqlDataReader);
6975
}
7076

77+
public static IEnumerable<object[]> TestEncryptDecryptWithCsp_Data
78+
{
79+
get
80+
{
81+
const string providerRegistryKeyPath = @"SOFTWARE\Microsoft\Cryptography\Defaults\Provider";
82+
using RegistryKey defaultProviderRegistryKey = Registry.LocalMachine.OpenSubKey(providerRegistryKeyPath);
83+
if (defaultProviderRegistryKey is null)
84+
{
85+
// No test cases can be generated if the registry key doesn't exist.
86+
yield break;
87+
}
88+
89+
foreach (string subKeyName in defaultProviderRegistryKey.GetSubKeyNames())
90+
{
91+
// Skip inappropriate providers
92+
if (!subKeyName.Contains(@"RSA and AES"))
93+
{
94+
continue;
95+
}
96+
97+
// Open the provider
98+
using RegistryKey providerKey = defaultProviderRegistryKey.OpenSubKey(subKeyName);
99+
if (providerKey is null)
100+
{
101+
continue;
102+
}
103+
104+
// Read provider name
105+
string providerName = Path.GetFileName(providerKey.Name);
106+
107+
// Read provider type
108+
object providerTypeValue = providerKey.GetValue(@"Type");
109+
if (providerTypeValue is not int providerType)
110+
{
111+
continue;
112+
}
113+
114+
// Combine with AE connection strings
115+
foreach (string aeConnectionString in DataTestUtility.AEConnStrings)
116+
{
117+
yield return new object[] { aeConnectionString, providerName, providerType };
118+
}
119+
}
120+
}
121+
}
122+
71123
/// <summary>
72124
/// Validates that the results are the ones expected.
73125
/// </summary>

src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/TestFixtures/AEConnectionStringProviderWithCspParameters.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/Microsoft.Data.SqlClient/tests/ManualTests/Microsoft.Data.SqlClient.ManualTesting.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
<Compile Include="AlwaysEncrypted\End2EndSmokeTests.cs" />
4949
<Compile Include="AlwaysEncrypted\SqlBulkCopyTruncation.cs" />
5050
<Compile Include="AlwaysEncrypted\SqlNullValues.cs" />
51-
<Compile Include="AlwaysEncrypted\TestFixtures\AEConnectionStringProviderWithCspParameters.cs" />
5251
<Compile Include="AlwaysEncrypted\TestFixtures\AzureKeyVaultKeyFixture.cs" />
5352
<Compile Include="AlwaysEncrypted\TestFixtures\DatabaseHelper.cs" />
5453
<Compile Include="AlwaysEncrypted\TestFixtures\SQLSetupStrategy.cs" />

0 commit comments

Comments
 (0)