2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
- using System ;
5
+ using System . Collections . Generic ;
6
+ using System . IO ;
6
7
using System . Linq ;
7
8
using System . Security . Cryptography . X509Certificates ;
8
9
using Xunit ;
9
10
using System . Security . Cryptography ;
10
11
using Microsoft . Data . SqlClient . TestUtilities . Fixtures ;
11
- #if ! NETFRAMEWORK
12
+ using Microsoft . Win32 ;
13
+
14
+ #if NET
12
15
using System . Runtime . Versioning ;
13
16
#endif
14
17
@@ -26,7 +29,7 @@ public class CspProviderExt
26
29
{
27
30
// [Fact(Skip="Run this in non-parallel mode")] or [ConditionalFact()]
28
31
[ Fact ( Skip = "Failing in TCE" ) ]
29
- public void TestRoundTripWithCSPAndCertStoreProvider ( )
32
+ public void TestRoundTripWithCspAndCertStoreProvider ( )
30
33
{
31
34
using CspCertificateFixture cspCertificateFixture = new CspCertificateFixture ( ) ;
32
35
@@ -48,26 +51,75 @@ public void TestRoundTripWithCSPAndCertStoreProvider()
48
51
}
49
52
50
53
[ 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 )
53
56
{
54
57
string keyIdentifier = DataTestUtility . GetUniqueNameForSqlServer ( "CSP" ) ;
55
- CspParameters namedCspParameters = new CspParameters ( cspParameters . ProviderType , cspParameters . ProviderName , keyIdentifier ) ;
58
+ CspParameters namedCspParameters = new CspParameters ( providerType , providerName , keyIdentifier ) ;
56
59
using SQLSetupStrategyCspProvider sqlSetupStrategyCsp = new SQLSetupStrategyCspProvider ( namedCspParameters ) ;
57
60
58
61
using SqlConnection sqlConn = new ( connectionString ) ;
59
62
sqlConn . Open ( ) ;
60
63
61
64
// 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
+
64
70
SqlParameter customerFirstParam = sqlCommand . Parameters . AddWithValue ( @"firstName" , @"Microsoft" ) ;
65
71
customerFirstParam . Direction = System . Data . ParameterDirection . Input ;
66
72
67
73
using SqlDataReader sqlDataReader = sqlCommand . ExecuteReader ( ) ;
68
74
ValidateResultSet ( sqlDataReader ) ;
69
75
}
70
76
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
+
71
123
/// <summary>
72
124
/// Validates that the results are the ones expected.
73
125
/// </summary>
0 commit comments