Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sign tests #275

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading