Skip to content

Commit aea4bbe

Browse files
author
Johnny Pham
authored
Update ConversionTests.cs (#1251)
1 parent fbfd5d7 commit aea4bbe

File tree

1 file changed

+101
-123
lines changed

1 file changed

+101
-123
lines changed

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

Lines changed: 101 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public sealed class ConversionTests : IDisposable
3434
private ColumnEncryptionKey columnEncryptionKey;
3535
private SqlColumnEncryptionCertificateStoreProvider certStoreProvider = new SqlColumnEncryptionCertificateStoreProvider();
3636
private List<DbObject> _databaseObjects = new List<DbObject>();
37+
private List<string> _tables = new();
3738

3839
private class ColumnMetaData
3940
{
@@ -55,7 +56,7 @@ public ColumnMetaData(SqlDbType columnType, int columnSize, int precision, int s
5556

5657
public ConversionTests()
5758
{
58-
if(certificate == null)
59+
if (certificate == null)
5960
{
6061
certificate = CertificateUtility.CreateCertificate();
6162
}
@@ -111,58 +112,49 @@ public void ConversionSmallerToLargerInsertAndSelect(string connString, SqlDbTyp
111112
sqlConnectionEncrypted.Open();
112113
sqlConnectionUnencrypted.Open();
113114

114-
try
115+
// Select each value we just inserted with a predicate and verify that encrypted and unencrypted return the same result.
116+
for (int i = 0; i < NumberOfRows; i++)
115117
{
116-
// Select each value we just inserted with a predicate and verify that encrypted and unencrypted return the same result.
117-
for (int i = 0; i < NumberOfRows; i++)
118+
object value;
119+
120+
// Use the retrieved values for DateTime2 and DateTimeOffset due to fractional insertion adjustment
121+
if (smallColumnInfo.ColumnType is SqlDbType.DateTime2 || smallColumnInfo.ColumnType is SqlDbType.DateTimeOffset)
118122
{
119-
object value;
123+
value = valuesToSelect[i];
124+
}
125+
else
126+
{
127+
value = rawValues[i];
128+
}
120129

121-
// Use the retrieved values for DateTime2 and DateTimeOffset due to fractional insertion adjustment
122-
if (smallColumnInfo.ColumnType is SqlDbType.DateTime2 || smallColumnInfo.ColumnType is SqlDbType.DateTimeOffset)
123-
{
124-
value = valuesToSelect[i];
125-
}
126-
else
130+
using (SqlCommand cmdEncrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, encryptedTableName, FirstParamName), sqlConnectionEncrypted, null, SqlCommandColumnEncryptionSetting.Enabled))
131+
using (SqlCommand cmdUnencrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, unencryptedTableName, FirstParamName), sqlConnectionUnencrypted, null, SqlCommandColumnEncryptionSetting.Disabled))
132+
{
133+
SqlParameter paramEncrypted = new SqlParameter();
134+
paramEncrypted.ParameterName = FirstParamName;
135+
paramEncrypted.SqlDbType = largeDbType;
136+
SetParamSizeScalePrecision(ref paramEncrypted, largeColumnInfo);
137+
paramEncrypted.Value = value;
138+
cmdEncrypted.Parameters.Add(paramEncrypted);
139+
140+
SqlParameter paramUnencrypted = new SqlParameter();
141+
paramUnencrypted.ParameterName = FirstParamName;
142+
paramUnencrypted.SqlDbType = largeDbType;
143+
SetParamSizeScalePrecision(ref paramUnencrypted, largeColumnInfo);
144+
paramUnencrypted.Value = value;
145+
cmdUnencrypted.Parameters.Add(paramUnencrypted);
146+
147+
using (SqlDataReader readerUnencrypted = cmdUnencrypted.ExecuteReader())
148+
using (SqlDataReader readerEncrypted = cmdEncrypted.ExecuteReader())
127149
{
128-
value = rawValues[i];
129-
}
150+
// First check that we found some rows.
151+
Assert.True(readerEncrypted.HasRows, @"We didn't find any rows.");
130152

131-
using (SqlCommand cmdEncrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, encryptedTableName, FirstParamName), sqlConnectionEncrypted, null, SqlCommandColumnEncryptionSetting.Enabled))
132-
using (SqlCommand cmdUnencrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, unencryptedTableName, FirstParamName), sqlConnectionUnencrypted, null, SqlCommandColumnEncryptionSetting.Disabled))
133-
{
134-
SqlParameter paramEncrypted = new SqlParameter();
135-
paramEncrypted.ParameterName = FirstParamName;
136-
paramEncrypted.SqlDbType = largeDbType;
137-
SetParamSizeScalePrecision(ref paramEncrypted, largeColumnInfo);
138-
paramEncrypted.Value = value;
139-
cmdEncrypted.Parameters.Add(paramEncrypted);
140-
141-
SqlParameter paramUnencrypted = new SqlParameter();
142-
paramUnencrypted.ParameterName = FirstParamName;
143-
paramUnencrypted.SqlDbType = largeDbType;
144-
SetParamSizeScalePrecision(ref paramUnencrypted, largeColumnInfo);
145-
paramUnencrypted.Value = value;
146-
cmdUnencrypted.Parameters.Add(paramUnencrypted);
147-
148-
using (SqlDataReader readerUnencrypted = cmdUnencrypted.ExecuteReader())
149-
using (SqlDataReader readerEncrypted = cmdEncrypted.ExecuteReader())
150-
{
151-
// First check that we found some rows.
152-
Assert.True(readerEncrypted.HasRows, @"We didn't find any rows.");
153-
154-
// Now compare the result.
155-
CompareResults(readerEncrypted, readerUnencrypted);
156-
}
153+
// Now compare the result.
154+
CompareResults(readerEncrypted, readerUnencrypted);
157155
}
158156
}
159157
}
160-
finally
161-
{
162-
// DropTables
163-
DropTableIfExists(sqlConnectionEncrypted, encryptedTableName);
164-
DropTableIfExists(sqlConnectionUnencrypted, unencryptedTableName);
165-
}
166158
}
167159
}
168160

@@ -206,62 +198,52 @@ public void ConversionSmallerToLargerInsertAndSelectBulk(string connString, SqlD
206198
sqlConnectionEncrypted.Open();
207199
sqlConnectionUnencrypted.Open();
208200

209-
try
201+
// Select each value we just inserted with a predicate and verify that encrypted and unencrypted return the same result.
202+
for (int i = 0; i < NumberOfRows; i++)
210203
{
211-
// Select each value we just inserted with a predicate and verify that encrypted and unencrypted return the same result.
212-
for (int i = 0; i < NumberOfRows; i++)
204+
object value;
205+
206+
// Use the retrieved values for DateTime2 and DateTimeOffset due to fractional insertion adjustment
207+
if (smallColumnInfo.ColumnType is SqlDbType.DateTime2 ||
208+
smallColumnInfo.ColumnType is SqlDbType.DateTimeOffset ||
209+
smallColumnInfo.ColumnType is SqlDbType.Char ||
210+
smallColumnInfo.ColumnType is SqlDbType.NChar)
211+
{
212+
value = valuesToSelect[i];
213+
}
214+
else
213215
{
214-
object value;
216+
value = rawValues[i];
217+
}
215218

216-
// Use the retrieved values for DateTime2 and DateTimeOffset due to fractional insertion adjustment
217-
if (smallColumnInfo.ColumnType is SqlDbType.DateTime2 ||
218-
smallColumnInfo.ColumnType is SqlDbType.DateTimeOffset ||
219-
smallColumnInfo.ColumnType is SqlDbType.Char ||
220-
smallColumnInfo.ColumnType is SqlDbType.NChar)
221-
{
222-
value = valuesToSelect[i];
223-
}
224-
else
219+
using (SqlCommand cmdEncrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, targetTableName, FirstParamName), sqlConnectionEncrypted, null, SqlCommandColumnEncryptionSetting.Enabled))
220+
using (SqlCommand cmdUnencrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, witnessTableName, FirstParamName), sqlConnectionUnencrypted, null, SqlCommandColumnEncryptionSetting.Disabled))
221+
{
222+
SqlParameter paramEncrypted = new SqlParameter();
223+
paramEncrypted.ParameterName = FirstParamName;
224+
paramEncrypted.SqlDbType = largeDbType;
225+
SetParamSizeScalePrecision(ref paramEncrypted, largeColumnInfo);
226+
paramEncrypted.Value = value;
227+
cmdEncrypted.Parameters.Add(paramEncrypted);
228+
229+
SqlParameter paramUnencrypted = new SqlParameter();
230+
paramUnencrypted.ParameterName = FirstParamName;
231+
paramUnencrypted.SqlDbType = largeDbType;
232+
SetParamSizeScalePrecision(ref paramUnencrypted, largeColumnInfo);
233+
paramUnencrypted.Value = value;
234+
cmdUnencrypted.Parameters.Add(paramUnencrypted);
235+
236+
using (SqlDataReader readerUnencrypted = cmdUnencrypted.ExecuteReader())
237+
using (SqlDataReader readerEncrypted = cmdEncrypted.ExecuteReader())
225238
{
226-
value = rawValues[i];
227-
}
239+
// First check that we found some rows.
240+
Assert.True(readerEncrypted.HasRows, @"We didn't find any rows.");
228241

229-
using (SqlCommand cmdEncrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, targetTableName, FirstParamName), sqlConnectionEncrypted, null, SqlCommandColumnEncryptionSetting.Enabled))
230-
using (SqlCommand cmdUnencrypted = new SqlCommand(string.Format(@"SELECT {0} FROM [{1}] WHERE {0} = {2}", FirstColumnName, witnessTableName, FirstParamName), sqlConnectionUnencrypted, null, SqlCommandColumnEncryptionSetting.Disabled))
231-
{
232-
SqlParameter paramEncrypted = new SqlParameter();
233-
paramEncrypted.ParameterName = FirstParamName;
234-
paramEncrypted.SqlDbType = largeDbType;
235-
SetParamSizeScalePrecision(ref paramEncrypted, largeColumnInfo);
236-
paramEncrypted.Value = value;
237-
cmdEncrypted.Parameters.Add(paramEncrypted);
238-
239-
SqlParameter paramUnencrypted = new SqlParameter();
240-
paramUnencrypted.ParameterName = FirstParamName;
241-
paramUnencrypted.SqlDbType = largeDbType;
242-
SetParamSizeScalePrecision(ref paramUnencrypted, largeColumnInfo);
243-
paramUnencrypted.Value = value;
244-
cmdUnencrypted.Parameters.Add(paramUnencrypted);
245-
246-
using (SqlDataReader readerUnencrypted = cmdUnencrypted.ExecuteReader())
247-
using (SqlDataReader readerEncrypted = cmdEncrypted.ExecuteReader())
248-
{
249-
// First check that we found some rows.
250-
Assert.True(readerEncrypted.HasRows, @"We didn't find any rows.");
251-
252-
// Now compare the result.
253-
CompareResults(readerEncrypted, readerUnencrypted);
254-
}
242+
// Now compare the result.
243+
CompareResults(readerEncrypted, readerUnencrypted);
255244
}
256245
}
257246
}
258-
finally
259-
{
260-
// DropTables
261-
DropTableIfExists(sqlConnectionEncrypted, targetTableName);
262-
DropTableIfExists(sqlConnectionUnencrypted, witnessTableName);
263-
DropTableIfExists(sqlConnectionUnencrypted, originTableName);
264-
}
265247
}
266248
}
267249

