Skip to content

Commit

Permalink
Added unit test for AE enabled bulk copy error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Davoud Eshtehari authored and Davoud Eshtehari committed Feb 25, 2020
1 parent d7c49dc commit 11ba3c7
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
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);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class SQLSetupStrategy : IDisposable
protected internal readonly X509Certificate2 certificate;
public string keyPath { get; internal set; }
public Table ApiTestTable { get; private set; }
public Table BulkCopyAEErrorMessageTestTable { get; private set; }
public Table BulkCopyAETestTable { get; private set; }
public Table SqlParameterPropertiesTable { get; private set; }
public Table End2EndSmokeTable { get; private set; }
Expand Down Expand Up @@ -112,6 +113,9 @@ protected List<Table> CreateTables(IList<ColumnEncryptionKey> columnEncryptionKe
ApiTestTable = new ApiTestTable(GenerateUniqueName("ApiTestTable"), columnEncryptionKeys[0], columnEncryptionKeys[1]);
tables.Add(ApiTestTable);

BulkCopyAEErrorMessageTestTable = new BulkCopyAEErrorMessageTestTable(GenerateUniqueName("BulkCopyAEErrorMessageTestTable"), columnEncryptionKeys[0], columnEncryptionKeys[1]);
tables.Add(BulkCopyAEErrorMessageTestTable);

BulkCopyAETestTable = new BulkCopyAETestTable(GenerateUniqueName("BulkCopyAETestTable"), columnEncryptionKeys[0], columnEncryptionKeys[1]);
tables.Add(BulkCopyAETestTable);

Expand Down
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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</Compile>
<Compile Include="AlwaysEncrypted\AKVTests.cs" />
<Compile Include="AlwaysEncrypted\ApiShould.cs" />
<Compile Include="AlwaysEncrypted\BulkCopyAEErrorMessage.cs" />
<Compile Include="AlwaysEncrypted\BulkCopyAE.cs" />
<Compile Include="AlwaysEncrypted\ExceptionTestAKVStore.cs" />
<Compile Include="AlwaysEncrypted\CoreCryptoTests.cs" />
Expand All @@ -29,6 +30,7 @@
<Compile Include="AlwaysEncrypted\SqlBulkCopyTruncation.cs" />
<Compile Include="AlwaysEncrypted\SqlNullValues.cs" />
<Compile Include="AlwaysEncrypted\ExceptionsGenericError.cs" />
<Compile Include="AlwaysEncrypted\TestFixtures\Setup\BulkCopyAEErrorMessageTestTable.cs" />
<Compile Include="AlwaysEncrypted\TestFixtures\Setup\BulkCopyTruncationTables.cs" />
<Compile Include="AlwaysEncrypted\TestFixtures\Setup\SqlNullValuesTable.cs" />
<Compile Include="AlwaysEncrypted\TestFixtures\Setup\AKVTestTable.cs" />
Expand Down

0 comments on commit 11ba3c7

Please sign in to comment.