From 883d0b367da6c2a6954d7512f90370fe652f1404 Mon Sep 17 00:00:00 2001 From: mattosaurus Date: Tue, 23 Jan 2024 12:12:11 +0000 Subject: [PATCH] Add sign tests --- .../UnitTests/Sign/SignAsync.File.cs | 279 +++++++++++++++++ .../UnitTests/Sign/SignAsync.Stream.cs | 293 ++++++++++++++++++ .../UnitTests/Sign/SignAsync.String.cs | 240 ++++++++++++++ PgpCore.Tests/UnitTests/Sign/SignSync.File.cs | 279 +++++++++++++++++ .../UnitTests/Sign/SignSync.Stream.cs | 293 ++++++++++++++++++ .../UnitTests/Sign/SignSync.String.cs | 240 ++++++++++++++ 6 files changed, 1624 insertions(+) create mode 100644 PgpCore.Tests/UnitTests/Sign/SignAsync.File.cs create mode 100644 PgpCore.Tests/UnitTests/Sign/SignAsync.Stream.cs create mode 100644 PgpCore.Tests/UnitTests/Sign/SignAsync.String.cs create mode 100644 PgpCore.Tests/UnitTests/Sign/SignSync.File.cs create mode 100644 PgpCore.Tests/UnitTests/Sign/SignSync.Stream.cs create mode 100644 PgpCore.Tests/UnitTests/Sign/SignSync.String.cs diff --git a/PgpCore.Tests/UnitTests/Sign/SignAsync.File.cs b/PgpCore.Tests/UnitTests/Sign/SignAsync.File.cs new file mode 100644 index 0000000..dd76286 --- /dev/null +++ b/PgpCore.Tests/UnitTests/Sign/SignAsync.File.cs @@ -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 { { 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 { { 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(); + } + } +} diff --git a/PgpCore.Tests/UnitTests/Sign/SignAsync.Stream.cs b/PgpCore.Tests/UnitTests/Sign/SignAsync.Stream.cs new file mode 100644 index 0000000..b09ea5b --- /dev/null +++ b/PgpCore.Tests/UnitTests/Sign/SignAsync.Stream.cs @@ -0,0 +1,293 @@ +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_Stream : 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.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpSign.SignAsync(testFactory.ContentStream, outputFileStream); + + bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentStream); + + // 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(DEFAULTNAME); + 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.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpSign.SignAsync(testFactory.ContentStream, outputFileStream, armor: false); + + bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentStream); + + // 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.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpSign.SignAsync(testFactory.ContentStream, outputFileStream, name: TESTNAME); + + bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentStream); + + // 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.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpSign.SignAsync(testFactory.ContentStream, outputFileStream, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentStream); + + // 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(DEFAULTNAME); + 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.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpSign.SignAsync(testFactory.ContentStream, outputFileStream, oldFormat: true); + + bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentStream); + + // 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(DEFAULTNAME); + 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.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpSign.ClearSignAsync(testFactory.ContentStream, outputFileStream); + + bool verified = await pgpVerify.VerifyClearAsync(testFactory.EncryptedContentStream); + + // 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.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpSign.ClearSignAsync(testFactory.ContentStream, outputFileStream, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + bool verified = await pgpVerify.VerifyClearAsync(testFactory.EncryptedContentStream); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + File.ReadAllText(testFactory.EncryptedContentFileInfo.FullName).Should().Contain(testFactory.Content); + } + + // Teardown + testFactory.Teardown(); + } + } +} diff --git a/PgpCore.Tests/UnitTests/Sign/SignAsync.String.cs b/PgpCore.Tests/UnitTests/Sign/SignAsync.String.cs new file mode 100644 index 0000000..00daa16 --- /dev/null +++ b/PgpCore.Tests/UnitTests/Sign/SignAsync.String.cs @@ -0,0 +1,240 @@ +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_String : 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.PrivateKey, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKey); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + string signedContent = await pgpSign.SignAsync(testFactory.Content); + bool verified = await pgpVerify.VerifyAsync(signedContent); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + signedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(signedContent); + pgpInspectResult.IsEncrypted.Should().BeFalse(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(DEFAULTNAME); + 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_SignMessageWithName_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKey, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKey); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + string signedContent = await pgpSign.SignAsync(testFactory.Content, name: TESTNAME); + bool verified = await pgpVerify.VerifyAsync(signedContent); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + signedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(signedContent); + 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.PrivateKey, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKey); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + string signedContent = await pgpSign.SignAsync(testFactory.Content, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + bool verified = await pgpVerify.VerifyAsync(signedContent); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + signedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(signedContent); + pgpInspectResult.IsEncrypted.Should().BeFalse(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(DEFAULTNAME); + 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.PrivateKey, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKey); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + string signedContent = await pgpSign.SignAsync(testFactory.Content, oldFormat: true); + bool verified = await pgpVerify.VerifyAsync(signedContent); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + signedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(signedContent); + pgpInspectResult.IsEncrypted.Should().BeFalse(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(DEFAULTNAME); + 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.PrivateKey, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKey); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + string signedContent = await pgpSign.ClearSignAsync(testFactory.Content); + bool verified = await pgpVerify.VerifyClearAsync(signedContent); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + signedContent.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.PrivateKey, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKey); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + string signedContent = await pgpSign.ClearSignAsync(testFactory.Content, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + bool verified = await pgpVerify.VerifyClearAsync(signedContent); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + signedContent.Should().Contain(testFactory.Content); + } + + // Teardown + testFactory.Teardown(); + } + } +} diff --git a/PgpCore.Tests/UnitTests/Sign/SignSync.File.cs b/PgpCore.Tests/UnitTests/Sign/SignSync.File.cs new file mode 100644 index 0000000..419e237 --- /dev/null +++ b/PgpCore.Tests/UnitTests/Sign/SignSync.File.cs @@ -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 SignSync_File : TestBase + { + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void Sign_SignMessageWithDefaultProperties_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(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 + pgpSign.Sign(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo); + bool verified = pgpVerify.Verify(testFactory.EncryptedContentFileInfo); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(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 void Sign_SignMessageAsBinary_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(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 + pgpSign.Sign(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, armor: false); + bool verified = pgpVerify.Verify(testFactory.EncryptedContentFileInfo); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(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 void Sign_SignMessageWithName_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(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 + pgpSign.Sign(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, name: TESTNAME); + bool verified = pgpVerify.Verify(testFactory.EncryptedContentFileInfo); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(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 void Sign_SignMessageWithHeaders_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(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 + pgpSign.Sign(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + bool verified = pgpVerify.Verify(testFactory.EncryptedContentFileInfo); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(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 void Sign_SignMessageWithOldFormat_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(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 + pgpSign.Sign(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, oldFormat: true); + bool verified = pgpVerify.Verify(testFactory.EncryptedContentFileInfo); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(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 void ClearSign_SignMessageWithDefaultProperties_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(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 + pgpSign.ClearSign(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo); + bool verified = pgpVerify.VerifyClear(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 void ClearSign_SignMessageWithHeaders_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(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 + pgpSign.ClearSign(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + bool verified = pgpVerify.VerifyClear(testFactory.EncryptedContentFileInfo); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + File.ReadAllText(testFactory.EncryptedContentFileInfo.FullName).Should().Contain(testFactory.Content); + } + + // Teardown + testFactory.Teardown(); + } + } +} diff --git a/PgpCore.Tests/UnitTests/Sign/SignSync.Stream.cs b/PgpCore.Tests/UnitTests/Sign/SignSync.Stream.cs new file mode 100644 index 0000000..fb3be23 --- /dev/null +++ b/PgpCore.Tests/UnitTests/Sign/SignSync.Stream.cs @@ -0,0 +1,293 @@ +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 SignSync_Stream : TestBase + { + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void Sign_SignMessageWithDefaultProperties_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpSign.Sign(testFactory.ContentStream, outputFileStream); + + bool verified = pgpVerify.Verify(testFactory.EncryptedContentStream); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeFalse(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(DEFAULTNAME); + 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 void Sign_SignMessageAsBinary_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpSign.Sign(testFactory.ContentStream, outputFileStream, armor: false); + + bool verified = pgpVerify.Verify(testFactory.EncryptedContentStream); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(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 void Sign_SignMessageWithName_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpSign.Sign(testFactory.ContentStream, outputFileStream, name: TESTNAME); + + bool verified = pgpVerify.Verify(testFactory.EncryptedContentStream); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(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 void Sign_SignMessageWithHeaders_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpSign.Sign(testFactory.ContentStream, outputFileStream, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + bool verified = pgpVerify.Verify(testFactory.EncryptedContentStream); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeFalse(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(DEFAULTNAME); + 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 void Sign_SignMessageWithOldFormat_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpSign.Sign(testFactory.ContentStream, outputFileStream, oldFormat: true); + + bool verified = pgpVerify.Verify(testFactory.EncryptedContentStream); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeFalse(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(DEFAULTNAME); + 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 void ClearSign_SignMessageWithDefaultProperties_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpSign.ClearSign(testFactory.ContentStream, outputFileStream); + + bool verified = pgpVerify.VerifyClear(testFactory.EncryptedContentStream); + + // 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 void ClearSign_SignMessageWithHeaders_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyStream, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyStream); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpSign.ClearSign(testFactory.ContentStream, outputFileStream, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + bool verified = pgpVerify.VerifyClear(testFactory.EncryptedContentStream); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + File.ReadAllText(testFactory.EncryptedContentFileInfo.FullName).Should().Contain(testFactory.Content); + } + + // Teardown + testFactory.Teardown(); + } + } +} diff --git a/PgpCore.Tests/UnitTests/Sign/SignSync.String.cs b/PgpCore.Tests/UnitTests/Sign/SignSync.String.cs new file mode 100644 index 0000000..88824ad --- /dev/null +++ b/PgpCore.Tests/UnitTests/Sign/SignSync.String.cs @@ -0,0 +1,240 @@ +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 SignSync_String : TestBase + { + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void Sign_SignMessageWithDefaultProperties_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKey, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKey); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + string signedContent = pgpSign.Sign(testFactory.Content); + bool verified = pgpVerify.Verify(signedContent); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + signedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(signedContent); + pgpInspectResult.IsEncrypted.Should().BeFalse(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(DEFAULTNAME); + 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 void Sign_SignMessageWithName_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKey, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKey); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + string signedContent = pgpSign.Sign(testFactory.Content, name: TESTNAME); + bool verified = pgpVerify.Verify(signedContent); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + signedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(signedContent); + 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 void Sign_SignMessageWithHeaders_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKey, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKey); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + string signedContent = pgpSign.Sign(testFactory.Content, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + bool verified = pgpVerify.Verify(signedContent); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + signedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(signedContent); + pgpInspectResult.IsEncrypted.Should().BeFalse(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(DEFAULTNAME); + 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 void Sign_SignMessageWithOldFormat_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKey, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKey); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + string signedContent = pgpSign.Sign(testFactory.Content, oldFormat: true); + bool verified = pgpVerify.Verify(signedContent); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + } + + using (new AssertionScope()) + { + signedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpSign.Inspect(signedContent); + pgpInspectResult.IsEncrypted.Should().BeFalse(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(DEFAULTNAME); + 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 void ClearSign_SignMessageWithDefaultProperties_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKey, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKey); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + string signedContent = pgpSign.ClearSign(testFactory.Content); + bool verified = pgpVerify.VerifyClear(signedContent); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + signedContent.Should().Contain(testFactory.Content); + } + + // Teardown + testFactory.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void ClearSign_SignMessageWithHeaders_ShouldSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKey, testFactory.Password); + EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKey); + PGP pgpSign = new PGP(signingKeys); + PGP pgpVerify = new PGP(verificationKeys); + + // Act + string signedContent = pgpSign.ClearSign(testFactory.Content, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + bool verified = pgpVerify.VerifyClear(signedContent); + + // Assert + using (new AssertionScope()) + { + verified.Should().BeTrue(); + signedContent.Should().Contain(testFactory.Content); + } + + // Teardown + testFactory.Teardown(); + } + } +}