Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
751BOOST_CHECK_EQUAL_COLLECTIONS(752direct_result.begin(), direct_result.end(),753buffered_result.begin(), buffered_result.end()754);756fs::remove(test_direct.FileName(pos));757fs::remove(test_buffered.FileName(pos));758}760BOOST_AUTO_TEST_CASE(buffered_writer_reader)761{762const uint32_t v1{m_rng.rand32()}, v2{m_rng.rand32()}, v3{m_rng.rand32()};763const fs::path test_file{m_args.GetDataDirBase() / "test_buffered_write_read.bin"};765// Write out the values through a precisely sized BufferedWriter766AutoFile file{fsbridge::fopen(test_file, "w+b")};767{768BufferedWriter f(file, sizeof(v1) + sizeof(v2) + sizeof(v3));769f << v1 << v2;770f.write(std::as_bytes(std::span{&v3, 1}));771}772BOOST_REQUIRE_EQUAL(file.fclose(), 0);774// Read back and verify using BufferedReader775{776uint32_t _v1{0}, _v2{0}, _v3{0};777AutoFile file{fsbridge::fopen(test_file, "rb")};778BufferedReader f(std::move(file), sizeof(v1) + sizeof(v2) + sizeof(v3));779f >> _v1 >> _v2;780f.read(std::as_writable_bytes(std::span{&_v3, 1}));781BOOST_CHECK_EQUAL(_v1, v1);782BOOST_CHECK_EQUAL(_v2, v2);783BOOST_CHECK_EQUAL(_v3, v3);785DataBuffer excess_byte{1};786BOOST_CHECK_EXCEPTION(f.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});787}789fs::remove(test_file);790}792BOOST_AUTO_TEST_CASE(streams_hashed)793{794DataStream stream{};795HashedSourceWriter hash_writer{stream};796const std::string data{"bitcoin"};797hash_writer << data;799HashVerifier hash_verifier{stream};800std::string result;801hash_verifier >> result;802BOOST_CHECK_EQUAL(data, result);803BOOST_CHECK_EQUAL(hash_writer.GetHash(), hash_verifier.GetHash());804}806BOOST_AUTO_TEST_CASE(size_preserves_position)807{808const fs::path path = m_args.GetDataDirBase() / "size_pos_test.bin";809AutoFile f{fsbridge::fopen(path, "w+b")};810for (uint8_t j = 0; j < 10; ++j) {811f << j;812}814// Test that usage of size() does not change the current position815//816// Case: Pos at beginning of the file817f.seek(0, SEEK_SET);818(void)f.size();819uint8_t first{};820f >> first;821BOOST_CHECK_EQUAL(first, 0);823// Case: Pos at middle of the file824f.seek(0, SEEK_SET);825// Move pos to middle826f.ignore(4);827(void)f.size();828uint8_t middle{};829f >> middle;830// Pos still at 4831BOOST_CHECK_EQUAL(middle, 4);833// Case: Pos at EOF834f.seek(0, SEEK_END);835(void)f.size();836uint8_t end{};837BOOST_CHECK_EXCEPTION(f >> end, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});839BOOST_REQUIRE_EQUAL(f.fclose(), 0);840fs::remove(path);841}843BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.