Added a simple unit test to ensure E(D(M)) = M

This commit is contained in:
Leonetienne
2022-02-06 17:58:17 +01:00
parent 19f3d61335
commit 4064483cad
7 changed files with 268 additions and 2 deletions

View File

@@ -0,0 +1,44 @@
#include "CppUnitTest.h"
#include "../GhettoCrypt/Cipher.h"
#include "../GhettoCrypt/Util.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace GhettoCipher;
namespace SimpleTests
{
TEST_CLASS(EncryptEqualsDecrypt)
{
public:
// Tests that a decrypted ciphertext equals its plaintrext version
TEST_METHOD(tEncryptEqualsDecrypt)
{
// Yes, this unit test should ideally exclude string conversions,
// But like this it's easier to see what it's doing
// Define basic input
const std::string cleartext = "Hello, World!";
const std::string password = "1234";
// Instanciate our cipher and supply a key
const Cipher cipher(password);
// Recode the ascii-string to bits
const Flexblock cleartext_bits = StringToBits(cleartext);
// Encrypt our cleartext bits
const Flexblock ciphertext_bits = cipher.Encipher(cleartext_bits);
// Decipher it again
const Flexblock decryptedBits = cipher.Decipher(ciphertext_bits);
// Decode it back to ascii
const std::string decryptedText = BitsToString(decryptedBits);
// Assert that the decrypted text equals the plaintext
Assert::AreEqual(cleartext.length(), decryptedText.length());
}
};
}