Skip to content

Commit

Permalink
Rotate certificate for unit testing (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangpanMS authored Nov 18, 2024
1 parent d450b97 commit 24f4fda
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public void InitializeTest()
[Test]
[TestCase("AME")]
[TestCase("GBL")]
[TestCase("AME Infra CA 02")]
[TestCase("AME Infra CA 06")]
[TestCase("DC=AME")]
[TestCase("DC=GBL")]
[TestCase("CN=AME")]
[TestCase("CN=AME Infra CA 02")]
[TestCase("CN=AME Infra CA 02, DC=AME, DC=GBL")]
[TestCase("CN=AME Infra CA 02,DC=AME,DC=GBL")]
[TestCase("CN=AME Infra CA 06")]
[TestCase("CN=AME Infra CA 06, DC=AME, DC=GBL")]
[TestCase("CN=AME Infra CA 06,DC=AME,DC=GBL")]
public void CertificateManagerSearchesSupportsARangeOfFormatsForIssuersOnCertificates(string issuer)
{
X509Certificate2 certificate = this.mockFixture.Create<X509Certificate2>();
Expand All @@ -67,10 +67,10 @@ public void CertificateManagerDoesNotMismatchIssuersOnCertificates(string issuer

[Test]
[TestCase("virtualclient")]
[TestCase("virtualclient.corp")]
[TestCase("virtualclient.corp.azure.com")]
[TestCase("CN=virtualclient.corp")]
[TestCase("CN=virtualclient.corp.azure.com")]
[TestCase("virtualclient.test.corp")]
[TestCase("virtualclient.test.corp.azure.com")]
[TestCase("CN=virtualclient.test.corp")]
[TestCase("CN=virtualclient.test.corp.azure.com")]
public void CertificateManagerSearchesSupportsARangeOfFormatsForSubjectNamesOnCertificates(string subjectName)
{
X509Certificate2 certificate = this.mockFixture.Create<X509Certificate2>();
Expand All @@ -95,13 +95,13 @@ public async Task CertificateManagerSearchesTheExpectedDirectoryForCertificates(
this.testCertificateManager = new TestCertificateManager(this.mockFixture);

string expectedDirectory = CertificateManager.DefaultUnixCertificateDirectory;
string expectedCertificateFile = this.mockFixture.Combine(expectedDirectory, "A3706B2B12D35F8B2B5F8176F7B6F18534A23FAD");
string expectedCertificateFile = this.mockFixture.Combine(expectedDirectory, "C3F4A77CAD588341B8D62EE4DA02D85E8F100EFA");
bool confirmedDir = false;
bool confirmedFile = false;

// Issuer: AME
// Subject Name: virtualclient.corp.azure.com
// Thumbprint: A3706B2B12D35F8B2B5F8176F7B6F18534A23FAD
// Subject Name: virtualclient.test.corp.azure.com
// Thumbprint: C3F4A77CAD588341B8D62EE4DA02D85E8F100EFA
//
// Note that this is an expired/invalid certificate so there are no security concerns. It is merely
// used for testing purposes.
Expand Down Expand Up @@ -138,35 +138,35 @@ public async Task CertificateManagerSearchesTheExpectedDirectoryForCertificates(
// Expectation:
// We do not need to compare the certificate properties. We just need to ensure we attempted to
// read from the expected directory and that the certificate deserializes without error.
await this.testCertificateManager.GetCertificateFromPathAsync("AME", "virtualclient.corp.azure.com", expectedDirectory);
await this.testCertificateManager.GetCertificateFromPathAsync("AME", "virtualclient.test.corp.azure.com", expectedDirectory);

Assert.IsTrue(confirmedDir);
Assert.IsTrue(confirmedFile);
}

[Test]
[TestCase("AME", "virtualclient.corp.azure.com")]
[TestCase("GBL", "virtualclient.corp.azure.com")]
[TestCase("AME Infra CA 02", "virtualclient")]
[TestCase("AME", "virtualclient.test.corp.azure.com")]
[TestCase("GBL", "virtualclient.test.corp.azure.com")]
[TestCase("AME Infra CA 06", "virtualclient")]
[TestCase("DC=AME", "corp.azure.com")]
[TestCase("DC=GBL", "azure.com")]
[TestCase("CN=AME", "virtualclient.corp.azure.com")]
[TestCase("CN=AME Infra CA 02", "CN=virtualclient.corp.azure.com")]
[TestCase("CN=AME Infra CA 02, DC=AME, DC=GBL", "CN=virtualclient.corp.azure.com")]
[TestCase("CN=AME Infra CA 02,DC=AME,DC=GBL", "CN=virtualclient.corp.azure.com")]
[TestCase("CN=AME", "virtualclient.test.corp.azure.com")]
[TestCase("CN=AME Infra CA 06", "CN=virtualclient.test.corp.azure.com")]
[TestCase("CN=AME Infra CA 06, DC=AME, DC=GBL", "CN=virtualclient.test.corp.azure.com")]
[TestCase("CN=AME Infra CA 06,DC=AME,DC=GBL", "CN=virtualclient.test.corp.azure.com")]
public async Task CertificateManagerHandlesDifferentIssuerAndSubjectNameFormats(string issuer, string subjectName)
{
this.mockFixture.Setup(PlatformID.Unix);
this.testCertificateManager = new TestCertificateManager(this.mockFixture);

string expectedDirectory = CertificateManager.DefaultUnixCertificateDirectory;
string expectedCertificateFile = this.mockFixture.Combine(expectedDirectory, "A3706B2B12D35F8B2B5F8176F7B6F18534A23FAD");
string expectedCertificateFile = this.mockFixture.Combine(expectedDirectory, "C3F4A77CAD588341B8D62EE4DA02D85E8F100EFA");
bool confirmedDir = false;
bool confirmedFile = false;

// Issuer: AME
// Subject Name: virtualclient.corp.azure.com
// Thumbprint: A3706B2B12D35F8B2B5F8176F7B6F18534A23FAD
// Subject Name: virtualclient.test.corp.azure.com
// Thumbprint: C3F4A77CAD588341B8D62EE4DA02D85E8F100EFA
//
// Note that this is an expired/invalid certificate so there are no security concerns. It is merely
// used for testing purposes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ private static X509Certificate2 CreateCertificate(bool withPrivateKey = false)
if (withPrivateKey)
{
certificate = new X509Certificate2(
File.ReadAllBytes(Path.Combine(resourcesDirectory, "testcertificate2.private")),
File.ReadAllBytes(Path.Combine(resourcesDirectory, "test-certificate.private")),
string.Empty.ToSecureString(),
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
}
else
{
certificate = new X509Certificate2(
File.ReadAllBytes(Path.Combine(resourcesDirectory, "testcertificate2.private")));
File.ReadAllBytes(Path.Combine(resourcesDirectory, "test-certificate.private")));
}

return certificate;
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</ItemGroup>

<ItemGroup>
<None Update="Resources\testcertificate2.private">
<None Update="Resources\test-certificate.private">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down

0 comments on commit 24f4fda

Please sign in to comment.