@@ -293,44 +275,35 @@ public void TestOutOfRangeValues(string connString, SqlDbType currentDbType)
293275
sqlConnectionEncrypted.Open();
294276
sqlConnectionUnencrypted.Open();
295277

296-
try
278+
foreach (ValueErrorTuple tuple in valueList)
297279
{
298-
foreach (ValueErrorTuple tuple in valueList)
280+
using (SqlCommand sqlCmd = new SqlCommand(String.Format("INSERT INTO [{0}] VALUES ({1})", encryptedTableName, FirstParamName), sqlConnectionEncrypted, null, SqlCommandColumnEncryptionSetting.Enabled))
299281
{
300-
using (SqlCommand sqlCmd = new SqlCommand(String.Format("INSERT INTO [{0}] VALUES ({1})", encryptedTableName, FirstParamName), sqlConnectionEncrypted, null, SqlCommandColumnEncryptionSetting.Enabled))
301-
{
302-
SqlParameter param = new SqlParameter();
303-
param.ParameterName = FirstParamName;
304-
param.SqlDbType = currentColumnInfo.ColumnType;
305-
SetParamSizeScalePrecision(ref param, currentColumnInfo);
306-
param.Value = tuple.Value;
307-
sqlCmd.Parameters.Add(param);
308-
309-
ExecuteAndCheckForError(sqlCmd, tuple.ExpectsError);
310-
}
282+
SqlParameter param = new SqlParameter();
283+
param.ParameterName = FirstParamName;
284+
param.SqlDbType = currentColumnInfo.ColumnType;
285+
SetParamSizeScalePrecision(ref param, currentColumnInfo);
286+
param.Value = tuple.Value;
287+
sqlCmd.Parameters.Add(param);
311288

312-
// Add same value to the unencrypted table
313-
using (SqlCommand sqlCmd = new SqlCommand(String.Format("INSERT INTO [{0}] VALUES ({1})", unencryptedTableName, FirstParamName), sqlConnectionUnencrypted, null, SqlCommandColumnEncryptionSetting.Disabled))
314-
{
315-
SqlParameter param = new SqlParameter();
316-
param.ParameterName = FirstParamName;
317-
param.SqlDbType = currentColumnInfo.ColumnType;
318-
SetParamSizeScalePrecision(ref param, currentColumnInfo);
319-
param.Value = tuple.Value;
320-
sqlCmd.Parameters.Add(param);
321-
322-
ExecuteAndCheckForError(sqlCmd, tuple.ExpectsError);
323-
}
289+
ExecuteAndCheckForError(sqlCmd, tuple.ExpectsError);
290+
}
291+
292+
// Add same value to the unencrypted table
293+
using (SqlCommand sqlCmd = new SqlCommand(String.Format("INSERT INTO [{0}] VALUES ({1})", unencryptedTableName, FirstParamName), sqlConnectionUnencrypted, null, SqlCommandColumnEncryptionSetting.Disabled))
294+
{
295+
SqlParameter param = new SqlParameter();
296+
param.ParameterName = FirstParamName;
297+
param.SqlDbType = currentColumnInfo.ColumnType;
298+
SetParamSizeScalePrecision(ref param, currentColumnInfo);
299+
param.Value = tuple.Value;
300+
sqlCmd.Parameters.Add(param);
324301

302+
ExecuteAndCheckForError(sqlCmd, tuple.ExpectsError);
325303
}
326304

327305
CompareTables(connString, encryptedTableName, unencryptedTableName);
328306
}
329-
finally
330-
{
331-
DropTableIfExists(sqlConnectionEncrypted, encryptedTableName);
332-
DropTableIfExists(sqlConnectionUnencrypted, unencryptedTableName);
333-
}
334307
}
335308
}
336309

@@ -1305,6 +1278,7 @@ private void CreateTable(string connString, ColumnMetaData columnMeta, string ta
13051278
command.ExecuteNonQuery();
13061279
}
13071280
}
1281+
_tables.Add(tableName);
13081282
}
13091283

13101284
/// <summary>
@@ -1351,6 +1325,10 @@ public void Dispose()
13511325
using (SqlConnection sqlConnection = new SqlConnection(connectionStr))
13521326
{
13531327
sqlConnection.Open();
1328+
foreach (string table in _tables)
1329+
{
1330+
DropTableIfExists(sqlConnection, table);
1331+
}
13541332
_databaseObjects.ForEach(o => o.Drop(sqlConnection));
13551333
}
13561334
}

0 commit comments

Comments
 (0)