mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-13 17:23:02 -05:00
Add encryption unit tests (#2243)
This commit is contained in:
@@ -88,6 +88,12 @@ steps:
|
|||||||
command: restore
|
command: restore
|
||||||
projects: test/Microsoft.Kusto.ServiceLayer.UnitTests
|
projects: test/Microsoft.Kusto.ServiceLayer.UnitTests
|
||||||
|
|
||||||
|
- task: DotNetCoreCLI@2
|
||||||
|
displayName: 'dotnet restore test/Microsoft.SqlTools.Authentication.UnitTests'
|
||||||
|
inputs:
|
||||||
|
command: restore
|
||||||
|
projects: test/Microsoft.SqlTools.Authentication.UnitTests
|
||||||
|
|
||||||
- task: DotNetCoreCLI@2
|
- task: DotNetCoreCLI@2
|
||||||
displayName: 'dotnet test test/Microsoft.SqlTools.ServiceLayer.UnitTests'
|
displayName: 'dotnet test test/Microsoft.SqlTools.ServiceLayer.UnitTests'
|
||||||
inputs:
|
inputs:
|
||||||
@@ -104,6 +110,14 @@ steps:
|
|||||||
testRunTitle: Kusto.ServiceLayer.UnitTests
|
testRunTitle: Kusto.ServiceLayer.UnitTests
|
||||||
arguments: '--configuration $(buildConfiguration) --collect "XPlat Code Coverage"'
|
arguments: '--configuration $(buildConfiguration) --collect "XPlat Code Coverage"'
|
||||||
|
|
||||||
|
- task: DotNetCoreCLI@2
|
||||||
|
displayName: 'dotnet test test/Microsoft.SqlTools.Authentication.UnitTests'
|
||||||
|
inputs:
|
||||||
|
command: test
|
||||||
|
projects: test/Microsoft.SqlTools.Authentication.UnitTests
|
||||||
|
testRunTitle: SqlTools.Authentication.UnitTests
|
||||||
|
arguments: '--configuration $(buildConfiguration) --collect "XPlat Code Coverage"'
|
||||||
|
|
||||||
- script: |
|
- script: |
|
||||||
dotnet tool install -g dotnet-reportgenerator-globaltool
|
dotnet tool install -g dotnet-reportgenerator-globaltool
|
||||||
reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(Agent.TempDirectory)/coverlet/reports -reporttypes:"Cobertura"
|
reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(Agent.TempDirectory)/coverlet/reports -reporttypes:"Cobertura"
|
||||||
|
|||||||
@@ -14,6 +14,9 @@
|
|||||||
"Microsoft.Kusto.ServiceLayer.UnitTests": [
|
"Microsoft.Kusto.ServiceLayer.UnitTests": [
|
||||||
"net7.0"
|
"net7.0"
|
||||||
],
|
],
|
||||||
|
"Microsoft.SQlTools.Authentication.UnitTests": [
|
||||||
|
"net7.0"
|
||||||
|
],
|
||||||
"Microsoft.SqlTools.ServiceLayer.TestEnvConfig": [
|
"Microsoft.SqlTools.ServiceLayer.TestEnvConfig": [
|
||||||
"net7.0"
|
"net7.0"
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -101,6 +101,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.SqlTools.Migratio
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.SqlTools.Authentication", "src\Microsoft.SqlTools.Authentication\Microsoft.SqlTools.Authentication.csproj", "{2A32C3B6-3E9F-4A8E-BF98-59F9AEF6DAAC}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.SqlTools.Authentication", "src\Microsoft.SqlTools.Authentication\Microsoft.SqlTools.Authentication.csproj", "{2A32C3B6-3E9F-4A8E-BF98-59F9AEF6DAAC}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.SqlTools.Authentication.UnitTests", "test\Microsoft.SqlTools.Authentication.UnitTests\Microsoft.SqlTools.Authentication.UnitTests.csproj", "{D3960B1C-3191-4A0B-AD0C-3D5D0FFF60A9}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -246,6 +248,12 @@ Global
|
|||||||
{2A32C3B6-3E9F-4A8E-BF98-59F9AEF6DAAC}.Integration|Any CPU.Build.0 = Debug|Any CPU
|
{2A32C3B6-3E9F-4A8E-BF98-59F9AEF6DAAC}.Integration|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{2A32C3B6-3E9F-4A8E-BF98-59F9AEF6DAAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{2A32C3B6-3E9F-4A8E-BF98-59F9AEF6DAAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{2A32C3B6-3E9F-4A8E-BF98-59F9AEF6DAAC}.Release|Any CPU.Build.0 = Release|Any CPU
|
{2A32C3B6-3E9F-4A8E-BF98-59F9AEF6DAAC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{D3960B1C-3191-4A0B-AD0C-3D5D0FFF60A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{D3960B1C-3191-4A0B-AD0C-3D5D0FFF60A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{D3960B1C-3191-4A0B-AD0C-3D5D0FFF60A9}.Integration|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{D3960B1C-3191-4A0B-AD0C-3D5D0FFF60A9}.Integration|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{D3960B1C-3191-4A0B-AD0C-3D5D0FFF60A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{D3960B1C-3191-4A0B-AD0C-3D5D0FFF60A9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@@ -275,6 +283,7 @@ Global
|
|||||||
{22DB0C12-6848-4503-AD1C-DAD6A1D631AE} = {2BBD7364-054F-4693-97CD-1C395E3E84A9}
|
{22DB0C12-6848-4503-AD1C-DAD6A1D631AE} = {2BBD7364-054F-4693-97CD-1C395E3E84A9}
|
||||||
{5C7F4DAC-F794-4C21-A031-DCAAFAF3C0A9} = {AB9CA2B8-6F70-431C-8A1D-67479D8A7BE4}
|
{5C7F4DAC-F794-4C21-A031-DCAAFAF3C0A9} = {AB9CA2B8-6F70-431C-8A1D-67479D8A7BE4}
|
||||||
{2A32C3B6-3E9F-4A8E-BF98-59F9AEF6DAAC} = {2BBD7364-054F-4693-97CD-1C395E3E84A9}
|
{2A32C3B6-3E9F-4A8E-BF98-59F9AEF6DAAC} = {2BBD7364-054F-4693-97CD-1C395E3E84A9}
|
||||||
|
{D3960B1C-3191-4A0B-AD0C-3D5D0FFF60A9} = {AB9CA2B8-6F70-431C-8A1D-67479D8A7BE4}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {B31CDF4B-2851-45E5-8C5F-BE97125D9DD8}
|
SolutionGuid = {B31CDF4B-2851-45E5-8C5F-BE97125D9DD8}
|
||||||
|
|||||||
@@ -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