Skip to content

Commit

Permalink
Expand S/MIME unit tests to test both CmsRecipient identifier types
Browse files Browse the repository at this point in the history
Work towards fully testing bcgit/bc-csharp#532
  • Loading branch information
jstedfast committed May 10, 2024
1 parent e813f7e commit e19a419
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 46 deletions.
14 changes: 8 additions & 6 deletions UnitTests/Cryptography/ApplicationPkcs7MimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,15 @@ static async Task ImportAllAsync (SecureMimeContext ctx)
}
}

[Test]
public void TestEncryptCmsRecipients ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public void TestEncryptCmsRecipients (SubjectIdentifierType recipientIdentifierType)
{
foreach (var certificate in SecureMimeTestsBase.SupportedCertificates) {
var entity = new TextPart ("plain") { Text = "This is some text..." };
var signer = new CmsSigner (certificate.FileName, "no.secret");
var recipients = new CmsRecipientCollection {
new CmsRecipient (signer.Certificate)
new CmsRecipient (signer.Certificate, recipientIdentifierType)
};

var encrypted = ApplicationPkcs7Mime.Encrypt (recipients, entity);
Expand All @@ -380,14 +381,15 @@ public void TestEncryptCmsRecipients ()
}
}

[Test]
public async Task TestEncryptCmsRecipientsAsync ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public async Task TestEncryptCmsRecipientsAsync (SubjectIdentifierType recipientIdentifierType)
{
foreach (var certificate in SecureMimeTestsBase.SupportedCertificates) {
var entity = new TextPart ("plain") { Text = "This is some text..." };
var signer = new CmsSigner (certificate.FileName, "no.secret");
var recipients = new CmsRecipientCollection {
new CmsRecipient (signer.Certificate)
new CmsRecipient (signer.Certificate, recipientIdentifierType)
};

var encrypted = await ApplicationPkcs7Mime.EncryptAsync (recipients, entity).ConfigureAwait (false);
Expand Down
92 changes: 52 additions & 40 deletions UnitTests/Cryptography/SecureMimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1675,16 +1675,17 @@ public virtual async Task TestSecureMimeMessageEncryptionAsync ()
}
}

[Test]
public virtual void TestSecureMimeEncryption ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public virtual void TestSecureMimeEncryption (SubjectIdentifierType recipientIdentifierType)
{
foreach (var certificate in SupportedCertificates) {
if (!Supports (certificate.PublicKeyAlgorithm))
continue;

var body = new TextPart ("plain") { Text = "This is some cleartext that we'll end up encrypting..." };
var recipients = new CmsRecipientCollection {
new CmsRecipient (certificate.Certificate, SubjectIdentifierType.SubjectKeyIdentifier)
new CmsRecipient (certificate.Certificate, recipientIdentifierType)
};

var encrypted = ApplicationPkcs7Mime.Encrypt (recipients, body);
Expand All @@ -1698,16 +1699,17 @@ public virtual void TestSecureMimeEncryption ()
}
}

[Test]
public virtual async Task TestSecureMimeEncryptionAsync ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public virtual async Task TestSecureMimeEncryptionAsync (SubjectIdentifierType recipientIdentifierType)
{
foreach (var certificate in SupportedCertificates) {
if (!Supports (certificate.PublicKeyAlgorithm))
continue;

var body = new TextPart ("plain") { Text = "This is some cleartext that we'll end up encrypting..." };
var recipients = new CmsRecipientCollection {
new CmsRecipient (certificate.Certificate, SubjectIdentifierType.SubjectKeyIdentifier)
new CmsRecipient (certificate.Certificate, recipientIdentifierType)
};

var encrypted = await ApplicationPkcs7Mime.EncryptAsync (recipients, body);
Expand All @@ -1721,8 +1723,9 @@ public virtual async Task TestSecureMimeEncryptionAsync ()
}
}

