-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for Sign.SignatureProviders.TrustedSigning.
- Loading branch information
Showing
5 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
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
22 changes: 22 additions & 0 deletions
22
...SignatureProviders.TrustedSigning.Test/Sign.SignatureProviders.TrustedSigning.Test.csproj
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,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> |
136 changes: 136 additions & 0 deletions
136
test/Sign.SignatureProviders.TrustedSigning.Test/TrustedSigningServiceProviderTests.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,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()); | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
test/Sign.SignatureProviders.TrustedSigning.Test/TrustedSigningServiceTests.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,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); | ||
} | ||
} | ||
} |
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,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; |