Skip to content

Commit

Permalink
Update tests to cope with new armor check
Browse files Browse the repository at this point in the history
  • Loading branch information
mattosaurus committed Jun 7, 2022
1 parent d514097 commit 743a5e8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
4 changes: 2 additions & 2 deletions PgpCore.Tests/TestFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private void CreateRandomFile(string filePath, int sizeInMb)

byte[] data = new byte[blockSize];

using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
using (RandomNumberGenerator crypto = RandomNumberGenerator.Create())
{
using (FileStream stream = File.OpenWrite(filePath))
{
Expand All @@ -290,7 +290,7 @@ private async Task CreateRandomFileAsync(string filePath, int sizeInMb)

byte[] data = new byte[blockSize];

using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
using (RandomNumberGenerator crypto = RandomNumberGenerator.Create())
{
using (FileStream stream = File.OpenWrite(filePath))
{
Expand Down
30 changes: 30 additions & 0 deletions PgpCore.Tests/UnitTests/UnitTestsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ private static class File
public static bool Exists(string path) => System.IO.File.Exists(path);
#if NETFRAMEWORK
public static Task<string> ReadAllTextAsync(string path) => Task.FromResult(System.IO.File.ReadAllText(path));
public static Task WriteAllLinesAsync(string path, string[] lines) => Task.Run(() => System.IO.File.WriteAllLines(path, lines));
#else
public static Task<string> ReadAllTextAsync(string path) => System.IO.File.ReadAllTextAsync(path);
public static Task WriteAllLinesAsync(string path, string[] lines) => Task.FromResult(System.IO.File.WriteAllLinesAsync(path, lines));
#endif
public static Task<string[]> ReadAllLinesAsync(string path) => Task.FromResult(System.IO.File.ReadAllLines(path));
}

[Fact]
Expand Down Expand Up @@ -767,6 +770,33 @@ public async Task VerifyAsync_DoNotVerifyEncryptedAndSignedFile(KeyType keyType)
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task VerifyAsync_DoNotVerifySignedFileWithBadContent(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
await testFactory.ArrangeAsync(keyType, FileType.Known);
EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PublicKeyFileInfo, testFactory.PrivateKeyFileInfo, testFactory.Password);
PGP pgp = new PGP(encryptionKeys);

// Act
await pgp.SignFileAsync(testFactory.ContentFilePath, testFactory.EncryptedContentFilePath);
string[] fileLines = await File.ReadAllLinesAsync(testFactory.EncryptedContentFilePath);
fileLines[3] = fileLines[3].Substring(0, fileLines[3].Length - 1 - 1) + "x";
await File.WriteAllLinesAsync(testFactory.EncryptedContentFilePath, fileLines);
Func<Task<bool>> action = async () => await pgp.VerifyFileAsync(testFactory.EncryptedContentFilePath);

// Assert
var ex = await Assert.ThrowsAsync<IOException>(action);
Assert.Equal("invalid armor", ex.Message);

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down
5 changes: 3 additions & 2 deletions PgpCore.Tests/UnitTests/UnitTestsSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,11 @@ public void Verify_DoNotVerifySignedFileWithBadContent(KeyType keyType)
string[] fileLines = File.ReadAllLines(testFactory.EncryptedContentFilePath);
fileLines[3] = fileLines[3].Substring(0, fileLines[3].Length - 1 - 1) + "x";
File.WriteAllLines(testFactory.EncryptedContentFilePath, fileLines);
bool verified = pgp.VerifyFile(testFactory.EncryptedContentFilePath);
Action action = () => pgp.VerifyFile(testFactory.EncryptedContentFilePath);

// Assert
Assert.False(verified);
var ex = Assert.Throws<IOException>(action);
Assert.Equal("invalid armor", ex.Message);

// Teardown
testFactory.Teardown();
Expand Down

0 comments on commit 743a5e8

Please sign in to comment.