2022-05-27 16:44:42 +02:00
|
|
|
#include <DataFormatter.h>
|
2022-05-27 15:34:16 +02:00
|
|
|
#include <GCrypt/Key.h>
|
2022-05-27 15:56:28 +02:00
|
|
|
#include <GCrypt/GPrng.h>
|
2022-05-27 15:26:08 +02:00
|
|
|
#include "Catch2.h"
|
|
|
|
|
|
|
|
using namespace Leonetienne::GCrypt;
|
|
|
|
|
2022-05-27 16:08:24 +02:00
|
|
|
// Tests that recoding iobase formats works for individual blocks, with random data
|
2022-05-27 15:56:28 +02:00
|
|
|
TEMPLATE_TEST_CASE_SIG(
|
2022-05-27 16:08:24 +02:00
|
|
|
__FILE__"Data formats can be converted, without changing in value, with indivudual blocks, with random data",
|
2022-05-27 16:44:42 +02:00
|
|
|
"[DataFormatter]",
|
2022-05-27 15:56:28 +02:00
|
|
|
((Configuration::IOBASE_FORMAT formatUnderTest), formatUnderTest),
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_BYTES,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_2,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_8,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_10,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_16,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_64,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_UWU,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_UGH
|
|
|
|
) {
|
|
|
|
|
|
|
|
// Let's use a GPrng instead of Key::Random,
|
|
|
|
// because Key::Random is rather slow (because it's using hardware events).
|
|
|
|
// We just want randomized data for tests...
|
|
|
|
|
|
|
|
GPrng prng(Key::Random());
|
|
|
|
|
2022-05-27 16:08:24 +02:00
|
|
|
// Let's try 10 different random blocks per test
|
|
|
|
for (std::size_t i = 0; i < 10; i++) {
|
2022-05-27 15:56:28 +02:00
|
|
|
|
|
|
|
// Setup
|
2022-05-27 16:08:24 +02:00
|
|
|
const Block b_initial = prng.GetBlock();
|
2022-05-27 15:56:28 +02:00
|
|
|
|
|
|
|
// Exercise
|
|
|
|
// Convert to a custom base
|
2022-05-27 16:44:42 +02:00
|
|
|
const std::string b_format = DataFormatter::FormatBlock(
|
2022-05-27 15:56:28 +02:00
|
|
|
b_initial,
|
|
|
|
formatUnderTest
|
|
|
|
);
|
|
|
|
|
|
|
|
// Convert back to a block
|
2022-05-27 16:44:42 +02:00
|
|
|
const Block b_retrieved = DataFormatter::DecodeFormat(
|
2022-05-27 15:56:28 +02:00
|
|
|
b_format,
|
|
|
|
formatUnderTest
|
|
|
|
);
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
// Do b_initial and b_retrieved match?
|
|
|
|
REQUIRE(b_initial == b_retrieved);
|
|
|
|
}
|
2022-05-27 15:26:08 +02:00
|
|
|
}
|
|
|
|
|
2022-05-27 16:08:24 +02:00
|
|
|
// Tests that recoding iobase format works
|
|
|
|
TEMPLATE_TEST_CASE_SIG(
|
|
|
|
__FILE__"Data formats can be converted, without changing in value, with indivudual blocks, with very little data (lots of nullbytes)",
|
2022-05-27 16:44:42 +02:00
|
|
|
"[DataFormatter]",
|
2022-05-27 16:08:24 +02:00
|
|
|
((Configuration::IOBASE_FORMAT formatUnderTest), formatUnderTest),
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_BYTES,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_2,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_8,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_10,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_16,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_64,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_UWU,
|
|
|
|
Configuration::IOBASE_FORMAT::BASE_UGH
|
|
|
|
) {
|
|
|
|
|
|
|
|
// Setup
|
|
|
|
Block b_initial;
|
|
|
|
b_initial.FromTextString("Heyu");
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
// Convert to a custom base
|
2022-05-27 16:44:42 +02:00
|
|
|
const std::string b_format = DataFormatter::FormatBlock(
|
2022-05-27 16:08:24 +02:00
|
|
|
b_initial,
|
|
|
|
formatUnderTest
|
|
|
|
);
|
|
|
|
|
|
|
|
// Convert back to a block
|
2022-05-27 16:44:42 +02:00
|
|
|
const Block b_retrieved = DataFormatter::DecodeFormat(
|
2022-05-27 16:08:24 +02:00
|
|
|
b_format,
|
|
|
|
formatUnderTest
|
|
|
|
);
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
// Do b_initial and b_retrieved match?
|
|
|
|
REQUIRE(b_initial == b_retrieved);
|
|
|
|
}
|
|
|
|
|