Skip to content

Commit

Permalink
Added unit tests for Sign.SignatureProviders.TrustedSigning.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Jun 14, 2024
1 parent b18eb8a commit 0614fe5
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sign.sln
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sign.SignatureProviders.Cer
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sign.SignatureProviders.TrustedSigning", "src\Sign.SignatureProviders.TrustedSigning\Sign.SignatureProviders.TrustedSigning.csproj", "{060800AF-42FC-493C-AD99-9C87212BA969}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sign.SignatureProviders.TrustedSigning.Test", "test\Sign.SignatureProviders.TrustedSigning.Test\Sign.SignatureProviders.TrustedSigning.Test.csproj", "{A81695AF-088A-436A-9A38-4D0B0DB2D826}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -90,6 +92,10 @@ Global
{060800AF-42FC-493C-AD99-9C87212BA969}.Debug|Any CPU.Build.0 = Debug|Any CPU
{060800AF-42FC-493C-AD99-9C87212BA969}.Release|Any CPU.ActiveCfg = Release|Any CPU
{060800AF-42FC-493C-AD99-9C87212BA969}.Release|Any CPU.Build.0 = Release|Any CPU
{A81695AF-088A-436A-9A38-4D0B0DB2D826}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A81695AF-088A-436A-9A38-4D0B0DB2D826}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A81695AF-088A-436A-9A38-4D0B0DB2D826}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A81695AF-088A-436A-9A38-4D0B0DB2D826}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -105,6 +111,7 @@ Global
{68104303-9832-4841-89AB-B98712C4E618} = {92C73EE1-4EF3-4721-B6A9-9F458A673CA3}
{3AE48DC2-8422-4E3A-AFBC-12551D50DBCA} = {780818DD-6B52-47C8-AC54-71448DF822BD}
{060800AF-42FC-493C-AD99-9C87212BA969} = {92C73EE1-4EF3-4721-B6A9-9F458A673CA3}
{A81695AF-088A-436A-9A38-4D0B0DB2D826} = {780818DD-6B52-47C8-AC54-71448DF822BD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7AA1043F-37A2-404F-8EC3-34C747C1CEB7}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$(RepositoryRootDirectory)\SdkTools.props" />

<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<IsUnitTestProject>true</IsUnitTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Sign.SignatureProviders.TrustedSigning\Sign.SignatureProviders.TrustedSigning.csproj" />
<ProjectReference Include="..\Sign.TestInfrastructure\Sign.TestInfrastructure.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// 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.txt file in the project root for more information.

using System.Collections.Concurrent;
using Azure.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using Sign.Core;
using Sign.TestInfrastructure;

namespace Sign.SignatureProviders.TrustedSigning.Test
{
public class TrustedSigningServiceProviderTests
{
private readonly static TokenCredential TokenCredential = Mock.Of<TokenCredential>();
private readonly static Uri EndpointUrl = new("https://trustedsigning.test");
private const string AccountName = "a";
private const string CertificateProfileName = "b";
private readonly IServiceProvider serviceProvider;

public TrustedSigningServiceProviderTests()
{
ServiceCollection services = new();
services.AddSingleton<ILogger<TrustedSigningService>>(new TestLogger<TrustedSigningService>());
serviceProvider = services.BuildServiceProvider();
}

[Fact]
public void Constructor_WhenTokenCredentialIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningServiceProvider(tokenCredential: null!, EndpointUrl, AccountName, CertificateProfileName));

Assert.Equal("tokenCredential", exception.ParamName);
}

[Fact]
public void Constructor_WhenEndpointUrlIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningServiceProvider(TokenCredential, endpointUrl: null!, AccountName, CertificateProfileName));

Assert.Equal("endpointUrl", exception.ParamName);
}

[Fact]
public void Constructor_WhenAccountNameIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningServiceProvider(TokenCredential, EndpointUrl, accountName: null!, CertificateProfileName));

Assert.Equal("accountName", exception.ParamName);
}

[Fact]
public void Constructor_WhenAccountNameIsEmpty_Throws()
{
ArgumentException exception = Assert.Throws<ArgumentException>(
() => new TrustedSigningServiceProvider(TokenCredential, EndpointUrl, accountName: string.Empty, CertificateProfileName));

Assert.Equal("accountName", exception.ParamName);
}

[Fact]
public void Constructor_WhenCertificateProfileNameIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningServiceProvider(TokenCredential, EndpointUrl, AccountName, certificateProfileName: null!));

Assert.Equal("certificateProfileName", exception.ParamName);
}

[Fact]
public void Constructor_WhenCertificateProfileNameIsEmpty_Throws()
{
ArgumentException exception = Assert.Throws<ArgumentException>(
() => new TrustedSigningServiceProvider(TokenCredential, EndpointUrl, AccountName, certificateProfileName: string.Empty));

Assert.Equal("certificateProfileName", exception.ParamName);
}

