2022-05-22 14:26:23 +02:00
|
|
|
#include <GCrypt/GWrapper.h>
|
2022-05-16 22:35:28 +02:00
|
|
|
#include <GCrypt/Util.h>
|
|
|
|
#include "Catch2.h"
|
|
|
|
|
|
|
|
using namespace Leonetienne::GCrypt;
|
|
|
|
|
2022-05-26 15:47:24 +02:00
|
|
|
// Tests that encrypting and decrypting short strings using the wrapper works.
|
2022-05-16 22:35:28 +02:00
|
|
|
// This test will start from scratch after encryption, to ensure EVERYTHING has to be re-calculated.
|
2022-05-26 15:47:24 +02:00
|
|
|
TEST_CASE(__FILE__"/Encrypting and decrypting strings works, Single block", "[Wrapper]") {
|
2022-05-16 22:35:28 +02:00
|
|
|
|
2022-05-22 14:26:23 +02:00
|
|
|
// Setup
|
|
|
|
const std::string plaintext = "Hello, World!";
|
|
|
|
const Key key = Key::FromPassword("Der Affe will Zucker");
|
2022-05-16 22:35:28 +02:00
|
|
|
|
2022-05-22 14:26:23 +02:00
|
|
|
std::string ciphertext;
|
|
|
|
std::string decrypted;
|
2022-05-16 22:35:28 +02:00
|
|
|
|
2022-05-22 14:26:23 +02:00
|
|
|
// Encryption
|
|
|
|
ciphertext = GWrapper::EncryptString(plaintext, key);
|
2022-05-16 22:35:28 +02:00
|
|
|
|
2022-05-22 14:26:23 +02:00
|
|
|
// Decryption
|
|
|
|
decrypted = GWrapper::DecryptString(ciphertext, key);
|
2022-05-16 22:35:28 +02:00
|
|
|
|
2022-05-22 14:26:23 +02:00
|
|
|
// Assertion
|
|
|
|
REQUIRE(plaintext == decrypted);
|
2022-05-16 22:35:28 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 15:47:24 +02:00
|
|
|
// Tests that encrypting and decrypting very long strings using the wrapper works.
|
|
|
|
// This test will start from scratch after encryption, to ensure EVERYTHING has to be re-calculated.
|
|
|
|
TEST_CASE(__FILE__"/Encrypting and decrypting strings works, Many blocks block", "[Wrapper]") {
|
|
|
|
|
|
|
|
// Setup
|
|
|
|
|
|
|
|
// Read an not-multiple-of-blocksize amount of random chars, that's very large (about 200kb long string)
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
const std::string charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
for (std::size_t i = 0; i < 198273; i++) {
|
|
|
|
ss << charset[rand() % charset.length()];
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string plaintext = ss.str();
|
|
|
|
const Key key = Key::FromPassword("Der Affe will Zucker");
|
|
|
|
|
|
|
|
std::string ciphertext;
|
|
|
|
std::string decrypted;
|
|
|
|
|
|
|
|
// Encryption
|
|
|
|
ciphertext = GWrapper::EncryptString(plaintext, key);
|
|
|
|
|
|
|
|
// Decryption
|
|
|
|
decrypted = GWrapper::DecryptString(ciphertext, key);
|
|
|
|
|
|
|
|
// Assertion
|
|
|
|
REQUIRE(plaintext == decrypted);
|
|
|
|
}
|
|
|
|
|
2022-05-16 22:47:41 +02:00
|
|
|
// Tests that encrypting and decrypting files using the wrapper works.
|
|
|
|
// This test will start from scratch after encryption, to ensure EVERYTHING has to be re-calculated.
|
|
|
|
TEST_CASE(__FILE__"/Encrypting and decrypting files works", "[Wrapper]") {
|
2022-05-16 22:35:28 +02:00
|
|
|
|
2022-05-22 14:26:23 +02:00
|
|
|
// Setup
|
|
|
|
const std::string testfile_dir = "testAssets/";
|
2022-05-16 22:47:41 +02:00
|
|
|
|
2022-05-22 14:26:23 +02:00
|
|
|
const std::string filename_plain = testfile_dir + "testfile.png";
|
|
|
|
const std::string filename_encrypted = testfile_dir + "testfile.png.crypt";
|
|
|
|
const std::string filename_decrypted = testfile_dir + "testfile.png.clear.png";
|
|
|
|
const Key key = Key::FromPassword("Der Affe will Zucker");
|
2022-05-16 22:35:28 +02:00
|
|
|
|
|
|
|
|
2022-05-22 14:26:23 +02:00
|
|
|
// Encryption
|
|
|
|
GWrapper::EncryptFile(filename_plain, filename_encrypted, key);
|
2022-05-16 22:35:28 +02:00
|
|
|
|
2022-05-22 14:26:23 +02:00
|
|
|
// Decryption
|
|
|
|
GWrapper::DecryptFile(filename_encrypted, filename_decrypted, key);
|
2022-05-16 22:35:28 +02:00
|
|
|
|
2022-05-22 14:26:23 +02:00
|
|
|
// Read in both the base, and the decrypted file
|
2022-05-26 04:22:42 +02:00
|
|
|
const std::vector<Block> plainfile = ReadFileToBlocks(filename_plain);
|
|
|
|
const std::vector<Block> decryptfile = ReadFileToBlocks(filename_decrypted);
|
2022-05-16 22:35:28 +02:00
|
|
|
|
2022-05-22 14:26:23 +02:00
|
|
|
// Assertion (If this fails, maybe check if the image is even readable by an image viewer)
|
2022-05-26 04:22:42 +02:00
|
|
|
REQUIRE(plainfile.size() == decryptfile.size());
|
|
|
|
REQUIRE(plainfile == decryptfile);
|
2022-05-16 22:35:28 +02:00
|
|
|
}
|
2022-05-16 22:47:41 +02:00
|
|
|
|