Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add encrypt header test #278

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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