Skip to content

Commit

Permalink
Merge pull request #273 from mattosaurus/chore/add-recipient-tests
Browse files Browse the repository at this point in the history
Add recipient tests
  • Loading branch information
mattosaurus authored Jan 22, 2024
2 parents 729b977 + 7d586cd commit 39af9a7
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 72 deletions.
32 changes: 0 additions & 32 deletions PgpCore.Tests/UnitTests/GenerateKey/KeyAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,37 +290,5 @@ await pgp.GenerateKeyAsync(
}
}
}

private static PgpPublicKey ReadPublicKey(Stream inputStream)
{
PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(inputStream));
foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
{
foreach (PgpPublicKey k in kRing.GetPublicKeys())
{
if (k.IsEncryptionKey)
return k;
}
}
throw new ArgumentException("No encryption key found in public key ring.");
}

private static IEnumerable<T> GetEnumValues<T>() where T : struct, IConvertible
{
foreach (T enumValue in Enum.GetValues(typeof(T)))
{
yield return enumValue;
}
}

public static IEnumerable<object[]> GetAllCombinations()
{
foreach (CompressionAlgorithmTag compressionAlgorithmTag in GetEnumValues<CompressionAlgorithmTag>())
foreach (HashAlgorithmTag hashAlgorithmTag in GetEnumValues<HashAlgorithmTag>())
foreach (SymmetricKeyAlgorithmTag symmetricKeyAlgorithmTag in GetEnumValues<SymmetricKeyAlgorithmTag>())
{
yield return new object[] { compressionAlgorithmTag, hashAlgorithmTag, symmetricKeyAlgorithmTag };
}
}
}
}
32 changes: 0 additions & 32 deletions PgpCore.Tests/UnitTests/GenerateKey/KeySync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,37 +289,5 @@ public void GenerateKey_CreatePublicAndPrivateKeysWithExpiryDate_ShouldCreateKey
}
}
}

private static PgpPublicKey ReadPublicKey(Stream inputStream)
{
PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(inputStream));
foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
{
foreach (PgpPublicKey k in kRing.GetPublicKeys())
{
if (k.IsEncryptionKey)
return k;
}
}
throw new ArgumentException("No encryption key found in public key ring.");
}

private static IEnumerable<T> GetEnumValues<T>() where T : struct, IConvertible
{
foreach (T enumValue in Enum.GetValues(typeof(T)))
{
yield return enumValue;
}
}

public static IEnumerable<object[]> GetAllCombinations()
{
foreach (CompressionAlgorithmTag compressionAlgorithmTag in GetEnumValues<CompressionAlgorithmTag>())
foreach (HashAlgorithmTag hashAlgorithmTag in GetEnumValues<HashAlgorithmTag>())
foreach (SymmetricKeyAlgorithmTag symmetricKeyAlgorithmTag in GetEnumValues<SymmetricKeyAlgorithmTag>())
{
yield return new object[] { compressionAlgorithmTag, hashAlgorithmTag, symmetricKeyAlgorithmTag };
}
}
}
}
91 changes: 91 additions & 0 deletions PgpCore.Tests/UnitTests/Recipient/RecipientsSync.File.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using FluentAssertions.Execution;
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using System.IO;
using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Bcpg;

namespace PgpCore.Tests.UnitTests.Recipient
{
public class RecipientsSync : TestBase
{
[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
public void GetRecipients_GetTheRecipientOfEncyptedMessage_ShouldReturnRecipientId(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
testFactory.Arrange(keyType, FileType.Known);
EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo);
PGP pgpEncrypt = new PGP(encryptionKeys);

// Act
pgpEncrypt.Encrypt(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo);
IEnumerable<long> recipients = pgpEncrypt.GetRecipients(testFactory.EncryptedContentFileInfo);

// Assert
using (new AssertionScope())
{
testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue();
recipients.Should().NotBeEmpty();
recipients.Should().HaveCount(1);

using (Stream publicKeyStream = testFactory.PublicKeyFileInfo.OpenRead())
{
PgpPublicKey publicKey = ReadPublicKey(publicKeyStream);
recipients.Single().Should().Be(publicKey.KeyId);
}
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
public void GetRecipients_GetTheRecipientsOfEncyptedMessage_ShouldReturnRecipientIds(KeyType keyType)
{
// Arrange
TestFactory testFactory1 = new TestFactory();
TestFactory testFactory2 = new TestFactory();
testFactory1.Arrange(keyType, FileType.Known);
testFactory2.Arrange(keyType, FileType.Known);
EncryptionKeys encryptionKeys = new EncryptionKeys(new List<FileInfo>() { testFactory1.PublicKeyFileInfo, testFactory2.PublicKeyFileInfo });
PGP pgpEncrypt = new PGP(encryptionKeys);

// Act
pgpEncrypt.Encrypt(testFactory1.ContentFileInfo, testFactory1.EncryptedContentFileInfo);
IEnumerable<long> recipients = pgpEncrypt.GetRecipients(testFactory1.EncryptedContentFileInfo);

// Assert
using (new AssertionScope())
{
testFactory1.EncryptedContentFileInfo.Exists.Should().BeTrue();
recipients.Should().NotBeEmpty();
recipients.Should().HaveCount(2);

using (Stream publicKeyStream = testFactory1.PublicKeyFileInfo.OpenRead())
{
PgpPublicKey publicKey = ReadPublicKey(publicKeyStream);
recipients.Should().Contain(publicKey.KeyId);
}

using (Stream publicKeyStream = testFactory2.PublicKeyFileInfo.OpenRead())
{
PgpPublicKey publicKey = ReadPublicKey(publicKeyStream);
recipients.Should().Contain(publicKey.KeyId);
}
}

// Teardown
testFactory1.Teardown();
testFactory2.Teardown();
}
}
}
24 changes: 24 additions & 0 deletions PgpCore.Tests/UnitTests/TestBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Org.BouncyCastle.Bcpg;
using Org.BouncyCastle.Bcpg.OpenPgp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -46,5 +48,27 @@ public static IEnumerable<object[]> GetSymmetricAlgorithimTags()
yield return new object[] { symmetricKeyAlgorithmTag };
}
}

public static PgpPublicKey ReadPublicKey(Stream inputStream)
{
PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(inputStream));
foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
{
foreach (PgpPublicKey k in kRing.GetPublicKeys())
{
if (k.IsEncryptionKey)
return k;
}
}
throw new ArgumentException("No encryption key found in public key ring.");
}

public static IEnumerable<T> GetEnumValues<T>() where T : struct, IConvertible
{
foreach (T enumValue in Enum.GetValues(typeof(T)))
{
yield return enumValue;
}
}
}
}
16 changes: 8 additions & 8 deletions PgpCore/PGP.RecipientsSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public partial class PGP : IRecipientsSync
/// </summary>
/// <param name="inputFileInfo">PGP encrypted data file path</param>
/// <returns>Enumerable of public key ids. Value "0" means that the recipient is hidden.</returns>
public IEnumerable<long> GetFileRecipients(FileInfo inputFileInfo)
public IEnumerable<long> GetRecipients(FileInfo inputFileInfo)
{
if (inputFileInfo == null)
throw new ArgumentException("InputFileInfo");
Expand All @@ -28,15 +28,15 @@ public IEnumerable<long> GetFileRecipients(FileInfo inputFileInfo)
throw new FileNotFoundException($"Encrypted File [{inputFileInfo.FullName}] not found.");

using (Stream inputStream = File.OpenRead(inputFileInfo.FullName))
return GetStreamRecipients(inputStream);
return GetRecipients(inputStream);
}

/// <summary>
/// PGP get a recipients keys id of an encrypted stream.
/// </summary>
/// <param name="inputStream">PGP encrypted data stream</param>
/// <returns>Enumerable of public key ids. Value "0" means that the recipient is hidden.</returns>
public IEnumerable<long> GetStreamRecipients(Stream inputStream)
public IEnumerable<long> GetRecipients(Stream inputStream)
{
if (inputStream == null)
throw new ArgumentException("InputStream");
Expand Down Expand Up @@ -66,20 +66,20 @@ public IEnumerable<long> GetStreamRecipients(Stream inputStream)
/// </summary>
/// <param name="input">PGP encrypted string</param>
/// <returns>Enumerable of public key ids. Value "0" means that the recipient is hidden.</returns>
public IEnumerable<long> GetArmoredStringRecipients(string input)
public IEnumerable<long> GetRecipients(string input)
{
if (string.IsNullOrEmpty(input))
throw new ArgumentException("Input");

using (Stream inputStream = input.GetStream())
return GetStreamRecipients(inputStream);
return GetRecipients(inputStream);
}

public IEnumerable<long> GetRecipients(FileInfo inputFileInfo) => GetFileRecipients(inputFileInfo);
public IEnumerable<long> GetFileRecipients(FileInfo inputFileInfo) => GetRecipients(inputFileInfo);

public IEnumerable<long> GetRecipients(Stream inputStream) => GetStreamRecipients(inputStream);
public IEnumerable<long> GetStreamRecipients(Stream inputStream) => GetRecipients(inputStream);

public IEnumerable<long> GetRecipients(string input) => GetArmoredStringRecipients(input);
public IEnumerable<long> GetArmoredStringRecipients(string input) => GetRecipients(input);

#endregion GetArmoredStringRecipients
}
Expand Down

0 comments on commit 39af9a7

Please sign in to comment.