Merge bitcoin/bitcoin#33026: test, refactor: Embedded ASMap [1/3]: Selected minor preparatory work

7f318e1dd0 test: Add better coverage for Autofile size() (Fabian Jahr)
b7af960eb8 refactor: Add AutoFile::size (Fabian Jahr)
ec0f75862e refactor: Modernize logging in util/asmap.cpp (Fabian Jahr)
606a251e0a tests: add unit test vectors for asmap interpreter (Pieter Wuille)

Pull request description:

  This contains some commits from #28792 that can be easily reviewed and merged independently. I hope splitting this change off can make this part move a bit faster and reduce frequency of needed rebases for #28792.

  The commits in order:
  - Add additional unit test vectors to the asmap interpreter (written by sipa). This helps to ensure that the further refactors in #28792 don't change behavior.
  - Modernizes the logging in `util/asmap.cpp`, I added this while touching the rest of the file all over anyway.
  - Adds an `AutoFile::size` helper function with some additional test coverage in a separate commit

ACKs for top commit:
  maflcko:
    review ACK 7f318e1dd0 🏀
  hodlinator:
    tACK 7f318e1dd0
  laanwj:
    Code review ACK 7f318e1dd0

Tree-SHA512: 45156b74e4bd9278a7ec24521dfdafe4dab1ba3384243c7d589ef17e16ca374ee2af7178c86b7229e80ca262dbe78c4d456d80a6ee742ec31d2ab5243dac8b57
This commit is contained in:
merge-script
2025-11-19 09:28:44 +00:00
6 changed files with 116 additions and 8 deletions

View File

@@ -101,6 +101,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
BOOST_CHECK_EXCEPTION(xor_file << std::byte{}, std::ios_base::failure, HasReason{"AutoFile::write: file handle is nullptr"});
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: file handle is nullptr"});
BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: file handle is nullptr"});
BOOST_CHECK_EXCEPTION(xor_file.size(), std::ios_base::failure, HasReason{"AutoFile::size: file handle is nullptr"});
}
{
#ifdef __MINGW64__
@@ -111,6 +112,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
#endif
AutoFile xor_file{raw_file(mode), obfuscation};
xor_file << test1 << test2;
BOOST_CHECK_EQUAL(xor_file.size(), 7);
BOOST_REQUIRE_EQUAL(xor_file.fclose(), 0);
}
{
@@ -121,6 +123,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
BOOST_CHECK_EQUAL(HexStr(raw), "fc01fd03fd04fa");
// Check that no padding exists
BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
BOOST_CHECK_EQUAL(non_xor_file.size(), 7);
}
{
AutoFile xor_file{raw_file("rb"), obfuscation};
@@ -130,6 +133,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
// Check that eof was reached
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
BOOST_CHECK_EQUAL(xor_file.size(), 7);
}
{
AutoFile xor_file{raw_file("rb"), obfuscation};
@@ -141,6 +145,7 @@ BOOST_AUTO_TEST_CASE(xor_file)
// Check that ignore and read fail now
BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
BOOST_CHECK_EQUAL(xor_file.size(), 7);
}
}
@@ -781,4 +786,41 @@ BOOST_AUTO_TEST_CASE(streams_hashed)
BOOST_CHECK_EQUAL(hash_writer.GetHash(), hash_verifier.GetHash());
}
BOOST_AUTO_TEST_CASE(size_preserves_position)
{
const fs::path path = m_args.GetDataDirBase() / "size_pos_test.bin";
AutoFile f{fsbridge::fopen(path, "w+b")};
for (uint8_t j = 0; j < 10; ++j) {
f << j;
}
// Test that usage of size() does not change the current position
//
// Case: Pos at beginning of the file
f.seek(0, SEEK_SET);
(void)f.size();
uint8_t first{};
f >> first;
BOOST_CHECK_EQUAL(first, 0);
// Case: Pos at middle of the file
f.seek(0, SEEK_SET);
// Move pos to middle
f.ignore(4);
(void)f.size();
uint8_t middle{};
f >> middle;
// Pos still at 4
BOOST_CHECK_EQUAL(middle, 4);
// Case: Pos at EOF
f.seek(0, SEEK_END);
(void)f.size();
uint8_t end{};
BOOST_CHECK_EXCEPTION(f >> end, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
BOOST_REQUIRE_EQUAL(f.fclose(), 0);
fs::remove(path);
}
BOOST_AUTO_TEST_SUITE_END()