-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0cda50
commit 1442ca3
Showing
6 changed files
with
68 additions
and
9 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
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
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
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
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 |
---|---|---|
@@ -1,9 +1,66 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace CoseSignTool.tests; | ||
namespace CoseSignUnitTests; | ||
|
||
internal class ValidateCommandTests | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
using CST = CoseSignTool.CoseSignTool; | ||
|
||
[TestClass] | ||
public class ValidateCommandTests | ||
{ | ||
// Placeholder for tests for the ValidateCommand class | ||
// Certificates | ||
private static readonly X509Certificate2 SelfSignedCert = TestCertificateUtils.CreateCertificate(nameof(CoseHandlerSignValidateTests) + " self signed"); // A self-signed cert | ||
private static readonly X509Certificate2Collection CertChain1 = TestCertificateUtils.CreateTestChain(nameof(CoseHandlerSignValidateTests) + " set 1"); // Two complete cert chains | ||
private static readonly X509Certificate2Collection CertChain2 = TestCertificateUtils.CreateTestChain(nameof(CoseHandlerSignValidateTests) + " set 2"); | ||
private static readonly X509Certificate2 Root1Priv = CertChain1[0]; // Roots from the chains | ||
private static readonly X509Certificate2 Root2Priv = CertChain2[0]; | ||
private static readonly X509Certificate2 Int1Priv = CertChain1[1]; | ||
private static readonly X509Certificate2 Leaf1Priv = CertChain1[^1]; // Leaf node certs | ||
private static readonly X509Certificate2 Leaf2Priv = CertChain2[^1]; | ||
|
||
// File paths to export them to | ||
private static readonly string PrivateKeyCertFileSelfSigned = Path.GetTempFileName() + "_SelfSigned.pfx"; | ||
private static readonly string PublicKeyCertFileSelfSigned = Path.GetTempFileName() + "_SelfSigned.cer"; | ||
private static readonly string PrivateKeyRootCertFile = Path.GetTempFileName() + ".pfx"; | ||
private static readonly string PublicKeyIntermediateCertFile = Path.GetTempFileName() + ".cer"; | ||
private static readonly string PublicKeyRootCertFile = Path.GetTempFileName() + ".cer"; | ||
private static readonly string PrivateKeyCertFileChained = Path.GetTempFileName() + ".pfx"; | ||
private static readonly string PrivateKeyCertFileChainedWithPassword = Path.GetTempFileName() + ".pfx"; | ||
private static readonly string CertPassword = Guid.NewGuid().ToString(); | ||
|
||
|
||
private static string PayloadFile; | ||
Check notice Code scanning / CodeQL Missed 'readonly' opportunity Note
Field 'PayloadFile' can be 'readonly'.
|
||
private static readonly byte[] Payload1Bytes = Encoding.ASCII.GetBytes("Payload1!"); | ||
|
||
public ValidateCommandTests() | ||
{ | ||
// make payload file | ||
PayloadFile = Path.GetTempFileName(); | ||
Check notice Code scanning / CodeQL Static field written by instance method Note
Write to static field from instance method, property, or constructor.
|
||
File.WriteAllBytes(PayloadFile, Payload1Bytes); | ||
|
||
// export generated certs to files | ||
File.WriteAllBytes(PrivateKeyCertFileSelfSigned, SelfSignedCert.Export(X509ContentType.Pkcs12)); | ||
File.WriteAllBytes(PublicKeyCertFileSelfSigned, SelfSignedCert.Export(X509ContentType.Cert)); | ||
File.WriteAllBytes(PrivateKeyRootCertFile, Root1Priv.Export(X509ContentType.Pkcs12)); | ||
File.WriteAllBytes(PublicKeyRootCertFile, Root1Priv.Export(X509ContentType.Cert)); | ||
File.WriteAllBytes(PublicKeyIntermediateCertFile, Int1Priv.Export(X509ContentType.Cert)); | ||
File.WriteAllBytes(PrivateKeyCertFileChained, Leaf1Priv.Export(X509ContentType.Pkcs12)); | ||
File.WriteAllBytes(PrivateKeyCertFileChainedWithPassword, Leaf1Priv.Export(X509ContentType.Pkcs12, CertPassword)); | ||
} | ||
|
||
// Validates that signatures made from untrusted chains are rejected | ||
[TestMethod] | ||
public void ValidateTrustTest() | ||
{ | ||
// sign detached | ||
string[] args1 = { "sign", @"/p", PayloadFile, @"/pfx", PrivateKeyCertFileChained }; | ||
CST.Main(args1).Should().Be((int)ExitCode.Success, "Detach sign failed."); | ||
|
||
// validate without the root cert installed or passed in | ||
string[] args2 = { "validate", @"/payload", PayloadFile, @"/sf", PayloadFile + ".cose" }; | ||
CST.Main(args2).Should().Be((int)ExitCode.CertificateChainValidationFailure); | ||
} | ||
} |
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