[Test]
public virtual void TestSecureMimeEncryptionWithContext ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public virtual void TestSecureMimeEncryptionWithContext (SubjectIdentifierType recipientIdentifierType)
{
var body = new TextPart ("plain") { Text = "This is some cleartext that we'll end up encrypting..." };

Expand All @@ -1734,9 +1737,9 @@ public virtual void TestSecureMimeEncryptionWithContext ()
var recipients = new CmsRecipientCollection ();

if (ctx is WindowsSecureMimeContext)
recipients.Add (new CmsRecipient (certificate.Certificate.AsX509Certificate2 (), SubjectIdentifierType.SubjectKeyIdentifier));
recipients.Add (new CmsRecipient (certificate.Certificate.AsX509Certificate2 (), recipientIdentifierType));
else
recipients.Add (new CmsRecipient (certificate.Certificate, SubjectIdentifierType.SubjectKeyIdentifier));
recipients.Add (new CmsRecipient (certificate.Certificate, recipientIdentifierType));

var encrypted = ApplicationPkcs7Mime.Encrypt (ctx, recipients, body);

Expand All @@ -1755,8 +1758,9 @@ public virtual void TestSecureMimeEncryptionWithContext ()
}
}

[Test]
public virtual async Task TestSecureMimeEncryptionWithContextAsync ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public virtual async Task TestSecureMimeEncryptionWithContextAsync (SubjectIdentifierType recipientIdentifierType)
{
var body = new TextPart ("plain") { Text = "This is some cleartext that we'll end up encrypting..." };

Expand All @@ -1768,9 +1772,9 @@ public virtual async Task TestSecureMimeEncryptionWithContextAsync ()
var recipients = new CmsRecipientCollection ();

if (ctx is WindowsSecureMimeContext)
recipients.Add (new CmsRecipient (certificate.Certificate.AsX509Certificate2 (), SubjectIdentifierType.SubjectKeyIdentifier));
recipients.Add (new CmsRecipient (certificate.Certificate.AsX509Certificate2 (), recipientIdentifierType));
else
recipients.Add (new CmsRecipient (certificate.Certificate, SubjectIdentifierType.SubjectKeyIdentifier));
recipients.Add (new CmsRecipient (certificate.Certificate, recipientIdentifierType));

var encrypted = await ApplicationPkcs7Mime.EncryptAsync (ctx, recipients, body);

Expand All @@ -1789,8 +1793,9 @@ public virtual async Task TestSecureMimeEncryptionWithContextAsync ()
}
}

[Test]
public virtual void TestSecureMimeEncryptionWithAlgorithm ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public virtual void TestSecureMimeEncryptionWithAlgorithm (SubjectIdentifierType recipientIdentifierType)
{
var body = new TextPart ("plain") { Text = "This is some cleartext that we'll end up encrypting..." };

Expand All @@ -1802,9 +1807,9 @@ public virtual void TestSecureMimeEncryptionWithAlgorithm ()
var recipients = new CmsRecipientCollection ();

if (ctx is WindowsSecureMimeContext)
recipients.Add (new CmsRecipient (certificate.Certificate.AsX509Certificate2 (), SubjectIdentifierType.SubjectKeyIdentifier));
recipients.Add (new CmsRecipient (certificate.Certificate.AsX509Certificate2 (), recipientIdentifierType));
else
recipients.Add (new CmsRecipient (certificate.Certificate, SubjectIdentifierType.SubjectKeyIdentifier));
recipients.Add (new CmsRecipient (certificate.Certificate, recipientIdentifierType));

foreach (EncryptionAlgorithm algorithm in Enum.GetValues (typeof (EncryptionAlgorithm))) {
foreach (var recipient in recipients)
Expand Down Expand Up @@ -1886,8 +1891,9 @@ public virtual void TestSecureMimeEncryptionWithAlgorithm ()
}
}

