-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from mattosaurus/chore/add-sign-tests
Add sign tests
- Loading branch information
Showing
6 changed files
with
1,624 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,279 @@ | ||
using FluentAssertions.Execution; | ||
using FluentAssertions; | ||
using PgpCore.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using System.IO; | ||
|
||
namespace PgpCore.Tests.UnitTests.Sign | ||
{ | ||
public class SignAsync_File : TestBase | ||
{ | ||
[Theory] | ||
[InlineData(KeyType.Generated)] | ||
[InlineData(KeyType.Known)] | ||
[InlineData(KeyType.KnownGpg)] | ||
public async Task SignAsync_SignMessageWithDefaultProperties_ShouldSignMessage(KeyType keyType) | ||
{ | ||
// Arrange | ||
TestFactory testFactory = new TestFactory(); | ||
await testFactory.ArrangeAsync(keyType, FileType.Known); | ||
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password); | ||
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo); | ||
PGP pgpSign = new PGP(signingKeys); | ||
PGP pgpVerify = new PGP(verificationKeys); | ||
|
||
// Act | ||
await pgpSign.SignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo); | ||
bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentFileInfo); | ||
|
||
// Assert | ||
using (new AssertionScope()) | ||
{ | ||
verified.Should().BeTrue(); | ||
} | ||
|
||
using (new AssertionScope()) | ||
{ | ||
testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); | ||
PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(testFactory.EncryptedContentFileInfo); | ||
pgpInspectResult.IsEncrypted.Should().BeFalse(); | ||
pgpInspectResult.IsSigned.Should().BeTrue(); | ||
pgpInspectResult.IsArmored.Should().BeTrue(); | ||
pgpInspectResult.FileName.Should().Be(testFactory.ContentFileInfo.Name); | ||
pgpInspectResult.MessageHeaders.Should().HaveCount(1); | ||
pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version"); | ||
pgpInspectResult.MessageHeaders.Single().Value.Should().Be(VERSION); | ||
} | ||
|
||
// Teardown | ||
testFactory.Teardown(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(KeyType.Generated)] | ||
[InlineData(KeyType.Known)] | ||
[InlineData(KeyType.KnownGpg)] | ||
public async Task SignAsync_SignMessageAsBinary_ShouldSignMessage(KeyType keyType) | ||
{ | ||
// Arrange | ||
TestFactory testFactory = new TestFactory(); | ||
await testFactory.ArrangeAsync(keyType, FileType.Known); | ||
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password); | ||
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo); | ||
PGP pgpSign = new PGP(signingKeys); | ||
PGP pgpVerify = new PGP(verificationKeys); | ||
|
||
// Act | ||
await pgpSign.SignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, armor: false); | ||
bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentFileInfo); | ||
|
||
// Assert | ||
using (new AssertionScope()) | ||
{ | ||
verified.Should().BeTrue(); | ||
} | ||
|
||
using (new AssertionScope()) | ||
{ | ||
testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); | ||
PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(testFactory.EncryptedContentFileInfo); | ||
pgpInspectResult.IsEncrypted.Should().BeFalse(); | ||
pgpInspectResult.IsSigned.Should().BeTrue(); | ||
pgpInspectResult.IsArmored.Should().BeFalse(); | ||
pgpInspectResult.FileName.Should().NotBeNullOrEmpty(); | ||
pgpInspectResult.MessageHeaders.Should().BeNullOrEmpty(); | ||
} | ||
|
||
// Teardown | ||
testFactory.Teardown(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(KeyType.Generated)] | ||
[InlineData(KeyType.Known)] | ||
[InlineData(KeyType.KnownGpg)] | ||
public async Task SignAsync_SignMessageWithName_ShouldSignMessage(KeyType keyType) | ||
{ | ||
// Arrange | ||
TestFactory testFactory = new TestFactory(); | ||
await testFactory.ArrangeAsync(keyType, FileType.Known); | ||
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password); | ||
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo); | ||
PGP pgpSign = new PGP(signingKeys); | ||
PGP pgpVerify = new PGP(verificationKeys); | ||
|
||
// Act | ||
await pgpSign.SignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, name: TESTNAME); | ||
bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentFileInfo); | ||
|
||
// Assert | ||
using (new AssertionScope()) | ||
{ | ||
verified.Should().BeTrue(); | ||
} | ||
|
||
using (new AssertionScope()) | ||
{ | ||
testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); | ||
PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(testFactory.EncryptedContentFileInfo); | ||
pgpInspectResult.IsEncrypted.Should().BeFalse(); | ||
pgpInspectResult.IsSigned.Should().BeTrue(); | ||
pgpInspectResult.IsArmored.Should().BeTrue(); | ||
pgpInspectResult.FileName.Should().Be(TESTNAME); | ||
pgpInspectResult.MessageHeaders.Should().HaveCount(1); | ||
pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version"); | ||
pgpInspectResult.MessageHeaders.Single().Value.Should().Be(VERSION); | ||
} | ||
|
||
// Teardown | ||
testFactory.Teardown(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(KeyType.Generated)] | ||
[InlineData(KeyType.Known)] | ||
[InlineData(KeyType.KnownGpg)] | ||
public async Task SignAsync_SignMessageWithHeaders_ShouldSignMessage(KeyType keyType) | ||
{ | ||
// Arrange | ||
TestFactory testFactory = new TestFactory(); | ||
await testFactory.ArrangeAsync(keyType, FileType.Known); | ||
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password); | ||
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo); | ||
PGP pgpSign = new PGP(signingKeys); | ||
PGP pgpVerify = new PGP(verificationKeys); | ||
|
||
// Act | ||
await pgpSign.SignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, headers: new Dictionary<string, string> { { TESTHEADERKEY, TESTHEADERVALUE } }); | ||
bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentFileInfo); | ||
|
||
// Assert | ||
using (new AssertionScope()) | ||
{ | ||
verified.Should().BeTrue(); | ||
} | ||
|
||
using (new AssertionScope()) | ||
{ | ||
testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); | ||
PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(testFactory.EncryptedContentFileInfo); | ||
pgpInspectResult.IsEncrypted.Should().BeFalse(); | ||
pgpInspectResult.IsSigned.Should().BeTrue(); | ||
pgpInspectResult.IsArmored.Should().BeTrue(); | ||
pgpInspectResult.FileName.Should().Be(testFactory.ContentFileInfo.Name); | ||
pgpInspectResult.MessageHeaders.Should().HaveCount(2); | ||
pgpInspectResult.MessageHeaders.First().Key.Should().Be("Version"); | ||
pgpInspectResult.MessageHeaders.First().Value.Should().Be(VERSION); | ||
pgpInspectResult.MessageHeaders.Last().Key.Should().Be(TESTHEADERKEY); | ||
pgpInspectResult.MessageHeaders.Last().Value.Should().Be(TESTHEADERVALUE); | ||
} | ||
|
||
// Teardown | ||
testFactory.Teardown(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(KeyType.Generated)] | ||
[InlineData(KeyType.Known)] | ||
[InlineData(KeyType.KnownGpg)] | ||
public async Task SignAsync_SignMessageWithOldFormat_ShouldSignMessage(KeyType keyType) | ||
{ | ||
// Arrange | ||
TestFactory testFactory = new TestFactory(); | ||
await testFactory.ArrangeAsync(keyType, FileType.Known); | ||
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password); | ||
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo); | ||
PGP pgpSign = new PGP(signingKeys); | ||
PGP pgpVerify = new PGP(verificationKeys); | ||
|
||
// Act | ||
await pgpSign.SignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, oldFormat: true); | ||
bool verified = await pgpVerify.VerifyAsync(testFactory.EncryptedContentFileInfo); | ||
|
||
// Assert | ||
using (new AssertionScope()) | ||
{ | ||
verified.Should().BeTrue(); | ||
} | ||
|
||
using (new AssertionScope()) | ||
{ | ||
testFactory.EncryptedContentFileInfo.Exists.Should().BeTrue(); | ||
PgpInspectResult pgpInspectResult = await pgpSign.InspectAsync(testFactory.EncryptedContentFileInfo); | ||
pgpInspectResult.IsEncrypted.Should().BeFalse(); | ||
pgpInspectResult.IsSigned.Should().BeTrue(); | ||
pgpInspectResult.IsArmored.Should().BeTrue(); | ||
pgpInspectResult.FileName.Should().Be(testFactory.ContentFileInfo.Name); | ||
pgpInspectResult.MessageHeaders.Should().HaveCount(1); | ||
pgpInspectResult.MessageHeaders.Single().Key.Should().Be("Version"); | ||
pgpInspectResult.MessageHeaders.Single().Value.Should().Be(VERSION); | ||
} | ||
|
||
// Teardown | ||
testFactory.Teardown(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(KeyType.Generated)] | ||
[InlineData(KeyType.Known)] | ||
[InlineData(KeyType.KnownGpg)] | ||
public async Task ClearSignAsync_SignMessageWithDefaultProperties_ShouldSignMessage(KeyType keyType) | ||
{ | ||
// Arrange | ||
TestFactory testFactory = new TestFactory(); | ||
await testFactory.ArrangeAsync(keyType, FileType.Known); | ||
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password); | ||
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo); | ||
PGP pgpSign = new PGP(signingKeys); | ||
PGP pgpVerify = new PGP(verificationKeys); | ||
|
||
// Act | ||
await pgpSign.ClearSignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo); | ||
bool verified = await pgpVerify.VerifyClearAsync(testFactory.EncryptedContentFileInfo); | ||
|
||
// Assert | ||
using (new AssertionScope()) | ||
{ | ||
verified.Should().BeTrue(); | ||
File.ReadAllText(testFactory.EncryptedContentFileInfo.FullName).Should().Contain(testFactory.Content); | ||
} | ||
|
||
// Teardown | ||
testFactory.Teardown(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(KeyType.Generated)] | ||
[InlineData(KeyType.Known)] | ||
[InlineData(KeyType.KnownGpg)] | ||
public async Task ClearSignAsync_SignMessageWithHeaders_ShouldSignMessage(KeyType keyType) | ||
{ | ||
// Arrange | ||
TestFactory testFactory = new TestFactory(); | ||
await testFactory.ArrangeAsync(keyType, FileType.Known); | ||
EncryptionKeys signingKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password); | ||
EncryptionKeys verificationKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo); | ||
PGP pgpSign = new PGP(signingKeys); | ||
PGP pgpVerify = new PGP(verificationKeys); | ||
|
||
// Act | ||
await pgpSign.ClearSignAsync(testFactory.ContentFileInfo, testFactory.EncryptedContentFileInfo, headers: new Dictionary<string, string> { { TESTHEADERKEY, TESTHEADERVALUE } }); | ||
bool verified = await pgpVerify.VerifyClearAsync(testFactory.EncryptedContentFileInfo); | ||
|
||
// Assert | ||
using (new AssertionScope()) | ||
{ | ||
verified.Should().BeTrue(); | ||
File.ReadAllText(testFactory.EncryptedContentFileInfo.FullName).Should().Contain(testFactory.Content); | ||
} | ||
|
||
// Teardown | ||
testFactory.Teardown(); | ||
} | ||
} | ||
} |
Oops, something went wrong.