Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
using System.Linq;
using System.Reflection;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using Xunit;
using Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup;
using Microsoft.Data.SqlClient.Tests.Common.Fixtures;

namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted
{
public sealed class ConversionTests : IDisposable, IClassFixture<ColumnMasterKeyCertificateFixture>
public sealed class ConversionTests : IDisposable, IClassFixture<ConversionTestFixture>
{

private const string IdentityColumnName = "IdentityColumn";
Expand All @@ -29,9 +27,7 @@ public sealed class ConversionTests : IDisposable, IClassFixture<ColumnMasterKey
private const decimal SmallMoneyMinValue = -214748.3648M;
private const int MaxLength = 10000;
private int NumberOfRows = DataTestUtility.EnclaveEnabled ? 10 : 100;
private ColumnEncryptionKey columnEncryptionKey;
private SqlColumnEncryptionCertificateStoreProvider certStoreProvider = new SqlColumnEncryptionCertificateStoreProvider();
private List<DbObject> _databaseObjects = new List<DbObject>();
private readonly ColumnEncryptionKey columnEncryptionKey;
private List<string> _tables = new();

private class ColumnMetaData
Expand All @@ -52,33 +48,12 @@ public ColumnMetaData(SqlDbType columnType, int columnSize, int precision, int s
public bool UseMax { get; set; }
}

public ConversionTests(ColumnMasterKeyCertificateFixture fixture)
public ConversionTests(ConversionTestFixture fixture)
{
X509Certificate2 certificate = fixture.ColumnMasterKeyCertificate;
ColumnMasterKey columnMasterKey1 = new CspColumnMasterKey(
DatabaseHelper.GenerateUniqueName("CMK"),
certificate.Thumbprint,
certStoreProvider,
DataTestUtility.EnclaveEnabled);
_databaseObjects.Add(columnMasterKey1);

columnEncryptionKey = new ColumnEncryptionKey(
DatabaseHelper.GenerateUniqueName("CEK"),
columnMasterKey1,
certStoreProvider);
_databaseObjects.Add(columnEncryptionKey);

foreach (string connectionStr in DataTestUtility.AEConnStringsSetup)
{
var connectionString = new SqlConnectionStringBuilder(connectionStr);
connectionString.ConnectTimeout = Math.Max(connectionString.ConnectTimeout, 30); // The AE tests often fail with a connect timeout in this constructor. Making sure we have a reasonable timeout.

using (SqlConnection sqlConnection = new SqlConnection(connectionString.ConnectionString))
{
sqlConnection.Open();
_databaseObjects.ForEach(o => o.Create(sqlConnection));
}
}
// The Column Master Key and Column Encryption Key are created once per test class by
// the fixture, rather than once per test case, to avoid rapidly exhausting SQL Server's
// per-database identifier allocator (error 3807) on long-lived shared databases.
columnEncryptionKey = fixture.ColumnEncryptionKey;
}

[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE))]
Expand Down Expand Up @@ -621,7 +596,7 @@ private void AdjustSizePrecisionAndScale(ref ColumnMetaData largeColumnMeta, ref

if (TypeHasSize(largeColumnMeta.ColumnType))
{
// 20% of the time use (max) as the length.
// 20% of the time use (max) as the length.
largeColumnMeta.UseMax = (largeColumnMeta.ColumnType is SqlDbType.VarChar ||
largeColumnMeta.ColumnType is SqlDbType.NVarChar ||
largeColumnMeta.ColumnType is SqlDbType.VarBinary) &&
Expand Down Expand Up @@ -683,7 +658,7 @@ largeColumnMeta.ColumnType is SqlDbType.NVarChar ||
{
smallColumnMeta.Precision = 0;

// For Time / DateTime2 / DateTimeOffset types, actual scale is set to 7 when parameter.scale is zero.
// For Time / DateTime2 / DateTimeOffset types, actual scale is set to 7 when parameter.scale is zero.
// Active Issue in SQLParameter.cs when user wants to specify zero as the actual scale.
smallColumnMeta.Scale = random.Next(minScale, largeColumnMeta.Scale);
}
Expand Down Expand Up @@ -1354,7 +1329,7 @@ private void SetParamSizeScalePrecision(ref SqlParameter param, ColumnMetaData c

public void Dispose()
{
_databaseObjects.Reverse();
// Only the per-test tables are dropped here; the CMK/CEK are owned by the class fixture.
foreach (string connectionStr in DataTestUtility.AEConnStringsSetup)
{
using (SqlConnection sqlConnection = new SqlConnection(connectionStr))
Expand All @@ -1364,7 +1339,6 @@ public void Dispose()
{
DropTableIfExists(sqlConnection, table);
}
_databaseObjects.ForEach(o => o.Drop(sqlConnection));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup;
using Microsoft.Data.SqlClient.Tests.Common.Fixtures;

namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted
{
/// <summary>
/// Class fixture for <see cref="ConversionTests"/>. Creates a single Column Master Key (CMK) and
/// Column Encryption Key (CEK) once for the whole test class instead of once per test case.
/// </summary>
/// <remarks>
/// xUnit constructs a test class instance for every test case, so creating the keys in the
/// <see cref="ConversionTests"/> constructor issued a <c>CREATE COLUMN MASTER KEY</c> and
/// <c>CREATE COLUMN ENCRYPTION KEY</c> for each of the dozens of cases in the class. On a
/// long-lived shared database that rapidly advances SQL Server's internal per-database
/// identifier allocator, which is never reclaimed on <c>DROP</c>, eventually causing error 3807
/// ("Create failed because all available identifiers have been exhausted"). Creating the keys
/// once per class via <see cref="Xunit.IClassFixture{TFixture}"/> reduces that churn by roughly
/// two orders of magnitude.
/// </remarks>
public sealed class ConversionTestFixture : ColumnMasterKeyCertificateFixture
{
private readonly ColumnMasterKey _columnMasterKey;
private readonly SqlColumnEncryptionCertificateStoreProvider _certStoreProvider =
new SqlColumnEncryptionCertificateStoreProvider();
Comment thread
paulmedynski marked this conversation as resolved.

/// <summary>
/// The single Column Encryption Key shared by all cases in <see cref="ConversionTests"/>.
/// </summary>
public ColumnEncryptionKey ColumnEncryptionKey { get; }

public ConversionTestFixture()
{
_columnMasterKey = new CspColumnMasterKey(
DatabaseHelper.GenerateUniqueName("CMK"),
ColumnMasterKeyCertificate.Thumbprint,
_certStoreProvider,
DataTestUtility.EnclaveEnabled);

ColumnEncryptionKey = new ColumnEncryptionKey(
DatabaseHelper.GenerateUniqueName("CEK"),
_columnMasterKey,
_certStoreProvider);

foreach (string connectionStr in DataTestUtility.AEConnStringsSetup)
{
SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder(connectionStr);
// The AE setup often fails with a connect timeout here; ensure a reasonable minimum.
connectionString.ConnectTimeout = Math.Max(connectionString.ConnectTimeout, 30);

using SqlConnection sqlConnection = new SqlConnection(connectionString.ConnectionString);
sqlConnection.Open();
_columnMasterKey.Create(sqlConnection);
ColumnEncryptionKey.Create(sqlConnection);
}
}

protected override void Dispose(bool disposing)
{
try
{
foreach (string connectionStr in DataTestUtility.AEConnStringsSetup)
{
SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder(connectionStr);
// Match the constructor's minimum timeout; AE teardown is prone to connect timeouts,
// and skipping cleanup would leave the shared CMK/CEK behind.
connectionString.ConnectTimeout = Math.Max(connectionString.ConnectTimeout, 30);

// Key drops are best-effort: a failure here must not prevent the base fixture
// from removing the test certificate from the certificate store (see finally).
try
{
using SqlConnection sqlConnection = new SqlConnection(connectionString.ConnectionString);
sqlConnection.Open();
ColumnEncryptionKey.Drop(sqlConnection);
_columnMasterKey.Drop(sqlConnection);
}
catch (Exception ex)
{
Console.WriteLine(
$"ConversionTestFixture: failed to drop keys on '{connectionString.DataSource}': {ex.Message}");
}
}
}
finally
{
base.Dispose(disposing);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@
<Compile Include="AlwaysEncrypted\SqlBulkCopyTruncation.cs" />
<Compile Include="AlwaysEncrypted\SqlNullValues.cs" />
<Compile Include="AlwaysEncrypted\TestFixtures\AzureKeyVaultKeyFixture.cs" />
<Compile Include="AlwaysEncrypted\TestFixtures\ConversionTestFixture.cs" />
<Compile Include="AlwaysEncrypted\TestFixtures\DatabaseHelper.cs" />
<Compile Include="AlwaysEncrypted\TestFixtures\Setup\AkvColumnMasterKey.cs" />
<Compile Include="AlwaysEncrypted\TestFixtures\Setup\AKVTestTable.cs" />
Expand Down
Loading