Skip to content

Commit

Permalink
Merge pull request #142 from mattosaurus/verification-checks
Browse files Browse the repository at this point in the history
Verification checks
  • Loading branch information
mattosaurus authored Nov 19, 2021
2 parents 314d47b + 4b20e88 commit e371b70
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 28 deletions.
29 changes: 27 additions & 2 deletions PgpCore.Tests/UnitTests/LegacyUnitTestsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public async Task ClearSignFileAsync_CreateClearSignedFile(KeyType keyType)
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task ClearSignAndVerifyFileAsync_CreateClearSignedFileAndVerify(KeyType keyType)
public async Task ClearSignFileAsync_CreateClearSignedFileAndVerify(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
Expand All @@ -148,7 +148,7 @@ public async Task ClearSignAndVerifyFileAsync_CreateClearSignedFileAndVerify(Key
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task ClearSignAndDoNotVerifyFileAsync_CreateClearSignedFileAndDoNotVerify(KeyType keyType)
public async Task ClearSignFileAsync_CreateClearSignedFileAndDoNotVerify(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
Expand All @@ -168,6 +168,31 @@ public async Task ClearSignAndDoNotVerifyFileAsync_CreateClearSignedFileAndDoNot
testFactory2.Teardown();
}

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

// Act
await pgp.ClearSignFileAsync(testFactory.ContentFilePath, testFactory.SignedContentFilePath);
string fileContent = await File.ReadAllTextAsync(testFactory.SignedContentFilePath);
fileContent = fileContent.Replace("fox", "rabbit");
System.IO.File.WriteAllText(testFactory.SignedContentFilePath, fileContent);

// Assert
Assert.False(await pgp.VerifyClearFileAsync(testFactory.SignedContentFilePath, testFactory.PublicKeyFilePath));

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down
29 changes: 27 additions & 2 deletions PgpCore.Tests/UnitTests/LegacyUnitTestsSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void ClearSignFile_CreateClearSignedFile(KeyType keyType)
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public void ClearSignAndVerifyFile_CreateClearSignedFileAndVerify(KeyType keyType)
public void ClearSignFile_CreateClearSignedFileAndVerify(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
Expand All @@ -179,7 +179,7 @@ public void ClearSignAndVerifyFile_CreateClearSignedFileAndVerify(KeyType keyTyp
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public void ClearSignAndDoNotVerifyFile_CreateClearSignedFileAndDoNotVerify(KeyType keyType)
public void ClearSignFile_CreateClearSignedFileAndDoNotVerify(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
Expand All @@ -199,6 +199,31 @@ public void ClearSignAndDoNotVerifyFile_CreateClearSignedFileAndDoNotVerify(KeyT
testFactory2.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public void ClearSignFile_CreateClearSignedFileWithBadContentAndDoNotVerify(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
testFactory.Arrange(keyType, FileType.Known);
EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password);
PGP pgp = new PGP(encryptionKeys);

// Act
pgp.ClearSignFile(testFactory.ContentFilePath, testFactory.SignedContentFilePath);
string fileContent = File.ReadAllText(testFactory.SignedContentFilePath);
fileContent = fileContent.Replace("fox", "rabbit");
File.WriteAllText(testFactory.SignedContentFilePath, fileContent);

// Assert
Assert.False(pgp.VerifyClearFile(testFactory.SignedContentFilePath, testFactory.PublicKeyFilePath));

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down
29 changes: 27 additions & 2 deletions PgpCore.Tests/UnitTests/UnitTestsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public async Task ClearSignFileAsync_CreateClearSignedFile(KeyType keyType)
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task ClearSignAndVerifyFileAsync_CreateClearSignedFileAndVerify(KeyType keyType)
public async Task ClearSignFileAsync_CreateClearSignedFileAndVerify(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
Expand All @@ -153,7 +153,7 @@ public async Task ClearSignAndVerifyFileAsync_CreateClearSignedFileAndVerify(Key
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public async Task ClearSignAndDoNotVerifyFileAsync_CreateClearSignedFileAndDoNotVerify(KeyType keyType)
public async Task ClearSignFileAsync_CreateClearSignedFileAndDoNotVerify(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
Expand All @@ -176,6 +176,31 @@ public async Task ClearSignAndDoNotVerifyFileAsync_CreateClearSignedFileAndDoNot
testFactory2.Teardown();
}

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

// Act
await pgp.ClearSignFileAsync(testFactory.ContentFilePath, testFactory.SignedContentFilePath);
string fileContent = await File.ReadAllTextAsync(testFactory.SignedContentFilePath);
fileContent = fileContent.Replace("fox", "rabbit");
System.IO.File.WriteAllText(testFactory.SignedContentFilePath, fileContent);

// Assert
Assert.False(await pgp.VerifyClearFileAsync(testFactory.SignedContentFilePath, testFactory.PublicKeyFilePath));

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down
55 changes: 53 additions & 2 deletions PgpCore.Tests/UnitTests/UnitTestsSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void ClearSignFile_CreateClearSignedFile(KeyType keyType)
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public void ClearSignAndVerifyFile_CreateClearSignedFileAndVerify(KeyType keyType)
public void ClearSignFile_CreateClearSignedFileAndVerify(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
Expand All @@ -143,7 +143,7 @@ public void ClearSignAndVerifyFile_CreateClearSignedFileAndVerify(KeyType keyTyp
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public void ClearSignAndDoNotVerifyFile_CreateClearSignedFileAndDoNotVerify(KeyType keyType)
public void ClearSignFile_CreateClearSignedFileAndDoNotVerify(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
Expand All @@ -166,6 +166,31 @@ public void ClearSignAndDoNotVerifyFile_CreateClearSignedFileAndDoNotVerify(KeyT
testFactory2.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
[InlineData(KeyType.KnownGpg)]
public void ClearSignFile_CreateClearSignedFileWithBadContentAndDoNotVerify(KeyType keyType)
{
// Arrange
TestFactory testFactory = new TestFactory();
testFactory.Arrange(keyType, FileType.Known);
EncryptionKeys encryptionKeys = new EncryptionKeys(testFactory.PrivateKeyFileInfo, testFactory.Password);
PGP pgp = new PGP(encryptionKeys);

// Act
pgp.ClearSignFile(testFactory.ContentFilePath, testFactory.SignedContentFilePath);
string fileContent = File.ReadAllText(testFactory.SignedContentFilePath);
fileContent = fileContent.Replace("fox", "rabbit");
File.WriteAllText(testFactory.SignedContentFilePath, fileContent);

// Assert
Assert.False(pgp.VerifyClearFile(testFactory.SignedContentFilePath, testFactory.PublicKeyFilePath));

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down Expand Up @@ -670,6 +695,32 @@ public void Verify_DoNotVerifyEncryptedAndSignedFile(KeyType keyType)
testFactory.Teardown();
}

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

// Act
pgp.SignFile(testFactory.ContentFilePath, testFactory.EncryptedContentFilePath);
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);

// Assert
Assert.False(verified);

// Teardown
testFactory.Teardown();
}

[Theory]
[InlineData(KeyType.Generated)]
[InlineData(KeyType.Known)]
Expand Down
8 changes: 6 additions & 2 deletions PgpCore.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.352
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PgpCore", "PgpCore\PgpCore.csproj", "{109C82AE-0229-494D-845F-828B61C712E6}"
EndProject
Expand All @@ -21,6 +21,10 @@ Global
{9F70E456-1511-4466-B805-6D0AABC48AD9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F70E456-1511-4466-B805-6D0AABC48AD9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F70E456-1511-4466-B805-6D0AABC48AD9}.Release|Any CPU.Build.0 = Release|Any CPU
{DB356D7C-0C5C-4E8C-8DE3-A4372AA61850}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DB356D7C-0C5C-4E8C-8DE3-A4372AA61850}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB356D7C-0C5C-4E8C-8DE3-A4372AA61850}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DB356D7C-0C5C-4E8C-8DE3-A4372AA61850}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading

0 comments on commit e371b70

Please sign in to comment.