Make cert fixture lazy (#1412)

This commit is contained in:
Justin Kotalik 2018-09-19 12:26:14 -07:00 committed by GitHub
parent 4e57b0e1f1
commit b41f1f0cae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 40 deletions

View File

@ -10,8 +10,17 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests
{ {
public class ClientCertificateFixture : IDisposable public class ClientCertificateFixture : IDisposable
{ {
public ClientCertificateFixture() private X509Certificate2 _certificate;
public X509Certificate2 Certificate
{ {
get
{
if (_certificate != null)
{
return _certificate;
}
using (var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine)) using (var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine))
{ {
store.Open(OpenFlags.ReadWrite); store.Open(OpenFlags.ReadWrite);
@ -22,9 +31,9 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests
{ {
continue; continue;
} }
Certificate = cert; _certificate = cert;
store.Close(); store.Close();
return; return cert;
} }
var parentKey = CreateKeyMaterial(2048); var parentKey = CreateKeyMaterial(2048);
@ -58,17 +67,22 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests
Array.Clear(export, 0, export.Length); Array.Clear(export, 0, export.Length);
// Add the cert to the cert store // Add the cert to the cert store
Certificate = imported; _certificate = imported;
store.Add(certificate: imported); store.Add(certificate: imported);
store.Close(); store.Close();
return imported;
}
} }
} }
public X509Certificate2 Certificate { get; }
public void Dispose() public void Dispose()
{ {
if (_certificate == null)
{
return;
}
using (var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine)) using (var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine))
{ {
store.Open(OpenFlags.ReadWrite); store.Open(OpenFlags.ReadWrite);