diff --git a/PgpCore.Tests/UnitTests/Encrypt/EncryptAsync.File.cs b/PgpCore.Tests/UnitTests/Encrypt/EncryptAsync.File.cs new file mode 100644 index 0000000..194fb13 --- /dev/null +++ b/PgpCore.Tests/UnitTests/Encrypt/EncryptAsync.File.cs @@ -0,0 +1,454 @@ +using FluentAssertions.Execution; +using FluentAssertions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; +using PgpCore.Models; + +namespace PgpCore.Tests.UnitTests.Encrypt +{ + public class EncryptAsync_File : TestBase + { + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAsync_EncryptMessageWithDefaultProperties_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + await pgpEncrypt.EncryptAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAsync_EncryptMessageAsBinary_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + await pgpEncrypt.EncryptAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, armor: false); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeFalse(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().NotBeNullOrEmpty(); + pgpInspectResult.MessageHeaders.Should().BeNullOrEmpty(); + } + + // Teardown + testFactory.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAsync_EncryptMessageWithoutIntegrityCheck_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + await pgpEncrypt.EncryptAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, withIntegrityCheck: false); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeFalse(); + 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 EncryptAsync_EncryptMessageWithName_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + await pgpEncrypt.EncryptAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, name: TESTNAME); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAsync_EncryptMessageWithHeaders_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + await pgpEncrypt.EncryptAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAsync_EncryptMessageWithOldFormat_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + await pgpEncrypt.EncryptAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, oldFormat: true); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAndSignAsync_EncryptAndSignMessageWithDefaultProperties_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.ContentFileInfo, testFactoryEncrypt.EncryptedContentFileInfo); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(testFactoryEncrypt.ContentFileInfo.Name); + pgpInspectResult.MessageHeaders.Should().HaveCount(1); + pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version"); + pgpInspectResult.MessageHeaders.Single().Value.Should().Be(VERSION); + } + + // Teardown + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageAsBinary_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.ContentFileInfo, testFactoryEncrypt.EncryptedContentFileInfo, armor: false); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeFalse(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().NotBeNullOrEmpty(); + pgpInspectResult.MessageHeaders.Should().BeNullOrEmpty(); + } + + // Teardown + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageWithoutIntegrityCheck_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.ContentFileInfo, testFactoryEncrypt.EncryptedContentFileInfo, withIntegrityCheck: false); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeFalse(); + pgpInspectResult.FileName.Should().Be(testFactoryEncrypt.ContentFileInfo.Name); + pgpInspectResult.MessageHeaders.Should().HaveCount(1); + pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version"); + pgpInspectResult.MessageHeaders.Single().Value.Should().Be(VERSION); + } + + // Teardown + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageWithName_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.ContentFileInfo, testFactoryEncrypt.EncryptedContentFileInfo, name: TESTNAME); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageWithHeaders_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.ContentFileInfo, testFactoryEncrypt.EncryptedContentFileInfo, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(testFactoryEncrypt.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageWithOldFormat_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.ContentFileInfo, testFactoryEncrypt.EncryptedContentFileInfo, oldFormat: true); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(testFactoryEncrypt.ContentFileInfo.Name); + pgpInspectResult.MessageHeaders.Should().HaveCount(1); + pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version"); + pgpInspectResult.MessageHeaders.Single().Value.Should().Be(VERSION); + } + + // Teardown + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + } +} diff --git a/PgpCore.Tests/UnitTests/Encrypt/EncryptAsync.Stream.cs b/PgpCore.Tests/UnitTests/Encrypt/EncryptAsync.Stream.cs new file mode 100644 index 0000000..375fbf1 --- /dev/null +++ b/PgpCore.Tests/UnitTests/Encrypt/EncryptAsync.Stream.cs @@ -0,0 +1,468 @@ +using FluentAssertions.Execution; +using FluentAssertions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; +using PgpCore.Models; +using System.IO; +using System.Xml.Linq; + +namespace PgpCore.Tests.UnitTests.Encrypt +{ + public class EncryptAsync_Stream : TestBase + { + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAsync_EncryptMessageWithDefaultProperties_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyStream, testFactory.PrivateKeyStream, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpEncrypt.EncryptAsync(testFactory.ContentStream, outputFileStream); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAsync_EncryptMessageAsBinary_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyStream, testFactory.PrivateKeyStream, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpEncrypt.EncryptAsync(testFactory.ContentStream, outputFileStream, armor: false); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeFalse(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().NotBeNullOrEmpty(); + pgpInspectResult.MessageHeaders.Should().BeNullOrEmpty(); + } + + // Teardown + testFactory.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAsync_EncryptMessageWithoutIntegrityCheck_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyStream, testFactory.PrivateKeyStream, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpEncrypt.EncryptAsync(testFactory.ContentStream, outputFileStream, withIntegrityCheck: false); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeFalse(); + 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 EncryptAsync_EncryptMessageWithName_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyStream, testFactory.PrivateKeyStream, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpEncrypt.EncryptAsync(testFactory.ContentStream, outputFileStream, name: TESTNAME); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAsync_EncryptMessageWithHeaders_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyStream, testFactory.PrivateKeyStream, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpEncrypt.EncryptAsync(testFactory.ContentStream, outputFileStream, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAsync_EncryptMessageWithOldFormat_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyStream, testFactory.PrivateKeyStream, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + await pgpEncrypt.EncryptAsync(testFactory.ContentStream, outputFileStream, oldFormat: true); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAndSignAsync_EncryptAndSignMessageWithDefaultProperties_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + using (Stream outputFileStream = testFactoryEncrypt.EncryptedContentFileInfo.Create()) + await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.ContentStream, outputFileStream); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageAsBinary_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + using (Stream outputFileStream = testFactoryEncrypt.EncryptedContentFileInfo.Create()) + await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.ContentStream, outputFileStream, armor: false); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeFalse(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().NotBeNullOrEmpty(); + pgpInspectResult.MessageHeaders.Should().BeNullOrEmpty(); + } + + // Teardown + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageWithoutIntegrityCheck_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + using (Stream outputFileStream = testFactoryEncrypt.EncryptedContentFileInfo.Create()) + await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.ContentStream, outputFileStream, withIntegrityCheck: false); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeFalse(); + 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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageWithName_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + using (Stream outputFileStream = testFactoryEncrypt.EncryptedContentFileInfo.Create()) + await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.ContentStream, outputFileStream, name: TESTNAME); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageWithHeaders_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + using (Stream outputFileStream = testFactoryEncrypt.EncryptedContentFileInfo.Create()) + await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.ContentStream, outputFileStream, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageWithOldFormat_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + using (Stream outputFileStream = testFactoryEncrypt.EncryptedContentFileInfo.Create()) + await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.ContentStream, outputFileStream, oldFormat: true); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + } +} diff --git a/PgpCore.Tests/UnitTests/Encrypt/EncryptAsync.String.cs b/PgpCore.Tests/UnitTests/Encrypt/EncryptAsync.String.cs new file mode 100644 index 0000000..90ae01f --- /dev/null +++ b/PgpCore.Tests/UnitTests/Encrypt/EncryptAsync.String.cs @@ -0,0 +1,387 @@ +using FluentAssertions.Execution; +using FluentAssertions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; +using PgpCore.Models; +using System.IO; +using System.Xml.Linq; + +namespace PgpCore.Tests.UnitTests.Encrypt +{ + public class EncryptAsync_String : TestBase + { + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAsync_EncryptMessageWithDefaultProperties_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + string encryptedContent = await pgpEncrypt.EncryptAsync(testFactory.Content); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAsync_EncryptMessageWithoutIntegrityCheck_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + string encryptedContent = await pgpEncrypt.EncryptAsync(testFactory.Content, withIntegrityCheck: false); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeFalse(); + 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 EncryptAsync_EncryptMessageWithName_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + string encryptedContent = await pgpEncrypt.EncryptAsync(testFactory.Content, name: TESTNAME); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAsync_EncryptMessageWithHeaders_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + string encryptedContent = await pgpEncrypt.EncryptAsync(testFactory.Content, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAsync_EncryptMessageWithOldFormat_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + await testFactory.ArrangeAsync(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + string encryptedContent = await pgpEncrypt.EncryptAsync(testFactory.Content, oldFormat: true); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpEncrypt.InspectAsync(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAndSignAsync_EncryptAndSignMessageWithDefaultProperties_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + string encryptedContent = await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.Content); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageWithoutIntegrityCheck_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + string encryptedContent = await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.Content, withIntegrityCheck: false); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeFalse(); + 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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageWithName_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + string encryptedContent = await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.Content, name: TESTNAME); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageWithHeaders_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + string encryptedContent = await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.Content, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public async Task EncryptAndSignAsync_EncryptAndSignMessageWithOldFormat_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + await testFactoryEncrypt.ArrangeAsync(keyType, FileType.Known); + await testFactorySign.ArrangeAsync(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + string encryptedContent = await pgpEncrypt.EncryptAndSignAsync(testFactoryEncrypt.Content, oldFormat: true); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = await pgpInspect.InspectAsync(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + } +} diff --git a/PgpCore.Tests/UnitTests/Encrypt/EncryptSync.File.cs b/PgpCore.Tests/UnitTests/Encrypt/EncryptSync.File.cs new file mode 100644 index 0000000..020a951 --- /dev/null +++ b/PgpCore.Tests/UnitTests/Encrypt/EncryptSync.File.cs @@ -0,0 +1,454 @@ +using FluentAssertions.Execution; +using FluentAssertions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; +using PgpCore.Models; + +namespace PgpCore.Tests.UnitTests.Encrypt +{ + public class EncryptSync_File : TestBase + { + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void Encrypt_EncryptMessageWithDefaultProperties_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + pgpEncrypt.Encrypt(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 Encrypt_EncryptMessageAsBinary_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + pgpEncrypt.Encrypt(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, armor: false); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeFalse(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().NotBeNullOrEmpty(); + pgpInspectResult.MessageHeaders.Should().BeNullOrEmpty(); + } + + // Teardown + testFactory.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void Encrypt_EncryptMessageWithoutIntegrityCheck_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + pgpEncrypt.Encrypt(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, withIntegrityCheck: false); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeFalse(); + 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 Encrypt_EncryptMessageWithName_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + pgpEncrypt.Encrypt(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, name: TESTNAME); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 Encrypt_EncryptMessageWithHeaders_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + pgpEncrypt.Encrypt(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 Encrypt_EncryptMessageWithOldFormat_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + pgpEncrypt.Encrypt(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, oldFormat: true); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAndSign_EncryptAndSignMessageWithDefaultProperties_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + pgpEncrypt.EncryptAndSign(testFactoryEncrypt.ContentFileInfo, testFactoryEncrypt.EncryptedContentFileInfo); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(testFactoryEncrypt.ContentFileInfo.Name); + pgpInspectResult.MessageHeaders.Should().HaveCount(1); + pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version"); + pgpInspectResult.MessageHeaders.Single().Value.Should().Be(VERSION); + } + + // Teardown + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageAsBinary_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + pgpEncrypt.EncryptAndSign(testFactoryEncrypt.ContentFileInfo, testFactoryEncrypt.EncryptedContentFileInfo, armor: false); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeFalse(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().NotBeNullOrEmpty(); + pgpInspectResult.MessageHeaders.Should().BeNullOrEmpty(); + } + + // Teardown + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageWithoutIntegrityCheck_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + pgpEncrypt.EncryptAndSign(testFactoryEncrypt.ContentFileInfo, testFactoryEncrypt.EncryptedContentFileInfo, withIntegrityCheck: false); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeFalse(); + pgpInspectResult.FileName.Should().Be(testFactoryEncrypt.ContentFileInfo.Name); + pgpInspectResult.MessageHeaders.Should().HaveCount(1); + pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version"); + pgpInspectResult.MessageHeaders.Single().Value.Should().Be(VERSION); + } + + // Teardown + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageWithName_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + pgpEncrypt.EncryptAndSign(testFactoryEncrypt.ContentFileInfo, testFactoryEncrypt.EncryptedContentFileInfo, name: TESTNAME); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageWithHeaders_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + pgpEncrypt.EncryptAndSign(testFactoryEncrypt.ContentFileInfo, testFactoryEncrypt.EncryptedContentFileInfo, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(testFactoryEncrypt.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageWithOldFormat_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + pgpEncrypt.EncryptAndSign(testFactoryEncrypt.ContentFileInfo, testFactoryEncrypt.EncryptedContentFileInfo, oldFormat: true); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().Be(testFactoryEncrypt.ContentFileInfo.Name); + pgpInspectResult.MessageHeaders.Should().HaveCount(1); + pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version"); + pgpInspectResult.MessageHeaders.Single().Value.Should().Be(VERSION); + } + + // Teardown + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + } +} diff --git a/PgpCore.Tests/UnitTests/Encrypt/EncryptSync.Stream.cs b/PgpCore.Tests/UnitTests/Encrypt/EncryptSync.Stream.cs new file mode 100644 index 0000000..b2179b5 --- /dev/null +++ b/PgpCore.Tests/UnitTests/Encrypt/EncryptSync.Stream.cs @@ -0,0 +1,468 @@ +using FluentAssertions.Execution; +using FluentAssertions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; +using PgpCore.Models; +using System.IO; +using System.Xml.Linq; + +namespace PgpCore.Tests.UnitTests.Encrypt +{ + public class EncryptSync_Stream : TestBase + { + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void Encrypt_EncryptMessageWithDefaultProperties_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyStream, testFactory.PrivateKeyStream, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpEncrypt.Encrypt(testFactory.ContentStream, outputFileStream); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 Encrypt_EncryptMessageAsBinary_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyStream, testFactory.PrivateKeyStream, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpEncrypt.Encrypt(testFactory.ContentStream, outputFileStream, armor: false); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeFalse(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().NotBeNullOrEmpty(); + pgpInspectResult.MessageHeaders.Should().BeNullOrEmpty(); + } + + // Teardown + testFactory.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void Encrypt_EncryptMessageWithoutIntegrityCheck_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyStream, testFactory.PrivateKeyStream, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpEncrypt.Encrypt(testFactory.ContentStream, outputFileStream, withIntegrityCheck: false); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeFalse(); + 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 Encrypt_EncryptMessageWithName_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyStream, testFactory.PrivateKeyStream, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpEncrypt.Encrypt(testFactory.ContentStream, outputFileStream, name: TESTNAME); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 Encrypt_EncryptMessageWithHeaders_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyStream, testFactory.PrivateKeyStream, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpEncrypt.Encrypt(testFactory.ContentStream, outputFileStream, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 Encrypt_EncryptMessageWithOldFormat_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyStream, testFactory.PrivateKeyStream, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + using (Stream outputFileStream = testFactory.EncryptedContentFileInfo.Create()) + pgpEncrypt.Encrypt(testFactory.ContentStream, outputFileStream, oldFormat: true); + + // Assert + using (new AssertionScope()) + { + testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(testFactory.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAndSign_EncryptAndSignMessageWithDefaultProperties_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + using (Stream outputFileStream = testFactoryEncrypt.EncryptedContentFileInfo.Create()) + pgpEncrypt.EncryptAndSign(testFactoryEncrypt.ContentStream, outputFileStream); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageAsBinary_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + using (Stream outputFileStream = testFactoryEncrypt.EncryptedContentFileInfo.Create()) + pgpEncrypt.EncryptAndSign(testFactoryEncrypt.ContentStream, outputFileStream, armor: false); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeFalse(); + pgpInspectResult.IsIntegrityProtected.Should().BeTrue(); + pgpInspectResult.FileName.Should().NotBeNullOrEmpty(); + pgpInspectResult.MessageHeaders.Should().BeNullOrEmpty(); + } + + // Teardown + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageWithoutIntegrityCheck_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + using (Stream outputFileStream = testFactoryEncrypt.EncryptedContentFileInfo.Create()) + pgpEncrypt.EncryptAndSign(testFactoryEncrypt.ContentStream, outputFileStream, withIntegrityCheck: false); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeFalse(); + 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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageWithName_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + using (Stream outputFileStream = testFactoryEncrypt.EncryptedContentFileInfo.Create()) + pgpEncrypt.EncryptAndSign(testFactoryEncrypt.ContentStream, outputFileStream, name: TESTNAME); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageWithHeaders_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + using (Stream outputFileStream = testFactoryEncrypt.EncryptedContentFileInfo.Create()) + pgpEncrypt.EncryptAndSign(testFactoryEncrypt.ContentStream, outputFileStream, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageWithOldFormat_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + using (Stream outputFileStream = testFactoryEncrypt.EncryptedContentFileInfo.Create()) + pgpEncrypt.EncryptAndSign(testFactoryEncrypt.ContentStream, outputFileStream, oldFormat: true); + + // Assert + using (new AssertionScope()) + { + testFactoryEncrypt.EncryptedContentFileInfo.Exists.Should().BeTrue(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(testFactoryEncrypt.EncryptedContentFileInfo); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + } +} diff --git a/PgpCore.Tests/UnitTests/Encrypt/EncryptSync.String.cs b/PgpCore.Tests/UnitTests/Encrypt/EncryptSync.String.cs new file mode 100644 index 0000000..be9403a --- /dev/null +++ b/PgpCore.Tests/UnitTests/Encrypt/EncryptSync.String.cs @@ -0,0 +1,387 @@ +using FluentAssertions.Execution; +using FluentAssertions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; +using PgpCore.Models; +using System.IO; +using System.Xml.Linq; + +namespace PgpCore.Tests.UnitTests.Encrypt +{ + public class EncryptSync_String : TestBase + { + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void Encrypt_EncryptMessageWithDefaultProperties_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + string encryptedContent = pgpEncrypt.Encrypt(testFactory.Content); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 Encrypt_EncryptMessageWithoutIntegrityCheck_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + string encryptedContent = pgpEncrypt.Encrypt(testFactory.Content, withIntegrityCheck: false); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeFalse(); + 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 Encrypt_EncryptMessageWithName_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + string encryptedContent = pgpEncrypt.Encrypt(testFactory.Content, name: TESTNAME); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 Encrypt_EncryptMessageWithHeaders_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + string encryptedContent = pgpEncrypt.Encrypt(testFactory.Content, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 Encrypt_EncryptMessageWithOldFormat_ShouldEncryptMessage(KeyType keyType) + { + // Arrange + TestFactory testFactory = new TestFactory(); + testFactory.Arrange(keyType, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + + // Act + string encryptedContent = pgpEncrypt.Encrypt(testFactory.Content, oldFormat: true); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpEncrypt.Inspect(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeFalse(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 EncryptAndSign_EncryptAndSignMessageWithDefaultProperties_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + string encryptedContent = pgpEncrypt.EncryptAndSign(testFactoryEncrypt.Content); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageWithoutIntegrityCheck_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + string encryptedContent = pgpEncrypt.EncryptAndSign(testFactoryEncrypt.Content, withIntegrityCheck: false); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.Should().BeFalse(); + 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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageWithName_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + string encryptedContent = pgpEncrypt.EncryptAndSign(testFactoryEncrypt.Content, name: TESTNAME); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageWithHeaders_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + string encryptedContent = pgpEncrypt.EncryptAndSign(testFactoryEncrypt.Content, headers: new Dictionary { { TESTHEADERKEY, TESTHEADERVALUE } }); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + + [Theory] + [InlineData(KeyType.Generated)] + [InlineData(KeyType.Known)] + [InlineData(KeyType.KnownGpg)] + public void EncryptAndSign_EncryptAndSignMessageWithOldFormat_ShouldEncryptAndSignMessage(KeyType keyType) + { + // Arrange + TestFactory testFactoryEncrypt = new TestFactory(); + TestFactory testFactorySign = new TestFactory(); + testFactoryEncrypt.Arrange(keyType, FileType.Known); + testFactorySign.Arrange(KeyType.Generated, FileType.Known); + EncryptionKeys encryptionKeys = new EncryptionKeys(testFactoryEncrypt.PublicKeyFileInfo, testFactorySign.PrivateKeyFileInfo, testFactorySign.Password); + EncryptionKeys inspectionKeys = new EncryptionKeys(testFactoryEncrypt.PrivateKeyFileInfo, testFactoryEncrypt.Password); + PGP pgpEncrypt = new PGP(encryptionKeys); + PGP pgpInspect = new PGP(inspectionKeys); + + // Act + string encryptedContent = pgpEncrypt.EncryptAndSign(testFactoryEncrypt.Content, oldFormat: true); + + // Assert + using (new AssertionScope()) + { + encryptedContent.Should().NotBeNullOrEmpty(); + PgpInspectResult pgpInspectResult = pgpInspect.Inspect(encryptedContent); + pgpInspectResult.IsEncrypted.Should().BeTrue(); + pgpInspectResult.IsSigned.Should().BeTrue(); + pgpInspectResult.IsArmored.Should().BeTrue(); + pgpInspectResult.IsIntegrityProtected.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 + testFactoryEncrypt.Teardown(); + testFactorySign.Teardown(); + } + } +} diff --git a/PgpCore.Tests/UnitTests/GenerateKey/KeyAsync.cs b/PgpCore.Tests/UnitTests/GenerateKey/KeyAsync.cs index 5296c7b..858c5a0 100644 --- a/PgpCore.Tests/UnitTests/GenerateKey/KeyAsync.cs +++ b/PgpCore.Tests/UnitTests/GenerateKey/KeyAsync.cs @@ -4,22 +4,14 @@ using Xunit; using Org.BouncyCastle.Bcpg.OpenPgp; using System.IO; -using Org.BouncyCastle.Utilities.Zlib; using System; using System.Collections.Generic; using Org.BouncyCastle.Bcpg; -using System.Security.Cryptography.X509Certificates; namespace PgpCore.Tests.UnitTests.GenerateKey { - public class KeyAsync + public class KeyAsync : TestBase { -#if NETFRAMEWORK - public const string VERSION = "Version: BouncyCastle.NET Cryptography (net461) v2.1.1+851feee009"; -#else - public const string VERSION = "Version: BouncyCastle.NET Cryptography (net6.0) v2.1.1+851feee009"; -#endif - [Fact] public async Task GenerateKeyAsync_CreatePublicAndPrivateKeys_ShouldCreateKeysWithDefaultProperties() { diff --git a/PgpCore.Tests/UnitTests/GenerateKey/KeySync.cs b/PgpCore.Tests/UnitTests/GenerateKey/KeySync.cs index da0a804..573c48c 100644 --- a/PgpCore.Tests/UnitTests/GenerateKey/KeySync.cs +++ b/PgpCore.Tests/UnitTests/GenerateKey/KeySync.cs @@ -1,25 +1,16 @@ using FluentAssertions.Execution; using FluentAssertions; -using System.Threading.Tasks; using Xunit; using Org.BouncyCastle.Bcpg.OpenPgp; using System.IO; -using Org.BouncyCastle.Utilities.Zlib; using System; using System.Collections.Generic; using Org.BouncyCastle.Bcpg; -using System.Security.Cryptography.X509Certificates; namespace PgpCore.Tests.UnitTests.GenerateKey { - public class KeySync + public class KeySync : TestBase { -#if NETFRAMEWORK - public const string VERSION = "Version: BouncyCastle.NET Cryptography (net461) v2.1.1+851feee009"; -#else - public const string VERSION = "Version: BouncyCastle.NET Cryptography (net6.0) v2.1.1+851feee009"; -#endif - [Fact] public void GenerateKey_CreatePublicAndPrivateKeys_ShouldCreateKeysWithDefaultProperties() { diff --git a/PgpCore.Tests/UnitTests/TestBase.cs b/PgpCore.Tests/UnitTests/TestBase.cs index eb9a93c..fb358dd 100644 --- a/PgpCore.Tests/UnitTests/TestBase.cs +++ b/PgpCore.Tests/UnitTests/TestBase.cs @@ -9,6 +9,16 @@ namespace PgpCore.Tests.UnitTests { public class TestBase { +#if NETFRAMEWORK + public const string VERSION = "BouncyCastle.NET Cryptography (net461) v2.1.1+851feee009"; +#else + public const string VERSION = "BouncyCastle.NET Cryptography (net6.0) v2.1.1+851feee009"; +#endif + public const string DEFAULTNAME = "name"; + public const string TESTNAME = "Test Name"; + public const string TESTHEADERKEY = "Test Header"; + public const string TESTHEADERVALUE = "Test Value"; + public static IEnumerable GetCompressionAlgorithimTags() { foreach (CompressionAlgorithmTag compressionAlgorithmTag in TestHelper.GetEnumValues()) diff --git a/PgpCore.Tests/UnitTests/UnitTests.KeyAsync.cs b/PgpCore.Tests/UnitTests/UnitTests.KeyAsync.cs deleted file mode 100644 index af3c16f..0000000 --- a/PgpCore.Tests/UnitTests/UnitTests.KeyAsync.cs +++ /dev/null @@ -1,386 +0,0 @@ -using FluentAssertions.Execution; -using FluentAssertions; -using System.Threading.Tasks; -using Xunit; -using Org.BouncyCastle.Bcpg.OpenPgp; -using System.IO; -using Org.BouncyCastle.Utilities.Zlib; -using System; -using System.Collections.Generic; -using Org.BouncyCastle.Bcpg; -using System.Security.Cryptography.X509Certificates; - -namespace PgpCore.Tests.UnitTests -{ - public class KeyAsync - { -#if NETFRAMEWORK - public const string VERSION = "Version: BouncyCastle.NET Cryptography (net461) v2.1.1+851feee009"; -#else - public const string VERSION = "Version: BouncyCastle.NET Cryptography (net6.0) v2.1.1+851feee009"; -#endif - - //[Theory] - //[MemberData(nameof(GetAllCombinations))] - //public async Task GenerateKeyAsync_CreatePublicAndPrivateKeys_ShouldCreateKeysWithSpecifiedProperties( - // CompressionAlgorithmTag compressionAlgorithmTag, - // HashAlgorithmTag hashAlgorithmTag, - // SymmetricKeyAlgorithmTag symmetricKeyAlgorithmTag - // ) - //{ - // // Arrange - // TestFactory testFactory = new TestFactory(); - // testFactory.Arrange(); - // PGP pgp = new PGP(); - // PgpPublicKey publicKey = null; - - // // Act - // await pgp.GenerateKeyAsync( - // testFactory.PublicKeyFileInfo, - // testFactory.PrivateKeyFileInfo, - // testFactory.UserName, - // testFactory.Password, - // preferredCompressionAlgorithms: new CompressionAlgorithmTag[] { compressionAlgorithmTag }, - // preferredHashAlgorithmTags: new HashAlgorithmTag[] { hashAlgorithmTag }, - // preferredSymetricKeyAlgorithms: new SymmetricKeyAlgorithmTag[] { symmetricKeyAlgorithmTag } - // ); - - // using (Stream publicKeyStream = testFactory.PublicKeyFileInfo.OpenRead()) - // { - // publicKey = ReadPublicKey(publicKeyStream); - // // If we successfully read the public key without exceptions, it is considered valid - // } - - // // Assert - // using (new AssertionScope()) - // { - // testFactory.PublicKeyFileInfo.Exists.Should().BeTrue(); - // testFactory.PrivateKeyFileInfo.Exists.Should().BeTrue(); - // } - - // using (new AssertionScope()) - // { - // publicKey.Should().NotBeNull(); - // publicKey.Version.Should().Be(4); - // publicKey.CreationTime.Should().BeCloseTo(DateTime.UtcNow, new TimeSpan(0, 0, 1)); - // publicKey.IsEncryptionKey.Should().BeTrue(); - // publicKey.IsMasterKey.Should().BeTrue(); - // publicKey.IsRevoked().Should().BeFalse(); - // //publicKey.KeyId.Should().NotBe(0); - // //publicKey.BitStrength.Should().Be(2048); - // //publicKey.PublicKeyPacket.Should().NotBeNull(); - // } - //} - - [Fact] - public async Task GenerateKeyAsync_CreatePublicAndPrivateKeys_ShouldCreateKeysWithDefaultProperties() - { - // Arrange - TestFactory testFactory = new TestFactory(); - testFactory.Arrange(); - PGP pgp = new PGP(); - - // Act - await pgp.GenerateKeyAsync( - testFactory.PublicKeyFileInfo, - testFactory.PrivateKeyFileInfo, - testFactory.UserName, - testFactory.Password - ); - - // Assert - // Assert that the keys were created - using (new AssertionScope()) - { - testFactory.PublicKeyFileInfo.Exists.Should().BeTrue(); - testFactory.PrivateKeyFileInfo.Exists.Should().BeTrue(); - } - - // Assert public key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PublicKeyFileInfo.FullName).Should().Contain(VERSION); - - using (Stream publicKeyStream = testFactory.PublicKeyFileInfo.OpenRead()) - { - PgpPublicKey publicKey = publicKey = ReadPublicKey(publicKeyStream); - // If we successfully read the public key without exceptions, it is considered valid - publicKey.Should().NotBeNull(); - publicKey.Version.Should().Be(4); - publicKey.CreationTime.Should().BeCloseTo(DateTime.UtcNow, new TimeSpan(0, 0, 10)); - publicKey.IsEncryptionKey.Should().BeTrue(); - publicKey.IsMasterKey.Should().BeTrue(); - publicKey.IsRevoked().Should().BeFalse(); - publicKey.BitStrength.Should().Be(1024); - } - - } - - // Assert private key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PrivateKeyFileInfo.FullName).Should().Contain(VERSION); - - using (Stream privateKeyStream = testFactory.PrivateKeyFileInfo.OpenRead()) - { - PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)); - foreach (PgpSecretKeyRing kRing in pgpSec.GetKeyRings()) - { - foreach (PgpSecretKey k in kRing.GetSecretKeys()) - { - if (k.IsSigningKey) - { - k.Should().NotBeNull(); - k.IsSigningKey.Should().BeTrue(); - k.IsMasterKey.Should().BeTrue(); - k.KeyEncryptionAlgorithm.Should().Be(SymmetricKeyAlgorithmTag.TripleDes); - } - } - } - } - } - } - - [Fact] - public async Task GenerateKeyAsync_CreatePublicAndPrivateKeysWithKeyStrength_ShouldCreateKeysWithSpecifiedProperties() - { - // Arrange - TestFactory testFactory = new TestFactory(); - testFactory.Arrange(); - PGP pgp = new PGP(); - - // Act - await pgp.GenerateKeyAsync( - testFactory.PublicKeyFileInfo, - testFactory.PrivateKeyFileInfo, - testFactory.UserName, - testFactory.Password, - strength: 2048 - ); - - // Assert - // Assert that the keys were created - using (new AssertionScope()) - { - testFactory.PublicKeyFileInfo.Exists.Should().BeTrue(); - testFactory.PrivateKeyFileInfo.Exists.Should().BeTrue(); - } - - // Assert public key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PublicKeyFileInfo.FullName).Should().Contain(VERSION); - - using (Stream publicKeyStream = testFactory.PublicKeyFileInfo.OpenRead()) - { - PgpPublicKey publicKey = publicKey = ReadPublicKey(publicKeyStream); - // If we successfully read the public key without exceptions, it is considered valid - publicKey.Should().NotBeNull(); - publicKey.Version.Should().Be(4); - publicKey.CreationTime.Should().BeCloseTo(DateTime.UtcNow, new TimeSpan(0, 0, 10)); - publicKey.IsEncryptionKey.Should().BeTrue(); - publicKey.IsMasterKey.Should().BeTrue(); - publicKey.IsRevoked().Should().BeFalse(); - publicKey.BitStrength.Should().Be(2048); - } - } - - // Assert private key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PrivateKeyFileInfo.FullName).Should().Contain(VERSION); - - using (Stream privateKeyStream = testFactory.PrivateKeyFileInfo.OpenRead()) - { - PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)); - foreach (PgpSecretKeyRing kRing in pgpSec.GetKeyRings()) - { - foreach (PgpSecretKey k in kRing.GetSecretKeys()) - { - if (k.IsSigningKey) - { - k.Should().NotBeNull(); - k.IsSigningKey.Should().BeTrue(); - k.IsMasterKey.Should().BeTrue(); - k.KeyEncryptionAlgorithm.Should().Be(SymmetricKeyAlgorithmTag.TripleDes); - } - } - } - } - } - } - - [Fact] - public async Task GenerateKeyAsync_CreatePublicAndPrivateKeysWithoutVersion_ShouldCreateKeysWithSpecifiedProperties() - { - // Arrange - TestFactory testFactory = new TestFactory(); - testFactory.Arrange(); - PGP pgp = new PGP(); - - // Act - await pgp.GenerateKeyAsync( - testFactory.PublicKeyFileInfo, - testFactory.PrivateKeyFileInfo, - testFactory.UserName, - testFactory.Password, - emitVersion: false - ); - - // Assert - // Assert that the keys were created - using (new AssertionScope()) - { - testFactory.PublicKeyFileInfo.Exists.Should().BeTrue(); - testFactory.PrivateKeyFileInfo.Exists.Should().BeTrue(); - } - - // Assert public key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PublicKeyFileInfo.FullName).Should().NotContain(VERSION); - - using (Stream publicKeyStream = testFactory.PublicKeyFileInfo.OpenRead()) - { - PgpPublicKey publicKey = ReadPublicKey(publicKeyStream); - // If we successfully read the public key without exceptions, it is considered valid - publicKey.Should().NotBeNull(); - publicKey.Version.Should().Be(4); - publicKey.CreationTime.Should().BeCloseTo(DateTime.UtcNow, new TimeSpan(0, 0, 10)); - publicKey.IsEncryptionKey.Should().BeTrue(); - publicKey.IsMasterKey.Should().BeTrue(); - publicKey.IsRevoked().Should().BeFalse(); - publicKey.BitStrength.Should().Be(1024); - } - - } - - // Assert private key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PrivateKeyFileInfo.FullName).Should().NotContain(VERSION); - - using (Stream privateKeyStream = testFactory.PrivateKeyFileInfo.OpenRead()) - { - PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)); - foreach (PgpSecretKeyRing kRing in pgpSec.GetKeyRings()) - { - foreach (PgpSecretKey k in kRing.GetSecretKeys()) - { - if (k.IsSigningKey) - { - k.Should().NotBeNull(); - k.IsSigningKey.Should().BeTrue(); - k.IsMasterKey.Should().BeTrue(); - k.KeyEncryptionAlgorithm.Should().Be(SymmetricKeyAlgorithmTag.TripleDes); - } - } - } - } - } - } - - [Fact] - public async Task GenerateKeyAsync_CreatePublicAndPrivateKeysWithExpiryDate_ShouldCreateKeysWithSpecifiedProperties() - { - // Arrange - TestFactory testFactory = new TestFactory(); - testFactory.Arrange(); - PGP pgp = new PGP(); - - // Act - await pgp.GenerateKeyAsync( - testFactory.PublicKeyFileInfo, - testFactory.PrivateKeyFileInfo, - testFactory.UserName, - testFactory.Password, - keyExpirationInSeconds: 60 - ); - - // Assert - // Assert that the keys were created - using (new AssertionScope()) - { - testFactory.PublicKeyFileInfo.Exists.Should().BeTrue(); - testFactory.PrivateKeyFileInfo.Exists.Should().BeTrue(); - } - - // Assert public key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PublicKeyFileInfo.FullName).Should().Contain(VERSION); - - using (Stream publicKeyStream = testFactory.PublicKeyFileInfo.OpenRead()) - { - PgpPublicKey publicKey = ReadPublicKey(publicKeyStream); - // If we successfully read the public key without exceptions, it is considered valid - publicKey.Should().NotBeNull(); - publicKey.Version.Should().Be(4); - publicKey.CreationTime.Should().BeCloseTo(DateTime.UtcNow, new TimeSpan(0, 0, 10)); - publicKey.IsEncryptionKey.Should().BeTrue(); - publicKey.IsMasterKey.Should().BeTrue(); - publicKey.IsRevoked().Should().BeFalse(); - publicKey.BitStrength.Should().Be(1024); - publicKey.GetValidSeconds().Should().Be(60); - } - - } - - // Assert private key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PrivateKeyFileInfo.FullName).Should().Contain(VERSION); - - using (Stream privateKeyStream = testFactory.PrivateKeyFileInfo.OpenRead()) - { - PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)); - foreach (PgpSecretKeyRing kRing in pgpSec.GetKeyRings()) - { - foreach (PgpSecretKey k in kRing.GetSecretKeys()) - { - if (k.IsSigningKey) - { - k.Should().NotBeNull(); - k.IsSigningKey.Should().BeTrue(); - k.IsMasterKey.Should().BeTrue(); - k.KeyEncryptionAlgorithm.Should().Be(SymmetricKeyAlgorithmTag.TripleDes); - } - } - } - } - } - } - - 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 GetEnumValues() where T : struct, IConvertible - { - foreach (T enumValue in Enum.GetValues(typeof(T))) - { - yield return enumValue; - } - } - - public static IEnumerable GetAllCombinations() - { - foreach (CompressionAlgorithmTag compressionAlgorithmTag in GetEnumValues()) - foreach (HashAlgorithmTag hashAlgorithmTag in GetEnumValues()) - foreach (SymmetricKeyAlgorithmTag symmetricKeyAlgorithmTag in GetEnumValues()) - { - yield return new object[] { compressionAlgorithmTag, hashAlgorithmTag, symmetricKeyAlgorithmTag }; - } - } - } -} diff --git a/PgpCore.Tests/UnitTests/UnitTests.KeySync.cs b/PgpCore.Tests/UnitTests/UnitTests.KeySync.cs deleted file mode 100644 index b781370..0000000 --- a/PgpCore.Tests/UnitTests/UnitTests.KeySync.cs +++ /dev/null @@ -1,334 +0,0 @@ -using FluentAssertions.Execution; -using FluentAssertions; -using System.Threading.Tasks; -using Xunit; -using Org.BouncyCastle.Bcpg.OpenPgp; -using System.IO; -using Org.BouncyCastle.Utilities.Zlib; -using System; -using System.Collections.Generic; -using Org.BouncyCastle.Bcpg; -using System.Security.Cryptography.X509Certificates; - -namespace PgpCore.Tests.UnitTests -{ - public class KeySync - { -#if NETFRAMEWORK - public const string VERSION = "Version: BouncyCastle.NET Cryptography (net461) v2.1.1+851feee009"; -#else - public const string VERSION = "Version: BouncyCastle.NET Cryptography (net6.0) v2.1.1+851feee009"; -#endif - - [Fact] - public void GenerateKey_CreatePublicAndPrivateKeys_ShouldCreateKeysWithDefaultProperties() - { - // Arrange - TestFactory testFactory = new TestFactory(); - testFactory.Arrange(); - PGP pgp = new PGP(); - - // Act - pgp.GenerateKey( - testFactory.PublicKeyFileInfo, - testFactory.PrivateKeyFileInfo, - testFactory.UserName, - testFactory.Password - ); - - // Assert - // Assert that the keys were created - using (new AssertionScope()) - { - testFactory.PublicKeyFileInfo.Exists.Should().BeTrue(); - testFactory.PrivateKeyFileInfo.Exists.Should().BeTrue(); - } - - // Assert public key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PublicKeyFileInfo.FullName).Should().Contain(VERSION); - - using (Stream publicKeyStream = testFactory.PublicKeyFileInfo.OpenRead()) - { - PgpPublicKey publicKey = publicKey = ReadPublicKey(publicKeyStream); - // If we successfully read the public key without exceptions, it is considered valid - publicKey.Should().NotBeNull(); - publicKey.Version.Should().Be(4); - publicKey.CreationTime.Should().BeCloseTo(DateTime.UtcNow, new TimeSpan(0, 0, 10)); - publicKey.IsEncryptionKey.Should().BeTrue(); - publicKey.IsMasterKey.Should().BeTrue(); - publicKey.IsRevoked().Should().BeFalse(); - publicKey.BitStrength.Should().Be(1024); - } - - } - - // Assert private key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PrivateKeyFileInfo.FullName).Should().Contain(VERSION); - - using (Stream privateKeyStream = testFactory.PrivateKeyFileInfo.OpenRead()) - { - PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)); - foreach (PgpSecretKeyRing kRing in pgpSec.GetKeyRings()) - { - foreach (PgpSecretKey k in kRing.GetSecretKeys()) - { - if (k.IsSigningKey) - { - k.Should().NotBeNull(); - k.IsSigningKey.Should().BeTrue(); - k.IsMasterKey.Should().BeTrue(); - k.KeyEncryptionAlgorithm.Should().Be(SymmetricKeyAlgorithmTag.TripleDes); - } - } - } - } - } - } - - [Fact] - public void GenerateKey_CreatePublicAndPrivateKeysWithKeyStrength_ShouldCreateKeysWithSpecifiedProperties() - { - // Arrange - TestFactory testFactory = new TestFactory(); - testFactory.Arrange(); - PGP pgp = new PGP(); - - // Act - pgp.GenerateKey( - testFactory.PublicKeyFileInfo, - testFactory.PrivateKeyFileInfo, - testFactory.UserName, - testFactory.Password, - strength: 2048 - ); - - // Assert - // Assert that the keys were created - using (new AssertionScope()) - { - testFactory.PublicKeyFileInfo.Exists.Should().BeTrue(); - testFactory.PrivateKeyFileInfo.Exists.Should().BeTrue(); - } - - // Assert public key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PublicKeyFileInfo.FullName).Should().Contain(VERSION); - - using (Stream publicKeyStream = testFactory.PublicKeyFileInfo.OpenRead()) - { - PgpPublicKey publicKey = publicKey = ReadPublicKey(publicKeyStream); - // If we successfully read the public key without exceptions, it is considered valid - publicKey.Should().NotBeNull(); - publicKey.Version.Should().Be(4); - publicKey.CreationTime.Should().BeCloseTo(DateTime.UtcNow, new TimeSpan(0, 0, 10)); - publicKey.IsEncryptionKey.Should().BeTrue(); - publicKey.IsMasterKey.Should().BeTrue(); - publicKey.IsRevoked().Should().BeFalse(); - publicKey.BitStrength.Should().Be(2048); - } - } - - // Assert private key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PrivateKeyFileInfo.FullName).Should().Contain(VERSION); - - using (Stream privateKeyStream = testFactory.PrivateKeyFileInfo.OpenRead()) - { - PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)); - foreach (PgpSecretKeyRing kRing in pgpSec.GetKeyRings()) - { - foreach (PgpSecretKey k in kRing.GetSecretKeys()) - { - if (k.IsSigningKey) - { - k.Should().NotBeNull(); - k.IsSigningKey.Should().BeTrue(); - k.IsMasterKey.Should().BeTrue(); - k.KeyEncryptionAlgorithm.Should().Be(SymmetricKeyAlgorithmTag.TripleDes); - } - } - } - } - } - } - - [Fact] - public void GenerateKey_CreatePublicAndPrivateKeysWithoutVersion_ShouldCreateKeysWithSpecifiedProperties() - { - // Arrange - TestFactory testFactory = new TestFactory(); - testFactory.Arrange(); - PGP pgp = new PGP(); - - // Act - pgp.GenerateKey( - testFactory.PublicKeyFileInfo, - testFactory.PrivateKeyFileInfo, - testFactory.UserName, - testFactory.Password, - emitVersion: false - ); - - // Assert - // Assert that the keys were created - using (new AssertionScope()) - { - testFactory.PublicKeyFileInfo.Exists.Should().BeTrue(); - testFactory.PrivateKeyFileInfo.Exists.Should().BeTrue(); - } - - // Assert public key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PublicKeyFileInfo.FullName).Should().NotContain(VERSION); - - using (Stream publicKeyStream = testFactory.PublicKeyFileInfo.OpenRead()) - { - PgpPublicKey publicKey = ReadPublicKey(publicKeyStream); - // If we successfully read the public key without exceptions, it is considered valid - publicKey.Should().NotBeNull(); - publicKey.Version.Should().Be(4); - publicKey.CreationTime.Should().BeCloseTo(DateTime.UtcNow, new TimeSpan(0, 0, 10)); - publicKey.IsEncryptionKey.Should().BeTrue(); - publicKey.IsMasterKey.Should().BeTrue(); - publicKey.IsRevoked().Should().BeFalse(); - publicKey.BitStrength.Should().Be(1024); - } - - } - - // Assert private key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PrivateKeyFileInfo.FullName).Should().NotContain(VERSION); - - using (Stream privateKeyStream = testFactory.PrivateKeyFileInfo.OpenRead()) - { - PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)); - foreach (PgpSecretKeyRing kRing in pgpSec.GetKeyRings()) - { - foreach (PgpSecretKey k in kRing.GetSecretKeys()) - { - if (k.IsSigningKey) - { - k.Should().NotBeNull(); - k.IsSigningKey.Should().BeTrue(); - k.IsMasterKey.Should().BeTrue(); - k.KeyEncryptionAlgorithm.Should().Be(SymmetricKeyAlgorithmTag.TripleDes); - } - } - } - } - } - } - - [Fact] - public void GenerateKey_CreatePublicAndPrivateKeysWithExpiryDate_ShouldCreateKeysWithSpecifiedProperties() - { - // Arrange - TestFactory testFactory = new TestFactory(); - testFactory.Arrange(); - PGP pgp = new PGP(); - - // Act - pgp.GenerateKey( - testFactory.PublicKeyFileInfo, - testFactory.PrivateKeyFileInfo, - testFactory.UserName, - testFactory.Password, - keyExpirationInSeconds: 60 - ); - - // Assert - // Assert that the keys were created - using (new AssertionScope()) - { - testFactory.PublicKeyFileInfo.Exists.Should().BeTrue(); - testFactory.PrivateKeyFileInfo.Exists.Should().BeTrue(); - } - - // Assert public key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PublicKeyFileInfo.FullName).Should().Contain(VERSION); - - using (Stream publicKeyStream = testFactory.PublicKeyFileInfo.OpenRead()) - { - PgpPublicKey publicKey = ReadPublicKey(publicKeyStream); - // If we successfully read the public key without exceptions, it is considered valid - publicKey.Should().NotBeNull(); - publicKey.Version.Should().Be(4); - publicKey.CreationTime.Should().BeCloseTo(DateTime.UtcNow, new TimeSpan(0, 0, 10)); - publicKey.IsEncryptionKey.Should().BeTrue(); - publicKey.IsMasterKey.Should().BeTrue(); - publicKey.IsRevoked().Should().BeFalse(); - publicKey.BitStrength.Should().Be(1024); - publicKey.GetValidSeconds().Should().Be(60); - } - - } - - // Assert private key properties - using (new AssertionScope()) - { - File.ReadAllText(testFactory.PrivateKeyFileInfo.FullName).Should().Contain(VERSION); - - using (Stream privateKeyStream = testFactory.PrivateKeyFileInfo.OpenRead()) - { - PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)); - foreach (PgpSecretKeyRing kRing in pgpSec.GetKeyRings()) - { - foreach (PgpSecretKey k in kRing.GetSecretKeys()) - { - if (k.IsSigningKey) - { - k.Should().NotBeNull(); - k.IsSigningKey.Should().BeTrue(); - k.IsMasterKey.Should().BeTrue(); - k.KeyEncryptionAlgorithm.Should().Be(SymmetricKeyAlgorithmTag.TripleDes); - } - } - } - } - } - } - - 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 GetEnumValues() where T : struct, IConvertible - { - foreach (T enumValue in Enum.GetValues(typeof(T))) - { - yield return enumValue; - } - } - - public static IEnumerable GetAllCombinations() - { - foreach (CompressionAlgorithmTag compressionAlgorithmTag in GetEnumValues()) - foreach (HashAlgorithmTag hashAlgorithmTag in GetEnumValues()) - foreach (SymmetricKeyAlgorithmTag symmetricKeyAlgorithmTag in GetEnumValues()) - { - yield return new object[] { compressionAlgorithmTag, hashAlgorithmTag, symmetricKeyAlgorithmTag }; - } - } - } -} diff --git a/PgpCore/Models/EncryptionKeys.cs b/PgpCore/Models/EncryptionKeys.cs index ae463f0..4cb8bf9 100644 --- a/PgpCore/Models/EncryptionKeys.cs +++ b/PgpCore/Models/EncryptionKeys.cs @@ -360,6 +360,9 @@ public EncryptionKeys(IEnumerable publicKeyStreams) public PgpPrivateKey FindSecretKey(long keyId) { + if (SecretKeys == null) + throw new ArgumentNullException("No private keys found. These should be provided in EncryptionKeys constructor."); + PgpSecretKey pgpSecKey = SecretKeys.GetSecretKey(keyId); if (pgpSecKey == null) diff --git a/PgpCore/PGP.InspectAsync.cs b/PgpCore/PGP.InspectAsync.cs index 69802dd..090a074 100644 --- a/PgpCore/PGP.InspectAsync.cs +++ b/PgpCore/PGP.InspectAsync.cs @@ -28,7 +28,11 @@ public async Task InspectAsync(Stream inputStream) throw new ArgumentException("inputStream should be at start of stream"); bool isArmored = await IsArmoredAsync(inputStream); - Dictionary messageHeaders = await GetMessageHeadersAsync(inputStream); + Dictionary messageHeaders = null; + + if (isArmored) + messageHeaders = await GetMessageHeadersAsync(inputStream); + PgpInspectBaseResult pgpInspectBaseResult = GetPgpInspectBaseResult(inputStream); return new PgpInspectResult( diff --git a/PgpCore/PGP.InspectSync.cs b/PgpCore/PGP.InspectSync.cs index dd8fa9c..1e286db 100644 --- a/PgpCore/PGP.InspectSync.cs +++ b/PgpCore/PGP.InspectSync.cs @@ -33,7 +33,11 @@ public PgpInspectResult Inspect(Stream inputStream) throw new ArgumentException("inputStream should be at start of stream"); bool isArmored = IsArmored(inputStream); - Dictionary messageHeaders = GetMessageHeaders(inputStream); + Dictionary messageHeaders = null; + + if (isArmored) + messageHeaders = GetMessageHeaders(inputStream); + PgpInspectBaseResult pgpInspectBaseResult = GetPgpInspectBaseResult(inputStream); return new PgpInspectResult(