Skip to content

Commit

Permalink
Merge pull request #275 from mattosaurus/chore/add-sign-tests
Browse files Browse the repository at this point in the history
Add sign tests
  • Loading branch information
mattosaurus authored Jan 23, 2024
2 parents 39af9a7 + 883d0b3 commit 4c32816
Show file tree
Hide file tree
Showing 6 changed files with 1,624 additions and 0 deletions.
279 changes: 279 additions & 0 deletions PgpCore.Tests/UnitTests/Sign/SignAsync.File.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
using FluentAssertions.Execution;
using FluentAssertions;
using PgpCore.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using System.IO;

namespace PgpCore.Tests.UnitTests.Sign
{
public class SignAsync_File : TestBase
{
[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task SignAsync_SignMessageWithDefaultProperties_ShouldSignMessage(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
await testFactory.ArrangeAsync(keyType, FileType.Known);
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password);
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo);
PGP pgpSign = new PGP(signingKeys);
PGP pgpVerify = new PGP(verificationKeys);

// Act
await pgpSign.SignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo);
bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentFileInfo);

// Assert
using (new AssertionScope())
{
verified.Should().BeTrue();
}

using (new AssertionScope())
{
testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue();
PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(testFactory.EncryptedContentFileInfo);
pgpInspectResult.IsEncrypted.Should().BeFalse();
pgpInspectResult.IsSigned.Should().BeTrue();
pgpInspectResult.IsArmored.Should().BeTrue();
pgpInspectResult.FileName.Should().Be(testFactory.ContentFileInfo.Name);
pgpInspectResult.MessageHeaders.Should().HaveCount(1);
pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version");
pgpInspectResult.MessageHeaders.Single().Value.Should().Be(VERSION);
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task SignAsync_SignMessageAsBinary_ShouldSignMessage(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
await testFactory.ArrangeAsync(keyType, FileType.Known);
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password);
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo);
PGP pgpSign = new PGP(signingKeys);
PGP pgpVerify = new PGP(verificationKeys);

// Act
await pgpSign.SignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, armor: false);
bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentFileInfo);

// Assert
using (new AssertionScope())
{
verified.Should().BeTrue();
}

using (new AssertionScope())
{
testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue();
PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(testFactory.EncryptedContentFileInfo);
pgpInspectResult.IsEncrypted.Should().BeFalse();
pgpInspectResult.IsSigned.Should().BeTrue();
pgpInspectResult.IsArmored.Should().BeFalse();
pgpInspectResult.FileName.Should().NotBeNullOrEmpty();
pgpInspectResult.MessageHeaders.Should().BeNullOrEmpty();
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task SignAsync_SignMessageWithName_ShouldSignMessage(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
await testFactory.ArrangeAsync(keyType, FileType.Known);
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password);
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo);
PGP pgpSign = new PGP(signingKeys);
PGP pgpVerify = new PGP(verificationKeys);

// Act
await pgpSign.SignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, name: TESTNAME);
bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentFileInfo);

// Assert
using (new AssertionScope())
{
verified.Should().BeTrue();
}

using (new AssertionScope())
{
testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue();
PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(testFactory.EncryptedContentFileInfo);
pgpInspectResult.IsEncrypted.Should().BeFalse();
pgpInspectResult.IsSigned.Should().BeTrue();
pgpInspectResult.IsArmored.Should().BeTrue();
pgpInspectResult.FileName.Should().Be(TESTNAME);
pgpInspectResult.MessageHeaders.Should().HaveCount(1);
pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version");
pgpInspectResult.MessageHeaders.Single().Value.Should().Be(VERSION);
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task SignAsync_SignMessageWithHeaders_ShouldSignMessage(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
await testFactory.ArrangeAsync(keyType, FileType.Known);
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password);
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo);
PGP pgpSign = new PGP(signingKeys);
PGP pgpVerify = new PGP(verificationKeys);

// Act
await pgpSign.SignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, headers: new Dictionary<string, string> { { TESTHEADERKEY, TESTHEADERVALUE } });
bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentFileInfo);

// Assert
using (new AssertionScope())
{
verified.Should().BeTrue();
}

using (new AssertionScope())
{
testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue();
PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(testFactory.EncryptedContentFileInfo);
pgpInspectResult.IsEncrypted.Should().BeFalse();
pgpInspectResult.IsSigned.Should().BeTrue();
pgpInspectResult.IsArmored.Should().BeTrue();
pgpInspectResult.FileName.Should().Be(testFactory.ContentFileInfo.Name);
pgpInspectResult.MessageHeaders.Should().HaveCount(2);
pgpInspectResult.MessageHeaders.First().Key.Should().Be("Version");
pgpInspectResult.MessageHeaders.First().Value.Should().Be(VERSION);
pgpInspectResult.MessageHeaders.Last().Key.Should().Be(TESTHEADERKEY);
pgpInspectResult.MessageHeaders.Last().Value.Should().Be(TESTHEADERVALUE);
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task SignAsync_SignMessageWithOldFormat_ShouldSignMessage(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
await testFactory.ArrangeAsync(keyType, FileType.Known);
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password);
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo);
PGP pgpSign = new PGP(signingKeys);
PGP pgpVerify = new PGP(verificationKeys);

// Act
await pgpSign.SignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, oldFormat: true);
bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentFileInfo);

// Assert
using (new AssertionScope())
{
verified.Should().BeTrue();
}

using (new AssertionScope())
{
testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue();
PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(testFactory.EncryptedContentFileInfo);
pgpInspectResult.IsEncrypted.Should().BeFalse();
pgpInspectResult.IsSigned.Should().BeTrue();
pgpInspectResult.IsArmored.Should().BeTrue();
pgpInspectResult.FileName.Should().Be(testFactory.ContentFileInfo.Name);
pgpInspectResult.MessageHeaders.Should().HaveCount(1);
pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version");
pgpInspectResult.MessageHeaders.Single().Value.Should().Be(VERSION);
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task ClearSignAsync_SignMessageWithDefaultProperties_ShouldSignMessage(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
await testFactory.ArrangeAsync(keyType, FileType.Known);
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password);
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo);
PGP pgpSign = new PGP(signingKeys);
PGP pgpVerify = new PGP(verificationKeys);

// Act
await pgpSign.ClearSignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo);
bool verified = await pgpVerify.VerifyClearAsync(testFactory.EncryptedContentFileInfo);

// Assert
using (new AssertionScope())
{
verified.Should().BeTrue();
File.ReadAllText(testFactory.EncryptedContentFileInfo.FullName).Should().Contain(testFactory.Content);
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task ClearSignAsync_SignMessageWithHeaders_ShouldSignMessage(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
await testFactory.ArrangeAsync(keyType, FileType.Known);
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password);
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo);
PGP pgpSign = new PGP(signingKeys);
PGP pgpVerify = new PGP(verificationKeys);

// Act
await pgpSign.ClearSignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, headers: new Dictionary<string, string> { { TESTHEADERKEY, TESTHEADERVALUE } });
bool verified = await pgpVerify.VerifyClearAsync(testFactory.EncryptedContentFileInfo);

// Assert
using (new AssertionScope())
{
verified.Should().BeTrue();
File.ReadAllText(testFactory.EncryptedContentFileInfo.FullName).Should().Contain(testFactory.Content);
}

// Teardown
testFactory.Teardown();
}
}
}
Loading

0 comments on commit 4c32816

Please sign in to comment.