Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop tables in Dispose() for ConversionTests #1251

Merged
merged 1 commit into from
Sep 8, 2021
Merged
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 @@ -34,6 +34,7 @@ public sealed class ConversionTests : IDisposable
private ColumnEncryptionKey columnEncryptionKey;
private SqlColumnEncryptionCertificateStoreProvider certStoreProvider = new SqlColumnEncryptionCertificateStoreProvider();
private List<DbObject> _databaseObjects = new List<DbObject>();
private List<string> _tables = new();

private class ColumnMetaData
{
Expand All @@ -55,7 +56,7 @@ public ColumnMetaData(SqlDbType columnType, int columnSize, int precision, int s

public ConversionTests()
{
if(certificate == null)
if (certificate == null)
{
certificate = CertificateUtility.CreateCertificate();
}
Expand Down Expand Up @@ -111,58 +112,49 @@ public void ConversionSmallerToLargerInsertAndSelect(string connString, SqlDbTyp
sqlConnectionEncrypted.Open();
sqlConnectionUnencrypted.Open();

try
// Select each value we just inserted with a predicate and verify that encrypted and unencrypted return the same result.
for (int i = 0; i < NumberOfRows; i++)
{
// Select each value we just inserted with a predicate and verify that encrypted and unencrypted return the same result.
for (int i = 0; i < NumberOfRows; i++)
object value;

// Use the retrieved values for DateTime2 and DateTimeOffset due to fractional insertion adjustment
if (smallColumnInfo.ColumnType is SqlDbType.DateTime2 || smallColumnInfo.ColumnType is SqlDbType.DateTimeOffset)
{
object value;
value = valuesToSelect[i];
}
else
{
value = rawValues[i];
}

// Use the retrieved values for DateTime2 and DateTimeOffset due to fractional insertion adjustment
if (smallColumnInfo.ColumnType is SqlDbType.DateTime2 || smallColumnInfo.ColumnType is SqlDbType.DateTimeOffset)
{
value = valuesToSelect[i];
}
else
using (SqlCommand cmdEncrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, encryptedTableName, FirstParamName), sqlConnectionEncrypted, null, SqlCommandColumnEncryptionSetting.Enabled))
using (SqlCommand cmdUnencrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, unencryptedTableName, FirstParamName), sqlConnectionUnencrypted, null, SqlCommandColumnEncryptionSetting.Disabled))
{
SqlParameter paramEncrypted = new SqlParameter();
paramEncrypted.ParameterName = FirstParamName;
paramEncrypted.SqlDbType = largeDbType;
SetParamSizeScalePrecision(ref paramEncrypted, largeColumnInfo);
paramEncrypted.Value = value;
cmdEncrypted.Parameters.Add(paramEncrypted);

SqlParameter paramUnencrypted = new SqlParameter();
paramUnencrypted.ParameterName = FirstParamName;
paramUnencrypted.SqlDbType = largeDbType;
SetParamSizeScalePrecision(ref paramUnencrypted, largeColumnInfo);
paramUnencrypted.Value = value;
cmdUnencrypted.Parameters.Add(paramUnencrypted);

using (SqlDataReader readerUnencrypted = cmdUnencrypted.ExecuteReader())
using (SqlDataReader readerEncrypted = cmdEncrypted.ExecuteReader())
{
value = rawValues[i];
}
// First check that we found some rows.
Assert.True(readerEncrypted.HasRows, @"We didn't find any rows.");

using (SqlCommand cmdEncrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, encryptedTableName, FirstParamName), sqlConnectionEncrypted, null, SqlCommandColumnEncryptionSetting.Enabled))
using (SqlCommand cmdUnencrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, unencryptedTableName, FirstParamName), sqlConnectionUnencrypted, null, SqlCommandColumnEncryptionSetting.Disabled))
{
SqlParameter paramEncrypted = new SqlParameter();
paramEncrypted.ParameterName = FirstParamName;
paramEncrypted.SqlDbType = largeDbType;
SetParamSizeScalePrecision(ref paramEncrypted, largeColumnInfo);
paramEncrypted.Value = value;
cmdEncrypted.Parameters.Add(paramEncrypted);

SqlParameter paramUnencrypted = new SqlParameter();
paramUnencrypted.ParameterName = FirstParamName;
paramUnencrypted.SqlDbType = largeDbType;
SetParamSizeScalePrecision(ref paramUnencrypted, largeColumnInfo);
paramUnencrypted.Value = value;
cmdUnencrypted.Parameters.Add(paramUnencrypted);

using (SqlDataReader readerUnencrypted = cmdUnencrypted.ExecuteReader())
using (SqlDataReader readerEncrypted = cmdEncrypted.ExecuteReader())
{
// First check that we found some rows.
Assert.True(readerEncrypted.HasRows, @"We didn't find any rows.");

// Now compare the result.
CompareResults(readerEncrypted, readerUnencrypted);
}
// Now compare the result.
CompareResults(readerEncrypted, readerUnencrypted);
}
}
}
finally
{
// DropTables
DropTableIfExists(sqlConnectionEncrypted, encryptedTableName);
DropTableIfExists(sqlConnectionUnencrypted, unencryptedTableName);
}
}
}

