refactor: encapsulate vector/array keys into Obfuscation

This commit is contained in:
Lőrinc
2025-07-05 14:39:19 +02:00
parent 377aab8e5a
commit 478d40afc6
13 changed files with 136 additions and 85 deletions

View File

@@ -21,10 +21,10 @@ BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup)
// Test that obfuscation can be properly reverted even with random chunk sizes.
BOOST_AUTO_TEST_CASE(xor_roundtrip_random_chunks)
{
auto apply_random_xor_chunks{[&](std::span<std::byte> target, std::span<const std::byte, Obfuscation::KEY_SIZE> obfuscation) {
auto apply_random_xor_chunks{[&](std::span<std::byte> target, const Obfuscation& obfuscation) {
for (size_t offset{0}; offset < target.size();) {
const size_t chunk_size{1 + m_rng.randrange(target.size() - offset)};
Obfuscation().Xor(target.subspan(offset, chunk_size), obfuscation, offset);
obfuscation(target.subspan(offset, chunk_size), offset);
offset += chunk_size;
}
}};
@@ -35,13 +35,14 @@ BOOST_AUTO_TEST_CASE(xor_roundtrip_random_chunks)
std::vector roundtrip{original};
const auto key_bytes{m_rng.randbool() ? m_rng.randbytes<Obfuscation::KEY_SIZE>() : std::array<std::byte, Obfuscation::KEY_SIZE>{}};
apply_random_xor_chunks(roundtrip, key_bytes);
const Obfuscation obfuscation{key_bytes};
apply_random_xor_chunks(roundtrip, obfuscation);
const bool key_all_zeros{std::ranges::all_of(
std::span{key_bytes}.first(std::min(write_size, Obfuscation::KEY_SIZE)), [](auto b) { return b == std::byte{0}; })};
BOOST_CHECK(key_all_zeros ? original == roundtrip : original != roundtrip);
apply_random_xor_chunks(roundtrip, key_bytes);
apply_random_xor_chunks(roundtrip, obfuscation);
BOOST_CHECK(original == roundtrip);
}
}
@@ -62,24 +63,58 @@ BOOST_AUTO_TEST_CASE(xor_bytes_reference)
const size_t write_offset{std::min(write_size, m_rng.randrange(Obfuscation::KEY_SIZE * 2))}; // Write unaligned data
const auto key_bytes{m_rng.randbool() ? m_rng.randbytes<Obfuscation::KEY_SIZE>() : std::array<std::byte, Obfuscation::KEY_SIZE>{}};
const std::vector obfuscation{key_bytes.begin(), key_bytes.end()};
const Obfuscation obfuscation{key_bytes};
std::vector expected{m_rng.randbytes<std::byte>(write_size)};
std::vector actual{expected};
expected_xor(std::span{expected}.subspan(write_offset), key_bytes, key_offset);
Obfuscation().Xor(std::span{actual}.subspan(write_offset), key_bytes, key_offset);
obfuscation(std::span{actual}.subspan(write_offset), key_offset);
BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end());
}
}
BOOST_AUTO_TEST_CASE(obfuscation_hexkey)
{
const auto key_bytes{m_rng.randbytes<Obfuscation::KEY_SIZE>()};
const Obfuscation obfuscation{key_bytes};
BOOST_CHECK_EQUAL(obfuscation.HexKey(), HexStr(key_bytes));
}
BOOST_AUTO_TEST_CASE(obfuscation_serialize)
{
const Obfuscation original{m_rng.randbytes<Obfuscation::KEY_SIZE>()};
// Serialization
DataStream ds;
ds << original;
BOOST_CHECK_EQUAL(ds.size(), 1 + Obfuscation::KEY_SIZE); // serialized as a vector
// Deserialization
Obfuscation recovered{};
ds >> recovered;
BOOST_CHECK_EQUAL(recovered.HexKey(), original.HexKey());
}
BOOST_AUTO_TEST_CASE(obfuscation_empty)
{
const Obfuscation null_obf{};
BOOST_CHECK(!null_obf);
const Obfuscation non_null_obf{"ff00ff00ff00ff00"_hex};
BOOST_CHECK(non_null_obf);
}
BOOST_AUTO_TEST_CASE(xor_file)
{
fs::path xor_path{m_args.GetDataDirBase() / "test_xor.bin"};
auto raw_file{[&](const auto& mode) { return fsbridge::fopen(xor_path, mode); }};
const std::vector<uint8_t> test1{1, 2, 3};
const std::vector<uint8_t> test2{4, 5};
const auto obfuscation{"ff00ff00ff00ff00"_hex_v};
const Obfuscation obfuscation{"ff00ff00ff00ff00"_hex};
{
// Check errors for missing file
@@ -284,23 +319,23 @@ BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
// Degenerate case
{
DataStream ds{};
ds.Xor("0000000000000000"_hex_v_u8);
Obfuscation{}(ds);
BOOST_CHECK_EQUAL(""s, ds.str());
}
{
const auto obfuscation{"ffffffffffffffff"_hex_v_u8};
const Obfuscation obfuscation{"ffffffffffffffff"_hex};
DataStream ds{"0ff0"_hex};
ds.Xor(obfuscation);
obfuscation(ds);
BOOST_CHECK_EQUAL("\xf0\x0f"s, ds.str());
}
{
const auto obfuscation{"ff0fff0fff0fff0f"_hex_v_u8};
const Obfuscation obfuscation{"ff0fff0fff0fff0f"_hex};
DataStream ds{"f00f"_hex};
ds.Xor(obfuscation);
obfuscation(ds);
BOOST_CHECK_EQUAL("\x0f\x00"s, ds.str());
}
}
@@ -613,7 +648,7 @@ BOOST_AUTO_TEST_CASE(buffered_reader_matches_autofile_random_content)
const FlatFilePos pos{0, 0};
const FlatFileSeq test_file{m_args.GetDataDirBase(), "buffered_file_test_random", node::BLOCKFILE_CHUNK_SIZE};
const auto obfuscation{m_rng.randbytes<std::byte>(Obfuscation::KEY_SIZE)};
const Obfuscation obfuscation{m_rng.randbytes<Obfuscation::KEY_SIZE>()};
// Write out the file with random content
{
@@ -668,7 +703,7 @@ BOOST_AUTO_TEST_CASE(buffered_writer_matches_autofile_random_content)
const FlatFileSeq test_buffered{m_args.GetDataDirBase(), "buffered_write_test", node::BLOCKFILE_CHUNK_SIZE};
const FlatFileSeq test_direct{m_args.GetDataDirBase(), "direct_write_test", node::BLOCKFILE_CHUNK_SIZE};
const auto obfuscation{m_rng.randbytes<std::byte>(Obfuscation::KEY_SIZE)};
const Obfuscation obfuscation{m_rng.randbytes<Obfuscation::KEY_SIZE>()};
{
DataBuffer test_data{m_rng.randbytes<std::byte>(file_size)};