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.
1// Copyright (c) 2011-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.4//5#include <test/util/setup_common.h>6#include <util/fs.h>7#include <util/fs_helpers.h>9#include <boost/test/unit_test.hpp>11#include <fstream>12#include <ios>13#include <string>15BOOST_FIXTURE_TEST_SUITE(fs_tests, BasicTestingSetup)17BOOST_AUTO_TEST_CASE(fsbridge_pathtostring)18{19std::string u8_str = "fs_tests_₿_🏃";20std::u8string str8{u8"fs_tests_₿_🏃"};21BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(u8_str)), u8_str);22BOOST_CHECK_EQUAL(fs::u8path(u8_str).utf8string(), u8_str);23BOOST_CHECK_EQUAL(fs::path(str8).utf8string(), u8_str);24BOOST_CHECK(fs::path(str8).u8string() == str8);25BOOST_CHECK_EQUAL(fs::PathFromString(u8_str).utf8string(), u8_str);26BOOST_CHECK_EQUAL(fs::PathToString(fs::u8path(u8_str)), u8_str);27#ifndef WIN3228// On non-windows systems, verify that arbitrary byte strings containing29// invalid UTF-8 can be round tripped successfully with PathToString and30// PathFromString. On non-windows systems, paths are just byte strings so31// these functions do not do any encoding. On windows, paths are Unicode,32// and these functions do encoding and decoding, so the behavior of this33// test would be undefined.34std::string invalid_u8_str = "\xf0";35BOOST_CHECK_EQUAL(invalid_u8_str.size(), 1);36BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(invalid_u8_str)), invalid_u8_str);37#endif38}40BOOST_AUTO_TEST_CASE(fsbridge_stem)41{42std::string test_filename = "fs_tests_₿_🏃.dat";43std::string expected_stem = "fs_tests_₿_🏃";44BOOST_CHECK_EQUAL(fs::PathToString(fs::PathFromString(test_filename).stem()), expected_stem);45}47BOOST_AUTO_TEST_CASE(fsbridge_fstream)48{49fs::path tmpfolder = m_args.GetDataDirBase();50// tmpfile1 should be the same as tmpfile251fs::path tmpfile1 = tmpfolder / fs::u8path("fs_tests_₿_🏃");52fs::path tmpfile2 = tmpfolder / fs::path(u8"fs_tests_₿_🏃");53{54std::ofstream file{tmpfile1.std_path()};55file << "bitcoin";56}57{58std::ifstream file{tmpfile2.std_path()};59std::string input_buffer;60file >> input_buffer;61BOOST_CHECK_EQUAL(input_buffer, "bitcoin");62}63{64std::ifstream file{tmpfile1.std_path(), std::ios_base::in | std::ios_base::ate};65std::string input_buffer;66file >> input_buffer;67BOOST_CHECK_EQUAL(input_buffer, "");68}69{70std::ofstream file{tmpfile2.std_path(), std::ios_base::out | std::ios_base::app};71file << "tests";72}73{74std::ifstream file{tmpfile1.std_path()};75std::string input_buffer;76file >> input_buffer;77BOOST_CHECK_EQUAL(input_buffer, "bitcointests");78}79{80std::ofstream file{tmpfile2.std_path(), std::ios_base::out | std::ios_base::trunc};81file << "bitcoin";82}83{84std::ifstream file{tmpfile1.std_path()};85std::string input_buffer;86file >> input_buffer;87BOOST_CHECK_EQUAL(input_buffer, "bitcoin");88}89{90// Join an absolute path and a relative path.91fs::path p = fsbridge::AbsPathJoin(tmpfolder, fs::u8path("fs_tests_₿_🏃"));92BOOST_CHECK(p.is_absolute());93BOOST_CHECK_EQUAL(tmpfile1, p);94}95{96// Join two absolute paths.97fs::path p = fsbridge::AbsPathJoin(tmpfile1, tmpfile2);98BOOST_CHECK(p.is_absolute());99BOOST_CHECK_EQUAL(tmpfile2, p);100}101{102// Ensure joining with empty paths does not add trailing path components.103BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, ""));104BOOST_CHECK_EQUAL(tmpfile1, fsbridge::AbsPathJoin(tmpfile1, {}));105}106}108BOOST_AUTO_TEST_CASE(rename)109{110const fs::path tmpfolder{m_args.GetDataDirBase()};112const fs::path path1{tmpfolder / "a"};113const fs::path path2{tmpfolder / "b"};115const std::string path1_contents{"1111"};116const std::string path2_contents{"2222"};118{119std::ofstream file{path1.std_path()};120file << path1_contents;121}123{124std::ofstream file{path2.std_path()};125file << path2_contents;126}128// Rename path1 -> path2.129BOOST_CHECK(RenameOver(path1, path2));131BOOST_CHECK(!fs::exists(path1));133{134std::ifstream file{path2.std_path()};135std::string contents;136file >> contents;137BOOST_CHECK_EQUAL(contents, path1_contents);138}139fs::remove(path2);140}142BOOST_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.