Expand Down Expand Up @@ -206,62 +198,52 @@ public void ConversionSmallerToLargerInsertAndSelectBulk(string connString, SqlD
sqlConnectionEncrypted.Open();
sqlConnectionUnencrypted.Open();

try
// Select each value we just inserted with a predicate and verify that encrypted and unencrypted return the same result.
for (int i = 0; i < NumberOfRows; i++)
{
// Select each value we just inserted with a predicate and verify that encrypted and unencrypted return the same result.
for (int i = 0; i < NumberOfRows; i++)
object value;

// Use the retrieved values for DateTime2 and DateTimeOffset due to fractional insertion adjustment
if (smallColumnInfo.ColumnType is SqlDbType.DateTime2 ||
smallColumnInfo.ColumnType is SqlDbType.DateTimeOffset ||
smallColumnInfo.ColumnType is SqlDbType.Char ||
smallColumnInfo.ColumnType is SqlDbType.NChar)
{
value = valuesToSelect[i];
}
else
{
object value;
value = rawValues[i];
}

// Use the retrieved values for DateTime2 and DateTimeOffset due to fractional insertion adjustment
if (smallColumnInfo.ColumnType is SqlDbType.DateTime2 ||
smallColumnInfo.ColumnType is SqlDbType.DateTimeOffset ||
smallColumnInfo.ColumnType is SqlDbType.Char ||
smallColumnInfo.ColumnType is SqlDbType.NChar)
{
value = valuesToSelect[i];
}
else
using (SqlCommand cmdEncrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, targetTableName, FirstParamName), sqlConnectionEncrypted, null, SqlCommandColumnEncryptionSetting.Enabled))
using (SqlCommand cmdUnencrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, witnessTableName, FirstParamName), sqlConnectionUnencrypted, null, SqlCommandColumnEncryptionSetting.Disabled))
{
SqlParameter paramEncrypted = new SqlParameter();
paramEncrypted.ParameterName = FirstParamName;
paramEncrypted.SqlDbType = largeDbType;
SetParamSizeScalePrecision(ref paramEncrypted, largeColumnInfo);
paramEncrypted.Value = value;
cmdEncrypted.Parameters.Add(paramEncrypted);

SqlParameter paramUnencrypted = new SqlParameter();
paramUnencrypted.ParameterName = FirstParamName;
paramUnencrypted.SqlDbType = largeDbType;
SetParamSizeScalePrecision(ref paramUnencrypted, largeColumnInfo);
paramUnencrypted.Value = value;
cmdUnencrypted.Parameters.Add(paramUnencrypted);

using (SqlDataReader readerUnencrypted = cmdUnencrypted.ExecuteReader())
using (SqlDataReader readerEncrypted = cmdEncrypted.ExecuteReader())
{
value = rawValues[i];
}
// First check that we found some rows.
Assert.True(readerEncrypted.HasRows, @"We didn't find any rows.");

using (SqlCommand cmdEncrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, targetTableName, FirstParamName), sqlConnectionEncrypted, null, SqlCommandColumnEncryptionSetting.Enabled))
using (SqlCommand cmdUnencrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, witnessTableName, FirstParamName), sqlConnectionUnencrypted, null, SqlCommandColumnEncryptionSetting.Disabled))
{
SqlParameter paramEncrypted = new SqlParameter();
paramEncrypted.ParameterName = FirstParamName;
paramEncrypted.SqlDbType = largeDbType;
SetParamSizeScalePrecision(ref paramEncrypted, largeColumnInfo);
paramEncrypted.Value = value;
cmdEncrypted.Parameters.Add(paramEncrypted);

SqlParameter paramUnencrypted = new SqlParameter();
paramUnencrypted.ParameterName = FirstParamName;
paramUnencrypted.SqlDbType = largeDbType;
SetParamSizeScalePrecision(ref paramUnencrypted, largeColumnInfo);
paramUnencrypted.Value = value;
cmdUnencrypted.Parameters.Add(paramUnencrypted);

using (SqlDataReader readerUnencrypted = cmdUnencrypted.ExecuteReader())
using (SqlDataReader readerEncrypted = cmdEncrypted.ExecuteReader())
{
// First check that we found some rows.
Assert.True(readerEncrypted.HasRows, @"We didn't find any rows.");

// Now compare the result.
CompareResults(readerEncrypted, readerUnencrypted);
}
// Now compare the result.
CompareResults(readerEncrypted, readerUnencrypted);
}
}
}
finally
{
// DropTables
DropTableIfExists(sqlConnectionEncrypted, targetTableName);
DropTableIfExists(sqlConnectionUnencrypted, witnessTableName);
DropTableIfExists(sqlConnectionUnencrypted, originTableName);
}
}
}

