mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-19 17:23:55 -05:00
Add encryption unit tests (#2243)
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using Microsoft.SqlTools.Authentication.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.Authentication.UnitTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class EncryptionUtilsTests
|
||||
{
|
||||
[Test]
|
||||
public void AesEncryptDecrypt_ValidData_EncryptionDecryptionSuccessful()
|
||||
{
|
||||
// Arrange
|
||||
byte[] originalData = new byte[] { 0, 1, 2, 3, 4 };
|
||||
byte[] key = new byte[32]; // 256-bit key
|
||||
byte[] iv = new byte[16]; // 128-bit IV
|
||||
|
||||
// Act
|
||||
byte[] encryptedData = EncryptionUtils.AesEncrypt(originalData, key, iv);
|
||||
byte[] decryptedData = EncryptionUtils.AesDecrypt(encryptedData, key, iv);
|
||||
|
||||
// Assert
|
||||
CollectionAssert.AreEqual(originalData, decryptedData);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AesEncrypt_PaddingIsCorrectlyAdded()
|
||||
{
|
||||
// Arrange
|
||||
byte[] originalData = new byte[] { 0, 1, 2, 3, 4, 5 };
|
||||
byte[] key = new byte[32]; // 256-bit key
|
||||
byte[] iv = new byte[16]; // 128-bit IV
|
||||
|
||||
// Act
|
||||
byte[] encryptedData = EncryptionUtils.AesEncrypt(originalData, key, iv);
|
||||
|
||||
// Assert
|
||||
// Check if the encrypted data length is a multiple of the block size (128 bits or 16 bytes)
|
||||
int blockSizeInBytes = 16;
|
||||
int expectedPaddedLength = ((originalData.Length / blockSizeInBytes) + 1) * blockSizeInBytes;
|
||||
Assert.AreEqual(expectedPaddedLength, encryptedData.Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AesEncryptDecrypt_LargeData_PaddingIsCorrectlyAdded_TransformationIsSuccessful()
|
||||
{
|
||||
// Arrange
|
||||
byte[] originalData = new byte[2096];
|
||||
for (int i = 0; i < 2096; i++)
|
||||
{
|
||||
originalData[i] = (byte)Random.Shared.Next(0, 4);
|
||||
}
|
||||
byte[] key = new byte[32]; // 256-bit key
|
||||
byte[] iv = new byte[16]; // 128-bit IV
|
||||
|
||||
// Act
|
||||
byte[] encryptedData = EncryptionUtils.AesEncrypt(originalData, key, iv);
|
||||
|
||||
// Assert
|
||||
// Check if the encrypted data length is a multiple of the block size (128 bits or 16 bytes)
|
||||
int blockSizeInBytes = 16;
|
||||
int expectedPaddedLength = ((originalData.Length / blockSizeInBytes) + 1) * blockSizeInBytes;
|
||||
Assert.AreEqual(expectedPaddedLength, encryptedData.Length);
|
||||
|
||||
// Act
|
||||
byte[] decryptedData = EncryptionUtils.AesDecrypt(encryptedData, key, iv);
|
||||
|
||||
//Assert
|
||||
CollectionAssert.AreEqual(originalData, decryptedData);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AesEncrypt_NullPlainText_ThrowsArgumentNullException()
|
||||
{
|
||||
// Arrange
|
||||
byte[] key = new byte[32]; // 256-bit key
|
||||
byte[] iv = new byte[16]; // 128-bit IV
|
||||
|
||||
// Act
|
||||
Assert.Throws<ArgumentNullException>(() => EncryptionUtils.AesEncrypt(null, key, iv));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AesEncrypt_NullIV_ThrowsArgumentNullException()
|
||||
{
|
||||
// Arrange
|
||||
byte[] key = new byte[32]; // 256-bit key
|
||||
byte[] data = new byte[32]; // 256-bit data
|
||||
|
||||
// Act
|
||||
Assert.Throws<ArgumentNullException>(() => EncryptionUtils.AesEncrypt(data, key, null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AesEncrypt_NullKey_ThrowsArgumentNullException()
|
||||
{
|
||||
// Arrange
|
||||
byte[] iv = new byte[16]; // 128-bit IV
|
||||
byte[] data = new byte[32]; // 256-bit data
|
||||
|
||||
// Act
|
||||
Assert.Throws<ArgumentNullException>(() => EncryptionUtils.AesEncrypt(data, null, iv));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AesDecrypt_NullCipherText_ThrowsArgumentNullException()
|
||||
{
|
||||
// Arrange
|
||||
byte[] key = new byte[32]; // 256-bit key
|
||||
byte[] iv = new byte[16]; // 128-bit IV
|
||||
|
||||
// Act
|
||||
Assert.Throws<ArgumentNullException>(() => EncryptionUtils.AesDecrypt(null, key, iv));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AesDecrypt_NullIV_ThrowsArgumentNullException()
|
||||
{
|
||||
// Arrange
|
||||
byte[] key = new byte[32]; // 256-bit key
|
||||
byte[] data = new byte[32]; // 256-bit data
|
||||
|
||||
// Act
|
||||
Assert.Throws<ArgumentNullException>(() => EncryptionUtils.AesDecrypt(data, key, null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AesDecrypt_NullKey_ThrowsArgumentNullException()
|
||||
{
|
||||
// Arrange
|
||||
byte[] iv = new byte[16]; // 128-bit IV
|
||||
byte[] data = new byte[32]; // 256-bit data
|
||||
|
||||
// Act
|
||||
Assert.Throws<ArgumentNullException>(() => EncryptionUtils.AesDecrypt(data, null, iv));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
global using System;
|
||||
global using NUnit.Framework;
|
||||
@@ -0,0 +1,34 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup Label="Configuration">
|
||||
<OutputType>Exe</OutputType>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<DefineConstants>$(DefineConstants);NETCOREAPP1_0;TRACE</DefineConstants>
|
||||
<IsPackable>false</IsPackable>
|
||||
<ApplicationIcon />
|
||||
<StartupObject />
|
||||
<!-- False alerts, disabled due to issue: https://github.com/dotnet/roslyn/issues/65850 -->
|
||||
<NoWarn>$(NoWarn);CS8795</NoWarn>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Text.Encoding.CodePages" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="Moq" />
|
||||
<PackageReference Include="nunit" />
|
||||
<PackageReference Include="nunit3testadapter" />
|
||||
<PackageReference Include="nunit.console" />
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" />
|
||||
<PackageReference Include="coverlet.collector">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.msbuild">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../../src/Microsoft.SqlTools.Authentication/Microsoft.SqlTools.Authentication.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user