test: Add better coverage for Autofile size()
What changed, and why it matters
This commit only adds a new test to Bitcoin Core. It checks that calling size() on an AutoFile object reports the file size without moving the file's internal read/write position. There is no change to production code and no security fix or vulnerability is present in the diff.
No security action needed. Review as ordinary test-quality improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds a BOOST_AUTO_TEST_CASE named size_preserves_position in src/test/streams_tests.cpp. The test creates a temporary file, writes 10 bytes, then verifies that invoking AutoFile::size() leaves the file cursor unchanged at the beginning, middle, and end of the file. It is purely additive test coverage with no modifications to src/streams.h, src/streams.cpp, or any other runtime code.
Changed components
src/test/streams_tests.cppInspect captured patch +37 / −0
diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp
index 6b9d2b35..9633a79c 100644
--- a/src/test/streams_tests.cpp
+++ b/src/test/streams_tests.cpp
@@ -807,4 +807,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()
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.