Add a test condition for skipping tests when the default keychain is missing for macOS

This commit is contained in:
Nate McMaster 2018-06-08 12:31:39 -07:00
parent 72569151a6
commit 3f3bfe05ec
No known key found for this signature in database
GPG Key ID: A778D9601BD78810
2 changed files with 64 additions and 7 deletions

View File

@ -115,7 +115,8 @@ namespace Microsoft.AspNetCore.DataProtection
});
}
[Fact]
[ConditionalFact]
[X509StoreIsAvailable(StoreName.My, StoreLocation.CurrentUser)]
public void System_UsesProvidedDirectoryAndCertificate()
{
var filePath = Path.Combine(GetTestFilesPath(), "TestCert.pfx");
@ -162,12 +163,7 @@ namespace Microsoft.AspNetCore.DataProtection
var filePath = Path.Combine(GetTestFilesPath(), "TestCert2.pfx");
var certificate = new X509Certificate2(filePath, "password");
using (var store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
store.Open(OpenFlags.ReadOnly);
// ensure this cert is not in the x509 store
Assert.Empty(store.Certificates.Find(X509FindType.FindByThumbprint, certificate.Thumbprint, false));
}
AssetStoreDoesNotContain(certificate);
WithUniqueTempDirectory(directory =>
{
@ -189,6 +185,24 @@ namespace Microsoft.AspNetCore.DataProtection
});
}
private static void AssetStoreDoesNotContain(X509Certificate2 certificate)
{
using (var store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
try
{
store.Open(OpenFlags.ReadOnly);
}
catch
{
return;
}
// ensure this cert is not in the x509 store
Assert.Empty(store.Certificates.Find(X509FindType.FindByThumbprint, certificate.Thumbprint, false));
}
}
[Fact]
public void System_CanUnprotectWithCert()
{

View File

@ -0,0 +1,43 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Testing.xunit;
namespace Microsoft.AspNetCore.DataProtection
{
[AttributeUsage(AttributeTargets.Method)]
public class X509StoreIsAvailableAttribute : Attribute, ITestCondition
{
public X509StoreIsAvailableAttribute(StoreName name, StoreLocation location)
{
Name = name;
Location = location;
}
public bool IsMet
{
get
{
try
{
using (var store = new X509Store(Name, Location))
{
store.Open(OpenFlags.ReadWrite);
return true;
}
}
catch
{
return false;
}
}
}
public string SkipReason => $"Skipping because the X509Store({Name}/{Location}) is not available on this machine.";
public StoreName Name { get; }
public StoreLocation Location { get; }
}
}