[Test]
public virtual async Task TestSecureMimeEncryptionWithAlgorithmAsync ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public virtual async Task TestSecureMimeEncryptionWithAlgorithmAsync (SubjectIdentifierType recipientIdentifierType)
{
var body = new TextPart ("plain") { Text = "This is some cleartext that we'll end up encrypting..." };

Expand All @@ -1899,9 +1905,9 @@ public virtual async Task TestSecureMimeEncryptionWithAlgorithmAsync ()
var recipients = new CmsRecipientCollection ();

if (ctx is WindowsSecureMimeContext)
recipients.Add (new CmsRecipient (certificate.Certificate.AsX509Certificate2 (), SubjectIdentifierType.SubjectKeyIdentifier));
recipients.Add (new CmsRecipient (certificate.Certificate.AsX509Certificate2 (), recipientIdentifierType));
else
recipients.Add (new CmsRecipient (certificate.Certificate, SubjectIdentifierType.SubjectKeyIdentifier));
recipients.Add (new CmsRecipient (certificate.Certificate, recipientIdentifierType));

foreach (EncryptionAlgorithm algorithm in Enum.GetValues (typeof (EncryptionAlgorithm))) {
foreach (var recipient in recipients)
Expand Down Expand Up @@ -2885,58 +2891,64 @@ public override Task TestSecureMimeMessageEncryptionAsync ()
return base.TestSecureMimeMessageEncryptionAsync ();
}

[Test]
public override void TestSecureMimeEncryption ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public override void TestSecureMimeEncryption (SubjectIdentifierType recipientIdentifierType)
{
if (!IsEnabled)
return;

base.TestSecureMimeEncryption ();
base.TestSecureMimeEncryption (recipientIdentifierType);
}

[Test]
public override Task TestSecureMimeEncryptionAsync ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public override Task TestSecureMimeEncryptionAsync (SubjectIdentifierType recipientIdentifierType)
{
if (!IsEnabled)
return Task.CompletedTask;

return base.TestSecureMimeEncryptionAsync ();
return base.TestSecureMimeEncryptionAsync (recipientIdentifierType);
}

[Test]
public override void TestSecureMimeEncryptionWithContext ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public override void TestSecureMimeEncryptionWithContext (SubjectIdentifierType recipientIdentifierType)
{
if (!IsEnabled)
return;

base.TestSecureMimeEncryptionWithContext ();
base.TestSecureMimeEncryptionWithContext (recipientIdentifierType);
}

[Test]
public override Task TestSecureMimeEncryptionWithContextAsync ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public override Task TestSecureMimeEncryptionWithContextAsync (SubjectIdentifierType recipientIdentifierType)
{
if (!IsEnabled)
return Task.CompletedTask;

return base.TestSecureMimeEncryptionWithContextAsync ();
return base.TestSecureMimeEncryptionWithContextAsync (recipientIdentifierType);
}

[Test]
public override void TestSecureMimeEncryptionWithAlgorithm ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public override void TestSecureMimeEncryptionWithAlgorithm (SubjectIdentifierType recipientIdentifierType)
{
if (!IsEnabled)
return;

base.TestSecureMimeEncryptionWithAlgorithm ();
base.TestSecureMimeEncryptionWithAlgorithm (recipientIdentifierType);
}

[Test]
public override Task TestSecureMimeEncryptionWithAlgorithmAsync ()
[TestCase (SubjectIdentifierType.IssuerAndSerialNumber)]
[TestCase (SubjectIdentifierType.SubjectKeyIdentifier)]
public override Task TestSecureMimeEncryptionWithAlgorithmAsync (SubjectIdentifierType recipientIdentifierType)
{
if (!IsEnabled)
return Task.CompletedTask;

return base.TestSecureMimeEncryptionWithAlgorithmAsync ();
return base.TestSecureMimeEncryptionWithAlgorithmAsync (recipientIdentifierType);
}

[TestCase (DigestAlgorithm.Sha1)]
Expand Down

0 comments on commit e19a419

Please sign in to comment.