Skip to content

Commit

Permalink
Merge pull request #278 from mattosaurus/chore/add-encrypt-header-test
Browse files Browse the repository at this point in the history
Add encrypt header test
  • Loading branch information
mattosaurus authored Feb 12, 2024
2 parents 4c32816 + 692f0e0 commit 9f1074e
Show file tree
Hide file tree
Showing 15 changed files with 870 additions and 5 deletions.
34 changes: 34 additions & 0 deletions PgpCore.Tests/UnitTests/Encrypt/EncryptAsync.File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,40 @@ public async Task EncryptAsync_EncryptMessageWithHeaders_ShouldEncryptMessage(Ke
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task EncryptAsync_EncryptMessageAndOverwriteVersionHeader_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<string, string> { { "Version", 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(1);
pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version");
pgpInspectResult.MessageHeaders.Single().Value.Should().Be(TESTHEADERVALUE);
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down
35 changes: 35 additions & 0 deletions PgpCore.Tests/UnitTests/Encrypt/EncryptAsync.Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,41 @@ public async Task EncryptAsync_EncryptMessageWithHeaders_ShouldEncryptMessage(Ke
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task EncryptAsync_EncryptMessageAndOverwriteVersionHeader_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<string, string> { { "Version", 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(1);
pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version");
pgpInspectResult.MessageHeaders.Single().Value.Should().Be(TESTHEADERVALUE);
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down
34 changes: 34 additions & 0 deletions PgpCore.Tests/UnitTests/Encrypt/EncryptAsync.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,40 @@ public async Task EncryptAsync_EncryptMessageWithHeaders_ShouldEncryptMessage(Ke
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task EncryptAsync_EncryptMessageAndOverwriteVersionHeader_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<string, string> { { "Version", 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(1);
pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version");
pgpInspectResult.MessageHeaders.Single().Value.Should().Be(TESTHEADERVALUE);
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down
34 changes: 34 additions & 0 deletions PgpCore.Tests/UnitTests/Encrypt/EncryptSync.File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,40 @@ public void Encrypt_EncryptMessageWithHeaders_ShouldEncryptMessage(KeyType keyTy
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public void Encrypt_EncryptMessageAndOverwriteVersionHeader_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<string, string> { { "Version", 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(1);
pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version");
pgpInspectResult.MessageHeaders.Single().Value.Should().Be(TESTHEADERVALUE);
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down
35 changes: 35 additions & 0 deletions PgpCore.Tests/UnitTests/Encrypt/EncryptSync.Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,41 @@ public void Encrypt_EncryptMessageWithHeaders_ShouldEncryptMessage(KeyType keyTy
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public void Encrypt_EncryptMessageAndOverwriteVersionHeader_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<string, string> { { "Version", 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(1);
pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version");
pgpInspectResult.MessageHeaders.Single().Value.Should().Be(TESTHEADERVALUE);
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down
34 changes: 34 additions & 0 deletions PgpCore.Tests/UnitTests/Encrypt/EncryptSync.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,40 @@ public void Encrypt_EncryptMessageWithHeaders_ShouldEncryptMessage(KeyType keyTy
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public void Encrypt_EncryptMessageAndOverwriteVersionHeader_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<string, string> { { "Version", 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(1);
pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version");
pgpInspectResult.MessageHeaders.Single().Value.Should().Be(TESTHEADERVALUE);
}

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down
2 changes: 1 addition & 1 deletion PgpCore.Tests/UnitTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace PgpCore.Tests.UnitTests
{
public class TestBase
public abstract class TestBase
{
#if NETFRAMEWORK
public const string VERSION = "BouncyCastle.NET Cryptography (net461) v2.1.1+851feee009";
Expand Down
Loading

0 comments on commit 9f1074e

Please sign in to comment.