-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit test for AE enabled bulk copy error message
- Loading branch information
Davoud Eshtehari
authored and
Davoud Eshtehari
committed
Feb 25, 2020
1 parent
d7c49dc
commit 11ba3c7
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/BulkCopyAEErrorMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// 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 System.Data; | ||
using Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted | ||
{ | ||
/// <summary> | ||
/// Always Encrypted public API Manual tests. | ||
/// TODO: These tests are marked as Windows only for now but should be run for all platforms once the Master Key is accessible to this app from Azure Key Vault. | ||
/// </summary> | ||
[PlatformSpecific(TestPlatforms.Windows)] | ||
public class BulkCopyAEErrorMessage : IClassFixture<SQLSetupStrategyCertStoreProvider>, IDisposable | ||
{ | ||
private SQLSetupStrategyCertStoreProvider fixture; | ||
|
||
private readonly string _tableName; | ||
private readonly string _columnName; | ||
|
||
public BulkCopyAEErrorMessage(SQLSetupStrategyCertStoreProvider fixture) | ||
{ | ||
this.fixture = fixture; | ||
_tableName = fixture.BulkCopyAEErrorMessageTestTable.Name; | ||
_columnName = "c1"; | ||
} | ||
|
||
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE))] | ||
[ClassData(typeof(AEConnectionStringProvider))] | ||
public void TextToIntErrorMessageTest(string connectionString) | ||
{ | ||
string value = "stringValue"; | ||
DataTable dataTable = CreateTable(value); | ||
|
||
Assert.True(StringToIntTest(connectionString, _tableName, dataTable, value, dataTable.Rows.Count), "Did not get any exceptions for DataTable!"); | ||
Assert.True(StringToIntTest(connectionString, _tableName, dataTable.Select(), value, dataTable.Rows.Count),"Did not get any exceptions for DataRow[]!"); | ||
} | ||
|
||
private DataTable CreateTable(string value) | ||
{ | ||
var dataTable = new DataTable(); | ||
dataTable.Columns.Add(_columnName, typeof(string)); | ||
|
||
var dataRow = dataTable.NewRow(); | ||
dataRow[_columnName] = value; | ||
dataTable.Rows.Add(dataRow); | ||
dataTable.AcceptChanges(); | ||
|
||
return dataTable; | ||
} | ||
|
||
private bool StringToIntTest(string connectionString, string targetTable, object dataSet, string value, int rowNo, string targetType = "int") | ||
{ | ||
var encryptionEnabledConnectionString = new SqlConnectionStringBuilder(connectionString) | ||
{ | ||
ColumnEncryptionSetting = SqlConnectionColumnEncryptionSetting.Enabled | ||
}.ConnectionString; | ||
|
||
bool hitException = false; | ||
try | ||
{ | ||
using (var connection = new SqlConnection(encryptionEnabledConnectionString)) | ||
using (var bulkCopy = new SqlBulkCopy(connection) | ||
{ | ||
EnableStreaming = true, | ||
BatchSize = 1, | ||
DestinationTableName = "[" + _tableName + "]" | ||
}) | ||
{ | ||
connection.Open(); | ||
bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(0, 0)); | ||
|
||
if (dataSet as DataTable != null) | ||
bulkCopy.WriteToServer((DataTable)dataSet); | ||
if (dataSet as DataRow[] != null) | ||
bulkCopy.WriteToServer((DataRow[])dataSet); | ||
|
||
bulkCopy.Close(); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
object[] args = | ||
new object[] { string.Empty, value.GetType().Name, targetType, 0, _columnName, rowNo}; | ||
string expectedErrorMsg = string.Format(SystemDataResourceManager.Instance.SQL_BulkLoadCannotConvertValue, args); | ||
Assert.True(ex.Message.Contains(expectedErrorMsg), "Unexpected error message: " + ex.Message); | ||
hitException = true; | ||
} | ||
return hitException; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
foreach (string connection in DataTestUtility.AEConnStringsSetup) | ||
{ | ||
using (SqlConnection sqlConnection = new SqlConnection(connection)) | ||
{ | ||
sqlConnection.Open(); | ||
Table.DeleteData(_tableName, sqlConnection); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...t/tests/ManualTests/AlwaysEncrypted/TestFixtures/Setup/BulkCopyAEErrorMessageTestTable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// 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. | ||
|
||
namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup | ||
{ | ||
public class BulkCopyAEErrorMessageTestTable : Table | ||
{ | ||
private const string ColumnEncryptionAlgorithmName = @"AEAD_AES_256_CBC_HMAC_SHA_256"; | ||
private ColumnEncryptionKey columnEncryptionKey1; | ||
private ColumnEncryptionKey columnEncryptionKey2; | ||
|
||
public BulkCopyAEErrorMessageTestTable(string tableName, ColumnEncryptionKey columnEncryptionKey1, ColumnEncryptionKey columnEncryptionKey2) : base(tableName) | ||
{ | ||
this.columnEncryptionKey1 = columnEncryptionKey1; | ||
this.columnEncryptionKey2 = columnEncryptionKey2; | ||
} | ||
|
||
public override void Create(SqlConnection sqlConnection) | ||
{ | ||
string encryptionType = DataTestUtility.EnclaveEnabled ? "RANDOMIZED" : "DETERMINISTIC"; | ||
string sql = | ||
$@"CREATE TABLE [dbo].[{Name}] | ||
( | ||
[c1] int ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [{columnEncryptionKey1.Name}], ENCRYPTION_TYPE = {encryptionType}, ALGORITHM = '{ColumnEncryptionAlgorithmName}') | ||
)"; | ||
|
||
using (SqlCommand command = sqlConnection.CreateCommand()) | ||
{ | ||
command.CommandText = sql; | ||
command.ExecuteNonQuery(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters