Skip to content

Commit

Permalink
Merge branch 'master' into feature/238-specify-public-key-in-ring
Browse files Browse the repository at this point in the history
  • Loading branch information
mattosaurus authored Oct 17, 2023
2 parents fc73229 + d6b0537 commit 8e52de2
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 37 deletions.
90 changes: 90 additions & 0 deletions PgpCore.Tests/UnitTests/UnitTestsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,36 @@ public async Task VerifyFileInfoAsync_DoNotVerifySignedFile(KeyType keyType)
// Teardown
testFactory.Teardown();
}

[Fact]
public async Task VerifyFileInfoAsync_ThrowIfEncrypted()
{
// Arrange
TestFactory testFactory = new TestFactory();
await testFactory.ArrangeAsync(KeyType.Generated, FileType.GeneratedMedium);

EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password);
PGP pgp = new PGP(encryptionKeys);
using (Stream inputFileStream = testFactory.ContentStream)
using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
await pgp.EncryptStreamAsync(inputFileStream, outputFileStream);

// Act and Assert
try
{
await pgp.VerifyFileAsync(testFactory.EncryptedContentFileInfo, true);
Assert.Fail("Expected exception not thrown");
}
catch (ArgumentException e)
{
Assert.Equal("Input is encrypted. Decrypt the input first.", e.Message);
}
finally
{
// Teardown
testFactory.Teardown();
}
}
#endregion File - FileInfo

#region Stream
Expand Down Expand Up @@ -2122,6 +2152,36 @@ public async Task VerifyAsync_DoNotVerifySignedStream(KeyType keyType)
// Teardown
testFactory.Teardown();
}

[Fact]
public async Task VerifyStreamAsync_ThrowIfEncrypted()
{
// Arrange
TestFactory testFactory = new TestFactory();
await testFactory.ArrangeAsync(KeyType.Generated, FileType.GeneratedMedium);

EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password);
PGP pgp = new PGP(encryptionKeys);
using (Stream inputFileStream = testFactory.ContentStream)
using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
await pgp.EncryptStreamAsync(inputFileStream, outputFileStream);

// Act and Assert
try
{
await pgp.VerifyStreamAsync(testFactory.EncryptedContentStream, true);
Assert.Fail("Expected exception not thrown");
}
catch (ArgumentException e)
{
Assert.Equal("Input is encrypted. Decrypt the input first.", e.Message);
}
finally
{
// Teardown
testFactory.Teardown();
}
}
#endregion Stream

#region Armor
Expand Down Expand Up @@ -2863,6 +2923,36 @@ public async Task VerifyAndReadAsync_DoNotVerifySignedString(KeyType keyType)
// Teardown
testFactory.Teardown();
}

[Fact]
public async Task VerifyArmoredStringAsync_ThrowIfEncrypted()
{
// Arrange
TestFactory testFactory = new TestFactory();
await testFactory.ArrangeAsync(KeyType.Generated, FileType.GeneratedMedium);

EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password);
PGP pgp = new PGP(encryptionKeys);
using (Stream inputFileStream = testFactory.ContentStream)
using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
await pgp.EncryptStreamAsync(inputFileStream, outputFileStream);

// Act and Assert
try
{
await pgp.VerifyAndReadSignedArmoredStringAsync(testFactory.EncryptedContent, true);
Assert.Fail("Expected exception not thrown");
}
catch (ArgumentException e)
{
Assert.Equal("Input is encrypted. Decrypt the input first.", e.Message);
}
finally
{
// Teardown
testFactory.Teardown();
}
}
#endregion Armor

public static IEnumerable<object[]> KeyTypeValues()
Expand Down
91 changes: 91 additions & 0 deletions PgpCore.Tests/UnitTests/UnitTestsSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,36 @@ public void Recipients_GetFileRecipients(KeyType keyType)
// Teardown
testFactory.Teardown();
}

[Fact]
public void VerifyFile_ThrowIfEncrypted()
{
// Arrange
TestFactory testFactory = new TestFactory();
testFactory.Arrange(KeyType.Generated, FileType.GeneratedMedium);

EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password);
PGP pgp = new PGP(encryptionKeys);
using (Stream inputFileStream = testFactory.ContentStream)
using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
pgp.EncryptStream(inputFileStream, outputFileStream);

// Act and Assert
try
{
pgp.VerifyFile(testFactory.EncryptedContentFileInfo, true);
Assert.Fail("Expected exception not thrown");
}
catch (ArgumentException e)
{
Assert.Equal("Input is encrypted. Decrypt the input first.", e.Message);
}
finally
{
// Teardown
testFactory.Teardown();
}
}
#endregion File - Path

#region File - FileInfo
Expand Down Expand Up @@ -2095,6 +2125,37 @@ public void Recipients_GetStreamRecipients(KeyType keyType)
// Teardown
testFactory.Teardown();
}

[Fact]
public void VerifyStream_ThrowIfEncrypted()
{
// Arrange
TestFactory testFactory = new TestFactory();
testFactory.Arrange(KeyType.Generated, FileType.GeneratedMedium);

EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password);
PGP pgp = new PGP(encryptionKeys);
using (Stream inputFileStream = testFactory.ContentStream)
using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
pgp.EncryptStream(inputFileStream, outputFileStream);

// Act and Assert
try
{
pgp.VerifyStream(testFactory.EncryptedContentStream, true);
Assert.Fail("Expected exception not thrown");
}
catch (ArgumentException e)
{
Assert.Equal("Input is encrypted. Decrypt the input first.", e.Message);
}
finally
{
// Teardown
testFactory.Teardown();
}
}

#endregion Stream

#region Armor
Expand Down Expand Up @@ -2891,6 +2952,36 @@ public void Recipients_GetStringRecipients(KeyType keyType)
// Teardown
testFactory.Teardown();
}

[Fact]
public void Verify_ThrowIfEncrypted()
{
// Arrange
TestFactory testFactory = new TestFactory();
testFactory.Arrange(KeyType.Generated, FileType.GeneratedMedium);

EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKey, testFactory.PrivateKey, testFactory.Password);
PGP pgp = new PGP(encryptionKeys);
using (Stream inputFileStream = testFactory.ContentStream)
using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
pgp.EncryptStream(inputFileStream, outputFileStream);

// Act and Assert
try
{
pgp.VerifyAndReadSignedArmoredString(testFactory.EncryptedContent, true);
Assert.Fail("Expected exception not thrown");
}
catch (ArgumentException e)
{
Assert.Equal("Input is encrypted. Decrypt the input first.", e.Message);
}
finally
{
// Teardown
testFactory.Teardown();
}
}
#endregion Armor

public static IEnumerable<object[]> KeyTypeValues()
Expand Down
Loading

0 comments on commit 8e52de2

Please sign in to comment.