mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-27 19:39:02 +02:00
test: merge xor_roundtrip_random_chunks and xor_bytes_reference
Instead of a separate roundtrip test and a simplified xor reference test, we can merge the two and provide the same coverage See: https://github.com/bitcoin/bitcoin/pull/31144#discussion_r2211205949 Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
This commit is contained in:
@@ -18,8 +18,9 @@ using namespace util::hex_literals;
|
||||
|
||||
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)
|
||||
// Check optimized obfuscation with random offsets and sizes to ensure proper
|
||||
// handling of key wrapping. Also verify it roundtrips.
|
||||
BOOST_AUTO_TEST_CASE(xor_random_chunks)
|
||||
{
|
||||
auto apply_random_xor_chunks{[&](std::span<std::byte> target, const Obfuscation& obfuscation) {
|
||||
for (size_t offset{0}; offset < target.size();) {
|
||||
@@ -37,41 +38,14 @@ BOOST_AUTO_TEST_CASE(xor_roundtrip_random_chunks)
|
||||
const auto key_bytes{m_rng.randbool() ? m_rng.randbytes<Obfuscation::KEY_SIZE>() : std::array<std::byte, Obfuscation::KEY_SIZE>{}};
|
||||
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);
|
||||
BOOST_CHECK_EQUAL(roundtrip.size(), original.size());
|
||||
for (size_t i{0}; i < original.size(); ++i) {
|
||||
BOOST_CHECK_EQUAL(roundtrip[i], original[i] ^ key_bytes[i % Obfuscation::KEY_SIZE]);
|
||||
}
|
||||
|
||||
apply_random_xor_chunks(roundtrip, obfuscation);
|
||||
BOOST_CHECK(original == roundtrip);
|
||||
}
|
||||
}
|
||||
|
||||
// Compares optimized obfuscation against a trivial, byte-by-byte reference implementation
|
||||
// with random offsets to ensure proper handling of key wrapping.
|
||||
BOOST_AUTO_TEST_CASE(xor_bytes_reference)
|
||||
{
|
||||
auto expected_xor{[](std::span<std::byte> target, std::span<const std::byte, Obfuscation::KEY_SIZE> obfuscation, size_t key_offset) {
|
||||
for (auto& b : target) {
|
||||
b ^= obfuscation[key_offset++ % obfuscation.size()];
|
||||
}
|
||||
}};
|
||||
|
||||
for (size_t test{0}; test < 100; ++test) {
|
||||
const size_t write_size{1 + m_rng.randrange(100U)};
|
||||
const size_t key_offset{m_rng.randrange(3 * Obfuscation::KEY_SIZE)}; // Make sure the key can wrap around
|
||||
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 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(std::span{actual}.subspan(write_offset), key_offset);
|
||||
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end());
|
||||
}
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(roundtrip.begin(), roundtrip.end(), original.begin(), original.end());
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(obfuscation_hexkey)
|
||||
|
Reference in New Issue
Block a user