From a17d8202c36abf8a17fb8736e05f318422a3c7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Tue, 22 Jul 2025 09:39:21 -0700 Subject: [PATCH] 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 --- src/test/streams_tests.cpp | 44 ++++++++------------------------------ 1 file changed, 9 insertions(+), 35 deletions(-) diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp index ce496df5a9..44aaf4d177 100644 --- a/src/test/streams_tests.cpp +++ b/src/test/streams_tests.cpp @@ -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 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() : std::array{}}; 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 target, std::span 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() : std::array{}}; - const Obfuscation obfuscation{key_bytes}; - std::vector expected{m_rng.randbytes(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)