[Fact]
public void GetSignatureAlgorithmProvider_WhenServiceProviderIsNull_Throws()
{
TrustedSigningServiceProvider provider = new(TokenCredential, EndpointUrl, AccountName, CertificateProfileName);

ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => provider.GetSignatureAlgorithmProvider(serviceProvider: null!));

Assert.Equal("serviceProvider", exception.ParamName);
}

[Fact]
public void GetSignatureAlgorithmProvider_ReturnsSameInstance()
{
TrustedSigningServiceProvider provider = new(TokenCredential, EndpointUrl, AccountName, CertificateProfileName);

ConcurrentBag<ISignatureAlgorithmProvider> signatureAlgorithmProviders = [];
Parallel.For(0, 2, (_, _) =>
{
signatureAlgorithmProviders.Add(provider.GetSignatureAlgorithmProvider(serviceProvider));
});

Assert.Equal(2, signatureAlgorithmProviders.Count);
Assert.Same(signatureAlgorithmProviders.First(), signatureAlgorithmProviders.Last());
}

[Fact]
public void GetCertificateProvider_WhenServiceProviderIsNull_Throws()
{
TrustedSigningServiceProvider provider = new(TokenCredential, EndpointUrl, AccountName, CertificateProfileName);

ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => provider.GetSignatureAlgorithmProvider(serviceProvider: null!));

Assert.Equal("serviceProvider", exception.ParamName);
}

[Fact]
public void GetCertificateProvider_ReturnsSameInstance()
{
TrustedSigningServiceProvider provider = new(TokenCredential, EndpointUrl, AccountName, CertificateProfileName);

ConcurrentBag<ICertificateProvider> certificateProviders = [];
Parallel.For(0, 2, (_, _) =>
{
certificateProviders.Add(provider.GetCertificateProvider(serviceProvider));
});

Assert.Equal(2, certificateProviders.Count);
Assert.Same(certificateProviders.First(), certificateProviders.Last());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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.txt file in the project root for more information.

using Azure.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using Sign.TestInfrastructure;

namespace Sign.SignatureProviders.TrustedSigning.Test
{
public class TrustedSigningServiceTests
{
private readonly static TokenCredential TokenCredential = Mock.Of<TokenCredential>();
private readonly static Uri EndpointUrl = new("https://trustedsigning.test");
private const string AccountName = "a";
private const string CertificateProfileName = "b";
private readonly IServiceProvider serviceProvider;

public TrustedSigningServiceTests()
{
ServiceCollection services = new();
services.AddSingleton<ILogger<TrustedSigningService>>(new TestLogger<TrustedSigningService>());
serviceProvider = services.BuildServiceProvider();
}

[Fact]
public void Constructor_WhenServiceProviderIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningService(serviceProvider: null!, TokenCredential, EndpointUrl, AccountName, CertificateProfileName));

Assert.Equal("serviceProvider", exception.ParamName);
}

[Fact]
public void Constructor_WhenTokenCredentialIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningService(serviceProvider, tokenCredential: null!, EndpointUrl, AccountName, CertificateProfileName));

Assert.Equal("tokenCredential", exception.ParamName);
}

[Fact]
public void Constructor_WhenEndpointUrlIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningService(serviceProvider, TokenCredential, endpointUrl: null!, AccountName, CertificateProfileName));

Assert.Equal("endpointUrl", exception.ParamName);
}

[Fact]
public void Constructor_WhenAccountNameIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningService(serviceProvider, TokenCredential, EndpointUrl, accountName: null!, CertificateProfileName));

Assert.Equal("accountName", exception.ParamName);
}

[Fact]
public void Constructor_WhenAccountNameIsEmpty_Throws()
{
ArgumentException exception = Assert.Throws<ArgumentException>(
() => new TrustedSigningService(serviceProvider, TokenCredential, EndpointUrl, accountName: string.Empty, CertificateProfileName));

Assert.Equal("accountName", exception.ParamName);
}

[Fact]
public void Constructor_WhenCertificateProfileNameIsNull_Throws()
{
ArgumentNullException exception = Assert.Throws<ArgumentNullException>(
() => new TrustedSigningService(serviceProvider, TokenCredential, EndpointUrl, AccountName, certificateProfileName: null!));

Assert.Equal("certificateProfileName", exception.ParamName);
}

[Fact]
public void Constructor_WhenCertificateProfileNameIsEmpty_Throws()
{
ArgumentException exception = Assert.Throws<ArgumentException>(
() => new TrustedSigningService(serviceProvider, TokenCredential, EndpointUrl, AccountName, certificateProfileName: string.Empty));

Assert.Equal("certificateProfileName", exception.ParamName);
}
}
}
5 changes: 5 additions & 0 deletions test/Sign.SignatureProviders.TrustedSigning.Test/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// 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.txt file in the project root for more information.

global using Xunit;

0 comments on commit 0614fe5

Please sign in to comment.