Expand Down Expand Up @@ -293,44 +275,35 @@ public void TestOutOfRangeValues(string connString, SqlDbType currentDbType)
sqlConnectionEncrypted.Open();
sqlConnectionUnencrypted.Open();

try
foreach (ValueErrorTuple tuple in valueList)
{
foreach (ValueErrorTuple tuple in valueList)
using (SqlCommand sqlCmd = new SqlCommand(String.Format("INSERT INTO [{0}] VALUES ({1})", encryptedTableName, FirstParamName), sqlConnectionEncrypted, null, SqlCommandColumnEncryptionSetting.Enabled))
{
using (SqlCommand sqlCmd = new SqlCommand(String.Format("INSERT INTO [{0}] VALUES ({1})", encryptedTableName, FirstParamName), sqlConnectionEncrypted, null, SqlCommandColumnEncryptionSetting.Enabled))
{
SqlParameter param = new SqlParameter();
param.ParameterName = FirstParamName;
param.SqlDbType = currentColumnInfo.ColumnType;
SetParamSizeScalePrecision(ref param, currentColumnInfo);
param.Value = tuple.Value;
sqlCmd.Parameters.Add(param);

ExecuteAndCheckForError(sqlCmd, tuple.ExpectsError);
}
SqlParameter param = new SqlParameter();
param.ParameterName = FirstParamName;
param.SqlDbType = currentColumnInfo.ColumnType;
SetParamSizeScalePrecision(ref param, currentColumnInfo);
param.Value = tuple.Value;
sqlCmd.Parameters.Add(param);

// Add same value to the unencrypted table
using (SqlCommand sqlCmd = new SqlCommand(String.Format("INSERT INTO [{0}] VALUES ({1})", unencryptedTableName, FirstParamName), sqlConnectionUnencrypted, null, SqlCommandColumnEncryptionSetting.Disabled))
{
SqlParameter param = new SqlParameter();
param.ParameterName = FirstParamName;
param.SqlDbType = currentColumnInfo.ColumnType;
SetParamSizeScalePrecision(ref param, currentColumnInfo);
param.Value = tuple.Value;
sqlCmd.Parameters.Add(param);

ExecuteAndCheckForError(sqlCmd, tuple.ExpectsError);
}
ExecuteAndCheckForError(sqlCmd, tuple.ExpectsError);
}

// Add same value to the unencrypted table
using (SqlCommand sqlCmd = new SqlCommand(String.Format("INSERT INTO [{0}] VALUES ({1})", unencryptedTableName, FirstParamName), sqlConnectionUnencrypted, null, SqlCommandColumnEncryptionSetting.Disabled))
{
SqlParameter param = new SqlParameter();
param.ParameterName = FirstParamName;
param.SqlDbType = currentColumnInfo.ColumnType;
SetParamSizeScalePrecision(ref param, currentColumnInfo);
param.Value = tuple.Value;
sqlCmd.Parameters.Add(param);

ExecuteAndCheckForError(sqlCmd, tuple.ExpectsError);
}

CompareTables(connString, encryptedTableName, unencryptedTableName);
}
finally
{
DropTableIfExists(sqlConnectionEncrypted, encryptedTableName);
DropTableIfExists(sqlConnectionUnencrypted, unencryptedTableName);
}
}
}

Expand Down Expand Up @@ -1305,6 +1278,7 @@ private void CreateTable(string connString, ColumnMetaData columnMeta, string ta
command.ExecuteNonQuery();
}
}
_tables.Add(tableName);
}

/// <summary>
Expand Down Expand Up @@ -1351,6 +1325,10 @@ public void Dispose()
using (SqlConnection sqlConnection = new SqlConnection(connectionStr))
{
sqlConnection.Open();
foreach (string table in _tables)
{
DropTableIfExists(sqlConnection, table);
}
_databaseObjects.ForEach(o => o.Drop(sqlConnection));
}
}
Expand Down